GLib 2.33.14
[platform/upstream/glib.git] / glib / gstrfuncs.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/.
25  */
26
27 /*
28  * MT safe
29  */
30
31 #include "config.h"
32
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <locale.h>
37 #include <string.h>
38 #include <locale.h>
39 #include <errno.h>
40 #include <ctype.h>              /* For tolower() */
41
42 #ifdef HAVE_XLOCALE_H
43 /* Needed on BSD/OS X for e.g. strtod_l */
44 #include <xlocale.h>
45 #endif
46
47 #ifdef G_OS_WIN32
48 #include <windows.h>
49 #endif
50
51 /* do not include <unistd.h> here, it may interfere with g_strsignal() */
52
53 #include "gstrfuncs.h"
54
55 #include "gprintf.h"
56 #include "gprintfint.h"
57 #include "glibintl.h"
58
59
60 /**
61  * SECTION:string_utils
62  * @title: String Utility Functions
63  * @short_description: various string-related functions
64  *
65  * This section describes a number of utility functions for creating,
66  * duplicating, and manipulating strings.
67  *
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>&lt;glib/gprintf.h&gt;</literal> in order to use the GLib
75  * printf() functions.
76  *
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>&percnt;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>&percnt;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>&percnt;Ns</literal>. If your intention is
87  * to format strings for a certain number of columns, then
88  * <literal>&percnt;Ns</literal> is not a correct solution anyway, since it
89  * fails to take wide characters (see g_unichar_iswide()) into account.
90  * </para>
91  */
92
93 /**
94  * g_ascii_isalnum:
95  * @c: any character
96  *
97  * Determines whether a character is alphanumeric.
98  *
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.
105  *
106  * Returns: %TRUE if @c is an ASCII alphanumeric character
107  */
108
109 /**
110  * g_ascii_isalpha:
111  * @c: any character
112  *
113  * Determines whether a character is alphabetic (i.e. a letter).
114  *
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.
121  *
122  * Returns: %TRUE if @c is an ASCII alphabetic character
123  */
124
125 /**
126  * g_ascii_iscntrl:
127  * @c: any character
128  *
129  * Determines whether a character is a control character.
130  *
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.
137  *
138  * Returns: %TRUE if @c is an ASCII control character.
139  */
140
141 /**
142  * g_ascii_isdigit:
143  * @c: any character
144  *
145  * Determines whether a character is digit (0-9).
146  *
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.
151  *
152  * Returns: %TRUE if @c is an ASCII digit.
153  */
154
155 /**
156  * g_ascii_isgraph:
157  * @c: any character
158  *
159  * Determines whether a character is a printing character and not a space.
160  *
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.
167  *
168  * Returns: %TRUE if @c is an ASCII printing character other than space.
169  */
170
171 /**
172  * g_ascii_islower:
173  * @c: any character
174  *
175  * Determines whether a character is an ASCII lower case letter.
176  *
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.
184  *
185  * Returns: %TRUE if @c is an ASCII lower case letter
186  */
187
188 /**
189  * g_ascii_isprint:
190  * @c: any character
191  *
192  * Determines whether a character is a printing character.
193  *
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.
200  *
201  * Returns: %TRUE if @c is an ASCII printing character.
202  */
203
204 /**
205  * g_ascii_ispunct:
206  * @c: any character
207  *
208  * Determines whether a character is a punctuation character.
209  *
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.
216  *
217  * Returns: %TRUE if @c is an ASCII punctuation character.
218  */
219
220 /**
221  * g_ascii_isspace:
222  * @c: any character
223  *
224  * Determines whether a character is a white-space character.
225  *
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.
232  *
233  * Returns: %TRUE if @c is an ASCII white-space character
234  */
235
236 /**
237  * g_ascii_isupper:
238  * @c: any character
239  *
240  * Determines whether a character is an ASCII upper case letter.
241  *
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
248  * character in.
249  *
250  * Returns: %TRUE if @c is an ASCII upper case letter
251  */
252
253 /**
254  * g_ascii_isxdigit:
255  * @c: any character
256  *
257  * Determines whether a character is a hexadecimal-digit character.
258  *
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.
263  *
264  * Returns: %TRUE if @c is an ASCII hexadecimal-digit character.
265  */
266
267 /**
268  * G_ASCII_DTOSTR_BUF_SIZE:
269  *
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.
273  *
274  * The typical usage would be something like:
275  * |[
276  *   char buf[G_ASCII_DTOSTR_BUF_SIZE];
277  *
278  *   fprintf (out, "value=&percnt;s\n", g_ascii_dtostr (buf, sizeof (buf), value));
279  * ]|
280  */
281
282 /**
283  * g_strstrip:
284  * @string: a string to remove the leading and trailing whitespace from
285  *
286  * Removes leading and trailing whitespace from a string.
287  * See g_strchomp() and g_strchug().
288  *
289  * Returns: @string
290  */
291
292 /**
293  * G_STR_DELIMITERS:
294  *
295  * The standard delimiters, used in g_strdelimit().
296  */
297
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 */
316 };
317
318 const guint16 * const g_ascii_table = ascii_table_data;
319
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
326 #endif
327
328 #ifdef USE_XLOCALE
329 static locale_t
330 get_C_locale (void)
331 {
332   static gsize initialized = FALSE;
333   static locale_t C_locale = NULL;
334
335   if (g_once_init_enter (&initialized))
336     {
337       C_locale = newlocale (LC_ALL_MASK, "C", NULL);
338       g_once_init_leave (&initialized, TRUE);
339     }
340
341   return C_locale;
342 }
343 #endif
344
345 /**
346  * g_strdup:
347  * @str: the string to duplicate
348  *
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.
352  *
353  * Returns: a newly-allocated copy of @str
354  */
355 gchar*
356 g_strdup (const gchar *str)
357 {
358   gchar *new_str;
359   gsize length;
360
361   if (str)
362     {
363       length = strlen (str) + 1;
364       new_str = g_new (char, length);
365       memcpy (new_str, str, length);
366     }
367   else
368     new_str = NULL;
369
370   return new_str;
371 }
372
373 /**
374  * g_memdup:
375  * @mem: the memory to copy.
376  * @byte_size: the number of bytes to copy.
377  *
378  * Allocates @byte_size bytes of memory, and copies @byte_size bytes into it
379  * from @mem. If @mem is %NULL it returns %NULL.
380  *
381  * Returns: a pointer to the newly-allocated copy of the memory, or %NULL if @mem
382  *  is %NULL.
383  */
384 gpointer
385 g_memdup (gconstpointer mem,
386           guint         byte_size)
387 {
388   gpointer new_mem;
389
390   if (mem)
391     {
392       new_mem = g_malloc (byte_size);
393       memcpy (new_mem, mem, byte_size);
394     }
395   else
396     new_mem = NULL;
397
398   return new_mem;
399 }
400
401 /**
402  * g_strndup:
403  * @str: the string to duplicate
404  * @n: the maximum number of bytes to copy from @str
405  *
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.
411  *
412  * <note><para>
413  * To copy a number of characters from a UTF-8 encoded string, use
414  * g_utf8_strncpy() instead.
415  * </para></note>
416  *
417  * Returns: a newly-allocated buffer containing the first @n bytes
418  *          of @str, nul-terminated
419  */
420 gchar*
421 g_strndup (const gchar *str,
422            gsize        n)
423 {
424   gchar *new_str;
425
426   if (str)
427     {
428       new_str = g_new (gchar, n + 1);
429       strncpy (new_str, str, n);
430       new_str[n] = '\0';
431     }
432   else
433     new_str = NULL;
434
435   return new_str;
436 }
437
438 /**
439  * g_strnfill:
440  * @length: the length of the new string
441  * @fill_char: the byte to fill the string with
442  *
443  * Creates a new string @length bytes long filled with @fill_char.
444  * The returned string should be freed when no longer needed.
445  *
446  * Returns: a newly-allocated string filled the @fill_char
447  */
448 gchar*
449 g_strnfill (gsize length,
450             gchar fill_char)
451 {
452   gchar *str;
453
454   str = g_new (gchar, length + 1);
455   memset (str, (guchar)fill_char, length);
456   str[length] = '\0';
457
458   return str;
459 }
460
461 /**
462  * g_stpcpy:
463  * @dest: destination buffer.
464  * @src: source string.
465  *
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.
470  *
471  * Return value: a pointer to trailing nul byte.
472  **/
473 gchar *
474 g_stpcpy (gchar       *dest,
475           const gchar *src)
476 {
477 #ifdef HAVE_STPCPY
478   g_return_val_if_fail (dest != NULL, NULL);
479   g_return_val_if_fail (src != NULL, NULL);
480   return stpcpy (dest, src);
481 #else
482   register gchar *d = dest;
483   register const gchar *s = src;
484
485   g_return_val_if_fail (dest != NULL, NULL);
486   g_return_val_if_fail (src != NULL, NULL);
487   do
488     *d++ = *s;
489   while (*s++ != '\0');
490
491   return d - 1;
492 #endif
493 }
494
495 /**
496  * g_strdup_vprintf:
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
500  *
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
504  * no longer needed.
505  *
506  * See also g_vasprintf(), which offers the same functionality, but
507  * additionally returns the length of the allocated string.
508  *
509  * Returns: a newly-allocated string holding the result
510  */
511 gchar*
512 g_strdup_vprintf (const gchar *format,
513                   va_list      args)
514 {
515   gchar *string = NULL;
516
517   g_vasprintf (&string, format, args);
518
519   return string;
520 }
521
522 /**
523  * g_strdup_printf:
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
527  *
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
531  * longer needed.
532  *
533  * Returns: a newly-allocated string holding the result
534  */
535 gchar*
536 g_strdup_printf (const gchar *format,
537                  ...)
538 {
539   gchar *buffer;
540   va_list args;
541
542   va_start (args, format);
543   buffer = g_strdup_vprintf (format, args);
544   va_end (args);
545
546   return buffer;
547 }
548
549 /**
550  * g_strconcat:
551  * @string1: the first string to add, which must not be %NULL
552  * @...: a %NULL-terminated list of strings to append to the string
553  *
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.
556  *
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.
560  *
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>
564  *
565  * Returns: a newly-allocated string containing all the string arguments
566  */
567 gchar*
568 g_strconcat (const gchar *string1, ...)
569 {
570   gsize   l;
571   va_list args;
572   gchar   *s;
573   gchar   *concat;
574   gchar   *ptr;
575
576   if (!string1)
577     return NULL;
578
579   l = 1 + strlen (string1);
580   va_start (args, string1);
581   s = va_arg (args, gchar*);
582   while (s)
583     {
584       l += strlen (s);
585       s = va_arg (args, gchar*);
586     }
587   va_end (args);
588
589   concat = g_new (gchar, l);
590   ptr = concat;
591
592   ptr = g_stpcpy (ptr, string1);
593   va_start (args, string1);
594   s = va_arg (args, gchar*);
595   while (s)
596     {
597       ptr = g_stpcpy (ptr, s);
598       s = va_arg (args, gchar*);
599     }
600   va_end (args);
601
602   return concat;
603 }
604
605 /**
606  * g_strtod:
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.
610  *
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.
615  *
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.
622  *
623  * Return value: the #gdouble value.
624  **/
625 gdouble
626 g_strtod (const gchar *nptr,
627           gchar      **endptr)
628 {
629   gchar *fail_pos_1;
630   gchar *fail_pos_2;
631   gdouble val_1;
632   gdouble val_2 = 0;
633
634   g_return_val_if_fail (nptr != NULL, 0);
635
636   fail_pos_1 = NULL;
637   fail_pos_2 = NULL;
638
639   val_1 = strtod (nptr, &fail_pos_1);
640
641   if (fail_pos_1 && fail_pos_1[0] != 0)
642     val_2 = g_ascii_strtod (nptr, &fail_pos_2);
643
644   if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
645     {
646       if (endptr)
647         *endptr = fail_pos_1;
648       return val_1;
649     }
650   else
651     {
652       if (endptr)
653         *endptr = fail_pos_2;
654       return val_2;
655     }
656 }
657
658 /**
659  * g_ascii_strtod:
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.
663  *
664  * Converts a string to a #gdouble value.
665  *
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.
671  *
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.
676  *
677  * To convert from a #gdouble to a string in a locale-insensitive
678  * way, use g_ascii_dtostr().
679  *
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>.
684  *
685  * This function resets <literal>errno</literal> before calling strtod() so that
686  * you can reliably detect overflow and underflow.
687  *
688  * Return value: the #gdouble value.
689  */
690 gdouble
691 g_ascii_strtod (const gchar *nptr,
692                 gchar      **endptr)
693 {
694 #ifdef USE_XLOCALE
695
696   g_return_val_if_fail (nptr != NULL, 0);
697
698   errno = 0;
699
700   return strtod_l (nptr, endptr, get_C_locale ());
701
702 #else
703
704   gchar *fail_pos;
705   gdouble val;
706   struct lconv *locale_data;
707   const char *decimal_point;
708   int decimal_point_len;
709   const char *p, *decimal_point_pos;
710   const char *end = NULL; /* Silence gcc */
711   int strtod_errno;
712
713   g_return_val_if_fail (nptr != NULL, 0);
714
715   fail_pos = NULL;
716
717   locale_data = localeconv ();
718   decimal_point = locale_data->decimal_point;
719   decimal_point_len = strlen (decimal_point);
720
721   g_assert (decimal_point_len != 0);
722
723   decimal_point_pos = NULL;
724   end = NULL;
725
726   if (decimal_point[0] != '.' ||
727       decimal_point[1] != 0)
728     {
729       p = nptr;
730       /* Skip leading space */
731       while (g_ascii_isspace (*p))
732         p++;
733
734       /* Skip leading optional sign */
735       if (*p == '+' || *p == '-')
736         p++;
737
738       if (p[0] == '0' &&
739           (p[1] == 'x' || p[1] == 'X'))
740         {
741           p += 2;
742           /* HEX - find the (optional) decimal point */
743
744           while (g_ascii_isxdigit (*p))
745             p++;
746
747           if (*p == '.')
748             decimal_point_pos = p++;
749
750           while (g_ascii_isxdigit (*p))
751             p++;
752
753           if (*p == 'p' || *p == 'P')
754             p++;
755           if (*p == '+' || *p == '-')
756             p++;
757           while (g_ascii_isdigit (*p))
758             p++;
759
760           end = p;
761         }
762       else if (g_ascii_isdigit (*p) || *p == '.')
763         {
764           while (g_ascii_isdigit (*p))
765             p++;
766
767           if (*p == '.')
768             decimal_point_pos = p++;
769
770           while (g_ascii_isdigit (*p))
771             p++;
772
773           if (*p == 'e' || *p == 'E')
774             p++;
775           if (*p == '+' || *p == '-')
776             p++;
777           while (g_ascii_isdigit (*p))
778             p++;
779
780           end = p;
781         }
782       /* For the other cases, we need not convert the decimal point */
783     }
784
785   if (decimal_point_pos)
786     {
787       char *copy, *c;
788
789       /* We need to convert the '.' to the locale specific decimal point */
790       copy = g_malloc (end - nptr + 1 + decimal_point_len);
791
792       c = copy;
793       memcpy (c, nptr, decimal_point_pos - nptr);
794       c += decimal_point_pos - nptr;
795       memcpy (c, decimal_point, decimal_point_len);
796       c += decimal_point_len;
797       memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
798       c += end - (decimal_point_pos + 1);
799       *c = 0;
800
801       errno = 0;
802       val = strtod (copy, &fail_pos);
803       strtod_errno = errno;
804
805       if (fail_pos)
806         {
807           if (fail_pos - copy > decimal_point_pos - nptr)
808             fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
809           else
810             fail_pos = (char *)nptr + (fail_pos - copy);
811         }
812
813       g_free (copy);
814
815     }
816   else if (end)
817     {
818       char *copy;
819
820       copy = g_malloc (end - (char *)nptr + 1);
821       memcpy (copy, nptr, end - nptr);
822       *(copy + (end - (char *)nptr)) = 0;
823
824       errno = 0;
825       val = strtod (copy, &fail_pos);
826       strtod_errno = errno;
827
828       if (fail_pos)
829         {
830           fail_pos = (char *)nptr + (fail_pos - copy);
831         }
832
833       g_free (copy);
834     }
835   else
836     {
837       errno = 0;
838       val = strtod (nptr, &fail_pos);
839       strtod_errno = errno;
840     }
841
842   if (endptr)
843     *endptr = fail_pos;
844
845   errno = strtod_errno;
846
847   return val;
848 #endif
849 }
850
851
852 /**
853  * g_ascii_dtostr:
854  * @buffer: A buffer to place the resulting string in
855  * @buf_len: The length of the buffer.
856  * @d: The #gdouble to convert
857  *
858  * Converts a #gdouble to a string, using the '.' as
859  * decimal point.
860  *
861  * This functions generates enough precision that converting
862  * the string back using g_ascii_strtod() gives the same machine-number
863  * (on machines with IEEE compatible 64bit doubles). It is
864  * guaranteed that the size of the resulting string will never
865  * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes.
866  *
867  * Return value: The pointer to the buffer with the converted string.
868  **/
869 gchar *
870 g_ascii_dtostr (gchar       *buffer,
871                 gint         buf_len,
872                 gdouble      d)
873 {
874   return g_ascii_formatd (buffer, buf_len, "%.17g", d);
875 }
876
877 /**
878  * g_ascii_formatd:
879  * @buffer: A buffer to place the resulting string in
880  * @buf_len: The length of the buffer.
881  * @format: The printf()-style format to use for the
882  *          code to use for converting.
883  * @d: The #gdouble to convert
884  *
885  * Converts a #gdouble to a string, using the '.' as
886  * decimal point. To format the number you pass in
887  * a printf()-style format string. Allowed conversion
888  * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'.
889  *
890  * If you just want to want to serialize the value into a
891  * string, use g_ascii_dtostr().
892  *
893  * Return value: The pointer to the buffer with the converted string.
894  */
895 gchar *
896 g_ascii_formatd (gchar       *buffer,
897                  gint         buf_len,
898                  const gchar *format,
899                  gdouble      d)
900 {
901 #ifdef USE_XLOCALE
902   locale_t old_locale;
903
904   old_locale = uselocale (get_C_locale ());
905   _g_snprintf (buffer, buf_len, format, d);
906   uselocale (old_locale);
907
908   return buffer;
909 #else
910   struct lconv *locale_data;
911   const char *decimal_point;
912   int decimal_point_len;
913   gchar *p;
914   int rest_len;
915   gchar format_char;
916
917   g_return_val_if_fail (buffer != NULL, NULL);
918   g_return_val_if_fail (format[0] == '%', NULL);
919   g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
920
921   format_char = format[strlen (format) - 1];
922
923   g_return_val_if_fail (format_char == 'e' || format_char == 'E' ||
924                         format_char == 'f' || format_char == 'F' ||
925                         format_char == 'g' || format_char == 'G',
926                         NULL);
927
928   if (format[0] != '%')
929     return NULL;
930
931   if (strpbrk (format + 1, "'l%"))
932     return NULL;
933
934   if (!(format_char == 'e' || format_char == 'E' ||
935         format_char == 'f' || format_char == 'F' ||
936         format_char == 'g' || format_char == 'G'))
937     return NULL;
938
939   _g_snprintf (buffer, buf_len, format, d);
940
941   locale_data = localeconv ();
942   decimal_point = locale_data->decimal_point;
943   decimal_point_len = strlen (decimal_point);
944
945   g_assert (decimal_point_len != 0);
946
947   if (decimal_point[0] != '.' ||
948       decimal_point[1] != 0)
949     {
950       p = buffer;
951
952       while (g_ascii_isspace (*p))
953         p++;
954
955       if (*p == '+' || *p == '-')
956         p++;
957
958       while (isdigit ((guchar)*p))
959         p++;
960
961       if (strncmp (p, decimal_point, decimal_point_len) == 0)
962         {
963           *p = '.';
964           p++;
965           if (decimal_point_len > 1)
966             {
967               rest_len = strlen (p + (decimal_point_len-1));
968               memmove (p, p + (decimal_point_len-1), rest_len);
969               p[rest_len] = 0;
970             }
971         }
972     }
973
974   return buffer;
975 #endif
976 }
977
978 #define ISSPACE(c)              ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
979                                  (c) == '\r' || (c) == '\t' || (c) == '\v')
980 #define ISUPPER(c)              ((c) >= 'A' && (c) <= 'Z')
981 #define ISLOWER(c)              ((c) >= 'a' && (c) <= 'z')
982 #define ISALPHA(c)              (ISUPPER (c) || ISLOWER (c))
983 #define TOUPPER(c)              (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
984 #define TOLOWER(c)              (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
985
986 #ifndef USE_XLOCALE
987
988 static guint64
989 g_parse_long_long (const gchar  *nptr,
990                    const gchar **endptr,
991                    guint         base,
992                    gboolean     *negative)
993 {
994   /* this code is based on on the strtol(3) code from GNU libc released under
995    * the GNU Lesser General Public License.
996    *
997    * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
998    *        Free Software Foundation, Inc.
999    */
1000   gboolean overflow;
1001   guint64 cutoff;
1002   guint64 cutlim;
1003   guint64 ui64;
1004   const gchar *s, *save;
1005   guchar c;
1006
1007   g_return_val_if_fail (nptr != NULL, 0);
1008
1009   *negative = FALSE;
1010   if (base == 1 || base > 36)
1011     {
1012       errno = EINVAL;
1013       if (endptr)
1014         *endptr = nptr;
1015       return 0;
1016     }
1017
1018   save = s = nptr;
1019
1020   /* Skip white space.  */
1021   while (ISSPACE (*s))
1022     ++s;
1023
1024   if (G_UNLIKELY (!*s))
1025     goto noconv;
1026
1027   /* Check for a sign.  */
1028   if (*s == '-')
1029     {
1030       *negative = TRUE;
1031       ++s;
1032     }
1033   else if (*s == '+')
1034     ++s;
1035
1036   /* Recognize number prefix and if BASE is zero, figure it out ourselves.  */
1037   if (*s == '0')
1038     {
1039       if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
1040         {
1041           s += 2;
1042           base = 16;
1043         }
1044       else if (base == 0)
1045         base = 8;
1046     }
1047   else if (base == 0)
1048     base = 10;
1049
1050   /* Save the pointer so we can check later if anything happened.  */
1051   save = s;
1052   cutoff = G_MAXUINT64 / base;
1053   cutlim = G_MAXUINT64 % base;
1054
1055   overflow = FALSE;
1056   ui64 = 0;
1057   c = *s;
1058   for (; c; c = *++s)
1059     {
1060       if (c >= '0' && c <= '9')
1061         c -= '0';
1062       else if (ISALPHA (c))
1063         c = TOUPPER (c) - 'A' + 10;
1064       else
1065         break;
1066       if (c >= base)
1067         break;
1068       /* Check for overflow.  */
1069       if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
1070         overflow = TRUE;
1071       else
1072         {
1073           ui64 *= base;
1074           ui64 += c;
1075         }
1076     }
1077
1078   /* Check if anything actually happened.  */
1079   if (s == save)
1080     goto noconv;
1081
1082   /* Store in ENDPTR the address of one character
1083      past the last character we converted.  */
1084   if (endptr)
1085     *endptr = s;
1086
1087   if (G_UNLIKELY (overflow))
1088     {
1089       errno = ERANGE;
1090       return G_MAXUINT64;
1091     }
1092
1093   return ui64;
1094
1095  noconv:
1096   /* We must handle a special case here: the base is 0 or 16 and the
1097      first two characters are '0' and 'x', but the rest are no
1098      hexadecimal digits.  This is no error case.  We return 0 and
1099      ENDPTR points to the `x`.  */
1100   if (endptr)
1101     {
1102       if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
1103           && save[-2] == '0')
1104         *endptr = &save[-1];
1105       else
1106         /*  There was no number to convert.  */
1107         *endptr = nptr;
1108     }
1109   return 0;
1110 }
1111 #endif /* !USE_XLOCALE */
1112
1113 /**
1114  * g_ascii_strtoull:
1115  * @nptr:    the string to convert to a numeric value.
1116  * @endptr:  if non-%NULL, it returns the character after
1117  *           the last character used in the conversion.
1118  * @base:    to be used for the conversion, 2..36 or 0
1119  *
1120  * Converts a string to a #guint64 value.
1121  * This function behaves like the standard strtoull() function
1122  * does in the C locale. It does this without actually
1123  * changing the current locale, since that would not be
1124  * thread-safe.
1125  *
1126  * This function is typically used when reading configuration
1127  * files or other non-user input that should be locale independent.
1128  * To handle input from the user you should normally use the
1129  * locale-sensitive system strtoull() function.
1130  *
1131  * If the correct value would cause overflow, %G_MAXUINT64
1132  * is returned, and <literal>ERANGE</literal> is stored in <literal>errno</literal>.
1133  * If the base is outside the valid range, zero is returned, and
1134  * <literal>EINVAL</literal> is stored in <literal>errno</literal>.
1135  * If the string conversion fails, zero is returned, and @endptr returns
1136  * @nptr (if @endptr is non-%NULL).
1137  *
1138  * Return value: the #guint64 value or zero on error.
1139  *
1140  * Since: 2.2
1141  */
1142 guint64
1143 g_ascii_strtoull (const gchar *nptr,
1144                   gchar      **endptr,
1145                   guint        base)
1146 {
1147 #ifdef USE_XLOCALE
1148   return strtoull_l (nptr, endptr, base, get_C_locale ());
1149 #else
1150   gboolean negative;
1151   guint64 result;
1152
1153   result = g_parse_long_long (nptr, (const gchar **) endptr, base, &negative);
1154
1155   /* Return the result of the appropriate sign.  */
1156   return negative ? -result : result;
1157 #endif
1158 }
1159
1160 /**
1161  * g_ascii_strtoll:
1162  * @nptr:    the string to convert to a numeric value.
1163  * @endptr:  if non-%NULL, it returns the character after
1164  *           the last character used in the conversion.
1165  * @base:    to be used for the conversion, 2..36 or 0
1166  *
1167  * Converts a string to a #gint64 value.
1168  * This function behaves like the standard strtoll() function
1169  * does in the C locale. It does this without actually
1170  * changing the current locale, since that would not be
1171  * thread-safe.
1172  *
1173  * This function is typically used when reading configuration
1174  * files or other non-user input that should be locale independent.
1175  * To handle input from the user you should normally use the
1176  * locale-sensitive system strtoll() function.
1177  *
1178  * If the correct value would cause overflow, %G_MAXINT64 or %G_MININT64
1179  * is returned, and <literal>ERANGE</literal> is stored in <literal>errno</literal>.
1180  * If the base is outside the valid range, zero is returned, and
1181  * <literal>EINVAL</literal> is stored in <literal>errno</literal>. If the
1182  * string conversion fails, zero is returned, and @endptr returns @nptr
1183  * (if @endptr is non-%NULL).
1184  *
1185  * Return value: the #gint64 value or zero on error.
1186  *
1187  * Since: 2.12
1188  */
1189 gint64
1190 g_ascii_strtoll (const gchar *nptr,
1191                  gchar      **endptr,
1192                  guint        base)
1193 {
1194 #ifdef USE_XLOCALE
1195   return strtoll_l (nptr, endptr, base, get_C_locale ());
1196 #else
1197   gboolean negative;
1198   guint64 result;
1199
1200   result = g_parse_long_long (nptr, (const gchar **) endptr, base, &negative);
1201
1202   if (negative && result > (guint64) G_MININT64)
1203     {
1204       errno = ERANGE;
1205       return G_MININT64;
1206     }
1207   else if (!negative && result > (guint64) G_MAXINT64)
1208     {
1209       errno = ERANGE;
1210       return G_MAXINT64;
1211     }
1212   else if (negative)
1213     return - (gint64) result;
1214   else
1215     return (gint64) result;
1216 #endif
1217 }
1218
1219 /**
1220  * g_strerror:
1221  * @errnum: the system error number. See the standard C %errno
1222  *     documentation
1223  *
1224  * Returns a string corresponding to the given error code, e.g.
1225  * "no such process". You should use this function in preference to
1226  * strerror(), because it returns a string in UTF-8 encoding, and since
1227  * not all platforms support the strerror() function.
1228  *
1229  * Returns: a UTF-8 string describing the error code. If the error code
1230  *     is unknown, it returns "unknown error (&lt;code&gt;)".
1231  */
1232 const gchar *
1233 g_strerror (gint errnum)
1234 {
1235   gchar buf[64];
1236   gchar *msg;
1237   gchar *tofree;
1238   const gchar *ret;
1239   gint saved_errno = errno;
1240
1241   msg = tofree = NULL;
1242
1243 #ifdef HAVE_STRERROR
1244   msg = strerror (errnum);
1245   if (!g_get_charset (NULL))
1246     msg = tofree = g_locale_to_utf8 (msg, -1, NULL, NULL, NULL);
1247 #endif
1248
1249   if (!msg)
1250     {
1251       msg = buf;
1252       _g_sprintf (msg, "unknown error (%d)", errnum);
1253     }
1254
1255   ret = g_intern_string (msg);
1256   g_free (tofree);
1257   errno = saved_errno;
1258   return ret;
1259 }
1260
1261 /**
1262  * g_strsignal:
1263  * @signum: the signal number. See the <literal>signal</literal>
1264  *     documentation
1265  *
1266  * Returns a string describing the given signal, e.g. "Segmentation fault".
1267  * You should use this function in preference to strsignal(), because it
1268  * returns a string in UTF-8 encoding, and since not all platforms support
1269  * the strsignal() function.
1270  *
1271  * Returns: a UTF-8 string describing the signal. If the signal is unknown,
1272  *     it returns "unknown signal (&lt;signum&gt;)".
1273  */
1274 const gchar *
1275 g_strsignal (gint signum)
1276 {
1277   gchar *msg;
1278   gchar *tofree;
1279   const gchar *ret;
1280
1281   msg = tofree = NULL;
1282
1283 #ifdef HAVE_STRSIGNAL
1284   msg = strsignal (signum);
1285   if (!g_get_charset (NULL))
1286     msg = tofree = g_locale_to_utf8 (msg, -1, NULL, NULL, NULL);
1287 #endif
1288
1289   if (!msg)
1290     msg = tofree = g_strdup_printf ("unknown signal (%d)", signum);
1291   ret = g_intern_string (msg);
1292   g_free (tofree);
1293
1294   return ret;
1295 }
1296
1297 /* Functions g_strlcpy and g_strlcat were originally developed by
1298  * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
1299  * See http://www.openbsd.org/cgi-bin/man.cgi?query=strlcpy 
1300  * for more information.
1301  */
1302
1303 #ifdef HAVE_STRLCPY
1304 /* Use the native ones, if available; they might be implemented in assembly */
1305 gsize
1306 g_strlcpy (gchar       *dest,
1307            const gchar *src,
1308            gsize        dest_size)
1309 {
1310   g_return_val_if_fail (dest != NULL, 0);
1311   g_return_val_if_fail (src  != NULL, 0);
1312
1313   return strlcpy (dest, src, dest_size);
1314 }
1315
1316 gsize
1317 g_strlcat (gchar       *dest,
1318            const gchar *src,
1319            gsize        dest_size)
1320 {
1321   g_return_val_if_fail (dest != NULL, 0);
1322   g_return_val_if_fail (src  != NULL, 0);
1323
1324   return strlcat (dest, src, dest_size);
1325 }
1326
1327 #else /* ! HAVE_STRLCPY */
1328 /**
1329  * g_strlcpy:
1330  * @dest: destination buffer
1331  * @src: source buffer
1332  * @dest_size: length of @dest in bytes
1333  *
1334  * Portability wrapper that calls strlcpy() on systems which have it,
1335  * and emulates strlcpy() otherwise. Copies @src to @dest; @dest is
1336  * guaranteed to be nul-terminated; @src must be nul-terminated;
1337  * @dest_size is the buffer size, not the number of chars to copy.
1338  *
1339  * At most dest_size - 1 characters will be copied. Always nul-terminates
1340  * (unless dest_size == 0). This function does <emphasis>not</emphasis>
1341  * allocate memory. Unlike strncpy(), this function doesn't pad dest (so
1342  * it's often faster). It returns the size of the attempted result,
1343  * strlen (src), so if @retval >= @dest_size, truncation occurred.
1344  *
1345  * <note><para>Caveat: strlcpy() is supposedly more secure than
1346  * strcpy() or strncpy(), but if you really want to avoid screwups,
1347  * g_strdup() is an even better idea.</para></note>
1348  *
1349  * Returns: length of @src
1350  */
1351 gsize
1352 g_strlcpy (gchar       *dest,
1353            const gchar *src,
1354            gsize        dest_size)
1355 {
1356   register gchar *d = dest;
1357   register const gchar *s = src;
1358   register gsize n = dest_size;
1359
1360   g_return_val_if_fail (dest != NULL, 0);
1361   g_return_val_if_fail (src  != NULL, 0);
1362
1363   /* Copy as many bytes as will fit */
1364   if (n != 0 && --n != 0)
1365     do
1366       {
1367         register gchar c = *s++;
1368
1369         *d++ = c;
1370         if (c == 0)
1371           break;
1372       }
1373     while (--n != 0);
1374
1375   /* If not enough room in dest, add NUL and traverse rest of src */
1376   if (n == 0)
1377     {
1378       if (dest_size != 0)
1379         *d = 0;
1380       while (*s++)
1381         ;
1382     }
1383
1384   return s - src - 1;  /* count does not include NUL */
1385 }
1386
1387 /**
1388  * g_strlcat:
1389  * @dest: destination buffer, already containing one nul-terminated string
1390  * @src: source buffer
1391  * @dest_size: length of @dest buffer in bytes (not length of existing string
1392  *     inside @dest)
1393  *
1394  * Portability wrapper that calls strlcat() on systems which have it,
1395  * and emulates it otherwise. Appends nul-terminated @src string to @dest,
1396  * guaranteeing nul-termination for @dest. The total size of @dest won't
1397  * exceed @dest_size.
1398  *
1399  * At most dest_size - 1 characters will be copied.
1400  * Unlike strncat, dest_size is the full size of dest, not the space left over.
1401  * This function does NOT allocate memory.
1402  * This always NUL terminates (unless siz == 0 or there were no NUL characters
1403  * in the dest_size characters of dest to start with).
1404  *
1405  * <note><para>Caveat: this is supposedly a more secure alternative to
1406  * strcat() or strncat(), but for real security g_strconcat() is harder
1407  * to mess up.</para></note>
1408  *
1409  * Returns: size of attempted result, which is MIN (dest_size, strlen
1410  *          (original dest)) + strlen (src), so if retval >= dest_size,
1411  *          truncation occurred.
1412  **/
1413 gsize
1414 g_strlcat (gchar       *dest,
1415            const gchar *src,
1416            gsize        dest_size)
1417 {
1418   register gchar *d = dest;
1419   register const gchar *s = src;
1420   register gsize bytes_left = dest_size;
1421   gsize dlength;  /* Logically, MIN (strlen (d), dest_size) */
1422
1423   g_return_val_if_fail (dest != NULL, 0);
1424   g_return_val_if_fail (src  != NULL, 0);
1425
1426   /* Find the end of dst and adjust bytes left but don't go past end */
1427   while (*d != 0 && bytes_left-- != 0)
1428     d++;
1429   dlength = d - dest;
1430   bytes_left = dest_size - dlength;
1431
1432   if (bytes_left == 0)
1433     return dlength + strlen (s);
1434
1435   while (*s != 0)
1436     {
1437       if (bytes_left != 1)
1438         {
1439           *d++ = *s;
1440           bytes_left--;
1441         }
1442       s++;
1443     }
1444   *d = 0;
1445
1446   return dlength + (s - src);  /* count does not include NUL */
1447 }
1448 #endif /* ! HAVE_STRLCPY */
1449
1450 /**
1451  * g_ascii_strdown:
1452  * @str: a string.
1453  * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1454  *
1455  * Converts all upper case ASCII letters to lower case ASCII letters.
1456  *
1457  * Return value: a newly-allocated string, with all the upper case
1458  *               characters in @str converted to lower case, with
1459  *               semantics that exactly match g_ascii_tolower(). (Note
1460  *               that this is unlike the old g_strdown(), which modified
1461  *               the string in place.)
1462  **/
1463 gchar*
1464 g_ascii_strdown (const gchar *str,
1465                  gssize       len)
1466 {
1467   gchar *result, *s;
1468
1469   g_return_val_if_fail (str != NULL, NULL);
1470
1471   if (len < 0)
1472     len = strlen (str);
1473
1474   result = g_strndup (str, len);
1475   for (s = result; *s; s++)
1476     *s = g_ascii_tolower (*s);
1477
1478   return result;
1479 }
1480
1481 /**
1482  * g_ascii_strup:
1483  * @str: a string.
1484  * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1485  *
1486  * Converts all lower case ASCII letters to upper case ASCII letters.
1487  *
1488  * Return value: a newly allocated string, with all the lower case
1489  *               characters in @str converted to upper case, with
1490  *               semantics that exactly match g_ascii_toupper(). (Note
1491  *               that this is unlike the old g_strup(), which modified
1492  *               the string in place.)
1493  **/
1494 gchar*
1495 g_ascii_strup (const gchar *str,
1496                gssize       len)
1497 {
1498   gchar *result, *s;
1499
1500   g_return_val_if_fail (str != NULL, NULL);
1501
1502   if (len < 0)
1503     len = strlen (str);
1504
1505   result = g_strndup (str, len);
1506   for (s = result; *s; s++)
1507     *s = g_ascii_toupper (*s);
1508
1509   return result;
1510 }
1511
1512 /**
1513  * g_strdown:
1514  * @string: the string to convert.
1515  *
1516  * Converts a string to lower case.
1517  *
1518  * Return value: the string
1519  *
1520  * Deprecated:2.2: This function is totally broken for the reasons discussed
1521  * in the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown()
1522  * instead.
1523  **/
1524 gchar*
1525 g_strdown (gchar *string)
1526 {
1527   register guchar *s;
1528
1529   g_return_val_if_fail (string != NULL, NULL);
1530
1531   s = (guchar *) string;
1532
1533   while (*s)
1534     {
1535       if (isupper (*s))
1536         *s = tolower (*s);
1537       s++;
1538     }
1539
1540   return (gchar *) string;
1541 }
1542
1543 /**
1544  * g_strup:
1545  * @string: the string to convert.
1546  *
1547  * Converts a string to upper case.
1548  *
1549  * Return value: the string
1550  *
1551  * Deprecated:2.2: This function is totally broken for the reasons discussed
1552  * in the g_strncasecmp() docs - use g_ascii_strup() or g_utf8_strup() instead.
1553  **/
1554 gchar*
1555 g_strup (gchar *string)
1556 {
1557   register guchar *s;
1558
1559   g_return_val_if_fail (string != NULL, NULL);
1560
1561   s = (guchar *) string;
1562
1563   while (*s)
1564     {
1565       if (islower (*s))
1566         *s = toupper (*s);
1567       s++;
1568     }
1569
1570   return (gchar *) string;
1571 }
1572
1573 /**
1574  * g_strreverse:
1575  * @string: the string to reverse
1576  *
1577  * Reverses all of the bytes in a string. For example,
1578  * <literal>g_strreverse ("abcdef")</literal> will result
1579  * in "fedcba".
1580  *
1581  * Note that g_strreverse() doesn't work on UTF-8 strings
1582  * containing multibyte characters. For that purpose, use
1583  * g_utf8_strreverse().
1584  *
1585  * Returns: the same pointer passed in as @string
1586  */
1587 gchar*
1588 g_strreverse (gchar *string)
1589 {
1590   g_return_val_if_fail (string != NULL, NULL);
1591
1592   if (*string)
1593     {
1594       register gchar *h, *t;
1595
1596       h = string;
1597       t = string + strlen (string) - 1;
1598
1599       while (h < t)
1600         {
1601           register gchar c;
1602
1603           c = *h;
1604           *h = *t;
1605           h++;
1606           *t = c;
1607           t--;
1608         }
1609     }
1610
1611   return string;
1612 }
1613
1614 /**
1615  * g_ascii_tolower:
1616  * @c: any character.
1617  *
1618  * Convert a character to ASCII lower case.
1619  *
1620  * Unlike the standard C library tolower() function, this only
1621  * recognizes standard ASCII letters and ignores the locale, returning
1622  * all non-ASCII characters unchanged, even if they are lower case
1623  * letters in a particular character set. Also unlike the standard
1624  * library function, this takes and returns a char, not an int, so
1625  * don't call it on <literal>EOF</literal> but no need to worry about casting to #guchar
1626  * before passing a possibly non-ASCII character in.
1627  *
1628  * Return value: the result of converting @c to lower case.
1629  *               If @c is not an ASCII upper case letter,
1630  *               @c is returned unchanged.
1631  **/
1632 gchar
1633 g_ascii_tolower (gchar c)
1634 {
1635   return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1636 }
1637
1638 /**
1639  * g_ascii_toupper:
1640  * @c: any character.
1641  *
1642  * Convert a character to ASCII upper case.
1643  *
1644  * Unlike the standard C library toupper() function, this only
1645  * recognizes standard ASCII letters and ignores the locale, returning
1646  * all non-ASCII characters unchanged, even if they are upper case
1647  * letters in a particular character set. Also unlike the standard
1648  * library function, this takes and returns a char, not an int, so
1649  * don't call it on <literal>EOF</literal> but no need to worry about casting to #guchar
1650  * before passing a possibly non-ASCII character in.
1651  *
1652  * Return value: the result of converting @c to upper case.
1653  *               If @c is not an ASCII lower case letter,
1654  *               @c is returned unchanged.
1655  **/
1656 gchar
1657 g_ascii_toupper (gchar c)
1658 {
1659   return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1660 }
1661
1662 /**
1663  * g_ascii_digit_value:
1664  * @c: an ASCII character.
1665  *
1666  * Determines the numeric value of a character as a decimal
1667  * digit. Differs from g_unichar_digit_value() because it takes
1668  * a char, so there's no worry about sign extension if characters
1669  * are signed.
1670  *
1671  * Return value: If @c is a decimal digit (according to
1672  * g_ascii_isdigit()), its numeric value. Otherwise, -1.
1673  **/
1674 int
1675 g_ascii_digit_value (gchar c)
1676 {
1677   if (g_ascii_isdigit (c))
1678     return c - '0';
1679   return -1;
1680 }
1681
1682 /**
1683  * g_ascii_xdigit_value:
1684  * @c: an ASCII character.
1685  *
1686  * Determines the numeric value of a character as a hexidecimal
1687  * digit. Differs from g_unichar_xdigit_value() because it takes
1688  * a char, so there's no worry about sign extension if characters
1689  * are signed.
1690  *
1691  * Return value: If @c is a hex digit (according to
1692  * g_ascii_isxdigit()), its numeric value. Otherwise, -1.
1693  **/
1694 int
1695 g_ascii_xdigit_value (gchar c)
1696 {
1697   if (c >= 'A' && c <= 'F')
1698     return c - 'A' + 10;
1699   if (c >= 'a' && c <= 'f')
1700     return c - 'a' + 10;
1701   return g_ascii_digit_value (c);
1702 }
1703
1704 /**
1705  * g_ascii_strcasecmp:
1706  * @s1: string to compare with @s2.
1707  * @s2: string to compare with @s1.
1708  *
1709  * Compare two strings, ignoring the case of ASCII characters.
1710  *
1711  * Unlike the BSD strcasecmp() function, this only recognizes standard
1712  * ASCII letters and ignores the locale, treating all non-ASCII
1713  * bytes as if they are not letters.
1714  *
1715  * This function should be used only on strings that are known to be
1716  * in encodings where the bytes corresponding to ASCII letters always
1717  * represent themselves. This includes UTF-8 and the ISO-8859-*
1718  * charsets, but not for instance double-byte encodings like the
1719  * Windows Codepage 932, where the trailing bytes of double-byte
1720  * characters include all ASCII letters. If you compare two CP932
1721  * strings using this function, you will get false matches.
1722  *
1723  * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2,
1724  *   or a positive value if @s1 &gt; @s2.
1725  **/
1726 gint
1727 g_ascii_strcasecmp (const gchar *s1,
1728                     const gchar *s2)
1729 {
1730   gint c1, c2;
1731
1732   g_return_val_if_fail (s1 != NULL, 0);
1733   g_return_val_if_fail (s2 != NULL, 0);
1734
1735   while (*s1 && *s2)
1736     {
1737       c1 = (gint)(guchar) TOLOWER (*s1);
1738       c2 = (gint)(guchar) TOLOWER (*s2);
1739       if (c1 != c2)
1740         return (c1 - c2);
1741       s1++; s2++;
1742     }
1743
1744   return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1745 }
1746
1747 /**
1748  * g_ascii_strncasecmp:
1749  * @s1: string to compare with @s2.
1750  * @s2: string to compare with @s1.
1751  * @n:  number of characters to compare.
1752  *
1753  * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1754  * characters after the first @n in each string.
1755  *
1756  * Unlike the BSD strcasecmp() function, this only recognizes standard
1757  * ASCII letters and ignores the locale, treating all non-ASCII
1758  * characters as if they are not letters.
1759  *
1760  * The same warning as in g_ascii_strcasecmp() applies: Use this
1761  * function only on strings known to be in encodings where bytes
1762  * corresponding to ASCII letters always represent themselves.
1763  *
1764  * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2,
1765  *   or a positive value if @s1 &gt; @s2.
1766  **/
1767 gint
1768 g_ascii_strncasecmp (const gchar *s1,
1769                      const gchar *s2,
1770                      gsize n)
1771 {
1772   gint c1, c2;
1773
1774   g_return_val_if_fail (s1 != NULL, 0);
1775   g_return_val_if_fail (s2 != NULL, 0);
1776
1777   while (n && *s1 && *s2)
1778     {
1779       n -= 1;
1780       c1 = (gint)(guchar) TOLOWER (*s1);
1781       c2 = (gint)(guchar) TOLOWER (*s2);
1782       if (c1 != c2)
1783         return (c1 - c2);
1784       s1++; s2++;
1785     }
1786
1787   if (n)
1788     return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1789   else
1790     return 0;
1791 }
1792
1793 /**
1794  * g_strcasecmp:
1795  * @s1: a string.
1796  * @s2: a string to compare with @s1.
1797  *
1798  * A case-insensitive string comparison, corresponding to the standard
1799  * strcasecmp() function on platforms which support it.
1800  *
1801  * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2,
1802  *   or a positive value if @s1 &gt; @s2.
1803  *
1804  * Deprecated:2.2: See g_strncasecmp() for a discussion of why this function
1805  *   is deprecated and how to replace it.
1806  **/
1807 gint
1808 g_strcasecmp (const gchar *s1,
1809               const gchar *s2)
1810 {
1811 #ifdef HAVE_STRCASECMP
1812   g_return_val_if_fail (s1 != NULL, 0);
1813   g_return_val_if_fail (s2 != NULL, 0);
1814
1815   return strcasecmp (s1, s2);
1816 #else
1817   gint c1, c2;
1818
1819   g_return_val_if_fail (s1 != NULL, 0);
1820   g_return_val_if_fail (s2 != NULL, 0);
1821
1822   while (*s1 && *s2)
1823     {
1824       /* According to A. Cox, some platforms have islower's that
1825        * don't work right on non-uppercase
1826        */
1827       c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1828       c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1829       if (c1 != c2)
1830         return (c1 - c2);
1831       s1++; s2++;
1832     }
1833
1834   return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1835 #endif
1836 }
1837
1838 /**
1839  * g_strncasecmp:
1840  * @s1: a string.
1841  * @s2: a string to compare with @s1.
1842  * @n: the maximum number of characters to compare.
1843  *
1844  * A case-insensitive string comparison, corresponding to the standard
1845  * strncasecmp() function on platforms which support it.
1846  * It is similar to g_strcasecmp() except it only compares the first @n
1847  * characters of the strings.
1848  *
1849  * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2,
1850  *   or a positive value if @s1 &gt; @s2.
1851  *
1852  * Deprecated:2.2: The problem with g_strncasecmp() is that it does the
1853  * comparison by calling toupper()/tolower(). These functions are
1854  * locale-specific and operate on single bytes. However, it is impossible
1855  * to handle things correctly from an I18N standpoint by operating on
1856  * bytes, since characters may be multibyte. Thus g_strncasecmp() is
1857  * broken if your string is guaranteed to be ASCII, since it's
1858  * locale-sensitive, and it's broken if your string is localized, since
1859  * it doesn't work on many encodings at all, including UTF-8, EUC-JP,
1860  * etc.
1861  *
1862  * There are therefore two replacement functions: g_ascii_strncasecmp(),
1863  * which only works on ASCII and is not locale-sensitive, and
1864  * g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8.
1865  **/
1866 gint
1867 g_strncasecmp (const gchar *s1,
1868                const gchar *s2,
1869                guint n)
1870 {
1871 #ifdef HAVE_STRNCASECMP
1872   return strncasecmp (s1, s2, n);
1873 #else
1874   gint c1, c2;
1875
1876   g_return_val_if_fail (s1 != NULL, 0);
1877   g_return_val_if_fail (s2 != NULL, 0);
1878
1879   while (n && *s1 && *s2)
1880     {
1881       n -= 1;
1882       /* According to A. Cox, some platforms have islower's that
1883        * don't work right on non-uppercase
1884        */
1885       c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1886       c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1887       if (c1 != c2)
1888         return (c1 - c2);
1889       s1++; s2++;
1890     }
1891
1892   if (n)
1893     return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1894   else
1895     return 0;
1896 #endif
1897 }
1898
1899 /**
1900  * g_strdelimit:
1901  * @string: the string to convert
1902  * @delimiters: (allow-none): a string containing the current delimiters, or %NULL
1903  *     to use the standard delimiters defined in #G_STR_DELIMITERS
1904  * @new_delimiter: the new delimiter character
1905  *
1906  * Converts any delimiter characters in @string to @new_delimiter.
1907  * Any characters in @string which are found in @delimiters are
1908  * changed to the @new_delimiter character. Modifies @string in place,
1909  * and returns @string itself, not a copy. The return value is to
1910  * allow nesting such as
1911  * |[
1912  *   g_ascii_strup (g_strdelimit (str, "abc", '?'))
1913  * ]|
1914  *
1915  * Returns: @string
1916  */
1917 gchar *
1918 g_strdelimit (gchar       *string,
1919               const gchar *delimiters,
1920               gchar        new_delim)
1921 {
1922   register gchar *c;
1923
1924   g_return_val_if_fail (string != NULL, NULL);
1925
1926   if (!delimiters)
1927     delimiters = G_STR_DELIMITERS;
1928
1929   for (c = string; *c; c++)
1930     {
1931       if (strchr (delimiters, *c))
1932         *c = new_delim;
1933     }
1934
1935   return string;
1936 }
1937
1938 /**
1939  * g_strcanon:
1940  * @string: a nul-terminated array of bytes
1941  * @valid_chars: bytes permitted in @string
1942  * @substitutor: replacement character for disallowed bytes
1943  *
1944  * For each character in @string, if the character is not in
1945  * @valid_chars, replaces the character with @substitutor.
1946  * Modifies @string in place, and return @string itself, not
1947  * a copy. The return value is to allow nesting such as
1948  * |[
1949  *   g_ascii_strup (g_strcanon (str, "abc", '?'))
1950  * ]|
1951  *
1952  * Returns: @string
1953  */
1954 gchar *
1955 g_strcanon (gchar       *string,
1956             const gchar *valid_chars,
1957             gchar        substitutor)
1958 {
1959   register gchar *c;
1960
1961   g_return_val_if_fail (string != NULL, NULL);
1962   g_return_val_if_fail (valid_chars != NULL, NULL);
1963
1964   for (c = string; *c; c++)
1965     {
1966       if (!strchr (valid_chars, *c))
1967         *c = substitutor;
1968     }
1969
1970   return string;
1971 }
1972
1973 /**
1974  * g_strcompress:
1975  * @source: a string to compress
1976  *
1977  * Replaces all escaped characters with their one byte equivalent.
1978  *
1979  * This function does the reverse conversion of g_strescape().
1980  *
1981  * Returns: a newly-allocated copy of @source with all escaped
1982  *     character compressed
1983  */
1984 gchar *
1985 g_strcompress (const gchar *source)
1986 {
1987   const gchar *p = source, *octal;
1988   gchar *dest;
1989   gchar *q;
1990
1991   g_return_val_if_fail (source != NULL, NULL);
1992
1993   dest = g_malloc (strlen (source) + 1);
1994   q = dest;
1995
1996   while (*p)
1997     {
1998       if (*p == '\\')
1999         {
2000           p++;
2001           switch (*p)
2002             {
2003             case '\0':
2004               g_warning ("g_strcompress: trailing \\");
2005               goto out;
2006             case '0':  case '1':  case '2':  case '3':  case '4':
2007             case '5':  case '6':  case '7':
2008               *q = 0;
2009               octal = p;
2010               while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
2011                 {
2012                   *q = (*q * 8) + (*p - '0');
2013                   p++;
2014                 }
2015               q++;
2016               p--;
2017               break;
2018             case 'b':
2019               *q++ = '\b';
2020               break;
2021             case 'f':
2022               *q++ = '\f';
2023               break;
2024             case 'n':
2025               *q++ = '\n';
2026               break;
2027             case 'r':
2028               *q++ = '\r';
2029               break;
2030             case 't':
2031               *q++ = '\t';
2032               break;
2033             case 'v':
2034               *q++ = '\v';
2035               break;
2036             default:            /* Also handles \" and \\ */
2037               *q++ = *p;
2038               break;
2039             }
2040         }
2041       else
2042         *q++ = *p;
2043       p++;
2044     }
2045 out:
2046   *q = 0;
2047
2048   return dest;
2049 }
2050
2051 /**
2052  * g_strescape:
2053  * @source: a string to escape
2054  * @exceptions: a string of characters not to escape in @source
2055  *
2056  * Escapes the special characters '\b', '\f', '\n', '\r', '\t', '\v', '\'
2057  * and '&quot;' in the string @source by inserting a '\' before
2058  * them. Additionally all characters in the range 0x01-0x1F (everything
2059  * below SPACE) and in the range 0x7F-0xFF (all non-ASCII chars) are
2060  * replaced with a '\' followed by their octal representation.
2061  * Characters supplied in @exceptions are not escaped.
2062  *
2063  * g_strcompress() does the reverse conversion.
2064  *
2065  * Returns: a newly-allocated copy of @source with certain
2066  *     characters escaped. See above.
2067  */
2068 gchar *
2069 g_strescape (const gchar *source,
2070              const gchar *exceptions)
2071 {
2072   const guchar *p;
2073   gchar *dest;
2074   gchar *q;
2075   guchar excmap[256];
2076
2077   g_return_val_if_fail (source != NULL, NULL);
2078
2079   p = (guchar *) source;
2080   /* Each source byte needs maximally four destination chars (\777) */
2081   q = dest = g_malloc (strlen (source) * 4 + 1);
2082
2083   memset (excmap, 0, 256);
2084   if (exceptions)
2085     {
2086       guchar *e = (guchar *) exceptions;
2087
2088       while (*e)
2089         {
2090           excmap[*e] = 1;
2091           e++;
2092         }
2093     }
2094
2095   while (*p)
2096     {
2097       if (excmap[*p])
2098         *q++ = *p;
2099       else
2100         {
2101           switch (*p)
2102             {
2103             case '\b':
2104               *q++ = '\\';
2105               *q++ = 'b';
2106               break;
2107             case '\f':
2108               *q++ = '\\';
2109               *q++ = 'f';
2110               break;
2111             case '\n':
2112               *q++ = '\\';
2113               *q++ = 'n';
2114               break;
2115             case '\r':
2116               *q++ = '\\';
2117               *q++ = 'r';
2118               break;
2119             case '\t':
2120               *q++ = '\\';
2121               *q++ = 't';
2122               break;
2123             case '\v':
2124               *q++ = '\\';
2125               *q++ = 'v';
2126               break;
2127             case '\\':
2128               *q++ = '\\';
2129               *q++ = '\\';
2130               break;
2131             case '"':
2132               *q++ = '\\';
2133               *q++ = '"';
2134               break;
2135             default:
2136               if ((*p < ' ') || (*p >= 0177))
2137                 {
2138                   *q++ = '\\';
2139                   *q++ = '0' + (((*p) >> 6) & 07);
2140                   *q++ = '0' + (((*p) >> 3) & 07);
2141                   *q++ = '0' + ((*p) & 07);
2142                 }
2143               else
2144                 *q++ = *p;
2145               break;
2146             }
2147         }
2148       p++;
2149     }
2150   *q = 0;
2151   return dest;
2152 }
2153
2154 /**
2155  * g_strchug:
2156  * @string: a string to remove the leading whitespace from
2157  *
2158  * Removes leading whitespace from a string, by moving the rest
2159  * of the characters forward.
2160  *
2161  * This function doesn't allocate or reallocate any memory;
2162  * it modifies @string in place. The pointer to @string is
2163  * returned to allow the nesting of functions.
2164  *
2165  * Also see g_strchomp() and g_strstrip().
2166  *
2167  * Returns: @string
2168  */
2169 gchar *
2170 g_strchug (gchar *string)
2171 {
2172   guchar *start;
2173
2174   g_return_val_if_fail (string != NULL, NULL);
2175
2176   for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
2177     ;
2178
2179   g_memmove (string, start, strlen ((gchar *) start) + 1);
2180
2181   return string;
2182 }
2183
2184 /**
2185  * g_strchomp:
2186  * @string: a string to remove the trailing whitespace from
2187  *
2188  * Removes trailing whitespace from a string.
2189  *
2190  * This function doesn't allocate or reallocate any memory;
2191  * it modifies @string in place. The pointer to @string is
2192  * returned to allow the nesting of functions.
2193  *
2194  * Also see g_strchug() and g_strstrip().
2195  *
2196  * Returns: @string.
2197  */
2198 gchar *
2199 g_strchomp (gchar *string)
2200 {
2201   gsize len;
2202
2203   g_return_val_if_fail (string != NULL, NULL);
2204
2205   len = strlen (string);
2206   while (len--)
2207     {
2208       if (g_ascii_isspace ((guchar) string[len]))
2209         string[len] = '\0';
2210       else
2211         break;
2212     }
2213
2214   return string;
2215 }
2216
2217 /**
2218  * g_strsplit:
2219  * @string: a string to split
2220  * @delimiter: a string which specifies the places at which to split
2221  *     the string. The delimiter is not included in any of the resulting
2222  *     strings, unless @max_tokens is reached.
2223  * @max_tokens: the maximum number of pieces to split @string into.
2224  *     If this is less than 1, the string is split completely.
2225  *
2226  * Splits a string into a maximum of @max_tokens pieces, using the given
2227  * @delimiter. If @max_tokens is reached, the remainder of @string is
2228  * appended to the last token.
2229  *
2230  * As a special case, the result of splitting the empty string "" is an empty
2231  * vector, not a vector containing a single string. The reason for this
2232  * special case is that being able to represent a empty vector is typically
2233  * more useful than consistent handling of empty elements. If you do need
2234  * to represent empty elements, you'll need to check for the empty string
2235  * before calling g_strsplit().
2236  *
2237  * Return value: a newly-allocated %NULL-terminated array of strings. Use
2238  *    g_strfreev() to free it.
2239  */
2240 gchar**
2241 g_strsplit (const gchar *string,
2242             const gchar *delimiter,
2243             gint         max_tokens)
2244 {
2245   GSList *string_list = NULL, *slist;
2246   gchar **str_array, *s;
2247   guint n = 0;
2248   const gchar *remainder;
2249
2250   g_return_val_if_fail (string != NULL, NULL);
2251   g_return_val_if_fail (delimiter != NULL, NULL);
2252   g_return_val_if_fail (delimiter[0] != '\0', NULL);
2253
2254   if (max_tokens < 1)
2255     max_tokens = G_MAXINT;
2256
2257   remainder = string;
2258   s = strstr (remainder, delimiter);
2259   if (s)
2260     {
2261       gsize delimiter_len = strlen (delimiter);
2262
2263       while (--max_tokens && s)
2264         {
2265           gsize len;
2266
2267           len = s - remainder;
2268           string_list = g_slist_prepend (string_list,
2269                                          g_strndup (remainder, len));
2270           n++;
2271           remainder = s + delimiter_len;
2272           s = strstr (remainder, delimiter);
2273         }
2274     }
2275   if (*string)
2276     {
2277       n++;
2278       string_list = g_slist_prepend (string_list, g_strdup (remainder));
2279     }
2280
2281   str_array = g_new (gchar*, n + 1);
2282
2283   str_array[n--] = NULL;
2284   for (slist = string_list; slist; slist = slist->next)
2285     str_array[n--] = slist->data;
2286
2287   g_slist_free (string_list);
2288
2289   return str_array;
2290 }
2291
2292 /**
2293  * g_strsplit_set:
2294  * @string: The string to be tokenized
2295  * @delimiters: A nul-terminated string containing bytes that are used
2296  *     to split the string.
2297  * @max_tokens: The maximum number of tokens to split @string into.
2298  *     If this is less than 1, the string is split completely
2299  *
2300  * Splits @string into a number of tokens not containing any of the characters
2301  * in @delimiter. A token is the (possibly empty) longest string that does not
2302  * contain any of the characters in @delimiters. If @max_tokens is reached, the
2303  * remainder is appended to the last token.
2304  *
2305  * For example the result of g_strsplit_set ("abc:def/ghi", ":/", -1) is a
2306  * %NULL-terminated vector containing the three strings "abc", "def",
2307  * and "ghi".
2308  *
2309  * The result if g_strsplit_set (":def/ghi:", ":/", -1) is a %NULL-terminated
2310  * vector containing the four strings "", "def", "ghi", and "".
2311  *
2312  * As a special case, the result of splitting the empty string "" is an empty
2313  * vector, not a vector containing a single string. The reason for this
2314  * special case is that being able to represent a empty vector is typically
2315  * more useful than consistent handling of empty elements. If you do need
2316  * to represent empty elements, you'll need to check for the empty string
2317  * before calling g_strsplit_set().
2318  *
2319  * Note that this function works on bytes not characters, so it can't be used
2320  * to delimit UTF-8 strings for anything but ASCII characters.
2321  *
2322  * Return value: a newly-allocated %NULL-terminated array of strings. Use
2323  *    g_strfreev() to free it.
2324  *
2325  * Since: 2.4
2326  **/
2327 gchar **
2328 g_strsplit_set (const gchar *string,
2329                 const gchar *delimiters,
2330                 gint         max_tokens)
2331 {
2332   gboolean delim_table[256];
2333   GSList *tokens, *list;
2334   gint n_tokens;
2335   const gchar *s;
2336   const gchar *current;
2337   gchar *token;
2338   gchar **result;
2339
2340   g_return_val_if_fail (string != NULL, NULL);
2341   g_return_val_if_fail (delimiters != NULL, NULL);
2342
2343   if (max_tokens < 1)
2344     max_tokens = G_MAXINT;
2345
2346   if (*string == '\0')
2347     {
2348       result = g_new (char *, 1);
2349       result[0] = NULL;
2350       return result;
2351     }
2352
2353   memset (delim_table, FALSE, sizeof (delim_table));
2354   for (s = delimiters; *s != '\0'; ++s)
2355     delim_table[*(guchar *)s] = TRUE;
2356
2357   tokens = NULL;
2358   n_tokens = 0;
2359
2360   s = current = string;
2361   while (*s != '\0')
2362     {
2363       if (delim_table[*(guchar *)s] && n_tokens + 1 < max_tokens)
2364         {
2365           token = g_strndup (current, s - current);
2366           tokens = g_slist_prepend (tokens, token);
2367           ++n_tokens;
2368
2369           current = s + 1;
2370         }
2371
2372       ++s;
2373     }
2374
2375   token = g_strndup (current, s - current);
2376   tokens = g_slist_prepend (tokens, token);
2377   ++n_tokens;
2378
2379   result = g_new (gchar *, n_tokens + 1);
2380
2381   result[n_tokens] = NULL;
2382   for (list = tokens; list != NULL; list = list->next)
2383     result[--n_tokens] = list->data;
2384
2385   g_slist_free (tokens);
2386
2387   return result;
2388 }
2389
2390 /**
2391  * g_strfreev:
2392  * @str_array: a %NULL-terminated array of strings to free
2393
2394  * Frees a %NULL-terminated array of strings, and the array itself.
2395  * If called on a %NULL value, g_strfreev() simply returns.
2396  **/
2397 void
2398 g_strfreev (gchar **str_array)
2399 {
2400   if (str_array)
2401     {
2402       int i;
2403
2404       for (i = 0; str_array[i] != NULL; i++)
2405         g_free (str_array[i]);
2406
2407       g_free (str_array);
2408     }
2409 }
2410
2411 /**
2412  * g_strdupv:
2413  * @str_array: a %NULL-terminated array of strings
2414  *
2415  * Copies %NULL-terminated array of strings. The copy is a deep copy;
2416  * the new array should be freed by first freeing each string, then
2417  * the array itself. g_strfreev() does this for you. If called
2418  * on a %NULL value, g_strdupv() simply returns %NULL.
2419  *
2420  * Return value: a new %NULL-terminated array of strings.
2421  */
2422 gchar**
2423 g_strdupv (gchar **str_array)
2424 {
2425   if (str_array)
2426     {
2427       gint i;
2428       gchar **retval;
2429
2430       i = 0;
2431       while (str_array[i])
2432         ++i;
2433
2434       retval = g_new (gchar*, i + 1);
2435
2436       i = 0;
2437       while (str_array[i])
2438         {
2439           retval[i] = g_strdup (str_array[i]);
2440           ++i;
2441         }
2442       retval[i] = NULL;
2443
2444       return retval;
2445     }
2446   else
2447     return NULL;
2448 }
2449
2450 /**
2451  * g_strjoinv:
2452  * @separator: (allow-none): a string to insert between each of the strings, or %NULL
2453  * @str_array: a %NULL-terminated array of strings to join
2454  *
2455  * Joins a number of strings together to form one long string, with the
2456  * optional @separator inserted between each of them. The returned string
2457  * should be freed with g_free().
2458  *
2459  * Returns: a newly-allocated string containing all of the strings joined
2460  *     together, with @separator between them
2461  */
2462 gchar*
2463 g_strjoinv (const gchar  *separator,
2464             gchar       **str_array)
2465 {
2466   gchar *string;
2467   gchar *ptr;
2468
2469   g_return_val_if_fail (str_array != NULL, NULL);
2470
2471   if (separator == NULL)
2472     separator = "";
2473
2474   if (*str_array)
2475     {
2476       gint i;
2477       gsize len;
2478       gsize separator_len;
2479
2480       separator_len = strlen (separator);
2481       /* First part, getting length */
2482       len = 1 + strlen (str_array[0]);
2483       for (i = 1; str_array[i] != NULL; i++)
2484         len += strlen (str_array[i]);
2485       len += separator_len * (i - 1);
2486
2487       /* Second part, building string */
2488       string = g_new (gchar, len);
2489       ptr = g_stpcpy (string, *str_array);
2490       for (i = 1; str_array[i] != NULL; i++)
2491         {
2492           ptr = g_stpcpy (ptr, separator);
2493           ptr = g_stpcpy (ptr, str_array[i]);
2494         }
2495       }
2496   else
2497     string = g_strdup ("");
2498
2499   return string;
2500 }
2501
2502 /**
2503  * g_strjoin:
2504  * @separator: (allow-none): a string to insert between each of the strings, or %NULL
2505  * @...: a %NULL-terminated list of strings to join
2506  *
2507  * Joins a number of strings together to form one long string, with the
2508  * optional @separator inserted between each of them. The returned string
2509  * should be freed with g_free().
2510  *
2511  * Returns: a newly-allocated string containing all of the strings joined
2512  *     together, with @separator between them
2513  */
2514 gchar*
2515 g_strjoin (const gchar *separator,
2516            ...)
2517 {
2518   gchar *string, *s;
2519   va_list args;
2520   gsize len;
2521   gsize separator_len;
2522   gchar *ptr;
2523
2524   if (separator == NULL)
2525     separator = "";
2526
2527   separator_len = strlen (separator);
2528
2529   va_start (args, separator);
2530
2531   s = va_arg (args, gchar*);
2532
2533   if (s)
2534     {
2535       /* First part, getting length */
2536       len = 1 + strlen (s);
2537
2538       s = va_arg (args, gchar*);
2539       while (s)
2540         {
2541           len += separator_len + strlen (s);
2542           s = va_arg (args, gchar*);
2543         }
2544       va_end (args);
2545
2546       /* Second part, building string */
2547       string = g_new (gchar, len);
2548
2549       va_start (args, separator);
2550
2551       s = va_arg (args, gchar*);
2552       ptr = g_stpcpy (string, s);
2553
2554       s = va_arg (args, gchar*);
2555       while (s)
2556         {
2557           ptr = g_stpcpy (ptr, separator);
2558           ptr = g_stpcpy (ptr, s);
2559           s = va_arg (args, gchar*);
2560         }
2561     }
2562   else
2563     string = g_strdup ("");
2564
2565   va_end (args);
2566
2567   return string;
2568 }
2569
2570
2571 /**
2572  * g_strstr_len:
2573  * @haystack: a string
2574  * @haystack_len: the maximum length of @haystack. Note that -1 is
2575  *     a valid length, if @haystack is nul-terminated, meaning it will
2576  *     search through the whole string.
2577  * @needle: the string to search for
2578  *
2579  * Searches the string @haystack for the first occurrence
2580  * of the string @needle, limiting the length of the search
2581  * to @haystack_len.
2582  *
2583  * Return value: a pointer to the found occurrence, or
2584  *    %NULL if not found.
2585  */
2586 gchar *
2587 g_strstr_len (const gchar *haystack,
2588               gssize       haystack_len,
2589               const gchar *needle)
2590 {
2591   g_return_val_if_fail (haystack != NULL, NULL);
2592   g_return_val_if_fail (needle != NULL, NULL);
2593
2594   if (haystack_len < 0)
2595     return strstr (haystack, needle);
2596   else
2597     {
2598       const gchar *p = haystack;
2599       gsize needle_len = strlen (needle);
2600       const gchar *end;
2601       gsize i;
2602
2603       if (needle_len == 0)
2604         return (gchar *)haystack;
2605
2606       if (haystack_len < needle_len)
2607         return NULL;
2608
2609       end = haystack + haystack_len - needle_len;
2610
2611       while (p <= end && *p)
2612         {
2613           for (i = 0; i < needle_len; i++)
2614             if (p[i] != needle[i])
2615               goto next;
2616
2617           return (gchar *)p;
2618
2619         next:
2620           p++;
2621         }
2622
2623       return NULL;
2624     }
2625 }
2626
2627 /**
2628  * g_strrstr:
2629  * @haystack: a nul-terminated string
2630  * @needle: the nul-terminated string to search for
2631  *
2632  * Searches the string @haystack for the last occurrence
2633  * of the string @needle.
2634  *
2635  * Return value: a pointer to the found occurrence, or
2636  *    %NULL if not found.
2637  */
2638 gchar *
2639 g_strrstr (const gchar *haystack,
2640            const gchar *needle)
2641 {
2642   gsize i;
2643   gsize needle_len;
2644   gsize haystack_len;
2645   const gchar *p;
2646
2647   g_return_val_if_fail (haystack != NULL, NULL);
2648   g_return_val_if_fail (needle != NULL, NULL);
2649
2650   needle_len = strlen (needle);
2651   haystack_len = strlen (haystack);
2652
2653   if (needle_len == 0)
2654     return (gchar *)haystack;
2655
2656   if (haystack_len < needle_len)
2657     return NULL;
2658
2659   p = haystack + haystack_len - needle_len;
2660
2661   while (p >= haystack)
2662     {
2663       for (i = 0; i < needle_len; i++)
2664         if (p[i] != needle[i])
2665           goto next;
2666
2667       return (gchar *)p;
2668
2669     next:
2670       p--;
2671     }
2672
2673   return NULL;
2674 }
2675
2676 /**
2677  * g_strrstr_len:
2678  * @haystack: a nul-terminated string
2679  * @haystack_len: the maximum length of @haystack
2680  * @needle: the nul-terminated string to search for
2681  *
2682  * Searches the string @haystack for the last occurrence
2683  * of the string @needle, limiting the length of the search
2684  * to @haystack_len.
2685  *
2686  * Return value: a pointer to the found occurrence, or
2687  *    %NULL if not found.
2688  */
2689 gchar *
2690 g_strrstr_len (const gchar *haystack,
2691                gssize        haystack_len,
2692                const gchar *needle)
2693 {
2694   g_return_val_if_fail (haystack != NULL, NULL);
2695   g_return_val_if_fail (needle != NULL, NULL);
2696
2697   if (haystack_len < 0)
2698     return g_strrstr (haystack, needle);
2699   else
2700     {
2701       gsize needle_len = strlen (needle);
2702       const gchar *haystack_max = haystack + haystack_len;
2703       const gchar *p = haystack;
2704       gsize i;
2705
2706       while (p < haystack_max && *p)
2707         p++;
2708
2709       if (p < haystack + needle_len)
2710         return NULL;
2711
2712       p -= needle_len;
2713
2714       while (p >= haystack)
2715         {
2716           for (i = 0; i < needle_len; i++)
2717             if (p[i] != needle[i])
2718               goto next;
2719
2720           return (gchar *)p;
2721
2722         next:
2723           p--;
2724         }
2725
2726       return NULL;
2727     }
2728 }
2729
2730
2731 /**
2732  * g_str_has_suffix:
2733  * @str: a nul-terminated string
2734  * @suffix: the nul-terminated suffix to look for
2735  *
2736  * Looks whether the string @str ends with @suffix.
2737  *
2738  * Return value: %TRUE if @str end with @suffix, %FALSE otherwise.
2739  *
2740  * Since: 2.2
2741  */
2742 gboolean
2743 g_str_has_suffix (const gchar *str,
2744                   const gchar *suffix)
2745 {
2746   int str_len;
2747   int suffix_len;
2748
2749   g_return_val_if_fail (str != NULL, FALSE);
2750   g_return_val_if_fail (suffix != NULL, FALSE);
2751
2752   str_len = strlen (str);
2753   suffix_len = strlen (suffix);
2754
2755   if (str_len < suffix_len)
2756     return FALSE;
2757
2758   return strcmp (str + str_len - suffix_len, suffix) == 0;
2759 }
2760
2761 /**
2762  * g_str_has_prefix:
2763  * @str: a nul-terminated string
2764  * @prefix: the nul-terminated prefix to look for
2765  *
2766  * Looks whether the string @str begins with @prefix.
2767  *
2768  * Return value: %TRUE if @str begins with @prefix, %FALSE otherwise.
2769  *
2770  * Since: 2.2
2771  */
2772 gboolean
2773 g_str_has_prefix (const gchar *str,
2774                   const gchar *prefix)
2775 {
2776   int str_len;
2777   int prefix_len;
2778
2779   g_return_val_if_fail (str != NULL, FALSE);
2780   g_return_val_if_fail (prefix != NULL, FALSE);
2781
2782   str_len = strlen (str);
2783   prefix_len = strlen (prefix);
2784
2785   if (str_len < prefix_len)
2786     return FALSE;
2787
2788   return strncmp (str, prefix, prefix_len) == 0;
2789 }
2790
2791 /**
2792  * g_strv_length:
2793  * @str_array: a %NULL-terminated array of strings
2794  *
2795  * Returns the length of the given %NULL-terminated
2796  * string array @str_array.
2797  *
2798  * Return value: length of @str_array.
2799  *
2800  * Since: 2.6
2801  */
2802 guint
2803 g_strv_length (gchar **str_array)
2804 {
2805   guint i = 0;
2806
2807   g_return_val_if_fail (str_array != NULL, 0);
2808
2809   while (str_array[i])
2810     ++i;
2811
2812   return i;
2813 }