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 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/.
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: the initial text to copy into the string
137 * Creates a new #GString, initialized with the given string.
139 * Returns: the new #GString
142 g_string_new (const gchar *init)
146 if (init == NULL || *init == '\0')
147 string = g_string_sized_new (2);
153 string = g_string_sized_new (len + 2);
155 g_string_append_len (string, init, len);
163 * @init: initial contents of the string
164 * @len: length of @init to use
166 * Creates a new #GString with @len bytes of the @init buffer.
167 * Because a length is provided, @init need not be nul-terminated,
168 * and can contain embedded nul bytes.
170 * Since this function does not stop at nul bytes, it is the caller's
171 * responsibility to ensure that @init has at least @len addressable
174 * Returns: a new #GString
177 g_string_new_len (const gchar *init,
183 return g_string_new (init);
186 string = g_string_sized_new (len);
189 g_string_append_len (string, init, len);
197 * @string: a #GString
198 * @free_segment: if %TRUE, the actual character data is freed as well
200 * Frees the memory allocated for the #GString.
201 * If @free_segment is %TRUE it also frees the character data. If
202 * it's %FALSE, the caller gains ownership of the buffer and must
203 * free it after use with g_free().
205 * Returns: the character data of @string
206 * (i.e. %NULL if @free_segment is %TRUE)
209 g_string_free (GString *string,
210 gboolean free_segment)
214 g_return_val_if_fail (string != NULL, NULL);
218 g_free (string->str);
222 segment = string->str;
224 g_slice_free (GString, string);
230 * g_string_free_to_bytes:
231 * @string: (transfer full): a #GString
233 * Transfers ownership of the contents of @string to a newly allocated
234 * #GBytes. The #GString structure itself is deallocated, and it is
235 * therefore invalid to use @string after invoking this function.
237 * Note that while #GString ensures that its buffer always has a
238 * trailing nul character (not reflected in its "len"), the returned
239 * #GBytes does not include this extra nul; i.e. it has length exactly
240 * equal to the "len" member.
242 * Returns: A newly allocated #GBytes containing contents of @string; @string itself is freed
246 g_string_free_to_bytes (GString *string)
251 g_return_val_if_fail (string != NULL, NULL);
255 buf = g_string_free (string, FALSE);
257 return g_bytes_new_take (buf, len);
263 * @v2: another #GString
265 * Compares two strings for equality, returning %TRUE if they are equal.
266 * For use with #GHashTable.
268 * Returns: %TRUE if the strings are the same length and contain the
272 g_string_equal (const GString *v,
276 GString *string1 = (GString *) v;
277 GString *string2 = (GString *) v2;
278 gsize i = string1->len;
280 if (i != string2->len)
298 * @str: a string to hash
300 * Creates a hash code for @str; for use with #GHashTable.
302 * Returns: hash code for @str
305 g_string_hash (const GString *str)
307 const gchar *p = str->str;
311 /* 31 bit hash function */
314 h = (h << 5) - h + *p;
323 * @string: the destination #GString. Its current contents
325 * @rval: the string to copy into @string
327 * Copies the bytes from a string into a #GString,
328 * destroying any previous contents. It is rather like
329 * the standard strcpy() function, except that you do not
330 * have to worry about having enough space to copy the string.
335 g_string_assign (GString *string,
338 g_return_val_if_fail (string != NULL, NULL);
339 g_return_val_if_fail (rval != NULL, string);
341 /* Make sure assigning to itself doesn't corrupt the string. */
342 if (string->str != rval)
344 /* Assigning from substring should be ok, since
345 * g_string_truncate() does not reallocate.
347 g_string_truncate (string, 0);
348 g_string_append (string, rval);
356 * @string: a #GString
357 * @len: the new size of @string
359 * Cuts off the end of the GString, leaving the first @len bytes.
364 g_string_truncate (GString *string,
367 g_return_val_if_fail (string != NULL, NULL);
369 string->len = MIN (len, string->len);
370 string->str[string->len] = 0;
377 * @string: a #GString
378 * @len: the new length
380 * Sets the length of a #GString. If the length is less than
381 * the current length, the string will be truncated. If the
382 * length is greater than the current length, the contents
383 * of the newly added area are undefined. (However, as
384 * always, string->str[string->len] will be a nul byte.)
389 g_string_set_size (GString *string,
392 g_return_val_if_fail (string != NULL, NULL);
394 if (len >= string->allocated_len)
395 g_string_maybe_expand (string, len - string->len);
398 string->str[len] = 0;
404 * g_string_insert_len:
405 * @string: a #GString
406 * @pos: position in @string where insertion should
407 * happen, or -1 for at the end
408 * @val: bytes to insert
409 * @len: number of bytes of @val to insert
411 * Inserts @len bytes of @val into @string at @pos.
412 * Because @len is provided, @val may contain embedded
413 * nuls and need not be nul-terminated. If @pos is -1,
414 * bytes are inserted at the end of the string.
416 * Since this function does not stop at nul bytes, it is
417 * the caller's responsibility to ensure that @val has at
418 * least @len addressable bytes.
423 g_string_insert_len (GString *string,
428 g_return_val_if_fail (string != NULL, NULL);
429 g_return_val_if_fail (len == 0 || val != NULL, string);
440 g_return_val_if_fail (pos <= string->len, string);
442 /* Check whether val represents a substring of string.
443 * This test probably violates chapter and verse of the C standards,
444 * since ">=" and "<=" are only valid when val really is a substring.
445 * In practice, it will work on modern archs.
447 if (val >= string->str && val <= string->str + string->len)
449 gsize offset = val - string->str;
452 g_string_maybe_expand (string, len);
453 val = string->str + offset;
454 /* At this point, val is valid again. */
456 /* Open up space where we are going to insert. */
457 if (pos < string->len)
458 memmove (string->str + pos + len, string->str + pos, string->len - pos);
460 /* Move the source part before the gap, if any. */
463 precount = MIN (len, pos - offset);
464 memcpy (string->str + pos, val, precount);
467 /* Move the source part after the gap, if any. */
469 memcpy (string->str + pos + precount,
470 val + /* Already moved: */ precount + /* Space opened up: */ len,
475 g_string_maybe_expand (string, len);
477 /* If we aren't appending at the end, move a hunk
478 * of the old string to the end, opening up space
480 if (pos < string->len)
481 memmove (string->str + pos + len, string->str + pos, string->len - pos);
483 /* insert the new string */
485 string->str[pos] = *val;
487 memcpy (string->str + pos, val, len);
492 string->str[string->len] = 0;
497 #define SUB_DELIM_CHARS "!$&'()*+,;="
501 const char *reserved_chars_allowed)
503 if (g_ascii_isalnum (c) ||
510 if (reserved_chars_allowed &&
511 strchr (reserved_chars_allowed, c) != NULL)
518 gunichar_ok (gunichar c)
521 (c != (gunichar) -2) &&
522 (c != (gunichar) -1);
526 * g_string_append_uri_escaped:
527 * @string: a #GString
528 * @unescaped: a string
529 * @reserved_chars_allowed: a string of reserved characters allowed
530 * to be used, or %NULL
531 * @allow_utf8: set %TRUE if the escaped string may include UTF8 characters
533 * Appends @unescaped to @string, escaped any characters that
534 * are reserved in URIs using URI-style escape sequences.
541 g_string_append_uri_escaped (GString *string,
542 const gchar *unescaped,
543 const gchar *reserved_chars_allowed,
548 static const gchar hex[16] = "0123456789ABCDEF";
550 g_return_val_if_fail (string != NULL, NULL);
551 g_return_val_if_fail (unescaped != NULL, NULL);
553 end = unescaped + strlen (unescaped);
555 while ((c = *unescaped) != 0)
557 if (c >= 0x80 && allow_utf8 &&
558 gunichar_ok (g_utf8_get_char_validated (unescaped, end - unescaped)))
560 int len = g_utf8_skip [c];
561 g_string_append_len (string, unescaped, len);
564 else if (is_valid (c, reserved_chars_allowed))
566 g_string_append_c (string, c);
571 g_string_append_c (string, '%');
572 g_string_append_c (string, hex[((guchar)c) >> 4]);
573 g_string_append_c (string, hex[((guchar)c) & 0xf]);
583 * @string: a #GString
584 * @val: the string to append onto the end of @string
586 * Adds a string onto the end of a #GString, expanding
592 g_string_append (GString *string,
595 g_return_val_if_fail (string != NULL, NULL);
596 g_return_val_if_fail (val != NULL, string);
598 return g_string_insert_len (string, -1, val, -1);
602 * g_string_append_len:
603 * @string: a #GString
604 * @val: bytes to append
605 * @len: number of bytes of @val to use
607 * Appends @len bytes of @val to @string. Because @len is
608 * provided, @val may contain embedded nuls and need not
611 * Since this function does not stop at nul bytes, it is
612 * the caller's responsibility to ensure that @val has at
613 * least @len addressable bytes.
618 g_string_append_len (GString *string,
622 g_return_val_if_fail (string != NULL, NULL);
623 g_return_val_if_fail (len == 0 || val != NULL, string);
625 return g_string_insert_len (string, -1, val, len);
630 * @string: a #GString
631 * @c: the byte to append onto the end of @string
633 * Adds a byte onto the end of a #GString, expanding
638 #undef g_string_append_c
640 g_string_append_c (GString *string,
643 g_return_val_if_fail (string != NULL, NULL);
645 return g_string_insert_c (string, -1, c);
649 * g_string_append_unichar:
650 * @string: a #GString
651 * @wc: a Unicode character
653 * Converts a Unicode character into UTF-8, and appends it
659 g_string_append_unichar (GString *string,
662 g_return_val_if_fail (string != NULL, NULL);
664 return g_string_insert_unichar (string, -1, wc);
669 * @string: a #GString
670 * @val: the string to prepend on the start of @string
672 * Adds a string on to the start of a #GString,
673 * expanding it if necessary.
678 g_string_prepend (GString *string,
681 g_return_val_if_fail (string != NULL, NULL);
682 g_return_val_if_fail (val != NULL, string);
684 return g_string_insert_len (string, 0, val, -1);
688 * g_string_prepend_len:
689 * @string: a #GString
690 * @val: bytes to prepend
691 * @len: number of bytes in @val to prepend
693 * Prepends @len bytes of @val to @string.
694 * Because @len is provided, @val may contain
695 * embedded nuls and need not be nul-terminated.
697 * Since this function does not stop at nul bytes,
698 * it is the caller's responsibility to ensure that
699 * @val has at least @len addressable bytes.
704 g_string_prepend_len (GString *string,
708 g_return_val_if_fail (string != NULL, NULL);
709 g_return_val_if_fail (val != NULL, string);
711 return g_string_insert_len (string, 0, val, len);
715 * g_string_prepend_c:
716 * @string: a #GString
717 * @c: the byte to prepend on the start of the #GString
719 * Adds a byte onto the start of a #GString,
720 * expanding it if necessary.
725 g_string_prepend_c (GString *string,
728 g_return_val_if_fail (string != NULL, NULL);
730 return g_string_insert_c (string, 0, c);
734 * g_string_prepend_unichar:
735 * @string: a #GString
736 * @wc: a Unicode character
738 * Converts a Unicode character into UTF-8, and prepends it
744 g_string_prepend_unichar (GString *string,
747 g_return_val_if_fail (string != NULL, NULL);
749 return g_string_insert_unichar (string, 0, wc);
754 * @string: a #GString
755 * @pos: the position to insert the copy of the string
756 * @val: the string to insert
758 * Inserts a copy of a string into a #GString,
759 * expanding it if necessary.
764 g_string_insert (GString *string,
768 g_return_val_if_fail (string != NULL, NULL);
769 g_return_val_if_fail (val != NULL, string);
772 g_return_val_if_fail (pos <= string->len, string);
774 return g_string_insert_len (string, pos, val, -1);
779 * @string: a #GString
780 * @pos: the position to insert the byte
781 * @c: the byte to insert
783 * Inserts a byte into a #GString, expanding it if necessary.
788 g_string_insert_c (GString *string,
792 g_return_val_if_fail (string != NULL, NULL);
794 g_string_maybe_expand (string, 1);
799 g_return_val_if_fail (pos <= string->len, string);
801 /* If not just an append, move the old stuff */
802 if (pos < string->len)
803 memmove (string->str + pos + 1, string->str + pos, string->len - pos);
805 string->str[pos] = c;
809 string->str[string->len] = 0;
815 * g_string_insert_unichar:
816 * @string: a #GString
817 * @pos: the position at which to insert character, or -1
818 * to append at the end of the string
819 * @wc: a Unicode character
821 * Converts a Unicode character into UTF-8, and insert it
822 * into the string at the given position.
827 g_string_insert_unichar (GString *string,
831 gint charlen, first, i;
834 g_return_val_if_fail (string != NULL, NULL);
836 /* Code copied from g_unichar_to_utf() */
847 else if (wc < 0x10000)
852 else if (wc < 0x200000)
857 else if (wc < 0x4000000)
867 /* End of copied code */
869 g_string_maybe_expand (string, charlen);
874 g_return_val_if_fail (pos <= string->len, string);
876 /* If not just an append, move the old stuff */
877 if (pos < string->len)
878 memmove (string->str + pos + charlen, string->str + pos, string->len - pos);
880 dest = string->str + pos;
881 /* Code copied from g_unichar_to_utf() */
882 for (i = charlen - 1; i > 0; --i)
884 dest[i] = (wc & 0x3f) | 0x80;
887 dest[0] = wc | first;
888 /* End of copied code */
890 string->len += charlen;
892 string->str[string->len] = 0;
898 * g_string_overwrite:
899 * @string: a #GString
900 * @pos: the position at which to start overwriting
901 * @val: the string that will overwrite the @string starting at @pos
903 * Overwrites part of a string, lengthening it if necessary.
910 g_string_overwrite (GString *string,
914 g_return_val_if_fail (val != NULL, string);
915 return g_string_overwrite_len (string, pos, val, strlen (val));
919 * g_string_overwrite_len:
920 * @string: a #GString
921 * @pos: the position at which to start overwriting
922 * @val: the string that will overwrite the @string starting at @pos
923 * @len: the number of bytes to write from @val
925 * Overwrites part of a string, lengthening it if necessary.
926 * This function will work with embedded nuls.
933 g_string_overwrite_len (GString *string,
940 g_return_val_if_fail (string != NULL, NULL);
945 g_return_val_if_fail (val != NULL, string);
946 g_return_val_if_fail (pos <= string->len, string);
953 if (end > string->len)
954 g_string_maybe_expand (string, end - string->len);
956 memcpy (string->str + pos, val, len);
958 if (end > string->len)
960 string->str[end] = '\0';
969 * @string: a #GString
970 * @pos: the position of the content to remove
971 * @len: the number of bytes to remove, or -1 to remove all
974 * Removes @len bytes from a #GString, starting at position @pos.
975 * The rest of the #GString is shifted down to fill the gap.
980 g_string_erase (GString *string,
984 g_return_val_if_fail (string != NULL, NULL);
985 g_return_val_if_fail (pos >= 0, string);
986 g_return_val_if_fail (pos <= string->len, string);
989 len = string->len - pos;
992 g_return_val_if_fail (pos + len <= string->len, string);
994 if (pos + len < string->len)
995 memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
1000 string->str[string->len] = 0;
1006 * g_string_ascii_down:
1007 * @string: a GString
1009 * Converts all uppercase ASCII letters to lowercase ASCII letters.
1011 * Returns: 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: 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: 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.
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: 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: 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);