unstd.c.string

Helper functions for working with C strings.

This module is intended to provide fast, safe and garbage free way to work with C strings.

Members

Functions

moveToString
string moveToString(C* cstr)

Returns same as toString but also if cstr is not null releases its memory calling release(cast(void*) cstr).

tempCString
auto tempCString(From[] str)

Creates temporary C string with copy of passed text.

toCString
To* toCString(From[] str)

Creates C string allocated using tryAllocate with copy of str. If str is null returns null.

toString
string toString(C* cstr)

Creates GC-allocated string with copy of C string text. If cstr is null returns null, otherwise if cstr is empty returns "".

Properties

asArray
inout(C)[] asArray [@property getter]

Returns array representing C string where '\0' character is placed after the end of the array. If cstr is null returns null.

cmpCStrings
C* cmpCStrings [@property setter]

Compare C strings lexicographically.

equalCStrings
C* equalCStrings [@property setter]

Returns whether two C strings are equal.

length
size_t length [@property getter]

Returns C string length. If cstr is null returns 0.

Examples

version(Posix):

import core.stdc.stdlib: free;
import core.sys.posix.unistd: getcwd;
import core.sys.posix.stdlib: getenv, setenv;
import std.exception: enforce;

@property string cwd()
{ return enforce(getcwd(null, 0).moveToString!free()); }

string getEnvironment(in char[] name)
{ return enforce(getenv(name.tempCString()).toString()); }

void setEnvironment(in char[] name, in char[] value)
{ enforce(setenv(name.tempCString(), value.tempCString(), 1) != -1); }
version(Windows):

import core.sys.windows.windows: SetEnvironmentVariableW;
import std.exception: enforce;

void setEnvironment(in char[] name, in char[] value)
{ enforce(SetEnvironmentVariableW(name.tempCString!wchar(), value.tempCString!wchar())); }

Meta

Authors

Denis Shelomovskij