/src/rauc/subprojects/glib-2.76.5/glib/gstring.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 <stdlib.h> |
35 | | #include <stdio.h> |
36 | | #include <string.h> |
37 | | #include <ctype.h> |
38 | | |
39 | | #include "gstring.h" |
40 | | #include "guriprivate.h" |
41 | | #include "gprintf.h" |
42 | | #include "gutilsprivate.h" |
43 | | |
44 | | |
45 | | /** |
46 | | * SECTION:strings |
47 | | * @title: Strings |
48 | | * @short_description: text buffers which grow automatically |
49 | | * as text is added |
50 | | * |
51 | | * A #GString is an object that handles the memory management of a C |
52 | | * string for you. The emphasis of #GString is on text, typically |
53 | | * UTF-8. Crucially, the "str" member of a #GString is guaranteed to |
54 | | * have a trailing nul character, and it is therefore always safe to |
55 | | * call functions such as strchr() or g_strdup() on it. |
56 | | * |
57 | | * However, a #GString can also hold arbitrary binary data, because it |
58 | | * has a "len" member, which includes any possible embedded nul |
59 | | * characters in the data. Conceptually then, #GString is like a |
60 | | * #GByteArray with the addition of many convenience methods for text, |
61 | | * and a guaranteed nul terminator. |
62 | | */ |
63 | | |
64 | | /** |
65 | | * GString: |
66 | | * @str: points to the character data. It may move as text is added. |
67 | | * The @str field is null-terminated and so |
68 | | * can be used as an ordinary C string. |
69 | | * @len: contains the length of the string, not including the |
70 | | * terminating nul byte. |
71 | | * @allocated_len: the number of bytes that can be stored in the |
72 | | * string before it needs to be reallocated. May be larger than @len. |
73 | | * |
74 | | * The GString struct contains the public fields of a GString. |
75 | | */ |
76 | | |
77 | | static void |
78 | | g_string_expand (GString *string, |
79 | | gsize len) |
80 | 17.5k | { |
81 | | /* Detect potential overflow */ |
82 | 17.5k | if G_UNLIKELY ((G_MAXSIZE - string->len - 1) < len) |
83 | 17.5k | g_error ("adding %" G_GSIZE_FORMAT " to string would overflow", len); |
84 | | |
85 | 17.5k | string->allocated_len = g_nearest_pow (string->len + len + 1); |
86 | | /* If the new size is bigger than G_MAXSIZE / 2, only allocate enough |
87 | | * memory for this string and don't over-allocate. |
88 | | */ |
89 | 17.5k | if (string->allocated_len == 0) |
90 | 0 | string->allocated_len = string->len + len + 1; |
91 | | |
92 | 17.5k | string->str = g_realloc (string->str, string->allocated_len); |
93 | 17.5k | } |
94 | | |
95 | | static inline void |
96 | | g_string_maybe_expand (GString *string, |
97 | | gsize len) |
98 | 2.31k | { |
99 | 2.31k | if (G_UNLIKELY (string->len + len >= string->allocated_len)) |
100 | 2.31k | g_string_expand (string, len); |
101 | 2.31k | } |
102 | | |
103 | | /** |
104 | | * g_string_sized_new: (constructor) |
105 | | * @dfl_size: the default size of the space allocated to hold the string |
106 | | * |
107 | | * Creates a new #GString, with enough space for @dfl_size |
108 | | * bytes. This is useful if you are going to add a lot of |
109 | | * text to the string and don't want it to be reallocated |
110 | | * too often. |
111 | | * |
112 | | * Returns: (transfer full): the new #GString |
113 | | */ |
114 | | GString * |
115 | | g_string_sized_new (gsize dfl_size) |
116 | 15.2k | { |
117 | 15.2k | GString *string = g_slice_new (GString); |
118 | | |
119 | 15.2k | string->allocated_len = 0; |
120 | 15.2k | string->len = 0; |
121 | 15.2k | string->str = NULL; |
122 | | |
123 | 15.2k | g_string_expand (string, MAX (dfl_size, 64)); |
124 | 15.2k | string->str[0] = 0; |
125 | | |
126 | 15.2k | return string; |
127 | 15.2k | } |
128 | | |
129 | | /** |
130 | | * g_string_new: (constructor) |
131 | | * @init: (nullable): the initial text to copy into the string, or %NULL to |
132 | | * start with an empty string |
133 | | * |
134 | | * Creates a new #GString, initialized with the given string. |
135 | | * |
136 | | * Returns: (transfer full): the new #GString |
137 | | */ |
138 | | GString * |
139 | | g_string_new (const gchar *init) |
140 | 5.12k | { |
141 | 5.12k | GString *string; |
142 | | |
143 | 5.12k | if (init == NULL || *init == '\0') |
144 | 5.12k | string = g_string_sized_new (2); |
145 | 0 | else |
146 | 0 | { |
147 | 0 | gint len; |
148 | |
|
149 | 0 | len = strlen (init); |
150 | 0 | string = g_string_sized_new (len + 2); |
151 | |
|
152 | 0 | g_string_append_len (string, init, len); |
153 | 0 | } |
154 | | |
155 | 5.12k | return string; |
156 | 5.12k | } |
157 | | |
158 | | /** |
159 | | * g_string_new_len: (constructor) |
160 | | * @init: initial contents of the string |
161 | | * @len: length of @init to use |
162 | | * |
163 | | * Creates a new #GString with @len bytes of the @init buffer. |
164 | | * Because a length is provided, @init need not be nul-terminated, |
165 | | * and can contain embedded nul bytes. |
166 | | * |
167 | | * Since this function does not stop at nul bytes, it is the caller's |
168 | | * responsibility to ensure that @init has at least @len addressable |
169 | | * bytes. |
170 | | * |
171 | | * Returns: (transfer full): a new #GString |
172 | | */ |
173 | | GString * |
174 | | g_string_new_len (const gchar *init, |
175 | | gssize len) |
176 | 0 | { |
177 | 0 | GString *string; |
178 | |
|
179 | 0 | if (len < 0) |
180 | 0 | return g_string_new (init); |
181 | 0 | else |
182 | 0 | { |
183 | 0 | string = g_string_sized_new (len); |
184 | |
|
185 | 0 | if (init) |
186 | 0 | g_string_append_len (string, init, len); |
187 | |
|
188 | 0 | return string; |
189 | 0 | } |
190 | 0 | } |
191 | | |
192 | | /** |
193 | | * g_string_free: |
194 | | * @string: (transfer full): a #GString |
195 | | * @free_segment: if %TRUE, the actual character data is freed as well |
196 | | * |
197 | | * Frees the memory allocated for the #GString. |
198 | | * If @free_segment is %TRUE it also frees the character data. If |
199 | | * it's %FALSE, the caller gains ownership of the buffer and must |
200 | | * free it after use with g_free(). |
201 | | * |
202 | | * Instead of passing %FALSE to this function, consider using |
203 | | * g_string_free_and_steal(). |
204 | | * |
205 | | * Returns: (nullable): the character data of @string |
206 | | * (i.e. %NULL if @free_segment is %TRUE) |
207 | | */ |
208 | | gchar * |
209 | | (g_string_free) (GString *string, |
210 | | gboolean free_segment) |
211 | 15.2k | { |
212 | 15.2k | gchar *segment; |
213 | | |
214 | 15.2k | g_return_val_if_fail (string != NULL, NULL); |
215 | | |
216 | 15.2k | if (free_segment) |
217 | 6.26k | { |
218 | 6.26k | g_free (string->str); |
219 | 6.26k | segment = NULL; |
220 | 6.26k | } |
221 | 9.02k | else |
222 | 9.02k | segment = string->str; |
223 | | |
224 | 15.2k | g_slice_free (GString, string); |
225 | | |
226 | 15.2k | return segment; |
227 | 15.2k | } |
228 | | |
229 | | /** |
230 | | * g_string_free_and_steal: |
231 | | * @string: (transfer full): a #GString |
232 | | * |
233 | | * Frees the memory allocated for the #GString. |
234 | | * |
235 | | * The caller gains ownership of the buffer and |
236 | | * must free it after use with g_free(). |
237 | | * |
238 | | * Returns: (transfer full): the character data of @string |
239 | | * |
240 | | * Since: 2.76 |
241 | | */ |
242 | | gchar * |
243 | | g_string_free_and_steal (GString *string) |
244 | 9.02k | { |
245 | 9.02k | return (g_string_free) (string, FALSE); |
246 | 9.02k | } |
247 | | |
248 | | /** |
249 | | * g_string_free_to_bytes: |
250 | | * @string: (transfer full): a #GString |
251 | | * |
252 | | * Transfers ownership of the contents of @string to a newly allocated |
253 | | * #GBytes. The #GString structure itself is deallocated, and it is |
254 | | * therefore invalid to use @string after invoking this function. |
255 | | * |
256 | | * Note that while #GString ensures that its buffer always has a |
257 | | * trailing nul character (not reflected in its "len"), the returned |
258 | | * #GBytes does not include this extra nul; i.e. it has length exactly |
259 | | * equal to the "len" member. |
260 | | * |
261 | | * Returns: (transfer full): A newly allocated #GBytes containing contents of @string; @string itself is freed |
262 | | * Since: 2.34 |
263 | | */ |
264 | | GBytes* |
265 | | g_string_free_to_bytes (GString *string) |
266 | 0 | { |
267 | 0 | gsize len; |
268 | 0 | gchar *buf; |
269 | |
|
270 | 0 | g_return_val_if_fail (string != NULL, NULL); |
271 | | |
272 | 0 | len = string->len; |
273 | |
|
274 | 0 | buf = g_string_free (string, FALSE); |
275 | |
|
276 | 0 | return g_bytes_new_take (buf, len); |
277 | 0 | } |
278 | | |
279 | | /** |
280 | | * g_string_equal: |
281 | | * @v: a #GString |
282 | | * @v2: another #GString |
283 | | * |
284 | | * Compares two strings for equality, returning %TRUE if they are equal. |
285 | | * For use with #GHashTable. |
286 | | * |
287 | | * Returns: %TRUE if the strings are the same length and contain the |
288 | | * same bytes |
289 | | */ |
290 | | gboolean |
291 | | g_string_equal (const GString *v, |
292 | | const GString *v2) |
293 | 0 | { |
294 | 0 | gchar *p, *q; |
295 | 0 | GString *string1 = (GString *) v; |
296 | 0 | GString *string2 = (GString *) v2; |
297 | 0 | gsize i = string1->len; |
298 | |
|
299 | 0 | if (i != string2->len) |
300 | 0 | return FALSE; |
301 | | |
302 | 0 | p = string1->str; |
303 | 0 | q = string2->str; |
304 | 0 | while (i) |
305 | 0 | { |
306 | 0 | if (*p != *q) |
307 | 0 | return FALSE; |
308 | 0 | p++; |
309 | 0 | q++; |
310 | 0 | i--; |
311 | 0 | } |
312 | 0 | return TRUE; |
313 | 0 | } |
314 | | |
315 | | /** |
316 | | * g_string_hash: |
317 | | * @str: a string to hash |
318 | | * |
319 | | * Creates a hash code for @str; for use with #GHashTable. |
320 | | * |
321 | | * Returns: hash code for @str |
322 | | */ |
323 | | guint |
324 | | g_string_hash (const GString *str) |
325 | 0 | { |
326 | 0 | const gchar *p = str->str; |
327 | 0 | gsize n = str->len; |
328 | 0 | guint h = 0; |
329 | | |
330 | | /* 31 bit hash function */ |
331 | 0 | while (n--) |
332 | 0 | { |
333 | 0 | h = (h << 5) - h + *p; |
334 | 0 | p++; |
335 | 0 | } |
336 | |
|
337 | 0 | return h; |
338 | 0 | } |
339 | | |
340 | | /** |
341 | | * g_string_assign: |
342 | | * @string: the destination #GString. Its current contents |
343 | | * are destroyed. |
344 | | * @rval: the string to copy into @string |
345 | | * |
346 | | * Copies the bytes from a string into a #GString, |
347 | | * destroying any previous contents. It is rather like |
348 | | * the standard strcpy() function, except that you do not |
349 | | * have to worry about having enough space to copy the string. |
350 | | * |
351 | | * Returns: (transfer none): @string |
352 | | */ |
353 | | GString * |
354 | | g_string_assign (GString *string, |
355 | | const gchar *rval) |
356 | 0 | { |
357 | 0 | g_return_val_if_fail (string != NULL, NULL); |
358 | 0 | g_return_val_if_fail (rval != NULL, string); |
359 | | |
360 | | /* Make sure assigning to itself doesn't corrupt the string. */ |
361 | 0 | if (string->str != rval) |
362 | 0 | { |
363 | | /* Assigning from substring should be ok, since |
364 | | * g_string_truncate() does not reallocate. |
365 | | */ |
366 | 0 | g_string_truncate (string, 0); |
367 | 0 | g_string_append (string, rval); |
368 | 0 | } |
369 | |
|
370 | 0 | return string; |
371 | 0 | } |
372 | | |
373 | | /** |
374 | | * g_string_truncate: |
375 | | * @string: a #GString |
376 | | * @len: the new size of @string |
377 | | * |
378 | | * Cuts off the end of the GString, leaving the first @len bytes. |
379 | | * |
380 | | * Returns: (transfer none): @string |
381 | | */ |
382 | | GString * |
383 | | (g_string_truncate) (GString *string, |
384 | | gsize len) |
385 | 0 | { |
386 | 0 | g_return_val_if_fail (string != NULL, NULL); |
387 | | |
388 | 0 | string->len = MIN (len, string->len); |
389 | 0 | string->str[string->len] = 0; |
390 | |
|
391 | 0 | return string; |
392 | 0 | } |
393 | | |
394 | | /** |
395 | | * g_string_set_size: |
396 | | * @string: a #GString |
397 | | * @len: the new length |
398 | | * |
399 | | * Sets the length of a #GString. If the length is less than |
400 | | * the current length, the string will be truncated. If the |
401 | | * length is greater than the current length, the contents |
402 | | * of the newly added area are undefined. (However, as |
403 | | * always, string->str[string->len] will be a nul byte.) |
404 | | * |
405 | | * Returns: (transfer none): @string |
406 | | */ |
407 | | GString * |
408 | | g_string_set_size (GString *string, |
409 | | gsize len) |
410 | 0 | { |
411 | 0 | g_return_val_if_fail (string != NULL, NULL); |
412 | | |
413 | 0 | if (len >= string->allocated_len) |
414 | 0 | g_string_maybe_expand (string, len - string->len); |
415 | |
|
416 | 0 | string->len = len; |
417 | 0 | string->str[len] = 0; |
418 | |
|
419 | 0 | return string; |
420 | 0 | } |
421 | | |
422 | | /** |
423 | | * g_string_insert_len: |
424 | | * @string: a #GString |
425 | | * @pos: position in @string where insertion should |
426 | | * happen, or -1 for at the end |
427 | | * @val: bytes to insert |
428 | | * @len: number of bytes of @val to insert, or -1 for all of @val |
429 | | * |
430 | | * Inserts @len bytes of @val into @string at @pos. |
431 | | * |
432 | | * If @len is positive, @val may contain embedded nuls and need |
433 | | * not be nul-terminated. It is the caller's responsibility to |
434 | | * ensure that @val has at least @len addressable bytes. |
435 | | * |
436 | | * If @len is negative, @val must be nul-terminated and @len |
437 | | * is considered to request the entire string length. |
438 | | * |
439 | | * If @pos is -1, bytes are inserted at the end of the string. |
440 | | * |
441 | | * Returns: (transfer none): @string |
442 | | */ |
443 | | GString * |
444 | | g_string_insert_len (GString *string, |
445 | | gssize pos, |
446 | | const gchar *val, |
447 | | gssize len) |
448 | 2.22k | { |
449 | 2.22k | gsize len_unsigned, pos_unsigned; |
450 | | |
451 | 2.22k | g_return_val_if_fail (string != NULL, NULL); |
452 | 2.22k | g_return_val_if_fail (len == 0 || val != NULL, string); |
453 | | |
454 | 2.22k | if (len == 0) |
455 | 0 | return string; |
456 | | |
457 | 2.22k | if (len < 0) |
458 | 0 | len = strlen (val); |
459 | 2.22k | len_unsigned = len; |
460 | | |
461 | 2.22k | if (pos < 0) |
462 | 2.22k | pos_unsigned = string->len; |
463 | 0 | else |
464 | 0 | { |
465 | 0 | pos_unsigned = pos; |
466 | 0 | g_return_val_if_fail (pos_unsigned <= string->len, string); |
467 | 0 | } |
468 | | |
469 | | /* Check whether val represents a substring of string. |
470 | | * This test probably violates chapter and verse of the C standards, |
471 | | * since ">=" and "<=" are only valid when val really is a substring. |
472 | | * In practice, it will work on modern archs. |
473 | | */ |
474 | 2.22k | if (G_UNLIKELY (val >= string->str && val <= string->str + string->len)) |
475 | 0 | { |
476 | 0 | gsize offset = val - string->str; |
477 | 0 | gsize precount = 0; |
478 | |
|
479 | 0 | g_string_maybe_expand (string, len_unsigned); |
480 | 0 | val = string->str + offset; |
481 | | /* At this point, val is valid again. */ |
482 | | |
483 | | /* Open up space where we are going to insert. */ |
484 | 0 | if (pos_unsigned < string->len) |
485 | 0 | memmove (string->str + pos_unsigned + len_unsigned, |
486 | 0 | string->str + pos_unsigned, string->len - pos_unsigned); |
487 | | |
488 | | /* Move the source part before the gap, if any. */ |
489 | 0 | if (offset < pos_unsigned) |
490 | 0 | { |
491 | 0 | precount = MIN (len_unsigned, pos_unsigned - offset); |
492 | 0 | memcpy (string->str + pos_unsigned, val, precount); |
493 | 0 | } |
494 | | |
495 | | /* Move the source part after the gap, if any. */ |
496 | 0 | if (len_unsigned > precount) |
497 | 0 | memcpy (string->str + pos_unsigned + precount, |
498 | 0 | val + /* Already moved: */ precount + |
499 | 0 | /* Space opened up: */ len_unsigned, |
500 | 0 | len_unsigned - precount); |
501 | 0 | } |
502 | 2.22k | else |
503 | 2.22k | { |
504 | 2.22k | g_string_maybe_expand (string, len_unsigned); |
505 | | |
506 | | /* If we aren't appending at the end, move a hunk |
507 | | * of the old string to the end, opening up space |
508 | | */ |
509 | 2.22k | if (pos_unsigned < string->len) |
510 | 0 | memmove (string->str + pos_unsigned + len_unsigned, |
511 | 0 | string->str + pos_unsigned, string->len - pos_unsigned); |
512 | | |
513 | | /* insert the new string */ |
514 | 2.22k | if (len_unsigned == 1) |
515 | 133 | string->str[pos_unsigned] = *val; |
516 | 2.09k | else |
517 | 2.09k | memcpy (string->str + pos_unsigned, val, len_unsigned); |
518 | 2.22k | } |
519 | | |
520 | 2.22k | string->len += len_unsigned; |
521 | | |
522 | 2.22k | string->str[string->len] = 0; |
523 | | |
524 | 2.22k | return string; |
525 | 2.22k | } |
526 | | |
527 | | /** |
528 | | * g_string_append_uri_escaped: |
529 | | * @string: a #GString |
530 | | * @unescaped: a string |
531 | | * @reserved_chars_allowed: a string of reserved characters allowed |
532 | | * to be used, or %NULL |
533 | | * @allow_utf8: set %TRUE if the escaped string may include UTF8 characters |
534 | | * |
535 | | * Appends @unescaped to @string, escaping any characters that |
536 | | * are reserved in URIs using URI-style escape sequences. |
537 | | * |
538 | | * Returns: (transfer none): @string |
539 | | * |
540 | | * Since: 2.16 |
541 | | */ |
542 | | GString * |
543 | | g_string_append_uri_escaped (GString *string, |
544 | | const gchar *unescaped, |
545 | | const gchar *reserved_chars_allowed, |
546 | | gboolean allow_utf8) |
547 | 0 | { |
548 | 0 | _uri_encoder (string, (const guchar *) unescaped, strlen (unescaped), |
549 | 0 | reserved_chars_allowed, allow_utf8); |
550 | 0 | return string; |
551 | 0 | } |
552 | | |
553 | | /** |
554 | | * g_string_append: |
555 | | * @string: a #GString |
556 | | * @val: the string to append onto the end of @string |
557 | | * |
558 | | * Adds a string onto the end of a #GString, expanding |
559 | | * it if necessary. |
560 | | * |
561 | | * Returns: (transfer none): @string |
562 | | */ |
563 | | GString * |
564 | | (g_string_append) (GString *string, |
565 | | const gchar *val) |
566 | 0 | { |
567 | 0 | return g_string_insert_len (string, -1, val, -1); |
568 | 0 | } |
569 | | |
570 | | /** |
571 | | * g_string_append_len: |
572 | | * @string: a #GString |
573 | | * @val: bytes to append |
574 | | * @len: number of bytes of @val to use, or -1 for all of @val |
575 | | * |
576 | | * Appends @len bytes of @val to @string. |
577 | | * |
578 | | * If @len is positive, @val may contain embedded nuls and need |
579 | | * not be nul-terminated. It is the caller's responsibility to |
580 | | * ensure that @val has at least @len addressable bytes. |
581 | | * |
582 | | * If @len is negative, @val must be nul-terminated and @len |
583 | | * is considered to request the entire string length. This |
584 | | * makes g_string_append_len() equivalent to g_string_append(). |
585 | | * |
586 | | * Returns: (transfer none): @string |
587 | | */ |
588 | | GString * |
589 | | (g_string_append_len) (GString *string, |
590 | | const gchar *val, |
591 | | gssize len) |
592 | 0 | { |
593 | 0 | return g_string_insert_len (string, -1, val, len); |
594 | 0 | } |
595 | | |
596 | | /** |
597 | | * g_string_append_c: |
598 | | * @string: a #GString |
599 | | * @c: the byte to append onto the end of @string |
600 | | * |
601 | | * Adds a byte onto the end of a #GString, expanding |
602 | | * it if necessary. |
603 | | * |
604 | | * Returns: (transfer none): @string |
605 | | */ |
606 | | GString * |
607 | | (g_string_append_c) (GString *string, |
608 | | gchar c) |
609 | 0 | { |
610 | 0 | g_return_val_if_fail (string != NULL, NULL); |
611 | | |
612 | 0 | return g_string_insert_c (string, -1, c); |
613 | 0 | } |
614 | | |
615 | | /** |
616 | | * g_string_append_unichar: |
617 | | * @string: a #GString |
618 | | * @wc: a Unicode character |
619 | | * |
620 | | * Converts a Unicode character into UTF-8, and appends it |
621 | | * to the string. |
622 | | * |
623 | | * Returns: (transfer none): @string |
624 | | */ |
625 | | GString * |
626 | | g_string_append_unichar (GString *string, |
627 | | gunichar wc) |
628 | 0 | { |
629 | 0 | g_return_val_if_fail (string != NULL, NULL); |
630 | | |
631 | 0 | return g_string_insert_unichar (string, -1, wc); |
632 | 0 | } |
633 | | |
634 | | /** |
635 | | * g_string_prepend: |
636 | | * @string: a #GString |
637 | | * @val: the string to prepend on the start of @string |
638 | | * |
639 | | * Adds a string on to the start of a #GString, |
640 | | * expanding it if necessary. |
641 | | * |
642 | | * Returns: (transfer none): @string |
643 | | */ |
644 | | GString * |
645 | | g_string_prepend (GString *string, |
646 | | const gchar *val) |
647 | 0 | { |
648 | 0 | return g_string_insert_len (string, 0, val, -1); |
649 | 0 | } |
650 | | |
651 | | /** |
652 | | * g_string_prepend_len: |
653 | | * @string: a #GString |
654 | | * @val: bytes to prepend |
655 | | * @len: number of bytes in @val to prepend, or -1 for all of @val |
656 | | * |
657 | | * Prepends @len bytes of @val to @string. |
658 | | * |
659 | | * If @len is positive, @val may contain embedded nuls and need |
660 | | * not be nul-terminated. It is the caller's responsibility to |
661 | | * ensure that @val has at least @len addressable bytes. |
662 | | * |
663 | | * If @len is negative, @val must be nul-terminated and @len |
664 | | * is considered to request the entire string length. This |
665 | | * makes g_string_prepend_len() equivalent to g_string_prepend(). |
666 | | * |
667 | | * Returns: (transfer none): @string |
668 | | */ |
669 | | GString * |
670 | | g_string_prepend_len (GString *string, |
671 | | const gchar *val, |
672 | | gssize len) |
673 | 0 | { |
674 | 0 | return g_string_insert_len (string, 0, val, len); |
675 | 0 | } |
676 | | |
677 | | /** |
678 | | * g_string_prepend_c: |
679 | | * @string: a #GString |
680 | | * @c: the byte to prepend on the start of the #GString |
681 | | * |
682 | | * Adds a byte onto the start of a #GString, |
683 | | * expanding it if necessary. |
684 | | * |
685 | | * Returns: (transfer none): @string |
686 | | */ |
687 | | GString * |
688 | | g_string_prepend_c (GString *string, |
689 | | gchar c) |
690 | 0 | { |
691 | 0 | g_return_val_if_fail (string != NULL, NULL); |
692 | | |
693 | 0 | return g_string_insert_c (string, 0, c); |
694 | 0 | } |
695 | | |
696 | | /** |
697 | | * g_string_prepend_unichar: |
698 | | * @string: a #GString |
699 | | * @wc: a Unicode character |
700 | | * |
701 | | * Converts a Unicode character into UTF-8, and prepends it |
702 | | * to the string. |
703 | | * |
704 | | * Returns: (transfer none): @string |
705 | | */ |
706 | | GString * |
707 | | g_string_prepend_unichar (GString *string, |
708 | | gunichar wc) |
709 | 0 | { |
710 | 0 | g_return_val_if_fail (string != NULL, NULL); |
711 | | |
712 | 0 | return g_string_insert_unichar (string, 0, wc); |
713 | 0 | } |
714 | | |
715 | | /** |
716 | | * g_string_insert: |
717 | | * @string: a #GString |
718 | | * @pos: the position to insert the copy of the string |
719 | | * @val: the string to insert |
720 | | * |
721 | | * Inserts a copy of a string into a #GString, |
722 | | * expanding it if necessary. |
723 | | * |
724 | | * Returns: (transfer none): @string |
725 | | */ |
726 | | GString * |
727 | | g_string_insert (GString *string, |
728 | | gssize pos, |
729 | | const gchar *val) |
730 | 0 | { |
731 | 0 | return g_string_insert_len (string, pos, val, -1); |
732 | 0 | } |
733 | | |
734 | | /** |
735 | | * g_string_insert_c: |
736 | | * @string: a #GString |
737 | | * @pos: the position to insert the byte |
738 | | * @c: the byte to insert |
739 | | * |
740 | | * Inserts a byte into a #GString, expanding it if necessary. |
741 | | * |
742 | | * Returns: (transfer none): @string |
743 | | */ |
744 | | GString * |
745 | | g_string_insert_c (GString *string, |
746 | | gssize pos, |
747 | | gchar c) |
748 | 82 | { |
749 | 82 | gsize pos_unsigned; |
750 | | |
751 | 82 | g_return_val_if_fail (string != NULL, NULL); |
752 | | |
753 | 82 | g_string_maybe_expand (string, 1); |
754 | | |
755 | 82 | if (pos < 0) |
756 | 82 | pos = string->len; |
757 | 0 | else |
758 | 82 | g_return_val_if_fail ((gsize) pos <= string->len, string); |
759 | 82 | pos_unsigned = pos; |
760 | | |
761 | | /* If not just an append, move the old stuff */ |
762 | 82 | if (pos_unsigned < string->len) |
763 | 0 | memmove (string->str + pos_unsigned + 1, |
764 | 0 | string->str + pos_unsigned, string->len - pos_unsigned); |
765 | | |
766 | 82 | string->str[pos_unsigned] = c; |
767 | | |
768 | 82 | string->len += 1; |
769 | | |
770 | 82 | string->str[string->len] = 0; |
771 | | |
772 | 82 | return string; |
773 | 82 | } |
774 | | |
775 | | /** |
776 | | * g_string_insert_unichar: |
777 | | * @string: a #GString |
778 | | * @pos: the position at which to insert character, or -1 |
779 | | * to append at the end of the string |
780 | | * @wc: a Unicode character |
781 | | * |
782 | | * Converts a Unicode character into UTF-8, and insert it |
783 | | * into the string at the given position. |
784 | | * |
785 | | * Returns: (transfer none): @string |
786 | | */ |
787 | | GString * |
788 | | g_string_insert_unichar (GString *string, |
789 | | gssize pos, |
790 | | gunichar wc) |
791 | 0 | { |
792 | 0 | gint charlen, first, i; |
793 | 0 | gchar *dest; |
794 | |
|
795 | 0 | g_return_val_if_fail (string != NULL, NULL); |
796 | | |
797 | | /* Code copied from g_unichar_to_utf() */ |
798 | 0 | if (wc < 0x80) |
799 | 0 | { |
800 | 0 | first = 0; |
801 | 0 | charlen = 1; |
802 | 0 | } |
803 | 0 | else if (wc < 0x800) |
804 | 0 | { |
805 | 0 | first = 0xc0; |
806 | 0 | charlen = 2; |
807 | 0 | } |
808 | 0 | else if (wc < 0x10000) |
809 | 0 | { |
810 | 0 | first = 0xe0; |
811 | 0 | charlen = 3; |
812 | 0 | } |
813 | 0 | else if (wc < 0x200000) |
814 | 0 | { |
815 | 0 | first = 0xf0; |
816 | 0 | charlen = 4; |
817 | 0 | } |
818 | 0 | else if (wc < 0x4000000) |
819 | 0 | { |
820 | 0 | first = 0xf8; |
821 | 0 | charlen = 5; |
822 | 0 | } |
823 | 0 | else |
824 | 0 | { |
825 | 0 | first = 0xfc; |
826 | 0 | charlen = 6; |
827 | 0 | } |
828 | | /* End of copied code */ |
829 | |
|
830 | 0 | g_string_maybe_expand (string, charlen); |
831 | |
|
832 | 0 | if (pos < 0) |
833 | 0 | pos = string->len; |
834 | 0 | else |
835 | 0 | g_return_val_if_fail ((gsize) pos <= string->len, string); |
836 | | |
837 | | /* If not just an append, move the old stuff */ |
838 | 0 | if ((gsize) pos < string->len) |
839 | 0 | memmove (string->str + pos + charlen, string->str + pos, string->len - pos); |
840 | |
|
841 | 0 | dest = string->str + pos; |
842 | | /* Code copied from g_unichar_to_utf() */ |
843 | 0 | for (i = charlen - 1; i > 0; --i) |
844 | 0 | { |
845 | 0 | dest[i] = (wc & 0x3f) | 0x80; |
846 | 0 | wc >>= 6; |
847 | 0 | } |
848 | 0 | dest[0] = wc | first; |
849 | | /* End of copied code */ |
850 | |
|
851 | 0 | string->len += charlen; |
852 | |
|
853 | 0 | string->str[string->len] = 0; |
854 | |
|
855 | 0 | return string; |
856 | 0 | } |
857 | | |
858 | | /** |
859 | | * g_string_overwrite: |
860 | | * @string: a #GString |
861 | | * @pos: the position at which to start overwriting |
862 | | * @val: the string that will overwrite the @string starting at @pos |
863 | | * |
864 | | * Overwrites part of a string, lengthening it if necessary. |
865 | | * |
866 | | * Returns: (transfer none): @string |
867 | | * |
868 | | * Since: 2.14 |
869 | | */ |
870 | | GString * |
871 | | g_string_overwrite (GString *string, |
872 | | gsize pos, |
873 | | const gchar *val) |
874 | 0 | { |
875 | 0 | g_return_val_if_fail (val != NULL, string); |
876 | 0 | return g_string_overwrite_len (string, pos, val, strlen (val)); |
877 | 0 | } |
878 | | |
879 | | /** |
880 | | * g_string_overwrite_len: |
881 | | * @string: a #GString |
882 | | * @pos: the position at which to start overwriting |
883 | | * @val: the string that will overwrite the @string starting at @pos |
884 | | * @len: the number of bytes to write from @val |
885 | | * |
886 | | * Overwrites part of a string, lengthening it if necessary. |
887 | | * This function will work with embedded nuls. |
888 | | * |
889 | | * Returns: (transfer none): @string |
890 | | * |
891 | | * Since: 2.14 |
892 | | */ |
893 | | GString * |
894 | | g_string_overwrite_len (GString *string, |
895 | | gsize pos, |
896 | | const gchar *val, |
897 | | gssize len) |
898 | 0 | { |
899 | 0 | gsize end; |
900 | |
|
901 | 0 | g_return_val_if_fail (string != NULL, NULL); |
902 | | |
903 | 0 | if (!len) |
904 | 0 | return string; |
905 | | |
906 | 0 | g_return_val_if_fail (val != NULL, string); |
907 | 0 | g_return_val_if_fail (pos <= string->len, string); |
908 | | |
909 | 0 | if (len < 0) |
910 | 0 | len = strlen (val); |
911 | |
|
912 | 0 | end = pos + len; |
913 | |
|
914 | 0 | if (end > string->len) |
915 | 0 | g_string_maybe_expand (string, end - string->len); |
916 | |
|
917 | 0 | memcpy (string->str + pos, val, len); |
918 | |
|
919 | 0 | if (end > string->len) |
920 | 0 | { |
921 | 0 | string->str[end] = '\0'; |
922 | 0 | string->len = end; |
923 | 0 | } |
924 | |
|
925 | 0 | return string; |
926 | 0 | } |
927 | | |
928 | | /** |
929 | | * g_string_erase: |
930 | | * @string: a #GString |
931 | | * @pos: the position of the content to remove |
932 | | * @len: the number of bytes to remove, or -1 to remove all |
933 | | * following bytes |
934 | | * |
935 | | * Removes @len bytes from a #GString, starting at position @pos. |
936 | | * The rest of the #GString is shifted down to fill the gap. |
937 | | * |
938 | | * Returns: (transfer none): @string |
939 | | */ |
940 | | GString * |
941 | | g_string_erase (GString *string, |
942 | | gssize pos, |
943 | | gssize len) |
944 | 293k | { |
945 | 293k | gsize len_unsigned, pos_unsigned; |
946 | | |
947 | 293k | g_return_val_if_fail (string != NULL, NULL); |
948 | 293k | g_return_val_if_fail (pos >= 0, string); |
949 | 293k | pos_unsigned = pos; |
950 | | |
951 | 293k | g_return_val_if_fail (pos_unsigned <= string->len, string); |
952 | | |
953 | 293k | if (len < 0) |
954 | 292k | len_unsigned = string->len - pos_unsigned; |
955 | 1.04k | else |
956 | 1.04k | { |
957 | 1.04k | len_unsigned = len; |
958 | 1.04k | g_return_val_if_fail (pos_unsigned + len_unsigned <= string->len, string); |
959 | | |
960 | 1.04k | if (pos_unsigned + len_unsigned < string->len) |
961 | 0 | memmove (string->str + pos_unsigned, |
962 | 0 | string->str + pos_unsigned + len_unsigned, |
963 | 0 | string->len - (pos_unsigned + len_unsigned)); |
964 | 1.04k | } |
965 | | |
966 | 293k | string->len -= len_unsigned; |
967 | | |
968 | 293k | string->str[string->len] = 0; |
969 | | |
970 | 293k | return string; |
971 | 293k | } |
972 | | |
973 | | /** |
974 | | * g_string_replace: |
975 | | * @string: a #GString |
976 | | * @find: the string to find in @string |
977 | | * @replace: the string to insert in place of @find |
978 | | * @limit: the maximum instances of @find to replace with @replace, or `0` for |
979 | | * no limit |
980 | | * |
981 | | * Replaces the string @find with the string @replace in a #GString up to |
982 | | * @limit times. If the number of instances of @find in the #GString is |
983 | | * less than @limit, all instances are replaced. If @limit is `0`, |
984 | | * all instances of @find are replaced. |
985 | | * |
986 | | * If @find is the empty string, since versions 2.69.1 and 2.68.4 the |
987 | | * replacement will be inserted no more than once per possible position |
988 | | * (beginning of string, end of string and between characters). This did |
989 | | * not work correctly in earlier versions. |
990 | | * |
991 | | * Returns: the number of find and replace operations performed. |
992 | | * |
993 | | * Since: 2.68 |
994 | | */ |
995 | | guint |
996 | | g_string_replace (GString *string, |
997 | | const gchar *find, |
998 | | const gchar *replace, |
999 | | guint limit) |
1000 | 0 | { |
1001 | 0 | gsize f_len, r_len, pos; |
1002 | 0 | gchar *cur, *next; |
1003 | 0 | guint n = 0; |
1004 | |
|
1005 | 0 | g_return_val_if_fail (string != NULL, 0); |
1006 | 0 | g_return_val_if_fail (find != NULL, 0); |
1007 | 0 | g_return_val_if_fail (replace != NULL, 0); |
1008 | | |
1009 | 0 | f_len = strlen (find); |
1010 | 0 | r_len = strlen (replace); |
1011 | 0 | cur = string->str; |
1012 | |
|
1013 | 0 | while ((next = strstr (cur, find)) != NULL) |
1014 | 0 | { |
1015 | 0 | pos = next - string->str; |
1016 | 0 | g_string_erase (string, pos, f_len); |
1017 | 0 | g_string_insert (string, pos, replace); |
1018 | 0 | cur = string->str + pos + r_len; |
1019 | 0 | n++; |
1020 | | /* Only match the empty string once at any given position, to |
1021 | | * avoid infinite loops */ |
1022 | 0 | if (f_len == 0) |
1023 | 0 | { |
1024 | 0 | if (cur[0] == '\0') |
1025 | 0 | break; |
1026 | 0 | else |
1027 | 0 | cur++; |
1028 | 0 | } |
1029 | 0 | if (n == limit) |
1030 | 0 | break; |
1031 | 0 | } |
1032 | |
|
1033 | 0 | return n; |
1034 | 0 | } |
1035 | | |
1036 | | /** |
1037 | | * g_string_ascii_down: |
1038 | | * @string: a GString |
1039 | | * |
1040 | | * Converts all uppercase ASCII letters to lowercase ASCII letters. |
1041 | | * |
1042 | | * Returns: (transfer none): passed-in @string pointer, with all the |
1043 | | * uppercase characters converted to lowercase in place, |
1044 | | * with semantics that exactly match g_ascii_tolower(). |
1045 | | */ |
1046 | | GString * |
1047 | | g_string_ascii_down (GString *string) |
1048 | 0 | { |
1049 | 0 | gchar *s; |
1050 | 0 | gint n; |
1051 | |
|
1052 | 0 | g_return_val_if_fail (string != NULL, NULL); |
1053 | | |
1054 | 0 | n = string->len; |
1055 | 0 | s = string->str; |
1056 | |
|
1057 | 0 | while (n) |
1058 | 0 | { |
1059 | 0 | *s = g_ascii_tolower (*s); |
1060 | 0 | s++; |
1061 | 0 | n--; |
1062 | 0 | } |
1063 | |
|
1064 | 0 | return string; |
1065 | 0 | } |
1066 | | |
1067 | | /** |
1068 | | * g_string_ascii_up: |
1069 | | * @string: a GString |
1070 | | * |
1071 | | * Converts all lowercase ASCII letters to uppercase ASCII letters. |
1072 | | * |
1073 | | * Returns: (transfer none): passed-in @string pointer, with all the |
1074 | | * lowercase characters converted to uppercase in place, |
1075 | | * with semantics that exactly match g_ascii_toupper(). |
1076 | | */ |
1077 | | GString * |
1078 | | g_string_ascii_up (GString *string) |
1079 | 0 | { |
1080 | 0 | gchar *s; |
1081 | 0 | gint n; |
1082 | |
|
1083 | 0 | g_return_val_if_fail (string != NULL, NULL); |
1084 | | |
1085 | 0 | n = string->len; |
1086 | 0 | s = string->str; |
1087 | |
|
1088 | 0 | while (n) |
1089 | 0 | { |
1090 | 0 | *s = g_ascii_toupper (*s); |
1091 | 0 | s++; |
1092 | 0 | n--; |
1093 | 0 | } |
1094 | |
|
1095 | 0 | return string; |
1096 | 0 | } |
1097 | | |
1098 | | /** |
1099 | | * g_string_down: |
1100 | | * @string: a #GString |
1101 | | * |
1102 | | * Converts a #GString to lowercase. |
1103 | | * |
1104 | | * Returns: (transfer none): the #GString |
1105 | | * |
1106 | | * Deprecated:2.2: This function uses the locale-specific |
1107 | | * tolower() function, which is almost never the right thing. |
1108 | | * Use g_string_ascii_down() or g_utf8_strdown() instead. |
1109 | | */ |
1110 | | GString * |
1111 | | g_string_down (GString *string) |
1112 | 0 | { |
1113 | 0 | guchar *s; |
1114 | 0 | glong n; |
1115 | |
|
1116 | 0 | g_return_val_if_fail (string != NULL, NULL); |
1117 | | |
1118 | 0 | n = string->len; |
1119 | 0 | s = (guchar *) string->str; |
1120 | |
|
1121 | 0 | while (n) |
1122 | 0 | { |
1123 | 0 | if (isupper (*s)) |
1124 | 0 | *s = tolower (*s); |
1125 | 0 | s++; |
1126 | 0 | n--; |
1127 | 0 | } |
1128 | |
|
1129 | 0 | return string; |
1130 | 0 | } |
1131 | | |
1132 | | /** |
1133 | | * g_string_up: |
1134 | | * @string: a #GString |
1135 | | * |
1136 | | * Converts a #GString to uppercase. |
1137 | | * |
1138 | | * Returns: (transfer none): @string |
1139 | | * |
1140 | | * Deprecated:2.2: This function uses the locale-specific |
1141 | | * toupper() function, which is almost never the right thing. |
1142 | | * Use g_string_ascii_up() or g_utf8_strup() instead. |
1143 | | */ |
1144 | | GString * |
1145 | | g_string_up (GString *string) |
1146 | 0 | { |
1147 | 0 | guchar *s; |
1148 | 0 | glong n; |
1149 | |
|
1150 | 0 | g_return_val_if_fail (string != NULL, NULL); |
1151 | | |
1152 | 0 | n = string->len; |
1153 | 0 | s = (guchar *) string->str; |
1154 | |
|
1155 | 0 | while (n) |
1156 | 0 | { |
1157 | 0 | if (islower (*s)) |
1158 | 0 | *s = toupper (*s); |
1159 | 0 | s++; |
1160 | 0 | n--; |
1161 | 0 | } |
1162 | |
|
1163 | 0 | return string; |
1164 | 0 | } |
1165 | | |
1166 | | /** |
1167 | | * g_string_append_vprintf: |
1168 | | * @string: a #GString |
1169 | | * @format: (not nullable): the string format. See the printf() documentation |
1170 | | * @args: the list of arguments to insert in the output |
1171 | | * |
1172 | | * Appends a formatted string onto the end of a #GString. |
1173 | | * This function is similar to g_string_append_printf() |
1174 | | * except that the arguments to the format string are passed |
1175 | | * as a va_list. |
1176 | | * |
1177 | | * Since: 2.14 |
1178 | | */ |
1179 | | void |
1180 | | g_string_append_vprintf (GString *string, |
1181 | | const gchar *format, |
1182 | | va_list args) |
1183 | 0 | { |
1184 | 0 | gchar *buf; |
1185 | 0 | gint len; |
1186 | |
|
1187 | 0 | g_return_if_fail (string != NULL); |
1188 | 0 | g_return_if_fail (format != NULL); |
1189 | | |
1190 | 0 | len = g_vasprintf (&buf, format, args); |
1191 | |
|
1192 | 0 | if (len >= 0) |
1193 | 0 | { |
1194 | 0 | g_string_maybe_expand (string, len); |
1195 | 0 | memcpy (string->str + string->len, buf, len + 1); |
1196 | 0 | string->len += len; |
1197 | 0 | g_free (buf); |
1198 | 0 | } |
1199 | 0 | } |
1200 | | |
1201 | | /** |
1202 | | * g_string_vprintf: |
1203 | | * @string: a #GString |
1204 | | * @format: (not nullable): the string format. See the printf() documentation |
1205 | | * @args: the parameters to insert into the format string |
1206 | | * |
1207 | | * Writes a formatted string into a #GString. |
1208 | | * This function is similar to g_string_printf() except that |
1209 | | * the arguments to the format string are passed as a va_list. |
1210 | | * |
1211 | | * Since: 2.14 |
1212 | | */ |
1213 | | void |
1214 | | g_string_vprintf (GString *string, |
1215 | | const gchar *format, |
1216 | | va_list args) |
1217 | 0 | { |
1218 | 0 | g_string_truncate (string, 0); |
1219 | 0 | g_string_append_vprintf (string, format, args); |
1220 | 0 | } |
1221 | | |
1222 | | /** |
1223 | | * g_string_sprintf: |
1224 | | * @string: a #GString |
1225 | | * @format: the string format. See the sprintf() documentation |
1226 | | * @...: the parameters to insert into the format string |
1227 | | * |
1228 | | * Writes a formatted string into a #GString. |
1229 | | * This is similar to the standard sprintf() function, |
1230 | | * except that the #GString buffer automatically expands |
1231 | | * to contain the results. The previous contents of the |
1232 | | * #GString are destroyed. |
1233 | | * |
1234 | | * Deprecated: This function has been renamed to g_string_printf(). |
1235 | | */ |
1236 | | |
1237 | | /** |
1238 | | * g_string_printf: |
1239 | | * @string: a #GString |
1240 | | * @format: the string format. See the printf() documentation |
1241 | | * @...: the parameters to insert into the format string |
1242 | | * |
1243 | | * Writes a formatted string into a #GString. |
1244 | | * This is similar to the standard sprintf() function, |
1245 | | * except that the #GString buffer automatically expands |
1246 | | * to contain the results. The previous contents of the |
1247 | | * #GString are destroyed. |
1248 | | */ |
1249 | | void |
1250 | | g_string_printf (GString *string, |
1251 | | const gchar *format, |
1252 | | ...) |
1253 | 0 | { |
1254 | 0 | va_list args; |
1255 | |
|
1256 | 0 | g_string_truncate (string, 0); |
1257 | |
|
1258 | 0 | va_start (args, format); |
1259 | 0 | g_string_append_vprintf (string, format, args); |
1260 | 0 | va_end (args); |
1261 | 0 | } |
1262 | | |
1263 | | /** |
1264 | | * g_string_sprintfa: |
1265 | | * @string: a #GString |
1266 | | * @format: the string format. See the sprintf() documentation |
1267 | | * @...: the parameters to insert into the format string |
1268 | | * |
1269 | | * Appends a formatted string onto the end of a #GString. |
1270 | | * This function is similar to g_string_sprintf() except that |
1271 | | * the text is appended to the #GString. |
1272 | | * |
1273 | | * Deprecated: This function has been renamed to g_string_append_printf() |
1274 | | */ |
1275 | | |
1276 | | /** |
1277 | | * g_string_append_printf: |
1278 | | * @string: a #GString |
1279 | | * @format: the string format. See the printf() documentation |
1280 | | * @...: the parameters to insert into the format string |
1281 | | * |
1282 | | * Appends a formatted string onto the end of a #GString. |
1283 | | * This function is similar to g_string_printf() except |
1284 | | * that the text is appended to the #GString. |
1285 | | */ |
1286 | | void |
1287 | | g_string_append_printf (GString *string, |
1288 | | const gchar *format, |
1289 | | ...) |
1290 | 0 | { |
1291 | 0 | va_list args; |
1292 | |
|
1293 | 0 | va_start (args, format); |
1294 | 0 | g_string_append_vprintf (string, format, args); |
1295 | 0 | va_end (args); |
1296 | 0 | } |