Dstrings

SDK Defold String Utils API documentation

Namespace: DStrings
Include: #include <dmsdk/dlib/dstrings.h>
FUNCTIONS
 dmSnPrintf(char* buffer, size_t count, const char* format) Size-bounded string formating.
 dmStrCaseCmp(const char* s1, const char* s2) Case-insensitive string comparison
 dmStrError(char* dst, size_t size) Error code to string representation
 dmStrlCat(char* dst, char* src, size_t size) Size-bounded string concatenation.
 dmStrlCpy(char* dst, const char* src, size_t size) Size-bounded string copying.
 dmStrTok(char* string, const char* delim, char** lasts) Tokenize strings.

Functions

dmSnPrintf

 dmSnPrintf(char* buffer, size_t count, const char* format)

Size-bounded string formating. Resulting string is guaranteed to be 0-terminated.

PARAMETERS

char* buffer Buffer to write to
size_t count Size of the buffer
const char* format String format

RETURNS

of the resulting string (excl terminating 0) if it fits, -1 otherwise

EXAMPLES

char path[64];
dmSnPrintf(path, 64, PATH_FORMAT, filename);

dmStrCaseCmp

 dmStrCaseCmp(const char* s1, const char* s2)

Case-insensitive string comparison

PARAMETERS

const char* s1 First string to compare
const char* s2 Second string to compare

RETURNS

integer greater than, equal to, or less than 0 after lexicographically comparison of s1 and s2

EXAMPLES

dmStrCaseCmp("a", "b"); // <0
dmStrCaseCmp("b", "a"); // >0
dmStrCaseCmp("a", "a"); // 0

dmStrError

 dmStrError(char* dst, size_t size)

Error code to string representation. Wrapper for thread-safe strerror_s/r variants. If the size of the buffer is too small, the message will be truncated to fit the buffer. If the buffer is null, or if size is zero, nothing will happen.

PARAMETERS

char* dst Destination string that carries the error message
size_t size Max size of destination string in bytes

RETURNS

null-terminated error message

EXAMPLES

char buf[128];
dmStrError(buf, sizeof(buf), ENOENT); // buf => "No such file or directory"

dmStrlCat

 dmStrlCat(char* dst, char* src, size_t size)

Size-bounded string concatenation. Same as OpenBSD 2.4 strlcat. Appends src to string dst of size siz (unlike strncat, siz is the full size of dst, not space left). At most siz-1 characters will be copied. Always NUL terminates (unless siz == 0). Returns strlen(dst) + strlen(src); if retval >= siz, truncation occurred.

PARAMETERS

char* dst Destination string
char* src Source string
size_t size Max size

RETURNS

length of the created string

EXAMPLES

const char* src = "foo";
char dst[3] = { 0 };
dmStrlCat(dst, src, sizeof(dst)); // dst = "fo"

dmStrlCpy

 dmStrlCpy(char* dst, const char* src, size_t size)

Size-bounded string copying. Same as OpenBSD 2.4 strlcpy. Copy src to string dst of size siz. At most siz-1 characters will be copied. Always NUL terminates (unless siz == 0).Returns strlen(src); if retval >= siz, truncation occurred.

PARAMETERS

char* dst Destination string
const char* src Source string
size_t size Max size

RETURNS

length of the created string

EXAMPLES

const char* src = "foo";
char dst[3];
dmStrlCpy(dst, src, sizeof(dst)); // dst = "fo"

dmStrTok

 dmStrTok(char* string, const char* delim, char** lasts)

Tokenize strings. Equivalent to BSD strsep_r. Thread-save version of strtok.

PARAMETERS

char* string Pointer to string. For the first call string is the string to tokenize. Subsequent should pass NULL.
const char* delim Delimiter string
char** lasts Internal state pointer

RETURNS

call to dmStrTok() returns a pointer to a null-terminated string containing the next token. This string does not include the delimiting byte. If no more tokens are found, dmStrTok() returns NULL

EXAMPLES

char* string = strdup("a b c");
char* s, *last;
s = dmStrTok(string, " ", &last); // a
s = dmStrTok(0, " ", &last); // b
s = dmStrTok(0, " ", &last); // c