Branch data Line data Source code
1 : : /* GLIB - Library of useful routines for C programming
2 : : * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 : : *
4 : : * SPDX-License-Identifier: LGPL-2.1-or-later
5 : : *
6 : : * This library is free software; you can redistribute it and/or
7 : : * modify it under the terms of the GNU Lesser General Public
8 : : * License as published by the Free Software Foundation; either
9 : : * version 2.1 of the License, or (at your option) any later version.
10 : : *
11 : : * This library is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : : * Lesser General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU Lesser General Public
17 : : * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 : : */
19 : :
20 : : /*
21 : : * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 : : * file for a list of people on the GLib Team. See the ChangeLog
23 : : * files for a list of changes. These files are distributed with
24 : : * GLib at ftp://ftp.gtk.org/pub/gtk/.
25 : : */
26 : :
27 : : /*
28 : : * MT safe
29 : : */
30 : :
31 : : #include "config.h"
32 : :
33 : : #include <stdarg.h>
34 : : #include <stdio.h>
35 : : #include <stdlib.h>
36 : : #include <locale.h>
37 : : #include <string.h>
38 : : #include <locale.h>
39 : : #include <errno.h>
40 : : #include <garray.h>
41 : : #include <ctype.h> /* For tolower() */
42 : :
43 : : #ifdef HAVE_XLOCALE_H
44 : : /* Needed on BSD/OS X for e.g. strtod_l */
45 : : #include <xlocale.h>
46 : : #endif
47 : :
48 : : #ifdef G_OS_WIN32
49 : : #include <windows.h>
50 : : #endif
51 : :
52 : : /* do not include <unistd.h> here, it may interfere with g_strsignal() */
53 : :
54 : : #include "gstrfuncs.h"
55 : :
56 : : #include "gprintf.h"
57 : : #include "gprintfint.h"
58 : : #include "glibintl.h"
59 : :
60 : : /**
61 : : * g_ascii_isalnum:
62 : : * @c: any character
63 : : *
64 : : * Determines whether a character is alphanumeric.
65 : : *
66 : : * Unlike the standard C library `isalnum()` function, this only
67 : : * recognizes standard ASCII letters and ignores the locale,
68 : : * returning false for all non-ASCII characters. Also, unlike
69 : : * the standard library function, this takes a `char`, not an `int`,
70 : : * so don't call it on `EOF`, but no need to cast to `guchar` before
71 : : * passing a possibly non-ASCII character in.
72 : : *
73 : : * Returns: true if @c is an ASCII alphanumeric character
74 : : */
75 : :
76 : : /**
77 : : * g_ascii_isalpha:
78 : : * @c: any character
79 : : *
80 : : * Determines whether a character is alphabetic (i.e. a letter).
81 : : *
82 : : * Unlike the standard C library `isalpha()` function, this only
83 : : * recognizes standard ASCII letters and ignores the locale,
84 : : * returning false for all non-ASCII characters. Also, unlike
85 : : * the standard library function, this takes a `char`, not an `int`,
86 : : * so don't call it on `EOF`, but no need to cast to `guchar` before
87 : : * passing a possibly non-ASCII character in.
88 : : *
89 : : * Returns: true if @c is an ASCII alphabetic character
90 : : */
91 : :
92 : : /**
93 : : * g_ascii_iscntrl:
94 : : * @c: any character
95 : : *
96 : : * Determines whether a character is a control character.
97 : : *
98 : : * Unlike the standard C library `iscntrl()` function, this only
99 : : * recognizes standard ASCII control characters and ignores the
100 : : * locale, returning false for all non-ASCII characters. Also,
101 : : * unlike the standard library function, this takes a `char`, not
102 : : * an `int`, so don't call it on `EOF`, but no need to cast to `guchar`
103 : : * before passing a possibly non-ASCII character in.
104 : : *
105 : : * Returns: true if @c is an ASCII control character
106 : : */
107 : :
108 : : /**
109 : : * g_ascii_isdigit:
110 : : * @c: any character
111 : : *
112 : : * Determines whether a character is digit (0-9).
113 : : *
114 : : * Unlike the standard C library `isdigit()` function, this takes
115 : : * a `char`, not an `int`, so don't call it on `EOF`, but no need to
116 : : * cast to `guchar` before passing a possibly non-ASCII character in.
117 : : *
118 : : * Returns: true if @c is an ASCII digit
119 : : */
120 : :
121 : : /**
122 : : * g_ascii_isgraph:
123 : : * @c: any character
124 : : *
125 : : * Determines whether a character is a printing character and not a space.
126 : : *
127 : : * Unlike the standard C library `isgraph()` function, this only
128 : : * recognizes standard ASCII characters and ignores the locale,
129 : : * returning false for all non-ASCII characters. Also, unlike
130 : : * the standard library function, this takes a `char`, not an `int`,
131 : : * so don't call it on `EOF`, but no need to cast to `guchar` before
132 : : * passing a possibly non-ASCII character in.
133 : : *
134 : : * Returns: true if @c is an ASCII printing character other than space
135 : : */
136 : :
137 : : /**
138 : : * g_ascii_islower:
139 : : * @c: any character
140 : : *
141 : : * Determines whether a character is an ASCII lower case letter.
142 : : *
143 : : * Unlike the standard C library `islower()` function, this only
144 : : * recognizes standard ASCII letters and ignores the locale,
145 : : * returning false for all non-ASCII characters. Also, unlike
146 : : * the standard library function, this takes a `char`, not an `int`,
147 : : * so don't call it on `EOF`, but no need to worry about casting
148 : : * to `guchar` before passing a possibly non-ASCII character in.
149 : : *
150 : : * Returns: true if @c is an ASCII lower case letter
151 : : */
152 : :
153 : : /**
154 : : * g_ascii_isprint:
155 : : * @c: any character
156 : : *
157 : : * Determines whether a character is a printing character.
158 : : *
159 : : * Unlike the standard C library `isprint()` function, this only
160 : : * recognizes standard ASCII characters and ignores the locale,
161 : : * returning false for all non-ASCII characters. Also, unlike
162 : : * the standard library function, this takes a `char`, not an `int`,
163 : : * so don't call it on `EOF`, but no need to cast to `guchar` before
164 : : * passing a possibly non-ASCII character in.
165 : : *
166 : : * Returns: true if @c is an ASCII printing character
167 : : */
168 : :
169 : : /**
170 : : * g_ascii_ispunct:
171 : : * @c: any character
172 : : *
173 : : * Determines whether a character is a punctuation character.
174 : : *
175 : : * Unlike the standard C library `ispunct()` function, this only
176 : : * recognizes standard ASCII letters and ignores the locale,
177 : : * returning false for all non-ASCII characters. Also, unlike
178 : : * the standard library function, this takes a `char`, not an `int`,
179 : : * so don't call it on `EOF`, but no need to cast to `guchar` before
180 : : * passing a possibly non-ASCII character in.
181 : : *
182 : : * Returns: true if @c is an ASCII punctuation character
183 : : */
184 : :
185 : : /**
186 : : * g_ascii_isspace:
187 : : * @c: any character
188 : : *
189 : : * Determines whether a character is a white-space character.
190 : : *
191 : : * Unlike the standard C library `isspace()` function, this only
192 : : * recognizes standard ASCII white-space and ignores the locale,
193 : : * returning false for all non-ASCII characters. Also, unlike
194 : : * the standard library function, this takes a `char`, not an `int`,
195 : : * so don't call it on `EOF`, but no need to cast to `guchar` before
196 : : * passing a possibly non-ASCII character in.
197 : : *
198 : : * Returns: true if @c is an ASCII white-space character
199 : : */
200 : :
201 : : /**
202 : : * g_ascii_isupper:
203 : : * @c: any character
204 : : *
205 : : * Determines whether a character is an ASCII upper case letter.
206 : : *
207 : : * Unlike the standard C library `isupper()` function, this only
208 : : * recognizes standard ASCII letters and ignores the locale,
209 : : * returning false for all non-ASCII characters. Also, unlike
210 : : * the standard library function, this takes a `char`, not an `int`,
211 : : * so don't call it on `EOF`, but no need to worry about casting
212 : : * to `guchar` before passing a possibly non-ASCII character in.
213 : : *
214 : : * Returns: true if @c is an ASCII upper case letter
215 : : */
216 : :
217 : : /**
218 : : * g_ascii_isxdigit:
219 : : * @c: any character
220 : : *
221 : : * Determines whether a character is a hexadecimal-digit character.
222 : : *
223 : : * Unlike the standard C library `isxdigit()` function, this takes
224 : : * a `char`, not an `int`, so don't call it on `EOF`, but no need to
225 : : * cast to `guchar` before passing a possibly non-ASCII character in.
226 : : *
227 : : * Returns: true if @c is an ASCII hexadecimal-digit character
228 : : */
229 : :
230 : : /**
231 : : * G_ASCII_DTOSTR_BUF_SIZE:
232 : : *
233 : : * A good size for a buffer to be passed into [func@GLib.ascii_dtostr].
234 : : * It is guaranteed to be enough for all output of that function
235 : : * on systems with 64bit IEEE-compatible doubles.
236 : : *
237 : : * The typical usage would be something like:
238 : : * ```C
239 : : * char buf[G_ASCII_DTOSTR_BUF_SIZE];
240 : : *
241 : : * fprintf (out, "value=%s\n", g_ascii_dtostr (buf, sizeof (buf), value));
242 : : * ```
243 : : */
244 : :
245 : : /**
246 : : * g_strstrip:
247 : : * @string: a string to remove the leading and trailing whitespace from
248 : : *
249 : : * Removes leading and trailing whitespace from a string.
250 : : *
251 : : * See [func@GLib.strchomp] and [func@GLib.strchug].
252 : : *
253 : : * Returns: @string
254 : : */
255 : :
256 : : /**
257 : : * G_STR_DELIMITERS:
258 : : *
259 : : * The standard delimiters, used in [func@GLib.strdelimit].
260 : : */
261 : :
262 : : static const guint16 ascii_table_data[256] = {
263 : : 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
264 : : 0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004,
265 : : 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
266 : : 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
267 : : 0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
268 : : 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
269 : : 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
270 : : 0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
271 : : 0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
272 : : 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
273 : : 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
274 : : 0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
275 : : 0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
276 : : 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
277 : : 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
278 : : 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004
279 : : /* the upper 128 are all zeroes */
280 : : };
281 : :
282 : : const guint16 * const g_ascii_table = ascii_table_data;
283 : :
284 : : #if defined(HAVE_NEWLOCALE) && \
285 : : defined(HAVE_USELOCALE)
286 : : #define USE_XLOCALE 1
287 : : #endif
288 : :
289 : : #ifdef USE_XLOCALE
290 : : static locale_t
291 : 270184 : get_C_locale (void)
292 : : {
293 : : static gsize initialized = FALSE;
294 : : static locale_t C_locale = NULL;
295 : :
296 : 270184 : if (g_once_init_enter (&initialized))
297 : : {
298 : 740 : C_locale = newlocale (LC_ALL_MASK, "C", NULL);
299 : 740 : g_once_init_leave (&initialized, TRUE);
300 : : }
301 : :
302 : 270184 : return C_locale;
303 : : }
304 : : #endif
305 : :
306 : : /**
307 : : * g_strdup:
308 : : * @str: (nullable): the string to duplicate
309 : : *
310 : : * Duplicates a string. If @str is `NULL` it returns `NULL`.
311 : : *
312 : : * Returns: a newly-allocated copy of @str
313 : : */
314 : : gchar*
315 : 16030795 : (g_strdup) (const gchar *str)
316 : : {
317 : : gchar *new_str;
318 : : gsize length;
319 : :
320 : 16030795 : if G_LIKELY (str)
321 : : {
322 : 15753143 : length = strlen (str) + 1;
323 : 15753143 : new_str = g_new (char, length);
324 : 15753143 : memcpy (new_str, str, length);
325 : 9157275 : }
326 : : else
327 : 277652 : new_str = NULL;
328 : :
329 : 16030795 : return new_str;
330 : : }
331 : :
332 : : /**
333 : : * g_memdup:
334 : : * @mem: the memory to copy
335 : : * @byte_size: the number of bytes to copy
336 : : *
337 : : * Allocates @byte_size bytes of memory, and copies @byte_size bytes into it
338 : : * from @mem. If @mem is `NULL` it returns `NULL`.
339 : : *
340 : : * Returns: (transfer full) (nullable): a pointer to the newly-allocated copy of the memory
341 : : *
342 : : * Deprecated: 2.68: Use [func@GLib.memdup2] instead, as it accepts a gsize argument
343 : : * for @byte_size, avoiding the possibility of overflow in a `gsize` → `guint`
344 : : * conversion
345 : : */
346 : : gpointer
347 : 8 : g_memdup (gconstpointer mem,
348 : : guint byte_size)
349 : : {
350 : : gpointer new_mem;
351 : :
352 : 8 : if (mem && byte_size != 0)
353 : : {
354 : 2 : new_mem = g_malloc (byte_size);
355 : 2 : memcpy (new_mem, mem, byte_size);
356 : 1 : }
357 : : else
358 : 6 : new_mem = NULL;
359 : :
360 : 8 : return new_mem;
361 : : }
362 : :
363 : : /**
364 : : * g_memdup2:
365 : : * @mem: (nullable): the memory to copy
366 : : * @byte_size: the number of bytes to copy
367 : : *
368 : : * Allocates @byte_size bytes of memory, and copies @byte_size bytes into it
369 : : * from @mem. If @mem is `NULL` it returns `NULL`.
370 : : *
371 : : * This replaces [func@GLib.memdup], which was prone to integer overflows when
372 : : * converting the argument from a `gsize` to a `guint`.
373 : : *
374 : : * Returns: (transfer full) (nullable): a pointer to the newly-allocated copy of the memory
375 : : *
376 : : * Since: 2.68
377 : : */
378 : : gpointer
379 : 2880014 : g_memdup2 (gconstpointer mem,
380 : : gsize byte_size)
381 : : {
382 : : gpointer new_mem;
383 : :
384 : 2880014 : if (mem && byte_size != 0)
385 : : {
386 : 2879202 : new_mem = g_malloc (byte_size);
387 : 2879202 : memcpy (new_mem, mem, byte_size);
388 : 795992 : }
389 : : else
390 : 812 : new_mem = NULL;
391 : :
392 : 2880014 : return new_mem;
393 : : }
394 : :
395 : : /**
396 : : * g_strndup:
397 : : * @str: (nullable): the string to duplicate
398 : : * @n: the maximum number of bytes to copy from @str
399 : : *
400 : : * Duplicates the first @n bytes of a string, returning a newly-allocated
401 : : * buffer @n + 1 bytes long which will always be nul-terminated. If @str
402 : : * is less than @n bytes long the buffer is padded with nuls. If @str is
403 : : * `NULL` it returns `NULL`.
404 : : *
405 : : * To copy a number of characters from a UTF-8 encoded string,
406 : : * use [func@GLib.utf8_strncpy] instead.
407 : : *
408 : : * Returns: (nullable): a newly-allocated buffer containing the first
409 : : * @n bytes of @str
410 : : */
411 : : gchar*
412 : 1109701 : g_strndup (const gchar *str,
413 : : gsize n)
414 : : {
415 : : gchar *new_str;
416 : :
417 : 1109701 : if (str)
418 : : {
419 : 1109699 : g_return_val_if_fail (n < G_MAXSIZE, NULL);
420 : :
421 : 1109697 : new_str = g_new (gchar, n + 1);
422 : 1109697 : strncpy (new_str, str, n);
423 : 1109697 : new_str[n] = '\0';
424 : 471780 : }
425 : : else
426 : 2 : new_str = NULL;
427 : :
428 : 1109699 : return new_str;
429 : 471782 : }
430 : :
431 : : /**
432 : : * g_strnfill:
433 : : * @length: the length of the new string
434 : : * @fill_char: the byte to fill the string with
435 : : *
436 : : * Creates a new string @length bytes long filled with @fill_char.
437 : : *
438 : : * Returns: a newly-allocated string filled with @fill_char
439 : : */
440 : : gchar*
441 : 8 : g_strnfill (gsize length,
442 : : gchar fill_char)
443 : : {
444 : : gchar *str;
445 : :
446 : 8 : g_return_val_if_fail (length < G_MAXSIZE, NULL);
447 : :
448 : 6 : str = g_new (gchar, length + 1);
449 : 6 : memset (str, (guchar)fill_char, length);
450 : 6 : str[length] = '\0';
451 : :
452 : 6 : return str;
453 : 3 : }
454 : :
455 : : /**
456 : : * g_stpcpy:
457 : : * @dest: destination buffer
458 : : * @src: source string
459 : : *
460 : : * Copies a nul-terminated string into the destination buffer, including
461 : : * the trailing nul byte, and returns a pointer to the trailing nul byte
462 : : * in `dest`. The return value is useful for concatenating multiple
463 : : * strings without having to repeatedly scan for the end.
464 : : *
465 : : * Returns: a pointer to the trailing nul byte in `dest`
466 : : **/
467 : : gchar *
468 : 1517455 : g_stpcpy (gchar *dest,
469 : : const gchar *src)
470 : : {
471 : : #ifdef HAVE_STPCPY
472 : 888788 : g_return_val_if_fail (dest != NULL, NULL);
473 : 888787 : g_return_val_if_fail (src != NULL, NULL);
474 : 888786 : return stpcpy (dest, src);
475 : : #else
476 : 628667 : gchar *d = dest;
477 : 628667 : const gchar *s = src;
478 : :
479 : 628667 : g_return_val_if_fail (dest != NULL, NULL);
480 : 628666 : g_return_val_if_fail (src != NULL, NULL);
481 : 628665 : do
482 : 4837994 : *d++ = *s;
483 : 4837994 : while (*s++ != '\0');
484 : :
485 : 628665 : return d - 1;
486 : : #endif
487 : 628667 : }
488 : :
489 : : /**
490 : : * g_strdup_vprintf:
491 : : * @format: (not nullable): a standard `printf()` format string, but notice
492 : : * [string precision pitfalls](string-utils.html#string-precision-pitfalls)
493 : : * @args: the list of parameters to insert into the format string
494 : : *
495 : : * Similar to the standard C `vsprintf()` function but safer, since it
496 : : * calculates the maximum space required and allocates memory to hold
497 : : * the result.
498 : : *
499 : : * The returned string is guaranteed to be non-NULL, unless @format
500 : : * contains `%lc` or `%ls` conversions, which can fail if no multibyte
501 : : * representation is available for the given character.
502 : : *
503 : : * See also [func@GLib.vasprintf], which offers the same functionality, but
504 : : * additionally returns the length of the allocated string.
505 : : *
506 : : * Returns: (nullable) (transfer full): a newly-allocated string holding the
507 : : * result
508 : : */
509 : : gchar*
510 : 7619060 : g_strdup_vprintf (const gchar *format,
511 : : va_list args)
512 : : {
513 : 7619060 : gchar *string = NULL;
514 : :
515 : 7619060 : g_vasprintf (&string, format, args);
516 : :
517 : 7619060 : return string;
518 : : }
519 : :
520 : : /**
521 : : * g_strdup_printf:
522 : : * @format: (not nullable): a standard `printf()` format string, but notice
523 : : * [string precision pitfalls](string-utils.html#string-precision-pitfalls)
524 : : * @...: the parameters to insert into the format string
525 : : *
526 : : * Similar to the standard C `sprintf()` function but safer, since it
527 : : * calculates the maximum space required and allocates memory to hold
528 : : * the result.
529 : : *
530 : : * The returned string is guaranteed to be non-NULL, unless @format
531 : : * contains `%lc` or `%ls` conversions, which can fail if no multibyte
532 : : * representation is available for the given character.
533 : : *
534 : : * Returns: (nullable) (transfer full): a newly-allocated string holding the
535 : : * result
536 : : */
537 : : gchar*
538 : 395416 : g_strdup_printf (const gchar *format,
539 : : ...)
540 : : {
541 : : gchar *buffer;
542 : : va_list args;
543 : :
544 : 395416 : va_start (args, format);
545 : 395416 : buffer = g_strdup_vprintf (format, args);
546 : 395416 : va_end (args);
547 : :
548 : 395416 : return buffer;
549 : : }
550 : :
551 : : /**
552 : : * g_strconcat:
553 : : * @string1: the first string to add, which must not be `NULL`
554 : : * @...: a `NULL`-terminated list of strings to append to the string
555 : : *
556 : : * Concatenates all of the given strings into one long string.
557 : : *
558 : : * The variable argument list must end with `NULL`. If you forget the `NULL`,
559 : : * `g_strconcat()` will start appending random memory junk to your string.
560 : : *
561 : : * Note that this function is usually not the right function to use to
562 : : * assemble a translated message from pieces, since proper translation
563 : : * often requires the pieces to be reordered.
564 : : *
565 : : * Returns: a newly-allocated string containing all the string arguments
566 : : */
567 : : gchar*
568 : 174464 : g_strconcat (const gchar *string1, ...)
569 : : {
570 : : gsize l;
571 : : va_list args;
572 : : gchar *s;
573 : : gchar *concat;
574 : : gchar *ptr;
575 : :
576 : 174464 : if (!string1)
577 : 2 : return NULL;
578 : :
579 : 174462 : l = 1 + strlen (string1);
580 : 174462 : va_start (args, string1);
581 : 174462 : s = va_arg (args, gchar*);
582 : 461467 : while (s)
583 : : {
584 : 287005 : if (!g_size_checked_add (&l, l, strlen (s)))
585 : 0 : g_error ("%s: overflow concatenating strings", G_STRLOC);
586 : 287005 : s = va_arg (args, gchar*);
587 : : }
588 : 174462 : va_end (args);
589 : :
590 : 174462 : concat = g_new (gchar, l);
591 : 174462 : ptr = concat;
592 : :
593 : 174462 : ptr = g_stpcpy (ptr, string1);
594 : 174462 : va_start (args, string1);
595 : 174462 : s = va_arg (args, gchar*);
596 : 461467 : while (s)
597 : : {
598 : 287005 : ptr = g_stpcpy (ptr, s);
599 : 287005 : s = va_arg (args, gchar*);
600 : : }
601 : 174462 : va_end (args);
602 : :
603 : 174462 : return concat;
604 : 44674 : }
605 : :
606 : : /**
607 : : * g_strtod:
608 : : * @nptr: the string to convert to a numeric value
609 : : * @endptr: (out) (transfer none) (optional): if non-`NULL`, it returns the
610 : : * character after the last character used in the conversion
611 : : *
612 : : * Converts a string to a floating point value.
613 : : *
614 : : * It calls the standard `strtod()` function to handle the conversion, but
615 : : * if the string is not completely converted it attempts the conversion
616 : : * again with [func@GLib.ascii_strtod], and returns the best match.
617 : : *
618 : : * This function should seldom be used. The normal situation when reading
619 : : * numbers not for human consumption is to use [func@GLib.ascii_strtod]. Only when
620 : : * you know that you must expect both locale formatted and C formatted numbers
621 : : * should you use this. Make sure that you don't pass strings such as comma
622 : : * separated lists of values, since the commas may be interpreted as a decimal
623 : : * point in some locales, causing unexpected results.
624 : : *
625 : : * Returns: the converted value
626 : : **/
627 : : gdouble
628 : 34 : g_strtod (const gchar *nptr,
629 : : gchar **endptr)
630 : : {
631 : : gchar *fail_pos_1;
632 : : gchar *fail_pos_2;
633 : : gdouble val_1;
634 : 34 : gdouble val_2 = 0;
635 : :
636 : 34 : g_return_val_if_fail (nptr != NULL, 0);
637 : :
638 : 32 : fail_pos_1 = NULL;
639 : 32 : fail_pos_2 = NULL;
640 : :
641 : 32 : val_1 = strtod (nptr, &fail_pos_1);
642 : :
643 : 32 : if (fail_pos_1 && fail_pos_1[0] != 0)
644 : 6 : val_2 = g_ascii_strtod (nptr, &fail_pos_2);
645 : :
646 : 32 : if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
647 : : {
648 : 32 : if (endptr)
649 : 24 : *endptr = fail_pos_1;
650 : 32 : return val_1;
651 : : }
652 : : else
653 : : {
654 : 0 : if (endptr)
655 : 0 : *endptr = fail_pos_2;
656 : 0 : return val_2;
657 : : }
658 : 17 : }
659 : :
660 : : /**
661 : : * g_ascii_strtod:
662 : : * @nptr: the string to convert to a numeric value
663 : : * @endptr: (out) (transfer none) (optional): if non-`NULL`, it returns the
664 : : * character after the last character used in the conversion
665 : : *
666 : : * Converts a string to a floating point value.
667 : : *
668 : : * This function behaves like the standard `strtod()` function
669 : : * does in the C locale. It does this without actually changing
670 : : * the current locale, since that would not be thread-safe.
671 : : * A limitation of the implementation is that this function
672 : : * will still accept localized versions of infinities and NANs.
673 : : *
674 : : * This function is typically used when reading configuration
675 : : * files or other non-user input that should be locale independent.
676 : : * To handle input from the user you should normally use the
677 : : * locale-sensitive system `strtod()` function.
678 : : *
679 : : * To convert from a gdouble to a string in a locale-insensitive
680 : : * way, use [func@GLib.ascii_dtostr].
681 : : *
682 : : * If the correct value would cause overflow, plus or minus `HUGE_VAL`
683 : : * is returned (according to the sign of the value), and `ERANGE` is
684 : : * stored in `errno`. If the correct value would cause underflow, a value
685 : : * whose magnitude is no greater than the smallest normalised positive number
686 : : * is returned; whether `ERANGE` is set is implementation-defined (it may
687 : : * not be set for gradual underflow where a subnormal value is returned).
688 : : *
689 : : * This function resets `errno` before calling `strtod()` so that
690 : : * you can reliably detect overflow and underflow.
691 : : *
692 : : * Returns: the converted value
693 : : */
694 : : gdouble
695 : 11914 : g_ascii_strtod (const gchar *nptr,
696 : : gchar **endptr)
697 : : {
698 : : #if defined(USE_XLOCALE) && defined(HAVE_STRTOD_L)
699 : : locale_t c_locale;
700 : :
701 : 1519 : g_return_val_if_fail (nptr != NULL, 0);
702 : :
703 : 1518 : c_locale = get_C_locale ();
704 : 1518 : errno = 0;
705 : :
706 : 1518 : return strtod_l (nptr, endptr, c_locale);
707 : :
708 : : #else
709 : :
710 : : gchar *fail_pos;
711 : : gdouble val;
712 : : #ifndef __BIONIC__
713 : : struct lconv *locale_data;
714 : : #endif
715 : : const char *decimal_point;
716 : : gsize decimal_point_len;
717 : : const char *p, *decimal_point_pos;
718 : 10395 : const char *end = NULL; /* Silence gcc */
719 : : int strtod_errno;
720 : :
721 : 10395 : g_return_val_if_fail (nptr != NULL, 0);
722 : :
723 : 10394 : fail_pos = NULL;
724 : :
725 : : #ifndef __BIONIC__
726 : 10394 : locale_data = localeconv ();
727 : 10394 : decimal_point = locale_data->decimal_point;
728 : 10394 : decimal_point_len = strlen (decimal_point);
729 : : #else
730 : : decimal_point = ".";
731 : : decimal_point_len = 1;
732 : : #endif
733 : :
734 : 10394 : g_assert (decimal_point_len != 0);
735 : :
736 : 10394 : decimal_point_pos = NULL;
737 : 10394 : end = NULL;
738 : :
739 : 10394 : if (decimal_point[0] != '.' ||
740 : 10307 : decimal_point[1] != 0)
741 : : {
742 : 87 : p = nptr;
743 : : /* Skip leading space */
744 : 108 : while (g_ascii_isspace (*p))
745 : 21 : p++;
746 : :
747 : : /* Skip leading optional sign */
748 : 87 : if (*p == '+' || *p == '-')
749 : 26 : p++;
750 : :
751 : 108 : if (p[0] == '0' &&
752 : 36 : (p[1] == 'x' || p[1] == 'X'))
753 : : {
754 : 15 : p += 2;
755 : : /* HEX - find the (optional) decimal point */
756 : :
757 : 30 : while (g_ascii_isxdigit (*p))
758 : 15 : p++;
759 : :
760 : 15 : if (*p == '.')
761 : 15 : decimal_point_pos = p++;
762 : :
763 : 30 : while (g_ascii_isxdigit (*p))
764 : 15 : p++;
765 : :
766 : 15 : if (*p == 'p' || *p == 'P')
767 : 9 : p++;
768 : 15 : if (*p == '+' || *p == '-')
769 : 6 : p++;
770 : 24 : while (g_ascii_isdigit (*p))
771 : 9 : p++;
772 : :
773 : 15 : end = p;
774 : 15 : }
775 : 72 : else if (g_ascii_isdigit (*p) || *p == '.')
776 : : {
777 : 153 : while (g_ascii_isdigit (*p))
778 : 93 : p++;
779 : :
780 : 60 : if (*p == '.')
781 : 53 : decimal_point_pos = p++;
782 : :
783 : 233 : while (g_ascii_isdigit (*p))
784 : 173 : p++;
785 : :
786 : 60 : if (*p == 'e' || *p == 'E')
787 : 20 : p++;
788 : 60 : if (*p == '+' || *p == '-')
789 : 11 : p++;
790 : 90 : while (g_ascii_isdigit (*p))
791 : 30 : p++;
792 : :
793 : 60 : end = p;
794 : 60 : }
795 : : /* For the other cases, we need not convert the decimal point */
796 : 87 : }
797 : :
798 : 10394 : if (decimal_point_pos)
799 : : {
800 : : char *copy, *c;
801 : :
802 : : /* We need to convert the '.' to the locale specific decimal point */
803 : 68 : copy = g_malloc (end - nptr + 1 + decimal_point_len);
804 : :
805 : 68 : c = copy;
806 : 68 : memcpy (c, nptr, decimal_point_pos - nptr);
807 : 68 : c += decimal_point_pos - nptr;
808 : 68 : memcpy (c, decimal_point, decimal_point_len);
809 : 68 : c += decimal_point_len;
810 : 68 : memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
811 : 68 : c += end - (decimal_point_pos + 1);
812 : 68 : *c = 0;
813 : :
814 : 68 : errno = 0;
815 : 68 : val = strtod (copy, &fail_pos);
816 : 68 : strtod_errno = errno;
817 : :
818 : 68 : if (fail_pos)
819 : : {
820 : 68 : if (fail_pos - copy > decimal_point_pos - nptr)
821 : 68 : fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
822 : : else
823 : 0 : fail_pos = (char *)nptr + (fail_pos - copy);
824 : 68 : }
825 : :
826 : 68 : g_free (copy);
827 : :
828 : 68 : }
829 : 10326 : else if (end)
830 : : {
831 : : char *copy;
832 : :
833 : 7 : copy = g_malloc (end - (char *)nptr + 1);
834 : 7 : memcpy (copy, nptr, end - nptr);
835 : 7 : *(copy + (end - (char *)nptr)) = 0;
836 : :
837 : 7 : errno = 0;
838 : 7 : val = strtod (copy, &fail_pos);
839 : 7 : strtod_errno = errno;
840 : :
841 : 7 : if (fail_pos)
842 : : {
843 : 7 : fail_pos = (char *)nptr + (fail_pos - copy);
844 : 7 : }
845 : :
846 : 7 : g_free (copy);
847 : 7 : }
848 : : else
849 : : {
850 : 10319 : errno = 0;
851 : 10319 : val = strtod (nptr, &fail_pos);
852 : 10319 : strtod_errno = errno;
853 : : }
854 : :
855 : 10394 : if (endptr)
856 : 10345 : *endptr = fail_pos;
857 : :
858 : 10394 : errno = strtod_errno;
859 : :
860 : 10394 : return val;
861 : : #endif
862 : 10395 : }
863 : :
864 : :
865 : : /**
866 : : * g_ascii_dtostr:
867 : : * @buffer: a buffer to place the resulting string in
868 : : * @buf_len: the length of the buffer
869 : : * @d: the value to convert
870 : : *
871 : : * Converts a `gdouble` to a string, using the '.' as
872 : : * decimal point.
873 : : *
874 : : * This function generates enough precision that converting
875 : : * the string back using [func@GLib.ascii_strtod] gives the same machine-number
876 : : * (on machines with IEEE compatible 64bit doubles). It is
877 : : * guaranteed that the size of the resulting string will never
878 : : * be larger than [const@GLib.ASCII_DTOSTR_BUF_SIZE] bytes, including the terminating
879 : : * nul character, which is always added.
880 : : *
881 : : * Returns: the pointer to the buffer with the converted string
882 : : **/
883 : : gchar *
884 : 211623 : g_ascii_dtostr (gchar *buffer,
885 : : gint buf_len,
886 : : gdouble d)
887 : : {
888 : 211623 : return g_ascii_formatd (buffer, buf_len, "%.17g", d);
889 : : }
890 : :
891 : : #pragma GCC diagnostic push
892 : : #pragma GCC diagnostic ignored "-Wformat-nonliteral"
893 : :
894 : : /**
895 : : * g_ascii_formatd:
896 : : * @buffer: a buffer to place the resulting string in
897 : : * @buf_len: the length of the buffer
898 : : * @format: the `printf()`-style format to use for the
899 : : * code to use for converting
900 : : * @d: the value to convert
901 : : *
902 : : * Converts a `gdouble` to a string, using the '.' as
903 : : * decimal point. To format the number you pass in
904 : : * a `printf()`-style format string. Allowed conversion
905 : : * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'.
906 : : *
907 : : * The @format must just be a single format specifier
908 : : * starting with `%`, expecting a `gdouble` argument.
909 : : *
910 : : * The returned buffer is guaranteed to be nul-terminated.
911 : : *
912 : : * If you just want to want to serialize the value into a
913 : : * string, use [func@GLib.ascii_dtostr].
914 : : *
915 : : * Returns: the pointer to the buffer with the converted string
916 : : */
917 : : gchar *
918 : 211673 : g_ascii_formatd (gchar *buffer,
919 : : gint buf_len,
920 : : const gchar *format,
921 : : gdouble d)
922 : : {
923 : : #ifdef USE_XLOCALE
924 : : locale_t old_locale;
925 : :
926 : 158232 : g_return_val_if_fail (buffer != NULL, NULL);
927 : 158232 : g_return_val_if_fail (format[0] == '%', NULL);
928 : 158232 : g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
929 : :
930 : 158232 : old_locale = uselocale (get_C_locale ());
931 : 158232 : _g_snprintf (buffer, buf_len, format, d);
932 : 158232 : uselocale (old_locale);
933 : :
934 : 158232 : return buffer;
935 : : #else
936 : : #ifndef __BIONIC__
937 : : struct lconv *locale_data;
938 : : #endif
939 : : const char *decimal_point;
940 : : gsize decimal_point_len;
941 : : gchar *p;
942 : : size_t rest_len;
943 : : gchar format_char;
944 : :
945 : 53441 : g_return_val_if_fail (buffer != NULL, NULL);
946 : 53441 : g_return_val_if_fail (format[0] == '%', NULL);
947 : 53441 : g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
948 : :
949 : 53441 : format_char = format[strlen (format) - 1];
950 : :
951 : 106877 : g_return_val_if_fail (format_char == 'e' || format_char == 'E' ||
952 : 53436 : format_char == 'f' || format_char == 'F' ||
953 : 53416 : format_char == 'g' || format_char == 'G',
954 : : NULL);
955 : :
956 : 53441 : if (format[0] != '%')
957 : 0 : return NULL;
958 : :
959 : 53441 : if (strpbrk (format + 1, "'l%"))
960 : 0 : return NULL;
961 : :
962 : 53441 : if (!(format_char == 'e' || format_char == 'E' ||
963 : 53436 : format_char == 'f' || format_char == 'F' ||
964 : 53416 : format_char == 'g' || format_char == 'G'))
965 : 0 : return NULL;
966 : :
967 : 53441 : _g_snprintf (buffer, buf_len, format, d);
968 : :
969 : : #ifndef __BIONIC__
970 : 53441 : locale_data = localeconv ();
971 : 53441 : decimal_point = locale_data->decimal_point;
972 : 53441 : decimal_point_len = strlen (decimal_point);
973 : : #else
974 : : decimal_point = ".";
975 : : decimal_point_len = 1;
976 : : #endif
977 : :
978 : 53441 : g_assert (decimal_point_len != 0);
979 : :
980 : 53441 : if (decimal_point[0] != '.' ||
981 : 53422 : decimal_point[1] != 0)
982 : : {
983 : 19 : p = buffer;
984 : :
985 : 22 : while (g_ascii_isspace (*p))
986 : 3 : p++;
987 : :
988 : 19 : if (*p == '+' || *p == '-')
989 : 8 : p++;
990 : :
991 : 38 : while (isdigit ((guchar)*p))
992 : 19 : p++;
993 : :
994 : 19 : if (strncmp (p, decimal_point, decimal_point_len) == 0)
995 : : {
996 : 16 : *p = '.';
997 : 16 : p++;
998 : 16 : if (decimal_point_len > 1)
999 : : {
1000 : 0 : rest_len = strlen (p + (decimal_point_len - 1));
1001 : 0 : memmove (p, p + (decimal_point_len - 1), rest_len);
1002 : 0 : p[rest_len] = 0;
1003 : 0 : }
1004 : 16 : }
1005 : 19 : }
1006 : :
1007 : 53441 : return buffer;
1008 : : #endif
1009 : 53441 : }
1010 : : #pragma GCC diagnostic pop
1011 : :
1012 : : #define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
1013 : : (c) == '\r' || (c) == '\t' || (c) == '\v')
1014 : : #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
1015 : : #define ISLOWER(c) ((c) >= 'a' && (c) <= 'z')
1016 : : #define ISALPHA(c) (ISUPPER (c) || ISLOWER (c))
1017 : : #define TOUPPER(c) (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
1018 : : #define TOLOWER(c) (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
1019 : :
1020 : : #if !defined(USE_XLOCALE) || !defined(HAVE_STRTOULL_L) || !defined(HAVE_STRTOLL_L)
1021 : :
1022 : : static guint64
1023 : 70479 : g_parse_long_long (const gchar *nptr,
1024 : : const gchar **endptr,
1025 : : guint base,
1026 : : gboolean *negative)
1027 : : {
1028 : : /* this code is based on the strtol(3) code from GNU libc released under
1029 : : * the GNU Lesser General Public License.
1030 : : *
1031 : : * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
1032 : : * Free Software Foundation, Inc.
1033 : : */
1034 : : gboolean overflow;
1035 : : guint64 cutoff;
1036 : : guint64 cutlim;
1037 : : guint64 ui64;
1038 : : const gchar *s, *save;
1039 : : guchar c;
1040 : :
1041 : 70479 : g_return_val_if_fail (nptr != NULL, 0);
1042 : :
1043 : 70479 : *negative = FALSE;
1044 : 70479 : if (base == 1 || base > 36)
1045 : : {
1046 : 0 : errno = EINVAL;
1047 : 0 : if (endptr)
1048 : 0 : *endptr = nptr;
1049 : 0 : return 0;
1050 : : }
1051 : :
1052 : 70479 : save = s = nptr;
1053 : :
1054 : : /* Skip white space. */
1055 : 70481 : while (ISSPACE (*s))
1056 : 2 : ++s;
1057 : :
1058 : 70479 : if (G_UNLIKELY (!*s))
1059 : 0 : goto noconv;
1060 : :
1061 : : /* Check for a sign. */
1062 : 70479 : if (*s == '-')
1063 : : {
1064 : 85 : *negative = TRUE;
1065 : 85 : ++s;
1066 : 85 : }
1067 : 70394 : else if (*s == '+')
1068 : 13 : ++s;
1069 : :
1070 : : /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
1071 : 70479 : if (*s == '0')
1072 : : {
1073 : 10379 : if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
1074 : : {
1075 : 7630 : s += 2;
1076 : 7630 : base = 16;
1077 : 7630 : }
1078 : 2749 : else if (base == 0)
1079 : 43 : base = 8;
1080 : 10379 : }
1081 : 60100 : else if (base == 0)
1082 : 18434 : base = 10;
1083 : :
1084 : : /* Save the pointer so we can check later if anything happened. */
1085 : 70479 : save = s;
1086 : 70479 : cutoff = G_MAXUINT64 / base;
1087 : 70479 : cutlim = G_MAXUINT64 % base;
1088 : :
1089 : 70479 : overflow = FALSE;
1090 : 70479 : ui64 = 0;
1091 : 70479 : c = *s;
1092 : 621786 : for (; c; c = *++s)
1093 : : {
1094 : 551352 : if (c >= '0' && c <= '9')
1095 : 454466 : c -= '0';
1096 : 96886 : else if (ISALPHA (c))
1097 : 96855 : c = TOUPPER (c) - 'A' + 10;
1098 : : else
1099 : 31 : break;
1100 : 551321 : if (c >= base)
1101 : 14 : break;
1102 : : /* Check for overflow. */
1103 : 551307 : if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
1104 : 91 : overflow = TRUE;
1105 : : else
1106 : : {
1107 : 551216 : ui64 *= base;
1108 : 551216 : ui64 += c;
1109 : : }
1110 : 551307 : }
1111 : :
1112 : : /* Check if anything actually happened. */
1113 : 70479 : if (s == save)
1114 : 9 : goto noconv;
1115 : :
1116 : : /* Store in ENDPTR the address of one character
1117 : : past the last character we converted. */
1118 : 70470 : if (endptr)
1119 : 57686 : *endptr = s;
1120 : :
1121 : 70470 : if (G_UNLIKELY (overflow))
1122 : : {
1123 : 12 : errno = ERANGE;
1124 : 12 : return G_MAXUINT64;
1125 : : }
1126 : :
1127 : 70458 : return ui64;
1128 : :
1129 : : noconv:
1130 : : /* We must handle a special case here: the base is 0 or 16 and the
1131 : : first two characters are '0' and 'x', but the rest are no
1132 : : hexadecimal digits. This is no error case. We return 0 and
1133 : : ENDPTR points to the `x`. */
1134 : 9 : if (endptr)
1135 : : {
1136 : 9 : if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
1137 : 0 : && save[-2] == '0')
1138 : 0 : *endptr = &save[-1];
1139 : : else
1140 : : /* There was no number to convert. */
1141 : 9 : *endptr = nptr;
1142 : 9 : }
1143 : 9 : return 0;
1144 : 70479 : }
1145 : : #endif /* !defined(USE_XLOCALE) || !defined(HAVE_STRTOULL_L) || !defined(HAVE_STRTOLL_L) */
1146 : :
1147 : : /**
1148 : : * g_ascii_strtoull:
1149 : : * @nptr: the string to convert to a numeric value
1150 : : * @endptr: (out) (transfer none) (optional): if non-`NULL`, it returns the
1151 : : * character after the last character used in the conversion
1152 : : * @base: to be used for the conversion, 2..36 or 0
1153 : : *
1154 : : * Converts a string to a `guint64` value.
1155 : : *
1156 : : * This function behaves like the standard `strtoull()` function
1157 : : * does in the C locale. It does this without actually
1158 : : * changing the current locale, since that would not be
1159 : : * thread-safe.
1160 : : *
1161 : : * Note that input with a leading minus sign (`-`) is accepted, and will return
1162 : : * the negation of the parsed number, unless that would overflow a `guint64`.
1163 : : * Critically, this means you cannot assume that a short fixed length input will
1164 : : * result in a low return value, as the input could have a leading `-`.
1165 : : *
1166 : : * This function is typically used when reading configuration
1167 : : * files or other non-user input that should be locale independent.
1168 : : * To handle input from the user you should normally use the
1169 : : * locale-sensitive system `strtoull()` function.
1170 : : *
1171 : : * If the correct value would cause overflow, [const@GLib.MAXUINT64]
1172 : : * is returned, and `ERANGE` is stored in `errno`.
1173 : : * If the base is outside the valid range, zero is returned, and
1174 : : * `EINVAL` is stored in `errno`.
1175 : : * If the string conversion fails, zero is returned, and @endptr returns
1176 : : * @nptr (if @endptr is non-`NULL`).
1177 : : *
1178 : : * Returns: the converted value, or zero on error
1179 : : *
1180 : : * Since: 2.2
1181 : : */
1182 : : guint64
1183 : 154812 : g_ascii_strtoull (const gchar *nptr,
1184 : : gchar **endptr,
1185 : : guint base)
1186 : : {
1187 : : #if defined(USE_XLOCALE) && defined(HAVE_STRTOULL_L)
1188 : 97206 : locale_t c_locale = get_C_locale ();
1189 : :
1190 : 97206 : errno = 0;
1191 : 97206 : return strtoull_l (nptr, endptr, base, c_locale);
1192 : : #else
1193 : : gboolean negative;
1194 : : guint64 result;
1195 : :
1196 : 57606 : result = g_parse_long_long (nptr, (const gchar **) endptr, base, &negative);
1197 : :
1198 : : /* Return the result of the appropriate sign. */
1199 : 57606 : return negative ? -result : result;
1200 : : #endif
1201 : : }
1202 : :
1203 : : /**
1204 : : * g_ascii_strtoll:
1205 : : * @nptr: the string to convert to a numeric value
1206 : : * @endptr: (out) (transfer none) (optional): if non-`NULL`, it returns the
1207 : : * character after the last character used in the conversion
1208 : : * @base: to be used for the conversion, 2..36 or 0
1209 : : *
1210 : : * Converts a string to a `gint64` value.
1211 : : *
1212 : : * This function behaves like the standard `strtoll()` function
1213 : : * does in the C locale. It does this without actually
1214 : : * changing the current locale, since that would not be
1215 : : * thread-safe.
1216 : : *
1217 : : * This function is typically used when reading configuration
1218 : : * files or other non-user input that should be locale independent.
1219 : : * To handle input from the user you should normally use the
1220 : : * locale-sensitive system `strtoll()` function.
1221 : : *
1222 : : * If the correct value would cause overflow, [const@GLib.MAXINT64] or
1223 : : * [const@GLib.MININT64] is returned, and `ERANGE` is stored in `errno`.
1224 : : * If the base is outside the valid range, zero is returned, and
1225 : : * `EINVAL` is stored in `errno`. If the
1226 : : * string conversion fails, zero is returned, and @endptr returns @nptr
1227 : : * (if @endptr is non-`NULL`).
1228 : : *
1229 : : * Returns: the converted value, or zero on error
1230 : : *
1231 : : * Since: 2.12
1232 : : */
1233 : : gint64
1234 : 26101 : g_ascii_strtoll (const gchar *nptr,
1235 : : gchar **endptr,
1236 : : guint base)
1237 : : {
1238 : : #if defined(USE_XLOCALE) && defined(HAVE_STRTOLL_L)
1239 : 13228 : locale_t c_locale = get_C_locale ();
1240 : :
1241 : 13228 : errno = 0;
1242 : 13228 : return strtoll_l (nptr, endptr, base, c_locale);
1243 : : #else
1244 : : gboolean negative;
1245 : : guint64 result;
1246 : :
1247 : 12873 : result = g_parse_long_long (nptr, (const gchar **) endptr, base, &negative);
1248 : :
1249 : 12873 : if (negative && result > (guint64) G_MININT64)
1250 : : {
1251 : 2 : errno = ERANGE;
1252 : 2 : return G_MININT64;
1253 : : }
1254 : 12871 : else if (!negative && result > (guint64) G_MAXINT64)
1255 : : {
1256 : 3 : errno = ERANGE;
1257 : 3 : return G_MAXINT64;
1258 : : }
1259 : 12868 : else if (negative)
1260 : 74 : return (result == (guint64) G_MININT64) ? G_MININT64 : -(gint64) result;
1261 : : else
1262 : 12794 : return (gint64) result;
1263 : : #endif
1264 : 12873 : }
1265 : :
1266 : : /**
1267 : : * g_strerror:
1268 : : * @errnum: the system error number. See the standard C `errno` documentation
1269 : : *
1270 : : * Returns a string corresponding to the given error code, e.g. "no
1271 : : * such process".
1272 : : *
1273 : : * Unlike `strerror()`, this always returns a string in
1274 : : * UTF-8 encoding, and the pointer is guaranteed to remain valid for
1275 : : * the lifetime of the process. If the error code is unknown, it returns a
1276 : : * string like “Unknown error <code\>”.
1277 : : *
1278 : : * Note that the string may be translated according to the current locale.
1279 : : *
1280 : : * The value of `errno` will not be changed by this function. However, it may
1281 : : * be changed by intermediate function calls, so you should save its value
1282 : : * as soon as the call returns:
1283 : : * ```C
1284 : : * int saved_errno;
1285 : : *
1286 : : * ret = read (blah);
1287 : : * saved_errno = errno;
1288 : : *
1289 : : * g_strerror (saved_errno);
1290 : : * ```
1291 : : *
1292 : : * Returns: the string describing the error code
1293 : : */
1294 : : const gchar *
1295 : 6496 : g_strerror (gint errnum)
1296 : : {
1297 : : static GHashTable *errors;
1298 : : G_LOCK_DEFINE_STATIC (errors);
1299 : : const gchar *msg;
1300 : 6496 : gint saved_errno = errno;
1301 : :
1302 : 6496 : G_LOCK (errors);
1303 : 6496 : if (errors)
1304 : 6237 : msg = g_hash_table_lookup (errors, GINT_TO_POINTER (errnum));
1305 : : else
1306 : : {
1307 : 259 : errors = g_hash_table_new (NULL, NULL);
1308 : 259 : msg = NULL;
1309 : : }
1310 : :
1311 : 6496 : if (!msg)
1312 : : {
1313 : : gchar buf[1024];
1314 : 781 : GError *error = NULL;
1315 : : #if defined(HAVE_STRERROR_R) && !defined(STRERROR_R_CHAR_P)
1316 : : int ret;
1317 : : #endif
1318 : :
1319 : : #if defined(G_OS_WIN32)
1320 : 232 : strerror_s (buf, sizeof (buf), errnum);
1321 : 232 : msg = buf;
1322 : : #elif defined(HAVE_STRERROR_R)
1323 : : /* Match the condition in strerror_r(3) for glibc */
1324 : : # if defined(STRERROR_R_CHAR_P)
1325 : 549 : msg = strerror_r (errnum, buf, sizeof (buf));
1326 : : # else
1327 : : ret = strerror_r (errnum, buf, sizeof (buf));
1328 : : if (ret == 0 || ret == EINVAL)
1329 : : msg = buf;
1330 : : # endif /* HAVE_STRERROR_R */
1331 : : #else
1332 : : g_strlcpy (buf, strerror (errnum), sizeof (buf));
1333 : : msg = buf;
1334 : : #endif
1335 : :
1336 : 781 : if (!msg)
1337 : : {
1338 : 0 : G_UNLOCK (errors);
1339 : :
1340 : 0 : errno = saved_errno;
1341 : 0 : return NULL;
1342 : : }
1343 : :
1344 : 781 : if (!g_get_console_charset (NULL))
1345 : : {
1346 : 516 : msg = g_locale_to_utf8 (msg, -1, NULL, NULL, &error);
1347 : 516 : if (error)
1348 : : {
1349 : 0 : g_print ("%s\n", error->message);
1350 : 0 : g_error_free (error);
1351 : 0 : }
1352 : 0 : }
1353 : 265 : else if (msg == (const gchar *)buf)
1354 : 232 : msg = g_strdup (buf);
1355 : :
1356 : 781 : g_hash_table_insert (errors, GINT_TO_POINTER (errnum), (char *) msg);
1357 : 232 : }
1358 : 6496 : G_UNLOCK (errors);
1359 : :
1360 : 6496 : errno = saved_errno;
1361 : 6496 : return msg;
1362 : 312 : }
1363 : :
1364 : : /**
1365 : : * g_strsignal:
1366 : : * @signum: the signal number. See the `signal` documentation
1367 : : *
1368 : : * Returns a string describing the given signal, e.g. "Segmentation fault".
1369 : : * If the signal is unknown, it returns “unknown signal (<signum\>)”.
1370 : : *
1371 : : * You should use this function in preference to `strsignal()`, because it
1372 : : * returns a string in UTF-8 encoding, and since not all platforms support
1373 : : * the `strsignal()` function.
1374 : : *
1375 : : * Returns: the string describing the signal
1376 : : */
1377 : : const gchar *
1378 : 38 : g_strsignal (gint signum)
1379 : : {
1380 : : gchar *msg;
1381 : : gchar *tofree;
1382 : : const gchar *ret;
1383 : :
1384 : 38 : msg = tofree = NULL;
1385 : :
1386 : : #ifdef HAVE_STRSIGNAL
1387 : 19 : msg = strsignal (signum);
1388 : 19 : if (!g_get_console_charset (NULL))
1389 : 19 : msg = tofree = g_locale_to_utf8 (msg, -1, NULL, NULL, NULL);
1390 : : #endif
1391 : :
1392 : 38 : if (!msg)
1393 : 19 : msg = tofree = g_strdup_printf ("unknown signal (%d)", signum);
1394 : 38 : ret = g_intern_string (msg);
1395 : 38 : g_free (tofree);
1396 : :
1397 : 38 : return ret;
1398 : : }
1399 : :
1400 : : /* Functions g_strlcpy and g_strlcat were originally developed by
1401 : : * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
1402 : : * See http://www.openbsd.org/cgi-bin/man.cgi?query=strlcpy
1403 : : * for more information.
1404 : : */
1405 : :
1406 : : #ifdef HAVE_STRLCPY
1407 : : /* Use the native ones, if available; they might be implemented in assembly */
1408 : : gsize
1409 : 14573 : g_strlcpy (gchar *dest,
1410 : : const gchar *src,
1411 : : gsize dest_size)
1412 : : {
1413 : 14573 : g_return_val_if_fail (dest != NULL, 0);
1414 : 14572 : g_return_val_if_fail (src != NULL, 0);
1415 : :
1416 : 14571 : return strlcpy (dest, src, dest_size);
1417 : : }
1418 : :
1419 : : gsize
1420 : 8 : g_strlcat (gchar *dest,
1421 : : const gchar *src,
1422 : : gsize dest_size)
1423 : : {
1424 : 8 : g_return_val_if_fail (dest != NULL, 0);
1425 : 7 : g_return_val_if_fail (src != NULL, 0);
1426 : :
1427 : 6 : return strlcat (dest, src, dest_size);
1428 : : }
1429 : :
1430 : : #else /* ! HAVE_STRLCPY */
1431 : : /**
1432 : : * g_strlcpy:
1433 : : * @dest: destination buffer
1434 : : * @src: source buffer
1435 : : * @dest_size: length of @dest in bytes
1436 : : *
1437 : : * Portability wrapper that calls `strlcpy()` on systems which have it,
1438 : : * and emulates `strlcpy()` otherwise. Copies @src to @dest; @dest is
1439 : : * guaranteed to be nul-terminated; @src must be nul-terminated;
1440 : : * @dest_size is the buffer size, not the number of bytes to copy.
1441 : : *
1442 : : * At most @dest_size - 1 characters will be copied. Always nul-terminates
1443 : : * (unless @dest_size is 0). This function does not allocate memory. Unlike
1444 : : * `strncpy()`, this function doesn't pad @dest (so it's often faster). It
1445 : : * returns the size of the attempted result, `strlen (src)`, so if
1446 : : * @retval >= @dest_size, truncation occurred.
1447 : : *
1448 : : * Caveat: `strlcpy()` is supposedly more secure than `strcpy()` or `strncpy()`,
1449 : : * but if you really want to avoid screwups, [func@GLib.strdup] is an even better
1450 : : * idea.
1451 : : *
1452 : : * Returns: length of @src
1453 : : */
1454 : : gsize
1455 : 2390 : g_strlcpy (gchar *dest,
1456 : : const gchar *src,
1457 : : gsize dest_size)
1458 : : {
1459 : 2390 : gchar *d = dest;
1460 : 2390 : const gchar *s = src;
1461 : 2390 : gsize n = dest_size;
1462 : :
1463 : 2390 : g_return_val_if_fail (dest != NULL, 0);
1464 : 2389 : g_return_val_if_fail (src != NULL, 0);
1465 : :
1466 : : /* Copy as many bytes as will fit */
1467 : 2388 : if (n != 0 && --n != 0)
1468 : 2386 : do
1469 : : {
1470 : 24049 : gchar c = *s++;
1471 : :
1472 : 24049 : *d++ = c;
1473 : 24049 : if (c == 0)
1474 : 1438 : break;
1475 : 24049 : }
1476 : 22611 : while (--n != 0);
1477 : :
1478 : : /* If not enough room in dest, add NUL and traverse rest of src */
1479 : 2388 : if (n == 0)
1480 : : {
1481 : 950 : if (dest_size != 0)
1482 : 948 : *d = 0;
1483 : 4745 : while (*s++)
1484 : : ;
1485 : 950 : }
1486 : :
1487 : 2388 : return s - src - 1; /* count does not include NUL */
1488 : 2390 : }
1489 : :
1490 : : /**
1491 : : * g_strlcat:
1492 : : * @dest: destination buffer, already containing one nul-terminated string
1493 : : * @src: source buffer
1494 : : * @dest_size: length of @dest buffer in bytes (not length of existing string
1495 : : * inside @dest)
1496 : : *
1497 : : * Portability wrapper that calls `strlcat()` on systems which have it,
1498 : : * and emulates it otherwise. Appends nul-terminated @src string to @dest,
1499 : : * guaranteeing nul-termination for @dest. The total size of @dest won't
1500 : : * exceed @dest_size.
1501 : : *
1502 : : * At most @dest_size - 1 characters will be copied. Unlike `strncat()`,
1503 : : * @dest_size is the full size of dest, not the space left over. This
1504 : : * function does not allocate memory. It always nul-terminates (unless
1505 : : * @dest_size == 0 or there were no nul characters in the @dest_size
1506 : : * characters of dest to start with).
1507 : : *
1508 : : * Caveat: this is supposedly a more secure alternative to `strcat()` or
1509 : : * `strncat()`, but for real security [func@GLib.strconcat] is harder to mess up.
1510 : : *
1511 : : * Returns: size of attempted result, which is `MIN (dest_size, strlen
1512 : : * (original dest)) + strlen (src)`, so if @retval >= @dest_size,
1513 : : * truncation occurred
1514 : : */
1515 : : gsize
1516 : 8 : g_strlcat (gchar *dest,
1517 : : const gchar *src,
1518 : : gsize dest_size)
1519 : : {
1520 : 8 : gchar *d = dest;
1521 : 8 : const gchar *s = src;
1522 : 8 : gsize bytes_left = dest_size;
1523 : : gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
1524 : :
1525 : 8 : g_return_val_if_fail (dest != NULL, 0);
1526 : 7 : g_return_val_if_fail (src != NULL, 0);
1527 : :
1528 : : /* Find the end of dst and adjust bytes left but don't go past end */
1529 : 120 : while (*d != 0 && bytes_left-- != 0)
1530 : 114 : d++;
1531 : 6 : dlength = d - dest;
1532 : 6 : bytes_left = dest_size - dlength;
1533 : :
1534 : 6 : if (bytes_left == 0)
1535 : 4 : return dlength + strlen (s);
1536 : :
1537 : 88 : while (*s != 0)
1538 : : {
1539 : 86 : if (bytes_left != 1)
1540 : : {
1541 : 57 : *d++ = *s;
1542 : 57 : bytes_left--;
1543 : 57 : }
1544 : 86 : s++;
1545 : : }
1546 : 2 : *d = 0;
1547 : :
1548 : 2 : return dlength + (s - src); /* count does not include NUL */
1549 : 8 : }
1550 : : #endif /* ! HAVE_STRLCPY */
1551 : :
1552 : : /**
1553 : : * g_ascii_strdown:
1554 : : * @str: a string
1555 : : * @len: length of @str in bytes, or `-1` if @str is nul-terminated
1556 : : *
1557 : : * Converts all upper case ASCII letters to lower case ASCII letters, with
1558 : : * semantics that exactly match [func@GLib.ascii_tolower].
1559 : : *
1560 : : * Returns: a newly-allocated string, with all the upper case characters in
1561 : : * @str converted to lower case. (Note that this is unlike the old
1562 : : * [func@GLib.strdown], which modified the string in place.)
1563 : : */
1564 : : gchar*
1565 : 2982 : g_ascii_strdown (const gchar *str,
1566 : : gssize len)
1567 : : {
1568 : : gchar *result, *s;
1569 : :
1570 : 2982 : g_return_val_if_fail (str != NULL, NULL);
1571 : :
1572 : 2980 : if (len < 0)
1573 : 124 : result = g_strdup (str);
1574 : : else
1575 : 2856 : result = g_strndup (str, (gsize) len);
1576 : :
1577 : 16391 : for (s = result; *s; s++)
1578 : 13411 : *s = g_ascii_tolower (*s);
1579 : :
1580 : 2980 : return result;
1581 : 1465 : }
1582 : :
1583 : : /**
1584 : : * g_ascii_strup:
1585 : : * @str: a string
1586 : : * @len: length of @str in bytes, or `-1` if @str is nul-terminated
1587 : : *
1588 : : * Converts all lower case ASCII letters to upper case ASCII letters, with
1589 : : * semantics that exactly match [func@GLib.ascii_toupper].
1590 : : *
1591 : : * Returns: a newly-allocated string, with all the lower case characters
1592 : : * in @str converted to upper case. (Note that this is unlike the old
1593 : : * [func@GLib.strup], which modified the string in place.)
1594 : : */
1595 : : gchar*
1596 : 22 : g_ascii_strup (const gchar *str,
1597 : : gssize len)
1598 : : {
1599 : : gchar *result, *s;
1600 : :
1601 : 22 : g_return_val_if_fail (str != NULL, NULL);
1602 : :
1603 : 20 : if (len < 0)
1604 : 12 : result = g_strdup (str);
1605 : : else
1606 : 8 : result = g_strndup (str, (gsize) len);
1607 : :
1608 : 310 : for (s = result; *s; s++)
1609 : 290 : *s = g_ascii_toupper (*s);
1610 : :
1611 : 20 : return result;
1612 : 11 : }
1613 : :
1614 : : /**
1615 : : * g_strdown:
1616 : : * @string: the string to convert
1617 : : *
1618 : : * Converts a string to lower case.
1619 : : *
1620 : : * Returns: the string
1621 : : *
1622 : : * Deprecated: 2.2: This function is totally broken for the reasons discussed
1623 : : * in the [func@GLib.strncasecmp] docs — use [func@GLib.ascii_strdown] or
1624 : : * [func@GLib.utf8_strdown] instead.
1625 : : **/
1626 : : gchar*
1627 : 4 : g_strdown (gchar *string)
1628 : : {
1629 : : guchar *s;
1630 : :
1631 : 4 : g_return_val_if_fail (string != NULL, NULL);
1632 : :
1633 : 2 : s = (guchar *) string;
1634 : :
1635 : 24 : while (*s)
1636 : : {
1637 : 22 : if (isupper (*s))
1638 : 20 : *s = tolower (*s);
1639 : 22 : s++;
1640 : : }
1641 : :
1642 : 2 : return (gchar *) string;
1643 : 2 : }
1644 : :
1645 : : /**
1646 : : * g_strup:
1647 : : * @string: the string to convert
1648 : : *
1649 : : * Converts a string to upper case.
1650 : : *
1651 : : * Returns: the string
1652 : : *
1653 : : * Deprecated: 2.2: This function is totally broken for the reasons discussed
1654 : : * in the [func@GLib.strncasecmp] docs — use [func@GLib.ascii_strup] or
1655 : : * [func@GLib.utf8_strup] instead.
1656 : : */
1657 : : gchar*
1658 : 4 : g_strup (gchar *string)
1659 : : {
1660 : : guchar *s;
1661 : :
1662 : 4 : g_return_val_if_fail (string != NULL, NULL);
1663 : :
1664 : 2 : s = (guchar *) string;
1665 : :
1666 : 24 : while (*s)
1667 : : {
1668 : 22 : if (islower (*s))
1669 : 10 : *s = toupper (*s);
1670 : 22 : s++;
1671 : : }
1672 : :
1673 : 2 : return (gchar *) string;
1674 : 2 : }
1675 : :
1676 : : /**
1677 : : * g_strreverse:
1678 : : * @string: the string to reverse
1679 : : *
1680 : : * Reverses all of the bytes in a string. For example,
1681 : : * `g_strreverse ("abcdef")` will result in "fedcba".
1682 : : *
1683 : : * Note that `g_strreverse()` doesn't work on UTF-8 strings
1684 : : * containing multibyte characters. For that purpose, use
1685 : : * [func@GLib.utf8_strreverse].
1686 : : *
1687 : : * Returns: the @string, reversed in place
1688 : : */
1689 : : gchar*
1690 : 4 : g_strreverse (gchar *string)
1691 : : {
1692 : 4 : g_return_val_if_fail (string != NULL, NULL);
1693 : :
1694 : 2 : if (*string)
1695 : : {
1696 : : gchar *h, *t;
1697 : :
1698 : 2 : h = string;
1699 : 2 : t = string + strlen (string) - 1;
1700 : :
1701 : 6 : while (h < t)
1702 : : {
1703 : : gchar c;
1704 : :
1705 : 4 : c = *h;
1706 : 4 : *h = *t;
1707 : 4 : h++;
1708 : 4 : *t = c;
1709 : 4 : t--;
1710 : : }
1711 : 1 : }
1712 : :
1713 : 2 : return string;
1714 : 2 : }
1715 : :
1716 : : /**
1717 : : * g_ascii_tolower:
1718 : : * @c: any character
1719 : : *
1720 : : * Convert a character to ASCII lower case. If the character is not an
1721 : : * ASCII upper case letter, it is returned unchanged.
1722 : : *
1723 : : * Unlike the standard C library `tolower()` function, this only
1724 : : * recognizes standard ASCII letters and ignores the locale, returning
1725 : : * all non-ASCII characters unchanged, even if they are lower case
1726 : : * letters in a particular character set. Also unlike the standard
1727 : : * library function, this takes and returns a char, not an int, so
1728 : : * don't call it on `EOF` but no need to worry about casting to `guchar`
1729 : : * before passing a possibly non-ASCII character in.
1730 : : *
1731 : : * Returns: the result of the conversion
1732 : : */
1733 : : gchar
1734 : 24393 : g_ascii_tolower (gchar c)
1735 : : {
1736 : 24393 : return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1737 : : }
1738 : :
1739 : : /**
1740 : : * g_ascii_toupper:
1741 : : * @c: any character
1742 : : *
1743 : : * Convert a character to ASCII upper case. If the character is not an
1744 : : * ASCII lower case letter, it is returned unchanged.
1745 : : *
1746 : : * Unlike the standard C library `toupper()` function, this only
1747 : : * recognizes standard ASCII letters and ignores the locale, returning
1748 : : * all non-ASCII characters unchanged, even if they are upper case
1749 : : * letters in a particular character set. Also unlike the standard
1750 : : * library function, this takes and returns a char, not an int, so
1751 : : * don't call it on `EOF` but no need to worry about casting to `guchar`
1752 : : * before passing a possibly non-ASCII character in.
1753 : : *
1754 : : * Returns: the result of the conversion
1755 : : */
1756 : : gchar
1757 : 990 : g_ascii_toupper (gchar c)
1758 : : {
1759 : 990 : return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1760 : : }
1761 : :
1762 : : /**
1763 : : * g_ascii_digit_value:
1764 : : * @c: an ASCII character
1765 : : *
1766 : : * Determines the numeric value of a character as a decimal digit. If the
1767 : : * character is not a decimal digit according to [func@GLib.ascii_isdigit],
1768 : : * `-1` is returned.
1769 : : *
1770 : : * Differs from [func@GLib.unichar_digit_value] because it takes a char, so
1771 : : * there's no worry about sign extension if characters are signed.
1772 : : *
1773 : : * Returns: the numerical value of @c if it is a decimal digit, `-1` otherwise
1774 : : */
1775 : : int
1776 : 23517 : g_ascii_digit_value (gchar c)
1777 : : {
1778 : 23517 : if (g_ascii_isdigit (c))
1779 : 22404 : return c - '0';
1780 : 1113 : return -1;
1781 : 5994 : }
1782 : :
1783 : : /**
1784 : : * g_ascii_xdigit_value:
1785 : : * @c: an ASCII character
1786 : : *
1787 : : * Determines the numeric value of a character as a hexadecimal digit. If the
1788 : : * character is not a hex digit according to [func@GLib.ascii_isxdigit],
1789 : : * `-1` is returned.
1790 : : *
1791 : : * Differs from [func@GLib.unichar_xdigit_value] because it takes a char, so
1792 : : * there's no worry about sign extension if characters are signed.
1793 : : *
1794 : : * Differs from [func@GLib.unichar_xdigit_value] because it takes a char, so
1795 : : * there's no worry about sign extension if characters are signed.
1796 : : *
1797 : : * Returns: the numerical value of @c if it is a hex digit, `-1` otherwise
1798 : : */
1799 : : int
1800 : 24194 : g_ascii_xdigit_value (gchar c)
1801 : : {
1802 : 24194 : if (c >= 'A' && c <= 'F')
1803 : 498 : return c - 'A' + 10;
1804 : 23696 : if (c >= 'a' && c <= 'f')
1805 : 983 : return c - 'a' + 10;
1806 : 22713 : return g_ascii_digit_value (c);
1807 : 6287 : }
1808 : :
1809 : : /**
1810 : : * g_ascii_strcasecmp:
1811 : : * @s1: string to compare with @s2
1812 : : * @s2: string to compare with @s1
1813 : : *
1814 : : * Compare two strings, ignoring the case of ASCII characters.
1815 : : *
1816 : : * Unlike the BSD `strcasecmp()` function, this only recognizes standard
1817 : : * ASCII letters and ignores the locale, treating all non-ASCII
1818 : : * bytes as if they are not letters.
1819 : : *
1820 : : * This function should be used only on strings that are known to be
1821 : : * in encodings where the bytes corresponding to ASCII letters always
1822 : : * represent themselves. This includes UTF-8 and the ISO-8859-*
1823 : : * charsets, but not for instance double-byte encodings like the
1824 : : * Windows Codepage 932, where the trailing bytes of double-byte
1825 : : * characters include all ASCII letters. If you compare two CP932
1826 : : * strings using this function, you will get false matches.
1827 : : *
1828 : : * Both @s1 and @s2 must be non-`NULL`.
1829 : : *
1830 : : * Returns: 0 if the strings match, a negative value if @s1 < @s2,
1831 : : * or a positive value if @s1 > @s2
1832 : : */
1833 : : gint
1834 : 3878 : g_ascii_strcasecmp (const gchar *s1,
1835 : : const gchar *s2)
1836 : : {
1837 : : gint c1, c2;
1838 : :
1839 : 3878 : g_return_val_if_fail (s1 != NULL, 0);
1840 : 3876 : g_return_val_if_fail (s2 != NULL, 0);
1841 : :
1842 : 7973 : while (*s1 && *s2)
1843 : : {
1844 : 6957 : c1 = (gint)(guchar) TOLOWER (*s1);
1845 : 6957 : c2 = (gint)(guchar) TOLOWER (*s2);
1846 : 6957 : if (c1 != c2)
1847 : 2858 : return (c1 - c2);
1848 : 4099 : s1++; s2++;
1849 : : }
1850 : :
1851 : 1016 : return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1852 : 3151 : }
1853 : :
1854 : : /**
1855 : : * g_ascii_strncasecmp:
1856 : : * @s1: string to compare with @s2
1857 : : * @s2: string to compare with @s1
1858 : : * @n: number of characters to compare
1859 : : *
1860 : : * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1861 : : * characters after the first @n in each string. If either string is
1862 : : * less than @n bytes long, comparison will stop at the first nul byte
1863 : : * encountered.
1864 : : *
1865 : : * Unlike the BSD `strncasecmp()` function, this only recognizes standard
1866 : : * ASCII letters and ignores the locale, treating all non-ASCII
1867 : : * characters as if they are not letters.
1868 : : *
1869 : : * The same warning as in [func@GLib.ascii_strcasecmp] applies: Use this
1870 : : * function only on strings known to be in encodings where bytes
1871 : : * corresponding to ASCII letters always represent themselves.
1872 : : *
1873 : : * Returns: 0 if the strings match, a negative value if @s1 < @s2,
1874 : : * or a positive value if @s1 > @s2
1875 : : */
1876 : : gint
1877 : 83550 : g_ascii_strncasecmp (const gchar *s1,
1878 : : const gchar *s2,
1879 : : gsize n)
1880 : : {
1881 : : gint c1, c2;
1882 : :
1883 : 83550 : g_return_val_if_fail (s1 != NULL, 0);
1884 : 83548 : g_return_val_if_fail (s2 != NULL, 0);
1885 : :
1886 : 96377 : while (n && *s1 && *s2)
1887 : : {
1888 : 87451 : n -= 1;
1889 : 87451 : c1 = (gint)(guchar) TOLOWER (*s1);
1890 : 87451 : c2 = (gint)(guchar) TOLOWER (*s2);
1891 : 87451 : if (c1 != c2)
1892 : 74620 : return (c1 - c2);
1893 : 12831 : s1++; s2++;
1894 : : }
1895 : :
1896 : 8926 : if (n)
1897 : 8365 : return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1898 : : else
1899 : 561 : return 0;
1900 : 23033 : }
1901 : :
1902 : : /**
1903 : : * g_strcasecmp:
1904 : : * @s1: string to compare with @s2
1905 : : * @s2: string to compare with @s1
1906 : : *
1907 : : * A case-insensitive string comparison, corresponding to the standard
1908 : : * `strcasecmp()` function on platforms which support it.
1909 : : *
1910 : : * Returns: 0 if the strings match, a negative value if @s1 < @s2,
1911 : : * or a positive value if @s1 > @s2
1912 : : *
1913 : : * Deprecated: 2.2: See [func@GLib.strncasecmp] for a discussion of why this
1914 : : * function is deprecated and how to replace it.
1915 : : */
1916 : : gint
1917 : 6 : g_strcasecmp (const gchar *s1,
1918 : : const gchar *s2)
1919 : : {
1920 : : #ifdef HAVE_STRCASECMP
1921 : 6 : g_return_val_if_fail (s1 != NULL, 0);
1922 : 4 : g_return_val_if_fail (s2 != NULL, 0);
1923 : :
1924 : 2 : return strcasecmp (s1, s2);
1925 : : #else
1926 : : gint c1, c2;
1927 : :
1928 : : g_return_val_if_fail (s1 != NULL, 0);
1929 : : g_return_val_if_fail (s2 != NULL, 0);
1930 : :
1931 : : while (*s1 && *s2)
1932 : : {
1933 : : /* According to A. Cox, some platforms have islower's that
1934 : : * don't work right on non-uppercase
1935 : : */
1936 : : c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1937 : : c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1938 : : if (c1 != c2)
1939 : : return (c1 - c2);
1940 : : s1++; s2++;
1941 : : }
1942 : :
1943 : : return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1944 : : #endif
1945 : 3 : }
1946 : :
1947 : : /**
1948 : : * g_strncasecmp:
1949 : : * @s1: string to compare with @s2
1950 : : * @s2: string to compare with @s1
1951 : : * @n: the maximum number of characters to compare
1952 : : *
1953 : : * A case-insensitive string comparison, corresponding to the standard
1954 : : * `strncasecmp()` function on platforms which support it. It is similar
1955 : : * to [func@GLib.strcasecmp] except it only compares the first @n characters of
1956 : : * the strings.
1957 : : *
1958 : : * Returns: 0 if the strings match, a negative value if @s1 < @s2,
1959 : : * or a positive value if @s1 > @s2
1960 : : *
1961 : : * Deprecated: 2.2: The problem with `g_strncasecmp()` is that it does
1962 : : * the comparison by calling `toupper()`/`tolower()`. These functions
1963 : : * are locale-specific and operate on single bytes. However, it is
1964 : : * impossible to handle things correctly from an internationalization
1965 : : * standpoint by operating on bytes, since characters may be multibyte.
1966 : : * Thus `g_strncasecmp()` is broken if your string is guaranteed to be
1967 : : * ASCII, since it is locale-sensitive, and it's broken if your string
1968 : : * is localized, since it doesn't work on many encodings at all,
1969 : : * including UTF-8, EUC-JP, etc.
1970 : : *
1971 : : * There are therefore two replacement techniques: [func@GLib.ascii_strncasecmp],
1972 : : * which only works on ASCII and is not locale-sensitive, and
1973 : : * [func@GLib.utf8_casefold] followed by `strcmp()` on the resulting strings,
1974 : : * which is good for case-insensitive sorting of UTF-8.
1975 : : */
1976 : : gint
1977 : 4 : g_strncasecmp (const gchar *s1,
1978 : : const gchar *s2,
1979 : : guint n)
1980 : : {
1981 : : #ifdef HAVE_STRNCASECMP
1982 : 4 : return strncasecmp (s1, s2, n);
1983 : : #else
1984 : : gint c1, c2;
1985 : :
1986 : : g_return_val_if_fail (s1 != NULL, 0);
1987 : : g_return_val_if_fail (s2 != NULL, 0);
1988 : :
1989 : : while (n && *s1 && *s2)
1990 : : {
1991 : : n -= 1;
1992 : : /* According to A. Cox, some platforms have islower's that
1993 : : * don't work right on non-uppercase
1994 : : */
1995 : : c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1996 : : c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1997 : : if (c1 != c2)
1998 : : return (c1 - c2);
1999 : : s1++; s2++;
2000 : : }
2001 : :
2002 : : if (n)
2003 : : return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
2004 : : else
2005 : : return 0;
2006 : : #endif
2007 : : }
2008 : :
2009 : : /**
2010 : : * g_strdelimit:
2011 : : * @string: the string to convert
2012 : : * @delimiters: (nullable): a string containing the current delimiters, or
2013 : : * `NULL` to use the standard delimiters defined in [const@GLib.STR_DELIMITERS]
2014 : : * @new_delimiter: the new delimiter character
2015 : : *
2016 : : * Converts any delimiter characters in @string to @new_delimiter.
2017 : : *
2018 : : * Any characters in @string which are found in @delimiters are
2019 : : * changed to the @new_delimiter character. Modifies @string in place,
2020 : : * and returns @string itself, not a copy.
2021 : : *
2022 : : * The return value is to allow nesting such as:
2023 : : * ```C
2024 : : * g_ascii_strup (g_strdelimit (str, "abc", '?'))
2025 : : * ```
2026 : : *
2027 : : * In order to modify a copy, you may use [func@GLib.strdup]:
2028 : : * ```C
2029 : : * reformatted = g_strdelimit (g_strdup (const_str), "abc", '?');
2030 : : * …
2031 : : * g_free (reformatted);
2032 : : * ```
2033 : : *
2034 : : * Returns: the modified @string
2035 : : */
2036 : : gchar *
2037 : 84 : g_strdelimit (gchar *string,
2038 : : const gchar *delimiters,
2039 : : gchar new_delim)
2040 : : {
2041 : : gchar *c;
2042 : :
2043 : 84 : g_return_val_if_fail (string != NULL, NULL);
2044 : :
2045 : 82 : if (!delimiters)
2046 : 2 : delimiters = G_STR_DELIMITERS;
2047 : :
2048 : 284 : for (c = string; *c; c++)
2049 : : {
2050 : 202 : if (strchr (delimiters, *c))
2051 : 10 : *c = new_delim;
2052 : 101 : }
2053 : :
2054 : 82 : return string;
2055 : 42 : }
2056 : :
2057 : : /**
2058 : : * g_strcanon:
2059 : : * @string: a nul-terminated array of bytes
2060 : : * @valid_chars: bytes permitted in @string
2061 : : * @substitutor: replacement character for disallowed bytes
2062 : : *
2063 : : * For each character in @string, if the character is not in @valid_chars,
2064 : : * replaces the character with @substitutor.
2065 : : *
2066 : : * Modifies @string in place, and return @string itself, not a copy. The
2067 : : * return value is to allow nesting such as:
2068 : : * ```C
2069 : : * g_ascii_strup (g_strcanon (str, "abc", '?'))
2070 : : * ```
2071 : : *
2072 : : * In order to modify a copy, you may use [func@GLib.strdup]:
2073 : : * ```C
2074 : : * reformatted = g_strcanon (g_strdup (const_str), "abc", '?');
2075 : : * …
2076 : : * g_free (reformatted);
2077 : : * ```
2078 : : *
2079 : : * Returns: the modified @string
2080 : : */
2081 : : gchar *
2082 : 6 : g_strcanon (gchar *string,
2083 : : const gchar *valid_chars,
2084 : : gchar substitutor)
2085 : : {
2086 : : gchar *c;
2087 : :
2088 : 6 : g_return_val_if_fail (string != NULL, NULL);
2089 : 4 : g_return_val_if_fail (valid_chars != NULL, NULL);
2090 : :
2091 : 18 : for (c = string; *c; c++)
2092 : : {
2093 : 16 : if (!strchr (valid_chars, *c))
2094 : 4 : *c = substitutor;
2095 : 8 : }
2096 : :
2097 : 2 : return string;
2098 : 3 : }
2099 : :
2100 : : /**
2101 : : * g_strcompress:
2102 : : * @source: a string to compress
2103 : : *
2104 : : * Makes a copy of a string replacing C string-style escape
2105 : : * sequences with their one byte equivalent:
2106 : : *
2107 : : * - `\b` → [U+0008 Backspace](https://en.wikipedia.org/wiki/Backspace)
2108 : : * - `\f` → [U+000C Form Feed](https://en.wikipedia.org/wiki/Form_feed)
2109 : : * - `\n` → [U+000A Line Feed](https://en.wikipedia.org/wiki/Newline)
2110 : : * - `\r` → [U+000D Carriage Return](https://en.wikipedia.org/wiki/Carriage_return)
2111 : : * - `\t` → [U+0009 Horizontal Tabulation](https://en.wikipedia.org/wiki/Tab_character)
2112 : : * - `\v` → [U+000B Vertical Tabulation](https://en.wikipedia.org/wiki/Vertical_Tab)
2113 : : * - `\` followed by one to three octal digits → the numeric value (mod 256)
2114 : : * - `\` followed by any other character → the character as is.
2115 : : * For example, `\\` will turn into a backslash (`\`) and `\"` into a double quote (`"`).
2116 : : *
2117 : : * [func@GLib.strescape] does the reverse conversion.
2118 : : *
2119 : : * Returns: a newly-allocated copy of @source with all escaped
2120 : : * character compressed
2121 : : */
2122 : : gchar *
2123 : 12 : g_strcompress (const gchar *source)
2124 : : {
2125 : 12 : const gchar *p = source, *octal;
2126 : : gchar *dest;
2127 : : gchar *q;
2128 : :
2129 : 12 : g_return_val_if_fail (source != NULL, NULL);
2130 : :
2131 : 10 : dest = g_malloc (strlen (source) + 1);
2132 : 10 : q = dest;
2133 : :
2134 : 140 : while (*p)
2135 : : {
2136 : 132 : if (*p == '\\')
2137 : : {
2138 : 74 : p++;
2139 : 74 : switch (*p)
2140 : : {
2141 : 1 : case '\0':
2142 : 2 : g_warning ("g_strcompress: trailing \\");
2143 : 2 : goto out;
2144 : 19 : case '0': case '1': case '2': case '3': case '4':
2145 : : case '5': case '6': case '7':
2146 : 38 : *q = 0;
2147 : 38 : octal = p;
2148 : 144 : while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
2149 : : {
2150 : 106 : *q = (*q * 8) + (*p - '0');
2151 : 106 : p++;
2152 : : }
2153 : 38 : q++;
2154 : 38 : p--;
2155 : 38 : break;
2156 : 2 : case 'b':
2157 : 4 : *q++ = '\b';
2158 : 4 : break;
2159 : 2 : case 'f':
2160 : 4 : *q++ = '\f';
2161 : 4 : break;
2162 : 2 : case 'n':
2163 : 4 : *q++ = '\n';
2164 : 4 : break;
2165 : 2 : case 'r':
2166 : 4 : *q++ = '\r';
2167 : 4 : break;
2168 : 2 : case 't':
2169 : 4 : *q++ = '\t';
2170 : 4 : break;
2171 : 2 : case 'v':
2172 : 4 : *q++ = '\v';
2173 : 4 : break;
2174 : 5 : default: /* Also handles \" and \\ */
2175 : 10 : *q++ = *p;
2176 : 10 : break;
2177 : : }
2178 : 36 : }
2179 : : else
2180 : 58 : *q++ = *p;
2181 : 130 : p++;
2182 : : }
2183 : 4 : out:
2184 : 10 : *q = 0;
2185 : :
2186 : 10 : return dest;
2187 : 6 : }
2188 : :
2189 : : /**
2190 : : * g_strescape:
2191 : : * @source: a string to escape
2192 : : * @exceptions: (nullable): a string of characters not to escape in @source
2193 : : *
2194 : : * It replaces the following special characters in the string @source
2195 : : * with their corresponding C escape sequence:
2196 : : *
2197 : : * | Symbol | Escape |
2198 : : * |-----------------------------------------------------------------------------|--------|
2199 : : * | [U+0008 Backspace](https://en.wikipedia.org/wiki/Backspace) | `\b` |
2200 : : * | [U+000C Form Feed](https://en.wikipedia.org/wiki/Form_feed) | `\f` |
2201 : : * | [U+000A Line Feed](https://en.wikipedia.org/wiki/Newline) | `\n` |
2202 : : * | [U+000D Carriage Return](https://en.wikipedia.org/wiki/Carriage_return) | `\r` |
2203 : : * | [U+0009 Horizontal Tabulation](https://en.wikipedia.org/wiki/Tab_character) | `\t` |
2204 : : * | [U+000B Vertical Tabulation](https://en.wikipedia.org/wiki/Vertical_Tab) | `\v` |
2205 : : *
2206 : : * It also inserts a backslash (`\`) before any backslash or a double quote (`"`).
2207 : : * Additionally all characters in the range 0x01-0x1F (everything
2208 : : * below SPACE) and in the range 0x7F-0xFF (all non-ASCII chars) are
2209 : : * replaced with a backslash followed by their octal representation.
2210 : : * Characters supplied in @exceptions are not escaped.
2211 : : *
2212 : : * [func@GLib.strcompress] does the reverse conversion.
2213 : : *
2214 : : * Returns: a newly-allocated copy of @source with special characters escaped
2215 : : */
2216 : : gchar *
2217 : 327 : g_strescape (const gchar *source,
2218 : : const gchar *exceptions)
2219 : : {
2220 : : size_t len;
2221 : : const guchar *p;
2222 : : gchar *dest;
2223 : : gchar *q;
2224 : : guchar excmap[256];
2225 : :
2226 : 327 : g_return_val_if_fail (source != NULL, NULL);
2227 : :
2228 : 325 : p = (guchar *) source;
2229 : : /* Each source byte needs maximally four destination chars (\777) */
2230 : 325 : if (!g_size_checked_mul (&len, strlen (source), 4) ||
2231 : 325 : !g_size_checked_add (&len, len, 1))
2232 : : {
2233 : 0 : g_error ("%s: overflow allocating %" G_GSIZE_FORMAT "*4+1 bytes",
2234 : 0 : G_STRLOC, strlen (source));
2235 : 0 : }
2236 : 325 : q = dest = g_malloc (len);
2237 : :
2238 : 325 : memset (excmap, 0, 256);
2239 : 325 : if (exceptions)
2240 : : {
2241 : 2 : guchar *e = (guchar *) exceptions;
2242 : :
2243 : 14 : while (*e)
2244 : : {
2245 : 12 : excmap[*e] = 1;
2246 : 12 : e++;
2247 : : }
2248 : 1 : }
2249 : :
2250 : 5075 : while (*p)
2251 : : {
2252 : 4750 : if (excmap[*p])
2253 : 6 : *q++ = *p;
2254 : : else
2255 : : {
2256 : 4744 : switch (*p)
2257 : : {
2258 : 2 : case '\b':
2259 : 4 : *q++ = '\\';
2260 : 4 : *q++ = 'b';
2261 : 4 : break;
2262 : 2 : case '\f':
2263 : 4 : *q++ = '\\';
2264 : 4 : *q++ = 'f';
2265 : 4 : break;
2266 : 3 : case '\n':
2267 : 6 : *q++ = '\\';
2268 : 6 : *q++ = 'n';
2269 : 6 : break;
2270 : 3 : case '\r':
2271 : 6 : *q++ = '\\';
2272 : 6 : *q++ = 'r';
2273 : 6 : break;
2274 : 7 : case '\t':
2275 : 10 : *q++ = '\\';
2276 : 10 : *q++ = 't';
2277 : 10 : break;
2278 : 3 : case '\v':
2279 : 6 : *q++ = '\\';
2280 : 6 : *q++ = 'v';
2281 : 6 : break;
2282 : 4 : case '\\':
2283 : 8 : *q++ = '\\';
2284 : 8 : *q++ = '\\';
2285 : 8 : break;
2286 : 3 : case '"':
2287 : 6 : *q++ = '\\';
2288 : 6 : *q++ = '"';
2289 : 6 : break;
2290 : 2574 : default:
2291 : 4694 : if ((*p < ' ') || (*p >= 0177))
2292 : : {
2293 : 262 : *q++ = '\\';
2294 : 262 : *q++ = '0' + (((*p) >> 6) & 07);
2295 : 262 : *q++ = '0' + (((*p) >> 3) & 07);
2296 : 262 : *q++ = '0' + ((*p) & 07);
2297 : 26 : }
2298 : : else
2299 : 4432 : *q++ = *p;
2300 : 4694 : break;
2301 : : }
2302 : : }
2303 : 4750 : p++;
2304 : : }
2305 : 325 : *q = 0;
2306 : 325 : return dest;
2307 : 154 : }
2308 : :
2309 : : /**
2310 : : * g_strchug:
2311 : : * @string: a string to remove the leading whitespace from
2312 : : *
2313 : : * Removes leading whitespace from a string, by moving the rest
2314 : : * of the characters forward.
2315 : : *
2316 : : * This function doesn't allocate or reallocate any memory;
2317 : : * it modifies @string in place. Therefore, it cannot be used on
2318 : : * statically allocated strings.
2319 : : *
2320 : : * The pointer to @string is returned to allow the nesting of functions.
2321 : : *
2322 : : * Also see [func@GLib.strchomp] and [func@GLib.strstrip].
2323 : : *
2324 : : * Returns: the modified @string
2325 : : */
2326 : : gchar *
2327 : 8466 : g_strchug (gchar *string)
2328 : : {
2329 : : guchar *start;
2330 : :
2331 : 8466 : g_return_val_if_fail (string != NULL, NULL);
2332 : :
2333 : 8860 : for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
2334 : : ;
2335 : :
2336 : 8464 : memmove (string, start, strlen ((gchar *) start) + 1);
2337 : :
2338 : 8464 : return string;
2339 : 231 : }
2340 : :
2341 : : /**
2342 : : * g_strchomp:
2343 : : * @string: a string to remove the trailing whitespace from
2344 : : *
2345 : : * Removes trailing whitespace from a string.
2346 : : *
2347 : : * This function doesn't allocate or reallocate any memory;
2348 : : * it modifies @string in place. Therefore, it cannot be used
2349 : : * on statically allocated strings.
2350 : : *
2351 : : * The pointer to @string is returned to allow the nesting of functions.
2352 : : *
2353 : : * Also see [func@GLib.strchug] and [func@GLib.strstrip].
2354 : : *
2355 : : * Returns: the modified @string
2356 : : */
2357 : : gchar *
2358 : 9288 : g_strchomp (gchar *string)
2359 : : {
2360 : : gsize len;
2361 : :
2362 : 9288 : g_return_val_if_fail (string != NULL, NULL);
2363 : :
2364 : 9286 : len = strlen (string);
2365 : 14928 : while (len--)
2366 : : {
2367 : 12179 : if (g_ascii_isspace ((guchar) string[len]))
2368 : 5642 : string[len] = '\0';
2369 : : else
2370 : 6537 : break;
2371 : : }
2372 : :
2373 : 9286 : return string;
2374 : 634 : }
2375 : :
2376 : : /**
2377 : : * g_strsplit:
2378 : : * @string: a string to split
2379 : : * @delimiter: a string which specifies the places at which to split
2380 : : * the string. The delimiter is not included in any of the resulting
2381 : : * strings, unless @max_tokens is reached.
2382 : : * @max_tokens: the maximum number of pieces to split @string into
2383 : : * If this is less than 1, the string is split completely
2384 : : *
2385 : : * Splits a string into a maximum of @max_tokens pieces, using the given
2386 : : * @delimiter. If @max_tokens is reached, the remainder of @string is
2387 : : * appended to the last token.
2388 : : *
2389 : : * As an example, the result of `g_strsplit (":a:bc::d:", ":", -1)` is an array
2390 : : * containing the six strings "", "a", "bc", "", "d" and "".
2391 : : *
2392 : : * As a special case, the result of splitting the empty string "" is an empty
2393 : : * array, not an array containing a single string. The reason for this
2394 : : * special case is that being able to represent an empty array is typically
2395 : : * more useful than consistent handling of empty elements. If you do need
2396 : : * to represent empty elements, you'll need to check for the empty string
2397 : : * before calling `g_strsplit()`.
2398 : : *
2399 : : * Returns: (transfer full): a newly-allocated array of strings, freed with
2400 : : * [func@GLib.strfreev]
2401 : : */
2402 : : gchar**
2403 : 203214 : g_strsplit (const gchar *string,
2404 : : const gchar *delimiter,
2405 : : gint max_tokens)
2406 : : {
2407 : : const char *s;
2408 : : const gchar *remainder;
2409 : : GPtrArray *string_list;
2410 : :
2411 : 203214 : g_return_val_if_fail (string != NULL, NULL);
2412 : 203212 : g_return_val_if_fail (delimiter != NULL, NULL);
2413 : 203210 : g_return_val_if_fail (delimiter[0] != '\0', NULL);
2414 : :
2415 : 203208 : if (max_tokens < 1)
2416 : : {
2417 : 202687 : max_tokens = G_MAXINT;
2418 : 202687 : string_list = g_ptr_array_new ();
2419 : 94939 : }
2420 : : else
2421 : : {
2422 : 521 : string_list = g_ptr_array_new_full (max_tokens + 1, NULL);
2423 : : }
2424 : :
2425 : 203208 : remainder = string;
2426 : 203208 : s = strstr (remainder, delimiter);
2427 : 203208 : if (s)
2428 : : {
2429 : 130027 : gsize delimiter_len = strlen (delimiter);
2430 : :
2431 : 798444 : while (--max_tokens && s)
2432 : : {
2433 : : gsize len;
2434 : :
2435 : 668417 : len = s - remainder;
2436 : 668417 : g_ptr_array_add (string_list, g_strndup (remainder, len));
2437 : 668417 : remainder = s + delimiter_len;
2438 : 668417 : s = strstr (remainder, delimiter);
2439 : : }
2440 : 60665 : }
2441 : 203208 : if (*string)
2442 : 203069 : g_ptr_array_add (string_list, g_strdup (remainder));
2443 : :
2444 : 203208 : g_ptr_array_add (string_list, NULL);
2445 : :
2446 : 203208 : return (char **) g_ptr_array_free (string_list, FALSE);
2447 : 95227 : }
2448 : :
2449 : : /**
2450 : : * g_strsplit_set:
2451 : : * @string: a string to split
2452 : : * @delimiters: (array zero-terminated=1) (element-type guint8): a
2453 : : * nul-terminated byte array containing bytes that are used to
2454 : : * split the string; can be empty (just a nul byte), which will result in no
2455 : : * string splitting
2456 : : * @max_tokens: the maximum number of tokens to split @string into.
2457 : : * If this is less than 1, the string is split completely
2458 : : *
2459 : : * Splits @string into a number of tokens not containing any of the
2460 : : * bytes in @delimiters.
2461 : : *
2462 : : * A token is the (possibly empty) longest string that does not
2463 : : * contain any of the bytes in @delimiters. Note that separators
2464 : : * will only be single bytes from @delimiters. If @max_tokens is reached,
2465 : : * the remainder is appended to the last token.
2466 : : *
2467 : : * For example, the result of `g_strsplit_set ("abc:def/ghi", ":/", -1)`
2468 : : * is an array containing the three strings `"abc"`, `"def"`, and `"ghi"`.
2469 : : *
2470 : : * The result of `g_strsplit_set (":def/ghi:/x", ":/", -1)` is an array
2471 : : * containing the five strings `""`, `"def"`, `"ghi"`, `""`, `"x"`.
2472 : : *
2473 : : * As a special case, the result of splitting the empty string `""` is an empty
2474 : : * array, not an array containing a single string. The reason for this
2475 : : * special case is that being able to represent an empty array is typically
2476 : : * more useful than consistent handling of empty elements. If you do need
2477 : : * to represent empty elements, you'll need to check for the empty string
2478 : : * before calling `g_strsplit_set()`.
2479 : : *
2480 : : * Note that this function works on bytes not characters, so it can't be used
2481 : : * to delimit UTF-8 strings for anything but ASCII characters.
2482 : : *
2483 : : * Returns: (transfer full): a newly-allocated array of strings. Use
2484 : : * [func@GLib.strfreev] to free it.
2485 : : *
2486 : : * Since: 2.4
2487 : : **/
2488 : : gchar **
2489 : 182 : g_strsplit_set (const gchar *string,
2490 : : const gchar *delimiters,
2491 : : gint max_tokens)
2492 : : {
2493 : : guint8 delim_table[256]; /* 1 = index is a separator; 0 otherwise */
2494 : : GSList *tokens, *list;
2495 : : gint n_tokens;
2496 : : const gchar *s;
2497 : : const gchar *current;
2498 : : gchar *token;
2499 : : gchar **result;
2500 : :
2501 : 182 : g_return_val_if_fail (string != NULL, NULL);
2502 : 180 : g_return_val_if_fail (delimiters != NULL, NULL);
2503 : :
2504 : 178 : if (max_tokens < 1)
2505 : 118 : max_tokens = G_MAXINT;
2506 : :
2507 : 178 : if (*string == '\0')
2508 : : {
2509 : 30 : result = g_new (char *, 1);
2510 : 30 : result[0] = NULL;
2511 : 30 : return result;
2512 : : }
2513 : :
2514 : : /* Check if each character in @string is a separator, by indexing by the
2515 : : * character value into the @delim_table, which has value 1 stored at an index
2516 : : * if that index is a separator. */
2517 : 148 : memset (delim_table, FALSE, sizeof (delim_table));
2518 : 390 : for (s = delimiters; *s != '\0'; ++s)
2519 : 242 : delim_table[*(guchar *)s] = TRUE;
2520 : :
2521 : 148 : tokens = NULL;
2522 : 148 : n_tokens = 0;
2523 : :
2524 : 148 : s = current = string;
2525 : 3085 : while (*s != '\0')
2526 : : {
2527 : 2937 : if (delim_table[*(guchar *)s] && n_tokens + 1 < max_tokens)
2528 : : {
2529 : 262 : token = g_strndup (current, s - current);
2530 : 262 : tokens = g_slist_prepend (tokens, token);
2531 : 262 : ++n_tokens;
2532 : :
2533 : 262 : current = s + 1;
2534 : 106 : }
2535 : :
2536 : 2937 : ++s;
2537 : : }
2538 : :
2539 : 148 : token = g_strndup (current, s - current);
2540 : 148 : tokens = g_slist_prepend (tokens, token);
2541 : 148 : ++n_tokens;
2542 : :
2543 : 148 : result = g_new (gchar *, n_tokens + 1);
2544 : :
2545 : 148 : result[n_tokens] = NULL;
2546 : 558 : for (list = tokens; list != NULL; list = list->next)
2547 : 410 : result[--n_tokens] = list->data;
2548 : :
2549 : 148 : g_slist_free (tokens);
2550 : :
2551 : 148 : return result;
2552 : 66 : }
2553 : :
2554 : : /**
2555 : : * GStrv:
2556 : : *
2557 : : * A typedef alias for gchar**. This is mostly useful when used together with
2558 : : * `g_auto()`.
2559 : : */
2560 : :
2561 : : /**
2562 : : * g_strfreev:
2563 : : * @str_array: (array zero-terminated=1) (nullable) (transfer full): an
2564 : : * array of strings to free
2565 : : *
2566 : : * Frees an array of strings, as well as each string it contains.
2567 : : *
2568 : : * If @str_array is `NULL`, this function simply returns.
2569 : : */
2570 : : void
2571 : 484235 : g_strfreev (gchar **str_array)
2572 : : {
2573 : 484235 : if (str_array)
2574 : : {
2575 : : gsize i;
2576 : :
2577 : 1248863 : for (i = 0; str_array[i] != NULL; i++)
2578 : 1032637 : g_free (str_array[i]);
2579 : :
2580 : 216226 : g_free (str_array);
2581 : 98078 : }
2582 : 484235 : }
2583 : :
2584 : : /**
2585 : : * g_strdupv:
2586 : : * @str_array: (array zero-terminated=1) (nullable): an array of strings to copy
2587 : : *
2588 : : * Copies an array of strings. The copy is a deep copy; each string is also
2589 : : * copied.
2590 : : *
2591 : : * If called on a `NULL` value, `g_strdupv()` simply returns `NULL`.
2592 : : *
2593 : : * Returns: (array zero-terminated=1) (nullable) (transfer full): a
2594 : : * newly-allocated array of strings. Use [func@GLib.strfreev] to free it.
2595 : : */
2596 : : gchar**
2597 : 5176 : g_strdupv (gchar **str_array)
2598 : : {
2599 : 5176 : if (str_array)
2600 : : {
2601 : : gsize i;
2602 : : gchar **retval;
2603 : :
2604 : 5020 : i = 0;
2605 : 88501 : while (str_array[i])
2606 : 83481 : ++i;
2607 : :
2608 : 5020 : retval = g_new (gchar*, i + 1);
2609 : :
2610 : 5020 : i = 0;
2611 : 88501 : while (str_array[i])
2612 : : {
2613 : 83481 : retval[i] = g_strdup (str_array[i]);
2614 : 83481 : ++i;
2615 : : }
2616 : 5020 : retval[i] = NULL;
2617 : :
2618 : 5020 : return retval;
2619 : : }
2620 : : else
2621 : 156 : return NULL;
2622 : 1457 : }
2623 : :
2624 : : /**
2625 : : * g_strjoinv:
2626 : : * @separator: (nullable): a string to insert between each of the strings
2627 : : * @str_array: (array zero-terminated=1): an array of strings to join
2628 : : *
2629 : : * Joins an array of strings together to form one long string, with the
2630 : : * optional @separator inserted between each of them.
2631 : : *
2632 : : * If @str_array has no items, the return value will be an
2633 : : * empty string. If @str_array contains a single item, @separator will not
2634 : : * appear in the resulting string.
2635 : : *
2636 : : * Returns: a newly-allocated string containing all of the strings joined
2637 : : * together, with @separator between them
2638 : : */
2639 : : gchar*
2640 : 236939 : g_strjoinv (const gchar *separator,
2641 : : gchar **str_array)
2642 : : {
2643 : : gchar *string;
2644 : : gchar *ptr;
2645 : :
2646 : 236939 : g_return_val_if_fail (str_array != NULL, NULL);
2647 : :
2648 : 236937 : if (separator == NULL)
2649 : 4 : separator = "";
2650 : :
2651 : 236937 : if (*str_array)
2652 : : {
2653 : : gsize i;
2654 : : gsize len;
2655 : : gsize separator_len;
2656 : : gsize separators_len;
2657 : :
2658 : 183201 : separator_len = strlen (separator);
2659 : : /* First part, getting length */
2660 : 183201 : len = 1 + strlen (str_array[0]);
2661 : 482073 : for (i = 1; str_array[i] != NULL; i++)
2662 : 298872 : if (!g_size_checked_add (&len, len, strlen (str_array[i])))
2663 : 0 : g_error ("%s: overflow joining strings", G_STRLOC);
2664 : :
2665 : 183201 : if (!g_size_checked_mul (&separators_len, separator_len, (i - 1)) ||
2666 : 183201 : !g_size_checked_add (&len, len, separators_len))
2667 : 0 : g_error ("%s: overflow joining strings", G_STRLOC);
2668 : :
2669 : : /* Second part, building string */
2670 : 183201 : string = g_new (gchar, len);
2671 : 183201 : ptr = g_stpcpy (string, *str_array);
2672 : 482073 : for (i = 1; str_array[i] != NULL; i++)
2673 : : {
2674 : 298872 : ptr = g_stpcpy (ptr, separator);
2675 : 298872 : ptr = g_stpcpy (ptr, str_array[i]);
2676 : 150324 : }
2677 : 98171 : }
2678 : : else
2679 : 53736 : string = g_strdup ("");
2680 : :
2681 : 236937 : return string;
2682 : 128544 : }
2683 : :
2684 : : /**
2685 : : * g_strjoin:
2686 : : * @separator: (nullable): a string to insert between each of the strings
2687 : : * @...: a `NULL`-terminated list of strings to join
2688 : : *
2689 : : * Joins a number of strings together to form one long string, with the
2690 : : * optional @separator inserted between each of them.
2691 : : *
2692 : : * Returns: a newly-allocated string containing all of the strings joined
2693 : : * together, with @separator between them
2694 : : */
2695 : : gchar*
2696 : 29819 : g_strjoin (const gchar *separator,
2697 : : ...)
2698 : : {
2699 : : gchar *string, *s;
2700 : : va_list args;
2701 : : gsize len;
2702 : : gsize separator_len;
2703 : : gchar *ptr;
2704 : :
2705 : 29819 : if (separator == NULL)
2706 : 6 : separator = "";
2707 : :
2708 : 29819 : separator_len = strlen (separator);
2709 : :
2710 : 29819 : va_start (args, separator);
2711 : :
2712 : 29819 : s = va_arg (args, gchar*);
2713 : :
2714 : 29819 : if (s)
2715 : : {
2716 : : /* First part, getting length */
2717 : 29815 : len = 1 + strlen (s);
2718 : :
2719 : 29815 : s = va_arg (args, gchar*);
2720 : 140131 : while (s)
2721 : : {
2722 : 110316 : if (!g_size_checked_add (&len, len, separator_len) ||
2723 : 110316 : !g_size_checked_add (&len, len, strlen (s)))
2724 : 0 : g_error ("%s: overflow joining strings", G_STRLOC);
2725 : 110316 : s = va_arg (args, gchar*);
2726 : : }
2727 : 29815 : va_end (args);
2728 : :
2729 : : /* Second part, building string */
2730 : 29815 : string = g_new (gchar, len);
2731 : :
2732 : 29815 : va_start (args, separator);
2733 : :
2734 : 29815 : s = va_arg (args, gchar*);
2735 : 29815 : ptr = g_stpcpy (string, s);
2736 : :
2737 : 29815 : s = va_arg (args, gchar*);
2738 : 140131 : while (s)
2739 : : {
2740 : 110316 : ptr = g_stpcpy (ptr, separator);
2741 : 110316 : ptr = g_stpcpy (ptr, s);
2742 : 110316 : s = va_arg (args, gchar*);
2743 : : }
2744 : 12583 : }
2745 : : else
2746 : 4 : string = g_strdup ("");
2747 : :
2748 : 29819 : va_end (args);
2749 : :
2750 : 29819 : return string;
2751 : : }
2752 : :
2753 : :
2754 : : /**
2755 : : * g_strstr_len:
2756 : : * @haystack: a string to search in
2757 : : * @haystack_len: the maximum length of @haystack in bytes, or `-1` to
2758 : : * search it entirely
2759 : : * @needle: the string to search for
2760 : : *
2761 : : * Searches the string @haystack for the first occurrence
2762 : : * of the string @needle, limiting the length of the search
2763 : : * to @haystack_len or a nul terminator byte (whichever is reached first).
2764 : : *
2765 : : * A length of `-1` can be used to mean “search the entire string”, like
2766 : : * `strstr()`.
2767 : : *
2768 : : * The fact that this function returns `gchar *` rather than `const gchar *` is
2769 : : * a historical artifact.
2770 : : *
2771 : : * Returns: (transfer none) (nullable): a pointer to the found occurrence, or
2772 : : * `NULL` if not found
2773 : : */
2774 : : gchar *
2775 : 59506 : g_strstr_len (const gchar *haystack,
2776 : : gssize haystack_len,
2777 : : const gchar *needle)
2778 : : {
2779 : 59506 : g_return_val_if_fail (haystack != NULL, NULL);
2780 : 59504 : g_return_val_if_fail (needle != NULL, NULL);
2781 : :
2782 : 59502 : if (haystack_len < 0)
2783 : 59358 : return (gchar *)strstr (haystack, needle);
2784 : : else
2785 : : {
2786 : 144 : const gchar *p = haystack;
2787 : 144 : gsize needle_len = strlen (needle);
2788 : 144 : gsize haystack_len_unsigned = haystack_len;
2789 : : const gchar *end;
2790 : : gsize i;
2791 : :
2792 : 144 : if (needle_len == 0)
2793 : 2 : return (gchar *)haystack;
2794 : :
2795 : 142 : if (haystack_len_unsigned < needle_len)
2796 : 6 : return NULL;
2797 : :
2798 : 136 : end = haystack + haystack_len - needle_len;
2799 : :
2800 : 81844 : while (p <= end && *p)
2801 : : {
2802 : 86356 : for (i = 0; i < needle_len; i++)
2803 : 86246 : if (p[i] != needle[i])
2804 : 81708 : goto next;
2805 : :
2806 : 110 : return (gchar *)p;
2807 : :
2808 : 40854 : next:
2809 : 81708 : p++;
2810 : : }
2811 : :
2812 : 26 : return NULL;
2813 : : }
2814 : 1090 : }
2815 : :
2816 : : /**
2817 : : * g_strrstr:
2818 : : * @haystack: a string to search in
2819 : : * @needle: the string to search for
2820 : : *
2821 : : * Searches the string @haystack for the last occurrence
2822 : : * of the string @needle.
2823 : : *
2824 : : * The fact that this function returns `gchar *` rather than `const gchar *` is
2825 : : * a historical artifact.
2826 : : *
2827 : : * Returns: (transfer none) (nullable): a pointer to the found occurrence, or
2828 : : * `NULL` if not found
2829 : : */
2830 : : gchar *
2831 : 59091 : g_strrstr (const gchar *haystack,
2832 : : const gchar *needle)
2833 : : {
2834 : : gsize i;
2835 : : gsize needle_len;
2836 : : gsize haystack_len;
2837 : : const gchar *p;
2838 : :
2839 : 59091 : g_return_val_if_fail (haystack != NULL, NULL);
2840 : 59089 : g_return_val_if_fail (needle != NULL, NULL);
2841 : :
2842 : 59087 : needle_len = strlen (needle);
2843 : 59087 : haystack_len = strlen (haystack);
2844 : :
2845 : 59087 : if (needle_len == 0)
2846 : 2 : return (gchar *)haystack;
2847 : :
2848 : 59085 : if (haystack_len < needle_len)
2849 : 4 : return NULL;
2850 : :
2851 : 59081 : p = haystack + haystack_len - needle_len;
2852 : :
2853 : 617375 : while (p >= haystack)
2854 : : {
2855 : 629912 : for (i = 0; i < needle_len; i++)
2856 : 598229 : if (p[i] != needle[i])
2857 : 558294 : goto next;
2858 : :
2859 : 31683 : return (gchar *)p;
2860 : :
2861 : 552270 : next:
2862 : 558294 : p--;
2863 : : }
2864 : :
2865 : 27398 : return NULL;
2866 : 525 : }
2867 : :
2868 : : /**
2869 : : * g_strrstr_len:
2870 : : * @haystack: a string to search in
2871 : : * @haystack_len: the maximum length of @haystack in bytes. A length of `-1`
2872 : : * can be used to mean "search the entire string", like [func@GLib.strrstr]
2873 : : * @needle: the string to search for
2874 : : *
2875 : : * Searches the string @haystack for the last occurrence
2876 : : * of the string @needle, limiting the length of the search
2877 : : * to @haystack_len.
2878 : : *
2879 : : * The fact that this function returns `gchar *` rather than `const gchar *` is
2880 : : * a historical artifact.
2881 : : *
2882 : : * Returns: (transfer none) (nullable): a pointer to the found occurrence, or
2883 : : * `NULL` if not found
2884 : : */
2885 : : gchar *
2886 : 74 : g_strrstr_len (const gchar *haystack,
2887 : : gssize haystack_len,
2888 : : const gchar *needle)
2889 : : {
2890 : 74 : g_return_val_if_fail (haystack != NULL, NULL);
2891 : 72 : g_return_val_if_fail (needle != NULL, NULL);
2892 : :
2893 : 70 : if (haystack_len < 0)
2894 : 8 : return g_strrstr (haystack, needle);
2895 : : else
2896 : : {
2897 : 62 : gsize needle_len = strlen (needle);
2898 : 62 : const gchar *haystack_max = haystack + haystack_len;
2899 : 62 : const gchar *p = haystack;
2900 : : gsize i;
2901 : :
2902 : 29222 : while (p < haystack_max && *p)
2903 : 29160 : p++;
2904 : :
2905 : 62 : if (p < haystack + needle_len)
2906 : 2 : return NULL;
2907 : :
2908 : 60 : p -= needle_len;
2909 : :
2910 : 1940 : while (p >= haystack)
2911 : : {
2912 : 2872 : for (i = 0; i < needle_len; i++)
2913 : 2820 : if (p[i] != needle[i])
2914 : 1880 : goto next;
2915 : :
2916 : 52 : return (gchar *)p;
2917 : :
2918 : 940 : next:
2919 : 1880 : p--;
2920 : : }
2921 : :
2922 : 8 : return NULL;
2923 : : }
2924 : 37 : }
2925 : :
2926 : :
2927 : : /**
2928 : : * g_str_has_suffix:
2929 : : * @str: a string to look in
2930 : : * @suffix: the suffix to look for
2931 : : *
2932 : : * Looks whether a string ends with @suffix.
2933 : : *
2934 : : * Returns: true if @str ends with @suffix, false otherwise
2935 : : *
2936 : : * Since: 2.2
2937 : : */
2938 : 47 : gboolean (g_str_has_suffix) (const gchar *str,
2939 : : const gchar *suffix)
2940 : : {
2941 : : gsize str_len;
2942 : : gsize suffix_len;
2943 : :
2944 : 47 : g_return_val_if_fail (str != NULL, FALSE);
2945 : 43 : g_return_val_if_fail (suffix != NULL, FALSE);
2946 : :
2947 : 39 : str_len = strlen (str);
2948 : 39 : suffix_len = strlen (suffix);
2949 : :
2950 : 39 : if (str_len < suffix_len)
2951 : 4 : return FALSE;
2952 : :
2953 : 35 : return strcmp (str + str_len - suffix_len, suffix) == 0;
2954 : 21 : }
2955 : :
2956 : : /**
2957 : : * g_str_has_prefix:
2958 : : * @str: a string to look in
2959 : : * @prefix: the prefix to look for
2960 : : *
2961 : : * Looks whether the string @str begins with @prefix.
2962 : : *
2963 : : * Returns: true if @str begins with @prefix, false otherwise
2964 : : *
2965 : : * Since: 2.2
2966 : : */
2967 : 22305 : gboolean (g_str_has_prefix) (const gchar *str,
2968 : : const gchar *prefix)
2969 : : {
2970 : 22305 : g_return_val_if_fail (str != NULL, FALSE);
2971 : 22298 : g_return_val_if_fail (prefix != NULL, FALSE);
2972 : :
2973 : 22294 : return strncmp (str, prefix, strlen (prefix)) == 0;
2974 : 286 : }
2975 : :
2976 : : /**
2977 : : * g_strv_length:
2978 : : * @str_array: (array zero-terminated=1): an array of strings
2979 : : *
2980 : : * Returns the length of an array of strings. @str_array must not be `NULL`.
2981 : : *
2982 : : * Returns: length of @str_array
2983 : : *
2984 : : * Since: 2.6
2985 : : */
2986 : : guint
2987 : 81903 : g_strv_length (gchar **str_array)
2988 : : {
2989 : 81903 : guint i = 0;
2990 : :
2991 : 81903 : g_return_val_if_fail (str_array != NULL, 0);
2992 : :
2993 : 226946 : while (str_array[i])
2994 : 145045 : ++i;
2995 : :
2996 : 81901 : return i;
2997 : 39144 : }
2998 : :
2999 : : static void
3000 : 5218 : index_add_folded (GPtrArray *array,
3001 : : const gchar *start,
3002 : : const gchar *end)
3003 : : {
3004 : : gchar *normal;
3005 : :
3006 : 5218 : normal = g_utf8_normalize (start, end - start, G_NORMALIZE_ALL_COMPOSE);
3007 : :
3008 : : /* TODO: Invent time machine. Converse with Mustafa Ataturk... */
3009 : 5218 : if (strstr (normal, "ı") || strstr (normal, "İ"))
3010 : : {
3011 : 16 : gchar *s = normal;
3012 : : GString *tmp;
3013 : :
3014 : 16 : tmp = g_string_new (NULL);
3015 : :
3016 : 40 : while (*s)
3017 : : {
3018 : : gchar *i, *I, *e;
3019 : :
3020 : 36 : i = strstr (s, "ı");
3021 : 36 : I = strstr (s, "İ");
3022 : :
3023 : 36 : if (!i && !I)
3024 : 6 : break;
3025 : 24 : else if (i && !I)
3026 : 8 : e = i;
3027 : 16 : else if (I && !i)
3028 : 8 : e = I;
3029 : 8 : else if (i < I)
3030 : 4 : e = i;
3031 : : else
3032 : 4 : e = I;
3033 : :
3034 : 24 : g_string_append_len (tmp, s, e - s);
3035 : 12 : g_string_append_c (tmp, 'i');
3036 : 24 : s = g_utf8_next_char (e);
3037 : : }
3038 : :
3039 : 8 : g_string_append (tmp, s);
3040 : 16 : g_free (normal);
3041 : 16 : normal = g_string_free (tmp, FALSE);
3042 : 8 : }
3043 : :
3044 : 5218 : g_ptr_array_add (array, g_utf8_casefold (normal, -1));
3045 : 5218 : g_free (normal);
3046 : 5218 : }
3047 : :
3048 : : static gchar **
3049 : 2451 : split_words (const gchar *value)
3050 : : {
3051 : 2451 : const gchar *start = NULL;
3052 : : GPtrArray *result;
3053 : : const gchar *s;
3054 : :
3055 : 2451 : result = g_ptr_array_new ();
3056 : :
3057 : 35921 : for (s = value; *s; s = g_utf8_next_char (s))
3058 : : {
3059 : 33470 : gunichar c = g_utf8_get_char (s);
3060 : :
3061 : 33470 : if (start == NULL)
3062 : : {
3063 : 5236 : if (g_unichar_isalnum (c) || g_unichar_ismark (c))
3064 : 5218 : start = s;
3065 : 121 : }
3066 : : else
3067 : : {
3068 : 28234 : if (!g_unichar_isalnum (c) && !g_unichar_ismark (c))
3069 : : {
3070 : 3233 : index_add_folded (result, start, s);
3071 : 3233 : start = NULL;
3072 : 66 : }
3073 : : }
3074 : 555 : }
3075 : :
3076 : 2451 : if (start)
3077 : 1985 : index_add_folded (result, start, s);
3078 : :
3079 : 2451 : g_ptr_array_add (result, NULL);
3080 : :
3081 : 2451 : return (gchar **) g_ptr_array_free (result, FALSE);
3082 : : }
3083 : :
3084 : : /**
3085 : : * g_str_tokenize_and_fold:
3086 : : * @string: a string to tokenize
3087 : : * @translit_locale: (nullable): the language code (like 'de' or
3088 : : * 'en_GB') from which @string originates
3089 : : * @ascii_alternates: (out) (optional) (transfer full) (array zero-terminated=1):
3090 : : * a return location for ASCII alternates
3091 : : *
3092 : : * Tokenizes @string and performs folding on each token.
3093 : : *
3094 : : * A token is a non-empty sequence of alphanumeric characters in the
3095 : : * source string, separated by non-alphanumeric characters. An
3096 : : * "alphanumeric" character for this purpose is one that matches
3097 : : * [func@GLib.unichar_isalnum] or [func@GLib.unichar_ismark].
3098 : : *
3099 : : * Each token is then (Unicode) normalised and case-folded. If
3100 : : * @ascii_alternates is non-`NULL` and some of the returned tokens
3101 : : * contain non-ASCII characters, ASCII alternatives will be generated.
3102 : : *
3103 : : * The number of ASCII alternatives that are generated and the method
3104 : : * for doing so is unspecified, but @translit_locale (if specified) may
3105 : : * improve the transliteration if the language of the source string is
3106 : : * known.
3107 : : *
3108 : : * Returns: (transfer full) (array zero-terminated=1): the folded tokens
3109 : : *
3110 : : * Since: 2.40
3111 : : **/
3112 : : gchar **
3113 : 2453 : g_str_tokenize_and_fold (const gchar *string,
3114 : : const gchar *translit_locale,
3115 : : gchar ***ascii_alternates)
3116 : : {
3117 : : gchar **result;
3118 : :
3119 : 2453 : g_return_val_if_fail (string != NULL, NULL);
3120 : :
3121 : 2451 : if (ascii_alternates && g_str_is_ascii (string))
3122 : : {
3123 : 2338 : *ascii_alternates = g_new0 (gchar *, 0 + 1);
3124 : 2338 : ascii_alternates = NULL;
3125 : 14 : }
3126 : :
3127 : 2451 : result = split_words (string);
3128 : :
3129 : 2451 : if (ascii_alternates)
3130 : : {
3131 : : gint i, j, n;
3132 : :
3133 : 36 : n = g_strv_length (result);
3134 : 36 : *ascii_alternates = g_new (gchar *, n + 1);
3135 : 36 : j = 0;
3136 : :
3137 : 154 : for (i = 0; i < n; i++)
3138 : : {
3139 : 118 : if (!g_str_is_ascii (result[i]))
3140 : : {
3141 : : gchar *composed;
3142 : : gchar *ascii;
3143 : : gint k;
3144 : :
3145 : 28 : composed = g_utf8_normalize (result[i], -1, G_NORMALIZE_ALL_COMPOSE);
3146 : :
3147 : 28 : ascii = g_str_to_ascii (composed, translit_locale);
3148 : :
3149 : : /* Only accept strings that are now entirely alnums */
3150 : 216 : for (k = 0; ascii[k]; k++)
3151 : 188 : if (!g_ascii_isalnum (ascii[k]))
3152 : 0 : break;
3153 : :
3154 : 28 : if (ascii[k] == '\0')
3155 : : /* Made it to the end... */
3156 : 28 : (*ascii_alternates)[j++] = ascii;
3157 : : else
3158 : 0 : g_free (ascii);
3159 : :
3160 : 28 : g_free (composed);
3161 : 8 : }
3162 : 34 : }
3163 : :
3164 : 36 : (*ascii_alternates)[j] = NULL;
3165 : 12 : }
3166 : :
3167 : 2451 : return result;
3168 : 53 : }
3169 : :
3170 : : /**
3171 : : * g_str_match_string:
3172 : : * @search_term: the search term from the user
3173 : : * @potential_hit: the text that may be a hit
3174 : : * @accept_alternates: if true, ASCII alternates are accepted
3175 : : *
3176 : : * Checks if a search conducted for @search_term should match
3177 : : * @potential_hit.
3178 : : *
3179 : : * This function calls [func@GLib.str_tokenize_and_fold] on both
3180 : : * @search_term and @potential_hit. ASCII alternates are never taken
3181 : : * for @search_term but will be taken for @potential_hit according to
3182 : : * the value of @accept_alternates.
3183 : : *
3184 : : * A hit occurs when each folded token in @search_term is a prefix of a
3185 : : * folded token from @potential_hit.
3186 : : *
3187 : : * Depending on how you're performing the search, it will typically be
3188 : : * faster to call `g_str_tokenize_and_fold()` on each string in
3189 : : * your corpus and build an index on the returned folded tokens, then
3190 : : * call `g_str_tokenize_and_fold()` on the search term and
3191 : : * perform lookups into that index.
3192 : : *
3193 : : * As some examples, searching for ‘fred’ would match the potential hit
3194 : : * ‘Smith, Fred’ and also ‘Frédéric’. Searching for ‘Fréd’ would match
3195 : : * ‘Frédéric’ but not ‘Frederic’ (due to the one-directional nature of
3196 : : * accent matching). Searching ‘fo’ would match ‘Foo’ and ‘Bar Foo
3197 : : * Baz’, but not ‘SFO’ (because no word has ‘fo’ as a prefix).
3198 : : *
3199 : : * Returns: true if @potential_hit is a hit
3200 : : *
3201 : : * Since: 2.40
3202 : : **/
3203 : : gboolean
3204 : 49 : g_str_match_string (const gchar *search_term,
3205 : : const gchar *potential_hit,
3206 : : gboolean accept_alternates)
3207 : 16 : {
3208 : 49 : gchar **alternates = NULL;
3209 : : gchar **term_tokens;
3210 : : gchar **hit_tokens;
3211 : : gboolean matched;
3212 : : gint i, j;
3213 : :
3214 : 49 : g_return_val_if_fail (search_term != NULL, FALSE);
3215 : 47 : g_return_val_if_fail (potential_hit != NULL, FALSE);
3216 : :
3217 : 45 : term_tokens = g_str_tokenize_and_fold (search_term, NULL, NULL);
3218 : 45 : hit_tokens = g_str_tokenize_and_fold (potential_hit, NULL, accept_alternates ? &alternates : NULL);
3219 : :
3220 : 45 : matched = TRUE;
3221 : :
3222 : 79 : for (i = 0; term_tokens[i]; i++)
3223 : : {
3224 : 104 : for (j = 0; hit_tokens[j]; j++)
3225 : 71 : if (g_str_has_prefix (hit_tokens[j], term_tokens[i]))
3226 : 22 : goto one_matched;
3227 : :
3228 : 15 : if (accept_alternates)
3229 : 27 : for (j = 0; alternates[j]; j++)
3230 : 12 : if (g_str_has_prefix (alternates[j], term_tokens[i]))
3231 : 20 : goto one_matched;
3232 : :
3233 : 17 : matched = FALSE;
3234 : 17 : break;
3235 : :
3236 : 16 : one_matched:
3237 : 34 : continue;
3238 : : }
3239 : :
3240 : 63 : g_strfreev (term_tokens);
3241 : 63 : g_strfreev (hit_tokens);
3242 : 63 : g_strfreev (alternates);
3243 : :
3244 : 63 : return matched;
3245 : 44 : }
3246 : :
3247 : : /**
3248 : : * g_strv_contains:
3249 : : * @strv: (array zero-terminated=1): an array of strings to search in
3250 : : * @str: the string to search for
3251 : : *
3252 : : * Checks if an array of strings contains the string @str according to
3253 : : * [func@GLib.str_equal]. @strv must not be `NULL`.
3254 : : *
3255 : : * Returns: true if @str is an element of @strv
3256 : : *
3257 : : * Since: 2.44
3258 : : */
3259 : : gboolean
3260 : 1146 : g_strv_contains (const gchar * const *strv,
3261 : : const gchar *str)
3262 : : {
3263 : 1146 : g_return_val_if_fail (strv != NULL, FALSE);
3264 : 1144 : g_return_val_if_fail (str != NULL, FALSE);
3265 : :
3266 : 10894 : for (; *strv != NULL; strv++)
3267 : : {
3268 : 10135 : if (g_str_equal (str, *strv))
3269 : 383 : return TRUE;
3270 : 15 : }
3271 : :
3272 : 759 : return FALSE;
3273 : 68 : }
3274 : :
3275 : : /**
3276 : : * g_strv_equal:
3277 : : * @strv1: (array zero-terminated=1): an array of strings to compare to @strv2
3278 : : * @strv2: (array zero-terminated=1): an array of strings to compare to @strv1
3279 : : *
3280 : : * Checks if two arrays of strings contain exactly the same elements in
3281 : : * exactly the same order.
3282 : : *
3283 : : * Elements are compared using [func@GLib.str_equal]. To match independently
3284 : : * of order, sort the arrays first (using [func@GLib.qsort_with_data]
3285 : : * or similar).
3286 : : *
3287 : : * Two empty arrays are considered equal. Neither @strv1 nor @strv2 may be
3288 : : * `NULL`.
3289 : : *
3290 : : * Returns: true if @strv1 and @strv2 are equal
3291 : : * Since: 2.60
3292 : : */
3293 : : gboolean
3294 : 3149 : g_strv_equal (const gchar * const *strv1,
3295 : : const gchar * const *strv2)
3296 : : {
3297 : 3149 : g_return_val_if_fail (strv1 != NULL, FALSE);
3298 : 3147 : g_return_val_if_fail (strv2 != NULL, FALSE);
3299 : :
3300 : 3145 : if (strv1 == strv2)
3301 : 4 : return TRUE;
3302 : :
3303 : 3369 : for (; *strv1 != NULL && *strv2 != NULL; strv1++, strv2++)
3304 : : {
3305 : 3269 : if (!g_str_equal (*strv1, *strv2))
3306 : 3041 : return FALSE;
3307 : 114 : }
3308 : :
3309 : 100 : return (*strv1 == NULL && *strv2 == NULL);
3310 : 1174 : }
3311 : :
3312 : : static gboolean
3313 : 5799 : str_has_sign (const gchar *str)
3314 : : {
3315 : 5799 : return str[0] == '-' || str[0] == '+';
3316 : : }
3317 : :
3318 : : static gboolean
3319 : 18 : str_has_hex_prefix (const gchar *str)
3320 : : {
3321 : 18 : return str[0] == '0' && g_ascii_tolower (str[1]) == 'x';
3322 : : }
3323 : :
3324 : : /**
3325 : : * g_ascii_string_to_signed:
3326 : : * @str: a string to convert
3327 : : * @base: base of a parsed number
3328 : : * @min: a lower bound (inclusive)
3329 : : * @max: an upper bound (inclusive)
3330 : : * @out_num: (out) (optional): a return location for a number
3331 : : * @error: a return location for #GError
3332 : : *
3333 : : * A convenience function for converting a string to a signed number.
3334 : : *
3335 : : * This function assumes that @str contains only a number of the given
3336 : : * @base that is within inclusive bounds limited by @min and @max. If
3337 : : * this is true, then the converted number is stored in @out_num. An
3338 : : * empty string is not a valid input. A string with leading or
3339 : : * trailing whitespace is also an invalid input.
3340 : : *
3341 : : * @base can be between 2 and 36 inclusive. Hexadecimal numbers must
3342 : : * not be prefixed with "0x" or "0X". Such a problem does not exist
3343 : : * for octal numbers, since they were usually prefixed with a zero
3344 : : * which does not change the value of the parsed number.
3345 : : *
3346 : : * Parsing failures result in an error with the `G_NUMBER_PARSER_ERROR`
3347 : : * domain. If the input is invalid, the error code will be
3348 : : * [error@GLib.NumberParserError.INVALID]. If the parsed number is out of
3349 : : * bounds - [error@GLib.NumberParserError.OUT_OF_BOUNDS].
3350 : : *
3351 : : * See [func@GLib.ascii_strtoll] if you have more complex needs such as
3352 : : * parsing a string which starts with a number, but then has other
3353 : : * characters.
3354 : : *
3355 : : * Returns: true if @str was a number, false otherwise
3356 : : *
3357 : : * Since: 2.54
3358 : : */
3359 : : gboolean
3360 : 72 : g_ascii_string_to_signed (const gchar *str,
3361 : : guint base,
3362 : : gint64 min,
3363 : : gint64 max,
3364 : : gint64 *out_num,
3365 : : GError **error)
3366 : : {
3367 : : gint64 number;
3368 : 72 : const gchar *end_ptr = NULL;
3369 : 72 : gint saved_errno = 0;
3370 : :
3371 : 72 : g_return_val_if_fail (str != NULL, FALSE);
3372 : 70 : g_return_val_if_fail (base >= 2 && base <= 36, FALSE);
3373 : 70 : g_return_val_if_fail (min <= max, FALSE);
3374 : 68 : g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
3375 : :
3376 : 68 : if (str[0] == '\0')
3377 : : {
3378 : 3 : g_set_error_literal (error,
3379 : 1 : G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_INVALID,
3380 : 1 : _("Empty string is not a number"));
3381 : 2 : return FALSE;
3382 : : }
3383 : :
3384 : 66 : errno = 0;
3385 : 66 : number = g_ascii_strtoll (str, (gchar **)&end_ptr, base);
3386 : 66 : saved_errno = errno;
3387 : :
3388 : 6 : if (/* We do not allow leading whitespace, but g_ascii_strtoll
3389 : : * accepts it and just skips it, so we need to check for it
3390 : : * ourselves.
3391 : : */
3392 : 66 : g_ascii_isspace (str[0]) ||
3393 : : /* We don't support hexadecimal numbers prefixed with 0x or
3394 : : * 0X.
3395 : : */
3396 : 36 : (base == 16 &&
3397 : 45 : (str_has_sign (str) ? str_has_hex_prefix (str + 1) : str_has_hex_prefix (str))) ||
3398 : 26 : (saved_errno != 0 && saved_errno != ERANGE) ||
3399 : 54 : end_ptr == NULL ||
3400 : 54 : *end_ptr != '\0')
3401 : : {
3402 : 50 : g_set_error (error,
3403 : 20 : G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_INVALID,
3404 : 20 : _("“%s” is not a signed number"), str);
3405 : 30 : return FALSE;
3406 : : }
3407 : 42 : if (saved_errno == ERANGE || number < min || number > max)
3408 : : {
3409 : 14 : gchar *min_str = g_strdup_printf ("%" G_GINT64_FORMAT, min);
3410 : 14 : gchar *max_str = g_strdup_printf ("%" G_GINT64_FORMAT, max);
3411 : :
3412 : 21 : g_set_error (error,
3413 : 7 : G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS,
3414 : 7 : _("Number “%s” is out of bounds [%s, %s]"),
3415 : 7 : str, min_str, max_str);
3416 : 14 : g_free (min_str);
3417 : 14 : g_free (max_str);
3418 : 14 : return FALSE;
3419 : : }
3420 : 28 : if (out_num != NULL)
3421 : 28 : *out_num = number;
3422 : 28 : return TRUE;
3423 : 36 : }
3424 : :
3425 : : /**
3426 : : * g_ascii_string_to_unsigned:
3427 : : * @str: a string
3428 : : * @base: base of a parsed number
3429 : : * @min: a lower bound (inclusive)
3430 : : * @max: an upper bound (inclusive)
3431 : : * @out_num: (out) (optional): a return location for a number
3432 : : * @error: a return location for #GError
3433 : : *
3434 : : * A convenience function for converting a string to an unsigned number.
3435 : : *
3436 : : * This function assumes that @str contains only a number of the given
3437 : : * @base that is within inclusive bounds limited by @min and @max. If
3438 : : * this is true, then the converted number is stored in @out_num. An
3439 : : * empty string is not a valid input. A string with leading or
3440 : : * trailing whitespace is also an invalid input. A string with a leading sign
3441 : : * (`-` or `+`) is not a valid input for the unsigned parser.
3442 : : *
3443 : : * @base can be between 2 and 36 inclusive. Hexadecimal numbers must
3444 : : * not be prefixed with "0x" or "0X". Such a problem does not exist
3445 : : * for octal numbers, since they were usually prefixed with a zero
3446 : : * which does not change the value of the parsed number.
3447 : : *
3448 : : * Parsing failures result in an error with the `G_NUMBER_PARSER_ERROR`
3449 : : * domain. If the input is invalid, the error code will be
3450 : : * [error@GLib.NumberParserError.INVALID]. If the parsed number is out of
3451 : : * bounds - [error@GLib.NumberParserError.OUT_OF_BOUNDS].
3452 : : *
3453 : : * See [func@GLib.ascii_strtoull] if you have more complex needs such as
3454 : : * parsing a string which starts with a number, but then has other
3455 : : * characters.
3456 : : *
3457 : : * Returns: true if @str was a number, false otherwise
3458 : : *
3459 : : * Since: 2.54
3460 : : */
3461 : : gboolean
3462 : 5799 : g_ascii_string_to_unsigned (const gchar *str,
3463 : : guint base,
3464 : : guint64 min,
3465 : : guint64 max,
3466 : : guint64 *out_num,
3467 : : GError **error)
3468 : : {
3469 : : guint64 number;
3470 : 5799 : const gchar *end_ptr = NULL;
3471 : 5799 : gint saved_errno = 0;
3472 : :
3473 : 5799 : g_return_val_if_fail (str != NULL, FALSE);
3474 : 5797 : g_return_val_if_fail (base >= 2 && base <= 36, FALSE);
3475 : 5795 : g_return_val_if_fail (min <= max, FALSE);
3476 : 5793 : g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
3477 : :
3478 : 5793 : if (str[0] == '\0')
3479 : : {
3480 : 3 : g_set_error_literal (error,
3481 : 1 : G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_INVALID,
3482 : 1 : _("Empty string is not a number"));
3483 : 2 : return FALSE;
3484 : : }
3485 : :
3486 : 5791 : errno = 0;
3487 : 5791 : number = g_ascii_strtoull (str, (gchar **)&end_ptr, base);
3488 : 5791 : saved_errno = errno;
3489 : :
3490 : 2898 : if (/* We do not allow leading whitespace, but g_ascii_strtoull
3491 : : * accepts it and just skips it, so we need to check for it
3492 : : * ourselves.
3493 : : */
3494 : 8688 : g_ascii_isspace (str[0]) ||
3495 : : /* Unsigned number should have no sign.
3496 : : */
3497 : 8672 : str_has_sign (str) ||
3498 : : /* We don't support hexadecimal numbers prefixed with 0x or
3499 : : * 0X.
3500 : : */
3501 : 5763 : (base == 16 && str_has_hex_prefix (str)) ||
3502 : 2876 : (saved_errno != 0 && saved_errno != ERANGE) ||
3503 : 5761 : end_ptr == NULL ||
3504 : 5761 : *end_ptr != '\0')
3505 : : {
3506 : 69 : g_set_error (error,
3507 : 25 : G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_INVALID,
3508 : 25 : _("“%s” is not an unsigned number"), str);
3509 : 44 : return FALSE;
3510 : : }
3511 : 5751 : if (saved_errno == ERANGE || number < min || number > max)
3512 : : {
3513 : 10 : gchar *min_str = g_strdup_printf ("%" G_GUINT64_FORMAT, min);
3514 : 10 : gchar *max_str = g_strdup_printf ("%" G_GUINT64_FORMAT, max);
3515 : :
3516 : 15 : g_set_error (error,
3517 : 5 : G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS,
3518 : 5 : _("Number “%s” is out of bounds [%s, %s]"),
3519 : 5 : str, min_str, max_str);
3520 : 10 : g_free (min_str);
3521 : 10 : g_free (max_str);
3522 : 10 : return FALSE;
3523 : : }
3524 : 5741 : if (out_num != NULL)
3525 : 5741 : *out_num = number;
3526 : 5741 : return TRUE;
3527 : 2896 : }
3528 : :
3529 : 166 : G_DEFINE_QUARK (g-number-parser-error-quark, g_number_parser_error)
|