tempCString

Temporary string buffer. It can be used to build temporary \0 ended C strings. For lengths < 255, it uses static char array, mallocated buffer otherwise.

NOTE: be careful that pointer becomes invalid as soon as the struct comes out of scope! NOTE: inspired by std.internal.cstring.TempCStringBuffer in Phobos library

  1. struct TempCString(C)
  2. auto tempCString(S str)
    nothrow @nogc
    tempCString
    (
    C = char
    S
    )
    (
    scope S str
    )

Examples

import core.stdc.string : strlen;

string str = "abc";

// Intended usage
assert(strlen(str.tempCString()) == 3);

// Correct usage
auto tmp = str.tempCString();
assert(strlen(tmp) == 3); // or `tmp.ptr`, or `tmp.buffPtr`

// $(RED WARNING): $(RED Incorrect usage)
auto pInvalid1 = str.tempCString().ptr;
const char* pInvalid2 = str.tempCString();

Meta