Str

Description This section describes a number of utility functions for creating, duplicating, and manipulating strings. Note that the functions g_printf(), g_fprintf(), g_sprintf(), g_snprintf(), g_vprintf(), g_vfprintf(), g_vsprintf() and g_vsnprintf() are declared in the header gprintf.h which is not included in glib.h (otherwise using glib.h would drag in stdio.h), so you'll have to explicitly include <glib/gprintf.h> in order to use the GLib printf() functions. While you may use the printf() functions to format UTF-8 strings, notice that the precision of a %Ns parameter is interpreted as the number of bytes, not characters to print. On top of that, the GNU libc implementation of the printf() functions has the "feature" that it checks that the string given for the %Ns parameter consists of a whole number of characters in the current encoding. So, unless you are sure you are always going to be in an UTF-8 locale or your know your text is restricted to ASCII, avoid using %Ns. If your intention is to format strings for a certain number of columns, then %Ns is not a correct solution anyway, since it fails to take wide characters (see g_unichar_iswide()) into account.

class Str {}

Members

Static functions

asciiDigitValue
int asciiDigitValue(char c)

Determines the numeric value of a character as a decimal digit. Differs from g_unichar_digit_value() because it takes a char, so there's no worry about sign extension if characters are signed.

asciiDtostr
string asciiDtostr(string buffer, int bufLen, double d)

Converts a gdouble to a string, using the '.' as decimal point. This functions generates enough precision that converting the string back using g_ascii_strtod() gives the same machine-number (on machines with IEEE compatible 64bit doubles). It is guaranteed that the size of the resulting string will never be larger than G_ASCII_DTOSTR_BUF_SIZE bytes.

asciiFormatd
string asciiFormatd(string buffer, int bufLen, string format, double d)

Converts a gdouble to a string, using the '.' as decimal point. To format the number you pass in a printf()-style format string. Allowed conversion specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'. If you just want to want to serialize the value into a string, use g_ascii_dtostr().

asciiIsalnum
int asciiIsalnum(char c)

Determines whether a character is alphanumeric. Unlike the standard C library isalnum() function, this only recognizes standard ASCII letters and ignores the locale, returning FALSE for all non-ASCII characters. Also unlike the standard library function, this takes a char, not an int, so don't call it on EOF but no need to cast to guchar before passing a possibly non-ASCII character in.

asciiIsalpha
int asciiIsalpha(char c)

Determines whether a character is alphabetic (i.e. a letter). Unlike the standard C library isalpha() function, this only recognizes standard ASCII letters and ignores the locale, returning FALSE for all non-ASCII characters. Also unlike the standard library function, this takes a char, not an int, so don't call it on EOF but no need to cast to guchar before passing a possibly non-ASCII character in.

asciiIscntrl
int asciiIscntrl(char c)

Determines whether a character is a control character. Unlike the standard C library iscntrl() function, this only recognizes standard ASCII control characters and ignores the locale, returning FALSE for all non-ASCII characters. Also unlike the standard library function, this takes a char, not an int, so don't call it on EOF but no need to cast to guchar before passing a possibly non-ASCII character in.

asciiIsdigit
int asciiIsdigit(char c)

Determines whether a character is digit (0-9). Unlike the standard C library isdigit() function, this takes a char, not an int, so don't call it on EOF but no need to cast to guchar before passing a possibly non-ASCII character in.

asciiIsgraph
int asciiIsgraph(char c)

Determines whether a character is a printing character and not a space. Unlike the standard C library isgraph() function, this only recognizes standard ASCII characters and ignores the locale, returning FALSE for all non-ASCII characters. Also unlike the standard library function, this takes a char, not an int, so don't call it on EOF but no need to cast to guchar before passing a possibly non-ASCII character in.

asciiIslower
int asciiIslower(char c)

Determines whether a character is an ASCII lower case letter. Unlike the standard C library islower() function, this only recognizes standard ASCII letters and ignores the locale, returning FALSE for all non-ASCII characters. Also unlike the standard library function, this takes a char, not an int, so don't call it on EOF but no need to worry about casting to guchar before passing a possibly non-ASCII character in.

asciiIsprint
int asciiIsprint(char c)

Determines whether a character is a printing character. Unlike the standard C library isprint() function, this only recognizes standard ASCII characters and ignores the locale, returning FALSE for all non-ASCII characters. Also unlike the standard library function, this takes a char, not an int, so don't call it on EOF but no need to cast to guchar before passing a possibly non-ASCII character in.

asciiIspunct
int asciiIspunct(char c)

Determines whether a character is a punctuation character. Unlike the standard C library ispunct() function, this only recognizes standard ASCII letters and ignores the locale, returning FALSE for all non-ASCII characters. Also unlike the standard library function, this takes a char, not an int, so don't call it on EOF but no need to cast to guchar before passing a possibly non-ASCII character in.

asciiIsspace
int asciiIsspace(char c)

Determines whether a character is a white-space character. Unlike the standard C library isspace() function, this only recognizes standard ASCII white-space and ignores the locale, returning FALSE for all non-ASCII characters. Also unlike the standard library function, this takes a char, not an int, so don't call it on EOF but no need to cast to guchar before passing a possibly non-ASCII character in.

asciiIsupper
int asciiIsupper(char c)

Determines whether a character is an ASCII upper case letter. Unlike the standard C library isupper() function, this only recognizes standard ASCII letters and ignores the locale, returning FALSE for all non-ASCII characters. Also unlike the standard library function, this takes a char, not an int, so don't call it on EOF but no need to worry about casting to guchar before passing a possibly non-ASCII character in.

asciiIsxdigit
int asciiIsxdigit(char c)

Determines whether a character is a hexadecimal-digit character. Unlike the standard C library isxdigit() function, this takes a char, not an int, so don't call it on EOF but no need to cast to guchar before passing a possibly non-ASCII character in.

asciiStrcasecmp
int asciiStrcasecmp(string s1, string s2)

Compare two strings, ignoring the case of ASCII characters. Unlike the BSD strcasecmp() function, this only recognizes standard ASCII letters and ignores the locale, treating all non-ASCII bytes as if they are not letters. This function should be used only on strings that are known to be in encodings where the bytes corresponding to ASCII letters always represent themselves. This includes UTF-8 and the ISO-8859-* charsets, but not for instance double-byte encodings like the Windows Codepage 932, where the trailing bytes of double-byte characters include all ASCII letters. If you compare two CP932 strings using this function, you will get false matches.

asciiStrdown
string asciiStrdown(string str, gssize len)

Converts all upper case ASCII letters to lower case ASCII letters.

asciiStrncasecmp
int asciiStrncasecmp(string s1, string s2, gsize n)

Compare s1 and s2, ignoring the case of ASCII characters and any characters after the first n in each string. Unlike the BSD strcasecmp() function, this only recognizes standard ASCII letters and ignores the locale, treating all non-ASCII characters as if they are not letters. The same warning as in g_ascii_strcasecmp() applies: Use this function only on strings known to be in encodings where bytes corresponding to ASCII letters always represent themselves.

asciiStrtod
double asciiStrtod(string nptr, string endptr)

Converts a string to a gdouble value. This function behaves like the standard strtod() function does in the C locale. It does this without actually changing the current locale, since that would not be thread-safe. A limitation of the implementation is that this function will still accept localized versions of infinities and NANs. This function is typically used when reading configuration files or other non-user input that should be locale independent. To handle input from the user you should normally use the locale-sensitive system strtod() function. To convert from a gdouble to a string in a locale-insensitive way, use g_ascii_dtostr(). If the correct value would cause overflow, plus or minus HUGE_VAL is returned (according to the sign of the value), and ERANGE is stored in errno. If the correct value would cause underflow, zero is returned and ERANGE is stored in errno. This function resets errno before calling strtod() so that you can reliably detect overflow and underflow.

asciiStrtoll
long asciiStrtoll(string nptr, string endptr, uint base)

Converts a string to a gint64 value. This function behaves like the standard strtoll() function does in the C locale. It does this without actually changing the current locale, since that would not be thread-safe. This function is typically used when reading configuration files or other non-user input that should be locale independent. To handle input from the user you should normally use the locale-sensitive system strtoll() function. If the correct value would cause overflow, G_MAXINT64 or G_MININT64 is returned, and ERANGE is stored in errno. If the base is outside the valid range, zero is returned, and EINVAL is stored in errno. If the string conversion fails, zero is returned, and endptr returns nptr (if endptr is non-NULL). Since 2.12

asciiStrtoull
ulong asciiStrtoull(string nptr, string endptr, uint base)

Converts a string to a guint64 value. This function behaves like the standard strtoull() function does in the C locale. It does this without actually changing the current locale, since that would not be thread-safe. This function is typically used when reading configuration files or other non-user input that should be locale independent. To handle input from the user you should normally use the locale-sensitive system strtoull() function. If the correct value would cause overflow, G_MAXUINT64 is returned, and ERANGE is stored in errno. If the base is outside the valid range, zero is returned, and EINVAL is stored in errno. If the string conversion fails, zero is returned, and endptr returns nptr (if endptr is non-NULL). Since 2.2

asciiStrup
string asciiStrup(string str, gssize len)

Converts all lower case ASCII letters to upper case ASCII letters.

asciiTolower
char asciiTolower(char c)

Convert a character to ASCII lower case. Unlike the standard C library tolower() function, this only recognizes standard ASCII letters and ignores the locale, returning all non-ASCII characters unchanged, even if they are lower case letters in a particular character set. Also unlike the standard library function, this takes and returns a char, not an int, so don't call it on EOF but no need to worry about casting to guchar before passing a possibly non-ASCII character in.

asciiToupper
char asciiToupper(char c)

Convert a character to ASCII upper case. Unlike the standard C library toupper() function, this only recognizes standard ASCII letters and ignores the locale, returning all non-ASCII characters unchanged, even if they are upper case letters in a particular character set. Also unlike the standard library function, this takes and returns a char, not an int, so don't call it on EOF but no need to worry about casting to guchar before passing a possibly non-ASCII character in.

asciiXdigitValue
int asciiXdigitValue(char c)

Determines the numeric value of a character as a hexidecimal digit. Differs from g_unichar_xdigit_value() because it takes a char, so there's no worry about sign extension if characters are signed.

printfStringUpperBound
gsize printfStringUpperBound(string format, void* args)

Calculates the maximum space needed to store the output of the sprintf() function.

stpcpy
string stpcpy(string dest, string src)

Copies a nul-terminated string into the dest buffer, include the trailing nul, and return a pointer to the trailing nul byte. This is useful for concatenating multiple strings together without having to repeatedly scan for the end.

strHasPrefix
int strHasPrefix(string str, string prefix)

Looks whether the string str begins with prefix. Since 2.2

strHasSuffix
int strHasSuffix(string str, string suffix)

Looks whether the string str ends with suffix. Since 2.2

strcanon
string strcanon(string string, string validChars, char substitutor)

For each character in string, if the character is not in valid_chars, replaces the character with substitutor. Modifies string in place, and return string itself, not a copy. The return value is to allow nesting such as g_ascii_strup (g_strcanon (str, "abc", '?')).

strcasecmp
int strcasecmp(string s1, string s2)

Warning g_strcasecmp has been deprecated since version 2.2 and should not be used in newly-written code. See g_strncasecmp() for a discussion of why this function is deprecated and how to replace it. A case-insensitive string comparison, corresponding to the standard strcasecmp() function on platforms which support it.

strchomp
string strchomp(string string)

Removes trailing whitespace from a string. This function doesn't allocate or reallocate any memory; it modifies string in place. The pointer to string is returned to allow the nesting of functions. Also see g_strchug() and g_strstrip().

strchug
string strchug(string string)

Removes leading whitespace from a string, by moving the rest of the characters forward. This function doesn't allocate or reallocate any memory; it modifies string in place. The pointer to string is returned to allow the nesting of functions. Also see g_strchomp() and g_strstrip().

strcmp0
int strcmp0(string str1, string str2)

Compares str1 and str2 like strcmp(). Handles NULL gracefully by sorting it before non-NULL strings. Comparing two NULL pointers returns 0. Since 2.16

strcompress
string strcompress(string source)

Replaces all escaped characters with their one byte equivalent. It does the reverse conversion of g_strescape().

strdelimit
string strdelimit(string string, string delimiters, char newDelimiter)

Converts any delimiter characters in string to new_delimiter. Any characters in string which are found in delimiters are changed to the new_delimiter character. Modifies string in place, and returns string itself, not a copy. The return value is to allow nesting such as g_ascii_strup (g_strdelimit (str, "abc", '?')).

strdown
string strdown(string string)

Warning g_strdown has been deprecated since version 2.2 and should not be used in newly-written code. This function is totally broken for the reasons discussed in the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown() instead. Converts a string to lower case.

strdup
string strdup(string str)

Duplicates a string. If str is NULL it returns NULL. The returned string should be freed with g_free() when no longer needed.

strdupVprintf
string strdupVprintf(string format, void* args)

Similar to the standard C vsprintf() function but safer, since it calculates the maximum space required and allocates memory to hold the result. The returned string should be freed with g_free() when no longer needed. See also g_vasprintf(), which offers the same functionality, but additionally returns the length of the allocated string.

strdupv
string[] strdupv(string[] strArray)

Copies NULL-terminated array of strings. The copy is a deep copy; the new array should be freed by first freeing each string, then the array itself. g_strfreev() does this for you. If called on a NULL value, g_strdupv() simply returns NULL.

strerror
string strerror(int errnum)

Returns a string corresponding to the given error code, e.g. "no such process". You should use this function in preference to strerror(), because it returns a string in UTF-8 encoding, and since not all platforms support the strerror() function.

strescape
string strescape(string source, string exceptions)

Escapes the special characters '\b', '\f', '\n', '\r', '\t', '\' and '"' in the string source by inserting a '\' before them. Additionally all characters in the range 0x01-0x1F (everything below SPACE) and in the range 0x7F-0xFF (all non-ASCII chars) are replaced with a '\' followed by their octal representation. Characters supplied in exceptions are not escaped. g_strcompress() does the reverse conversion.

strfreev
void strfreev(string[] strArray)

Frees a NULL-terminated array of strings, and the array itself. If called on a NULL value, g_strfreev() simply returns.

stringAsciiDown
StringG stringAsciiDown(StringG string)

Converts all upper case ASCII letters to lower case ASCII letters.

stringAsciiUp
StringG stringAsciiUp(StringG string)

Converts all lower case ASCII letters to upper case ASCII letters.

strjoinv
string strjoinv(string separator, string[] strArray)

Joins a number of strings together to form one long string, with the optional separator inserted between each of them. The returned string should be freed with g_free().

strlcat
gsize strlcat(string dest, string src, gsize destSize)

Portability wrapper that calls strlcat() on systems which have it, and emulates it otherwise. Appends nul-terminated src string to dest, guaranteeing nul-termination for dest. The total size of dest won't exceed dest_size. At most dest_size - 1 characters will be copied. Unlike strncat, dest_size is the full size of dest, not the space left over. This function does NOT allocate memory. This always NUL terminates (unless siz == 0 or there were no NUL characters in the dest_size characters of dest to start with). Note Caveat: this is supposedly a more secure alternative to strcat() or strncat(), but for real security g_strconcat() is harder to mess up.

strlcpy
gsize strlcpy(string dest, string src, gsize destSize)

Portability wrapper that calls strlcpy() on systems which have it, and emulates strlcpy() otherwise. Copies src to dest; dest is guaranteed to be nul-terminated; src must be nul-terminated; dest_size is the buffer size, not the number of chars to copy. At most dest_size - 1 characters will be copied. Always nul-terminates (unless dest_size == 0). This function does not allocate memory. Unlike strncpy(), this function doesn't pad dest (so it's often faster). It returns the size of the attempted result, strlen (src), so if retval >= dest_size, truncation occurred. Note Caveat: strlcpy() is supposedly more secure than strcpy() or strncpy(), but if you really want to avoid screwups, g_strdup() is an even better idea.

strncasecmp
int strncasecmp(string s1, string s2, uint n)

Warning g_strncasecmp has been deprecated since version 2.2 and should not be used in newly-written code. The problem with g_strncasecmp() is that it does the comparison by calling toupper()/tolower(). These functions are locale-specific and operate on single bytes. However, it is impossible to handle things correctly from an I18N standpoint by operating on bytes, since characters may be multibyte. Thus g_strncasecmp() is broken if your string is guaranteed to be ASCII, since it's locale-sensitive, and it's broken if your string is localized, since it doesn't work on many encodings at all, including UTF-8, EUC-JP, etc. There are therefore two replacement functions: g_ascii_strncasecmp(), which only works on ASCII and is not locale-sensitive, and g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8. A case-insensitive string comparison, corresponding to the standard strncasecmp() function on platforms which support it. It is similar to g_strcasecmp() except it only compares the first n characters of the strings.

strndup
string strndup(string str, gsize n)

Duplicates the first n bytes of a string, returning a newly-allocated buffer n + 1 bytes long which will always be nul-terminated. If str is less than n bytes long the buffer is padded with nuls. If str is NULL it returns NULL. The returned value should be freed when no longer needed. Note To copy a number of characters from a UTF-8 encoded string, use g_utf8_strncpy() instead.

strnfill
string strnfill(gsize length, char fillChar)

Creates a new string length bytes long filled with fill_char. The returned string should be freed when no longer needed.

strreverse
string strreverse(string string)

Reverses all of the bytes in a string. For example, g_strreverse ("abcdef") will result in "fedcba". Note that g_strreverse() doesn't work on UTF-8 strings containing multibyte characters. For that purpose, use g_utf8_strreverse().

strrstr
string strrstr(string haystack, string needle)

Searches the string haystack for the last occurrence of the string needle.

strrstrLen
string strrstrLen(string haystack, gssize haystackLen, string needle)

Searches the string haystack for the last occurrence of the string needle, limiting the length of the search to haystack_len.

strsignal
string strsignal(int signum)

Returns a string describing the given signal, e.g. "Segmentation fault". You should use this function in preference to strsignal(), because it returns a string in UTF-8 encoding, and since not all platforms support the strsignal() function.

strsplit
string[] strsplit(string string, string delimiter, int maxTokens)

Splits a string into a maximum of max_tokens pieces, using the given delimiter. If max_tokens is reached, the remainder of string is appended to the last token. As a special case, the result of splitting the empty string "" is an empty vector, not a vector containing a single string. The reason for this special case is that being able to represent a empty vector is typically more useful than consistent handling of empty elements. If you do need to represent empty elements, you'll need to check for the empty string before calling g_strsplit().

strsplitSet
string[] strsplitSet(string string, string delimiters, int maxTokens)

Splits string into a number of tokens not containing any of the characters in delimiter. A token is the (possibly empty) longest string that does not contain any of the characters in delimiters. If max_tokens is reached, the remainder is appended to the last token. For example the result of g_strsplit_set ("abc:def/ghi", ":/", -1) is a NULL-terminated vector containing the three strings "abc", "def", and "ghi". The result if g_strsplit_set (":def/ghi:", ":/", -1) is a NULL-terminated vector containing the four strings "", "def", "ghi", and "". As a special case, the result of splitting the empty string "" is an empty vector, not a vector containing a single string. The reason for this special case is that being able to represent a empty vector is typically more useful than consistent handling of empty elements. If you do need to represent empty elements, you'll need to check for the empty string before calling g_strsplit_set(). Note that this function works on bytes not characters, so it can't be used to delimit UTF-8 strings for anything but ASCII characters. Since 2.4

strstrLen
string strstrLen(string haystack, gssize haystackLen, string needle)

Searches the string haystack for the first occurrence of the string needle, limiting the length of the search to haystack_len.

strtod
double strtod(string nptr, string endptr)

Converts a string to a gdouble value. It calls the standard strtod() function to handle the conversion, but if the string is not completely converted it attempts the conversion again with g_ascii_strtod(), and returns the best match. This function should seldomly be used. The normal situation when reading numbers not for human consumption is to use g_ascii_strtod(). Only when you know that you must expect both locale formatted and C formatted numbers should you use this. Make sure that you don't pass strings such as comma separated lists of values, since the commas may be interpreted as a decimal point in some locales, causing unexpected results.

strup
string strup(string string)

Warning g_strup has been deprecated since version 2.2 and should not be used in newly-written code. This function is totally broken for the reasons discussed in the g_strncasecmp() docs - use g_ascii_strup() or g_utf8_strup() instead. Converts a string to upper case.

strvLength
uint strvLength(string[] strArray)

Returns the length of the given NULL-terminated string array str_array. Since 2.6

toString
string toString(char* s, size_t len)

Convert C-style 0 terminated string s to char[] string. copied from phobos

toString
string toString(bool b)
toString
char[] toString(char c)
toString
string toString(ubyte ub)
toString
string toString(ushort us)
toString
string toString(uint u)
toString
string toString(ulong u)
toString
string toString(byte b)
toString
string toString(short s)
toString
string toString(int i)
toStringArray
string[] toStringArray(char** args)
toStringz
char* toStringz(string s)

Convert array of chars s[] to a C-style 0 terminated string. copied from phobos

toStringzArray
char** toStringzArray(string[] args)
vasprintf
int vasprintf(string string, string format, void* args)

An implementation of the GNU vasprintf() function which supports positional parameters, as specified in the Single Unix Specification. This function is similar to g_vsprintf(), except that it allocates a string to hold the output, instead of putting the output in a buffer you allocate in advance. Since 2.4

vfprintf
int vfprintf(FILE* file, string format, void* args)

An implementation of the standard fprintf() function which supports positional parameters, as specified in the Single Unix Specification. Since 2.2

vprintf
int vprintf(string format, void* args)

An implementation of the standard vprintf() function which supports positional parameters, as specified in the Single Unix Specification. Since 2.2

vsnprintf
int vsnprintf(string string, gulong n, string format, void* args)

A safer form of the standard vsprintf() function. The output is guaranteed to not exceed n characters (including the terminating nul character), so it is easy to ensure that a buffer overflow cannot occur. See also g_strdup_vprintf(). In versions of GLib prior to 1.2.3, this function may return -1 if the output was truncated, and the truncated string may not be nul-terminated. In versions prior to 1.3.12, this function returns the length of the output string. The return value of g_vsnprintf() conforms to the vsnprintf() function as standardized in ISO C99. Note that this is different from traditional vsnprintf(), which returns the length of the output string. The format string may contain positional parameters, as specified in the Single Unix Specification.

vsprintf
int vsprintf(string string, string format, void* args)

An implementation of the standard vsprintf() function which supports positional parameters, as specified in the Single Unix Specification. Since 2.2

Static variables

digits
char[10] digits;

0..9

Meta