1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
38 #include "guriprivate.h"
45 * @short_description: text buffers which grow automatically
48 * A #GString is an object that handles the memory management of a C
49 * string for you. The emphasis of #GString is on text, typically
50 * UTF-8. Crucially, the "str" member of a #GString is guaranteed to
51 * have a trailing nul character, and it is therefore always safe to
52 * call functions such as strchr() or g_strdup() on it.
54 * However, a #GString can also hold arbitrary binary data, because it
55 * has a "len" member, which includes any possible embedded nul
56 * characters in the data. Conceptually then, #GString is like a
57 * #GByteArray with the addition of many convenience methods for text,
58 * and a guaranteed nul terminator.
63 * @str: points to the character data. It may move as text is added.
64 * The @str field is null-terminated and so
65 * can be used as an ordinary C string.
66 * @len: contains the length of the string, not including the
67 * terminating nul byte.
68 * @allocated_len: the number of bytes that can be stored in the
69 * string before it needs to be reallocated. May be larger than @len.
71 * The GString struct contains the public fields of a GString.
75 #define MY_MAXSIZE ((gsize)-1)
78 nearest_power (gsize base, gsize num)
80 if (num > MY_MAXSIZE / 2)
96 g_string_maybe_expand (GString *string,
99 if (string->len + len >= string->allocated_len)
101 string->allocated_len = nearest_power (1, string->len + len + 1);
102 string->str = g_realloc (string->str, string->allocated_len);
107 * g_string_sized_new:
108 * @dfl_size: the default size of the space allocated to
111 * Creates a new #GString, with enough space for @dfl_size
112 * bytes. This is useful if you are going to add a lot of
113 * text to the string and don't want it to be reallocated
116 * Returns: the new #GString
119 g_string_sized_new (gsize dfl_size)
121 GString *string = g_slice_new (GString);
123 string->allocated_len = 0;
127 g_string_maybe_expand (string, MAX (dfl_size, 2));
135 * @init: (nullable): the initial text to copy into the string, or %NULL to
136 * start with an empty string
138 * Creates a new #GString, initialized with the given string.
140 * Returns: the new #GString
143 g_string_new (const gchar *init)
147 if (init == NULL || *init == '\0')
148 string = g_string_sized_new (2);
154 string = g_string_sized_new (len + 2);
156 g_string_append_len (string, init, len);
164 * @init: initial contents of the string
165 * @len: length of @init to use
167 * Creates a new #GString with @len bytes of the @init buffer.
168 * Because a length is provided, @init need not be nul-terminated,
169 * and can contain embedded nul bytes.
171 * Since this function does not stop at nul bytes, it is the caller's
172 * responsibility to ensure that @init has at least @len addressable
175 * Returns: a new #GString
178 g_string_new_len (const gchar *init,
184 return g_string_new (init);
187 string = g_string_sized_new (len);
190 g_string_append_len (string, init, len);
198 * @string: (transfer full): a #GString
199 * @free_segment: if %TRUE, the actual character data is freed as well
201 * Frees the memory allocated for the #GString.
202 * If @free_segment is %TRUE it also frees the character data. If
203 * it's %FALSE, the caller gains ownership of the buffer and must
204 * free it after use with g_free().
206 * Returns: (nullable): the character data of @string
207 * (i.e. %NULL if @free_segment is %TRUE)
210 g_string_free (GString *string,
211 gboolean free_segment)
215 g_return_val_if_fail (string != NULL, NULL);
219 g_free (string->str);
223 segment = string->str;
225 g_slice_free (GString, string);
231 * g_string_free_to_bytes:
232 * @string: (transfer full): a #GString
234 * Transfers ownership of the contents of @string to a newly allocated
235 * #GBytes. The #GString structure itself is deallocated, and it is
236 * therefore invalid to use @string after invoking this function.
238 * Note that while #GString ensures that its buffer always has a
239 * trailing nul character (not reflected in its "len"), the returned
240 * #GBytes does not include this extra nul; i.e. it has length exactly
241 * equal to the "len" member.
243 * Returns: (transfer full): A newly allocated #GBytes containing contents of @string; @string itself is freed
247 g_string_free_to_bytes (GString *string)
252 g_return_val_if_fail (string != NULL, NULL);
256 buf = g_string_free (string, FALSE);
258 return g_bytes_new_take (buf, len);
264 * @v2: another #GString
266 * Compares two strings for equality, returning %TRUE if they are equal.
267 * For use with #GHashTable.
269 * Returns: %TRUE if the strings are the same length and contain the
273 g_string_equal (const GString *v,
277 GString *string1 = (GString *) v;
278 GString *string2 = (GString *) v2;
279 gsize i = string1->len;
281 if (i != string2->len)
299 * @str: a string to hash
301 * Creates a hash code for @str; for use with #GHashTable.
303 * Returns: hash code for @str
306 g_string_hash (const GString *str)
308 const gchar *p = str->str;
312 /* 31 bit hash function */
315 h = (h << 5) - h + *p;
324 * @string: the destination #GString. Its current contents
326 * @rval: the string to copy into @string
328 * Copies the bytes from a string into a #GString,
329 * destroying any previous contents. It is rather like
330 * the standard strcpy() function, except that you do not
331 * have to worry about having enough space to copy the string.
333 * Returns: (transfer none): @string
336 g_string_assign (GString *string,
339 g_return_val_if_fail (string != NULL, NULL);
340 g_return_val_if_fail (rval != NULL, string);
342 /* Make sure assigning to itself doesn't corrupt the string. */
343 if (string->str != rval)
345 /* Assigning from substring should be ok, since
346 * g_string_truncate() does not reallocate.
348 g_string_truncate (string, 0);
349 g_string_append (string, rval);
357 * @string: a #GString
358 * @len: the new size of @string
360 * Cuts off the end of the GString, leaving the first @len bytes.
362 * Returns: (transfer none): @string
365 g_string_truncate (GString *string,
368 g_return_val_if_fail (string != NULL, NULL);
370 string->len = MIN (len, string->len);
371 string->str[string->len] = 0;
378 * @string: a #GString
379 * @len: the new length
381 * Sets the length of a #GString. If the length is less than
382 * the current length, the string will be truncated. If the
383 * length is greater than the current length, the contents
384 * of the newly added area are undefined. (However, as
385 * always, string->str[string->len] will be a nul byte.)
387 * Returns: (transfer none): @string
390 g_string_set_size (GString *string,
393 g_return_val_if_fail (string != NULL, NULL);
395 if (len >= string->allocated_len)
396 g_string_maybe_expand (string, len - string->len);
399 string->str[len] = 0;
405 * g_string_insert_len:
406 * @string: a #GString
407 * @pos: position in @string where insertion should
408 * happen, or -1 for at the end
409 * @val: bytes to insert
410 * @len: number of bytes of @val to insert, or -1 for all of @val
412 * Inserts @len bytes of @val into @string at @pos.
414 * If @len is positive, @val may contain embedded nuls and need
415 * not be nul-terminated. It is the caller's responsibility to
416 * ensure that @val has at least @len addressable bytes.
418 * If @len is negative, @val must be nul-terminated and @len
419 * is considered to request the entire string length.
421 * If @pos is -1, bytes are inserted at the end of the string.
423 * Returns: (transfer none): @string
426 g_string_insert_len (GString *string,
431 gsize len_unsigned, pos_unsigned;
433 g_return_val_if_fail (string != NULL, NULL);
434 g_return_val_if_fail (len == 0 || val != NULL, string);
444 pos_unsigned = string->len;
448 g_return_val_if_fail (pos_unsigned <= string->len, string);
451 /* Check whether val represents a substring of string.
452 * This test probably violates chapter and verse of the C standards,
453 * since ">=" and "<=" are only valid when val really is a substring.
454 * In practice, it will work on modern archs.
456 if (G_UNLIKELY (val >= string->str && val <= string->str + string->len))
458 gsize offset = val - string->str;
461 g_string_maybe_expand (string, len_unsigned);
462 val = string->str + offset;
463 /* At this point, val is valid again. */
465 /* Open up space where we are going to insert. */
466 if (pos_unsigned < string->len)
467 memmove (string->str + pos_unsigned + len_unsigned,
468 string->str + pos_unsigned, string->len - pos_unsigned);
470 /* Move the source part before the gap, if any. */
471 if (offset < pos_unsigned)
473 precount = MIN (len_unsigned, pos_unsigned - offset);
474 memcpy (string->str + pos_unsigned, val, precount);
477 /* Move the source part after the gap, if any. */
478 if (len_unsigned > precount)
479 memcpy (string->str + pos_unsigned + precount,
480 val + /* Already moved: */ precount +
481 /* Space opened up: */ len_unsigned,
482 len_unsigned - precount);
486 g_string_maybe_expand (string, len_unsigned);
488 /* If we aren't appending at the end, move a hunk
489 * of the old string to the end, opening up space
491 if (pos_unsigned < string->len)
492 memmove (string->str + pos_unsigned + len_unsigned,
493 string->str + pos_unsigned, string->len - pos_unsigned);
495 /* insert the new string */
496 if (len_unsigned == 1)
497 string->str[pos_unsigned] = *val;
499 memcpy (string->str + pos_unsigned, val, len_unsigned);
502 string->len += len_unsigned;
504 string->str[string->len] = 0;
510 * g_string_append_uri_escaped:
511 * @string: a #GString
512 * @unescaped: a string
513 * @reserved_chars_allowed: a string of reserved characters allowed
514 * to be used, or %NULL
515 * @allow_utf8: set %TRUE if the escaped string may include UTF8 characters
517 * Appends @unescaped to @string, escaping any characters that
518 * are reserved in URIs using URI-style escape sequences.
520 * Returns: (transfer none): @string
525 g_string_append_uri_escaped (GString *string,
526 const gchar *unescaped,
527 const gchar *reserved_chars_allowed,
530 _uri_encoder (string, (const guchar *) unescaped, strlen (unescaped),
531 reserved_chars_allowed, allow_utf8);
537 * @string: a #GString
538 * @val: the string to append onto the end of @string
540 * Adds a string onto the end of a #GString, expanding
543 * Returns: (transfer none): @string
546 g_string_append (GString *string,
549 return g_string_insert_len (string, -1, val, -1);
553 * g_string_append_len:
554 * @string: a #GString
555 * @val: bytes to append
556 * @len: number of bytes of @val to use, or -1 for all of @val
558 * Appends @len bytes of @val to @string.
560 * If @len is positive, @val may contain embedded nuls and need
561 * not be nul-terminated. It is the caller's responsibility to
562 * ensure that @val has at least @len addressable bytes.
564 * If @len is negative, @val must be nul-terminated and @len
565 * is considered to request the entire string length. This
566 * makes g_string_append_len() equivalent to g_string_append().
568 * Returns: (transfer none): @string
571 g_string_append_len (GString *string,
575 return g_string_insert_len (string, -1, val, len);
580 * @string: a #GString
581 * @c: the byte to append onto the end of @string
583 * Adds a byte onto the end of a #GString, expanding
586 * Returns: (transfer none): @string
588 #undef g_string_append_c
590 g_string_append_c (GString *string,
593 g_return_val_if_fail (string != NULL, NULL);
595 return g_string_insert_c (string, -1, c);
599 * g_string_append_unichar:
600 * @string: a #GString
601 * @wc: a Unicode character
603 * Converts a Unicode character into UTF-8, and appends it
606 * Returns: (transfer none): @string
609 g_string_append_unichar (GString *string,
612 g_return_val_if_fail (string != NULL, NULL);
614 return g_string_insert_unichar (string, -1, wc);
619 * @string: a #GString
620 * @val: the string to prepend on the start of @string
622 * Adds a string on to the start of a #GString,
623 * expanding it if necessary.
625 * Returns: (transfer none): @string
628 g_string_prepend (GString *string,
631 return g_string_insert_len (string, 0, val, -1);
635 * g_string_prepend_len:
636 * @string: a #GString
637 * @val: bytes to prepend
638 * @len: number of bytes in @val to prepend, or -1 for all of @val
640 * Prepends @len bytes of @val to @string.
642 * If @len is positive, @val may contain embedded nuls and need
643 * not be nul-terminated. It is the caller's responsibility to
644 * ensure that @val has at least @len addressable bytes.
646 * If @len is negative, @val must be nul-terminated and @len
647 * is considered to request the entire string length. This
648 * makes g_string_prepend_len() equivalent to g_string_prepend().
650 * Returns: (transfer none): @string
653 g_string_prepend_len (GString *string,
657 return g_string_insert_len (string, 0, val, len);
661 * g_string_prepend_c:
662 * @string: a #GString
663 * @c: the byte to prepend on the start of the #GString
665 * Adds a byte onto the start of a #GString,
666 * expanding it if necessary.
668 * Returns: (transfer none): @string
671 g_string_prepend_c (GString *string,
674 g_return_val_if_fail (string != NULL, NULL);
676 return g_string_insert_c (string, 0, c);
680 * g_string_prepend_unichar:
681 * @string: a #GString
682 * @wc: a Unicode character
684 * Converts a Unicode character into UTF-8, and prepends it
687 * Returns: (transfer none): @string
690 g_string_prepend_unichar (GString *string,
693 g_return_val_if_fail (string != NULL, NULL);
695 return g_string_insert_unichar (string, 0, wc);
700 * @string: a #GString
701 * @pos: the position to insert the copy of the string
702 * @val: the string to insert
704 * Inserts a copy of a string into a #GString,
705 * expanding it if necessary.
707 * Returns: (transfer none): @string
710 g_string_insert (GString *string,
714 return g_string_insert_len (string, pos, val, -1);
719 * @string: a #GString
720 * @pos: the position to insert the byte
721 * @c: the byte to insert
723 * Inserts a byte into a #GString, expanding it if necessary.
725 * Returns: (transfer none): @string
728 g_string_insert_c (GString *string,
734 g_return_val_if_fail (string != NULL, NULL);
736 g_string_maybe_expand (string, 1);
741 g_return_val_if_fail ((gsize) pos <= string->len, string);
744 /* If not just an append, move the old stuff */
745 if (pos_unsigned < string->len)
746 memmove (string->str + pos_unsigned + 1,
747 string->str + pos_unsigned, string->len - pos_unsigned);
749 string->str[pos_unsigned] = c;
753 string->str[string->len] = 0;
759 * g_string_insert_unichar:
760 * @string: a #GString
761 * @pos: the position at which to insert character, or -1
762 * to append at the end of the string
763 * @wc: a Unicode character
765 * Converts a Unicode character into UTF-8, and insert it
766 * into the string at the given position.
768 * Returns: (transfer none): @string
771 g_string_insert_unichar (GString *string,
775 gint charlen, first, i;
778 g_return_val_if_fail (string != NULL, NULL);
780 /* Code copied from g_unichar_to_utf() */
791 else if (wc < 0x10000)
796 else if (wc < 0x200000)
801 else if (wc < 0x4000000)
811 /* End of copied code */
813 g_string_maybe_expand (string, charlen);
818 g_return_val_if_fail ((gsize) pos <= string->len, string);
820 /* If not just an append, move the old stuff */
821 if ((gsize) pos < string->len)
822 memmove (string->str + pos + charlen, string->str + pos, string->len - pos);
824 dest = string->str + pos;
825 /* Code copied from g_unichar_to_utf() */
826 for (i = charlen - 1; i > 0; --i)
828 dest[i] = (wc & 0x3f) | 0x80;
831 dest[0] = wc | first;
832 /* End of copied code */
834 string->len += charlen;
836 string->str[string->len] = 0;
842 * g_string_overwrite:
843 * @string: a #GString
844 * @pos: the position at which to start overwriting
845 * @val: the string that will overwrite the @string starting at @pos
847 * Overwrites part of a string, lengthening it if necessary.
849 * Returns: (transfer none): @string
854 g_string_overwrite (GString *string,
858 g_return_val_if_fail (val != NULL, string);
859 return g_string_overwrite_len (string, pos, val, strlen (val));
863 * g_string_overwrite_len:
864 * @string: a #GString
865 * @pos: the position at which to start overwriting
866 * @val: the string that will overwrite the @string starting at @pos
867 * @len: the number of bytes to write from @val
869 * Overwrites part of a string, lengthening it if necessary.
870 * This function will work with embedded nuls.
872 * Returns: (transfer none): @string
877 g_string_overwrite_len (GString *string,
884 g_return_val_if_fail (string != NULL, NULL);
889 g_return_val_if_fail (val != NULL, string);
890 g_return_val_if_fail (pos <= string->len, string);
897 if (end > string->len)
898 g_string_maybe_expand (string, end - string->len);
900 memcpy (string->str + pos, val, len);
902 if (end > string->len)
904 string->str[end] = '\0';
913 * @string: a #GString
914 * @pos: the position of the content to remove
915 * @len: the number of bytes to remove, or -1 to remove all
918 * Removes @len bytes from a #GString, starting at position @pos.
919 * The rest of the #GString is shifted down to fill the gap.
921 * Returns: (transfer none): @string
924 g_string_erase (GString *string,
928 gsize len_unsigned, pos_unsigned;
930 g_return_val_if_fail (string != NULL, NULL);
931 g_return_val_if_fail (pos >= 0, string);
934 g_return_val_if_fail (pos_unsigned <= string->len, string);
937 len_unsigned = string->len - pos_unsigned;
941 g_return_val_if_fail (pos_unsigned + len_unsigned <= string->len, string);
943 if (pos_unsigned + len_unsigned < string->len)
944 memmove (string->str + pos_unsigned,
945 string->str + pos_unsigned + len_unsigned,
946 string->len - (pos_unsigned + len_unsigned));
949 string->len -= len_unsigned;
951 string->str[string->len] = 0;
958 * @string: a #GString
959 * @find: the string to find in @string
960 * @replace: the string to insert in place of @find
961 * @limit: the maximum instances of @find to replace with @replace, or `0` for
964 * Replaces the string @find with the string @replace in a #GString up to
965 * @limit times. If the number of instances of @find in the #GString is
966 * less than @limit, all instances are replaced. If the number of
967 * instances is `0`, all instances of @find are replaced.
969 * Returns: the number of find and replace operations performed.
974 g_string_replace (GString *string,
976 const gchar *replace,
979 gsize f_len, r_len, pos;
983 g_return_val_if_fail (string != NULL, 0);
984 g_return_val_if_fail (find != NULL, 0);
985 g_return_val_if_fail (replace != NULL, 0);
987 f_len = strlen (find);
988 r_len = strlen (replace);
991 while ((next = strstr (cur, find)) != NULL)
993 pos = next - string->str;
994 g_string_erase (string, pos, f_len);
995 g_string_insert (string, pos, replace);
996 cur = string->str + pos + r_len;
1006 * g_string_ascii_down:
1007 * @string: a GString
1009 * Converts all uppercase ASCII letters to lowercase ASCII letters.
1011 * Returns: (transfer none): passed-in @string pointer, with all the
1012 * uppercase characters converted to lowercase in place,
1013 * with semantics that exactly match g_ascii_tolower().
1016 g_string_ascii_down (GString *string)
1021 g_return_val_if_fail (string != NULL, NULL);
1028 *s = g_ascii_tolower (*s);
1037 * g_string_ascii_up:
1038 * @string: a GString
1040 * Converts all lowercase ASCII letters to uppercase ASCII letters.
1042 * Returns: (transfer none): passed-in @string pointer, with all the
1043 * lowercase characters converted to uppercase in place,
1044 * with semantics that exactly match g_ascii_toupper().
1047 g_string_ascii_up (GString *string)
1052 g_return_val_if_fail (string != NULL, NULL);
1059 *s = g_ascii_toupper (*s);
1069 * @string: a #GString
1071 * Converts a #GString to lowercase.
1073 * Returns: (transfer none): the #GString
1075 * Deprecated:2.2: This function uses the locale-specific
1076 * tolower() function, which is almost never the right thing.
1077 * Use g_string_ascii_down() or g_utf8_strdown() instead.
1080 g_string_down (GString *string)
1085 g_return_val_if_fail (string != NULL, NULL);
1088 s = (guchar *) string->str;
1103 * @string: a #GString
1105 * Converts a #GString to uppercase.
1107 * Returns: (transfer none): @string
1109 * Deprecated:2.2: This function uses the locale-specific
1110 * toupper() function, which is almost never the right thing.
1111 * Use g_string_ascii_up() or g_utf8_strup() instead.
1114 g_string_up (GString *string)
1119 g_return_val_if_fail (string != NULL, NULL);
1122 s = (guchar *) string->str;
1136 * g_string_append_vprintf:
1137 * @string: a #GString
1138 * @format: (not nullable): the string format. See the printf() documentation
1139 * @args: the list of arguments to insert in the output
1141 * Appends a formatted string onto the end of a #GString.
1142 * This function is similar to g_string_append_printf()
1143 * except that the arguments to the format string are passed
1149 g_string_append_vprintf (GString *string,
1150 const gchar *format,
1156 g_return_if_fail (string != NULL);
1157 g_return_if_fail (format != NULL);
1159 len = g_vasprintf (&buf, format, args);
1163 g_string_maybe_expand (string, len);
1164 memcpy (string->str + string->len, buf, len + 1);
1172 * @string: a #GString
1173 * @format: (not nullable): the string format. See the printf() documentation
1174 * @args: the parameters to insert into the format string
1176 * Writes a formatted string into a #GString.
1177 * This function is similar to g_string_printf() except that
1178 * the arguments to the format string are passed as a va_list.
1183 g_string_vprintf (GString *string,
1184 const gchar *format,
1187 g_string_truncate (string, 0);
1188 g_string_append_vprintf (string, format, args);
1193 * @string: a #GString
1194 * @format: the string format. See the sprintf() documentation
1195 * @...: the parameters to insert into the format string
1197 * Writes a formatted string into a #GString.
1198 * This is similar to the standard sprintf() function,
1199 * except that the #GString buffer automatically expands
1200 * to contain the results. The previous contents of the
1201 * #GString are destroyed.
1203 * Deprecated: This function has been renamed to g_string_printf().
1208 * @string: a #GString
1209 * @format: the string format. See the printf() documentation
1210 * @...: the parameters to insert into the format string
1212 * Writes a formatted string into a #GString.
1213 * This is similar to the standard sprintf() function,
1214 * except that the #GString buffer automatically expands
1215 * to contain the results. The previous contents of the
1216 * #GString are destroyed.
1219 g_string_printf (GString *string,
1220 const gchar *format,
1225 g_string_truncate (string, 0);
1227 va_start (args, format);
1228 g_string_append_vprintf (string, format, args);
1233 * g_string_sprintfa:
1234 * @string: a #GString
1235 * @format: the string format. See the sprintf() documentation
1236 * @...: the parameters to insert into the format string
1238 * Appends a formatted string onto the end of a #GString.
1239 * This function is similar to g_string_sprintf() except that
1240 * the text is appended to the #GString.
1242 * Deprecated: This function has been renamed to g_string_append_printf()
1246 * g_string_append_printf:
1247 * @string: a #GString
1248 * @format: the string format. See the printf() documentation
1249 * @...: the parameters to insert into the format string
1251 * Appends a formatted string onto the end of a #GString.
1252 * This function is similar to g_string_printf() except
1253 * that the text is appended to the #GString.
1256 g_string_append_printf (GString *string,
1257 const gchar *format,
1262 va_start (args, format);
1263 g_string_append_vprintf (string, format, args);