1 <!-- ##### SECTION Title ##### -->
2 String Utility Functions
4 <!-- ##### SECTION Short_Description ##### -->
5 various string-related functions.
7 <!-- ##### SECTION Long_Description ##### -->
9 This section describes a number of utility functions for creating,
10 duplicating, and manipulating strings.
13 <!-- ##### SECTION See_Also ##### -->
18 <!-- ##### FUNCTION g_strdup ##### -->
21 The returned string should be freed when no longer needed.
24 @str: the string to duplicate.
25 @Returns: a newly-allocated copy of @str.
28 <!-- ##### FUNCTION g_strndup ##### -->
30 Duplicates the first @n characters of a string, returning a newly-allocated
31 buffer @n + 1 characters long which will always be null-terminated.
32 If @str is less than @n characters long the buffer is padded with nulls.
33 The returned value should be freed when no longer needed.
36 @str: the string to duplicate part of.
37 @n: the maximum number of characters to copy from @str.
38 @Returns: a newly-allocated buffer containing the first @n characters of @str,
42 <!-- ##### FUNCTION g_strnfill ##### -->
44 Creates a new string @length characters long filled with @fill_char.
45 The returned string should be freed when no longer needed.
48 @length: the length of the new string.
49 @fill_char: the character to fill the string with.
50 @Returns: a newly-allocated string filled the @fill_char.
53 <!-- ##### FUNCTION g_strlcpy ##### -->
64 <!-- ##### FUNCTION g_strlcat ##### -->
75 <!-- ##### FUNCTION g_strdup_printf ##### -->
77 Similar to the standard C <function>sprintf()</function> function
78 but safer, since it calculates the maximum space required and allocates
79 memory to hold the result.
80 The returned string should be freed when no longer needed.
83 @format: the standard <function>sprintf()</function> format string.
84 @Varargs: the parameters to insert into the format string.
85 @Returns: a newly-allocated string holding the result.
88 <!-- ##### FUNCTION g_strdup_vprintf ##### -->
90 Similar to the standard C <function>vsprintf()</function> function
91 but safer, since it calculates the maximum space required and allocates
92 memory to hold the result.
93 The returned string should be freed when no longer needed.
96 @format: the standard <function>sprintf()</function> format string.
97 @args: the list of parameters to insert into the format string.
98 @Returns: a newly-allocated string holding the result.
101 <!-- ##### FUNCTION g_snprintf ##### -->
103 A safer form of the standard <function>sprintf()</function> function.
104 The output is guaranteed to not exceed @n characters (including the
105 terminating NULL character), so it is easy to ensure that a buffer overflow
109 See also g_strdup_printf().
113 In versions of GLib prior to 1.2.3, this function may return -1 if the output
114 was truncated, and the truncated string may not be NULL-terminated.
118 @string: the buffer to hold the output.
119 @n: the maximum number of characters to produce (including the terminating null
121 @format: the format string. See the <function>sprintf()</function>
123 @Varargs: the arguments to insert in the output.
124 @Returns: the length of the output string.
127 <!-- ##### FUNCTION g_vsnprintf ##### -->
129 A safer form of the standard <function>vsprintf()</function> function.
130 The output is guaranteed to not exceed @n characters (including the
131 terminating NULL character), so it is easy to ensure that a buffer overflow
135 See also g_strdup_vprintf().
139 In versions of GLib prior to 1.2.3, this function may return -1 if the output
140 was truncated, and the truncated string may not be NULL-terminated.
144 @string: the buffer to hold the output.
145 @n: the maximum number of characters to produce (including the terminating null
147 @format: the format string. See the <function>sprintf()</function>
149 @args: the list of arguments to insert in the output.
150 @Returns: the length of the output string.
153 <!-- ##### FUNCTION g_printf_string_upper_bound ##### -->
155 Calculates the maximum space needed to store the output of the
156 <function>sprintf()</function> function.
159 @format: the format string. See the <function>printf()</function>
161 @args: the parameters to be inserted into the format string.
162 @Returns: the maximum space needed to store the formatted string.
165 <!-- ##### FUNCTION g_strup ##### -->
167 Converts a string to upper case.
170 @string: the string to convert.
174 <!-- ##### FUNCTION g_strdown ##### -->
176 Converts a string to lower case.
179 @string: the string to convert.
183 <!-- ##### FUNCTION g_strcasecmp ##### -->
185 A case-insensitive string comparison, corresponding to the standard
186 <function>strcasecmp()</function> function on platforms which support it.
190 @s2: a string to compare with @s1.
191 @Returns: 0 if the strings match, a negative value if @s1 < @s2, or a positive
195 <!-- ##### FUNCTION g_strncasecmp ##### -->
197 A case-insensitive string comparison, corresponding to the standard
198 <function>strncasecmp()</function> function on platforms which support it.
199 It is similar to g_strcasecmp() except it only compares the first @n characters
204 @s2: a string to compare with @s1.
205 @n: the maximum number of characters to compare.
206 @Returns: 0 if the strings match, a negative value if @s1 < @s2, or a positive
210 <!-- ##### FUNCTION g_strreverse ##### -->
212 Reverses all of the characters in a string.
213 For example, g_strreverse ("abcdef") would be "fedcba".
216 @string: the string to reverse.
220 <!-- ##### FUNCTION g_strtod ##### -->
222 Converts a string to a gdouble value.
223 It calls the standard <function>strtod()</function> function
224 to handle the conversion, but if the string is not completely converted
225 it attempts the conversion again in the "C" locale, and returns the best
229 @nptr: the string to convert to a numeric value.
230 @endptr: if non-NULL, it returns the character after the last character used
232 @Returns: the gdouble value.
235 <!-- ##### FUNCTION g_strchug ##### -->
237 Removes leading whitespace from a string, by moving the rest of the
241 @string: a string to remove the leading whitespace from.
245 <!-- ##### FUNCTION g_strchomp ##### -->
247 Removes trailing whitespace from a string.
250 @string: a string to remove the trailing whitespace from.
254 <!-- ##### MACRO g_strstrip ##### -->
256 Removes leading and trailing whitespace from a string.
259 @string: a string to remove the leading and trailing whitespace from.
262 <!-- ##### FUNCTION g_strdelimit ##### -->
264 Converts any delimiter characters in @string to @new_delimiter.
265 Any characters in @string which are found in @delimiters are changed
266 to the @new_delimiter character.
269 @string: the string to convert.
270 @delimiters: a string containing the current delimiters, or NULL to use the
271 standard delimiters defined in #G_STR_DELIMITERS.
272 @new_delimiter: the new delimiter character.
276 <!-- ##### MACRO G_STR_DELIMITERS ##### -->
278 The standard delimiters, used in #g_strdelimit.
283 <!-- ##### FUNCTION g_strescape ##### -->
285 Escapes the special characters '\b', '\f', '\n', '\r', '\t', '\' and
286 '"' in the string @source by inserting a '\' before
287 them. Additionally all characters in the range 0x01-0x1F (everything
288 below SPACE) and in the range 0x80-0xFF (all non-ASCII chars) are
289 replaced with a '\' followed by their octal representation. Characters
290 supplied in @exceptions are not escaped.
294 g_strcompress() does the reverse conversion.
297 @source: a string to escape.
298 @exceptions: a string of characters not to escape in @source.
299 @Returns: a newly allocated copy of @source with certain
300 characters escaped. See above.
303 <!-- ##### FUNCTION g_strcompress ##### -->
305 Replaces all escaped characters with their one byte equivalent. It
306 does the reverse conversion of g_strescape().
309 @source: a string to compress.
310 @Returns: a newly allocated copy of @source with all escaped
311 character compressed.
314 <!-- ##### FUNCTION g_strcanon ##### -->
325 <!-- ##### FUNCTION g_strsplit ##### -->
327 Splits a string into a maximum of @max_tokens pieces, using the given
328 @delimiter. If @max_tokens is reached, the final string in the returned
329 string array contains the remainder of @string.
332 @string: a string to split.
333 @delimiter: a string which specifies the places at which to split the string.
334 The delimiter is not included in any of the resulting strings, unless
335 max_tokens is reached.
336 @max_tokens: the maximum number of strings to split @string into. If this is
337 less than 1, the string is split completely.
338 @Returns: a newly-allocated array of strings. Use g_strfreev() to free it.
341 <!-- ##### FUNCTION g_strfreev ##### -->
343 Frees a NULL-terminated array of strings, and the array itself.
346 @str_array: a NULL-terminated array of strings to free.
349 <!-- ##### FUNCTION g_strconcat ##### -->
351 Concatenates all of the given strings into one long string.
352 The returned string should be freed when no longer needed.
355 @string1: The first string to add, which must not be NULL.
356 @Varargs: a NULL-terminated list of strings to append to the string.
357 @Returns: a newly-allocated string containing all the string arguments.
360 <!-- ##### FUNCTION g_strjoin ##### -->
362 Joins a number of strings together to form one long string, with the optional
363 @separator inserted between each of them.
366 @separator: a string to insert between each of the strings, or NULL.
367 @Varargs: a NULL-terminated list of strings to join.
368 @Returns: a newly-allocated string containing all of the strings joined
369 together, with @separator between them.
372 <!-- ##### FUNCTION g_strjoinv ##### -->
374 Joins a number of strings together to form one long string, with the optional
375 @separator inserted between each of them.
378 @separator: a string to insert between each of the strings, or NULL.
379 @str_array: a NULL-terminated array of strings to join.
380 @Returns: a newly-allocated string containing all of the strings joined
381 together, with @separator between them.
384 <!-- ##### FUNCTION g_strerror ##### -->
386 Returns a string corresponding to the given error code, e.g. "no such process".
387 This function is included since not all platforms support the
388 <function>strerror()</function> function.
391 @errnum: the system error number. See the standard C %errno
393 @Returns: a string describing the error code.
394 If the error code is unknown, it returns "unknown error (<code>)".
395 The string can only be used until the next call to g_strerror.
398 <!-- ##### FUNCTION g_strsignal ##### -->
400 Returns a string describing the given signal, e.g. "Segmentation fault".
401 This function is included since not all platforms support the
402 <function>strsignal()</function> function.
405 @signum: the signal number. See the <literal>signal</literal>
407 @Returns: a string describing the signal.
408 If the signal is unknown, it returns "unknown signal (<signum>)".
409 The string can only be used until the next call to g_strsignal.