GIcon: add g_icon_[de]serialize()
[glib.git] / glib / gstrfuncs.c
blob95092331064bdb9c66dfc6cfac2dd44446ca3dd9
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
28 * MT safe
31 #include "config.h"
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() */
42 #ifdef HAVE_XLOCALE_H
43 /* Needed on BSD/OS X for e.g. strtod_l */
44 #include <xlocale.h>
45 #endif
47 #ifdef G_OS_WIN32
48 #include <windows.h>
49 #endif
51 /* do not include <unistd.h> here, it may interfere with g_strsignal() */
53 #include "gstrfuncs.h"
55 #include "gprintf.h"
56 #include "gprintfint.h"
57 #include "glibintl.h"
60 /**
61 * SECTION:string_utils
62 * @title: String Utility Functions
63 * @short_description: various string-related functions
65 * This section describes a number of utility functions for creating,
66 * duplicating, and manipulating strings.
68 * Note that the functions g_printf(), g_fprintf(), g_sprintf(),
69 * g_snprintf(), g_vprintf(), g_vfprintf(), g_vsprintf() and g_vsnprintf()
70 * are declared in the header <filename>gprintf.h</filename> which is
71 * <emphasis>not</emphasis> included in <filename>glib.h</filename>
72 * (otherwise using <filename>glib.h</filename> would drag in
73 * <filename>stdio.h</filename>), so you'll have to explicitly include
74 * <literal>&lt;glib/gprintf.h&gt;</literal> in order to use the GLib
75 * printf() functions.
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>
93 /**
94 * g_ascii_isalnum:
95 * @c: any character
97 * Determines whether a character is alphanumeric.
99 * Unlike the standard C library isalnum() function, this only
100 * recognizes standard ASCII letters and ignores the locale,
101 * returning %FALSE for all non-ASCII characters. Also, unlike
102 * the standard library function, this takes a <type>char</type>,
103 * not an <type>int</type>, so don't call it on <literal>EOF</literal>, but no need to
104 * cast to #guchar before passing a possibly non-ASCII character in.
106 * Returns: %TRUE if @c is an ASCII alphanumeric character
110 * g_ascii_isalpha:
111 * @c: any character
113 * Determines whether a character is alphabetic (i.e. a letter).
115 * Unlike the standard C library isalpha() function, this only
116 * recognizes standard ASCII letters and ignores the locale,
117 * returning %FALSE for all non-ASCII characters. Also, unlike
118 * the standard library function, this takes a <type>char</type>,
119 * not an <type>int</type>, so don't call it on <literal>EOF</literal>, but no need to
120 * cast to #guchar before passing a possibly non-ASCII character in.
122 * Returns: %TRUE if @c is an ASCII alphabetic character
126 * g_ascii_iscntrl:
127 * @c: any character
129 * Determines whether a character is a control character.
131 * Unlike the standard C library iscntrl() function, this only
132 * recognizes standard ASCII control characters and ignores the
133 * locale, returning %FALSE for all non-ASCII characters. Also,
134 * unlike the standard library function, this takes a <type>char</type>,
135 * not an <type>int</type>, so don't call it on <literal>EOF</literal>, but no need to
136 * cast to #guchar before passing a possibly non-ASCII character in.
138 * Returns: %TRUE if @c is an ASCII control character.
142 * g_ascii_isdigit:
143 * @c: any character
145 * Determines whether a character is digit (0-9).
147 * Unlike the standard C library isdigit() function, this takes
148 * a <type>char</type>, not an <type>int</type>, so don't call it
149 * on <literal>EOF</literal>, but no need to cast to #guchar before passing a possibly
150 * non-ASCII character in.
152 * Returns: %TRUE if @c is an ASCII digit.
156 * g_ascii_isgraph:
157 * @c: any character
159 * Determines whether a character is a printing character and not a space.
161 * Unlike the standard C library isgraph() function, this only
162 * recognizes standard ASCII characters and ignores the locale,
163 * returning %FALSE for all non-ASCII characters. Also, unlike
164 * the standard library function, this takes a <type>char</type>,
165 * not an <type>int</type>, so don't call it on <literal>EOF</literal>, but no need
166 * to cast to #guchar before passing a possibly non-ASCII character in.
168 * Returns: %TRUE if @c is an ASCII printing character other than space.
172 * g_ascii_islower:
173 * @c: any character
175 * Determines whether a character is an ASCII lower case letter.
177 * Unlike the standard C library islower() function, this only
178 * recognizes standard ASCII letters and ignores the locale,
179 * returning %FALSE for all non-ASCII characters. Also, unlike
180 * the standard library function, this takes a <type>char</type>,
181 * not an <type>int</type>, so don't call it on <literal>EOF</literal>, but no need
182 * to worry about casting to #guchar before passing a possibly
183 * non-ASCII character in.
185 * Returns: %TRUE if @c is an ASCII lower case letter
189 * g_ascii_isprint:
190 * @c: any character
192 * Determines whether a character is a printing character.
194 * Unlike the standard C library isprint() function, this only
195 * recognizes standard ASCII characters and ignores the locale,
196 * returning %FALSE for all non-ASCII characters. Also, unlike
197 * the standard library function, this takes a <type>char</type>,
198 * not an <type>int</type>, so don't call it on <literal>EOF</literal>, but no need
199 * to cast to #guchar before passing a possibly non-ASCII character in.
201 * Returns: %TRUE if @c is an ASCII printing character.
205 * g_ascii_ispunct:
206 * @c: any character
208 * Determines whether a character is a punctuation character.
210 * Unlike the standard C library ispunct() function, this only
211 * recognizes standard ASCII letters and ignores the locale,
212 * returning %FALSE for all non-ASCII characters. Also, unlike
213 * the standard library function, this takes a <type>char</type>,
214 * not an <type>int</type>, so don't call it on <literal>EOF</literal>, but no need to
215 * cast to #guchar before passing a possibly non-ASCII character in.
217 * Returns: %TRUE if @c is an ASCII punctuation character.
221 * g_ascii_isspace:
222 * @c: any character
224 * Determines whether a character is a white-space character.
226 * Unlike the standard C library isspace() function, this only
227 * recognizes standard ASCII white-space and ignores the locale,
228 * returning %FALSE for all non-ASCII characters. Also, unlike
229 * the standard library function, this takes a <type>char</type>,
230 * not an <type>int</type>, so don't call it on <literal>EOF</literal>, but no need to
231 * cast to #guchar before passing a possibly non-ASCII character in.
233 * Returns: %TRUE if @c is an ASCII white-space character
237 * g_ascii_isupper:
238 * @c: any character
240 * Determines whether a character is an ASCII upper case letter.
242 * Unlike the standard C library isupper() function, this only
243 * recognizes standard ASCII letters and ignores the locale,
244 * returning %FALSE for all non-ASCII characters. Also, unlike
245 * the standard library function, this takes a <type>char</type>,
246 * not an <type>int</type>, so don't call it on <literal>EOF</literal>, but no need to
247 * worry about casting to #guchar before passing a possibly non-ASCII
248 * character in.
250 * Returns: %TRUE if @c is an ASCII upper case letter
254 * g_ascii_isxdigit:
255 * @c: any character
257 * Determines whether a character is a hexadecimal-digit character.
259 * Unlike the standard C library isxdigit() function, this takes
260 * a <type>char</type>, not an <type>int</type>, so don't call it
261 * on <literal>EOF</literal>, but no need to cast to #guchar before passing a
262 * possibly non-ASCII character in.
264 * Returns: %TRUE if @c is an ASCII hexadecimal-digit character.
268 * G_ASCII_DTOSTR_BUF_SIZE:
270 * A good size for a buffer to be passed into g_ascii_dtostr().
271 * It is guaranteed to be enough for all output of that function
272 * on systems with 64bit IEEE-compatible doubles.
274 * The typical usage would be something like:
275 * |[
276 * char buf[G_ASCII_DTOSTR_BUF_SIZE];
278 * fprintf (out, "value=&percnt;s\n", g_ascii_dtostr (buf, sizeof (buf), value));
279 * ]|
283 * g_strstrip:
284 * @string: a string to remove the leading and trailing whitespace from
286 * Removes leading and trailing whitespace from a string.
287 * See g_strchomp() and g_strchug().
289 * Returns: @string
293 * G_STR_DELIMITERS:
295 * The standard delimiters, used in g_strdelimit().
298 static const guint16 ascii_table_data[256] = {
299 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
300 0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004,
301 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
302 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
303 0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
304 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
305 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
306 0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
307 0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
308 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
309 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
310 0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
311 0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
312 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
313 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
314 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004
315 /* the upper 128 are all zeroes */
318 const guint16 * const g_ascii_table = ascii_table_data;
320 #if defined (HAVE_NEWLOCALE) && \
321 defined (HAVE_USELOCALE) && \
322 defined (HAVE_STRTOD_L) && \
323 defined (HAVE_STRTOULL_L) && \
324 defined (HAVE_STRTOLL_L)
325 #define USE_XLOCALE 1
326 #endif
328 #ifdef USE_XLOCALE
329 static locale_t
330 get_C_locale (void)
332 static gsize initialized = FALSE;
333 static locale_t C_locale = NULL;
335 if (g_once_init_enter (&initialized))
337 C_locale = newlocale (LC_ALL_MASK, "C", NULL);
338 g_once_init_leave (&initialized, TRUE);
341 return C_locale;
343 #endif
346 * g_strdup:
347 * @str: the string to duplicate
349 * Duplicates a string. If @str is %NULL it returns %NULL.
350 * The returned string should be freed with g_free()
351 * when no longer needed.
353 * Returns: a newly-allocated copy of @str
355 gchar*
356 g_strdup (const gchar *str)
358 gchar *new_str;
359 gsize length;
361 if (str)
363 length = strlen (str) + 1;
364 new_str = g_new (char, length);
365 memcpy (new_str, str, length);
367 else
368 new_str = NULL;
370 return new_str;
374 * g_memdup:
375 * @mem: the memory to copy.
376 * @byte_size: the number of bytes to copy.
378 * Allocates @byte_size bytes of memory, and copies @byte_size bytes into it
379 * from @mem. If @mem is %NULL it returns %NULL.
381 * Returns: a pointer to the newly-allocated copy of the memory, or %NULL if @mem
382 * is %NULL.
384 gpointer
385 g_memdup (gconstpointer mem,
386 guint byte_size)
388 gpointer new_mem;
390 if (mem)
392 new_mem = g_malloc (byte_size);
393 memcpy (new_mem, mem, byte_size);
395 else
396 new_mem = NULL;
398 return new_mem;
402 * g_strndup:
403 * @str: the string to duplicate
404 * @n: the maximum number of bytes to copy from @str
406 * Duplicates the first @n bytes of a string, returning a newly-allocated
407 * buffer @n + 1 bytes long which will always be nul-terminated.
408 * If @str is less than @n bytes long the buffer is padded with nuls.
409 * If @str is %NULL it returns %NULL.
410 * The returned value should be freed when no longer needed.
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>
417 * Returns: a newly-allocated buffer containing the first @n bytes
418 * of @str, nul-terminated
420 gchar*
421 g_strndup (const gchar *str,
422 gsize n)
424 gchar *new_str;
426 if (str)
428 new_str = g_new (gchar, n + 1);
429 strncpy (new_str, str, n);
430 new_str[n] = '\0';
432 else
433 new_str = NULL;
435 return new_str;
439 * g_strnfill:
440 * @length: the length of the new string
441 * @fill_char: the byte to fill the string with
443 * Creates a new string @length bytes long filled with @fill_char.
444 * The returned string should be freed when no longer needed.
446 * Returns: a newly-allocated string filled the @fill_char
448 gchar*
449 g_strnfill (gsize length,
450 gchar fill_char)
452 gchar *str;
454 str = g_new (gchar, length + 1);
455 memset (str, (guchar)fill_char, length);
456 str[length] = '\0';
458 return str;
462 * g_stpcpy:
463 * @dest: destination buffer.
464 * @src: source string.
466 * Copies a nul-terminated string into the dest buffer, include the
467 * trailing nul, and return a pointer to the trailing nul byte.
468 * This is useful for concatenating multiple strings together
469 * without having to repeatedly scan for the end.
471 * Return value: a pointer to trailing nul byte.
473 gchar *
474 g_stpcpy (gchar *dest,
475 const gchar *src)
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;
485 g_return_val_if_fail (dest != NULL, NULL);
486 g_return_val_if_fail (src != NULL, NULL);
488 *d++ = *s;
489 while (*s++ != '\0');
491 return d - 1;
492 #endif
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
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.
506 * See also g_vasprintf(), which offers the same functionality, but
507 * additionally returns the length of the allocated string.
509 * Returns: a newly-allocated string holding the result
511 gchar*
512 g_strdup_vprintf (const gchar *format,
513 va_list args)
515 gchar *string = NULL;
517 g_vasprintf (&string, format, args);
519 return string;
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
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.
533 * Returns: a newly-allocated string holding the result
535 gchar*
536 g_strdup_printf (const gchar *format,
537 ...)
539 gchar *buffer;
540 va_list args;
542 va_start (args, format);
543 buffer = g_strdup_vprintf (format, args);
544 va_end (args);
546 return buffer;
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
554 * Concatenates all of the given strings into one long string.
555 * The returned string should be freed with g_free() when no longer needed.
557 * Note that this function is usually not the right function to use to
558 * assemble a translated message from pieces, since proper translation
559 * often requires the pieces to be reordered.
561 * <warning><para>The variable argument list <emphasis>must</emphasis> end
562 * with %NULL. If you forget the %NULL, g_strconcat() will start appending
563 * random memory junk to your string.</para></warning>
565 * Returns: a newly-allocated string containing all the string arguments
567 gchar*
568 g_strconcat (const gchar *string1, ...)
570 gsize l;
571 va_list args;
572 gchar *s;
573 gchar *concat;
574 gchar *ptr;
576 if (!string1)
577 return NULL;
579 l = 1 + strlen (string1);
580 va_start (args, string1);
581 s = va_arg (args, gchar*);
582 while (s)
584 l += strlen (s);
585 s = va_arg (args, gchar*);
587 va_end (args);
589 concat = g_new (gchar, l);
590 ptr = concat;
592 ptr = g_stpcpy (ptr, string1);
593 va_start (args, string1);
594 s = va_arg (args, gchar*);
595 while (s)
597 ptr = g_stpcpy (ptr, s);
598 s = va_arg (args, gchar*);
600 va_end (args);
602 return concat;
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.
611 * Converts a string to a #gdouble value.
612 * It calls the standard strtod() function to handle the conversion, but
613 * if the string is not completely converted it attempts the conversion
614 * again with g_ascii_strtod(), and returns the best match.
616 * This function should seldom be used. The normal situation when reading
617 * numbers not for human consumption is to use g_ascii_strtod(). Only when
618 * you know that you must expect both locale formatted and C formatted numbers
619 * should you use this. Make sure that you don't pass strings such as comma
620 * separated lists of values, since the commas may be interpreted as a decimal
621 * point in some locales, causing unexpected results.
623 * Return value: the #gdouble value.
625 gdouble
626 g_strtod (const gchar *nptr,
627 gchar **endptr)
629 gchar *fail_pos_1;
630 gchar *fail_pos_2;
631 gdouble val_1;
632 gdouble val_2 = 0;
634 g_return_val_if_fail (nptr != NULL, 0);
636 fail_pos_1 = NULL;
637 fail_pos_2 = NULL;
639 val_1 = strtod (nptr, &fail_pos_1);
641 if (fail_pos_1 && fail_pos_1[0] != 0)
642 val_2 = g_ascii_strtod (nptr, &fail_pos_2);
644 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
646 if (endptr)
647 *endptr = fail_pos_1;
648 return val_1;
650 else
652 if (endptr)
653 *endptr = fail_pos_2;
654 return val_2;
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.
664 * Converts a string to a #gdouble value.
666 * This function behaves like the standard strtod() function
667 * does in the C locale. It does this without actually changing
668 * the current locale, since that would not be thread-safe.
669 * A limitation of the implementation is that this function
670 * will still accept localized versions of infinities and NANs.
672 * This function is typically used when reading configuration
673 * files or other non-user input that should be locale independent.
674 * To handle input from the user you should normally use the
675 * locale-sensitive system strtod() function.
677 * To convert from a #gdouble to a string in a locale-insensitive
678 * way, use g_ascii_dtostr().
680 * If the correct value would cause overflow, plus or minus <literal>HUGE_VAL</literal>
681 * is returned (according to the sign of the value), and <literal>ERANGE</literal> is
682 * stored in <literal>errno</literal>. If the correct value would cause underflow,
683 * zero is returned and <literal>ERANGE</literal> is stored in <literal>errno</literal>.
685 * This function resets <literal>errno</literal> before calling strtod() so that
686 * you can reliably detect overflow and underflow.
688 * Return value: the #gdouble value.
690 gdouble
691 g_ascii_strtod (const gchar *nptr,
692 gchar **endptr)
694 #ifdef USE_XLOCALE
696 g_return_val_if_fail (nptr != NULL, 0);
698 errno = 0;
700 return strtod_l (nptr, endptr, get_C_locale ());
702 #else
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;
715 g_return_val_if_fail (nptr != NULL, 0);
717 fail_pos = NULL;
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
728 g_assert (decimal_point_len != 0);
730 decimal_point_pos = NULL;
731 end = NULL;
733 if (decimal_point[0] != '.' ||
734 decimal_point[1] != 0)
736 p = nptr;
737 /* Skip leading space */
738 while (g_ascii_isspace (*p))
739 p++;
741 /* Skip leading optional sign */
742 if (*p == '+' || *p == '-')
743 p++;
745 if (p[0] == '0' &&
746 (p[1] == 'x' || p[1] == 'X'))
748 p += 2;
749 /* HEX - find the (optional) decimal point */
751 while (g_ascii_isxdigit (*p))
752 p++;
754 if (*p == '.')
755 decimal_point_pos = p++;
757 while (g_ascii_isxdigit (*p))
758 p++;
760 if (*p == 'p' || *p == 'P')
761 p++;
762 if (*p == '+' || *p == '-')
763 p++;
764 while (g_ascii_isdigit (*p))
765 p++;
767 end = p;
769 else if (g_ascii_isdigit (*p) || *p == '.')
771 while (g_ascii_isdigit (*p))
772 p++;
774 if (*p == '.')
775 decimal_point_pos = p++;
777 while (g_ascii_isdigit (*p))
778 p++;
780 if (*p == 'e' || *p == 'E')
781 p++;
782 if (*p == '+' || *p == '-')
783 p++;
784 while (g_ascii_isdigit (*p))
785 p++;
787 end = p;
789 /* For the other cases, we need not convert the decimal point */
792 if (decimal_point_pos)
794 char *copy, *c;
796 /* We need to convert the '.' to the locale specific decimal point */
797 copy = g_malloc (end - nptr + 1 + decimal_point_len);
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;
808 errno = 0;
809 val = strtod (copy, &fail_pos);
810 strtod_errno = errno;
812 if (fail_pos)
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);
820 g_free (copy);
823 else if (end)
825 char *copy;
827 copy = g_malloc (end - (char *)nptr + 1);
828 memcpy (copy, nptr, end - nptr);
829 *(copy + (end - (char *)nptr)) = 0;
831 errno = 0;
832 val = strtod (copy, &fail_pos);
833 strtod_errno = errno;
835 if (fail_pos)
837 fail_pos = (char *)nptr + (fail_pos - copy);
840 g_free (copy);
842 else
844 errno = 0;
845 val = strtod (nptr, &fail_pos);
846 strtod_errno = errno;
849 if (endptr)
850 *endptr = fail_pos;
852 errno = strtod_errno;
854 return val;
855 #endif
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
865 * Converts a #gdouble to a string, using the '.' as
866 * decimal point.
868 * This functions generates enough precision that converting
869 * the string back using g_ascii_strtod() gives the same machine-number
870 * (on machines with IEEE compatible 64bit doubles). It is
871 * guaranteed that the size of the resulting string will never
872 * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes.
874 * Return value: The pointer to the buffer with the converted string.
876 gchar *
877 g_ascii_dtostr (gchar *buffer,
878 gint buf_len,
879 gdouble d)
881 return g_ascii_formatd (buffer, buf_len, "%.17g", d);
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
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'.
897 * If you just want to want to serialize the value into a
898 * string, use g_ascii_dtostr().
900 * Return value: The pointer to the buffer with the converted string.
902 gchar *
903 g_ascii_formatd (gchar *buffer,
904 gint buf_len,
905 const gchar *format,
906 gdouble d)
908 #ifdef USE_XLOCALE
909 locale_t old_locale;
911 old_locale = uselocale (get_C_locale ());
912 _g_snprintf (buffer, buf_len, format, d);
913 uselocale (old_locale);
915 return buffer;
916 #else
917 #ifndef __BIONIC__
918 struct lconv *locale_data;
919 #endif
920 const char *decimal_point;
921 int decimal_point_len;
922 gchar *p;
923 int rest_len;
924 gchar format_char;
926 g_return_val_if_fail (buffer != NULL, NULL);
927 g_return_val_if_fail (format[0] == '%', NULL);
928 g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
930 format_char = format[strlen (format) - 1];
932 g_return_val_if_fail (format_char == 'e' || format_char == 'E' ||
933 format_char == 'f' || format_char == 'F' ||
934 format_char == 'g' || format_char == 'G',
935 NULL);
937 if (format[0] != '%')
938 return NULL;
940 if (strpbrk (format + 1, "'l%"))
941 return NULL;
943 if (!(format_char == 'e' || format_char == 'E' ||
944 format_char == 'f' || format_char == 'F' ||
945 format_char == 'g' || format_char == 'G'))
946 return NULL;
948 _g_snprintf (buffer, buf_len, format, d);
950 #ifndef __BIONIC__
951 locale_data = localeconv ();
952 decimal_point = locale_data->decimal_point;
953 decimal_point_len = strlen (decimal_point);
954 #else
955 decimal_point = ".";
956 decimal_point_len = 1;
957 #endif
959 g_assert (decimal_point_len != 0);
961 if (decimal_point[0] != '.' ||
962 decimal_point[1] != 0)
964 p = buffer;
966 while (g_ascii_isspace (*p))
967 p++;
969 if (*p == '+' || *p == '-')
970 p++;
972 while (isdigit ((guchar)*p))
973 p++;
975 if (strncmp (p, decimal_point, decimal_point_len) == 0)
977 *p = '.';
978 p++;
979 if (decimal_point_len > 1)
981 rest_len = strlen (p + (decimal_point_len-1));
982 memmove (p, p + (decimal_point_len-1), rest_len);
983 p[rest_len] = 0;
988 return buffer;
989 #endif
992 #define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
993 (c) == '\r' || (c) == '\t' || (c) == '\v')
994 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
995 #define ISLOWER(c) ((c) >= 'a' && (c) <= 'z')
996 #define ISALPHA(c) (ISUPPER (c) || ISLOWER (c))
997 #define TOUPPER(c) (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
998 #define TOLOWER(c) (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
1000 #ifndef USE_XLOCALE
1002 static guint64
1003 g_parse_long_long (const gchar *nptr,
1004 const gchar **endptr,
1005 guint base,
1006 gboolean *negative)
1008 /* this code is based on on the strtol(3) code from GNU libc released under
1009 * the GNU Lesser General Public License.
1011 * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
1012 * Free Software Foundation, Inc.
1014 gboolean overflow;
1015 guint64 cutoff;
1016 guint64 cutlim;
1017 guint64 ui64;
1018 const gchar *s, *save;
1019 guchar c;
1021 g_return_val_if_fail (nptr != NULL, 0);
1023 *negative = FALSE;
1024 if (base == 1 || base > 36)
1026 errno = EINVAL;
1027 if (endptr)
1028 *endptr = nptr;
1029 return 0;
1032 save = s = nptr;
1034 /* Skip white space. */
1035 while (ISSPACE (*s))
1036 ++s;
1038 if (G_UNLIKELY (!*s))
1039 goto noconv;
1041 /* Check for a sign. */
1042 if (*s == '-')
1044 *negative = TRUE;
1045 ++s;
1047 else if (*s == '+')
1048 ++s;
1050 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
1051 if (*s == '0')
1053 if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
1055 s += 2;
1056 base = 16;
1058 else if (base == 0)
1059 base = 8;
1061 else if (base == 0)
1062 base = 10;
1064 /* Save the pointer so we can check later if anything happened. */
1065 save = s;
1066 cutoff = G_MAXUINT64 / base;
1067 cutlim = G_MAXUINT64 % base;
1069 overflow = FALSE;
1070 ui64 = 0;
1071 c = *s;
1072 for (; c; c = *++s)
1074 if (c >= '0' && c <= '9')
1075 c -= '0';
1076 else if (ISALPHA (c))
1077 c = TOUPPER (c) - 'A' + 10;
1078 else
1079 break;
1080 if (c >= base)
1081 break;
1082 /* Check for overflow. */
1083 if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
1084 overflow = TRUE;
1085 else
1087 ui64 *= base;
1088 ui64 += c;
1092 /* Check if anything actually happened. */
1093 if (s == save)
1094 goto noconv;
1096 /* Store in ENDPTR the address of one character
1097 past the last character we converted. */
1098 if (endptr)
1099 *endptr = s;
1101 if (G_UNLIKELY (overflow))
1103 errno = ERANGE;
1104 return G_MAXUINT64;
1107 return ui64;
1109 noconv:
1110 /* We must handle a special case here: the base is 0 or 16 and the
1111 first two characters are '0' and 'x', but the rest are no
1112 hexadecimal digits. This is no error case. We return 0 and
1113 ENDPTR points to the `x`. */
1114 if (endptr)
1116 if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
1117 && save[-2] == '0')
1118 *endptr = &save[-1];
1119 else
1120 /* There was no number to convert. */
1121 *endptr = nptr;
1123 return 0;
1125 #endif /* !USE_XLOCALE */
1128 * g_ascii_strtoull:
1129 * @nptr: the string to convert to a numeric value.
1130 * @endptr: if non-%NULL, it returns the character after
1131 * the last character used in the conversion.
1132 * @base: to be used for the conversion, 2..36 or 0
1134 * Converts a string to a #guint64 value.
1135 * This function behaves like the standard strtoull() function
1136 * does in the C locale. It does this without actually
1137 * changing the current locale, since that would not be
1138 * thread-safe.
1140 * This function is typically used when reading configuration
1141 * files or other non-user input that should be locale independent.
1142 * To handle input from the user you should normally use the
1143 * locale-sensitive system strtoull() function.
1145 * If the correct value would cause overflow, %G_MAXUINT64
1146 * is returned, and <literal>ERANGE</literal> is stored in <literal>errno</literal>.
1147 * If the base is outside the valid range, zero is returned, and
1148 * <literal>EINVAL</literal> is stored in <literal>errno</literal>.
1149 * If the string conversion fails, zero is returned, and @endptr returns
1150 * @nptr (if @endptr is non-%NULL).
1152 * Return value: the #guint64 value or zero on error.
1154 * Since: 2.2
1156 guint64
1157 g_ascii_strtoull (const gchar *nptr,
1158 gchar **endptr,
1159 guint base)
1161 #ifdef USE_XLOCALE
1162 return strtoull_l (nptr, endptr, base, get_C_locale ());
1163 #else
1164 gboolean negative;
1165 guint64 result;
1167 result = g_parse_long_long (nptr, (const gchar **) endptr, base, &negative);
1169 /* Return the result of the appropriate sign. */
1170 return negative ? -result : result;
1171 #endif
1175 * g_ascii_strtoll:
1176 * @nptr: the string to convert to a numeric value.
1177 * @endptr: if non-%NULL, it returns the character after
1178 * the last character used in the conversion.
1179 * @base: to be used for the conversion, 2..36 or 0
1181 * Converts a string to a #gint64 value.
1182 * This function behaves like the standard strtoll() function
1183 * does in the C locale. It does this without actually
1184 * changing the current locale, since that would not be
1185 * thread-safe.
1187 * This function is typically used when reading configuration
1188 * files or other non-user input that should be locale independent.
1189 * To handle input from the user you should normally use the
1190 * locale-sensitive system strtoll() function.
1192 * If the correct value would cause overflow, %G_MAXINT64 or %G_MININT64
1193 * is returned, and <literal>ERANGE</literal> is stored in <literal>errno</literal>.
1194 * If the base is outside the valid range, zero is returned, and
1195 * <literal>EINVAL</literal> is stored in <literal>errno</literal>. If the
1196 * string conversion fails, zero is returned, and @endptr returns @nptr
1197 * (if @endptr is non-%NULL).
1199 * Return value: the #gint64 value or zero on error.
1201 * Since: 2.12
1203 gint64
1204 g_ascii_strtoll (const gchar *nptr,
1205 gchar **endptr,
1206 guint base)
1208 #ifdef USE_XLOCALE
1209 return strtoll_l (nptr, endptr, base, get_C_locale ());
1210 #else
1211 gboolean negative;
1212 guint64 result;
1214 result = g_parse_long_long (nptr, (const gchar **) endptr, base, &negative);
1216 if (negative && result > (guint64) G_MININT64)
1218 errno = ERANGE;
1219 return G_MININT64;
1221 else if (!negative && result > (guint64) G_MAXINT64)
1223 errno = ERANGE;
1224 return G_MAXINT64;
1226 else if (negative)
1227 return - (gint64) result;
1228 else
1229 return (gint64) result;
1230 #endif
1234 * g_strerror:
1235 * @errnum: the system error number. See the standard C %errno
1236 * documentation
1238 * Returns a string corresponding to the given error code, e.g.
1239 * "no such process". You should use this function in preference to
1240 * strerror(), because it returns a string in UTF-8 encoding, and since
1241 * not all platforms support the strerror() function.
1243 * Returns: a UTF-8 string describing the error code. If the error code
1244 * is unknown, it returns "unknown error (&lt;code&gt;)".
1246 const gchar *
1247 g_strerror (gint errnum)
1249 gchar buf[64];
1250 gchar *msg;
1251 gchar *tofree;
1252 const gchar *ret;
1253 gint saved_errno = errno;
1255 msg = tofree = NULL;
1257 #ifdef HAVE_STRERROR
1258 msg = strerror (errnum);
1259 if (!g_get_charset (NULL))
1260 msg = tofree = g_locale_to_utf8 (msg, -1, NULL, NULL, NULL);
1261 #endif
1263 if (!msg)
1265 msg = buf;
1266 _g_sprintf (msg, "unknown error (%d)", errnum);
1269 ret = g_intern_string (msg);
1270 g_free (tofree);
1271 errno = saved_errno;
1272 return ret;
1276 * g_strsignal:
1277 * @signum: the signal number. See the <literal>signal</literal>
1278 * documentation
1280 * Returns a string describing the given signal, e.g. "Segmentation fault".
1281 * You should use this function in preference to strsignal(), because it
1282 * returns a string in UTF-8 encoding, and since not all platforms support
1283 * the strsignal() function.
1285 * Returns: a UTF-8 string describing the signal. If the signal is unknown,
1286 * it returns "unknown signal (&lt;signum&gt;)".
1288 const gchar *
1289 g_strsignal (gint signum)
1291 gchar *msg;
1292 gchar *tofree;
1293 const gchar *ret;
1295 msg = tofree = NULL;
1297 #ifdef HAVE_STRSIGNAL
1298 msg = strsignal (signum);
1299 if (!g_get_charset (NULL))
1300 msg = tofree = g_locale_to_utf8 (msg, -1, NULL, NULL, NULL);
1301 #endif
1303 if (!msg)
1304 msg = tofree = g_strdup_printf ("unknown signal (%d)", signum);
1305 ret = g_intern_string (msg);
1306 g_free (tofree);
1308 return ret;
1311 /* Functions g_strlcpy and g_strlcat were originally developed by
1312 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
1313 * See http://www.openbsd.org/cgi-bin/man.cgi?query=strlcpy
1314 * for more information.
1317 #ifdef HAVE_STRLCPY
1318 /* Use the native ones, if available; they might be implemented in assembly */
1319 gsize
1320 g_strlcpy (gchar *dest,
1321 const gchar *src,
1322 gsize dest_size)
1324 g_return_val_if_fail (dest != NULL, 0);
1325 g_return_val_if_fail (src != NULL, 0);
1327 return strlcpy (dest, src, dest_size);
1330 gsize
1331 g_strlcat (gchar *dest,
1332 const gchar *src,
1333 gsize dest_size)
1335 g_return_val_if_fail (dest != NULL, 0);
1336 g_return_val_if_fail (src != NULL, 0);
1338 return strlcat (dest, src, dest_size);
1341 #else /* ! HAVE_STRLCPY */
1343 * g_strlcpy:
1344 * @dest: destination buffer
1345 * @src: source buffer
1346 * @dest_size: length of @dest in bytes
1348 * Portability wrapper that calls strlcpy() on systems which have it,
1349 * and emulates strlcpy() otherwise. Copies @src to @dest; @dest is
1350 * guaranteed to be nul-terminated; @src must be nul-terminated;
1351 * @dest_size is the buffer size, not the number of chars to copy.
1353 * At most dest_size - 1 characters will be copied. Always nul-terminates
1354 * (unless dest_size == 0). This function does <emphasis>not</emphasis>
1355 * allocate memory. Unlike strncpy(), this function doesn't pad dest (so
1356 * it's often faster). It returns the size of the attempted result,
1357 * strlen (src), so if @retval >= @dest_size, truncation occurred.
1359 * <note><para>Caveat: strlcpy() is supposedly more secure than
1360 * strcpy() or strncpy(), but if you really want to avoid screwups,
1361 * g_strdup() is an even better idea.</para></note>
1363 * Returns: length of @src
1365 gsize
1366 g_strlcpy (gchar *dest,
1367 const gchar *src,
1368 gsize dest_size)
1370 register gchar *d = dest;
1371 register const gchar *s = src;
1372 register gsize n = dest_size;
1374 g_return_val_if_fail (dest != NULL, 0);
1375 g_return_val_if_fail (src != NULL, 0);
1377 /* Copy as many bytes as will fit */
1378 if (n != 0 && --n != 0)
1381 register gchar c = *s++;
1383 *d++ = c;
1384 if (c == 0)
1385 break;
1387 while (--n != 0);
1389 /* If not enough room in dest, add NUL and traverse rest of src */
1390 if (n == 0)
1392 if (dest_size != 0)
1393 *d = 0;
1394 while (*s++)
1398 return s - src - 1; /* count does not include NUL */
1402 * g_strlcat:
1403 * @dest: destination buffer, already containing one nul-terminated string
1404 * @src: source buffer
1405 * @dest_size: length of @dest buffer in bytes (not length of existing string
1406 * inside @dest)
1408 * Portability wrapper that calls strlcat() on systems which have it,
1409 * and emulates it otherwise. Appends nul-terminated @src string to @dest,
1410 * guaranteeing nul-termination for @dest. The total size of @dest won't
1411 * exceed @dest_size.
1413 * At most dest_size - 1 characters will be copied.
1414 * Unlike strncat, dest_size is the full size of dest, not the space left over.
1415 * This function does NOT allocate memory.
1416 * This always NUL terminates (unless siz == 0 or there were no NUL characters
1417 * in the dest_size characters of dest to start with).
1419 * <note><para>Caveat: this is supposedly a more secure alternative to
1420 * strcat() or strncat(), but for real security g_strconcat() is harder
1421 * to mess up.</para></note>
1423 * Returns: size of attempted result, which is MIN (dest_size, strlen
1424 * (original dest)) + strlen (src), so if retval >= dest_size,
1425 * truncation occurred.
1427 gsize
1428 g_strlcat (gchar *dest,
1429 const gchar *src,
1430 gsize dest_size)
1432 register gchar *d = dest;
1433 register const gchar *s = src;
1434 register gsize bytes_left = dest_size;
1435 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
1437 g_return_val_if_fail (dest != NULL, 0);
1438 g_return_val_if_fail (src != NULL, 0);
1440 /* Find the end of dst and adjust bytes left but don't go past end */
1441 while (*d != 0 && bytes_left-- != 0)
1442 d++;
1443 dlength = d - dest;
1444 bytes_left = dest_size - dlength;
1446 if (bytes_left == 0)
1447 return dlength + strlen (s);
1449 while (*s != 0)
1451 if (bytes_left != 1)
1453 *d++ = *s;
1454 bytes_left--;
1456 s++;
1458 *d = 0;
1460 return dlength + (s - src); /* count does not include NUL */
1462 #endif /* ! HAVE_STRLCPY */
1465 * g_ascii_strdown:
1466 * @str: a string.
1467 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1469 * Converts all upper case ASCII letters to lower case ASCII letters.
1471 * Return value: a newly-allocated string, with all the upper case
1472 * characters in @str converted to lower case, with
1473 * semantics that exactly match g_ascii_tolower(). (Note
1474 * that this is unlike the old g_strdown(), which modified
1475 * the string in place.)
1477 gchar*
1478 g_ascii_strdown (const gchar *str,
1479 gssize len)
1481 gchar *result, *s;
1483 g_return_val_if_fail (str != NULL, NULL);
1485 if (len < 0)
1486 len = strlen (str);
1488 result = g_strndup (str, len);
1489 for (s = result; *s; s++)
1490 *s = g_ascii_tolower (*s);
1492 return result;
1496 * g_ascii_strup:
1497 * @str: a string.
1498 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1500 * Converts all lower case ASCII letters to upper case ASCII letters.
1502 * Return value: a newly allocated string, with all the lower case
1503 * characters in @str converted to upper case, with
1504 * semantics that exactly match g_ascii_toupper(). (Note
1505 * that this is unlike the old g_strup(), which modified
1506 * the string in place.)
1508 gchar*
1509 g_ascii_strup (const gchar *str,
1510 gssize len)
1512 gchar *result, *s;
1514 g_return_val_if_fail (str != NULL, NULL);
1516 if (len < 0)
1517 len = strlen (str);
1519 result = g_strndup (str, len);
1520 for (s = result; *s; s++)
1521 *s = g_ascii_toupper (*s);
1523 return result;
1527 * g_strdown:
1528 * @string: the string to convert.
1530 * Converts a string to lower case.
1532 * Return value: the string
1534 * Deprecated:2.2: This function is totally broken for the reasons discussed
1535 * in the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown()
1536 * instead.
1538 gchar*
1539 g_strdown (gchar *string)
1541 register guchar *s;
1543 g_return_val_if_fail (string != NULL, NULL);
1545 s = (guchar *) string;
1547 while (*s)
1549 if (isupper (*s))
1550 *s = tolower (*s);
1551 s++;
1554 return (gchar *) string;
1558 * g_strup:
1559 * @string: the string to convert.
1561 * Converts a string to upper case.
1563 * Return value: the string
1565 * Deprecated:2.2: This function is totally broken for the reasons discussed
1566 * in the g_strncasecmp() docs - use g_ascii_strup() or g_utf8_strup() instead.
1568 gchar*
1569 g_strup (gchar *string)
1571 register guchar *s;
1573 g_return_val_if_fail (string != NULL, NULL);
1575 s = (guchar *) string;
1577 while (*s)
1579 if (islower (*s))
1580 *s = toupper (*s);
1581 s++;
1584 return (gchar *) string;
1588 * g_strreverse:
1589 * @string: the string to reverse
1591 * Reverses all of the bytes in a string. For example,
1592 * <literal>g_strreverse ("abcdef")</literal> will result
1593 * in "fedcba".
1595 * Note that g_strreverse() doesn't work on UTF-8 strings
1596 * containing multibyte characters. For that purpose, use
1597 * g_utf8_strreverse().
1599 * Returns: the same pointer passed in as @string
1601 gchar*
1602 g_strreverse (gchar *string)
1604 g_return_val_if_fail (string != NULL, NULL);
1606 if (*string)
1608 register gchar *h, *t;
1610 h = string;
1611 t = string + strlen (string) - 1;
1613 while (h < t)
1615 register gchar c;
1617 c = *h;
1618 *h = *t;
1619 h++;
1620 *t = c;
1621 t--;
1625 return string;
1629 * g_ascii_tolower:
1630 * @c: any character.
1632 * Convert a character to ASCII lower case.
1634 * Unlike the standard C library tolower() function, this only
1635 * recognizes standard ASCII letters and ignores the locale, returning
1636 * all non-ASCII characters unchanged, even if they are lower case
1637 * letters in a particular character set. Also unlike the standard
1638 * library function, this takes and returns a char, not an int, so
1639 * don't call it on <literal>EOF</literal> but no need to worry about casting to #guchar
1640 * before passing a possibly non-ASCII character in.
1642 * Return value: the result of converting @c to lower case.
1643 * If @c is not an ASCII upper case letter,
1644 * @c is returned unchanged.
1646 gchar
1647 g_ascii_tolower (gchar c)
1649 return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1653 * g_ascii_toupper:
1654 * @c: any character.
1656 * Convert a character to ASCII upper case.
1658 * Unlike the standard C library toupper() function, this only
1659 * recognizes standard ASCII letters and ignores the locale, returning
1660 * all non-ASCII characters unchanged, even if they are upper case
1661 * letters in a particular character set. Also unlike the standard
1662 * library function, this takes and returns a char, not an int, so
1663 * don't call it on <literal>EOF</literal> but no need to worry about casting to #guchar
1664 * before passing a possibly non-ASCII character in.
1666 * Return value: the result of converting @c to upper case.
1667 * If @c is not an ASCII lower case letter,
1668 * @c is returned unchanged.
1670 gchar
1671 g_ascii_toupper (gchar c)
1673 return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1677 * g_ascii_digit_value:
1678 * @c: an ASCII character.
1680 * Determines the numeric value of a character as a decimal
1681 * digit. Differs from g_unichar_digit_value() because it takes
1682 * a char, so there's no worry about sign extension if characters
1683 * are signed.
1685 * Return value: If @c is a decimal digit (according to
1686 * g_ascii_isdigit()), its numeric value. Otherwise, -1.
1689 g_ascii_digit_value (gchar c)
1691 if (g_ascii_isdigit (c))
1692 return c - '0';
1693 return -1;
1697 * g_ascii_xdigit_value:
1698 * @c: an ASCII character.
1700 * Determines the numeric value of a character as a hexidecimal
1701 * digit. Differs from g_unichar_xdigit_value() because it takes
1702 * a char, so there's no worry about sign extension if characters
1703 * are signed.
1705 * Return value: If @c is a hex digit (according to
1706 * g_ascii_isxdigit()), its numeric value. Otherwise, -1.
1709 g_ascii_xdigit_value (gchar c)
1711 if (c >= 'A' && c <= 'F')
1712 return c - 'A' + 10;
1713 if (c >= 'a' && c <= 'f')
1714 return c - 'a' + 10;
1715 return g_ascii_digit_value (c);
1719 * g_ascii_strcasecmp:
1720 * @s1: string to compare with @s2.
1721 * @s2: string to compare with @s1.
1723 * Compare two strings, ignoring the case of ASCII characters.
1725 * Unlike the BSD strcasecmp() function, this only recognizes standard
1726 * ASCII letters and ignores the locale, treating all non-ASCII
1727 * bytes as if they are not letters.
1729 * This function should be used only on strings that are known to be
1730 * in encodings where the bytes corresponding to ASCII letters always
1731 * represent themselves. This includes UTF-8 and the ISO-8859-*
1732 * charsets, but not for instance double-byte encodings like the
1733 * Windows Codepage 932, where the trailing bytes of double-byte
1734 * characters include all ASCII letters. If you compare two CP932
1735 * strings using this function, you will get false matches.
1737 * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2,
1738 * or a positive value if @s1 &gt; @s2.
1740 gint
1741 g_ascii_strcasecmp (const gchar *s1,
1742 const gchar *s2)
1744 gint c1, c2;
1746 g_return_val_if_fail (s1 != NULL, 0);
1747 g_return_val_if_fail (s2 != NULL, 0);
1749 while (*s1 && *s2)
1751 c1 = (gint)(guchar) TOLOWER (*s1);
1752 c2 = (gint)(guchar) TOLOWER (*s2);
1753 if (c1 != c2)
1754 return (c1 - c2);
1755 s1++; s2++;
1758 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1762 * g_ascii_strncasecmp:
1763 * @s1: string to compare with @s2.
1764 * @s2: string to compare with @s1.
1765 * @n: number of characters to compare.
1767 * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1768 * characters after the first @n in each string.
1770 * Unlike the BSD strcasecmp() function, this only recognizes standard
1771 * ASCII letters and ignores the locale, treating all non-ASCII
1772 * characters as if they are not letters.
1774 * The same warning as in g_ascii_strcasecmp() applies: Use this
1775 * function only on strings known to be in encodings where bytes
1776 * corresponding to ASCII letters always represent themselves.
1778 * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2,
1779 * or a positive value if @s1 &gt; @s2.
1781 gint
1782 g_ascii_strncasecmp (const gchar *s1,
1783 const gchar *s2,
1784 gsize n)
1786 gint c1, c2;
1788 g_return_val_if_fail (s1 != NULL, 0);
1789 g_return_val_if_fail (s2 != NULL, 0);
1791 while (n && *s1 && *s2)
1793 n -= 1;
1794 c1 = (gint)(guchar) TOLOWER (*s1);
1795 c2 = (gint)(guchar) TOLOWER (*s2);
1796 if (c1 != c2)
1797 return (c1 - c2);
1798 s1++; s2++;
1801 if (n)
1802 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1803 else
1804 return 0;
1808 * g_strcasecmp:
1809 * @s1: a string.
1810 * @s2: a string to compare with @s1.
1812 * A case-insensitive string comparison, corresponding to the standard
1813 * strcasecmp() function on platforms which support it.
1815 * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2,
1816 * or a positive value if @s1 &gt; @s2.
1818 * Deprecated:2.2: See g_strncasecmp() for a discussion of why this function
1819 * is deprecated and how to replace it.
1821 gint
1822 g_strcasecmp (const gchar *s1,
1823 const gchar *s2)
1825 #ifdef HAVE_STRCASECMP
1826 g_return_val_if_fail (s1 != NULL, 0);
1827 g_return_val_if_fail (s2 != NULL, 0);
1829 return strcasecmp (s1, s2);
1830 #else
1831 gint c1, c2;
1833 g_return_val_if_fail (s1 != NULL, 0);
1834 g_return_val_if_fail (s2 != NULL, 0);
1836 while (*s1 && *s2)
1838 /* According to A. Cox, some platforms have islower's that
1839 * don't work right on non-uppercase
1841 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1842 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1843 if (c1 != c2)
1844 return (c1 - c2);
1845 s1++; s2++;
1848 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1849 #endif
1853 * g_strncasecmp:
1854 * @s1: a string.
1855 * @s2: a string to compare with @s1.
1856 * @n: the maximum number of characters to compare.
1858 * A case-insensitive string comparison, corresponding to the standard
1859 * strncasecmp() function on platforms which support it.
1860 * It is similar to g_strcasecmp() except it only compares the first @n
1861 * characters of the strings.
1863 * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2,
1864 * or a positive value if @s1 &gt; @s2.
1866 * Deprecated:2.2: The problem with g_strncasecmp() is that it does the
1867 * comparison by calling toupper()/tolower(). These functions are
1868 * locale-specific and operate on single bytes. However, it is impossible
1869 * to handle things correctly from an I18N standpoint by operating on
1870 * bytes, since characters may be multibyte. Thus g_strncasecmp() is
1871 * broken if your string is guaranteed to be ASCII, since it's
1872 * locale-sensitive, and it's broken if your string is localized, since
1873 * it doesn't work on many encodings at all, including UTF-8, EUC-JP,
1874 * etc.
1876 * There are therefore two replacement techniques: g_ascii_strncasecmp(),
1877 * which only works on ASCII and is not locale-sensitive, and
1878 * g_utf8_casefold() followed by strcmp() on the resulting strings, which is
1879 * good for case-insensitive sorting of UTF-8.
1881 gint
1882 g_strncasecmp (const gchar *s1,
1883 const gchar *s2,
1884 guint n)
1886 #ifdef HAVE_STRNCASECMP
1887 return strncasecmp (s1, s2, n);
1888 #else
1889 gint c1, c2;
1891 g_return_val_if_fail (s1 != NULL, 0);
1892 g_return_val_if_fail (s2 != NULL, 0);
1894 while (n && *s1 && *s2)
1896 n -= 1;
1897 /* According to A. Cox, some platforms have islower's that
1898 * don't work right on non-uppercase
1900 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1901 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1902 if (c1 != c2)
1903 return (c1 - c2);
1904 s1++; s2++;
1907 if (n)
1908 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1909 else
1910 return 0;
1911 #endif
1915 * g_strdelimit:
1916 * @string: the string to convert
1917 * @delimiters: (allow-none): a string containing the current delimiters, or %NULL
1918 * to use the standard delimiters defined in #G_STR_DELIMITERS
1919 * @new_delimiter: the new delimiter character
1921 * Converts any delimiter characters in @string to @new_delimiter.
1922 * Any characters in @string which are found in @delimiters are
1923 * changed to the @new_delimiter character. Modifies @string in place,
1924 * and returns @string itself, not a copy. The return value is to
1925 * allow nesting such as
1926 * |[
1927 * g_ascii_strup (g_strdelimit (str, "abc", '?'))
1928 * ]|
1930 * Returns: @string
1932 gchar *
1933 g_strdelimit (gchar *string,
1934 const gchar *delimiters,
1935 gchar new_delim)
1937 register gchar *c;
1939 g_return_val_if_fail (string != NULL, NULL);
1941 if (!delimiters)
1942 delimiters = G_STR_DELIMITERS;
1944 for (c = string; *c; c++)
1946 if (strchr (delimiters, *c))
1947 *c = new_delim;
1950 return string;
1954 * g_strcanon:
1955 * @string: a nul-terminated array of bytes
1956 * @valid_chars: bytes permitted in @string
1957 * @substitutor: replacement character for disallowed bytes
1959 * For each character in @string, if the character is not in
1960 * @valid_chars, replaces the character with @substitutor.
1961 * Modifies @string in place, and return @string itself, not
1962 * a copy. The return value is to allow nesting such as
1963 * |[
1964 * g_ascii_strup (g_strcanon (str, "abc", '?'))
1965 * ]|
1967 * Returns: @string
1969 gchar *
1970 g_strcanon (gchar *string,
1971 const gchar *valid_chars,
1972 gchar substitutor)
1974 register gchar *c;
1976 g_return_val_if_fail (string != NULL, NULL);
1977 g_return_val_if_fail (valid_chars != NULL, NULL);
1979 for (c = string; *c; c++)
1981 if (!strchr (valid_chars, *c))
1982 *c = substitutor;
1985 return string;
1989 * g_strcompress:
1990 * @source: a string to compress
1992 * Replaces all escaped characters with their one byte equivalent.
1994 * This function does the reverse conversion of g_strescape().
1996 * Returns: a newly-allocated copy of @source with all escaped
1997 * character compressed
1999 gchar *
2000 g_strcompress (const gchar *source)
2002 const gchar *p = source, *octal;
2003 gchar *dest;
2004 gchar *q;
2006 g_return_val_if_fail (source != NULL, NULL);
2008 dest = g_malloc (strlen (source) + 1);
2009 q = dest;
2011 while (*p)
2013 if (*p == '\\')
2015 p++;
2016 switch (*p)
2018 case '\0':
2019 g_warning ("g_strcompress: trailing \\");
2020 goto out;
2021 case '0': case '1': case '2': case '3': case '4':
2022 case '5': case '6': case '7':
2023 *q = 0;
2024 octal = p;
2025 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
2027 *q = (*q * 8) + (*p - '0');
2028 p++;
2030 q++;
2031 p--;
2032 break;
2033 case 'b':
2034 *q++ = '\b';
2035 break;
2036 case 'f':
2037 *q++ = '\f';
2038 break;
2039 case 'n':
2040 *q++ = '\n';
2041 break;
2042 case 'r':
2043 *q++ = '\r';
2044 break;
2045 case 't':
2046 *q++ = '\t';
2047 break;
2048 case 'v':
2049 *q++ = '\v';
2050 break;
2051 default: /* Also handles \" and \\ */
2052 *q++ = *p;
2053 break;
2056 else
2057 *q++ = *p;
2058 p++;
2060 out:
2061 *q = 0;
2063 return dest;
2067 * g_strescape:
2068 * @source: a string to escape
2069 * @exceptions: a string of characters not to escape in @source
2071 * Escapes the special characters '\b', '\f', '\n', '\r', '\t', '\v', '\'
2072 * and '&quot;' in the string @source by inserting a '\' before
2073 * them. Additionally all characters in the range 0x01-0x1F (everything
2074 * below SPACE) and in the range 0x7F-0xFF (all non-ASCII chars) are
2075 * replaced with a '\' followed by their octal representation.
2076 * Characters supplied in @exceptions are not escaped.
2078 * g_strcompress() does the reverse conversion.
2080 * Returns: a newly-allocated copy of @source with certain
2081 * characters escaped. See above.
2083 gchar *
2084 g_strescape (const gchar *source,
2085 const gchar *exceptions)
2087 const guchar *p;
2088 gchar *dest;
2089 gchar *q;
2090 guchar excmap[256];
2092 g_return_val_if_fail (source != NULL, NULL);
2094 p = (guchar *) source;
2095 /* Each source byte needs maximally four destination chars (\777) */
2096 q = dest = g_malloc (strlen (source) * 4 + 1);
2098 memset (excmap, 0, 256);
2099 if (exceptions)
2101 guchar *e = (guchar *) exceptions;
2103 while (*e)
2105 excmap[*e] = 1;
2106 e++;
2110 while (*p)
2112 if (excmap[*p])
2113 *q++ = *p;
2114 else
2116 switch (*p)
2118 case '\b':
2119 *q++ = '\\';
2120 *q++ = 'b';
2121 break;
2122 case '\f':
2123 *q++ = '\\';
2124 *q++ = 'f';
2125 break;
2126 case '\n':
2127 *q++ = '\\';
2128 *q++ = 'n';
2129 break;
2130 case '\r':
2131 *q++ = '\\';
2132 *q++ = 'r';
2133 break;
2134 case '\t':
2135 *q++ = '\\';
2136 *q++ = 't';
2137 break;
2138 case '\v':
2139 *q++ = '\\';
2140 *q++ = 'v';
2141 break;
2142 case '\\':
2143 *q++ = '\\';
2144 *q++ = '\\';
2145 break;
2146 case '"':
2147 *q++ = '\\';
2148 *q++ = '"';
2149 break;
2150 default:
2151 if ((*p < ' ') || (*p >= 0177))
2153 *q++ = '\\';
2154 *q++ = '0' + (((*p) >> 6) & 07);
2155 *q++ = '0' + (((*p) >> 3) & 07);
2156 *q++ = '0' + ((*p) & 07);
2158 else
2159 *q++ = *p;
2160 break;
2163 p++;
2165 *q = 0;
2166 return dest;
2170 * g_strchug:
2171 * @string: a string to remove the leading whitespace from
2173 * Removes leading whitespace from a string, by moving the rest
2174 * of the characters forward.
2176 * This function doesn't allocate or reallocate any memory;
2177 * it modifies @string in place. The pointer to @string is
2178 * returned to allow the nesting of functions.
2180 * Also see g_strchomp() and g_strstrip().
2182 * Returns: @string
2184 gchar *
2185 g_strchug (gchar *string)
2187 guchar *start;
2189 g_return_val_if_fail (string != NULL, NULL);
2191 for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
2194 g_memmove (string, start, strlen ((gchar *) start) + 1);
2196 return string;
2200 * g_strchomp:
2201 * @string: a string to remove the trailing whitespace from
2203 * Removes trailing whitespace from a string.
2205 * This function doesn't allocate or reallocate any memory;
2206 * it modifies @string in place. The pointer to @string is
2207 * returned to allow the nesting of functions.
2209 * Also see g_strchug() and g_strstrip().
2211 * Returns: @string.
2213 gchar *
2214 g_strchomp (gchar *string)
2216 gsize len;
2218 g_return_val_if_fail (string != NULL, NULL);
2220 len = strlen (string);
2221 while (len--)
2223 if (g_ascii_isspace ((guchar) string[len]))
2224 string[len] = '\0';
2225 else
2226 break;
2229 return string;
2233 * g_strsplit:
2234 * @string: a string to split
2235 * @delimiter: a string which specifies the places at which to split
2236 * the string. The delimiter is not included in any of the resulting
2237 * strings, unless @max_tokens is reached.
2238 * @max_tokens: the maximum number of pieces to split @string into.
2239 * If this is less than 1, the string is split completely.
2241 * Splits a string into a maximum of @max_tokens pieces, using the given
2242 * @delimiter. If @max_tokens is reached, the remainder of @string is
2243 * appended to the last token.
2245 * As a special case, the result of splitting the empty string "" is an empty
2246 * vector, not a vector containing a single string. The reason for this
2247 * special case is that being able to represent a empty vector is typically
2248 * more useful than consistent handling of empty elements. If you do need
2249 * to represent empty elements, you'll need to check for the empty string
2250 * before calling g_strsplit().
2252 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2253 * g_strfreev() to free it.
2255 gchar**
2256 g_strsplit (const gchar *string,
2257 const gchar *delimiter,
2258 gint max_tokens)
2260 GSList *string_list = NULL, *slist;
2261 gchar **str_array, *s;
2262 guint n = 0;
2263 const gchar *remainder;
2265 g_return_val_if_fail (string != NULL, NULL);
2266 g_return_val_if_fail (delimiter != NULL, NULL);
2267 g_return_val_if_fail (delimiter[0] != '\0', NULL);
2269 if (max_tokens < 1)
2270 max_tokens = G_MAXINT;
2272 remainder = string;
2273 s = strstr (remainder, delimiter);
2274 if (s)
2276 gsize delimiter_len = strlen (delimiter);
2278 while (--max_tokens && s)
2280 gsize len;
2282 len = s - remainder;
2283 string_list = g_slist_prepend (string_list,
2284 g_strndup (remainder, len));
2285 n++;
2286 remainder = s + delimiter_len;
2287 s = strstr (remainder, delimiter);
2290 if (*string)
2292 n++;
2293 string_list = g_slist_prepend (string_list, g_strdup (remainder));
2296 str_array = g_new (gchar*, n + 1);
2298 str_array[n--] = NULL;
2299 for (slist = string_list; slist; slist = slist->next)
2300 str_array[n--] = slist->data;
2302 g_slist_free (string_list);
2304 return str_array;
2308 * g_strsplit_set:
2309 * @string: The string to be tokenized
2310 * @delimiters: A nul-terminated string containing bytes that are used
2311 * to split the string.
2312 * @max_tokens: The maximum number of tokens to split @string into.
2313 * If this is less than 1, the string is split completely
2315 * Splits @string into a number of tokens not containing any of the characters
2316 * in @delimiter. A token is the (possibly empty) longest string that does not
2317 * contain any of the characters in @delimiters. If @max_tokens is reached, the
2318 * remainder is appended to the last token.
2320 * For example the result of g_strsplit_set ("abc:def/ghi", ":/", -1) is a
2321 * %NULL-terminated vector containing the three strings "abc", "def",
2322 * and "ghi".
2324 * The result if g_strsplit_set (":def/ghi:", ":/", -1) is a %NULL-terminated
2325 * vector containing the four strings "", "def", "ghi", and "".
2327 * As a special case, the result of splitting the empty string "" is an empty
2328 * vector, not a vector containing a single string. The reason for this
2329 * special case is that being able to represent a empty vector is typically
2330 * more useful than consistent handling of empty elements. If you do need
2331 * to represent empty elements, you'll need to check for the empty string
2332 * before calling g_strsplit_set().
2334 * Note that this function works on bytes not characters, so it can't be used
2335 * to delimit UTF-8 strings for anything but ASCII characters.
2337 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2338 * g_strfreev() to free it.
2340 * Since: 2.4
2342 gchar **
2343 g_strsplit_set (const gchar *string,
2344 const gchar *delimiters,
2345 gint max_tokens)
2347 gboolean delim_table[256];
2348 GSList *tokens, *list;
2349 gint n_tokens;
2350 const gchar *s;
2351 const gchar *current;
2352 gchar *token;
2353 gchar **result;
2355 g_return_val_if_fail (string != NULL, NULL);
2356 g_return_val_if_fail (delimiters != NULL, NULL);
2358 if (max_tokens < 1)
2359 max_tokens = G_MAXINT;
2361 if (*string == '\0')
2363 result = g_new (char *, 1);
2364 result[0] = NULL;
2365 return result;
2368 memset (delim_table, FALSE, sizeof (delim_table));
2369 for (s = delimiters; *s != '\0'; ++s)
2370 delim_table[*(guchar *)s] = TRUE;
2372 tokens = NULL;
2373 n_tokens = 0;
2375 s = current = string;
2376 while (*s != '\0')
2378 if (delim_table[*(guchar *)s] && n_tokens + 1 < max_tokens)
2380 token = g_strndup (current, s - current);
2381 tokens = g_slist_prepend (tokens, token);
2382 ++n_tokens;
2384 current = s + 1;
2387 ++s;
2390 token = g_strndup (current, s - current);
2391 tokens = g_slist_prepend (tokens, token);
2392 ++n_tokens;
2394 result = g_new (gchar *, n_tokens + 1);
2396 result[n_tokens] = NULL;
2397 for (list = tokens; list != NULL; list = list->next)
2398 result[--n_tokens] = list->data;
2400 g_slist_free (tokens);
2402 return result;
2406 * g_strfreev:
2407 * @str_array: a %NULL-terminated array of strings to free
2409 * Frees a %NULL-terminated array of strings, and the array itself.
2410 * If called on a %NULL value, g_strfreev() simply returns.
2412 void
2413 g_strfreev (gchar **str_array)
2415 if (str_array)
2417 int i;
2419 for (i = 0; str_array[i] != NULL; i++)
2420 g_free (str_array[i]);
2422 g_free (str_array);
2427 * g_strdupv:
2428 * @str_array: a %NULL-terminated array of strings
2430 * Copies %NULL-terminated array of strings. The copy is a deep copy;
2431 * the new array should be freed by first freeing each string, then
2432 * the array itself. g_strfreev() does this for you. If called
2433 * on a %NULL value, g_strdupv() simply returns %NULL.
2435 * Return value: a new %NULL-terminated array of strings.
2437 gchar**
2438 g_strdupv (gchar **str_array)
2440 if (str_array)
2442 gint i;
2443 gchar **retval;
2445 i = 0;
2446 while (str_array[i])
2447 ++i;
2449 retval = g_new (gchar*, i + 1);
2451 i = 0;
2452 while (str_array[i])
2454 retval[i] = g_strdup (str_array[i]);
2455 ++i;
2457 retval[i] = NULL;
2459 return retval;
2461 else
2462 return NULL;
2466 * g_strjoinv:
2467 * @separator: (allow-none): a string to insert between each of the strings, or %NULL
2468 * @str_array: a %NULL-terminated array of strings to join
2470 * Joins a number of strings together to form one long string, with the
2471 * optional @separator inserted between each of them. The returned string
2472 * should be freed with g_free().
2474 * Returns: a newly-allocated string containing all of the strings joined
2475 * together, with @separator between them
2477 gchar*
2478 g_strjoinv (const gchar *separator,
2479 gchar **str_array)
2481 gchar *string;
2482 gchar *ptr;
2484 g_return_val_if_fail (str_array != NULL, NULL);
2486 if (separator == NULL)
2487 separator = "";
2489 if (*str_array)
2491 gint i;
2492 gsize len;
2493 gsize separator_len;
2495 separator_len = strlen (separator);
2496 /* First part, getting length */
2497 len = 1 + strlen (str_array[0]);
2498 for (i = 1; str_array[i] != NULL; i++)
2499 len += strlen (str_array[i]);
2500 len += separator_len * (i - 1);
2502 /* Second part, building string */
2503 string = g_new (gchar, len);
2504 ptr = g_stpcpy (string, *str_array);
2505 for (i = 1; str_array[i] != NULL; i++)
2507 ptr = g_stpcpy (ptr, separator);
2508 ptr = g_stpcpy (ptr, str_array[i]);
2511 else
2512 string = g_strdup ("");
2514 return string;
2518 * g_strjoin:
2519 * @separator: (allow-none): a string to insert between each of the strings, or %NULL
2520 * @...: a %NULL-terminated list of strings to join
2522 * Joins a number of strings together to form one long string, with the
2523 * optional @separator inserted between each of them. The returned string
2524 * should be freed with g_free().
2526 * Returns: a newly-allocated string containing all of the strings joined
2527 * together, with @separator between them
2529 gchar*
2530 g_strjoin (const gchar *separator,
2531 ...)
2533 gchar *string, *s;
2534 va_list args;
2535 gsize len;
2536 gsize separator_len;
2537 gchar *ptr;
2539 if (separator == NULL)
2540 separator = "";
2542 separator_len = strlen (separator);
2544 va_start (args, separator);
2546 s = va_arg (args, gchar*);
2548 if (s)
2550 /* First part, getting length */
2551 len = 1 + strlen (s);
2553 s = va_arg (args, gchar*);
2554 while (s)
2556 len += separator_len + strlen (s);
2557 s = va_arg (args, gchar*);
2559 va_end (args);
2561 /* Second part, building string */
2562 string = g_new (gchar, len);
2564 va_start (args, separator);
2566 s = va_arg (args, gchar*);
2567 ptr = g_stpcpy (string, s);
2569 s = va_arg (args, gchar*);
2570 while (s)
2572 ptr = g_stpcpy (ptr, separator);
2573 ptr = g_stpcpy (ptr, s);
2574 s = va_arg (args, gchar*);
2577 else
2578 string = g_strdup ("");
2580 va_end (args);
2582 return string;
2587 * g_strstr_len:
2588 * @haystack: a string
2589 * @haystack_len: the maximum length of @haystack. Note that -1 is
2590 * a valid length, if @haystack is nul-terminated, meaning it will
2591 * search through the whole string.
2592 * @needle: the string to search for
2594 * Searches the string @haystack for the first occurrence
2595 * of the string @needle, limiting the length of the search
2596 * to @haystack_len.
2598 * Return value: a pointer to the found occurrence, or
2599 * %NULL if not found.
2601 gchar *
2602 g_strstr_len (const gchar *haystack,
2603 gssize haystack_len,
2604 const gchar *needle)
2606 g_return_val_if_fail (haystack != NULL, NULL);
2607 g_return_val_if_fail (needle != NULL, NULL);
2609 if (haystack_len < 0)
2610 return strstr (haystack, needle);
2611 else
2613 const gchar *p = haystack;
2614 gsize needle_len = strlen (needle);
2615 const gchar *end;
2616 gsize i;
2618 if (needle_len == 0)
2619 return (gchar *)haystack;
2621 if (haystack_len < needle_len)
2622 return NULL;
2624 end = haystack + haystack_len - needle_len;
2626 while (p <= end && *p)
2628 for (i = 0; i < needle_len; i++)
2629 if (p[i] != needle[i])
2630 goto next;
2632 return (gchar *)p;
2634 next:
2635 p++;
2638 return NULL;
2643 * g_strrstr:
2644 * @haystack: a nul-terminated string
2645 * @needle: the nul-terminated string to search for
2647 * Searches the string @haystack for the last occurrence
2648 * of the string @needle.
2650 * Return value: a pointer to the found occurrence, or
2651 * %NULL if not found.
2653 gchar *
2654 g_strrstr (const gchar *haystack,
2655 const gchar *needle)
2657 gsize i;
2658 gsize needle_len;
2659 gsize haystack_len;
2660 const gchar *p;
2662 g_return_val_if_fail (haystack != NULL, NULL);
2663 g_return_val_if_fail (needle != NULL, NULL);
2665 needle_len = strlen (needle);
2666 haystack_len = strlen (haystack);
2668 if (needle_len == 0)
2669 return (gchar *)haystack;
2671 if (haystack_len < needle_len)
2672 return NULL;
2674 p = haystack + haystack_len - needle_len;
2676 while (p >= haystack)
2678 for (i = 0; i < needle_len; i++)
2679 if (p[i] != needle[i])
2680 goto next;
2682 return (gchar *)p;
2684 next:
2685 p--;
2688 return NULL;
2692 * g_strrstr_len:
2693 * @haystack: a nul-terminated string
2694 * @haystack_len: the maximum length of @haystack
2695 * @needle: the nul-terminated string to search for
2697 * Searches the string @haystack for the last occurrence
2698 * of the string @needle, limiting the length of the search
2699 * to @haystack_len.
2701 * Return value: a pointer to the found occurrence, or
2702 * %NULL if not found.
2704 gchar *
2705 g_strrstr_len (const gchar *haystack,
2706 gssize haystack_len,
2707 const gchar *needle)
2709 g_return_val_if_fail (haystack != NULL, NULL);
2710 g_return_val_if_fail (needle != NULL, NULL);
2712 if (haystack_len < 0)
2713 return g_strrstr (haystack, needle);
2714 else
2716 gsize needle_len = strlen (needle);
2717 const gchar *haystack_max = haystack + haystack_len;
2718 const gchar *p = haystack;
2719 gsize i;
2721 while (p < haystack_max && *p)
2722 p++;
2724 if (p < haystack + needle_len)
2725 return NULL;
2727 p -= needle_len;
2729 while (p >= haystack)
2731 for (i = 0; i < needle_len; i++)
2732 if (p[i] != needle[i])
2733 goto next;
2735 return (gchar *)p;
2737 next:
2738 p--;
2741 return NULL;
2747 * g_str_has_suffix:
2748 * @str: a nul-terminated string
2749 * @suffix: the nul-terminated suffix to look for
2751 * Looks whether the string @str ends with @suffix.
2753 * Return value: %TRUE if @str end with @suffix, %FALSE otherwise.
2755 * Since: 2.2
2757 gboolean
2758 g_str_has_suffix (const gchar *str,
2759 const gchar *suffix)
2761 int str_len;
2762 int suffix_len;
2764 g_return_val_if_fail (str != NULL, FALSE);
2765 g_return_val_if_fail (suffix != NULL, FALSE);
2767 str_len = strlen (str);
2768 suffix_len = strlen (suffix);
2770 if (str_len < suffix_len)
2771 return FALSE;
2773 return strcmp (str + str_len - suffix_len, suffix) == 0;
2777 * g_str_has_prefix:
2778 * @str: a nul-terminated string
2779 * @prefix: the nul-terminated prefix to look for
2781 * Looks whether the string @str begins with @prefix.
2783 * Return value: %TRUE if @str begins with @prefix, %FALSE otherwise.
2785 * Since: 2.2
2787 gboolean
2788 g_str_has_prefix (const gchar *str,
2789 const gchar *prefix)
2791 int str_len;
2792 int prefix_len;
2794 g_return_val_if_fail (str != NULL, FALSE);
2795 g_return_val_if_fail (prefix != NULL, FALSE);
2797 str_len = strlen (str);
2798 prefix_len = strlen (prefix);
2800 if (str_len < prefix_len)
2801 return FALSE;
2803 return strncmp (str, prefix, prefix_len) == 0;
2807 * g_strv_length:
2808 * @str_array: a %NULL-terminated array of strings
2810 * Returns the length of the given %NULL-terminated
2811 * string array @str_array.
2813 * Return value: length of @str_array.
2815 * Since: 2.6
2817 guint
2818 g_strv_length (gchar **str_array)
2820 guint i = 0;
2822 g_return_val_if_fail (str_array != NULL, 0);
2824 while (str_array[i])
2825 ++i;
2827 return i;