toStaticArray

* Infers the length N of a string literal and coerces its type to a static * array with length N + 1. Returns the string with a null character appended * to the end. * * Params: * literal = string literal * * Notes: * - LDC produces quite optimal code for short strings: * - https://d.godbolt.org/z/M69Z1g * - https://gist.github.com/PetarKirov/338e4ab9292b6b2b311a3070572a07fb (backup URL)

char[N + 1]
toStaticArray
(
size_t N
)
(
scope const(char)[N] literal
)

Examples

auto m = "123".toStaticArray;
const c = "123".toStaticArray;
immutable i = "123".toStaticArray;
enum e = "123".toStaticArray;

assert(m == "123\0");
assert(c == "123\0");
assert(i == "123\0");
static assert(e == "123\0");

const empty = "".toStaticArray;
static assert(empty.length == 1);
static assert(empty[0] == '\0');

Meta