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/.
33 #define _GNU_SOURCE /* For stpcpy */
41 #include <ctype.h> /* For tolower() */
42 #if !defined (HAVE_STRSIGNAL) || !defined(NO_SYS_SIGLIST_DECL)
48 #include "gprintfint.h"
54 /* do not include <unistd.h> in this place since it
55 * interferes with g_strsignal() on some OSes
58 static const guint16 ascii_table_data[256] = {
59 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
60 0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004,
61 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
62 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
63 0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
64 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
65 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
66 0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
67 0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
68 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
69 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
70 0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
71 0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
72 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
73 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
74 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004
75 /* the upper 128 are all zeroes */
78 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
81 const guint16 * const g_ascii_table = ascii_table_data;
84 g_strdup (const gchar *str)
91 length = strlen (str) + 1;
92 new_str = g_new (char, length);
93 memcpy (new_str, str, length);
102 g_memdup (gconstpointer mem,
109 new_mem = g_malloc (byte_size);
110 memcpy (new_mem, mem, byte_size);
119 g_strndup (const gchar *str,
126 new_str = g_new (gchar, n + 1);
127 strncpy (new_str, str, n);
137 g_strnfill (gsize length,
142 str = g_new (gchar, length + 1);
143 memset (str, (guchar)fill_char, length);
151 * @dest: destination buffer.
152 * @src: source string.
154 * Copies a nul-terminated string into the dest buffer, include the
155 * trailing nul, and return a pointer to the trailing nul byte.
156 * This is useful for concatenating multiple strings together
157 * without having to repeatedly scan for the end.
159 * Return value: a pointer to trailing nul byte.
162 g_stpcpy (gchar *dest,
166 g_return_val_if_fail (dest != NULL, NULL);
167 g_return_val_if_fail (src != NULL, NULL);
168 return stpcpy (dest, src);
170 register gchar *d = dest;
171 register const gchar *s = src;
173 g_return_val_if_fail (dest != NULL, NULL);
174 g_return_val_if_fail (src != NULL, NULL);
177 while (*s++ != '\0');
184 g_strdup_vprintf (const gchar *format,
187 gchar *string = NULL;
189 g_vasprintf (&string, format, args);
195 g_strdup_printf (const gchar *format,
201 va_start (args, format);
202 buffer = g_strdup_vprintf (format, args);
209 g_strconcat (const gchar *string1, ...)
217 g_return_val_if_fail (string1 != NULL, NULL);
219 l = 1 + strlen (string1);
220 va_start (args, string1);
221 s = va_arg (args, gchar*);
225 s = va_arg (args, gchar*);
229 concat = g_new (gchar, l);
232 ptr = g_stpcpy (ptr, string1);
233 va_start (args, string1);
234 s = va_arg (args, gchar*);
237 ptr = g_stpcpy (ptr, s);
238 s = va_arg (args, gchar*);
247 * @nptr: the string to convert to a numeric value.
248 * @endptr: if non-%NULL, it returns the character after
249 * the last character used in the conversion.
251 * Converts a string to a #gdouble value.
252 * It calls the standard strtod() function to handle the conversion, but
253 * if the string is not completely converted it attempts the conversion
254 * again with g_ascii_strtod(), and returns the best match.
256 * This function should seldomly be used. The normal situation when reading
257 * numbers not for human consumption is to use g_ascii_strtod(). Only when
258 * you know that you must expect both locale formatted and C formatted numbers
259 * should you use this. Make sure that you don't pass strings such as comma
260 * separated lists of values, since the commas may be interpreted as a decimal
261 * point in some locales, causing unexpected results.
263 * Return value: the #gdouble value.
266 g_strtod (const gchar *nptr,
274 g_return_val_if_fail (nptr != NULL, 0);
279 val_1 = strtod (nptr, &fail_pos_1);
281 if (fail_pos_1 && fail_pos_1[0] != 0)
282 val_2 = g_ascii_strtod (nptr, &fail_pos_2);
284 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
287 *endptr = fail_pos_1;
293 *endptr = fail_pos_2;
300 * @nptr: the string to convert to a numeric value.
301 * @endptr: if non-%NULL, it returns the character after
302 * the last character used in the conversion.
304 * Converts a string to a #gdouble value.
305 * This function behaves like the standard strtod() function
306 * does in the C locale. It does this without actually
307 * changing the current locale, since that would not be
310 * This function is typically used when reading configuration
311 * files or other non-user input that should be locale independent.
312 * To handle input from the user you should normally use the
313 * locale-sensitive system strtod() function.
315 * To convert from a #gdouble to a string in a locale-insensitive
316 * way, use g_ascii_dtostr().
318 * If the correct value would cause overflow, plus or minus %HUGE_VAL
319 * is returned (according to the sign of the value), and %ERANGE is
320 * stored in %errno. If the correct value would cause underflow,
321 * zero is returned and %ERANGE is stored in %errno.
323 * This function resets %errno before calling strtod() so that
324 * you can reliably detect overflow and underflow.
326 * Return value: the #gdouble value.
329 g_ascii_strtod (const gchar *nptr,
334 struct lconv *locale_data;
335 const char *decimal_point;
336 int decimal_point_len;
337 const char *p, *decimal_point_pos;
338 const char *end = NULL; /* Silence gcc */
340 g_return_val_if_fail (nptr != NULL, 0);
344 locale_data = localeconv ();
345 decimal_point = locale_data->decimal_point;
346 decimal_point_len = strlen (decimal_point);
348 g_assert (decimal_point_len != 0);
350 decimal_point_pos = NULL;
351 if (decimal_point[0] != '.' ||
352 decimal_point[1] != 0)
355 /* Skip leading space */
356 while (g_ascii_isspace (*p))
359 /* Skip leading optional sign */
360 if (*p == '+' || *p == '-')
364 (p[1] == 'x' || p[1] == 'X'))
367 /* HEX - find the (optional) decimal point */
369 while (g_ascii_isxdigit (*p))
374 decimal_point_pos = p++;
376 while (g_ascii_isxdigit (*p))
379 if (*p == 'p' || *p == 'P')
381 if (*p == '+' || *p == '-')
383 while (g_ascii_isdigit (*p))
390 while (g_ascii_isdigit (*p))
395 decimal_point_pos = p++;
397 while (g_ascii_isdigit (*p))
400 if (*p == 'e' || *p == 'E')
402 if (*p == '+' || *p == '-')
404 while (g_ascii_isdigit (*p))
409 /* For the other cases, we need not convert the decimal point */
412 /* Set errno to zero, so that we can distinguish zero results
416 if (decimal_point_pos)
420 /* We need to convert the '.' to the locale specific decimal point */
421 copy = g_malloc (end - nptr + 1 + decimal_point_len);
424 memcpy (c, nptr, decimal_point_pos - nptr);
425 c += decimal_point_pos - nptr;
426 memcpy (c, decimal_point, decimal_point_len);
427 c += decimal_point_len;
428 memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
429 c += end - (decimal_point_pos + 1);
432 val = strtod (copy, &fail_pos);
436 if (fail_pos > decimal_point_pos)
437 fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
439 fail_pos = (char *)nptr + (fail_pos - copy);
446 val = strtod (nptr, &fail_pos);
457 * @buffer: A buffer to place the resulting string in
458 * @buf_len: The length of the buffer.
459 * @d: The #gdouble to convert
461 * Converts a #gdouble to a string, using the '.' as
464 * This functions generates enough precision that converting
465 * the string back using g_ascii_strtod() gives the same machine-number
466 * (on machines with IEEE compatible 64bit doubles). It is
467 * guaranteed that the size of the resulting string will never
468 * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes.
470 * Return value: The pointer to the buffer with the converted string.
473 g_ascii_dtostr (gchar *buffer,
477 return g_ascii_formatd (buffer, buf_len, "%.17g", d);
482 * @buffer: A buffer to place the resulting string in
483 * @buf_len: The length of the buffer.
484 * @format: The printf()-style format to use for the
485 * code to use for converting.
486 * @d: The #gdouble to convert
488 * Converts a #gdouble to a string, using the '.' as
489 * decimal point. To format the number you pass in
490 * a printf()-style format string. Allowed conversion
491 * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'.
493 * If you just want to want to serialize the value into a
494 * string, use g_ascii_dtostr().
496 * Return value: The pointer to the buffer with the converted string.
499 g_ascii_formatd (gchar *buffer,
504 struct lconv *locale_data;
505 const char *decimal_point;
506 int decimal_point_len;
511 g_return_val_if_fail (buffer != NULL, NULL);
512 g_return_val_if_fail (format[0] == '%', NULL);
513 g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
515 format_char = format[strlen (format) - 1];
517 g_return_val_if_fail (format_char == 'e' || format_char == 'E' ||
518 format_char == 'f' || format_char == 'F' ||
519 format_char == 'g' || format_char == 'G',
522 if (format[0] != '%')
525 if (strpbrk (format + 1, "'l%"))
528 if (!(format_char == 'e' || format_char == 'E' ||
529 format_char == 'f' || format_char == 'F' ||
530 format_char == 'g' || format_char == 'G'))
534 _g_snprintf (buffer, buf_len, format, d);
536 locale_data = localeconv ();
537 decimal_point = locale_data->decimal_point;
538 decimal_point_len = strlen (decimal_point);
540 g_assert (decimal_point_len != 0);
542 if (decimal_point[0] != '.' ||
543 decimal_point[1] != 0)
547 if (*p == '+' || *p == '-')
550 while (isdigit ((guchar)*p))
553 if (strncmp (p, decimal_point, decimal_point_len) == 0)
557 if (decimal_point_len > 1) {
558 rest_len = strlen (p + (decimal_point_len-1));
559 memmove (p, p + (decimal_point_len-1),
572 * @nptr: the string to convert to a numeric value.
573 * @endptr: if non-%NULL, it returns the character after
574 * the last character used in the conversion.
575 * @base: to be used for the conversion, 2..36 or 0
577 * Converts a string to a #guint64 value.
578 * This function behaves like the standard strtoull() function
579 * does in the C locale. It does this without actually
580 * changing the current locale, since that would not be
583 * This function is typically used when reading configuration
584 * files or other non-user input that should be locale independent.
585 * To handle input from the user you should normally use the
586 * locale-sensitive system strtoull() function.
588 * If the correct value would cause overflow, %G_MAXUINT64
589 * is returned, and %ERANGE is stored in %errno.
591 * Return value: the #guint64 value.
596 g_ascii_strtoull (const gchar *nptr,
600 /* this code is based on on the strtol(3) code from GNU libc released under
601 * the GNU Lesser General Public License.
603 * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
604 * Free Software Foundation, Inc.
606 #define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
607 (c) == '\r' || (c) == '\t' || (c) == '\v')
608 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
609 #define ISLOWER(c) ((c) >= 'a' && (c) <= 'z')
610 #define ISALPHA(c) (ISUPPER (c) || ISLOWER (c))
611 #define TOUPPER(c) (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
612 #define TOLOWER(c) (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
613 gboolean negative, overflow;
617 const gchar *s, *save;
620 g_return_val_if_fail (nptr != NULL, 0);
622 if (base == 1 || base > 36)
630 /* Skip white space. */
636 /* Check for a sign. */
646 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
649 if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
660 /* Save the pointer so we can check later if anything happened. */
662 cutoff = G_MAXUINT64 / base;
663 cutlim = G_MAXUINT64 % base;
670 if (c >= '0' && c <= '9')
672 else if (ISALPHA (c))
673 c = TOUPPER (c) - 'A' + 10;
678 /* Check for overflow. */
679 if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
688 /* Check if anything actually happened. */
692 /* Store in ENDPTR the address of one character
693 past the last character we converted. */
695 *endptr = (gchar*) s;
703 /* Return the result of the appropriate sign. */
704 return negative ? -ui64 : ui64;
707 /* We must handle a special case here: the base is 0 or 16 and the
708 first two characters are '0' and 'x', but the rest are no
709 hexadecimal digits. This is no error case. We return 0 and
710 ENDPTR points to the `x`. */
713 if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
715 *endptr = (gchar*) &save[-1];
717 /* There was no number to convert. */
718 *endptr = (gchar*) nptr;
724 G_CONST_RETURN gchar*
725 g_strerror (gint errnum)
727 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
731 const char *msg_locale;
733 msg_locale = strerror (errnum);
734 if (g_get_charset (NULL))
738 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
741 /* Stick in the quark table so that we can return a static result
743 GQuark msg_quark = g_quark_from_string (msg_utf8);
746 return g_quark_to_string (msg_quark);
753 case E2BIG: return "argument list too long";
756 case EACCES: return "permission denied";
759 case EADDRINUSE: return "address already in use";
762 case EADDRNOTAVAIL: return "can't assign requested address";
765 case EADV: return "advertise error";
768 case EAFNOSUPPORT: return "address family not supported by protocol family";
771 case EAGAIN: return "try again";
774 case EALIGN: return "EALIGN";
777 case EALREADY: return "operation already in progress";
780 case EBADE: return "bad exchange descriptor";
783 case EBADF: return "bad file number";
786 case EBADFD: return "file descriptor in bad state";
789 case EBADMSG: return "not a data message";
792 case EBADR: return "bad request descriptor";
795 case EBADRPC: return "RPC structure is bad";
798 case EBADRQC: return "bad request code";
801 case EBADSLT: return "invalid slot";
804 case EBFONT: return "bad font file format";
807 case EBUSY: return "mount device busy";
810 case ECHILD: return "no children";
813 case ECHRNG: return "channel number out of range";
816 case ECOMM: return "communication error on send";
819 case ECONNABORTED: return "software caused connection abort";
822 case ECONNREFUSED: return "connection refused";
825 case ECONNRESET: return "connection reset by peer";
827 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
828 case EDEADLK: return "resource deadlock avoided";
831 case EDEADLOCK: return "resource deadlock avoided";
834 case EDESTADDRREQ: return "destination address required";
837 case EDIRTY: return "mounting a dirty fs w/o force";
840 case EDOM: return "math argument out of range";
843 case EDOTDOT: return "cross mount point";
846 case EDQUOT: return "disk quota exceeded";
849 case EDUPPKG: return "duplicate package name";
852 case EEXIST: return "file already exists";
855 case EFAULT: return "bad address in system call argument";
858 case EFBIG: return "file too large";
861 case EHOSTDOWN: return "host is down";
864 case EHOSTUNREACH: return "host is unreachable";
867 case EIDRM: return "identifier removed";
870 case EINIT: return "initialization error";
873 case EINPROGRESS: return "operation now in progress";
876 case EINTR: return "interrupted system call";
879 case EINVAL: return "invalid argument";
882 case EIO: return "I/O error";
885 case EISCONN: return "socket is already connected";
888 case EISDIR: return "is a directory";
891 case EISNAM: return "is a name file";
894 case ELBIN: return "ELBIN";
897 case EL2HLT: return "level 2 halted";
900 case EL2NSYNC: return "level 2 not synchronized";
903 case EL3HLT: return "level 3 halted";
906 case EL3RST: return "level 3 reset";
909 case ELIBACC: return "can not access a needed shared library";
912 case ELIBBAD: return "accessing a corrupted shared library";
915 case ELIBEXEC: return "can not exec a shared library directly";
918 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
921 case ELIBSCN: return ".lib section in a.out corrupted";
924 case ELNRNG: return "link number out of range";
927 case ELOOP: return "too many levels of symbolic links";
930 case EMFILE: return "too many open files";
933 case EMLINK: return "too many links";
936 case EMSGSIZE: return "message too long";
939 case EMULTIHOP: return "multihop attempted";
942 case ENAMETOOLONG: return "file name too long";
945 case ENAVAIL: return "not available";
948 case ENET: return "ENET";
951 case ENETDOWN: return "network is down";
954 case ENETRESET: return "network dropped connection on reset";
957 case ENETUNREACH: return "network is unreachable";
960 case ENFILE: return "file table overflow";
963 case ENOANO: return "anode table overflow";
965 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
966 case ENOBUFS: return "no buffer space available";
969 case ENOCSI: return "no CSI structure available";
972 case ENODATA: return "no data available";
975 case ENODEV: return "no such device";
978 case ENOENT: return "no such file or directory";
981 case ENOEXEC: return "exec format error";
984 case ENOLCK: return "no locks available";
987 case ENOLINK: return "link has be severed";
990 case ENOMEM: return "not enough memory";
993 case ENOMSG: return "no message of desired type";
996 case ENONET: return "machine is not on the network";
999 case ENOPKG: return "package not installed";
1002 case ENOPROTOOPT: return "bad proocol option";
1005 case ENOSPC: return "no space left on device";
1008 case ENOSR: return "out of stream resources";
1011 case ENOSTR: return "not a stream device";
1014 case ENOSYM: return "unresolved symbol name";
1017 case ENOSYS: return "function not implemented";
1020 case ENOTBLK: return "block device required";
1023 case ENOTCONN: return "socket is not connected";
1026 case ENOTDIR: return "not a directory";
1029 case ENOTEMPTY: return "directory not empty";
1032 case ENOTNAM: return "not a name file";
1035 case ENOTSOCK: return "socket operation on non-socket";
1038 case ENOTTY: return "inappropriate device for ioctl";
1041 case ENOTUNIQ: return "name not unique on network";
1044 case ENXIO: return "no such device or address";
1047 case EOPNOTSUPP: return "operation not supported on socket";
1050 case EPERM: return "not owner";
1053 case EPFNOSUPPORT: return "protocol family not supported";
1056 case EPIPE: return "broken pipe";
1059 case EPROCLIM: return "too many processes";
1062 case EPROCUNAVAIL: return "bad procedure for program";
1064 #ifdef EPROGMISMATCH
1065 case EPROGMISMATCH: return "program version wrong";
1068 case EPROGUNAVAIL: return "RPC program not available";
1071 case EPROTO: return "protocol error";
1073 #ifdef EPROTONOSUPPORT
1074 case EPROTONOSUPPORT: return "protocol not suppored";
1077 case EPROTOTYPE: return "protocol wrong type for socket";
1080 case ERANGE: return "math result unrepresentable";
1082 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
1083 case EREFUSED: return "EREFUSED";
1086 case EREMCHG: return "remote address changed";
1089 case EREMDEV: return "remote device";
1092 case EREMOTE: return "pathname hit remote file system";
1095 case EREMOTEIO: return "remote i/o error";
1097 #ifdef EREMOTERELEASE
1098 case EREMOTERELEASE: return "EREMOTERELEASE";
1101 case EROFS: return "read-only file system";
1104 case ERPCMISMATCH: return "RPC version is wrong";
1107 case ERREMOTE: return "object is remote";
1110 case ESHUTDOWN: return "can't send afer socket shutdown";
1112 #ifdef ESOCKTNOSUPPORT
1113 case ESOCKTNOSUPPORT: return "socket type not supported";
1116 case ESPIPE: return "invalid seek";
1119 case ESRCH: return "no such process";
1122 case ESRMNT: return "srmount error";
1125 case ESTALE: return "stale remote file handle";
1128 case ESUCCESS: return "Error 0";
1131 case ETIME: return "timer expired";
1134 case ETIMEDOUT: return "connection timed out";
1137 case ETOOMANYREFS: return "too many references: can't splice";
1140 case ETXTBSY: return "text file or pseudo-device busy";
1143 case EUCLEAN: return "structure needs cleaning";
1146 case EUNATCH: return "protocol driver not attached";
1149 case EUSERS: return "too many users";
1152 case EVERSION: return "version mismatch";
1154 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
1155 case EWOULDBLOCK: return "operation would block";
1158 case EXDEV: return "cross-domain link";
1161 case EXFULL: return "message tables full";
1164 #else /* NO_SYS_ERRLIST */
1165 extern int sys_nerr;
1166 extern char *sys_errlist[];
1168 if ((errnum > 0) && (errnum <= sys_nerr))
1169 return sys_errlist [errnum];
1170 #endif /* NO_SYS_ERRLIST */
1172 msg = g_static_private_get (&msg_private);
1175 msg = g_new (gchar, 64);
1176 g_static_private_set (&msg_private, msg, g_free);
1179 _g_sprintf (msg, "unknown error (%d)", errnum);
1184 G_CONST_RETURN gchar*
1185 g_strsignal (gint signum)
1187 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
1190 #ifdef HAVE_STRSIGNAL
1191 const char *msg_locale;
1193 #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
1194 extern const char *strsignal(int);
1196 /* this is declared differently (const) in string.h on BeOS */
1197 extern char *strsignal (int sig);
1198 #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
1199 msg_locale = strsignal (signum);
1200 if (g_get_charset (NULL))
1204 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
1207 /* Stick in the quark table so that we can return a static result
1209 GQuark msg_quark = g_quark_from_string (msg_utf8);
1212 return g_quark_to_string (msg_quark);
1215 #elif NO_SYS_SIGLIST
1219 case SIGHUP: return "Hangup";
1222 case SIGINT: return "Interrupt";
1225 case SIGQUIT: return "Quit";
1228 case SIGILL: return "Illegal instruction";
1231 case SIGTRAP: return "Trace/breakpoint trap";
1234 case SIGABRT: return "IOT trap/Abort";
1237 case SIGBUS: return "Bus error";
1240 case SIGFPE: return "Floating point exception";
1243 case SIGKILL: return "Killed";
1246 case SIGUSR1: return "User defined signal 1";
1249 case SIGSEGV: return "Segmentation fault";
1252 case SIGUSR2: return "User defined signal 2";
1255 case SIGPIPE: return "Broken pipe";
1258 case SIGALRM: return "Alarm clock";
1261 case SIGTERM: return "Terminated";
1264 case SIGSTKFLT: return "Stack fault";
1267 case SIGCHLD: return "Child exited";
1270 case SIGCONT: return "Continued";
1273 case SIGSTOP: return "Stopped (signal)";
1276 case SIGTSTP: return "Stopped";
1279 case SIGTTIN: return "Stopped (tty input)";
1282 case SIGTTOU: return "Stopped (tty output)";
1285 case SIGURG: return "Urgent condition";
1288 case SIGXCPU: return "CPU time limit exceeded";
1291 case SIGXFSZ: return "File size limit exceeded";
1294 case SIGVTALRM: return "Virtual time alarm";
1297 case SIGPROF: return "Profile signal";
1300 case SIGWINCH: return "Window size changed";
1303 case SIGIO: return "Possible I/O";
1306 case SIGPWR: return "Power failure";
1309 case SIGUNUSED: return "Unused signal";
1312 #else /* NO_SYS_SIGLIST */
1314 #ifdef NO_SYS_SIGLIST_DECL
1315 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
1318 return (char*) /* this function should return const --josh */ sys_siglist [signum];
1319 #endif /* NO_SYS_SIGLIST */
1321 msg = g_static_private_get (&msg_private);
1324 msg = g_new (gchar, 64);
1325 g_static_private_set (&msg_private, msg, g_free);
1328 _g_sprintf (msg, "unknown signal (%d)", signum);
1333 /* Functions g_strlcpy and g_strlcat were originally developed by
1334 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
1335 * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
1336 * for more information.
1340 /* Use the native ones, if available; they might be implemented in assembly */
1342 g_strlcpy (gchar *dest,
1346 g_return_val_if_fail (dest != NULL, 0);
1347 g_return_val_if_fail (src != NULL, 0);
1349 return strlcpy (dest, src, dest_size);
1353 g_strlcat (gchar *dest,
1357 g_return_val_if_fail (dest != NULL, 0);
1358 g_return_val_if_fail (src != NULL, 0);
1360 return strlcat (dest, src, dest_size);
1363 #else /* ! HAVE_STRLCPY */
1366 * Copy string src to buffer dest (of buffer size dest_size). At most
1367 * dest_size-1 characters will be copied. Always NUL terminates
1368 * (unless dest_size == 0). This function does NOT allocate memory.
1369 * Unlike strncpy, this function doesn't pad dest (so it's often faster).
1370 * Returns size of attempted result, strlen(src),
1371 * so if retval >= dest_size, truncation occurred.
1374 g_strlcpy (gchar *dest,
1378 register gchar *d = dest;
1379 register const gchar *s = src;
1380 register gsize n = dest_size;
1382 g_return_val_if_fail (dest != NULL, 0);
1383 g_return_val_if_fail (src != NULL, 0);
1385 /* Copy as many bytes as will fit */
1386 if (n != 0 && --n != 0)
1389 register gchar c = *s++;
1397 /* If not enough room in dest, add NUL and traverse rest of src */
1406 return s - src - 1; /* count does not include NUL */
1411 * Appends string src to buffer dest (of buffer size dest_size).
1412 * At most dest_size-1 characters will be copied.
1413 * Unlike strncat, dest_size is the full size of dest, not the space left over.
1414 * This function does NOT allocate memory.
1415 * This always NUL terminates (unless siz == 0 or there were no NUL characters
1416 * in the dest_size characters of dest to start with).
1417 * Returns size of attempted result, which is
1418 * MIN (dest_size, strlen (original dest)) + strlen (src),
1419 * so if retval >= dest_size, truncation occurred.
1422 g_strlcat (gchar *dest,
1426 register gchar *d = dest;
1427 register const gchar *s = src;
1428 register gsize bytes_left = dest_size;
1429 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
1431 g_return_val_if_fail (dest != NULL, 0);
1432 g_return_val_if_fail (src != NULL, 0);
1434 /* Find the end of dst and adjust bytes left but don't go past end */
1435 while (*d != 0 && bytes_left-- != 0)
1438 bytes_left = dest_size - dlength;
1440 if (bytes_left == 0)
1441 return dlength + strlen (s);
1445 if (bytes_left != 1)
1454 return dlength + (s - src); /* count does not include NUL */
1456 #endif /* ! HAVE_STRLCPY */
1461 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1463 * Converts all upper case ASCII letters to lower case ASCII letters.
1465 * Return value: a newly-allocated string, with all the upper case
1466 * characters in @str converted to lower case, with
1467 * semantics that exactly match g_ascii_tolower(). (Note
1468 * that this is unlike the old g_strdown(), which modified
1469 * the string in place.)
1472 g_ascii_strdown (const gchar *str,
1477 g_return_val_if_fail (str != NULL, NULL);
1482 result = g_strndup (str, len);
1483 for (s = result; *s; s++)
1484 *s = g_ascii_tolower (*s);
1492 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1494 * Converts all lower case ASCII letters to upper case ASCII letters.
1496 * Return value: a newly allocated string, with all the lower case
1497 * characters in @str converted to upper case, with
1498 * semantics that exactly match g_ascii_toupper(). (Note
1499 * that this is unlike the old g_strup(), which modified
1500 * the string in place.)
1503 g_ascii_strup (const gchar *str,
1508 g_return_val_if_fail (str != NULL, NULL);
1513 result = g_strndup (str, len);
1514 for (s = result; *s; s++)
1515 *s = g_ascii_toupper (*s);
1522 * @string: the string to convert.
1524 * Converts a string to lower case.
1526 * Return value: the string
1528 * Deprecated: This function is totally broken for the reasons discussed in
1529 * the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown()
1533 g_strdown (gchar *string)
1537 g_return_val_if_fail (string != NULL, NULL);
1539 s = (guchar *) string;
1548 return (gchar *) string;
1553 * @string: the string to convert.
1555 * Converts a string to upper case.
1557 * Return value: the string
1559 * Deprecated: This function is totally broken for the reasons discussed in
1560 * the g_strncasecmp() docs - use g_ascii_strup() or g_utf8_strup() instead.
1563 g_strup (gchar *string)
1567 g_return_val_if_fail (string != NULL, NULL);
1569 s = (guchar *) string;
1578 return (gchar *) string;
1582 g_strreverse (gchar *string)
1584 g_return_val_if_fail (string != NULL, NULL);
1588 register gchar *h, *t;
1591 t = string + strlen (string) - 1;
1610 * @c: any character.
1612 * Convert a character to ASCII lower case.
1614 * Unlike the standard C library tolower() function, this only
1615 * recognizes standard ASCII letters and ignores the locale, returning
1616 * all non-ASCII characters unchanged, even if they are lower case
1617 * letters in a particular character set. Also unlike the standard
1618 * library function, this takes and returns a char, not an int, so
1619 * don't call it on %EOF but no need to worry about casting to #guchar
1620 * before passing a possibly non-ASCII character in.
1622 * Return value: the result of converting @c to lower case.
1623 * If @c is not an ASCII upper case letter,
1624 * @c is returned unchanged.
1627 g_ascii_tolower (gchar c)
1629 return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1634 * @c: any character.
1636 * Convert a character to ASCII upper case.
1638 * Unlike the standard C library toupper() function, this only
1639 * recognizes standard ASCII letters and ignores the locale, returning
1640 * all non-ASCII characters unchanged, even if they are upper case
1641 * letters in a particular character set. Also unlike the standard
1642 * library function, this takes and returns a char, not an int, so
1643 * don't call it on %EOF but no need to worry about casting to #guchar
1644 * before passing a possibly non-ASCII character in.
1646 * Return value: the result of converting @c to upper case.
1647 * If @c is not an ASCII lower case letter,
1648 * @c is returned unchanged.
1651 g_ascii_toupper (gchar c)
1653 return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1657 * g_ascii_digit_value:
1658 * @c: an ASCII character.
1660 * Determines the numeric value of a character as a decimal
1661 * digit. Differs from g_unichar_digit_value() because it takes
1662 * a char, so there's no worry about sign extension if characters
1665 * Return value: If @c is a decimal digit (according to
1666 * g_ascii_isdigit()), its numeric value. Otherwise, -1.
1669 g_ascii_digit_value (gchar c)
1671 if (g_ascii_isdigit (c))
1677 * g_ascii_xdigit_value:
1678 * @c: an ASCII character.
1680 * Determines the numeric value of a character as a hexidecimal
1681 * digit. Differs from g_unichar_xdigit_value() because it takes
1682 * a char, so there's no worry about sign extension if characters
1685 * Return value: If @c is a hex digit (according to
1686 * g_ascii_isxdigit()), its numeric value. Otherwise, -1.
1689 g_ascii_xdigit_value (gchar c)
1691 if (c >= 'A' && c <= 'F')
1692 return c - 'A' + 10;
1693 if (c >= 'a' && c <= 'f')
1694 return c - 'a' + 10;
1695 return g_ascii_digit_value (c);
1699 * g_ascii_strcasecmp:
1700 * @s1: string to compare with @s2.
1701 * @s2: string to compare with @s1.
1703 * Compare two strings, ignoring the case of ASCII characters.
1705 * Unlike the BSD strcasecmp() function, this only recognizes standard
1706 * ASCII letters and ignores the locale, treating all non-ASCII
1707 * characters as if they are not letters.
1709 * Return value: an integer less than, equal to, or greater than
1710 * zero if @s1 is found, respectively, to be less than,
1711 * to match, or to be greater than @s2.
1714 g_ascii_strcasecmp (const gchar *s1,
1719 g_return_val_if_fail (s1 != NULL, 0);
1720 g_return_val_if_fail (s2 != NULL, 0);
1724 c1 = (gint)(guchar) TOLOWER (*s1);
1725 c2 = (gint)(guchar) TOLOWER (*s2);
1731 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1735 * g_ascii_strncasecmp:
1736 * @s1: string to compare with @s2.
1737 * @s2: string to compare with @s1.
1738 * @n: number of characters to compare.
1740 * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1741 * characters after the first @n in each string.
1743 * Unlike the BSD strcasecmp() function, this only recognizes standard
1744 * ASCII letters and ignores the locale, treating all non-ASCII
1745 * characters as if they are not letters.
1747 * Return value: an integer less than, equal to, or greater than zero
1748 * if the first @n bytes of @s1 is found, respectively,
1749 * to be less than, to match, or to be greater than the
1750 * first @n bytes of @s2.
1753 g_ascii_strncasecmp (const gchar *s1,
1759 g_return_val_if_fail (s1 != NULL, 0);
1760 g_return_val_if_fail (s2 != NULL, 0);
1762 while (n && *s1 && *s2)
1765 c1 = (gint)(guchar) TOLOWER (*s1);
1766 c2 = (gint)(guchar) TOLOWER (*s2);
1773 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1781 * @s2: a string to compare with @s1.
1783 * A case-insensitive string comparison, corresponding to the standard
1784 * strcasecmp() function on platforms which support it.
1786 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1787 * or a positive value if @s1 > @s2.
1789 * Deprecated: See g_strncasecmp() for a discussion of why this function is
1790 * deprecated and how to replace it.
1793 g_strcasecmp (const gchar *s1,
1796 #ifdef HAVE_STRCASECMP
1797 g_return_val_if_fail (s1 != NULL, 0);
1798 g_return_val_if_fail (s2 != NULL, 0);
1800 return strcasecmp (s1, s2);
1804 g_return_val_if_fail (s1 != NULL, 0);
1805 g_return_val_if_fail (s2 != NULL, 0);
1809 /* According to A. Cox, some platforms have islower's that
1810 * don't work right on non-uppercase
1812 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1813 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1819 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1826 * @s2: a string to compare with @s1.
1827 * @n: the maximum number of characters to compare.
1829 * A case-insensitive string comparison, corresponding to the standard
1830 * strncasecmp() function on platforms which support it.
1831 * It is similar to g_strcasecmp() except it only compares the first @n
1832 * characters of the strings.
1834 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1835 * or a positive value if @s1 > @s2.
1837 * Deprecated: The problem with g_strncasecmp() is that it does the
1838 * comparison by calling toupper()/tolower(). These functions are
1839 * locale-specific and operate on single bytes. However, it is impossible
1840 * to handle things correctly from an I18N standpoint by operating on
1841 * bytes, since characters may be multibyte. Thus g_strncasecmp() is
1842 * broken if your string is guaranteed to be ASCII, since it's
1843 * locale-sensitive, and it's broken if your string is localized, since
1844 * it doesn't work on many encodings at all, including UTF-8, EUC-JP,
1847 * There are therefore two replacement functions: g_ascii_strncasecmp(),
1848 * which only works on ASCII and is not locale-sensitive, and
1849 * g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8.
1852 g_strncasecmp (const gchar *s1,
1856 #ifdef HAVE_STRNCASECMP
1857 return strncasecmp (s1, s2, n);
1861 g_return_val_if_fail (s1 != NULL, 0);
1862 g_return_val_if_fail (s2 != NULL, 0);
1864 while (n && *s1 && *s2)
1867 /* According to A. Cox, some platforms have islower's that
1868 * don't work right on non-uppercase
1870 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1871 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1878 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1885 g_strdelimit (gchar *string,
1886 const gchar *delimiters,
1891 g_return_val_if_fail (string != NULL, NULL);
1894 delimiters = G_STR_DELIMITERS;
1896 for (c = string; *c; c++)
1898 if (strchr (delimiters, *c))
1906 g_strcanon (gchar *string,
1907 const gchar *valid_chars,
1912 g_return_val_if_fail (string != NULL, NULL);
1913 g_return_val_if_fail (valid_chars != NULL, NULL);
1915 for (c = string; *c; c++)
1917 if (!strchr (valid_chars, *c))
1925 g_strcompress (const gchar *source)
1927 const gchar *p = source, *octal;
1928 gchar *dest = g_malloc (strlen (source) + 1);
1938 case '0': case '1': case '2': case '3': case '4':
1939 case '5': case '6': case '7':
1942 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
1944 *q = (*q * 8) + (*p - '0');
1965 default: /* Also handles \" and \\ */
1980 g_strescape (const gchar *source,
1981 const gchar *exceptions)
1988 g_return_val_if_fail (source != NULL, NULL);
1990 p = (guchar *) source;
1991 /* Each source byte needs maximally four destination chars (\777) */
1992 q = dest = g_malloc (strlen (source) * 4 + 1);
1994 memset (excmap, 0, 256);
1997 guchar *e = (guchar *) exceptions;
2043 if ((*p < ' ') || (*p >= 0177))
2046 *q++ = '0' + (((*p) >> 6) & 07);
2047 *q++ = '0' + (((*p) >> 3) & 07);
2048 *q++ = '0' + ((*p) & 07);
2062 g_strchug (gchar *string)
2066 g_return_val_if_fail (string != NULL, NULL);
2068 for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
2071 g_memmove (string, start, strlen ((gchar *) start) + 1);
2077 g_strchomp (gchar *string)
2081 g_return_val_if_fail (string != NULL, NULL);
2083 len = strlen (string);
2086 if (g_ascii_isspace ((guchar) string[len]))
2097 * @string: a string to split.
2098 * @delimiter: a string which specifies the places at which to split the string.
2099 * The delimiter is not included in any of the resulting strings, unless
2100 * @max_tokens is reached.
2101 * @max_tokens: the maximum number of pieces to split @string into. If this is
2102 * less than 1, the string is split completely.
2104 * Splits a string into a maximum of @max_tokens pieces, using the given
2105 * @delimiter. If @max_tokens is reached, the remainder of @string is appended
2106 * to the last token.
2108 * As a special case, the result of splitting the empty string "" is an empty
2109 * vector, not a vector containing a single string. The reason for this
2110 * special case is that being able to represent a empty vector is typically
2111 * more useful than consistent handling of empty elements. If you do need
2112 * to represent empty elements, you'll need to check for the empty string
2113 * before calling g_strsplit().
2115 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2116 * g_strfreev() to free it.
2119 g_strsplit (const gchar *string,
2120 const gchar *delimiter,
2123 GSList *string_list = NULL, *slist;
2124 gchar **str_array, *s;
2126 const gchar *remainder;
2128 g_return_val_if_fail (string != NULL, NULL);
2129 g_return_val_if_fail (delimiter != NULL, NULL);
2130 g_return_val_if_fail (delimiter[0] != '\0', NULL);
2133 max_tokens = G_MAXINT;
2136 s = strstr (remainder, delimiter);
2139 gsize delimiter_len = strlen (delimiter);
2141 while (--max_tokens && s)
2146 len = s - remainder;
2147 new_string = g_new (gchar, len + 1);
2148 strncpy (new_string, remainder, len);
2149 new_string[len] = 0;
2150 string_list = g_slist_prepend (string_list, new_string);
2152 remainder = s + delimiter_len;
2153 s = strstr (remainder, delimiter);
2159 string_list = g_slist_prepend (string_list, g_strdup (remainder));
2162 str_array = g_new (gchar*, n + 1);
2164 str_array[n--] = NULL;
2165 for (slist = string_list; slist; slist = slist->next)
2166 str_array[n--] = slist->data;
2168 g_slist_free (string_list);
2175 * @str_array: a %NULL-terminated array of strings to free.
2177 * Frees a %NULL-terminated array of strings, and the array itself.
2178 * If called on a %NULL value, g_strfreev() simply returns.
2181 g_strfreev (gchar **str_array)
2187 for(i = 0; str_array[i] != NULL; i++)
2188 g_free(str_array[i]);
2196 * @str_array: %NULL-terminated array of strings.
2198 * Copies %NULL-terminated array of strings. The copy is a deep copy;
2199 * the new array should be freed by first freeing each string, then
2200 * the array itself. g_strfreev() does this for you. If called
2201 * on a %NULL value, g_strdupv() simply returns %NULL.
2203 * Return value: a new %NULL-terminated array of strings.
2206 g_strdupv (gchar **str_array)
2214 while (str_array[i])
2217 retval = g_new (gchar*, i + 1);
2220 while (str_array[i])
2222 retval[i] = g_strdup (str_array[i]);
2234 g_strjoinv (const gchar *separator,
2240 g_return_val_if_fail (str_array != NULL, NULL);
2242 if (separator == NULL)
2249 gsize separator_len;
2251 separator_len = strlen (separator);
2252 /* First part, getting length */
2253 len = 1 + strlen (str_array[0]);
2254 for (i = 1; str_array[i] != NULL; i++)
2255 len += strlen (str_array[i]);
2256 len += separator_len * (i - 1);
2258 /* Second part, building string */
2259 string = g_new (gchar, len);
2260 ptr = g_stpcpy (string, *str_array);
2261 for (i = 1; str_array[i] != NULL; i++)
2263 ptr = g_stpcpy (ptr, separator);
2264 ptr = g_stpcpy (ptr, str_array[i]);
2268 string = g_strdup ("");
2274 g_strjoin (const gchar *separator,
2280 gsize separator_len;
2283 if (separator == NULL)
2286 separator_len = strlen (separator);
2288 va_start (args, separator);
2290 s = va_arg (args, gchar*);
2294 /* First part, getting length */
2295 len = 1 + strlen (s);
2297 s = va_arg (args, gchar*);
2300 len += separator_len + strlen (s);
2301 s = va_arg (args, gchar*);
2305 /* Second part, building string */
2306 string = g_new (gchar, len);
2308 va_start (args, separator);
2310 s = va_arg (args, gchar*);
2311 ptr = g_stpcpy (string, s);
2313 s = va_arg (args, gchar*);
2316 ptr = g_stpcpy (ptr, separator);
2317 ptr = g_stpcpy (ptr, s);
2318 s = va_arg (args, gchar*);
2322 string = g_strdup ("");
2332 * @haystack: a string.
2333 * @haystack_len: the maximum length of @haystack.
2334 * @needle: the string to search for.
2336 * Searches the string @haystack for the first occurrence
2337 * of the string @needle, limiting the length of the search
2340 * Return value: a pointer to the found occurrence, or
2341 * %NULL if not found.
2344 g_strstr_len (const gchar *haystack,
2345 gssize haystack_len,
2346 const gchar *needle)
2348 g_return_val_if_fail (haystack != NULL, NULL);
2349 g_return_val_if_fail (needle != NULL, NULL);
2351 if (haystack_len < 0)
2352 return strstr (haystack, needle);
2355 const gchar *p = haystack;
2356 gsize needle_len = strlen (needle);
2360 if (needle_len == 0)
2361 return (gchar *)haystack;
2363 if (haystack_len < needle_len)
2366 end = haystack + haystack_len - needle_len;
2368 while (*p && p <= end)
2370 for (i = 0; i < needle_len; i++)
2371 if (p[i] != needle[i])
2386 * @haystack: a nul-terminated string.
2387 * @needle: the nul-terminated string to search for.
2389 * Searches the string @haystack for the last occurrence
2390 * of the string @needle.
2392 * Return value: a pointer to the found occurrence, or
2393 * %NULL if not found.
2396 g_strrstr (const gchar *haystack,
2397 const gchar *needle)
2404 g_return_val_if_fail (haystack != NULL, NULL);
2405 g_return_val_if_fail (needle != NULL, NULL);
2407 needle_len = strlen (needle);
2408 haystack_len = strlen (haystack);
2410 if (needle_len == 0)
2411 return (gchar *)haystack;
2413 if (haystack_len < needle_len)
2416 p = haystack + haystack_len - needle_len;
2418 while (p >= haystack)
2420 for (i = 0; i < needle_len; i++)
2421 if (p[i] != needle[i])
2435 * @haystack: a nul-terminated string.
2436 * @haystack_len: the maximum length of @haystack.
2437 * @needle: the nul-terminated string to search for.
2439 * Searches the string @haystack for the last occurrence
2440 * of the string @needle, limiting the length of the search
2443 * Return value: a pointer to the found occurrence, or
2444 * %NULL if not found.
2447 g_strrstr_len (const gchar *haystack,
2448 gssize haystack_len,
2449 const gchar *needle)
2451 g_return_val_if_fail (haystack != NULL, NULL);
2452 g_return_val_if_fail (needle != NULL, NULL);
2454 if (haystack_len < 0)
2455 return g_strrstr (haystack, needle);
2458 gsize needle_len = strlen (needle);
2459 const gchar *haystack_max = haystack + haystack_len;
2460 const gchar *p = haystack;
2463 while (p < haystack_max && *p)
2466 if (p < haystack + needle_len)
2471 while (p >= haystack)
2473 for (i = 0; i < needle_len; i++)
2474 if (p[i] != needle[i])
2490 * @str: a nul-terminated string.
2491 * @suffix: the nul-terminated suffix to look for.
2493 * Looks whether the string @str ends with @suffix.
2495 * Return value: %TRUE if @str end with @suffix, %FALSE otherwise.
2500 g_str_has_suffix (const gchar *str,
2501 const gchar *suffix)
2506 g_return_val_if_fail (str != NULL, FALSE);
2507 g_return_val_if_fail (suffix != NULL, FALSE);
2509 str_len = strlen (str);
2510 suffix_len = strlen (suffix);
2512 if (str_len < suffix_len)
2515 return strcmp (str + str_len - suffix_len, suffix) == 0;
2520 * @str: a nul-terminated string.
2521 * @prefix: the nul-terminated prefix to look for.
2523 * Looks whether the string @str begins with @prefix.
2525 * Return value: %TRUE if @str begins with @prefix, %FALSE otherwise.
2530 g_str_has_prefix (const gchar *str,
2531 const gchar *prefix)
2536 g_return_val_if_fail (str != NULL, FALSE);
2537 g_return_val_if_fail (prefix != NULL, FALSE);
2539 str_len = strlen (str);
2540 prefix_len = strlen (prefix);
2542 if (str_len < prefix_len)
2545 return strncmp (str, prefix, prefix_len) == 0;
2552 * @msgval: another string
2554 * An auxiliary function for gettext() support (see Q_()).
2556 * Return value: @msgval, unless @msgval is identical to @msgid and contains
2557 * a '|' character, in which case a pointer to the substring of msgid after
2558 * the first '|' character is returned.
2562 G_CONST_RETURN gchar *
2563 g_strip_context (const gchar *msgid,
2564 const gchar *msgval)
2566 if (msgval == msgid)
2568 const char *c = strchr (msgid, '|');