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