1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
40 #include <ctype.h> /* For tolower() */
43 /* Needed on BSD/OS X for e.g. strtod_l */
51 /* do not include <unistd.h> here, it may interfere with g_strsignal() */
53 #include "gstrfuncs.h"
56 #include "gprintfint.h"
61 * SECTION:string_utils
62 * @title: String Utility Functions
63 * @short_description: various string-related functions
65 * This section describes a number of utility functions for creating,
66 * duplicating, and manipulating strings.
68 * Note that the functions g_printf(), g_fprintf(), g_sprintf(),
69 * g_snprintf(), g_vprintf(), g_vfprintf(), g_vsprintf() and g_vsnprintf()
70 * are declared in the header <filename>gprintf.h</filename> which is
71 * <emphasis>not</emphasis> included in <filename>glib.h</filename>
72 * (otherwise using <filename>glib.h</filename> would drag in
73 * <filename>stdio.h</filename>), so you'll have to explicitly include
74 * <literal><glib/gprintf.h></literal> in order to use the GLib
77 * <para id="string-precision">While you may use the printf() functions
78 * to format UTF-8 strings, notice that the precision of a
79 * <literal>%Ns</literal> parameter is interpreted as the
80 * number of <emphasis>bytes</emphasis>, not <emphasis>characters</emphasis>
81 * to print. On top of that, the GNU libc implementation of the printf()
82 * functions has the "feature" that it checks that the string given for
83 * the <literal>%Ns</literal> parameter consists of a whole number
84 * of characters in the current encoding. So, unless you are sure you are
85 * always going to be in an UTF-8 locale or your know your text is restricted
86 * to ASCII, avoid using <literal>%Ns</literal>. If your intention is
87 * to format strings for a certain number of columns, then
88 * <literal>%Ns</literal> is not a correct solution anyway, since it
89 * fails to take wide characters (see g_unichar_iswide()) into account.
97 * Determines whether a character is alphanumeric.
99 * Unlike the standard C library isalnum() function, this only
100 * recognizes standard ASCII letters and ignores the locale,
101 * returning %FALSE for all non-ASCII characters. Also, unlike
102 * the standard library function, this takes a <type>char</type>,
103 * not an <type>int</type>, so don't call it on %EOF, but no need to
104 * cast to #guchar before passing a possibly non-ASCII character in.
106 * Returns: %TRUE if @c is an ASCII alphanumeric character
113 * Determines whether a character is alphabetic (i.e. a letter).
115 * Unlike the standard C library isalpha() function, this only
116 * recognizes standard ASCII letters and ignores the locale,
117 * returning %FALSE for all non-ASCII characters. Also, unlike
118 * the standard library function, this takes a <type>char</type>,
119 * not an <type>int</type>, so don't call it on %EOF, but no need to
120 * cast to #guchar before passing a possibly non-ASCII character in.
122 * Returns: %TRUE if @c is an ASCII alphabetic character
129 * Determines whether a character is a control character.
131 * Unlike the standard C library iscntrl() function, this only
132 * recognizes standard ASCII control characters and ignores the
133 * locale, returning %FALSE for all non-ASCII characters. Also,
134 * unlike the standard library function, this takes a <type>char</type>,
135 * not an <type>int</type>, so don't call it on %EOF, but no need to
136 * cast to #guchar before passing a possibly non-ASCII character in.
138 * Returns: %TRUE if @c is an ASCII control character.
145 * Determines whether a character is digit (0-9).
147 * Unlike the standard C library isdigit() function, this takes
148 * a <type>char</type>, not an <type>int</type>, so don't call it
149 * on %EOF, but no need to cast to #guchar before passing a possibly
150 * non-ASCII character in.
152 * Returns: %TRUE if @c is an ASCII digit.
159 * Determines whether a character is a printing character and not a space.
161 * Unlike the standard C library isgraph() function, this only
162 * recognizes standard ASCII characters and ignores the locale,
163 * returning %FALSE for all non-ASCII characters. Also, unlike
164 * the standard library function, this takes a <type>char</type>,
165 * not an <type>int</type>, so don't call it on %EOF, but no need
166 * to cast to #guchar before passing a possibly non-ASCII character in.
168 * Returns: %TRUE if @c is an ASCII printing character other than space.
175 * Determines whether a character is an ASCII lower case letter.
177 * Unlike the standard C library islower() function, this only
178 * recognizes standard ASCII letters and ignores the locale,
179 * returning %FALSE for all non-ASCII characters. Also, unlike
180 * the standard library function, this takes a <type>char</type>,
181 * not an <type>int</type>, so don't call it on %EOF, but no need
182 * to worry about casting to #guchar before passing a possibly
183 * non-ASCII character in.
185 * Returns: %TRUE if @c is an ASCII lower case letter
192 * Determines whether a character is a printing character.
194 * Unlike the standard C library isprint() function, this only
195 * recognizes standard ASCII characters and ignores the locale,
196 * returning %FALSE for all non-ASCII characters. Also, unlike
197 * the standard library function, this takes a <type>char</type>,
198 * not an <type>int</type>, so don't call it on %EOF, but no need
199 * to cast to #guchar before passing a possibly non-ASCII character in.
201 * Returns: %TRUE if @c is an ASCII printing character.
208 * Determines whether a character is a punctuation character.
210 * Unlike the standard C library ispunct() function, this only
211 * recognizes standard ASCII letters and ignores the locale,
212 * returning %FALSE for all non-ASCII characters. Also, unlike
213 * the standard library function, this takes a <type>char</type>,
214 * not an <type>int</type>, so don't call it on %EOF, but no need to
215 * cast to #guchar before passing a possibly non-ASCII character in.
217 * Returns: %TRUE if @c is an ASCII punctuation character.
224 * Determines whether a character is a white-space character.
226 * Unlike the standard C library isspace() function, this only
227 * recognizes standard ASCII white-space and ignores the locale,
228 * returning %FALSE for all non-ASCII characters. Also, unlike
229 * the standard library function, this takes a <type>char</type>,
230 * not an <type>int</type>, so don't call it on %EOF, but no need to
231 * cast to #guchar before passing a possibly non-ASCII character in.
233 * Returns: %TRUE if @c is an ASCII white-space character
240 * Determines whether a character is an ASCII upper case letter.
242 * Unlike the standard C library isupper() function, this only
243 * recognizes standard ASCII letters and ignores the locale,
244 * returning %FALSE for all non-ASCII characters. Also, unlike
245 * the standard library function, this takes a <type>char</type>,
246 * not an <type>int</type>, so don't call it on %EOF, but no need to
247 * worry about casting to #guchar before passing a possibly non-ASCII
250 * Returns: %TRUE if @c is an ASCII upper case letter
257 * Determines whether a character is a hexadecimal-digit character.
259 * Unlike the standard C library isxdigit() function, this takes
260 * a <type>char</type>, not an <type>int</type>, so don't call it
261 * on %EOF, but no need to cast to #guchar before passing a
262 * possibly non-ASCII character in.
264 * Returns: %TRUE if @c is an ASCII hexadecimal-digit character.
268 * G_ASCII_DTOSTR_BUF_SIZE:
270 * A good size for a buffer to be passed into g_ascii_dtostr().
271 * It is guaranteed to be enough for all output of that function
272 * on systems with 64bit IEEE-compatible doubles.
274 * The typical usage would be something like:
276 * char buf[G_ASCII_DTOSTR_BUF_SIZE];
278 * fprintf (out, "value=%s\n", g_ascii_dtostr (buf, sizeof (buf), value));
284 * @string: a string to remove the leading and trailing whitespace from
286 * Removes leading and trailing whitespace from a string.
287 * See g_strchomp() and g_strchug().
295 * The standard delimiters, used in g_strdelimit().
298 static const guint16 ascii_table_data[256] = {
299 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
300 0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004,
301 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
302 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
303 0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
304 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
305 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
306 0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
307 0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
308 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
309 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
310 0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
311 0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
312 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
313 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
314 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004
315 /* the upper 128 are all zeroes */
318 const guint16 * const g_ascii_table = ascii_table_data;
320 #ifdef HAVE_NEWLOCALE
324 static gsize initialized = FALSE;
325 static locale_t C_locale = NULL;
327 if (g_once_init_enter (&initialized))
329 C_locale = newlocale (LC_ALL_MASK, "C", NULL);
330 g_once_init_leave (&initialized, TRUE);
339 * @str: the string to duplicate
341 * Duplicates a string. If @str is %NULL it returns %NULL.
342 * The returned string should be freed with g_free()
343 * when no longer needed.
345 * Returns: a newly-allocated copy of @str
348 g_strdup (const gchar *str)
355 length = strlen (str) + 1;
356 new_str = g_new (char, length);
357 memcpy (new_str, str, length);
367 * @mem: the memory to copy.
368 * @byte_size: the number of bytes to copy.
370 * Allocates @byte_size bytes of memory, and copies @byte_size bytes into it
371 * from @mem. If @mem is %NULL it returns %NULL.
373 * Returns: a pointer to the newly-allocated copy of the memory, or %NULL if @mem
377 g_memdup (gconstpointer mem,
384 new_mem = g_malloc (byte_size);
385 memcpy (new_mem, mem, byte_size);
395 * @str: the string to duplicate
396 * @n: the maximum number of bytes to copy from @str
398 * Duplicates the first @n bytes of a string, returning a newly-allocated
399 * buffer @n + 1 bytes long which will always be nul-terminated.
400 * If @str is less than @n bytes long the buffer is padded with nuls.
401 * If @str is %NULL it returns %NULL.
402 * The returned value should be freed when no longer needed.
405 * To copy a number of characters from a UTF-8 encoded string, use
406 * g_utf8_strncpy() instead.
409 * Returns: a newly-allocated buffer containing the first @n bytes
410 * of @str, nul-terminated
413 g_strndup (const gchar *str,
420 new_str = g_new (gchar, n + 1);
421 strncpy (new_str, str, n);
432 * @length: the length of the new string
433 * @fill_char: the byte to fill the string with
435 * Creates a new string @length bytes long filled with @fill_char.
436 * The returned string should be freed when no longer needed.
438 * Returns: a newly-allocated string filled the @fill_char
441 g_strnfill (gsize length,
446 str = g_new (gchar, length + 1);
447 memset (str, (guchar)fill_char, length);
455 * @dest: destination buffer.
456 * @src: source string.
458 * Copies a nul-terminated string into the dest buffer, include the
459 * trailing nul, and return a pointer to the trailing nul byte.
460 * This is useful for concatenating multiple strings together
461 * without having to repeatedly scan for the end.
463 * Return value: a pointer to trailing nul byte.
466 g_stpcpy (gchar *dest,
470 g_return_val_if_fail (dest != NULL, NULL);
471 g_return_val_if_fail (src != NULL, NULL);
472 return stpcpy (dest, src);
474 register gchar *d = dest;
475 register const gchar *s = src;
477 g_return_val_if_fail (dest != NULL, NULL);
478 g_return_val_if_fail (src != NULL, NULL);
481 while (*s++ != '\0');
489 * @format: a standard printf() format string, but notice
490 * <link linkend="string-precision">string precision pitfalls</link>
491 * @args: the list of parameters to insert into the format string
493 * Similar to the standard C vsprintf() function but safer, since it
494 * calculates the maximum space required and allocates memory to hold
495 * the result. The returned string should be freed with g_free() when
498 * See also g_vasprintf(), which offers the same functionality, but
499 * additionally returns the length of the allocated string.
501 * Returns: a newly-allocated string holding the result
504 g_strdup_vprintf (const gchar *format,
507 gchar *string = NULL;
509 g_vasprintf (&string, format, args);
516 * @format: a standard printf() format string, but notice
517 * <link linkend="string-precision">string precision pitfalls</link>
518 * @...: the parameters to insert into the format string
520 * Similar to the standard C sprintf() function but safer, since it
521 * calculates the maximum space required and allocates memory to hold
522 * the result. The returned string should be freed with g_free() when no
525 * Returns: a newly-allocated string holding the result
528 g_strdup_printf (const gchar *format,
534 va_start (args, format);
535 buffer = g_strdup_vprintf (format, args);
543 * @string1: the first string to add, which must not be %NULL
544 * @...: a %NULL-terminated list of strings to append to the string
546 * Concatenates all of the given strings into one long string.
547 * The returned string should be freed with g_free() when no longer needed.
549 * Note that this function is usually not the right function to use to
550 * assemble a translated message from pieces, since proper translation
551 * often requires the pieces to be reordered.
553 * <warning><para>The variable argument list <emphasis>must</emphasis> end
554 * with %NULL. If you forget the %NULL, g_strconcat() will start appending
555 * random memory junk to your string.</para></warning>
557 * Returns: a newly-allocated string containing all the string arguments
560 g_strconcat (const gchar *string1, ...)
571 l = 1 + strlen (string1);
572 va_start (args, string1);
573 s = va_arg (args, gchar*);
577 s = va_arg (args, gchar*);
581 concat = g_new (gchar, l);
584 ptr = g_stpcpy (ptr, string1);
585 va_start (args, string1);
586 s = va_arg (args, gchar*);
589 ptr = g_stpcpy (ptr, s);
590 s = va_arg (args, gchar*);
599 * @nptr: the string to convert to a numeric value.
600 * @endptr: if non-%NULL, it returns the character after
601 * the last character used in the conversion.
603 * Converts a string to a #gdouble value.
604 * It calls the standard strtod() function to handle the conversion, but
605 * if the string is not completely converted it attempts the conversion
606 * again with g_ascii_strtod(), and returns the best match.
608 * This function should seldom be used. The normal situation when reading
609 * numbers not for human consumption is to use g_ascii_strtod(). Only when
610 * you know that you must expect both locale formatted and C formatted numbers
611 * should you use this. Make sure that you don't pass strings such as comma
612 * separated lists of values, since the commas may be interpreted as a decimal
613 * point in some locales, causing unexpected results.
615 * Return value: the #gdouble value.
618 g_strtod (const gchar *nptr,
626 g_return_val_if_fail (nptr != NULL, 0);
631 val_1 = strtod (nptr, &fail_pos_1);
633 if (fail_pos_1 && fail_pos_1[0] != 0)
634 val_2 = g_ascii_strtod (nptr, &fail_pos_2);
636 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
639 *endptr = fail_pos_1;
645 *endptr = fail_pos_2;
652 * @nptr: the string to convert to a numeric value.
653 * @endptr: if non-%NULL, it returns the character after
654 * the last character used in the conversion.
656 * Converts a string to a #gdouble value.
658 * This function behaves like the standard strtod() function
659 * does in the C locale. It does this without actually changing
660 * the current locale, since that would not be thread-safe.
661 * A limitation of the implementation is that this function
662 * will still accept localized versions of infinities and NANs.
664 * This function is typically used when reading configuration
665 * files or other non-user input that should be locale independent.
666 * To handle input from the user you should normally use the
667 * locale-sensitive system strtod() function.
669 * To convert from a #gdouble to a string in a locale-insensitive
670 * way, use g_ascii_dtostr().
672 * If the correct value would cause overflow, plus or minus %HUGE_VAL
673 * is returned (according to the sign of the value), and %ERANGE is
674 * stored in %errno. If the correct value would cause underflow,
675 * zero is returned and %ERANGE is stored in %errno.
677 * This function resets %errno before calling strtod() so that
678 * you can reliably detect overflow and underflow.
680 * Return value: the #gdouble value.
683 g_ascii_strtod (const gchar *nptr,
686 g_return_val_if_fail (nptr != NULL, 0);
691 return strtod_l (nptr, endptr, get_C_locale ());
695 struct lconv *locale_data;
696 const char *decimal_point;
697 int decimal_point_len;
698 const char *p, *decimal_point_pos;
699 const char *end = NULL; /* Silence gcc */
704 locale_data = localeconv ();
705 decimal_point = locale_data->decimal_point;
706 decimal_point_len = strlen (decimal_point);
708 g_assert (decimal_point_len != 0);
710 decimal_point_pos = NULL;
713 if (decimal_point[0] != '.' ||
714 decimal_point[1] != 0)
717 /* Skip leading space */
718 while (g_ascii_isspace (*p))
721 /* Skip leading optional sign */
722 if (*p == '+' || *p == '-')
726 (p[1] == 'x' || p[1] == 'X'))
729 /* HEX - find the (optional) decimal point */
731 while (g_ascii_isxdigit (*p))
735 decimal_point_pos = p++;
737 while (g_ascii_isxdigit (*p))
740 if (*p == 'p' || *p == 'P')
742 if (*p == '+' || *p == '-')
744 while (g_ascii_isdigit (*p))
749 else if (g_ascii_isdigit (*p) || *p == '.')
751 while (g_ascii_isdigit (*p))
755 decimal_point_pos = p++;
757 while (g_ascii_isdigit (*p))
760 if (*p == 'e' || *p == 'E')
762 if (*p == '+' || *p == '-')
764 while (g_ascii_isdigit (*p))
769 /* For the other cases, we need not convert the decimal point */
772 if (decimal_point_pos)
776 /* We need to convert the '.' to the locale specific decimal point */
777 copy = g_malloc (end - nptr + 1 + decimal_point_len);
780 memcpy (c, nptr, decimal_point_pos - nptr);
781 c += decimal_point_pos - nptr;
782 memcpy (c, decimal_point, decimal_point_len);
783 c += decimal_point_len;
784 memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
785 c += end - (decimal_point_pos + 1);
789 val = strtod (copy, &fail_pos);
790 strtod_errno = errno;
794 if (fail_pos - copy > decimal_point_pos - nptr)
795 fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
797 fail_pos = (char *)nptr + (fail_pos - copy);
807 copy = g_malloc (end - (char *)nptr + 1);
808 memcpy (copy, nptr, end - nptr);
809 *(copy + (end - (char *)nptr)) = 0;
812 val = strtod (copy, &fail_pos);
813 strtod_errno = errno;
817 fail_pos = (char *)nptr + (fail_pos - copy);
825 val = strtod (nptr, &fail_pos);
826 strtod_errno = errno;
832 errno = strtod_errno;
841 * @buffer: A buffer to place the resulting string in
842 * @buf_len: The length of the buffer.
843 * @d: The #gdouble to convert
845 * Converts a #gdouble to a string, using the '.' as
848 * This functions generates enough precision that converting
849 * the string back using g_ascii_strtod() gives the same machine-number
850 * (on machines with IEEE compatible 64bit doubles). It is
851 * guaranteed that the size of the resulting string will never
852 * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes.
854 * Return value: The pointer to the buffer with the converted string.
857 g_ascii_dtostr (gchar *buffer,
861 return g_ascii_formatd (buffer, buf_len, "%.17g", d);
866 * @buffer: A buffer to place the resulting string in
867 * @buf_len: The length of the buffer.
868 * @format: The printf()-style format to use for the
869 * code to use for converting.
870 * @d: The #gdouble to convert
872 * Converts a #gdouble to a string, using the '.' as
873 * decimal point. To format the number you pass in
874 * a printf()-style format string. Allowed conversion
875 * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'.
877 * If you just want to want to serialize the value into a
878 * string, use g_ascii_dtostr().
880 * Return value: The pointer to the buffer with the converted string.
883 g_ascii_formatd (gchar *buffer,
888 #ifdef HAVE_USELOCALE
891 old_locale = uselocale (get_C_locale ());
892 _g_snprintf (buffer, buf_len, format, d);
893 uselocale (old_locale);
897 struct lconv *locale_data;
898 const char *decimal_point;
899 int decimal_point_len;
904 g_return_val_if_fail (buffer != NULL, NULL);
905 g_return_val_if_fail (format[0] == '%', NULL);
906 g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
908 format_char = format[strlen (format) - 1];
910 g_return_val_if_fail (format_char == 'e' || format_char == 'E' ||
911 format_char == 'f' || format_char == 'F' ||
912 format_char == 'g' || format_char == 'G',
915 if (format[0] != '%')
918 if (strpbrk (format + 1, "'l%"))
921 if (!(format_char == 'e' || format_char == 'E' ||
922 format_char == 'f' || format_char == 'F' ||
923 format_char == 'g' || format_char == 'G'))
926 _g_snprintf (buffer, buf_len, format, d);
928 locale_data = localeconv ();
929 decimal_point = locale_data->decimal_point;
930 decimal_point_len = strlen (decimal_point);
932 g_assert (decimal_point_len != 0);
934 if (decimal_point[0] != '.' ||
935 decimal_point[1] != 0)
939 while (g_ascii_isspace (*p))
942 if (*p == '+' || *p == '-')
945 while (isdigit ((guchar)*p))
948 if (strncmp (p, decimal_point, decimal_point_len) == 0)
952 if (decimal_point_len > 1)
954 rest_len = strlen (p + (decimal_point_len-1));
955 memmove (p, p + (decimal_point_len-1), rest_len);
965 #define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
966 (c) == '\r' || (c) == '\t' || (c) == '\v')
967 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
968 #define ISLOWER(c) ((c) >= 'a' && (c) <= 'z')
969 #define ISALPHA(c) (ISUPPER (c) || ISLOWER (c))
970 #define TOUPPER(c) (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
971 #define TOLOWER(c) (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
973 #if !defined(HAVE_STRTOLL_L) || !defined(HAVE_STRTOULL_L)
976 g_parse_long_long (const gchar *nptr,
977 const gchar **endptr,
981 /* this code is based on on the strtol(3) code from GNU libc released under
982 * the GNU Lesser General Public License.
984 * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
985 * Free Software Foundation, Inc.
991 const gchar *s, *save;
994 g_return_val_if_fail (nptr != NULL, 0);
997 if (base == 1 || base > 36)
1007 /* Skip white space. */
1008 while (ISSPACE (*s))
1011 if (G_UNLIKELY (!*s))
1014 /* Check for a sign. */
1023 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
1026 if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
1037 /* Save the pointer so we can check later if anything happened. */
1039 cutoff = G_MAXUINT64 / base;
1040 cutlim = G_MAXUINT64 % base;
1047 if (c >= '0' && c <= '9')
1049 else if (ISALPHA (c))
1050 c = TOUPPER (c) - 'A' + 10;
1055 /* Check for overflow. */
1056 if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
1065 /* Check if anything actually happened. */
1069 /* Store in ENDPTR the address of one character
1070 past the last character we converted. */
1074 if (G_UNLIKELY (overflow))
1083 /* We must handle a special case here: the base is 0 or 16 and the
1084 first two characters are '0' and 'x', but the rest are no
1085 hexadecimal digits. This is no error case. We return 0 and
1086 ENDPTR points to the `x`. */
1089 if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
1091 *endptr = &save[-1];
1093 /* There was no number to convert. */
1102 * @nptr: the string to convert to a numeric value.
1103 * @endptr: if non-%NULL, it returns the character after
1104 * the last character used in the conversion.
1105 * @base: to be used for the conversion, 2..36 or 0
1107 * Converts a string to a #guint64 value.
1108 * This function behaves like the standard strtoull() function
1109 * does in the C locale. It does this without actually
1110 * changing the current locale, since that would not be
1113 * This function is typically used when reading configuration
1114 * files or other non-user input that should be locale independent.
1115 * To handle input from the user you should normally use the
1116 * locale-sensitive system strtoull() function.
1118 * If the correct value would cause overflow, %G_MAXUINT64
1119 * is returned, and %ERANGE is stored in %errno. If the base is
1120 * outside the valid range, zero is returned, and %EINVAL is stored
1121 * in %errno. If the string conversion fails, zero is returned, and
1122 * @endptr returns @nptr (if @endptr is non-%NULL).
1124 * Return value: the #guint64 value or zero on error.
1129 g_ascii_strtoull (const gchar *nptr,
1133 #ifdef HAVE_STRTOULL_L
1134 return strtoull_l (nptr, endptr, base, get_C_locale ());
1139 result = g_parse_long_long (nptr, (const gchar **) endptr, base, &negative);
1141 /* Return the result of the appropriate sign. */
1142 return negative ? -result : result;
1148 * @nptr: the string to convert to a numeric value.
1149 * @endptr: if non-%NULL, it returns the character after
1150 * the last character used in the conversion.
1151 * @base: to be used for the conversion, 2..36 or 0
1153 * Converts a string to a #gint64 value.
1154 * This function behaves like the standard strtoll() function
1155 * does in the C locale. It does this without actually
1156 * changing the current locale, since that would not be
1159 * This function is typically used when reading configuration
1160 * files or other non-user input that should be locale independent.
1161 * To handle input from the user you should normally use the
1162 * locale-sensitive system strtoll() function.
1164 * If the correct value would cause overflow, %G_MAXINT64 or %G_MININT64
1165 * is returned, and %ERANGE is stored in %errno. If the base is
1166 * outside the valid range, zero is returned, and %EINVAL is stored
1167 * in %errno. If the string conversion fails, zero is returned, and
1168 * @endptr returns @nptr (if @endptr is non-%NULL).
1170 * Return value: the #gint64 value or zero on error.
1175 g_ascii_strtoll (const gchar *nptr,
1179 #ifdef HAVE_STRTOLL_L
1180 return strtoll_l (nptr, endptr, base, get_C_locale ());
1185 result = g_parse_long_long (nptr, (const gchar **) endptr, base, &negative);
1187 if (negative && result > (guint64) G_MININT64)
1192 else if (!negative && result > (guint64) G_MAXINT64)
1198 return - (gint64) result;
1200 return (gint64) result;
1206 * @errnum: the system error number. See the standard C %errno
1209 * Returns a string corresponding to the given error code, e.g.
1210 * "no such process". You should use this function in preference to
1211 * strerror(), because it returns a string in UTF-8 encoding, and since
1212 * not all platforms support the strerror() function.
1214 * Returns: a UTF-8 string describing the error code. If the error code
1215 * is unknown, it returns "unknown error (<code>)".
1218 g_strerror (gint errnum)
1224 gint saved_errno = errno;
1226 msg = tofree = NULL;
1228 #ifdef HAVE_STRERROR
1229 msg = strerror (errnum);
1230 if (!g_get_charset (NULL))
1231 msg = tofree = g_locale_to_utf8 (msg, -1, NULL, NULL, NULL);
1237 _g_sprintf (msg, "unknown error (%d)", errnum);
1240 ret = g_intern_string (msg);
1242 errno = saved_errno;
1248 * @signum: the signal number. See the <literal>signal</literal>
1251 * Returns a string describing the given signal, e.g. "Segmentation fault".
1252 * You should use this function in preference to strsignal(), because it
1253 * returns a string in UTF-8 encoding, and since not all platforms support
1254 * the strsignal() function.
1256 * Returns: a UTF-8 string describing the signal. If the signal is unknown,
1257 * it returns "unknown signal (<signum>)".
1260 g_strsignal (gint signum)
1266 msg = tofree = NULL;
1268 #ifdef HAVE_STRSIGNAL
1269 msg = strsignal (signum);
1270 if (!g_get_charset (NULL))
1271 msg = tofree = g_locale_to_utf8 (msg, -1, NULL, NULL, NULL);
1275 msg = tofree = g_strdup_printf ("unknown signal (%d)", signum);
1276 ret = g_intern_string (msg);
1282 /* Functions g_strlcpy and g_strlcat were originally developed by
1283 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
1284 * See http://www.openbsd.org/cgi-bin/man.cgi?query=strlcpy
1285 * for more information.
1289 /* Use the native ones, if available; they might be implemented in assembly */
1291 g_strlcpy (gchar *dest,
1295 g_return_val_if_fail (dest != NULL, 0);
1296 g_return_val_if_fail (src != NULL, 0);
1298 return strlcpy (dest, src, dest_size);
1302 g_strlcat (gchar *dest,
1306 g_return_val_if_fail (dest != NULL, 0);
1307 g_return_val_if_fail (src != NULL, 0);
1309 return strlcat (dest, src, dest_size);
1312 #else /* ! HAVE_STRLCPY */
1315 * @dest: destination buffer
1316 * @src: source buffer
1317 * @dest_size: length of @dest in bytes
1319 * Portability wrapper that calls strlcpy() on systems which have it,
1320 * and emulates strlcpy() otherwise. Copies @src to @dest; @dest is
1321 * guaranteed to be nul-terminated; @src must be nul-terminated;
1322 * @dest_size is the buffer size, not the number of chars to copy.
1324 * At most dest_size - 1 characters will be copied. Always nul-terminates
1325 * (unless dest_size == 0). This function does <emphasis>not</emphasis>
1326 * allocate memory. Unlike strncpy(), this function doesn't pad dest (so
1327 * it's often faster). It returns the size of the attempted result,
1328 * strlen (src), so if @retval >= @dest_size, truncation occurred.
1330 * <note><para>Caveat: strlcpy() is supposedly more secure than
1331 * strcpy() or strncpy(), but if you really want to avoid screwups,
1332 * g_strdup() is an even better idea.</para></note>
1334 * Returns: length of @src
1337 g_strlcpy (gchar *dest,
1341 register gchar *d = dest;
1342 register const gchar *s = src;
1343 register gsize n = dest_size;
1345 g_return_val_if_fail (dest != NULL, 0);
1346 g_return_val_if_fail (src != NULL, 0);
1348 /* Copy as many bytes as will fit */
1349 if (n != 0 && --n != 0)
1352 register gchar c = *s++;
1360 /* If not enough room in dest, add NUL and traverse rest of src */
1369 return s - src - 1; /* count does not include NUL */
1374 * @dest: destination buffer, already containing one nul-terminated string
1375 * @src: source buffer
1376 * @dest_size: length of @dest buffer in bytes (not length of existing string
1379 * Portability wrapper that calls strlcat() on systems which have it,
1380 * and emulates it otherwise. Appends nul-terminated @src string to @dest,
1381 * guaranteeing nul-termination for @dest. The total size of @dest won't
1382 * exceed @dest_size.
1384 * At most dest_size - 1 characters will be copied.
1385 * Unlike strncat, dest_size is the full size of dest, not the space left over.
1386 * This function does NOT allocate memory.
1387 * This always NUL terminates (unless siz == 0 or there were no NUL characters
1388 * in the dest_size characters of dest to start with).
1390 * <note><para>Caveat: this is supposedly a more secure alternative to
1391 * strcat() or strncat(), but for real security g_strconcat() is harder
1392 * to mess up.</para></note>
1394 * Returns: size of attempted result, which is MIN (dest_size, strlen
1395 * (original dest)) + strlen (src), so if retval >= dest_size,
1396 * truncation occurred.
1399 g_strlcat (gchar *dest,
1403 register gchar *d = dest;
1404 register const gchar *s = src;
1405 register gsize bytes_left = dest_size;
1406 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
1408 g_return_val_if_fail (dest != NULL, 0);
1409 g_return_val_if_fail (src != NULL, 0);
1411 /* Find the end of dst and adjust bytes left but don't go past end */
1412 while (*d != 0 && bytes_left-- != 0)
1415 bytes_left = dest_size - dlength;
1417 if (bytes_left == 0)
1418 return dlength + strlen (s);
1422 if (bytes_left != 1)
1431 return dlength + (s - src); /* count does not include NUL */
1433 #endif /* ! HAVE_STRLCPY */
1438 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1440 * Converts all upper case ASCII letters to lower case ASCII letters.
1442 * Return value: a newly-allocated string, with all the upper case
1443 * characters in @str converted to lower case, with
1444 * semantics that exactly match g_ascii_tolower(). (Note
1445 * that this is unlike the old g_strdown(), which modified
1446 * the string in place.)
1449 g_ascii_strdown (const gchar *str,
1454 g_return_val_if_fail (str != NULL, NULL);
1459 result = g_strndup (str, len);
1460 for (s = result; *s; s++)
1461 *s = g_ascii_tolower (*s);
1469 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1471 * Converts all lower case ASCII letters to upper case ASCII letters.
1473 * Return value: a newly allocated string, with all the lower case
1474 * characters in @str converted to upper case, with
1475 * semantics that exactly match g_ascii_toupper(). (Note
1476 * that this is unlike the old g_strup(), which modified
1477 * the string in place.)
1480 g_ascii_strup (const gchar *str,
1485 g_return_val_if_fail (str != NULL, NULL);
1490 result = g_strndup (str, len);
1491 for (s = result; *s; s++)
1492 *s = g_ascii_toupper (*s);
1499 * @string: the string to convert.
1501 * Converts a string to lower case.
1503 * Return value: the string
1505 * Deprecated:2.2: This function is totally broken for the reasons discussed
1506 * in the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown()
1510 g_strdown (gchar *string)
1514 g_return_val_if_fail (string != NULL, NULL);
1516 s = (guchar *) string;
1525 return (gchar *) string;
1530 * @string: the string to convert.
1532 * Converts a string to upper case.
1534 * Return value: the string
1536 * Deprecated:2.2: This function is totally broken for the reasons discussed
1537 * in the g_strncasecmp() docs - use g_ascii_strup() or g_utf8_strup() instead.
1540 g_strup (gchar *string)
1544 g_return_val_if_fail (string != NULL, NULL);
1546 s = (guchar *) string;
1555 return (gchar *) string;
1560 * @string: the string to reverse
1562 * Reverses all of the bytes in a string. For example,
1563 * <literal>g_strreverse ("abcdef")</literal> will result
1566 * Note that g_strreverse() doesn't work on UTF-8 strings
1567 * containing multibyte characters. For that purpose, use
1568 * g_utf8_strreverse().
1570 * Returns: the same pointer passed in as @string
1573 g_strreverse (gchar *string)
1575 g_return_val_if_fail (string != NULL, NULL);
1579 register gchar *h, *t;
1582 t = string + strlen (string) - 1;
1601 * @c: any character.
1603 * Convert a character to ASCII lower case.
1605 * Unlike the standard C library tolower() function, this only
1606 * recognizes standard ASCII letters and ignores the locale, returning
1607 * all non-ASCII characters unchanged, even if they are lower case
1608 * letters in a particular character set. Also unlike the standard
1609 * library function, this takes and returns a char, not an int, so
1610 * don't call it on %EOF but no need to worry about casting to #guchar
1611 * before passing a possibly non-ASCII character in.
1613 * Return value: the result of converting @c to lower case.
1614 * If @c is not an ASCII upper case letter,
1615 * @c is returned unchanged.
1618 g_ascii_tolower (gchar c)
1620 return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1625 * @c: any character.
1627 * Convert a character to ASCII upper case.
1629 * Unlike the standard C library toupper() function, this only
1630 * recognizes standard ASCII letters and ignores the locale, returning
1631 * all non-ASCII characters unchanged, even if they are upper case
1632 * letters in a particular character set. Also unlike the standard
1633 * library function, this takes and returns a char, not an int, so
1634 * don't call it on %EOF but no need to worry about casting to #guchar
1635 * before passing a possibly non-ASCII character in.
1637 * Return value: the result of converting @c to upper case.
1638 * If @c is not an ASCII lower case letter,
1639 * @c is returned unchanged.
1642 g_ascii_toupper (gchar c)
1644 return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1648 * g_ascii_digit_value:
1649 * @c: an ASCII character.
1651 * Determines the numeric value of a character as a decimal
1652 * digit. Differs from g_unichar_digit_value() because it takes
1653 * a char, so there's no worry about sign extension if characters
1656 * Return value: If @c is a decimal digit (according to
1657 * g_ascii_isdigit()), its numeric value. Otherwise, -1.
1660 g_ascii_digit_value (gchar c)
1662 if (g_ascii_isdigit (c))
1668 * g_ascii_xdigit_value:
1669 * @c: an ASCII character.
1671 * Determines the numeric value of a character as a hexidecimal
1672 * digit. Differs from g_unichar_xdigit_value() because it takes
1673 * a char, so there's no worry about sign extension if characters
1676 * Return value: If @c is a hex digit (according to
1677 * g_ascii_isxdigit()), its numeric value. Otherwise, -1.
1680 g_ascii_xdigit_value (gchar c)
1682 if (c >= 'A' && c <= 'F')
1683 return c - 'A' + 10;
1684 if (c >= 'a' && c <= 'f')
1685 return c - 'a' + 10;
1686 return g_ascii_digit_value (c);
1690 * g_ascii_strcasecmp:
1691 * @s1: string to compare with @s2.
1692 * @s2: string to compare with @s1.
1694 * Compare two strings, ignoring the case of ASCII characters.
1696 * Unlike the BSD strcasecmp() function, this only recognizes standard
1697 * ASCII letters and ignores the locale, treating all non-ASCII
1698 * bytes as if they are not letters.
1700 * This function should be used only on strings that are known to be
1701 * in encodings where the bytes corresponding to ASCII letters always
1702 * represent themselves. This includes UTF-8 and the ISO-8859-*
1703 * charsets, but not for instance double-byte encodings like the
1704 * Windows Codepage 932, where the trailing bytes of double-byte
1705 * characters include all ASCII letters. If you compare two CP932
1706 * strings using this function, you will get false matches.
1708 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1709 * or a positive value if @s1 > @s2.
1712 g_ascii_strcasecmp (const gchar *s1,
1717 g_return_val_if_fail (s1 != NULL, 0);
1718 g_return_val_if_fail (s2 != NULL, 0);
1722 c1 = (gint)(guchar) TOLOWER (*s1);
1723 c2 = (gint)(guchar) TOLOWER (*s2);
1729 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1733 * g_ascii_strncasecmp:
1734 * @s1: string to compare with @s2.
1735 * @s2: string to compare with @s1.
1736 * @n: number of characters to compare.
1738 * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1739 * characters after the first @n in each string.
1741 * Unlike the BSD strcasecmp() function, this only recognizes standard
1742 * ASCII letters and ignores the locale, treating all non-ASCII
1743 * characters as if they are not letters.
1745 * The same warning as in g_ascii_strcasecmp() applies: Use this
1746 * function only on strings known to be in encodings where bytes
1747 * corresponding to ASCII letters always represent themselves.
1749 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1750 * or a positive value if @s1 > @s2.
1753 g_ascii_strncasecmp (const gchar *s1,
1759 g_return_val_if_fail (s1 != NULL, 0);
1760 g_return_val_if_fail (s2 != NULL, 0);
1762 while (n && *s1 && *s2)
1765 c1 = (gint)(guchar) TOLOWER (*s1);
1766 c2 = (gint)(guchar) TOLOWER (*s2);
1773 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1781 * @s2: a string to compare with @s1.
1783 * A case-insensitive string comparison, corresponding to the standard
1784 * strcasecmp() function on platforms which support it.
1786 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1787 * or a positive value if @s1 > @s2.
1789 * Deprecated:2.2: See g_strncasecmp() for a discussion of why this function
1790 * is deprecated and how to replace it.
1793 g_strcasecmp (const gchar *s1,
1796 #ifdef HAVE_STRCASECMP
1797 g_return_val_if_fail (s1 != NULL, 0);
1798 g_return_val_if_fail (s2 != NULL, 0);
1800 return strcasecmp (s1, s2);
1804 g_return_val_if_fail (s1 != NULL, 0);
1805 g_return_val_if_fail (s2 != NULL, 0);
1809 /* According to A. Cox, some platforms have islower's that
1810 * don't work right on non-uppercase
1812 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1813 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1819 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1826 * @s2: a string to compare with @s1.
1827 * @n: the maximum number of characters to compare.
1829 * A case-insensitive string comparison, corresponding to the standard
1830 * strncasecmp() function on platforms which support it.
1831 * It is similar to g_strcasecmp() except it only compares the first @n
1832 * characters of the strings.
1834 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1835 * or a positive value if @s1 > @s2.
1837 * Deprecated:2.2: The problem with g_strncasecmp() is that it does the
1838 * comparison by calling toupper()/tolower(). These functions are
1839 * locale-specific and operate on single bytes. However, it is impossible
1840 * to handle things correctly from an I18N standpoint by operating on
1841 * bytes, since characters may be multibyte. Thus g_strncasecmp() is
1842 * broken if your string is guaranteed to be ASCII, since it's
1843 * locale-sensitive, and it's broken if your string is localized, since
1844 * it doesn't work on many encodings at all, including UTF-8, EUC-JP,
1847 * There are therefore two replacement functions: g_ascii_strncasecmp(),
1848 * which only works on ASCII and is not locale-sensitive, and
1849 * g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8.
1852 g_strncasecmp (const gchar *s1,
1856 #ifdef HAVE_STRNCASECMP
1857 return strncasecmp (s1, s2, n);
1861 g_return_val_if_fail (s1 != NULL, 0);
1862 g_return_val_if_fail (s2 != NULL, 0);
1864 while (n && *s1 && *s2)
1867 /* According to A. Cox, some platforms have islower's that
1868 * don't work right on non-uppercase
1870 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1871 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1878 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1886 * @string: the string to convert
1887 * @delimiters: a string containing the current delimiters, or %NULL
1888 * to use the standard delimiters defined in #G_STR_DELIMITERS
1889 * @new_delimiter: the new delimiter character
1891 * Converts any delimiter characters in @string to @new_delimiter.
1892 * Any characters in @string which are found in @delimiters are
1893 * changed to the @new_delimiter character. Modifies @string in place,
1894 * and returns @string itself, not a copy. The return value is to
1895 * allow nesting such as
1897 * g_ascii_strup (g_strdelimit (str, "abc", '?'))
1903 g_strdelimit (gchar *string,
1904 const gchar *delimiters,
1909 g_return_val_if_fail (string != NULL, NULL);
1912 delimiters = G_STR_DELIMITERS;
1914 for (c = string; *c; c++)
1916 if (strchr (delimiters, *c))
1925 * @string: a nul-terminated array of bytes
1926 * @valid_chars: bytes permitted in @string
1927 * @substitutor: replacement character for disallowed bytes
1929 * For each character in @string, if the character is not in
1930 * @valid_chars, replaces the character with @substitutor.
1931 * Modifies @string in place, and return @string itself, not
1932 * a copy. The return value is to allow nesting such as
1934 * g_ascii_strup (g_strcanon (str, "abc", '?'))
1940 g_strcanon (gchar *string,
1941 const gchar *valid_chars,
1946 g_return_val_if_fail (string != NULL, NULL);
1947 g_return_val_if_fail (valid_chars != NULL, NULL);
1949 for (c = string; *c; c++)
1951 if (!strchr (valid_chars, *c))
1960 * @source: a string to compress
1962 * Replaces all escaped characters with their one byte equivalent.
1964 * This function does the reverse conversion of g_strescape().
1966 * Returns: a newly-allocated copy of @source with all escaped
1967 * character compressed
1970 g_strcompress (const gchar *source)
1972 const gchar *p = source, *octal;
1973 gchar *dest = g_malloc (strlen (source) + 1);
1984 g_warning ("g_strcompress: trailing \\");
1986 case '0': case '1': case '2': case '3': case '4':
1987 case '5': case '6': case '7':
1990 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
1992 *q = (*q * 8) + (*p - '0');
2013 default: /* Also handles \" and \\ */
2030 * @source: a string to escape
2031 * @exceptions: a string of characters not to escape in @source
2033 * Escapes the special characters '\b', '\f', '\n', '\r', '\t', '\'
2034 * and '"' in the string @source by inserting a '\' before
2035 * them. Additionally all characters in the range 0x01-0x1F (everything
2036 * below SPACE) and in the range 0x7F-0xFF (all non-ASCII chars) are
2037 * replaced with a '\' followed by their octal representation.
2038 * Characters supplied in @exceptions are not escaped.
2040 * g_strcompress() does the reverse conversion.
2042 * Returns: a newly-allocated copy of @source with certain
2043 * characters escaped. See above.
2046 g_strescape (const gchar *source,
2047 const gchar *exceptions)
2054 g_return_val_if_fail (source != NULL, NULL);
2056 p = (guchar *) source;
2057 /* Each source byte needs maximally four destination chars (\777) */
2058 q = dest = g_malloc (strlen (source) * 4 + 1);
2060 memset (excmap, 0, 256);
2063 guchar *e = (guchar *) exceptions;
2109 if ((*p < ' ') || (*p >= 0177))
2112 *q++ = '0' + (((*p) >> 6) & 07);
2113 *q++ = '0' + (((*p) >> 3) & 07);
2114 *q++ = '0' + ((*p) & 07);
2129 * @string: a string to remove the leading whitespace from
2131 * Removes leading whitespace from a string, by moving the rest
2132 * of the characters forward.
2134 * This function doesn't allocate or reallocate any memory;
2135 * it modifies @string in place. The pointer to @string is
2136 * returned to allow the nesting of functions.
2138 * Also see g_strchomp() and g_strstrip().
2143 g_strchug (gchar *string)
2147 g_return_val_if_fail (string != NULL, NULL);
2149 for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
2152 g_memmove (string, start, strlen ((gchar *) start) + 1);
2159 * @string: a string to remove the trailing whitespace from
2161 * Removes trailing whitespace from a string.
2163 * This function doesn't allocate or reallocate any memory;
2164 * it modifies @string in place. The pointer to @string is
2165 * returned to allow the nesting of functions.
2167 * Also see g_strchug() and g_strstrip().
2172 g_strchomp (gchar *string)
2176 g_return_val_if_fail (string != NULL, NULL);
2178 len = strlen (string);
2181 if (g_ascii_isspace ((guchar) string[len]))
2192 * @string: a string to split
2193 * @delimiter: a string which specifies the places at which to split
2194 * the string. The delimiter is not included in any of the resulting
2195 * strings, unless @max_tokens is reached.
2196 * @max_tokens: the maximum number of pieces to split @string into.
2197 * If this is less than 1, the string is split completely.
2199 * Splits a string into a maximum of @max_tokens pieces, using the given
2200 * @delimiter. If @max_tokens is reached, the remainder of @string is
2201 * appended to the last token.
2203 * As a special case, the result of splitting the empty string "" is an empty
2204 * vector, not a vector containing a single string. The reason for this
2205 * special case is that being able to represent a empty vector is typically
2206 * more useful than consistent handling of empty elements. If you do need
2207 * to represent empty elements, you'll need to check for the empty string
2208 * before calling g_strsplit().
2210 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2211 * g_strfreev() to free it.
2214 g_strsplit (const gchar *string,
2215 const gchar *delimiter,
2218 GSList *string_list = NULL, *slist;
2219 gchar **str_array, *s;
2221 const gchar *remainder;
2223 g_return_val_if_fail (string != NULL, NULL);
2224 g_return_val_if_fail (delimiter != NULL, NULL);
2225 g_return_val_if_fail (delimiter[0] != '\0', NULL);
2228 max_tokens = G_MAXINT;
2231 s = strstr (remainder, delimiter);
2234 gsize delimiter_len = strlen (delimiter);
2236 while (--max_tokens && s)
2240 len = s - remainder;
2241 string_list = g_slist_prepend (string_list,
2242 g_strndup (remainder, len));
2244 remainder = s + delimiter_len;
2245 s = strstr (remainder, delimiter);
2251 string_list = g_slist_prepend (string_list, g_strdup (remainder));
2254 str_array = g_new (gchar*, n + 1);
2256 str_array[n--] = NULL;
2257 for (slist = string_list; slist; slist = slist->next)
2258 str_array[n--] = slist->data;
2260 g_slist_free (string_list);
2267 * @string: The string to be tokenized
2268 * @delimiters: A nul-terminated string containing bytes that are used
2269 * to split the string.
2270 * @max_tokens: The maximum number of tokens to split @string into.
2271 * If this is less than 1, the string is split completely
2273 * Splits @string into a number of tokens not containing any of the characters
2274 * in @delimiter. A token is the (possibly empty) longest string that does not
2275 * contain any of the characters in @delimiters. If @max_tokens is reached, the
2276 * remainder is appended to the last token.
2278 * For example the result of g_strsplit_set ("abc:def/ghi", ":/", -1) is a
2279 * %NULL-terminated vector containing the three strings "abc", "def",
2282 * The result if g_strsplit_set (":def/ghi:", ":/", -1) is a %NULL-terminated
2283 * vector containing the four strings "", "def", "ghi", and "".
2285 * As a special case, the result of splitting the empty string "" is an empty
2286 * vector, not a vector containing a single string. The reason for this
2287 * special case is that being able to represent a empty vector is typically
2288 * more useful than consistent handling of empty elements. If you do need
2289 * to represent empty elements, you'll need to check for the empty string
2290 * before calling g_strsplit_set().
2292 * Note that this function works on bytes not characters, so it can't be used
2293 * to delimit UTF-8 strings for anything but ASCII characters.
2295 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2296 * g_strfreev() to free it.
2301 g_strsplit_set (const gchar *string,
2302 const gchar *delimiters,
2305 gboolean delim_table[256];
2306 GSList *tokens, *list;
2309 const gchar *current;
2313 g_return_val_if_fail (string != NULL, NULL);
2314 g_return_val_if_fail (delimiters != NULL, NULL);
2317 max_tokens = G_MAXINT;
2319 if (*string == '\0')
2321 result = g_new (char *, 1);
2326 memset (delim_table, FALSE, sizeof (delim_table));
2327 for (s = delimiters; *s != '\0'; ++s)
2328 delim_table[*(guchar *)s] = TRUE;
2333 s = current = string;
2336 if (delim_table[*(guchar *)s] && n_tokens + 1 < max_tokens)
2338 token = g_strndup (current, s - current);
2339 tokens = g_slist_prepend (tokens, token);
2348 token = g_strndup (current, s - current);
2349 tokens = g_slist_prepend (tokens, token);
2352 result = g_new (gchar *, n_tokens + 1);
2354 result[n_tokens] = NULL;
2355 for (list = tokens; list != NULL; list = list->next)
2356 result[--n_tokens] = list->data;
2358 g_slist_free (tokens);
2365 * @str_array: a %NULL-terminated array of strings to free
2367 * Frees a %NULL-terminated array of strings, and the array itself.
2368 * If called on a %NULL value, g_strfreev() simply returns.
2371 g_strfreev (gchar **str_array)
2377 for (i = 0; str_array[i] != NULL; i++)
2378 g_free (str_array[i]);
2386 * @str_array: a %NULL-terminated array of strings
2388 * Copies %NULL-terminated array of strings. The copy is a deep copy;
2389 * the new array should be freed by first freeing each string, then
2390 * the array itself. g_strfreev() does this for you. If called
2391 * on a %NULL value, g_strdupv() simply returns %NULL.
2393 * Return value: a new %NULL-terminated array of strings.
2396 g_strdupv (gchar **str_array)
2404 while (str_array[i])
2407 retval = g_new (gchar*, i + 1);
2410 while (str_array[i])
2412 retval[i] = g_strdup (str_array[i]);
2425 * @separator: a string to insert between each of the strings, or %NULL
2426 * @str_array: a %NULL-terminated array of strings to join
2428 * Joins a number of strings together to form one long string, with the
2429 * optional @separator inserted between each of them. The returned string
2430 * should be freed with g_free().
2432 * Returns: a newly-allocated string containing all of the strings joined
2433 * together, with @separator between them
2436 g_strjoinv (const gchar *separator,
2442 g_return_val_if_fail (str_array != NULL, NULL);
2444 if (separator == NULL)
2451 gsize separator_len;
2453 separator_len = strlen (separator);
2454 /* First part, getting length */
2455 len = 1 + strlen (str_array[0]);
2456 for (i = 1; str_array[i] != NULL; i++)
2457 len += strlen (str_array[i]);
2458 len += separator_len * (i - 1);
2460 /* Second part, building string */
2461 string = g_new (gchar, len);
2462 ptr = g_stpcpy (string, *str_array);
2463 for (i = 1; str_array[i] != NULL; i++)
2465 ptr = g_stpcpy (ptr, separator);
2466 ptr = g_stpcpy (ptr, str_array[i]);
2470 string = g_strdup ("");
2477 * @separator: a string to insert between each of the strings, or %NULL
2478 * @...: a %NULL-terminated list of strings to join
2480 * Joins a number of strings together to form one long string, with the
2481 * optional @separator inserted between each of them. The returned string
2482 * should be freed with g_free().
2484 * Returns: a newly-allocated string containing all of the strings joined
2485 * together, with @separator between them
2488 g_strjoin (const gchar *separator,
2494 gsize separator_len;
2497 if (separator == NULL)
2500 separator_len = strlen (separator);
2502 va_start (args, separator);
2504 s = va_arg (args, gchar*);
2508 /* First part, getting length */
2509 len = 1 + strlen (s);
2511 s = va_arg (args, gchar*);
2514 len += separator_len + strlen (s);
2515 s = va_arg (args, gchar*);
2519 /* Second part, building string */
2520 string = g_new (gchar, len);
2522 va_start (args, separator);
2524 s = va_arg (args, gchar*);
2525 ptr = g_stpcpy (string, s);
2527 s = va_arg (args, gchar*);
2530 ptr = g_stpcpy (ptr, separator);
2531 ptr = g_stpcpy (ptr, s);
2532 s = va_arg (args, gchar*);
2536 string = g_strdup ("");
2546 * @haystack: a string
2547 * @haystack_len: the maximum length of @haystack. Note that -1 is
2548 * a valid length, if @haystack is nul-terminated, meaning it will
2549 * search through the whole string.
2550 * @needle: the string to search for
2552 * Searches the string @haystack for the first occurrence
2553 * of the string @needle, limiting the length of the search
2556 * Return value: a pointer to the found occurrence, or
2557 * %NULL if not found.
2560 g_strstr_len (const gchar *haystack,
2561 gssize haystack_len,
2562 const gchar *needle)
2564 g_return_val_if_fail (haystack != NULL, NULL);
2565 g_return_val_if_fail (needle != NULL, NULL);
2567 if (haystack_len < 0)
2568 return strstr (haystack, needle);
2571 const gchar *p = haystack;
2572 gsize needle_len = strlen (needle);
2576 if (needle_len == 0)
2577 return (gchar *)haystack;
2579 if (haystack_len < needle_len)
2582 end = haystack + haystack_len - needle_len;
2584 while (p <= end && *p)
2586 for (i = 0; i < needle_len; i++)
2587 if (p[i] != needle[i])
2602 * @haystack: a nul-terminated string
2603 * @needle: the nul-terminated string to search for
2605 * Searches the string @haystack for the last occurrence
2606 * of the string @needle.
2608 * Return value: a pointer to the found occurrence, or
2609 * %NULL if not found.
2612 g_strrstr (const gchar *haystack,
2613 const gchar *needle)
2620 g_return_val_if_fail (haystack != NULL, NULL);
2621 g_return_val_if_fail (needle != NULL, NULL);
2623 needle_len = strlen (needle);
2624 haystack_len = strlen (haystack);
2626 if (needle_len == 0)
2627 return (gchar *)haystack;
2629 if (haystack_len < needle_len)
2632 p = haystack + haystack_len - needle_len;
2634 while (p >= haystack)
2636 for (i = 0; i < needle_len; i++)
2637 if (p[i] != needle[i])
2651 * @haystack: a nul-terminated string
2652 * @haystack_len: the maximum length of @haystack
2653 * @needle: the nul-terminated string to search for
2655 * Searches the string @haystack for the last occurrence
2656 * of the string @needle, limiting the length of the search
2659 * Return value: a pointer to the found occurrence, or
2660 * %NULL if not found.
2663 g_strrstr_len (const gchar *haystack,
2664 gssize haystack_len,
2665 const gchar *needle)
2667 g_return_val_if_fail (haystack != NULL, NULL);
2668 g_return_val_if_fail (needle != NULL, NULL);
2670 if (haystack_len < 0)
2671 return g_strrstr (haystack, needle);
2674 gsize needle_len = strlen (needle);
2675 const gchar *haystack_max = haystack + haystack_len;
2676 const gchar *p = haystack;
2679 while (p < haystack_max && *p)
2682 if (p < haystack + needle_len)
2687 while (p >= haystack)
2689 for (i = 0; i < needle_len; i++)
2690 if (p[i] != needle[i])
2706 * @str: a nul-terminated string
2707 * @suffix: the nul-terminated suffix to look for
2709 * Looks whether the string @str ends with @suffix.
2711 * Return value: %TRUE if @str end with @suffix, %FALSE otherwise.
2716 g_str_has_suffix (const gchar *str,
2717 const gchar *suffix)
2722 g_return_val_if_fail (str != NULL, FALSE);
2723 g_return_val_if_fail (suffix != NULL, FALSE);
2725 str_len = strlen (str);
2726 suffix_len = strlen (suffix);
2728 if (str_len < suffix_len)
2731 return strcmp (str + str_len - suffix_len, suffix) == 0;
2736 * @str: a nul-terminated string
2737 * @prefix: the nul-terminated prefix to look for
2739 * Looks whether the string @str begins with @prefix.
2741 * Return value: %TRUE if @str begins with @prefix, %FALSE otherwise.
2746 g_str_has_prefix (const gchar *str,
2747 const gchar *prefix)
2752 g_return_val_if_fail (str != NULL, FALSE);
2753 g_return_val_if_fail (prefix != NULL, FALSE);
2755 str_len = strlen (str);
2756 prefix_len = strlen (prefix);
2758 if (str_len < prefix_len)
2761 return strncmp (str, prefix, prefix_len) == 0;
2766 * @str_array: a %NULL-terminated array of strings
2768 * Returns the length of the given %NULL-terminated
2769 * string array @str_array.
2771 * Return value: length of @str_array.
2776 g_strv_length (gchar **str_array)
2780 g_return_val_if_fail (str_array != NULL, 0);
2782 while (str_array[i])