Struct String

GC-free UTF-8 string type based on dlib.container.array. Stores up to 128 bytes without dynamic memory allocation, so short strings are processed very fast. String is always zero-terminated and directly compatible with C.

struct String ;

Constructors

NameDescription
this (s) Construct from D string
this (wStr) Construct from zero-terminated UTF-16 LE string
this (cStr) Construct from zero-terminated C string (ASCII or UTF8)
this (istrm) Construct from an InputStream

Fields

NameTypeDescription
data Array!(char,128L)Underlying array of characters

Methods

NameDescription
decode () Range interface that iterates the string by Unicode code point (dchar), i.e., foreach(dchar c; str.decode)

Example

String s = "hello";
s ~= ", world";
s ~= '!';
assert(!s.isDynamic);
string dStr = s;
assert(dStr == "hello, world!");
s.free();
assert(s.length == 0);

const(char)* cStr = "Hello!";
String s2 = String(cStr);
assert(s2.toString == "Hello!");

import std.algorithm.comparison: equal;
assert(equal(s2.decode, ['H', 'e', 'l', 'l', 'o', '!']));

auto istrm = new ArrayStream([104, 101, 108, 108, 111]);
String s3 = String(istrm);
assert(s3.toString == "hello");