dmd.root.string
Contains various string related functions.
License
Source: root/string.d
Documentation: https://dlang.org/phobos/dmd_root_string.html
-
Declaration
pure nothrow @nogc inout(char)[]toDString(inout(char)*s);Slices a
\0-terminated C-string, excluding the terminator -
Declaration
static booliequals(const(char)[]s1, const(char)[]s2);Compare two slices for equality, in a case-insensitive way
Discussion
Comparison is based on
charand does not do decoding. As a result, it's only really accurate for plain ASCII strings.Parameters
const(char)[]s1string to compare
const(char)[]s2string to compare
Return Value
trueifregardless of cases1==s2 -
Declaration
nothrow autotoCStringThen(alias dg)(const(char)[]src);Copy the content of
into a C-string ('\0' terminated) then callsrcdgDiscussion
The intent of this function is to provide an allocation-less way to call a C function using a D slice. The function internally allocates a buffer if needed, but frees it on exit.
Note: The argument to
dgisscope. To keep the data around afterdgexits, one has to copy it.Parameters
const(char)[]srcSlice to use to call the C function
dgDelegate to call afterwards
Return Value
The return value of
T -
Declaration
pure nothrow @nogc @safe stringstripLeadingLineTerminator(stringstr);Strips one leading line terminator of the given string.
Discussion
The following are what the Unicode standard considers as line terminators:
This function will also stripName D Escape Sequence Unicode Code Point Line feed \nU+000ALine tabulation \vU+000BForm feed \fU+000CCarriage return \rU+000DNext line U+0085Line separator U+2028Paragraph separator U+2029\r\n. -
Declaration
@trusted intdstrcmp()(scope const char[]s1, scope const char[]s2);A string comparison functions that returns the same result as strcmp
Note: Strings are compared based on their ASCII values, no UTF-8 decoding.
Some C functions (e.g.qsort) require aintresult for comparison.See Also
Druntime's
core.internal.string -
Declaration
char[N + 1]toStaticArray(size_t N)(scope const(char)[N]literal);Infers the length
Nof a stringliteraland coerces its type to a static array with lengthN + 1. Returns the string with anullcharacter appended to the end.Parameters
const(char)[N]literalstring
literalNotes:
- LDC produces quite optimal code for short strings:
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');
-
Declaration
pure nothrow @nogc @system boolstartsWith(scope const(char)*p, scope const(char)[]needle);Checks if C string
starts withp.needleParameters
const(char)*pthe C string to check
const(char)[]needlethe string to look for
Return Value
trueifstarts withpneedleExamples
const buf = "123".toStaticArray; const ptr = &buf[0]; assert(ptr.startsWith("")); assert(ptr.startsWith("1")); assert(ptr.startsWith("12")); assert(ptr.startsWith("123")); assert(!ptr.startsWith("1234"));