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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
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/.
47 * @short_description: text buffers which grow automatically
50 * A #GString is an object that handles the memory management of a C
51 * string for you. The emphasis of #GString is on text, typically
52 * UTF-8. Crucially, the "str" member of a #GString is guaranteed to
53 * have a trailing nul character, and it is therefore always safe to
54 * call functions such as strchr() or g_strdup() on it.
56 * However, a #GString can also hold arbitrary binary data, because it
57 * has a "len" member, which includes any possible embedded nul
58 * characters in the data. Conceptually then, #GString is like a
59 * #GByteArray with the addition of many convenience methods for text,
60 * and a guaranteed nul terminator.
65 * @str: points to the character data. It may move as text is added.
66 * The @str field is null-terminated and so
67 * can be used as an ordinary C string.
68 * @len: contains the length of the string, not including the
69 * terminating nul byte.
70 * @allocated_len: the number of bytes that can be stored in the
71 * string before it needs to be reallocated. May be larger than @len.
73 * The GString struct contains the public fields of a GString.
77 #define MY_MAXSIZE ((gsize)-1)
80 nearest_power (gsize base
, gsize num
)
82 if (num
> MY_MAXSIZE
/ 2)
98 g_string_maybe_expand (GString
*string
,
101 if (string
->len
+ len
>= string
->allocated_len
)
103 string
->allocated_len
= nearest_power (1, string
->len
+ len
+ 1);
104 string
->str
= g_realloc (string
->str
, string
->allocated_len
);
109 * g_string_sized_new:
110 * @dfl_size: the default size of the space allocated to
113 * Creates a new #GString, with enough space for @dfl_size
114 * bytes. This is useful if you are going to add a lot of
115 * text to the string and don't want it to be reallocated
118 * Returns: the new #GString
121 g_string_sized_new (gsize dfl_size
)
123 GString
*string
= g_slice_new (GString
);
125 string
->allocated_len
= 0;
129 g_string_maybe_expand (string
, MAX (dfl_size
, 2));
137 * @init: the initial text to copy into the string
139 * Creates a new #GString, initialized with the given string.
141 * Returns: the new #GString
144 g_string_new (const gchar
*init
)
148 if (init
== NULL
|| *init
== '\0')
149 string
= g_string_sized_new (2);
155 string
= g_string_sized_new (len
+ 2);
157 g_string_append_len (string
, init
, len
);
165 * @init: initial contents of the string
166 * @len: length of @init to use
168 * Creates a new #GString with @len bytes of the @init buffer.
169 * Because a length is provided, @init need not be nul-terminated,
170 * and can contain embedded nul bytes.
172 * Since this function does not stop at nul bytes, it is the caller's
173 * responsibility to ensure that @init has at least @len addressable
176 * Returns: a new #GString
179 g_string_new_len (const gchar
*init
,
185 return g_string_new (init
);
188 string
= g_string_sized_new (len
);
191 g_string_append_len (string
, init
, len
);
199 * @string: a #GString
200 * @free_segment: if %TRUE, the actual character data is freed as well
202 * Frees the memory allocated for the #GString.
203 * If @free_segment is %TRUE it also frees the character data. If
204 * it's %FALSE, the caller gains ownership of the buffer and must
205 * free it after use with g_free().
207 * Returns: the character data of @string
208 * (i.e. %NULL if @free_segment is %TRUE)
211 g_string_free (GString
*string
,
212 gboolean free_segment
)
216 g_return_val_if_fail (string
!= NULL
, NULL
);
220 g_free (string
->str
);
224 segment
= string
->str
;
226 g_slice_free (GString
, string
);
232 * g_string_free_to_bytes:
233 * @string: (transfer full): a #GString
235 * Transfers ownership of the contents of @string to a newly allocated
236 * #GBytes. The #GString structure itself is deallocated, and it is
237 * therefore invalid to use @string after invoking this function.
239 * Note that while #GString ensures that its buffer always has a
240 * trailing nul character (not reflected in its "len"), the returned
241 * #GBytes does not include this extra nul; i.e. it has length exactly
242 * equal to the "len" member.
244 * Returns: A newly allocated #GBytes containing contents of @string; @string itself is freed
248 g_string_free_to_bytes (GString
*string
)
253 g_return_val_if_fail (string
!= NULL
, NULL
);
257 buf
= g_string_free (string
, FALSE
);
259 return g_bytes_new_take (buf
, len
);
265 * @v2: another #GString
267 * Compares two strings for equality, returning %TRUE if they are equal.
268 * For use with #GHashTable.
270 * Returns: %TRUE if the strings are the same length and contain the
274 g_string_equal (const GString
*v
,
278 GString
*string1
= (GString
*) v
;
279 GString
*string2
= (GString
*) v2
;
280 gsize i
= string1
->len
;
282 if (i
!= string2
->len
)
300 * @str: a string to hash
302 * Creates a hash code for @str; for use with #GHashTable.
304 * Returns: hash code for @str
307 g_string_hash (const GString
*str
)
309 const gchar
*p
= str
->str
;
313 /* 31 bit hash function */
316 h
= (h
<< 5) - h
+ *p
;
325 * @string: the destination #GString. Its current contents
327 * @rval: the string to copy into @string
329 * Copies the bytes from a string into a #GString,
330 * destroying any previous contents. It is rather like
331 * the standard strcpy() function, except that you do not
332 * have to worry about having enough space to copy the string.
337 g_string_assign (GString
*string
,
340 g_return_val_if_fail (string
!= NULL
, NULL
);
341 g_return_val_if_fail (rval
!= NULL
, string
);
343 /* Make sure assigning to itself doesn't corrupt the string. */
344 if (string
->str
!= rval
)
346 /* Assigning from substring should be ok, since
347 * g_string_truncate() does not reallocate.
349 g_string_truncate (string
, 0);
350 g_string_append (string
, rval
);
358 * @string: a #GString
359 * @len: the new size of @string
361 * Cuts off the end of the GString, leaving the first @len bytes.
366 g_string_truncate (GString
*string
,
369 g_return_val_if_fail (string
!= NULL
, NULL
);
371 string
->len
= MIN (len
, string
->len
);
372 string
->str
[string
->len
] = 0;
379 * @string: a #GString
380 * @len: the new length
382 * Sets the length of a #GString. If the length is less than
383 * the current length, the string will be truncated. If the
384 * length is greater than the current length, the contents
385 * of the newly added area are undefined. (However, as
386 * always, string->str[string->len] will be a nul byte.)
388 * Return value: @string
391 g_string_set_size (GString
*string
,
394 g_return_val_if_fail (string
!= NULL
, NULL
);
396 if (len
>= string
->allocated_len
)
397 g_string_maybe_expand (string
, len
- string
->len
);
400 string
->str
[len
] = 0;
406 * g_string_insert_len:
407 * @string: a #GString
408 * @pos: position in @string where insertion should
409 * happen, or -1 for at the end
410 * @val: bytes to insert
411 * @len: number of bytes of @val to insert
413 * Inserts @len bytes of @val into @string at @pos.
414 * Because @len is provided, @val may contain embedded
415 * nuls and need not be nul-terminated. If @pos is -1,
416 * bytes are inserted at the end of the string.
418 * Since this function does not stop at nul bytes, it is
419 * the caller's responsibility to ensure that @val has at
420 * least @len addressable bytes.
425 g_string_insert_len (GString
*string
,
430 g_return_val_if_fail (string
!= NULL
, NULL
);
431 g_return_val_if_fail (len
== 0 || val
!= NULL
, string
);
442 g_return_val_if_fail (pos
<= string
->len
, string
);
444 /* Check whether val represents a substring of string.
445 * This test probably violates chapter and verse of the C standards,
446 * since ">=" and "<=" are only valid when val really is a substring.
447 * In practice, it will work on modern archs.
449 if (val
>= string
->str
&& val
<= string
->str
+ string
->len
)
451 gsize offset
= val
- string
->str
;
454 g_string_maybe_expand (string
, len
);
455 val
= string
->str
+ offset
;
456 /* At this point, val is valid again. */
458 /* Open up space where we are going to insert. */
459 if (pos
< string
->len
)
460 memmove (string
->str
+ pos
+ len
, string
->str
+ pos
, string
->len
- pos
);
462 /* Move the source part before the gap, if any. */
465 precount
= MIN (len
, pos
- offset
);
466 memcpy (string
->str
+ pos
, val
, precount
);
469 /* Move the source part after the gap, if any. */
471 memcpy (string
->str
+ pos
+ precount
,
472 val
+ /* Already moved: */ precount
+ /* Space opened up: */ len
,
477 g_string_maybe_expand (string
, len
);
479 /* If we aren't appending at the end, move a hunk
480 * of the old string to the end, opening up space
482 if (pos
< string
->len
)
483 memmove (string
->str
+ pos
+ len
, string
->str
+ pos
, string
->len
- pos
);
485 /* insert the new string */
487 string
->str
[pos
] = *val
;
489 memcpy (string
->str
+ pos
, val
, len
);
494 string
->str
[string
->len
] = 0;
499 #define SUB_DELIM_CHARS "!$&'()*+,;="
503 const char *reserved_chars_allowed
)
505 if (g_ascii_isalnum (c
) ||
512 if (reserved_chars_allowed
&&
513 strchr (reserved_chars_allowed
, c
) != NULL
)
520 gunichar_ok (gunichar c
)
523 (c
!= (gunichar
) -2) &&
524 (c
!= (gunichar
) -1);
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
535 * Appends @unescaped to @string, escaped any characters that
536 * are reserved in URIs using URI-style escape sequences.
543 g_string_append_uri_escaped (GString
*string
,
544 const gchar
*unescaped
,
545 const gchar
*reserved_chars_allowed
,
550 static const gchar hex
[16] = "0123456789ABCDEF";
552 g_return_val_if_fail (string
!= NULL
, NULL
);
553 g_return_val_if_fail (unescaped
!= NULL
, NULL
);
555 end
= unescaped
+ strlen (unescaped
);
557 while ((c
= *unescaped
) != 0)
559 if (c
>= 0x80 && allow_utf8
&&
560 gunichar_ok (g_utf8_get_char_validated (unescaped
, end
- unescaped
)))
562 int len
= g_utf8_skip
[c
];
563 g_string_append_len (string
, unescaped
, len
);
566 else if (is_valid (c
, reserved_chars_allowed
))
568 g_string_append_c (string
, c
);
573 g_string_append_c (string
, '%');
574 g_string_append_c (string
, hex
[((guchar
)c
) >> 4]);
575 g_string_append_c (string
, hex
[((guchar
)c
) & 0xf]);
585 * @string: a #GString
586 * @val: the string to append onto the end of @string
588 * Adds a string onto the end of a #GString, expanding
594 g_string_append (GString
*string
,
597 g_return_val_if_fail (string
!= NULL
, NULL
);
598 g_return_val_if_fail (val
!= NULL
, string
);
600 return g_string_insert_len (string
, -1, val
, -1);
604 * g_string_append_len:
605 * @string: a #GString
606 * @val: bytes to append
607 * @len: number of bytes of @val to use
609 * Appends @len bytes of @val to @string. Because @len is
610 * provided, @val may contain embedded nuls and need not
613 * Since this function does not stop at nul bytes, it is
614 * the caller's responsibility to ensure that @val has at
615 * least @len addressable bytes.
620 g_string_append_len (GString
*string
,
624 g_return_val_if_fail (string
!= NULL
, NULL
);
625 g_return_val_if_fail (len
== 0 || val
!= NULL
, string
);
627 return g_string_insert_len (string
, -1, val
, len
);
632 * @string: a #GString
633 * @c: the byte to append onto the end of @string
635 * Adds a byte onto the end of a #GString, expanding
640 #undef g_string_append_c
642 g_string_append_c (GString
*string
,
645 g_return_val_if_fail (string
!= NULL
, NULL
);
647 return g_string_insert_c (string
, -1, c
);
651 * g_string_append_unichar:
652 * @string: a #GString
653 * @wc: a Unicode character
655 * Converts a Unicode character into UTF-8, and appends it
658 * Return value: @string
661 g_string_append_unichar (GString
*string
,
664 g_return_val_if_fail (string
!= NULL
, NULL
);
666 return g_string_insert_unichar (string
, -1, wc
);
671 * @string: a #GString
672 * @val: the string to prepend on the start of @string
674 * Adds a string on to the start of a #GString,
675 * expanding it if necessary.
680 g_string_prepend (GString
*string
,
683 g_return_val_if_fail (string
!= NULL
, NULL
);
684 g_return_val_if_fail (val
!= NULL
, string
);
686 return g_string_insert_len (string
, 0, val
, -1);
690 * g_string_prepend_len:
691 * @string: a #GString
692 * @val: bytes to prepend
693 * @len: number of bytes in @val to prepend
695 * Prepends @len bytes of @val to @string.
696 * Because @len is provided, @val may contain
697 * embedded nuls and need not be nul-terminated.
699 * Since this function does not stop at nul bytes,
700 * it is the caller's responsibility to ensure that
701 * @val has at least @len addressable bytes.
706 g_string_prepend_len (GString
*string
,
710 g_return_val_if_fail (string
!= NULL
, NULL
);
711 g_return_val_if_fail (val
!= NULL
, string
);
713 return g_string_insert_len (string
, 0, val
, len
);
717 * g_string_prepend_c:
718 * @string: a #GString
719 * @c: the byte to prepend on the start of the #GString
721 * Adds a byte onto the start of a #GString,
722 * expanding it if necessary.
727 g_string_prepend_c (GString
*string
,
730 g_return_val_if_fail (string
!= NULL
, NULL
);
732 return g_string_insert_c (string
, 0, c
);
736 * g_string_prepend_unichar:
737 * @string: a #GString
738 * @wc: a Unicode character
740 * Converts a Unicode character into UTF-8, and prepends it
743 * Return value: @string
746 g_string_prepend_unichar (GString
*string
,
749 g_return_val_if_fail (string
!= NULL
, NULL
);
751 return g_string_insert_unichar (string
, 0, wc
);
756 * @string: a #GString
757 * @pos: the position to insert the copy of the string
758 * @val: the string to insert
760 * Inserts a copy of a string into a #GString,
761 * expanding it if necessary.
766 g_string_insert (GString
*string
,
770 g_return_val_if_fail (string
!= NULL
, NULL
);
771 g_return_val_if_fail (val
!= NULL
, string
);
774 g_return_val_if_fail (pos
<= string
->len
, string
);
776 return g_string_insert_len (string
, pos
, val
, -1);
781 * @string: a #GString
782 * @pos: the position to insert the byte
783 * @c: the byte to insert
785 * Inserts a byte into a #GString, expanding it if necessary.
790 g_string_insert_c (GString
*string
,
794 g_return_val_if_fail (string
!= NULL
, NULL
);
796 g_string_maybe_expand (string
, 1);
801 g_return_val_if_fail (pos
<= string
->len
, string
);
803 /* If not just an append, move the old stuff */
804 if (pos
< string
->len
)
805 memmove (string
->str
+ pos
+ 1, string
->str
+ pos
, string
->len
- pos
);
807 string
->str
[pos
] = c
;
811 string
->str
[string
->len
] = 0;
817 * g_string_insert_unichar:
818 * @string: a #GString
819 * @pos: the position at which to insert character, or -1
820 * to append at the end of the string
821 * @wc: a Unicode character
823 * Converts a Unicode character into UTF-8, and insert it
824 * into the string at the given position.
826 * Return value: @string
829 g_string_insert_unichar (GString
*string
,
833 gint charlen
, first
, i
;
836 g_return_val_if_fail (string
!= NULL
, NULL
);
838 /* Code copied from g_unichar_to_utf() */
849 else if (wc
< 0x10000)
854 else if (wc
< 0x200000)
859 else if (wc
< 0x4000000)
869 /* End of copied code */
871 g_string_maybe_expand (string
, charlen
);
876 g_return_val_if_fail (pos
<= string
->len
, string
);
878 /* If not just an append, move the old stuff */
879 if (pos
< string
->len
)
880 memmove (string
->str
+ pos
+ charlen
, string
->str
+ pos
, string
->len
- pos
);
882 dest
= string
->str
+ pos
;
883 /* Code copied from g_unichar_to_utf() */
884 for (i
= charlen
- 1; i
> 0; --i
)
886 dest
[i
] = (wc
& 0x3f) | 0x80;
889 dest
[0] = wc
| first
;
890 /* End of copied code */
892 string
->len
+= charlen
;
894 string
->str
[string
->len
] = 0;
900 * g_string_overwrite:
901 * @string: a #GString
902 * @pos: the position at which to start overwriting
903 * @val: the string that will overwrite the @string starting at @pos
905 * Overwrites part of a string, lengthening it if necessary.
907 * Return value: @string
912 g_string_overwrite (GString
*string
,
916 g_return_val_if_fail (val
!= NULL
, string
);
917 return g_string_overwrite_len (string
, pos
, val
, strlen (val
));
921 * g_string_overwrite_len:
922 * @string: a #GString
923 * @pos: the position at which to start overwriting
924 * @val: the string that will overwrite the @string starting at @pos
925 * @len: the number of bytes to write from @val
927 * Overwrites part of a string, lengthening it if necessary.
928 * This function will work with embedded nuls.
930 * Return value: @string
935 g_string_overwrite_len (GString
*string
,
942 g_return_val_if_fail (string
!= NULL
, NULL
);
947 g_return_val_if_fail (val
!= NULL
, string
);
948 g_return_val_if_fail (pos
<= string
->len
, string
);
955 if (end
> string
->len
)
956 g_string_maybe_expand (string
, end
- string
->len
);
958 memcpy (string
->str
+ pos
, val
, len
);
960 if (end
> string
->len
)
962 string
->str
[end
] = '\0';
971 * @string: a #GString
972 * @pos: the position of the content to remove
973 * @len: the number of bytes to remove, or -1 to remove all
976 * Removes @len bytes from a #GString, starting at position @pos.
977 * The rest of the #GString is shifted down to fill the gap.
982 g_string_erase (GString
*string
,
986 g_return_val_if_fail (string
!= NULL
, NULL
);
987 g_return_val_if_fail (pos
>= 0, string
);
988 g_return_val_if_fail (pos
<= string
->len
, string
);
991 len
= string
->len
- pos
;
994 g_return_val_if_fail (pos
+ len
<= string
->len
, string
);
996 if (pos
+ len
< string
->len
)
997 memmove (string
->str
+ pos
, string
->str
+ pos
+ len
, string
->len
- (pos
+ len
));
1002 string
->str
[string
->len
] = 0;
1008 * g_string_ascii_down:
1009 * @string: a GString
1011 * Converts all uppercase ASCII letters to lowercase ASCII letters.
1013 * Return value: passed-in @string pointer, with all the
1014 * uppercase characters converted to lowercase in place,
1015 * with semantics that exactly match g_ascii_tolower().
1018 g_string_ascii_down (GString
*string
)
1023 g_return_val_if_fail (string
!= NULL
, NULL
);
1030 *s
= g_ascii_tolower (*s
);
1039 * g_string_ascii_up:
1040 * @string: a GString
1042 * Converts all lowercase ASCII letters to uppercase ASCII letters.
1044 * Return value: passed-in @string pointer, with all the
1045 * lowercase characters converted to uppercase in place,
1046 * with semantics that exactly match g_ascii_toupper().
1049 g_string_ascii_up (GString
*string
)
1054 g_return_val_if_fail (string
!= NULL
, NULL
);
1061 *s
= g_ascii_toupper (*s
);
1071 * @string: a #GString
1073 * Converts a #GString to lowercase.
1075 * Returns: the #GString
1077 * Deprecated:2.2: This function uses the locale-specific
1078 * tolower() function, which is almost never the right thing.
1079 * Use g_string_ascii_down() or g_utf8_strdown() instead.
1082 g_string_down (GString
*string
)
1087 g_return_val_if_fail (string
!= NULL
, NULL
);
1090 s
= (guchar
*) string
->str
;
1105 * @string: a #GString
1107 * Converts a #GString to uppercase.
1109 * Return value: @string
1111 * Deprecated:2.2: This function uses the locale-specific
1112 * toupper() function, which is almost never the right thing.
1113 * Use g_string_ascii_up() or g_utf8_strup() instead.
1116 g_string_up (GString
*string
)
1121 g_return_val_if_fail (string
!= NULL
, NULL
);
1124 s
= (guchar
*) string
->str
;
1138 * g_string_append_vprintf:
1139 * @string: a #GString
1140 * @format: the string format. See the printf() documentation
1141 * @args: the list of arguments to insert in the output
1143 * Appends a formatted string onto the end of a #GString.
1144 * This function is similar to g_string_append_printf()
1145 * except that the arguments to the format string are passed
1151 g_string_append_vprintf (GString
*string
,
1152 const gchar
*format
,
1158 g_return_if_fail (string
!= NULL
);
1159 g_return_if_fail (format
!= NULL
);
1161 len
= g_vasprintf (&buf
, format
, args
);
1165 g_string_maybe_expand (string
, len
);
1166 memcpy (string
->str
+ string
->len
, buf
, len
+ 1);
1174 * @string: a #GString
1175 * @format: the string format. See the printf() documentation
1176 * @args: the parameters to insert into the format string
1178 * Writes a formatted string into a #GString.
1179 * This function is similar to g_string_printf() except that
1180 * the arguments to the format string are passed as a va_list.
1185 g_string_vprintf (GString
*string
,
1186 const gchar
*format
,
1189 g_string_truncate (string
, 0);
1190 g_string_append_vprintf (string
, format
, args
);
1195 * @string: a #GString
1196 * @format: the string format. See the sprintf() documentation
1197 * @...: the parameters to insert into the format string
1199 * Writes a formatted string into a #GString.
1200 * This is similar to the standard sprintf() function,
1201 * except that the #GString buffer automatically expands
1202 * to contain the results. The previous contents of the
1203 * #GString are destroyed.
1205 * Deprecated: This function has been renamed to g_string_printf().
1210 * @string: a #GString
1211 * @format: the string format. See the printf() documentation
1212 * @...: the parameters to insert into the format string
1214 * Writes a formatted string into a #GString.
1215 * This is similar to the standard sprintf() function,
1216 * except that the #GString buffer automatically expands
1217 * to contain the results. The previous contents of the
1218 * #GString are destroyed.
1221 g_string_printf (GString
*string
,
1222 const gchar
*format
,
1227 g_string_truncate (string
, 0);
1229 va_start (args
, format
);
1230 g_string_append_vprintf (string
, format
, args
);
1235 * g_string_sprintfa:
1236 * @string: a #GString
1237 * @format: the string format. See the sprintf() documentation
1238 * @...: the parameters to insert into the format string
1240 * Appends a formatted string onto the end of a #GString.
1241 * This function is similar to g_string_sprintf() except that
1242 * the text is appended to the #GString.
1244 * Deprecated: This function has been renamed to g_string_append_printf()
1248 * g_string_append_printf:
1249 * @string: a #GString
1250 * @format: the string format. See the printf() documentation
1251 * @...: the parameters to insert into the format string
1253 * Appends a formatted string onto the end of a #GString.
1254 * This function is similar to g_string_printf() except
1255 * that the text is appended to the #GString.
1258 g_string_append_printf (GString
*string
,
1259 const gchar
*format
,
1264 va_start (args
, format
);
1265 g_string_append_vprintf (string
, format
, args
);