Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.


Small String

The module contains self-contained generic small string implementaton.
SmallString supports ASDF - Json Serialisation Library.
See also include/mir/small_series.h for a C++ version of this type. Both C++ and D implementations have the same ABI and name mangling.
Ilya Yaroshenko
struct SmallString(uint maxLength) if (maxLength);
Self-contained generic Small String implementaton.
SmallString!16 s16;

auto s8 = SmallString!8("Hellow!!");
assert(s8 == "Hellow!!", s8[]);

s16 = s8;
assert(s16 == "Hellow!!", s16[]);
s16[7] = '@';
s8 = null;
s8 = s16;
assert(s8 == "Hellow!@");

auto s8_2 = s8;
assert(s8_2 == "Hellow!@");
assert(s8_2 == s8);

assert(s8 < "Hey");
assert(s8 > "Hellow!");

assert(s8.opCmp("Hey") < 0);
assert(s8.opCmp(s8) == 0);
auto a = SmallString!16("asdf");
a ~= " ";
auto b = a ~ "qwerty";
static assert(is(typeof(b) == SmallString!16));
assert(b == "asdf qwerty");
assert(b == "asdf qwerty!!");

this(scope const(char)[] str);

this(uint n)(auto ref scope const SmallString!n str);

this(Range)(auto ref Range str)
if (isIterable!Range);
ref typeof(this) opAssign(typeof(null)) return;

ref @trusted typeof(this) opAssign(scope const(char)[] str) return;

ref typeof(this) opAssign(uint n)(auto ref scope const SmallString!n rhs) return
if (n != maxLength);

ref typeof(this) opAssign(uint n)(const SmallString!n rhs) return
if (n != maxLength);

nothrow @trusted void trustedAssign(scope const(char)[] str) return;
= operator
ref @trusted typeof(this) append(char c);
ref @trusted typeof(this) append(scope const(char)[] str);

alias put = append;

template opOpAssign(string op : "~")
const scope SmallString concat(scope const(char)[] str);

template opBinary(string op : "~")
inout scope @trusted inout(char)[] opIndex() return;
Returns an scope common string.
The property is used as common string representation self alias.
The alias helps with [], [i], [i .. j], ==, and != operations and implicit conversion to strings.
inout ref scope inout(char) opIndex(size_t index) return;
@property bool empty();
@property size_t length();
alias toString = opIndex;
size_t toHash();
Hash implementation
scope bool opEquals(ref scope const SmallString rhs);

scope bool opEquals(SmallString rhs);

scope bool opEquals(uint rhsMaxLength)(auto ref scope const SmallString!rhsMaxLength rhs)
if (rhsMaxLength != maxLength);

scope bool opEquals()(scope const(char)[] str);

scope int opCmp(uint rhsMaxLength)(auto ref scope const SmallString!rhsMaxLength rhs);

scope int opCmp()(scope const(char)[] str);
Comparisons operator overloads