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/.
50 * @short_description: text buffers which grow automatically
53 * A #GString is an object that handles the memory management
54 * of a C string for you. You can think of it as similar to a
55 * Java StringBuffer. In addition to the string itself, GString
56 * stores the length of the string, so can be used for binary
57 * data with embedded nul bytes. To access the C string managed
58 * by the GString @string, simply use @string->str.
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
);
232 * @v2: another #GString
234 * Compares two strings for equality, returning %TRUE if they are equal.
235 * For use with #GHashTable.
237 * Returns: %TRUE if they strings are the same length and contain the
241 g_string_equal (const GString
*v
,
245 GString
*string1
= (GString
*) v
;
246 GString
*string2
= (GString
*) v2
;
247 gsize i
= string1
->len
;
249 if (i
!= string2
->len
)
267 * @str: a string to hash
269 * Creates a hash code for @str; for use with #GHashTable.
271 * Returns: hash code for @str
274 g_string_hash (const GString
*str
)
276 const gchar
*p
= str
->str
;
280 /* 31 bit hash function */
283 h
= (h
<< 5) - h
+ *p
;
292 * @string: the destination #GString. Its current contents
294 * @rval: the string to copy into @string
296 * Copies the bytes from a string into a #GString,
297 * destroying any previous contents. It is rather like
298 * the standard strcpy() function, except that you do not
299 * have to worry about having enough space to copy the string.
304 g_string_assign (GString
*string
,
307 g_return_val_if_fail (string
!= NULL
, NULL
);
308 g_return_val_if_fail (rval
!= NULL
, string
);
310 /* Make sure assigning to itself doesn't corrupt the string. */
311 if (string
->str
!= rval
)
313 /* Assigning from substring should be ok, since
314 * g_string_truncate() does not reallocate.
316 g_string_truncate (string
, 0);
317 g_string_append (string
, rval
);
325 * @string: a #GString
326 * @len: the new size of @string
328 * Cuts off the end of the GString, leaving the first @len bytes.
333 g_string_truncate (GString
*string
,
336 g_return_val_if_fail (string
!= NULL
, NULL
);
338 string
->len
= MIN (len
, string
->len
);
339 string
->str
[string
->len
] = 0;
346 * @string: a #GString
347 * @len: the new length
349 * Sets the length of a #GString. If the length is less than
350 * the current length, the string will be truncated. If the
351 * length is greater than the current length, the contents
352 * of the newly added area are undefined. (However, as
353 * always, string->str[string->len] will be a nul byte.)
355 * Return value: @string
358 g_string_set_size (GString
*string
,
361 g_return_val_if_fail (string
!= NULL
, NULL
);
363 if (len
>= string
->allocated_len
)
364 g_string_maybe_expand (string
, len
- string
->len
);
367 string
->str
[len
] = 0;
373 * g_string_insert_len:
374 * @string: a #GString
375 * @pos: position in @string where insertion should
376 * happen, or -1 for at the end
377 * @val: bytes to insert
378 * @len: number of bytes of @val to insert
380 * Inserts @len bytes of @val into @string at @pos.
381 * Because @len is provided, @val may contain embedded
382 * nuls and need not be nul-terminated. If @pos is -1,
383 * bytes are inserted at the end of the string.
385 * Since this function does not stop at nul bytes, it is
386 * the caller's responsibility to ensure that @val has at
387 * least @len addressable bytes.
392 g_string_insert_len (GString
*string
,
397 g_return_val_if_fail (string
!= NULL
, NULL
);
398 g_return_val_if_fail (len
== 0 || val
!= NULL
, string
);
409 g_return_val_if_fail (pos
<= string
->len
, string
);
411 /* Check whether val represents a substring of string.
412 * This test probably violates chapter and verse of the C standards,
413 * since ">=" and "<=" are only valid when val really is a substring.
414 * In practice, it will work on modern archs.
416 if (val
>= string
->str
&& val
<= string
->str
+ string
->len
)
418 gsize offset
= val
- string
->str
;
421 g_string_maybe_expand (string
, len
);
422 val
= string
->str
+ offset
;
423 /* At this point, val is valid again. */
425 /* Open up space where we are going to insert. */
426 if (pos
< string
->len
)
427 g_memmove (string
->str
+ pos
+ len
, string
->str
+ pos
, string
->len
- pos
);
429 /* Move the source part before the gap, if any. */
432 precount
= MIN (len
, pos
- offset
);
433 memcpy (string
->str
+ pos
, val
, precount
);
436 /* Move the source part after the gap, if any. */
438 memcpy (string
->str
+ pos
+ precount
,
439 val
+ /* Already moved: */ precount
+ /* Space opened up: */ len
,
444 g_string_maybe_expand (string
, len
);
446 /* If we aren't appending at the end, move a hunk
447 * of the old string to the end, opening up space
449 if (pos
< string
->len
)
450 g_memmove (string
->str
+ pos
+ len
, string
->str
+ pos
, string
->len
- pos
);
452 /* insert the new string */
454 string
->str
[pos
] = *val
;
456 memcpy (string
->str
+ pos
, val
, len
);
461 string
->str
[string
->len
] = 0;
466 #define SUB_DELIM_CHARS "!$&'()*+,;="
470 const char *reserved_chars_allowed
)
472 if (g_ascii_isalnum (c
) ||
479 if (reserved_chars_allowed
&&
480 strchr (reserved_chars_allowed
, c
) != NULL
)
487 gunichar_ok (gunichar c
)
490 (c
!= (gunichar
) -2) &&
491 (c
!= (gunichar
) -1);
495 * g_string_append_uri_escaped:
496 * @string: a #GString
497 * @unescaped: a string
498 * @reserved_chars_allowed: a string of reserved characters allowed
499 * to be used, or %NULL
500 * @allow_utf8: set %TRUE if the escaped string may include UTF8 characters
502 * Appends @unescaped to @string, escaped any characters that
503 * are reserved in URIs using URI-style escape sequences.
510 g_string_append_uri_escaped (GString
*string
,
511 const gchar
*unescaped
,
512 const gchar
*reserved_chars_allowed
,
517 static const gchar hex
[16] = "0123456789ABCDEF";
519 g_return_val_if_fail (string
!= NULL
, NULL
);
520 g_return_val_if_fail (unescaped
!= NULL
, NULL
);
522 end
= unescaped
+ strlen (unescaped
);
524 while ((c
= *unescaped
) != 0)
526 if (c
>= 0x80 && allow_utf8
&&
527 gunichar_ok (g_utf8_get_char_validated (unescaped
, end
- unescaped
)))
529 int len
= g_utf8_skip
[c
];
530 g_string_append_len (string
, unescaped
, len
);
533 else if (is_valid (c
, reserved_chars_allowed
))
535 g_string_append_c (string
, c
);
540 g_string_append_c (string
, '%');
541 g_string_append_c (string
, hex
[((guchar
)c
) >> 4]);
542 g_string_append_c (string
, hex
[((guchar
)c
) & 0xf]);
552 * @string: a #GString
553 * @val: the string to append onto the end of @string
555 * Adds a string onto the end of a #GString, expanding
561 g_string_append (GString
*string
,
564 g_return_val_if_fail (string
!= NULL
, NULL
);
565 g_return_val_if_fail (val
!= NULL
, string
);
567 return g_string_insert_len (string
, -1, val
, -1);
571 * g_string_append_len:
572 * @string: a #GString
573 * @val: bytes to append
574 * @len: number of bytes of @val to use
576 * Appends @len bytes of @val to @string. Because @len is
577 * provided, @val may contain embedded nuls and need not
580 * Since this function does not stop at nul bytes, it is
581 * the caller's responsibility to ensure that @val has at
582 * least @len addressable bytes.
587 g_string_append_len (GString
*string
,
591 g_return_val_if_fail (string
!= NULL
, NULL
);
592 g_return_val_if_fail (len
== 0 || val
!= NULL
, string
);
594 return g_string_insert_len (string
, -1, val
, len
);
599 * @string: a #GString
600 * @c: the byte to append onto the end of @string
602 * Adds a byte onto the end of a #GString, expanding
607 #undef g_string_append_c
609 g_string_append_c (GString
*string
,
612 g_return_val_if_fail (string
!= NULL
, NULL
);
614 return g_string_insert_c (string
, -1, c
);
618 * g_string_append_unichar:
619 * @string: a #GString
620 * @wc: a Unicode character
622 * Converts a Unicode character into UTF-8, and appends it
625 * Return value: @string
628 g_string_append_unichar (GString
*string
,
631 g_return_val_if_fail (string
!= NULL
, NULL
);
633 return g_string_insert_unichar (string
, -1, wc
);
638 * @string: a #GString
639 * @val: the string to prepend on the start of @string
641 * Adds a string on to the start of a #GString,
642 * expanding it if necessary.
647 g_string_prepend (GString
*string
,
650 g_return_val_if_fail (string
!= NULL
, NULL
);
651 g_return_val_if_fail (val
!= NULL
, string
);
653 return g_string_insert_len (string
, 0, val
, -1);
657 * g_string_prepend_len:
658 * @string: a #GString
659 * @val: bytes to prepend
660 * @len: number of bytes in @val to prepend
662 * Prepends @len bytes of @val to @string.
663 * Because @len is provided, @val may contain
664 * embedded nuls and need not be nul-terminated.
666 * Since this function does not stop at nul bytes,
667 * it is the caller's responsibility to ensure that
668 * @val has at least @len addressable bytes.
673 g_string_prepend_len (GString
*string
,
677 g_return_val_if_fail (string
!= NULL
, NULL
);
678 g_return_val_if_fail (val
!= NULL
, string
);
680 return g_string_insert_len (string
, 0, val
, len
);
684 * g_string_prepend_c:
685 * @string: a #GString
686 * @c: the byte to prepend on the start of the #GString
688 * Adds a byte onto the start of a #GString,
689 * expanding it if necessary.
694 g_string_prepend_c (GString
*string
,
697 g_return_val_if_fail (string
!= NULL
, NULL
);
699 return g_string_insert_c (string
, 0, c
);
703 * g_string_prepend_unichar:
704 * @string: a #GString
705 * @wc: a Unicode character
707 * Converts a Unicode character into UTF-8, and prepends it
710 * Return value: @string
713 g_string_prepend_unichar (GString
*string
,
716 g_return_val_if_fail (string
!= NULL
, NULL
);
718 return g_string_insert_unichar (string
, 0, wc
);
723 * @string: a #GString
724 * @pos: the position to insert the copy of the string
725 * @val: the string to insert
727 * Inserts a copy of a string into a #GString,
728 * expanding it if necessary.
733 g_string_insert (GString
*string
,
737 g_return_val_if_fail (string
!= NULL
, NULL
);
738 g_return_val_if_fail (val
!= NULL
, string
);
741 g_return_val_if_fail (pos
<= string
->len
, string
);
743 return g_string_insert_len (string
, pos
, val
, -1);
748 * @string: a #GString
749 * @pos: the position to insert the byte
750 * @c: the byte to insert
752 * Inserts a byte into a #GString, expanding it if necessary.
757 g_string_insert_c (GString
*string
,
761 g_return_val_if_fail (string
!= NULL
, NULL
);
763 g_string_maybe_expand (string
, 1);
768 g_return_val_if_fail (pos
<= string
->len
, string
);
770 /* If not just an append, move the old stuff */
771 if (pos
< string
->len
)
772 g_memmove (string
->str
+ pos
+ 1, string
->str
+ pos
, string
->len
- pos
);
774 string
->str
[pos
] = c
;
778 string
->str
[string
->len
] = 0;
784 * g_string_insert_unichar:
785 * @string: a #GString
786 * @pos: the position at which to insert character, or -1
787 * to append at the end of the string
788 * @wc: a Unicode character
790 * Converts a Unicode character into UTF-8, and insert it
791 * into the string at the given position.
793 * Return value: @string
796 g_string_insert_unichar (GString
*string
,
800 gint charlen
, first
, i
;
803 g_return_val_if_fail (string
!= NULL
, NULL
);
805 /* Code copied from g_unichar_to_utf() */
816 else if (wc
< 0x10000)
821 else if (wc
< 0x200000)
826 else if (wc
< 0x4000000)
836 /* End of copied code */
838 g_string_maybe_expand (string
, charlen
);
843 g_return_val_if_fail (pos
<= string
->len
, string
);
845 /* If not just an append, move the old stuff */
846 if (pos
< string
->len
)
847 g_memmove (string
->str
+ pos
+ charlen
, string
->str
+ pos
, string
->len
- pos
);
849 dest
= string
->str
+ pos
;
850 /* Code copied from g_unichar_to_utf() */
851 for (i
= charlen
- 1; i
> 0; --i
)
853 dest
[i
] = (wc
& 0x3f) | 0x80;
856 dest
[0] = wc
| first
;
857 /* End of copied code */
859 string
->len
+= charlen
;
861 string
->str
[string
->len
] = 0;
867 * g_string_overwrite:
868 * @string: a #GString
869 * @pos: the position at which to start overwriting
870 * @val: the string that will overwrite the @string starting at @pos
872 * Overwrites part of a string, lengthening it if necessary.
874 * Return value: @string
879 g_string_overwrite (GString
*string
,
883 g_return_val_if_fail (val
!= NULL
, string
);
884 return g_string_overwrite_len (string
, pos
, val
, strlen (val
));
888 * g_string_overwrite_len:
889 * @string: a #GString
890 * @pos: the position at which to start overwriting
891 * @val: the string that will overwrite the @string starting at @pos
892 * @len: the number of bytes to write from @val
894 * Overwrites part of a string, lengthening it if necessary.
895 * This function will work with embedded nuls.
897 * Return value: @string
902 g_string_overwrite_len (GString
*string
,
909 g_return_val_if_fail (string
!= NULL
, NULL
);
914 g_return_val_if_fail (val
!= NULL
, string
);
915 g_return_val_if_fail (pos
<= string
->len
, string
);
922 if (end
> string
->len
)
923 g_string_maybe_expand (string
, end
- string
->len
);
925 memcpy (string
->str
+ pos
, val
, len
);
927 if (end
> string
->len
)
929 string
->str
[end
] = '\0';
938 * @string: a #GString
939 * @pos: the position of the content to remove
940 * @len: the number of bytes to remove, or -1 to remove all
943 * Removes @len bytes from a #GString, starting at position @pos.
944 * The rest of the #GString is shifted down to fill the gap.
949 g_string_erase (GString
*string
,
953 g_return_val_if_fail (string
!= NULL
, NULL
);
954 g_return_val_if_fail (pos
>= 0, string
);
955 g_return_val_if_fail (pos
<= string
->len
, string
);
958 len
= string
->len
- pos
;
961 g_return_val_if_fail (pos
+ len
<= string
->len
, string
);
963 if (pos
+ len
< string
->len
)
964 g_memmove (string
->str
+ pos
, string
->str
+ pos
+ len
, string
->len
- (pos
+ len
));
969 string
->str
[string
->len
] = 0;
975 * g_string_ascii_down:
978 * Converts all uppercase ASCII letters to lowercase ASCII letters.
980 * Return value: passed-in @string pointer, with all the
981 * uppercase characters converted to lowercase in place,
982 * with semantics that exactly match g_ascii_tolower().
985 g_string_ascii_down (GString
*string
)
990 g_return_val_if_fail (string
!= NULL
, NULL
);
997 *s
= g_ascii_tolower (*s
);
1006 * g_string_ascii_up:
1007 * @string: a GString
1009 * Converts all lowercase ASCII letters to uppercase ASCII letters.
1011 * Return value: passed-in @string pointer, with all the
1012 * lowercase characters converted to uppercase in place,
1013 * with semantics that exactly match g_ascii_toupper().
1016 g_string_ascii_up (GString
*string
)
1021 g_return_val_if_fail (string
!= NULL
, NULL
);
1028 *s
= g_ascii_toupper (*s
);
1038 * @string: a #GString
1040 * Converts a #GString to lowercase.
1042 * Returns: the #GString
1044 * Deprecated:2.2: This function uses the locale-specific
1045 * tolower() function, which is almost never the right thing.
1046 * Use g_string_ascii_down() or g_utf8_strdown() instead.
1049 g_string_down (GString
*string
)
1054 g_return_val_if_fail (string
!= NULL
, NULL
);
1057 s
= (guchar
*) string
->str
;
1072 * @string: a #GString
1074 * Converts a #GString to uppercase.
1076 * Return value: @string
1078 * Deprecated:2.2: This function uses the locale-specific
1079 * toupper() function, which is almost never the right thing.
1080 * Use g_string_ascii_up() or g_utf8_strup() instead.
1083 g_string_up (GString
*string
)
1088 g_return_val_if_fail (string
!= NULL
, NULL
);
1091 s
= (guchar
*) string
->str
;
1105 * g_string_append_vprintf:
1106 * @string: a #GString
1107 * @format: the string format. See the printf() documentation
1108 * @args: the list of arguments to insert in the output
1110 * Appends a formatted string onto the end of a #GString.
1111 * This function is similar to g_string_append_printf()
1112 * except that the arguments to the format string are passed
1118 g_string_append_vprintf (GString
*string
,
1119 const gchar
*format
,
1125 g_return_if_fail (string
!= NULL
);
1126 g_return_if_fail (format
!= NULL
);
1128 len
= g_vasprintf (&buf
, format
, args
);
1132 g_string_maybe_expand (string
, len
);
1133 memcpy (string
->str
+ string
->len
, buf
, len
+ 1);
1141 * @string: a #GString
1142 * @format: the string format. See the printf() documentation
1143 * @args: the parameters to insert into the format string
1145 * Writes a formatted string into a #GString.
1146 * This function is similar to g_string_printf() except that
1147 * the arguments to the format string are passed as a va_list.
1152 g_string_vprintf (GString
*string
,
1153 const gchar
*format
,
1156 g_string_truncate (string
, 0);
1157 g_string_append_vprintf (string
, format
, args
);
1162 * @string: a #GString
1163 * @format: the string format. See the sprintf() documentation
1164 * @...: the parameters to insert into the format string
1166 * Writes a formatted string into a #GString.
1167 * This is similar to the standard sprintf() function,
1168 * except that the #GString buffer automatically expands
1169 * to contain the results. The previous contents of the
1170 * #GString are destroyed.
1172 * Deprecated: This function has been renamed to g_string_printf().
1177 * @string: a #GString
1178 * @format: the string format. See the printf() documentation
1179 * @...: the parameters to insert into the format string
1181 * Writes a formatted string into a #GString.
1182 * This is similar to the standard sprintf() function,
1183 * except that the #GString buffer automatically expands
1184 * to contain the results. The previous contents of the
1185 * #GString are destroyed.
1188 g_string_printf (GString
*string
,
1189 const gchar
*format
,
1194 g_string_truncate (string
, 0);
1196 va_start (args
, format
);
1197 g_string_append_vprintf (string
, format
, args
);
1202 * g_string_sprintfa:
1203 * @string: a #GString
1204 * @format: the string format. See the sprintf() documentation
1205 * @...: the parameters to insert into the format string
1207 * Appends a formatted string onto the end of a #GString.
1208 * This function is similar to g_string_sprintf() except that
1209 * the text is appended to the #GString.
1211 * Deprecated: This function has been renamed to g_string_append_printf()
1215 * g_string_append_printf:
1216 * @string: a #GString
1217 * @format: the string format. See the printf() documentation
1218 * @...: the parameters to insert into the format string
1220 * Appends a formatted string onto the end of a #GString.
1221 * This function is similar to g_string_printf() except
1222 * that the text is appended to the #GString.
1225 g_string_append_printf (GString
*string
,
1226 const gchar
*format
,
1231 va_start (args
, format
);
1232 g_string_append_vprintf (string
, format
, args
);