RCXString

Reference Counted Array. Configured with character type E, maximum length for the small string optimization, and the allocation function, which must have the same semantics as realloc.

Constructors

this
this(C[] s)

Construct a RCXString from a slice s.

Destructor

A destructor is present on this object, but not explicitly documented in the source.

Postblit

A postblit is present on this object, but not explicitly documented in the source.

Members

Aliases

opDollar
alias opDollar = length

Returns the length of the string

Functions

back
dchar back()

Returns the last code point of this.

capacity
size_t capacity()

Returns the maximum number of character this string can store without requesting more memory.

empty
bool empty()

Returns true iff this is empty

front
auto front()

Returns the first code point of this.

length
size_t length()

Returns the length of the string

opBinary
RCXString opBinary(RCXString s)
RCXString opBinary(const(ME)[] s)

Returns the concatenation of this with s.

opBinaryRight
RCXString opBinaryRight(const(E)[] s)

Returns the concatenation of s with this.

opIndex
E opIndex(size_t n)

Returns the nth code unit in this.

opOpAssign
void opOpAssign(const(ME)[] s)
void opOpAssign(RCXString s)

Appends s to this.

opSlice
auto opSlice()
auto opSlice(size_t b, size_t e)

Returns a slice to the entire string or a portion of it.

popBack
void popBack()

Discards the last code point

popFront
void popFront()

Discards the first code point

toArray
string toArray()

Needed for correct printing in other modules

Examples

verify UTF-8 storage

string s = "åäö";
RCString rcs = s;
assert(rcs.length == 6);
import std.algorithm : count;
assert(rcs.count == 3);
assert(rcs.front == 'å');
rcs.popFront();
assert(rcs.front == 'ä');
rcs.popFront();
assert(rcs.front == 'ö');
rcs.popFront();
assert(rcs.empty);

shows performance increase for SSO over built-in string

1 enum maxSmallSize = 23;
2 alias S = RCXString!(immutable char, maxSmallSize);
3 
4 import std.datetime: StopWatch, Duration;
5 import std.conv : to;
6 import std.stdio;
7 
8 enum n = 2^^21;
9 
10 StopWatch sw;
11 
12 sw.reset;
13 sw.start;
14 char[maxSmallSize] ss;
15 foreach (i; 0 .. n)
16 {
17     auto x = S(ss);
18 }
19 sw.stop;
20 auto timeRCString = sw.peek().msecs;
21 writeln("> RCString took ", sw.peek().to!Duration);
22 
23 sw.reset;
24 sw.start;
25 foreach (i; 0 .. n)
26 {
27     string x = ss.idup;
28 }
29 sw.stop;
30 auto timeString = sw.peek().msecs;
31 writeln("> Builtin string took ", sw.peek().to!Duration);
32 
33 writeln("> Speedup: ", timeString/timeRCString);

See Also

Meta