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