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 <literal>EOF</literal>, 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 <literal>EOF</literal>, 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 <literal>EOF</literal>, 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 <literal>EOF</literal>, 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 <literal>EOF</literal>, 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 <literal>EOF</literal>, 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 <literal>EOF</literal>, 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 <literal>EOF</literal>, 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 <literal>EOF</literal>, 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 <literal>EOF</literal>, 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 <literal>EOF</literal>, 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 #if defined (HAVE_NEWLOCALE) && \
321 defined (HAVE_USELOCALE) && \
322 defined (HAVE_STRTOD_L) && \
323 defined (HAVE_STRTOULL_L) && \
324 defined (HAVE_STRTOLL_L)
325 #define USE_XLOCALE 1
332 static gsize initialized = FALSE;
333 static locale_t C_locale = NULL;
335 if (g_once_init_enter (&initialized))
337 C_locale = newlocale (LC_ALL_MASK, "C", NULL);
338 g_once_init_leave (&initialized, TRUE);
347 * @str: the string to duplicate
349 * Duplicates a string. If @str is %NULL it returns %NULL.
350 * The returned string should be freed with g_free()
351 * when no longer needed.
353 * Returns: a newly-allocated copy of @str
356 g_strdup (const gchar *str)
363 length = strlen (str) + 1;
364 new_str = g_new (char, length);
365 memcpy (new_str, str, length);
375 * @mem: the memory to copy.
376 * @byte_size: the number of bytes to copy.
378 * Allocates @byte_size bytes of memory, and copies @byte_size bytes into it
379 * from @mem. If @mem is %NULL it returns %NULL.
381 * Returns: a pointer to the newly-allocated copy of the memory, or %NULL if @mem
385 g_memdup (gconstpointer mem,
392 new_mem = g_malloc (byte_size);
393 memcpy (new_mem, mem, byte_size);
403 * @str: the string to duplicate
404 * @n: the maximum number of bytes to copy from @str
406 * Duplicates the first @n bytes of a string, returning a newly-allocated
407 * buffer @n + 1 bytes long which will always be nul-terminated.
408 * If @str is less than @n bytes long the buffer is padded with nuls.
409 * If @str is %NULL it returns %NULL.
410 * The returned value should be freed when no longer needed.
413 * To copy a number of characters from a UTF-8 encoded string, use
414 * g_utf8_strncpy() instead.
417 * Returns: a newly-allocated buffer containing the first @n bytes
418 * of @str, nul-terminated
421 g_strndup (const gchar *str,
428 new_str = g_new (gchar, n + 1);
429 strncpy (new_str, str, n);
440 * @length: the length of the new string
441 * @fill_char: the byte to fill the string with
443 * Creates a new string @length bytes long filled with @fill_char.
444 * The returned string should be freed when no longer needed.
446 * Returns: a newly-allocated string filled the @fill_char
449 g_strnfill (gsize length,
454 str = g_new (gchar, length + 1);
455 memset (str, (guchar)fill_char, length);
463 * @dest: destination buffer.
464 * @src: source string.
466 * Copies a nul-terminated string into the dest buffer, include the
467 * trailing nul, and return a pointer to the trailing nul byte.
468 * This is useful for concatenating multiple strings together
469 * without having to repeatedly scan for the end.
471 * Return value: a pointer to trailing nul byte.
474 g_stpcpy (gchar *dest,
478 g_return_val_if_fail (dest != NULL, NULL);
479 g_return_val_if_fail (src != NULL, NULL);
480 return stpcpy (dest, src);
482 register gchar *d = dest;
483 register const gchar *s = src;
485 g_return_val_if_fail (dest != NULL, NULL);
486 g_return_val_if_fail (src != NULL, NULL);
489 while (*s++ != '\0');
497 * @format: a standard printf() format string, but notice
498 * <link linkend="string-precision">string precision pitfalls</link>
499 * @args: the list of parameters to insert into the format string
501 * Similar to the standard C vsprintf() function but safer, since it
502 * calculates the maximum space required and allocates memory to hold
503 * the result. The returned string should be freed with g_free() when
506 * See also g_vasprintf(), which offers the same functionality, but
507 * additionally returns the length of the allocated string.
509 * Returns: a newly-allocated string holding the result
512 g_strdup_vprintf (const gchar *format,
515 gchar *string = NULL;
517 g_vasprintf (&string, format, args);
524 * @format: a standard printf() format string, but notice
525 * <link linkend="string-precision">string precision pitfalls</link>
526 * @...: the parameters to insert into the format string
528 * Similar to the standard C sprintf() function but safer, since it
529 * calculates the maximum space required and allocates memory to hold
530 * the result. The returned string should be freed with g_free() when no
533 * Returns: a newly-allocated string holding the result
536 g_strdup_printf (const gchar *format,
542 va_start (args, format);
543 buffer = g_strdup_vprintf (format, args);
551 * @string1: the first string to add, which must not be %NULL
552 * @...: a %NULL-terminated list of strings to append to the string
554 * Concatenates all of the given strings into one long string.
555 * The returned string should be freed with g_free() when no longer needed.
557 * Note that this function is usually not the right function to use to
558 * assemble a translated message from pieces, since proper translation
559 * often requires the pieces to be reordered.
561 * <warning><para>The variable argument list <emphasis>must</emphasis> end
562 * with %NULL. If you forget the %NULL, g_strconcat() will start appending
563 * random memory junk to your string.</para></warning>
565 * Returns: a newly-allocated string containing all the string arguments
568 g_strconcat (const gchar *string1, ...)
579 l = 1 + strlen (string1);
580 va_start (args, string1);
581 s = va_arg (args, gchar*);
585 s = va_arg (args, gchar*);
589 concat = g_new (gchar, l);
592 ptr = g_stpcpy (ptr, string1);
593 va_start (args, string1);
594 s = va_arg (args, gchar*);
597 ptr = g_stpcpy (ptr, s);
598 s = va_arg (args, gchar*);
607 * @nptr: the string to convert to a numeric value.
608 * @endptr: if non-%NULL, it returns the character after
609 * the last character used in the conversion.
611 * Converts a string to a #gdouble value.
612 * It calls the standard strtod() function to handle the conversion, but
613 * if the string is not completely converted it attempts the conversion
614 * again with g_ascii_strtod(), and returns the best match.
616 * This function should seldom be used. The normal situation when reading
617 * numbers not for human consumption is to use g_ascii_strtod(). Only when
618 * you know that you must expect both locale formatted and C formatted numbers
619 * should you use this. Make sure that you don't pass strings such as comma
620 * separated lists of values, since the commas may be interpreted as a decimal
621 * point in some locales, causing unexpected results.
623 * Return value: the #gdouble value.
626 g_strtod (const gchar *nptr,
634 g_return_val_if_fail (nptr != NULL, 0);
639 val_1 = strtod (nptr, &fail_pos_1);
641 if (fail_pos_1 && fail_pos_1[0] != 0)
642 val_2 = g_ascii_strtod (nptr, &fail_pos_2);
644 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
647 *endptr = fail_pos_1;
653 *endptr = fail_pos_2;
660 * @nptr: the string to convert to a numeric value.
661 * @endptr: if non-%NULL, it returns the character after
662 * the last character used in the conversion.
664 * Converts a string to a #gdouble value.
666 * This function behaves like the standard strtod() function
667 * does in the C locale. It does this without actually changing
668 * the current locale, since that would not be thread-safe.
669 * A limitation of the implementation is that this function
670 * will still accept localized versions of infinities and NANs.
672 * This function is typically used when reading configuration
673 * files or other non-user input that should be locale independent.
674 * To handle input from the user you should normally use the
675 * locale-sensitive system strtod() function.
677 * To convert from a #gdouble to a string in a locale-insensitive
678 * way, use g_ascii_dtostr().
680 * If the correct value would cause overflow, plus or minus <literal>HUGE_VAL</literal>
681 * is returned (according to the sign of the value), and <literal>ERANGE</literal> is
682 * stored in <literal>errno</literal>. If the correct value would cause underflow,
683 * zero is returned and <literal>ERANGE</literal> is stored in <literal>errno</literal>.
685 * This function resets <literal>errno</literal> before calling strtod() so that
686 * you can reliably detect overflow and underflow.
688 * Return value: the #gdouble value.
691 g_ascii_strtod (const gchar *nptr,
696 g_return_val_if_fail (nptr != NULL, 0);
700 return strtod_l (nptr, endptr, get_C_locale ());
707 struct lconv *locale_data;
709 const char *decimal_point;
710 int decimal_point_len;
711 const char *p, *decimal_point_pos;
712 const char *end = NULL; /* Silence gcc */
715 g_return_val_if_fail (nptr != NULL, 0);
720 locale_data = localeconv ();
721 decimal_point = locale_data->decimal_point;
722 decimal_point_len = strlen (decimal_point);
725 decimal_point_len = 1;
728 g_assert (decimal_point_len != 0);
730 decimal_point_pos = NULL;
733 if (decimal_point[0] != '.' ||
734 decimal_point[1] != 0)
737 /* Skip leading space */
738 while (g_ascii_isspace (*p))
741 /* Skip leading optional sign */
742 if (*p == '+' || *p == '-')
746 (p[1] == 'x' || p[1] == 'X'))
749 /* HEX - find the (optional) decimal point */
751 while (g_ascii_isxdigit (*p))
755 decimal_point_pos = p++;
757 while (g_ascii_isxdigit (*p))
760 if (*p == 'p' || *p == 'P')
762 if (*p == '+' || *p == '-')
764 while (g_ascii_isdigit (*p))
769 else if (g_ascii_isdigit (*p) || *p == '.')
771 while (g_ascii_isdigit (*p))
775 decimal_point_pos = p++;
777 while (g_ascii_isdigit (*p))
780 if (*p == 'e' || *p == 'E')
782 if (*p == '+' || *p == '-')
784 while (g_ascii_isdigit (*p))
789 /* For the other cases, we need not convert the decimal point */
792 if (decimal_point_pos)
796 /* We need to convert the '.' to the locale specific decimal point */
797 copy = g_malloc (end - nptr + 1 + decimal_point_len);
800 memcpy (c, nptr, decimal_point_pos - nptr);
801 c += decimal_point_pos - nptr;
802 memcpy (c, decimal_point, decimal_point_len);
803 c += decimal_point_len;
804 memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
805 c += end - (decimal_point_pos + 1);
809 val = strtod (copy, &fail_pos);
810 strtod_errno = errno;
814 if (fail_pos - copy > decimal_point_pos - nptr)
815 fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
817 fail_pos = (char *)nptr + (fail_pos - copy);
827 copy = g_malloc (end - (char *)nptr + 1);
828 memcpy (copy, nptr, end - nptr);
829 *(copy + (end - (char *)nptr)) = 0;
832 val = strtod (copy, &fail_pos);
833 strtod_errno = errno;
837 fail_pos = (char *)nptr + (fail_pos - copy);
845 val = strtod (nptr, &fail_pos);
846 strtod_errno = errno;
852 errno = strtod_errno;
861 * @buffer: A buffer to place the resulting string in
862 * @buf_len: The length of the buffer.
863 * @d: The #gdouble to convert
865 * Converts a #gdouble to a string, using the '.' as
868 * This functions generates enough precision that converting
869 * the string back using g_ascii_strtod() gives the same machine-number
870 * (on machines with IEEE compatible 64bit doubles). It is
871 * guaranteed that the size of the resulting string will never
872 * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes.
874 * Return value: The pointer to the buffer with the converted string.
877 g_ascii_dtostr (gchar *buffer,
881 return g_ascii_formatd (buffer, buf_len, "%.17g", d);
884 #pragma GCC diagnostic push
885 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
889 * @buffer: A buffer to place the resulting string in
890 * @buf_len: The length of the buffer.
891 * @format: The printf()-style format to use for the
892 * code to use for converting.
893 * @d: The #gdouble to convert
895 * Converts a #gdouble to a string, using the '.' as
896 * decimal point. To format the number you pass in
897 * a printf()-style format string. Allowed conversion
898 * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'.
900 * If you just want to want to serialize the value into a
901 * string, use g_ascii_dtostr().
903 * Return value: The pointer to the buffer with the converted string.
906 g_ascii_formatd (gchar *buffer,
914 old_locale = uselocale (get_C_locale ());
915 _g_snprintf (buffer, buf_len, format, d);
916 uselocale (old_locale);
921 struct lconv *locale_data;
923 const char *decimal_point;
924 int decimal_point_len;
929 g_return_val_if_fail (buffer != NULL, NULL);
930 g_return_val_if_fail (format[0] == '%', NULL);
931 g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
933 format_char = format[strlen (format) - 1];
935 g_return_val_if_fail (format_char == 'e' || format_char == 'E' ||
936 format_char == 'f' || format_char == 'F' ||
937 format_char == 'g' || format_char == 'G',
940 if (format[0] != '%')
943 if (strpbrk (format + 1, "'l%"))
946 if (!(format_char == 'e' || format_char == 'E' ||
947 format_char == 'f' || format_char == 'F' ||
948 format_char == 'g' || format_char == 'G'))
951 _g_snprintf (buffer, buf_len, format, d);
954 locale_data = localeconv ();
955 decimal_point = locale_data->decimal_point;
956 decimal_point_len = strlen (decimal_point);
959 decimal_point_len = 1;
962 g_assert (decimal_point_len != 0);
964 if (decimal_point[0] != '.' ||
965 decimal_point[1] != 0)
969 while (g_ascii_isspace (*p))
972 if (*p == '+' || *p == '-')
975 while (isdigit ((guchar)*p))
978 if (strncmp (p, decimal_point, decimal_point_len) == 0)
982 if (decimal_point_len > 1)
984 rest_len = strlen (p + (decimal_point_len-1));
985 memmove (p, p + (decimal_point_len-1), rest_len);
994 #pragma GCC diagnostic pop
996 #define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
997 (c) == '\r' || (c) == '\t' || (c) == '\v')
998 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
999 #define ISLOWER(c) ((c) >= 'a' && (c) <= 'z')
1000 #define ISALPHA(c) (ISUPPER (c) || ISLOWER (c))
1001 #define TOUPPER(c) (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
1002 #define TOLOWER(c) (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
1007 g_parse_long_long (const gchar *nptr,
1008 const gchar **endptr,
1012 /* this code is based on on the strtol(3) code from GNU libc released under
1013 * the GNU Lesser General Public License.
1015 * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
1016 * Free Software Foundation, Inc.
1022 const gchar *s, *save;
1025 g_return_val_if_fail (nptr != NULL, 0);
1028 if (base == 1 || base > 36)
1038 /* Skip white space. */
1039 while (ISSPACE (*s))
1042 if (G_UNLIKELY (!*s))
1045 /* Check for a sign. */
1054 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
1057 if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
1068 /* Save the pointer so we can check later if anything happened. */
1070 cutoff = G_MAXUINT64 / base;
1071 cutlim = G_MAXUINT64 % base;
1078 if (c >= '0' && c <= '9')
1080 else if (ISALPHA (c))
1081 c = TOUPPER (c) - 'A' + 10;
1086 /* Check for overflow. */
1087 if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
1096 /* Check if anything actually happened. */
1100 /* Store in ENDPTR the address of one character
1101 past the last character we converted. */
1105 if (G_UNLIKELY (overflow))
1114 /* We must handle a special case here: the base is 0 or 16 and the
1115 first two characters are '0' and 'x', but the rest are no
1116 hexadecimal digits. This is no error case. We return 0 and
1117 ENDPTR points to the `x`. */
1120 if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
1122 *endptr = &save[-1];
1124 /* There was no number to convert. */
1129 #endif /* !USE_XLOCALE */
1133 * @nptr: the string to convert to a numeric value.
1134 * @endptr: if non-%NULL, it returns the character after
1135 * the last character used in the conversion.
1136 * @base: to be used for the conversion, 2..36 or 0
1138 * Converts a string to a #guint64 value.
1139 * This function behaves like the standard strtoull() function
1140 * does in the C locale. It does this without actually
1141 * changing the current locale, since that would not be
1144 * This function is typically used when reading configuration
1145 * files or other non-user input that should be locale independent.
1146 * To handle input from the user you should normally use the
1147 * locale-sensitive system strtoull() function.
1149 * If the correct value would cause overflow, %G_MAXUINT64
1150 * is returned, and <literal>ERANGE</literal> is stored in <literal>errno</literal>.
1151 * If the base is outside the valid range, zero is returned, and
1152 * <literal>EINVAL</literal> is stored in <literal>errno</literal>.
1153 * If the string conversion fails, zero is returned, and @endptr returns
1154 * @nptr (if @endptr is non-%NULL).
1156 * Return value: the #guint64 value or zero on error.
1161 g_ascii_strtoull (const gchar *nptr,
1166 return strtoull_l (nptr, endptr, base, get_C_locale ());
1171 result = g_parse_long_long (nptr, (const gchar **) endptr, base, &negative);
1173 /* Return the result of the appropriate sign. */
1174 return negative ? -result : result;
1180 * @nptr: the string to convert to a numeric value.
1181 * @endptr: if non-%NULL, it returns the character after
1182 * the last character used in the conversion.
1183 * @base: to be used for the conversion, 2..36 or 0
1185 * Converts a string to a #gint64 value.
1186 * This function behaves like the standard strtoll() function
1187 * does in the C locale. It does this without actually
1188 * changing the current locale, since that would not be
1191 * This function is typically used when reading configuration
1192 * files or other non-user input that should be locale independent.
1193 * To handle input from the user you should normally use the
1194 * locale-sensitive system strtoll() function.
1196 * If the correct value would cause overflow, %G_MAXINT64 or %G_MININT64
1197 * is returned, and <literal>ERANGE</literal> is stored in <literal>errno</literal>.
1198 * If the base is outside the valid range, zero is returned, and
1199 * <literal>EINVAL</literal> is stored in <literal>errno</literal>. If the
1200 * string conversion fails, zero is returned, and @endptr returns @nptr
1201 * (if @endptr is non-%NULL).
1203 * Return value: the #gint64 value or zero on error.
1208 g_ascii_strtoll (const gchar *nptr,
1213 return strtoll_l (nptr, endptr, base, get_C_locale ());
1218 result = g_parse_long_long (nptr, (const gchar **) endptr, base, &negative);
1220 if (negative && result > (guint64) G_MININT64)
1225 else if (!negative && result > (guint64) G_MAXINT64)
1231 return - (gint64) result;
1233 return (gint64) result;
1239 * @errnum: the system error number. See the standard C %errno
1242 * Returns a string corresponding to the given error code, e.g.
1243 * "no such process". You should use this function in preference to
1244 * strerror(), because it returns a string in UTF-8 encoding, and since
1245 * not all platforms support the strerror() function.
1247 * Returns: a UTF-8 string describing the error code. If the error code
1248 * is unknown, it returns "unknown error (<code>)".
1251 g_strerror (gint errnum)
1257 gint saved_errno = errno;
1259 msg = tofree = NULL;
1261 #ifdef HAVE_STRERROR
1262 msg = strerror (errnum);
1263 if (!g_get_charset (NULL))
1264 msg = tofree = g_locale_to_utf8 (msg, -1, NULL, NULL, NULL);
1270 _g_sprintf (msg, "unknown error (%d)", errnum);
1273 ret = g_intern_string (msg);
1275 errno = saved_errno;
1281 * @signum: the signal number. See the <literal>signal</literal>
1284 * Returns a string describing the given signal, e.g. "Segmentation fault".
1285 * You should use this function in preference to strsignal(), because it
1286 * returns a string in UTF-8 encoding, and since not all platforms support
1287 * the strsignal() function.
1289 * Returns: a UTF-8 string describing the signal. If the signal is unknown,
1290 * it returns "unknown signal (<signum>)".
1293 g_strsignal (gint signum)
1299 msg = tofree = NULL;
1301 #ifdef HAVE_STRSIGNAL
1302 msg = strsignal (signum);
1303 if (!g_get_charset (NULL))
1304 msg = tofree = g_locale_to_utf8 (msg, -1, NULL, NULL, NULL);
1308 msg = tofree = g_strdup_printf ("unknown signal (%d)", signum);
1309 ret = g_intern_string (msg);
1315 /* Functions g_strlcpy and g_strlcat were originally developed by
1316 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
1317 * See http://www.openbsd.org/cgi-bin/man.cgi?query=strlcpy
1318 * for more information.
1322 /* Use the native ones, if available; they might be implemented in assembly */
1324 g_strlcpy (gchar *dest,
1328 g_return_val_if_fail (dest != NULL, 0);
1329 g_return_val_if_fail (src != NULL, 0);
1331 return strlcpy (dest, src, dest_size);
1335 g_strlcat (gchar *dest,
1339 g_return_val_if_fail (dest != NULL, 0);
1340 g_return_val_if_fail (src != NULL, 0);
1342 return strlcat (dest, src, dest_size);
1345 #else /* ! HAVE_STRLCPY */
1348 * @dest: destination buffer
1349 * @src: source buffer
1350 * @dest_size: length of @dest in bytes
1352 * Portability wrapper that calls strlcpy() on systems which have it,
1353 * and emulates strlcpy() otherwise. Copies @src to @dest; @dest is
1354 * guaranteed to be nul-terminated; @src must be nul-terminated;
1355 * @dest_size is the buffer size, not the number of chars to copy.
1357 * At most dest_size - 1 characters will be copied. Always nul-terminates
1358 * (unless dest_size == 0). This function does <emphasis>not</emphasis>
1359 * allocate memory. Unlike strncpy(), this function doesn't pad dest (so
1360 * it's often faster). It returns the size of the attempted result,
1361 * strlen (src), so if @retval >= @dest_size, truncation occurred.
1363 * <note><para>Caveat: strlcpy() is supposedly more secure than
1364 * strcpy() or strncpy(), but if you really want to avoid screwups,
1365 * g_strdup() is an even better idea.</para></note>
1367 * Returns: length of @src
1370 g_strlcpy (gchar *dest,
1374 register gchar *d = dest;
1375 register const gchar *s = src;
1376 register gsize n = dest_size;
1378 g_return_val_if_fail (dest != NULL, 0);
1379 g_return_val_if_fail (src != NULL, 0);
1381 /* Copy as many bytes as will fit */
1382 if (n != 0 && --n != 0)
1385 register gchar c = *s++;
1393 /* If not enough room in dest, add NUL and traverse rest of src */
1402 return s - src - 1; /* count does not include NUL */
1407 * @dest: destination buffer, already containing one nul-terminated string
1408 * @src: source buffer
1409 * @dest_size: length of @dest buffer in bytes (not length of existing string
1412 * Portability wrapper that calls strlcat() on systems which have it,
1413 * and emulates it otherwise. Appends nul-terminated @src string to @dest,
1414 * guaranteeing nul-termination for @dest. The total size of @dest won't
1415 * exceed @dest_size.
1417 * At most dest_size - 1 characters will be copied.
1418 * Unlike strncat, dest_size is the full size of dest, not the space left over.
1419 * This function does NOT allocate memory.
1420 * This always NUL terminates (unless siz == 0 or there were no NUL characters
1421 * in the dest_size characters of dest to start with).
1423 * <note><para>Caveat: this is supposedly a more secure alternative to
1424 * strcat() or strncat(), but for real security g_strconcat() is harder
1425 * to mess up.</para></note>
1427 * Returns: size of attempted result, which is MIN (dest_size, strlen
1428 * (original dest)) + strlen (src), so if retval >= dest_size,
1429 * truncation occurred.
1432 g_strlcat (gchar *dest,
1436 register gchar *d = dest;
1437 register const gchar *s = src;
1438 register gsize bytes_left = dest_size;
1439 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
1441 g_return_val_if_fail (dest != NULL, 0);
1442 g_return_val_if_fail (src != NULL, 0);
1444 /* Find the end of dst and adjust bytes left but don't go past end */
1445 while (*d != 0 && bytes_left-- != 0)
1448 bytes_left = dest_size - dlength;
1450 if (bytes_left == 0)
1451 return dlength + strlen (s);
1455 if (bytes_left != 1)
1464 return dlength + (s - src); /* count does not include NUL */
1466 #endif /* ! HAVE_STRLCPY */
1471 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1473 * Converts all upper case ASCII letters to lower case ASCII letters.
1475 * Return value: a newly-allocated string, with all the upper case
1476 * characters in @str converted to lower case, with
1477 * semantics that exactly match g_ascii_tolower(). (Note
1478 * that this is unlike the old g_strdown(), which modified
1479 * the string in place.)
1482 g_ascii_strdown (const gchar *str,
1487 g_return_val_if_fail (str != NULL, NULL);
1492 result = g_strndup (str, len);
1493 for (s = result; *s; s++)
1494 *s = g_ascii_tolower (*s);
1502 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1504 * Converts all lower case ASCII letters to upper case ASCII letters.
1506 * Return value: a newly allocated string, with all the lower case
1507 * characters in @str converted to upper case, with
1508 * semantics that exactly match g_ascii_toupper(). (Note
1509 * that this is unlike the old g_strup(), which modified
1510 * the string in place.)
1513 g_ascii_strup (const gchar *str,
1518 g_return_val_if_fail (str != NULL, NULL);
1523 result = g_strndup (str, len);
1524 for (s = result; *s; s++)
1525 *s = g_ascii_toupper (*s);
1532 * @string: a string.
1534 * Determines if a string is pure ASCII. A string is pure ASCII if it
1535 * contains no bytes with the high bit set.
1537 * Returns: %TRUE if @string is ascii
1542 g_str_is_ascii (const gchar *string)
1546 for (i = 0; string[i]; i++)
1547 if (string[i] & 0x80)
1555 * @string: the string to convert.
1557 * Converts a string to lower case.
1559 * Return value: the string
1561 * Deprecated:2.2: This function is totally broken for the reasons discussed
1562 * in the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown()
1566 g_strdown (gchar *string)
1570 g_return_val_if_fail (string != NULL, NULL);
1572 s = (guchar *) string;
1581 return (gchar *) string;
1586 * @string: the string to convert.
1588 * Converts a string to upper case.
1590 * Return value: the string
1592 * Deprecated:2.2: This function is totally broken for the reasons discussed
1593 * in the g_strncasecmp() docs - use g_ascii_strup() or g_utf8_strup() instead.
1596 g_strup (gchar *string)
1600 g_return_val_if_fail (string != NULL, NULL);
1602 s = (guchar *) string;
1611 return (gchar *) string;
1616 * @string: the string to reverse
1618 * Reverses all of the bytes in a string. For example,
1619 * <literal>g_strreverse ("abcdef")</literal> will result
1622 * Note that g_strreverse() doesn't work on UTF-8 strings
1623 * containing multibyte characters. For that purpose, use
1624 * g_utf8_strreverse().
1626 * Returns: the same pointer passed in as @string
1629 g_strreverse (gchar *string)
1631 g_return_val_if_fail (string != NULL, NULL);
1635 register gchar *h, *t;
1638 t = string + strlen (string) - 1;
1657 * @c: any character.
1659 * Convert a character to ASCII lower case.
1661 * Unlike the standard C library tolower() function, this only
1662 * recognizes standard ASCII letters and ignores the locale, returning
1663 * all non-ASCII characters unchanged, even if they are lower case
1664 * letters in a particular character set. Also unlike the standard
1665 * library function, this takes and returns a char, not an int, so
1666 * don't call it on <literal>EOF</literal> but no need to worry about casting to #guchar
1667 * before passing a possibly non-ASCII character in.
1669 * Return value: the result of converting @c to lower case.
1670 * If @c is not an ASCII upper case letter,
1671 * @c is returned unchanged.
1674 g_ascii_tolower (gchar c)
1676 return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1681 * @c: any character.
1683 * Convert a character to ASCII upper case.
1685 * Unlike the standard C library toupper() function, this only
1686 * recognizes standard ASCII letters and ignores the locale, returning
1687 * all non-ASCII characters unchanged, even if they are upper case
1688 * letters in a particular character set. Also unlike the standard
1689 * library function, this takes and returns a char, not an int, so
1690 * don't call it on <literal>EOF</literal> but no need to worry about casting to #guchar
1691 * before passing a possibly non-ASCII character in.
1693 * Return value: the result of converting @c to upper case.
1694 * If @c is not an ASCII lower case letter,
1695 * @c is returned unchanged.
1698 g_ascii_toupper (gchar c)
1700 return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1704 * g_ascii_digit_value:
1705 * @c: an ASCII character.
1707 * Determines the numeric value of a character as a decimal
1708 * digit. Differs from g_unichar_digit_value() because it takes
1709 * a char, so there's no worry about sign extension if characters
1712 * Return value: If @c is a decimal digit (according to
1713 * g_ascii_isdigit()), its numeric value. Otherwise, -1.
1716 g_ascii_digit_value (gchar c)
1718 if (g_ascii_isdigit (c))
1724 * g_ascii_xdigit_value:
1725 * @c: an ASCII character.
1727 * Determines the numeric value of a character as a hexidecimal
1728 * digit. Differs from g_unichar_xdigit_value() because it takes
1729 * a char, so there's no worry about sign extension if characters
1732 * Return value: If @c is a hex digit (according to
1733 * g_ascii_isxdigit()), its numeric value. Otherwise, -1.
1736 g_ascii_xdigit_value (gchar c)
1738 if (c >= 'A' && c <= 'F')
1739 return c - 'A' + 10;
1740 if (c >= 'a' && c <= 'f')
1741 return c - 'a' + 10;
1742 return g_ascii_digit_value (c);
1746 * g_ascii_strcasecmp:
1747 * @s1: string to compare with @s2.
1748 * @s2: string to compare with @s1.
1750 * Compare two strings, ignoring the case of ASCII characters.
1752 * Unlike the BSD strcasecmp() function, this only recognizes standard
1753 * ASCII letters and ignores the locale, treating all non-ASCII
1754 * bytes as if they are not letters.
1756 * This function should be used only on strings that are known to be
1757 * in encodings where the bytes corresponding to ASCII letters always
1758 * represent themselves. This includes UTF-8 and the ISO-8859-*
1759 * charsets, but not for instance double-byte encodings like the
1760 * Windows Codepage 932, where the trailing bytes of double-byte
1761 * characters include all ASCII letters. If you compare two CP932
1762 * strings using this function, you will get false matches.
1764 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1765 * or a positive value if @s1 > @s2.
1768 g_ascii_strcasecmp (const gchar *s1,
1773 g_return_val_if_fail (s1 != NULL, 0);
1774 g_return_val_if_fail (s2 != NULL, 0);
1778 c1 = (gint)(guchar) TOLOWER (*s1);
1779 c2 = (gint)(guchar) TOLOWER (*s2);
1785 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1789 * g_ascii_strncasecmp:
1790 * @s1: string to compare with @s2.
1791 * @s2: string to compare with @s1.
1792 * @n: number of characters to compare.
1794 * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1795 * characters after the first @n in each string.
1797 * Unlike the BSD strcasecmp() function, this only recognizes standard
1798 * ASCII letters and ignores the locale, treating all non-ASCII
1799 * characters as if they are not letters.
1801 * The same warning as in g_ascii_strcasecmp() applies: Use this
1802 * function only on strings known to be in encodings where bytes
1803 * corresponding to ASCII letters always represent themselves.
1805 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1806 * or a positive value if @s1 > @s2.
1809 g_ascii_strncasecmp (const gchar *s1,
1815 g_return_val_if_fail (s1 != NULL, 0);
1816 g_return_val_if_fail (s2 != NULL, 0);
1818 while (n && *s1 && *s2)
1821 c1 = (gint)(guchar) TOLOWER (*s1);
1822 c2 = (gint)(guchar) TOLOWER (*s2);
1829 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1837 * @s2: a string to compare with @s1.
1839 * A case-insensitive string comparison, corresponding to the standard
1840 * strcasecmp() function on platforms which support it.
1842 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1843 * or a positive value if @s1 > @s2.
1845 * Deprecated:2.2: See g_strncasecmp() for a discussion of why this function
1846 * is deprecated and how to replace it.
1849 g_strcasecmp (const gchar *s1,
1852 #ifdef HAVE_STRCASECMP
1853 g_return_val_if_fail (s1 != NULL, 0);
1854 g_return_val_if_fail (s2 != NULL, 0);
1856 return strcasecmp (s1, s2);
1860 g_return_val_if_fail (s1 != NULL, 0);
1861 g_return_val_if_fail (s2 != NULL, 0);
1865 /* According to A. Cox, some platforms have islower's that
1866 * don't work right on non-uppercase
1868 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1869 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1875 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1882 * @s2: a string to compare with @s1.
1883 * @n: the maximum number of characters to compare.
1885 * A case-insensitive string comparison, corresponding to the standard
1886 * strncasecmp() function on platforms which support it.
1887 * It is similar to g_strcasecmp() except it only compares the first @n
1888 * characters of the strings.
1890 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1891 * or a positive value if @s1 > @s2.
1893 * Deprecated:2.2: The problem with g_strncasecmp() is that it does the
1894 * comparison by calling toupper()/tolower(). These functions are
1895 * locale-specific and operate on single bytes. However, it is impossible
1896 * to handle things correctly from an I18N standpoint by operating on
1897 * bytes, since characters may be multibyte. Thus g_strncasecmp() is
1898 * broken if your string is guaranteed to be ASCII, since it's
1899 * locale-sensitive, and it's broken if your string is localized, since
1900 * it doesn't work on many encodings at all, including UTF-8, EUC-JP,
1903 * There are therefore two replacement techniques: g_ascii_strncasecmp(),
1904 * which only works on ASCII and is not locale-sensitive, and
1905 * g_utf8_casefold() followed by strcmp() on the resulting strings, which is
1906 * good for case-insensitive sorting of UTF-8.
1909 g_strncasecmp (const gchar *s1,
1913 #ifdef HAVE_STRNCASECMP
1914 return strncasecmp (s1, s2, n);
1918 g_return_val_if_fail (s1 != NULL, 0);
1919 g_return_val_if_fail (s2 != NULL, 0);
1921 while (n && *s1 && *s2)
1924 /* According to A. Cox, some platforms have islower's that
1925 * don't work right on non-uppercase
1927 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1928 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1935 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1943 * @string: the string to convert
1944 * @delimiters: (allow-none): a string containing the current delimiters, or %NULL
1945 * to use the standard delimiters defined in #G_STR_DELIMITERS
1946 * @new_delimiter: the new delimiter character
1948 * Converts any delimiter characters in @string to @new_delimiter.
1949 * Any characters in @string which are found in @delimiters are
1950 * changed to the @new_delimiter character. Modifies @string in place,
1951 * and returns @string itself, not a copy. The return value is to
1952 * allow nesting such as
1954 * g_ascii_strup (g_strdelimit (str, "abc", '?'))
1960 g_strdelimit (gchar *string,
1961 const gchar *delimiters,
1966 g_return_val_if_fail (string != NULL, NULL);
1969 delimiters = G_STR_DELIMITERS;
1971 for (c = string; *c; c++)
1973 if (strchr (delimiters, *c))
1982 * @string: a nul-terminated array of bytes
1983 * @valid_chars: bytes permitted in @string
1984 * @substitutor: replacement character for disallowed bytes
1986 * For each character in @string, if the character is not in
1987 * @valid_chars, replaces the character with @substitutor.
1988 * Modifies @string in place, and return @string itself, not
1989 * a copy. The return value is to allow nesting such as
1991 * g_ascii_strup (g_strcanon (str, "abc", '?'))
1997 g_strcanon (gchar *string,
1998 const gchar *valid_chars,
2003 g_return_val_if_fail (string != NULL, NULL);
2004 g_return_val_if_fail (valid_chars != NULL, NULL);
2006 for (c = string; *c; c++)
2008 if (!strchr (valid_chars, *c))
2017 * @source: a string to compress
2019 * Replaces all escaped characters with their one byte equivalent.
2021 * This function does the reverse conversion of g_strescape().
2023 * Returns: a newly-allocated copy of @source with all escaped
2024 * character compressed
2027 g_strcompress (const gchar *source)
2029 const gchar *p = source, *octal;
2033 g_return_val_if_fail (source != NULL, NULL);
2035 dest = g_malloc (strlen (source) + 1);
2046 g_warning ("g_strcompress: trailing \\");
2048 case '0': case '1': case '2': case '3': case '4':
2049 case '5': case '6': case '7':
2052 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
2054 *q = (*q * 8) + (*p - '0');
2078 default: /* Also handles \" and \\ */
2095 * @source: a string to escape
2096 * @exceptions: a string of characters not to escape in @source
2098 * Escapes the special characters '\b', '\f', '\n', '\r', '\t', '\v', '\'
2099 * and '"' in the string @source by inserting a '\' before
2100 * them. Additionally all characters in the range 0x01-0x1F (everything
2101 * below SPACE) and in the range 0x7F-0xFF (all non-ASCII chars) are
2102 * replaced with a '\' followed by their octal representation.
2103 * Characters supplied in @exceptions are not escaped.
2105 * g_strcompress() does the reverse conversion.
2107 * Returns: a newly-allocated copy of @source with certain
2108 * characters escaped. See above.
2111 g_strescape (const gchar *source,
2112 const gchar *exceptions)
2119 g_return_val_if_fail (source != NULL, NULL);
2121 p = (guchar *) source;
2122 /* Each source byte needs maximally four destination chars (\777) */
2123 q = dest = g_malloc (strlen (source) * 4 + 1);
2125 memset (excmap, 0, 256);
2128 guchar *e = (guchar *) exceptions;
2178 if ((*p < ' ') || (*p >= 0177))
2181 *q++ = '0' + (((*p) >> 6) & 07);
2182 *q++ = '0' + (((*p) >> 3) & 07);
2183 *q++ = '0' + ((*p) & 07);
2198 * @string: a string to remove the leading whitespace from
2200 * Removes leading whitespace from a string, by moving the rest
2201 * of the characters forward.
2203 * This function doesn't allocate or reallocate any memory;
2204 * it modifies @string in place. The pointer to @string is
2205 * returned to allow the nesting of functions.
2207 * Also see g_strchomp() and g_strstrip().
2212 g_strchug (gchar *string)
2216 g_return_val_if_fail (string != NULL, NULL);
2218 for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
2221 g_memmove (string, start, strlen ((gchar *) start) + 1);
2228 * @string: a string to remove the trailing whitespace from
2230 * Removes trailing whitespace from a string.
2232 * This function doesn't allocate or reallocate any memory;
2233 * it modifies @string in place. The pointer to @string is
2234 * returned to allow the nesting of functions.
2236 * Also see g_strchug() and g_strstrip().
2241 g_strchomp (gchar *string)
2245 g_return_val_if_fail (string != NULL, NULL);
2247 len = strlen (string);
2250 if (g_ascii_isspace ((guchar) string[len]))
2261 * @string: a string to split
2262 * @delimiter: a string which specifies the places at which to split
2263 * the string. The delimiter is not included in any of the resulting
2264 * strings, unless @max_tokens is reached.
2265 * @max_tokens: the maximum number of pieces to split @string into.
2266 * If this is less than 1, the string is split completely.
2268 * Splits a string into a maximum of @max_tokens pieces, using the given
2269 * @delimiter. If @max_tokens is reached, the remainder of @string is
2270 * appended to the last token.
2272 * As a special case, the result of splitting the empty string "" is an empty
2273 * vector, not a vector containing a single string. The reason for this
2274 * special case is that being able to represent a empty vector is typically
2275 * more useful than consistent handling of empty elements. If you do need
2276 * to represent empty elements, you'll need to check for the empty string
2277 * before calling g_strsplit().
2279 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2280 * g_strfreev() to free it.
2283 g_strsplit (const gchar *string,
2284 const gchar *delimiter,
2287 GSList *string_list = NULL, *slist;
2288 gchar **str_array, *s;
2290 const gchar *remainder;
2292 g_return_val_if_fail (string != NULL, NULL);
2293 g_return_val_if_fail (delimiter != NULL, NULL);
2294 g_return_val_if_fail (delimiter[0] != '\0', NULL);
2297 max_tokens = G_MAXINT;
2300 s = strstr (remainder, delimiter);
2303 gsize delimiter_len = strlen (delimiter);
2305 while (--max_tokens && s)
2309 len = s - remainder;
2310 string_list = g_slist_prepend (string_list,
2311 g_strndup (remainder, len));
2313 remainder = s + delimiter_len;
2314 s = strstr (remainder, delimiter);
2320 string_list = g_slist_prepend (string_list, g_strdup (remainder));
2323 str_array = g_new (gchar*, n + 1);
2325 str_array[n--] = NULL;
2326 for (slist = string_list; slist; slist = slist->next)
2327 str_array[n--] = slist->data;
2329 g_slist_free (string_list);
2336 * @string: The string to be tokenized
2337 * @delimiters: A nul-terminated string containing bytes that are used
2338 * to split the string.
2339 * @max_tokens: The maximum number of tokens to split @string into.
2340 * If this is less than 1, the string is split completely
2342 * Splits @string into a number of tokens not containing any of the characters
2343 * in @delimiter. A token is the (possibly empty) longest string that does not
2344 * contain any of the characters in @delimiters. If @max_tokens is reached, the
2345 * remainder is appended to the last token.
2347 * For example the result of g_strsplit_set ("abc:def/ghi", ":/", -1) is a
2348 * %NULL-terminated vector containing the three strings "abc", "def",
2351 * The result if g_strsplit_set (":def/ghi:", ":/", -1) is a %NULL-terminated
2352 * vector containing the four strings "", "def", "ghi", and "".
2354 * As a special case, the result of splitting the empty string "" is an empty
2355 * vector, not a vector containing a single string. The reason for this
2356 * special case is that being able to represent a empty vector is typically
2357 * more useful than consistent handling of empty elements. If you do need
2358 * to represent empty elements, you'll need to check for the empty string
2359 * before calling g_strsplit_set().
2361 * Note that this function works on bytes not characters, so it can't be used
2362 * to delimit UTF-8 strings for anything but ASCII characters.
2364 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2365 * g_strfreev() to free it.
2370 g_strsplit_set (const gchar *string,
2371 const gchar *delimiters,
2374 gboolean delim_table[256];
2375 GSList *tokens, *list;
2378 const gchar *current;
2382 g_return_val_if_fail (string != NULL, NULL);
2383 g_return_val_if_fail (delimiters != NULL, NULL);
2386 max_tokens = G_MAXINT;
2388 if (*string == '\0')
2390 result = g_new (char *, 1);
2395 memset (delim_table, FALSE, sizeof (delim_table));
2396 for (s = delimiters; *s != '\0'; ++s)
2397 delim_table[*(guchar *)s] = TRUE;
2402 s = current = string;
2405 if (delim_table[*(guchar *)s] && n_tokens + 1 < max_tokens)
2407 token = g_strndup (current, s - current);
2408 tokens = g_slist_prepend (tokens, token);
2417 token = g_strndup (current, s - current);
2418 tokens = g_slist_prepend (tokens, token);
2421 result = g_new (gchar *, n_tokens + 1);
2423 result[n_tokens] = NULL;
2424 for (list = tokens; list != NULL; list = list->next)
2425 result[--n_tokens] = list->data;
2427 g_slist_free (tokens);
2434 * @str_array: a %NULL-terminated array of strings to free
2436 * Frees a %NULL-terminated array of strings, and the array itself.
2437 * If called on a %NULL value, g_strfreev() simply returns.
2440 g_strfreev (gchar **str_array)
2446 for (i = 0; str_array[i] != NULL; i++)
2447 g_free (str_array[i]);
2455 * @str_array: a %NULL-terminated array of strings
2457 * Copies %NULL-terminated array of strings. The copy is a deep copy;
2458 * the new array should be freed by first freeing each string, then
2459 * the array itself. g_strfreev() does this for you. If called
2460 * on a %NULL value, g_strdupv() simply returns %NULL.
2462 * Return value: a new %NULL-terminated array of strings.
2465 g_strdupv (gchar **str_array)
2473 while (str_array[i])
2476 retval = g_new (gchar*, i + 1);
2479 while (str_array[i])
2481 retval[i] = g_strdup (str_array[i]);
2494 * @separator: (allow-none): a string to insert between each of the strings, or %NULL
2495 * @str_array: a %NULL-terminated array of strings to join
2497 * Joins a number of strings together to form one long string, with the
2498 * optional @separator inserted between each of them. The returned string
2499 * should be freed with g_free().
2501 * Returns: a newly-allocated string containing all of the strings joined
2502 * together, with @separator between them
2505 g_strjoinv (const gchar *separator,
2511 g_return_val_if_fail (str_array != NULL, NULL);
2513 if (separator == NULL)
2520 gsize separator_len;
2522 separator_len = strlen (separator);
2523 /* First part, getting length */
2524 len = 1 + strlen (str_array[0]);
2525 for (i = 1; str_array[i] != NULL; i++)
2526 len += strlen (str_array[i]);
2527 len += separator_len * (i - 1);
2529 /* Second part, building string */
2530 string = g_new (gchar, len);
2531 ptr = g_stpcpy (string, *str_array);
2532 for (i = 1; str_array[i] != NULL; i++)
2534 ptr = g_stpcpy (ptr, separator);
2535 ptr = g_stpcpy (ptr, str_array[i]);
2539 string = g_strdup ("");
2546 * @separator: (allow-none): a string to insert between each of the strings, or %NULL
2547 * @...: a %NULL-terminated list of strings to join
2549 * Joins a number of strings together to form one long string, with the
2550 * optional @separator inserted between each of them. The returned string
2551 * should be freed with g_free().
2553 * Returns: a newly-allocated string containing all of the strings joined
2554 * together, with @separator between them
2557 g_strjoin (const gchar *separator,
2563 gsize separator_len;
2566 if (separator == NULL)
2569 separator_len = strlen (separator);
2571 va_start (args, separator);
2573 s = va_arg (args, gchar*);
2577 /* First part, getting length */
2578 len = 1 + strlen (s);
2580 s = va_arg (args, gchar*);
2583 len += separator_len + strlen (s);
2584 s = va_arg (args, gchar*);
2588 /* Second part, building string */
2589 string = g_new (gchar, len);
2591 va_start (args, separator);
2593 s = va_arg (args, gchar*);
2594 ptr = g_stpcpy (string, s);
2596 s = va_arg (args, gchar*);
2599 ptr = g_stpcpy (ptr, separator);
2600 ptr = g_stpcpy (ptr, s);
2601 s = va_arg (args, gchar*);
2605 string = g_strdup ("");
2615 * @haystack: a string
2616 * @haystack_len: the maximum length of @haystack. Note that -1 is
2617 * a valid length, if @haystack is nul-terminated, meaning it will
2618 * search through the whole string.
2619 * @needle: the string to search for
2621 * Searches the string @haystack for the first occurrence
2622 * of the string @needle, limiting the length of the search
2625 * Return value: a pointer to the found occurrence, or
2626 * %NULL if not found.
2629 g_strstr_len (const gchar *haystack,
2630 gssize haystack_len,
2631 const gchar *needle)
2633 g_return_val_if_fail (haystack != NULL, NULL);
2634 g_return_val_if_fail (needle != NULL, NULL);
2636 if (haystack_len < 0)
2637 return strstr (haystack, needle);
2640 const gchar *p = haystack;
2641 gsize needle_len = strlen (needle);
2645 if (needle_len == 0)
2646 return (gchar *)haystack;
2648 if (haystack_len < needle_len)
2651 end = haystack + haystack_len - needle_len;
2653 while (p <= end && *p)
2655 for (i = 0; i < needle_len; i++)
2656 if (p[i] != needle[i])
2671 * @haystack: a nul-terminated string
2672 * @needle: the nul-terminated string to search for
2674 * Searches the string @haystack for the last occurrence
2675 * of the string @needle.
2677 * Return value: a pointer to the found occurrence, or
2678 * %NULL if not found.
2681 g_strrstr (const gchar *haystack,
2682 const gchar *needle)
2689 g_return_val_if_fail (haystack != NULL, NULL);
2690 g_return_val_if_fail (needle != NULL, NULL);
2692 needle_len = strlen (needle);
2693 haystack_len = strlen (haystack);
2695 if (needle_len == 0)
2696 return (gchar *)haystack;
2698 if (haystack_len < needle_len)
2701 p = haystack + haystack_len - needle_len;
2703 while (p >= haystack)
2705 for (i = 0; i < needle_len; i++)
2706 if (p[i] != needle[i])
2720 * @haystack: a nul-terminated string
2721 * @haystack_len: the maximum length of @haystack
2722 * @needle: the nul-terminated string to search for
2724 * Searches the string @haystack for the last occurrence
2725 * of the string @needle, limiting the length of the search
2728 * Return value: a pointer to the found occurrence, or
2729 * %NULL if not found.
2732 g_strrstr_len (const gchar *haystack,
2733 gssize haystack_len,
2734 const gchar *needle)
2736 g_return_val_if_fail (haystack != NULL, NULL);
2737 g_return_val_if_fail (needle != NULL, NULL);
2739 if (haystack_len < 0)
2740 return g_strrstr (haystack, needle);
2743 gsize needle_len = strlen (needle);
2744 const gchar *haystack_max = haystack + haystack_len;
2745 const gchar *p = haystack;
2748 while (p < haystack_max && *p)
2751 if (p < haystack + needle_len)
2756 while (p >= haystack)
2758 for (i = 0; i < needle_len; i++)
2759 if (p[i] != needle[i])
2775 * @str: a nul-terminated string
2776 * @suffix: the nul-terminated suffix to look for
2778 * Looks whether the string @str ends with @suffix.
2780 * Return value: %TRUE if @str end with @suffix, %FALSE otherwise.
2785 g_str_has_suffix (const gchar *str,
2786 const gchar *suffix)
2791 g_return_val_if_fail (str != NULL, FALSE);
2792 g_return_val_if_fail (suffix != NULL, FALSE);
2794 str_len = strlen (str);
2795 suffix_len = strlen (suffix);
2797 if (str_len < suffix_len)
2800 return strcmp (str + str_len - suffix_len, suffix) == 0;
2805 * @str: a nul-terminated string
2806 * @prefix: the nul-terminated prefix to look for
2808 * Looks whether the string @str begins with @prefix.
2810 * Return value: %TRUE if @str begins with @prefix, %FALSE otherwise.
2815 g_str_has_prefix (const gchar *str,
2816 const gchar *prefix)
2821 g_return_val_if_fail (str != NULL, FALSE);
2822 g_return_val_if_fail (prefix != NULL, FALSE);
2824 str_len = strlen (str);
2825 prefix_len = strlen (prefix);
2827 if (str_len < prefix_len)
2830 return strncmp (str, prefix, prefix_len) == 0;
2835 * @str_array: a %NULL-terminated array of strings
2837 * Returns the length of the given %NULL-terminated
2838 * string array @str_array.
2840 * Return value: length of @str_array.
2845 g_strv_length (gchar **str_array)
2849 g_return_val_if_fail (str_array != NULL, 0);
2851 while (str_array[i])
2858 index_add_folded (GPtrArray *array,
2864 normal = g_utf8_normalize (start, end - start, G_NORMALIZE_ALL_COMPOSE);
2866 /* TODO: Invent time machine. Converse with Mustafa Ataturk... */
2867 if (strstr (normal, "ı") || strstr (normal, "İ"))
2872 tmp = g_string_new (NULL);
2878 i = strstr (s, "ı");
2879 I = strstr (s, "İ");
2892 g_string_append_len (tmp, s, e - s);
2893 g_string_append_c (tmp, 'i');
2894 s = g_utf8_next_char (e);
2897 g_string_append (tmp, s);
2899 normal = g_string_free (tmp, FALSE);
2902 g_ptr_array_add (array, g_utf8_casefold (normal, -1));
2907 split_words (const gchar *value)
2909 const gchar *start = NULL;
2913 result = g_ptr_array_new ();
2915 for (s = value; *s; s = g_utf8_next_char (s))
2917 gunichar c = g_utf8_get_char (s);
2921 if (g_unichar_isalnum (c) || g_unichar_ismark (c))
2926 if (!g_unichar_isalnum (c) && !g_unichar_ismark (c))
2928 index_add_folded (result, start, s);
2935 index_add_folded (result, start, s);
2937 g_ptr_array_add (result, NULL);
2939 return (gchar **) g_ptr_array_free (result, FALSE);
2943 * g_str_tokenize_and_fold:
2945 * @translit_locale: (allow-none): the language code (like 'de' or
2946 * 'en_GB') from which @string originates
2947 * @ascii_alternates: (out) (transfer full) (array zero-terminated=1): a
2948 * return location for ASCII alternates
2950 * Tokenises @string and performs folding on each token.
2952 * A token is a non-empty sequence of alphanumeric characters in the
2953 * source string, separated by non-alphanumeric characters. An
2954 * "alphanumeric" character for this purpose is one that matches
2955 * g_unichar_isalnum() or g_unichar_ismark().
2957 * Each token is then (Unicode) normalised and case-folded. If
2958 * @ascii_alternates is non-%NULL and some of the returned tokens
2959 * contain non-ASCII characters, ASCII alternatives will be generated.
2961 * The number of ASCII alternatives that are generated and the method
2962 * for doing so is unspecified, but @translit_locale (if specified) may
2963 * improve the transliteration if the language of the source string is
2966 * Returns: the folded tokens
2971 g_str_tokenize_and_fold (const gchar *string,
2972 const gchar *translit_locale,
2973 gchar ***ascii_alternates)
2977 if (ascii_alternates && g_str_is_ascii (string))
2979 *ascii_alternates = g_new0 (gchar *, 0 + 1);
2980 ascii_alternates = NULL;
2983 result = split_words (string);
2985 /* TODO: proper iconv transliteration (locale-dependent) */
2986 if (ascii_alternates)
2990 n = g_strv_length (result);
2991 *ascii_alternates = g_new (gchar *, n + 1);
2994 for (i = 0; i < n; i++)
2996 if (!g_str_is_ascii (result[i]))
3003 decomposed = g_utf8_normalize (result[i], -1, G_NORMALIZE_ALL);
3004 ascii = g_malloc (strlen (decomposed) + 1);
3006 for (k = 0; decomposed[k]; k++)
3007 if (~decomposed[k] & 0x80)
3008 ascii[l++] = decomposed[k];
3011 (*ascii_alternates)[j++] = ascii;
3012 g_free (decomposed);
3016 (*ascii_alternates)[j] = NULL;
3023 * g_str_match_string:
3024 * @search_term: the search term from the user
3025 * @potential_hit: the text that may be a hit
3026 * @accept_alternates: %TRUE to accept ASCII alternates
3028 * Checks if a search conducted for @search_term should match
3031 * This function calls g_str_tokenize_and_fold() on both
3032 * @search_term and @potential_hit. ASCII alternates are never taken
3033 * for @search_term but will be taken for @potential_hit according to
3034 * the value of @accept_alternates.
3036 * A hit occurs when each folded token in @search_term is a prefix of a
3037 * folded token from @potential_hit.
3039 * Depending on how you're performing the search, it will typically be
3040 * faster to call g_str_tokenize_and_fold() on each string in
3041 * your corpus and build an index on the returned folded tokens, then
3042 * call g_str_tokenize_and_fold() on the search term and
3043 * perform lookups into that index.
3045 * As some examples, searching for "fred" would match the potential hit
3046 * "Smith, Fred" and also "Frédéric". Searching for "Fréd" would match
3047 * "Frédéric" but not "Frederic" (due to the one-directional nature of
3048 * accent matching). Searching "fo" would match "Foo" and "Bar Foo
3049 * Baz", but not "SFO" (because no word as "fo" as a prefix).
3051 * Returns: %TRUE if @potential_hit is a hit
3056 g_str_match_string (const gchar *search_term,
3057 const gchar *potential_hit,
3058 gboolean accept_alternates)
3060 gchar **alternates = NULL;
3061 gchar **term_tokens;
3066 term_tokens = g_str_tokenize_and_fold (search_term, NULL, NULL);
3067 hit_tokens = g_str_tokenize_and_fold (potential_hit, NULL, accept_alternates ? &alternates : NULL);
3071 for (i = 0; term_tokens[i]; i++)
3073 for (j = 0; hit_tokens[j]; j++)
3074 if (g_str_has_prefix (hit_tokens[j], term_tokens[i]))
3077 if (accept_alternates)
3078 for (j = 0; alternates[j]; j++)
3079 if (g_str_has_prefix (alternates[j], term_tokens[i]))
3089 g_strfreev (term_tokens);
3090 g_strfreev (hit_tokens);
3091 g_strfreev (alternates);