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 nul-terminated.
32 If @str is less than @n characters long the buffer is padded with nuls.
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_strdupv ##### -->
44 Copies a %NULL-terminated array of strings. The result consists of a
45 %NULL-terminated array, with one malloc block holding the array of strings, and
46 each string itself allocated. The simplest way to free the result is with
47 g_strfreev() which frees each string in a vector, then the vector itself.
50 @str_array: array to copy
54 <!-- ##### FUNCTION g_strnfill ##### -->
56 Creates a new string @length characters long filled with @fill_char.
57 The returned string should be freed when no longer needed.
60 @length: the length of the new string.
61 @fill_char: the character to fill the string with.
62 @Returns: a newly-allocated string filled the @fill_char.
65 <!-- ##### FUNCTION g_stpcpy ##### -->
75 <!-- ##### FUNCTION g_strstr_len ##### -->
86 <!-- ##### FUNCTION g_strrstr ##### -->
96 <!-- ##### FUNCTION g_strrstr_len ##### -->
107 <!-- ##### FUNCTION g_strlcpy ##### -->
109 Portability wrapper that calls strlcpy() on systems which have it, and emulates
110 strlcpy() otherwise. Copies @src to @dest; @dest is guaranteed to be
111 nul-terminated; @src must be nul-terminated; @dest_size is the buffer size, not
112 the number of chars to copy. Caveat: strlcpy() is supposedly more secure than
113 strcpy() or strncpy(), but if you really want to avoid screwups, g_strdup() is
117 @dest: destination buffer
119 @dest_size: length of @dest in bytes
120 @Returns: length of @src
123 <!-- ##### FUNCTION g_strlcat ##### -->
125 Portability wrapper that calls strlcat() on systems which have it, and emulates
126 strlcat() otherwise. Appends nul-terminated @src string to @dest, guaranteeing
127 nul-termination for @dest. The total size of @dest won't exceed
128 @dest_size. Caveat: this is supposedly a more secure alternative to strcat() or
129 strncat(), but for real security g_strconcat() is harder to mess up.
132 @dest: destination buffer, already containing one nul-terminated string
134 @dest_size: length of @dest buffer in bytes (not length of existing string inside @dest)
135 @Returns: length of @src plus initial length of string in @dest
138 <!-- ##### FUNCTION g_strdup_printf ##### -->
140 Similar to the standard C <function>sprintf()</function> function
141 but safer, since it calculates the maximum space required and allocates
142 memory to hold the result.
143 The returned string should be freed when no longer needed.
146 @format: the standard <function>sprintf()</function> format string.
147 @Varargs: the parameters to insert into the format string.
148 @Returns: a newly-allocated string holding the result.
151 <!-- ##### FUNCTION g_strdup_vprintf ##### -->
153 Similar to the standard C <function>vsprintf()</function> function
154 but safer, since it calculates the maximum space required and allocates
155 memory to hold the result.
156 The returned string should be freed when no longer needed.
159 @format: the standard <function>sprintf()</function> format string.
160 @args: the list of parameters to insert into the format string.
161 @Returns: a newly-allocated string holding the result.
164 <!-- ##### FUNCTION g_snprintf ##### -->
166 A safer form of the standard <function>sprintf()</function> function.
167 The output is guaranteed to not exceed @n characters (including the
168 terminating nul character), so it is easy to ensure that a buffer overflow
172 See also g_strdup_printf().
176 In versions of GLib prior to 1.2.3, this function may return -1 if the output
177 was truncated, and the truncated string may not be nul-terminated.
181 @string: the buffer to hold the output.
182 @n: the maximum number of characters to produce (including the terminating null
184 @format: the format string. See the <function>sprintf()</function>
186 @Varargs: the arguments to insert in the output.
187 @Returns: the length of the output string.
190 <!-- ##### FUNCTION g_vsnprintf ##### -->
192 A safer form of the standard <function>vsprintf()</function> function.
193 The output is guaranteed to not exceed @n characters (including the
194 terminating nul character), so it is easy to ensure that a buffer overflow
198 See also g_strdup_vprintf().
202 In versions of GLib prior to 1.2.3, this function may return -1 if the output
203 was truncated, and the truncated string may not be nul-terminated.
207 @string: the buffer to hold the output.
208 @n: the maximum number of characters to produce (including the terminating null
210 @format: the format string. See the <function>sprintf()</function>
212 @args: the list of arguments to insert in the output.
213 @Returns: the length of the output string.
216 <!-- ##### FUNCTION g_printf_string_upper_bound ##### -->
218 Calculates the maximum space needed to store the output of the
219 <function>sprintf()</function> function.
222 @format: the format string. See the <function>printf()</function>
224 @args: the parameters to be inserted into the format string.
225 @Returns: the maximum space needed to store the formatted string.
228 <!-- ##### FUNCTION g_ascii_isalnum ##### -->
230 Determines whether a character is alphanumeric.
233 Unlike the standard C library isalnum function, this only
234 recognizes standard ASCII letters and ignores the locale, returning
235 %FALSE for all non-ASCII characters. Also unlike the standard
236 library function, this takes a char, not an int, so don't call it
237 on EOF but no need to cast to guchar before passing a possibly
238 non-ASCII character in.
242 @Returns: %TRUE if @c is an ASCII alphanumeric character
245 <!-- ##### FUNCTION g_ascii_isalpha ##### -->
247 Determines whether a character is alphabetic (i.e. a letter).
250 Unlike the standard C library isalpha function, this only
251 recognizes standard ASCII letters and ignores the locale, returning
252 %FALSE for all non-ASCII characters. Also unlike the standard
253 library function, this takes a char, not an int, so don't call it
254 on EOF but no need to cast to guchar before passing a possibly
255 non-ASCII character in.
259 @Returns: %TRUE if @c is an ASCII alphabetic character
262 <!-- ##### FUNCTION g_ascii_iscntrl ##### -->
264 Determines whether a character is a control character.
267 Unlike the standard C library iscntrl function, this only
268 recognizes standard ASCII control characters and ignores the locale,
269 returning%FALSE for all non-ASCII characters. Also unlike the standard
270 library function, this takes a char, not an int, so don't call it
271 on EOF but no need to cast to guchar before passing a possibly
272 non-ASCII character in.
276 @Returns: %TRUE if @c is an ASCII control character.
279 <!-- ##### FUNCTION g_ascii_isdigit ##### -->
281 Determines whether a character is digit (0-9).
284 Unlike the standard C library isdigit function,
285 this takes a char, not an int, so don't call it
286 on EOF but no need to cast to guchar before passing a possibly
287 non-ASCII character in.
291 @Returns: %TRUE if @c is an ASCII digit.
294 <!-- ##### FUNCTION g_ascii_isgraph ##### -->
296 Determines whether a character is a printing character and not a space.
299 Unlike the standard C library isgraph function, this only
300 recognizes standard ASCII characters and ignores the locale, returning
301 %FALSE for all non-ASCII characters. Also unlike the standard
302 library function, this takes a char, not an int, so don't call it
303 on EOF but no need to cast to guchar before passing a possibly
304 non-ASCII character in.
308 @Returns: %TRUE if @c is an ASCII printing character other than space.
311 <!-- ##### FUNCTION g_ascii_islower ##### -->
313 Determines whether a character is an ASCII lower case letter.
316 Unlike the standard C library islower function, this only
317 recognizes standard ASCII letters and ignores the locale, returning
318 %FALSE for all non-ASCII characters. Also unlike the standard
319 library function, this takes a char, not an int, so don't call it
320 on EOF but no need to worry about casting to guchar before passing
321 a possibly non-ASCII character in.
325 @Returns: %TRUE if @c is an ASCII lower case letter
328 <!-- ##### FUNCTION g_ascii_isprint ##### -->
330 Determines whether a character is a printing character.
333 Unlike the standard C library isprint function, this only
334 recognizes standard ASCII characters and ignores the locale, returning
335 %FALSE for all non-ASCII characters. Also unlike the standard
336 library function, this takes a char, not an int, so don't call it
337 on EOF but no need to cast to guchar before passing a possibly
338 non-ASCII character in.
342 @Returns: %TRUE if @c is an ASCII printing character.
345 <!-- ##### FUNCTION g_ascii_ispunct ##### -->
347 Determines whether a character is a punctuation character.
350 Unlike the standard C library ispunct function, this only
351 recognizes standard ASCII letters and ignores the locale, returning
352 %FALSE for all non-ASCII characters. Also unlike the standard
353 library function, this takes a char, not an int, so don't call it
354 on EOF but no need to cast to guchar before passing a possibly
355 non-ASCII character in.
359 @Returns: %TRUE if @c is an ASCII punctuation character.
362 <!-- ##### FUNCTION g_ascii_isspace ##### -->
364 Determines whether a character is a white-space character.
367 Unlike the standard C library isspace function, this only
368 recognizes standard ASCII white-space and ignores the locale, returning
369 %FALSE for all non-ASCII characters. Also unlike the standard
370 library function, this takes a char, not an int, so don't call it
371 on EOF but no need to cast to guchar before passing a possibly
372 non-ASCII character in.
376 @Returns: %TRUE if @c is an ASCII white-space character
379 <!-- ##### FUNCTION g_ascii_isupper ##### -->
381 Determines whether a character is an ASCII upper case letter.
384 Unlike the standard C library isupper function, this only
385 recognizes standard ASCII letters and ignores the locale, returning
386 %FALSE for all non-ASCII characters. Also unlike the standard
387 library function, this takes a char, not an int, so don't call it
388 on EOF but no need to worry about casting to guchar before passing
389 a possibly non-ASCII character in.
393 @Returns: %TRUE if @c is an ASCII upper case letter
396 <!-- ##### FUNCTION g_ascii_isxdigit ##### -->
398 Determines whether a character is a hexadecimal-digit character.
401 Unlike the standard C library isxdigit function,
402 this takes a char, not an int, so
403 don't call it on EOF but no need to cast to guchar before passing a
404 possibly non-ASCII character in.
408 @Returns: %TRUE if @c is an ASCII hexadecimal-digit character.
411 <!-- ##### FUNCTION g_ascii_digit_value ##### -->
420 <!-- ##### FUNCTION g_ascii_xdigit_value ##### -->
429 <!-- ##### FUNCTION g_ascii_strcasecmp ##### -->
439 <!-- ##### FUNCTION g_ascii_strncasecmp ##### -->
450 <!-- ##### FUNCTION g_ascii_strup ##### -->
458 <!-- # Unused Parameters # -->
462 <!-- ##### FUNCTION g_ascii_strdown ##### -->
470 <!-- # Unused Parameters # -->
474 <!-- ##### FUNCTION g_ascii_tolower ##### -->
483 <!-- ##### FUNCTION g_ascii_toupper ##### -->
492 <!-- ##### FUNCTION g_string_ascii_up ##### -->
501 <!-- ##### FUNCTION g_string_ascii_down ##### -->
510 <!-- ##### FUNCTION g_strup ##### -->
512 Converts a string to upper case.
515 @string: the string to convert.
519 <!-- ##### FUNCTION g_strdown ##### -->
521 Converts a string to lower case.
524 @string: the string to convert.
528 <!-- ##### FUNCTION g_strcasecmp ##### -->
530 A case-insensitive string comparison, corresponding to the standard
531 <function>strcasecmp()</function> function on platforms which support it.
535 @s2: a string to compare with @s1.
536 @Returns: 0 if the strings match, a negative value if @s1 < @s2, or a positive
540 <!-- ##### FUNCTION g_strncasecmp ##### -->
542 A case-insensitive string comparison, corresponding to the standard
543 <function>strncasecmp()</function> function on platforms which support it.
544 It is similar to g_strcasecmp() except it only compares the first @n characters
549 @s2: a string to compare with @s1.
550 @n: the maximum number of characters to compare.
551 @Returns: 0 if the strings match, a negative value if @s1 < @s2, or a positive
555 <!-- ##### FUNCTION g_strreverse ##### -->
557 Reverses all of the characters in a string.
558 For example, g_strreverse ("abcdef") would be "fedcba".
561 @string: the string to reverse.
565 <!-- ##### FUNCTION g_strtod ##### -->
567 Converts a string to a gdouble value.
568 It calls the standard <function>strtod()</function> function
569 to handle the conversion, but if the string is not completely converted
570 it attempts the conversion again in the "C" locale, and returns the best
574 @nptr: the string to convert to a numeric value.
575 @endptr: if non-NULL, it returns the character after the last character used
577 @Returns: the gdouble value.
580 <!-- ##### FUNCTION g_strchug ##### -->
582 Removes leading whitespace from a string, by moving the rest of the
586 @string: a string to remove the leading whitespace from.
590 <!-- ##### FUNCTION g_strchomp ##### -->
592 Removes trailing whitespace from a string.
595 @string: a string to remove the trailing whitespace from.
599 <!-- ##### MACRO g_strstrip ##### -->
601 Removes leading and trailing whitespace from a string.
604 @string: a string to remove the leading and trailing whitespace from.
607 <!-- ##### FUNCTION g_strdelimit ##### -->
609 Converts any delimiter characters in @string to @new_delimiter.
610 Any characters in @string which are found in @delimiters are changed
611 to the @new_delimiter character.
614 @string: the string to convert.
615 @delimiters: a string containing the current delimiters, or %NULL to use the
616 standard delimiters defined in #G_STR_DELIMITERS.
617 @new_delimiter: the new delimiter character.
621 <!-- ##### MACRO G_STR_DELIMITERS ##### -->
623 The standard delimiters, used in #g_strdelimit.
628 <!-- ##### FUNCTION g_strescape ##### -->
630 Escapes the special characters '\b', '\f', '\n', '\r', '\t', '\' and
631 '"' in the string @source by inserting a '\' before
632 them. Additionally all characters in the range 0x01-0x1F (everything
633 below SPACE) and in the range 0x80-0xFF (all non-ASCII chars) are
634 replaced with a '\' followed by their octal representation. Characters
635 supplied in @exceptions are not escaped.
639 g_strcompress() does the reverse conversion.
642 @source: a string to escape.
643 @exceptions: a string of characters not to escape in @source.
644 @Returns: a newly-allocated copy of @source with certain
645 characters escaped. See above.
648 <!-- ##### FUNCTION g_strcompress ##### -->
650 Replaces all escaped characters with their one byte equivalent. It
651 does the reverse conversion of g_strescape().
654 @source: a string to compress.
655 @Returns: a newly-allocated copy of @source with all escaped
656 character compressed.
659 <!-- ##### FUNCTION g_strcanon ##### -->
661 For each character in @string, if the character is not in @valid_chars,
662 replaces the character with @substitutor. Modifies @string in place,
663 and return @string itself, not a copy. The return value is to allow
664 nesting such as <literal>g_strup (g_strcanon (str))</literal>.
667 @string: a nul-terminated array of bytes.
668 @valid_chars: bytes permitted in @string.
669 @substitutor: replacement character for disallowed bytes.
673 <!-- ##### FUNCTION g_strsplit ##### -->
683 <!-- ##### FUNCTION g_strfreev ##### -->
685 Frees a %NULL-terminated array of strings, and the array itself.
688 @str_array: a %NULL-terminated array of strings to free.
691 <!-- ##### FUNCTION g_strconcat ##### -->
693 Concatenates all of the given strings into one long string. The returned string
694 should be freed when no longer needed. WARNING: THE VARIABLE ARGUMENT LIST MUST
695 END WITH %NULL. If you forget the %NULL, g_strconcat() will start appending
696 random memory junk to your string.
699 @string1: The first string to add, which must not be %NULL.
700 @Varargs: a %NULL-terminated list of strings to append to the string.
701 @Returns: a newly-allocated string containing all the string arguments.
704 <!-- ##### FUNCTION g_strjoin ##### -->
706 Joins a number of strings together to form one long string, with the optional
707 @separator inserted between each of them.
710 @separator: a string to insert between each of the strings, or %NULL.
711 @Varargs: a %NULL-terminated list of strings to join.
712 @Returns: a newly-allocated string containing all of the strings joined
713 together, with @separator between them.
716 <!-- ##### FUNCTION g_strjoinv ##### -->
718 Joins a number of strings together to form one long string, with the optional
719 @separator inserted between each of them.
722 @separator: a string to insert between each of the strings, or %NULL.
723 @str_array: a %NULL-terminated array of strings to join.
724 @Returns: a newly-allocated string containing all of the strings joined
725 together, with @separator between them.
728 <!-- ##### FUNCTION g_strerror ##### -->
730 Returns a string corresponding to the given error code, e.g. "no such process".
731 This function is included since not all platforms support the
732 <function>strerror()</function> function.
735 @errnum: the system error number. See the standard C %errno
737 @Returns: a string describing the error code.
738 If the error code is unknown, it returns "unknown error (<code>)".
739 The string can only be used until the next call to g_strerror.
742 <!-- ##### FUNCTION g_strsignal ##### -->
744 Returns a string describing the given signal, e.g. "Segmentation fault".
745 This function is included since not all platforms support the
746 <function>strsignal()</function> function.
749 @signum: the signal number. See the <literal>signal</literal>
751 @Returns: a string describing the signal.
752 If the signal is unknown, it returns "unknown signal (<signum>)".
753 The string can only be used until the next call to g_strsignal.