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