Coverage Report

Created: 2025-11-16 06:24

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