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.

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

Parameters

literal
Type: const(char)[N]

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)

Examples

1 auto m = "123".toStaticArray;
2 const c = "123".toStaticArray;
3 immutable i = "123".toStaticArray;
4 enum e = "123".toStaticArray;
5 
6 assert(m == "123\0");
7 assert(c == "123\0");
8 assert(i == "123\0");
9 static assert(e == "123\0");
10 
11 const empty = "".toStaticArray;
12 static assert(empty.length == 1);
13 static assert(empty[0] == '\0');

Meta