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/.
49 GHashTable *const_table;
62 * @v2: a key to compare with @v1
64 * Compares two strings for byte-by-byte equality and returns %TRUE
65 * if they are equal. It can be passed to g_hash_table_new() as the
66 * @key_equal_func parameter, when using strings as keys in a #GHashTable.
68 * Returns: %TRUE if the two keys match
71 g_str_equal (gconstpointer v1,
74 const gchar *string1 = v1;
75 const gchar *string2 = v2;
77 return strcmp (string1, string2) == 0;
84 * Converts a string to a hash value.
85 * It can be passed to g_hash_table_new() as the @hash_func
86 * parameter, when using strings as keys in a #GHashTable.
88 * Returns: a hash value corresponding to the key
91 g_str_hash (gconstpointer v)
93 /* 31 bit hash function */
94 const signed char *p = v;
98 for (p += 1; *p != '\0'; p++)
99 h = (h << 5) - h + *p;
104 #define MY_MAXSIZE ((gsize)-1)
107 nearest_power (gsize base, gsize num)
109 if (num > MY_MAXSIZE / 2)
128 * g_string_chunk_new:
129 * @size: the default size of the blocks of memory which are
130 * allocated to store the strings. If a particular string
131 * is larger than this default size, a larger block of
132 * memory will be allocated for it.
134 * Creates a new #GStringChunk.
136 * Returns: a new #GStringChunk
139 g_string_chunk_new (gsize size)
141 GStringChunk *new_chunk = g_new (GStringChunk, 1);
142 gsize actual_size = 1;
144 actual_size = nearest_power (1, size);
146 new_chunk->const_table = NULL;
147 new_chunk->storage_list = NULL;
148 new_chunk->storage_next = actual_size;
149 new_chunk->default_size = actual_size;
150 new_chunk->this_size = actual_size;
156 * g_string_chunk_free:
157 * @chunk: a #GStringChunk
159 * Frees all memory allocated by the #GStringChunk.
160 * After calling g_string_chunk_free() it is not safe to
161 * access any of the strings which were contained within it.
164 g_string_chunk_free (GStringChunk *chunk)
168 g_return_if_fail (chunk != NULL);
170 if (chunk->storage_list)
172 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
173 g_free (tmp_list->data);
175 g_slist_free (chunk->storage_list);
178 if (chunk->const_table)
179 g_hash_table_destroy (chunk->const_table);
185 * g_string_chunk_clear:
186 * @chunk: a #GStringChunk
188 * Frees all strings contained within the #GStringChunk.
189 * After calling g_string_chunk_clear() it is not safe to
190 * access any of the strings which were contained within it.
195 g_string_chunk_clear (GStringChunk *chunk)
199 g_return_if_fail (chunk != NULL);
201 if (chunk->storage_list)
203 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
204 g_free (tmp_list->data);
206 g_slist_free (chunk->storage_list);
208 chunk->storage_list = NULL;
209 chunk->storage_next = chunk->default_size;
210 chunk->this_size = chunk->default_size;
213 if (chunk->const_table)
214 g_hash_table_remove_all (chunk->const_table);
218 * g_string_chunk_insert:
219 * @chunk: a #GStringChunk
220 * @string: the string to add
222 * Adds a copy of @string to the #GStringChunk.
223 * It returns a pointer to the new copy of the string
224 * in the #GStringChunk. The characters in the string
225 * can be changed, if necessary, though you should not
226 * change anything after the end of the string.
228 * Unlike g_string_chunk_insert_const(), this function
229 * does not check for duplicates. Also strings added
230 * with g_string_chunk_insert() will not be searched
231 * by g_string_chunk_insert_const() when looking for
234 * Returns: a pointer to the copy of @string within
238 g_string_chunk_insert (GStringChunk *chunk,
241 g_return_val_if_fail (chunk != NULL, NULL);
243 return g_string_chunk_insert_len (chunk, string, -1);
247 * g_string_chunk_insert_const:
248 * @chunk: a #GStringChunk
249 * @string: the string to add
251 * Adds a copy of @string to the #GStringChunk, unless the same
252 * string has already been added to the #GStringChunk with
253 * g_string_chunk_insert_const().
255 * This function is useful if you need to copy a large number
256 * of strings but do not want to waste space storing duplicates.
257 * But you must remember that there may be several pointers to
258 * the same string, and so any changes made to the strings
259 * should be done very carefully.
261 * Note that g_string_chunk_insert_const() will not return a
262 * pointer to a string added with g_string_chunk_insert(), even
265 * Returns: a pointer to the new or existing copy of @string
266 * within the #GStringChunk
269 g_string_chunk_insert_const (GStringChunk *chunk,
274 g_return_val_if_fail (chunk != NULL, NULL);
276 if (!chunk->const_table)
277 chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
279 lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
283 lookup = g_string_chunk_insert (chunk, string);
284 g_hash_table_insert (chunk->const_table, lookup, lookup);
291 * g_string_chunk_insert_len:
292 * @chunk: a #GStringChunk
293 * @string: bytes to insert
294 * @len: number of bytes of @string to insert, or -1 to insert a
295 * nul-terminated string
297 * Adds a copy of the first @len bytes of @string to the #GStringChunk.
298 * The copy is nul-terminated.
300 * Since this function does not stop at nul bytes, it is the caller's
301 * responsibility to ensure that @string has at least @len addressable
304 * The characters in the returned string can be changed, if necessary,
305 * though you should not change anything after the end of the string.
307 * Return value: a pointer to the copy of @string within the #GStringChunk
312 g_string_chunk_insert_len (GStringChunk *chunk,
319 g_return_val_if_fail (chunk != NULL, NULL);
322 size = strlen (string);
326 if ((chunk->storage_next + size + 1) > chunk->this_size)
328 gsize new_size = nearest_power (chunk->default_size, size + 1);
330 chunk->storage_list = g_slist_prepend (chunk->storage_list,
331 g_new (gchar, new_size));
333 chunk->this_size = new_size;
334 chunk->storage_next = 0;
337 pos = ((gchar *) chunk->storage_list->data) + chunk->storage_next;
339 *(pos + size) = '\0';
341 strncpy (pos, string, size);
345 chunk->storage_next += size + 1;
353 g_string_maybe_expand (GString* string,
356 if (string->len + len >= string->allocated_len)
358 string->allocated_len = nearest_power (1, string->len + len + 1);
359 string->str = g_realloc (string->str, string->allocated_len);
364 * g_string_sized_new:
365 * @dfl_size: the default size of the space allocated to
368 * Creates a new #GString, with enough space for @dfl_size
369 * bytes. This is useful if you are going to add a lot of
370 * text to the string and don't want it to be reallocated
373 * Returns: the new #GString
376 g_string_sized_new (gsize dfl_size)
378 GString *string = g_slice_new (GString);
380 string->allocated_len = 0;
384 g_string_maybe_expand (string, MAX (dfl_size, 2));
392 * @init: the initial text to copy into the string
394 * Creates a new #GString, initialized with the given string.
396 * Returns: the new #GString
399 g_string_new (const gchar *init)
403 if (init == NULL || *init == '\0')
404 string = g_string_sized_new (2);
410 string = g_string_sized_new (len + 2);
412 g_string_append_len (string, init, len);
420 * @init: initial contents of the string
421 * @len: length of @init to use
423 * Creates a new #GString with @len bytes of the @init buffer.
424 * Because a length is provided, @init need not be nul-terminated,
425 * and can contain embedded nul bytes.
427 * Since this function does not stop at nul bytes, it is the caller's
428 * responsibility to ensure that @init has at least @len addressable
431 * Returns: a new #GString
434 g_string_new_len (const gchar *init,
440 return g_string_new (init);
443 string = g_string_sized_new (len);
446 g_string_append_len (string, init, len);
454 * @string: a #GString
455 * @free_segment: if %TRUE the actual character data is freed as well
457 * Frees the memory allocated for the #GString.
458 * If @free_segment is %TRUE it also frees the character data.
460 * Returns: the character data of @string
461 * (i.e. %NULL if @free_segment is %TRUE)
464 g_string_free (GString *string,
465 gboolean free_segment)
469 g_return_val_if_fail (string != NULL, NULL);
473 g_free (string->str);
477 segment = string->str;
479 g_slice_free (GString, string);
487 * @v2: another #GString
489 * Compares two strings for equality, returning %TRUE if they are equal.
490 * For use with #GHashTable.
492 * Returns: %TRUE if they strings are the same length and contain the
496 g_string_equal (const GString *v,
500 GString *string1 = (GString *) v;
501 GString *string2 = (GString *) v2;
502 gsize i = string1->len;
504 if (i != string2->len)
522 * @str: a string to hash
524 * Creates a hash code for @str; for use with #GHashTable.
526 * Returns: hash code for @str
528 /* 31 bit hash function */
530 g_string_hash (const GString *str)
532 const gchar *p = str->str;
538 h = (h << 5) - h + *p;
547 * @string: the destination #GString. Its current contents
549 * @rval: the string to copy into @string
551 * Copies the bytes from a string into a #GString,
552 * destroying any previous contents. It is rather like
553 * the standard strcpy() function, except that you do not
554 * have to worry about having enough space to copy the string.
559 g_string_assign (GString *string,
562 g_return_val_if_fail (string != NULL, NULL);
563 g_return_val_if_fail (rval != NULL, string);
565 /* Make sure assigning to itself doesn't corrupt the string. */
566 if (string->str != rval)
568 /* Assigning from substring should be ok since g_string_truncate
570 g_string_truncate (string, 0);
571 g_string_append (string, rval);
579 * @string: a #GString
580 * @len: the new size of @string
582 * Cuts off the end of the GString, leaving the first @len bytes.
587 g_string_truncate (GString *string,
590 g_return_val_if_fail (string != NULL, NULL);
592 string->len = MIN (len, string->len);
593 string->str[string->len] = 0;
600 * @string: a #GString
601 * @len: the new length
603 * Sets the length of a #GString. If the length is less than
604 * the current length, the string will be truncated. If the
605 * length is greater than the current length, the contents
606 * of the newly added area are undefined. (However, as
607 * always, string->str[string->len] will be a nul byte.)
609 * Return value: @string
612 g_string_set_size (GString *string,
615 g_return_val_if_fail (string != NULL, NULL);
617 if (len >= string->allocated_len)
618 g_string_maybe_expand (string, len - string->len);
621 string->str[len] = 0;
627 * g_string_insert_len:
628 * @string: a #GString
629 * @pos: position in @string where insertion should
630 * happen, or -1 for at the end
631 * @val: bytes to insert
632 * @len: number of bytes of @val to insert
634 * Inserts @len bytes of @val into @string at @pos.
635 * Because @len is provided, @val may contain embedded
636 * nuls and need not be nul-terminated. If @pos is -1,
637 * bytes are inserted at the end of the string.
639 * Since this function does not stop at nul bytes, it is
640 * the caller's responsibility to ensure that @val has at
641 * least @len addressable bytes.
646 g_string_insert_len (GString *string,
651 g_return_val_if_fail (string != NULL, NULL);
652 g_return_val_if_fail (val != NULL, string);
660 g_return_val_if_fail (pos <= string->len, string);
662 /* Check whether val represents a substring of string. This test
663 probably violates chapter and verse of the C standards, since
664 ">=" and "<=" are only valid when val really is a substring.
665 In practice, it will work on modern archs. */
666 if (val >= string->str && val <= string->str + string->len)
668 gsize offset = val - string->str;
671 g_string_maybe_expand (string, len);
672 val = string->str + offset;
673 /* At this point, val is valid again. */
675 /* Open up space where we are going to insert. */
676 if (pos < string->len)
677 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
679 /* Move the source part before the gap, if any. */
682 precount = MIN (len, pos - offset);
683 memcpy (string->str + pos, val, precount);
686 /* Move the source part after the gap, if any. */
688 memcpy (string->str + pos + precount,
689 val + /* Already moved: */ precount + /* Space opened up: */ len,
694 g_string_maybe_expand (string, len);
696 /* If we aren't appending at the end, move a hunk
697 * of the old string to the end, opening up space
699 if (pos < string->len)
700 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
702 /* insert the new string */
704 string->str[pos] = *val;
706 memcpy (string->str + pos, val, len);
711 string->str[string->len] = 0;
718 * @string: a #GString
719 * @val: the string to append onto the end of @string
721 * Adds a string onto the end of a #GString, expanding
727 g_string_append (GString *string,
730 g_return_val_if_fail (string != NULL, NULL);
731 g_return_val_if_fail (val != NULL, string);
733 return g_string_insert_len (string, -1, val, -1);
737 * g_string_append_len:
738 * @string: a #GString
739 * @val: bytes to append
740 * @len: number of bytes of @val to use
742 * Appends @len bytes of @val to @string. Because @len is
743 * provided, @val may contain embedded nuls and need not
746 * Since this function does not stop at nul bytes, it is
747 * the caller's responsibility to ensure that @val has at
748 * least @len addressable bytes.
753 g_string_append_len (GString *string,
757 g_return_val_if_fail (string != NULL, NULL);
758 g_return_val_if_fail (val != NULL, string);
760 return g_string_insert_len (string, -1, val, len);
765 * @string: a #GString
766 * @c: the byte to append onto the end of @string
768 * Adds a byte onto the end of a #GString, expanding
773 #undef g_string_append_c
775 g_string_append_c (GString *string,
778 g_return_val_if_fail (string != NULL, NULL);
780 return g_string_insert_c (string, -1, c);
784 * g_string_append_unichar:
785 * @string: a #GString
786 * @wc: a Unicode character
788 * Converts a Unicode character into UTF-8, and appends it
791 * Return value: @string
794 g_string_append_unichar (GString *string,
797 g_return_val_if_fail (string != NULL, NULL);
799 return g_string_insert_unichar (string, -1, wc);
804 * @string: a #GString
805 * @val: the string to prepend on the start of @string
807 * Adds a string on to the start of a #GString,
808 * expanding it if necessary.
813 g_string_prepend (GString *string,
816 g_return_val_if_fail (string != NULL, NULL);
817 g_return_val_if_fail (val != NULL, string);
819 return g_string_insert_len (string, 0, val, -1);
823 * g_string_prepend_len:
824 * @string: a #GString
825 * @val: bytes to prepend
826 * @len: number of bytes in @val to prepend
828 * Prepends @len bytes of @val to @string.
829 * Because @len is provided, @val may contain
830 * embedded nuls and need not be nul-terminated.
832 * Since this function does not stop at nul bytes,
833 * it is the caller's responsibility to ensure that
834 * @val has at least @len addressable bytes.
839 g_string_prepend_len (GString *string,
843 g_return_val_if_fail (string != NULL, NULL);
844 g_return_val_if_fail (val != NULL, string);
846 return g_string_insert_len (string, 0, val, len);
850 * g_string_prepend_c:
851 * @string: a #GString
852 * @c: the byte to prepend on the start of the #GString
854 * Adds a byte onto the start of a #GString,
855 * expanding it if necessary.
860 g_string_prepend_c (GString *string,
863 g_return_val_if_fail (string != NULL, NULL);
865 return g_string_insert_c (string, 0, c);
869 * g_string_prepend_unichar:
870 * @string: a #GString
871 * @wc: a Unicode character
873 * Converts a Unicode character into UTF-8, and prepends it
876 * Return value: @string
879 g_string_prepend_unichar (GString *string,
882 g_return_val_if_fail (string != NULL, NULL);
884 return g_string_insert_unichar (string, 0, wc);
889 * @string: a #GString
890 * @pos: the position to insert the copy of the string
891 * @val: the string to insert
893 * Inserts a copy of a string into a #GString,
894 * expanding it if necessary.
899 g_string_insert (GString *string,
903 g_return_val_if_fail (string != NULL, NULL);
904 g_return_val_if_fail (val != NULL, string);
906 g_return_val_if_fail (pos <= string->len, string);
908 return g_string_insert_len (string, pos, val, -1);
913 * @string: a #GString
914 * @pos: the position to insert the byte
915 * @c: the byte to insert
917 * Inserts a byte into a #GString, expanding it if necessary.
922 g_string_insert_c (GString *string,
926 g_return_val_if_fail (string != NULL, NULL);
928 g_string_maybe_expand (string, 1);
933 g_return_val_if_fail (pos <= string->len, string);
935 /* If not just an append, move the old stuff */
936 if (pos < string->len)
937 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
939 string->str[pos] = c;
943 string->str[string->len] = 0;
949 * g_string_insert_unichar:
950 * @string: a #GString
951 * @pos: the position at which to insert character, or -1 to
952 * append at the end of the string
953 * @wc: a Unicode character
955 * Converts a Unicode character into UTF-8, and insert it
956 * into the string at the given position.
958 * Return value: @string
961 g_string_insert_unichar (GString *string,
965 gint charlen, first, i;
968 g_return_val_if_fail (string != NULL, NULL);
970 /* Code copied from g_unichar_to_utf() */
981 else if (wc < 0x10000)
986 else if (wc < 0x200000)
991 else if (wc < 0x4000000)
1001 /* End of copied code */
1003 g_string_maybe_expand (string, charlen);
1008 g_return_val_if_fail (pos <= string->len, string);
1010 /* If not just an append, move the old stuff */
1011 if (pos < string->len)
1012 g_memmove (string->str + pos + charlen, string->str + pos, string->len - pos);
1014 dest = string->str + pos;
1015 /* Code copied from g_unichar_to_utf() */
1016 for (i = charlen - 1; i > 0; --i)
1018 dest[i] = (wc & 0x3f) | 0x80;
1021 dest[0] = wc | first;
1022 /* End of copied code */
1024 string->len += charlen;
1026 string->str[string->len] = 0;
1032 * g_string_overwrite:
1033 * @string: a #GString
1034 * @pos: the position at which to start overwriting
1035 * @val: the string that will overwrite the @string starting at @pos
1037 * Overwrites part of a string, lengthening it if necessary.
1039 * Return value: @string
1044 g_string_overwrite (GString *string,
1048 g_return_val_if_fail (val != NULL, string);
1049 return g_string_overwrite_len (string, pos, val, strlen (val));
1053 * g_string_overwrite_len:
1054 * @string: a #GString
1055 * @pos: the position at which to start overwriting
1056 * @val: the string that will overwrite the @string starting at @pos
1057 * @len: the number of bytes to write from @val
1059 * Overwrites part of a string, lengthening it if necessary.
1060 * This function will work with embedded nuls.
1062 * Return value: @string
1067 g_string_overwrite_len (GString *string,
1074 g_return_val_if_fail (string != NULL, NULL);
1079 g_return_val_if_fail (val != NULL, string);
1080 g_return_val_if_fail (pos <= string->len, string);
1087 if (end > string->len)
1088 g_string_maybe_expand (string, end - string->len);
1090 memcpy (string->str + pos, val, len);
1092 if (end > string->len)
1094 string->str[end] = '\0';
1103 * @string: a #GString
1104 * @pos: the position of the content to remove
1105 * @len: the number of bytes to remove, or -1 to remove all
1108 * Removes @len bytes from a #GString, starting at position @pos.
1109 * The rest of the #GString is shifted down to fill the gap.
1114 g_string_erase (GString *string,
1118 g_return_val_if_fail (string != NULL, NULL);
1119 g_return_val_if_fail (pos >= 0, string);
1120 g_return_val_if_fail (pos <= string->len, string);
1123 len = string->len - pos;
1126 g_return_val_if_fail (pos + len <= string->len, string);
1128 if (pos + len < string->len)
1129 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
1134 string->str[string->len] = 0;
1140 * g_string_ascii_down:
1141 * @string: a GString
1143 * Converts all upper case ASCII letters to lower case ASCII letters.
1145 * Return value: passed-in @string pointer, with all the upper case
1146 * characters converted to lower case in place, with
1147 * semantics that exactly match g_ascii_tolower().
1150 g_string_ascii_down (GString *string)
1155 g_return_val_if_fail (string != NULL, NULL);
1162 *s = g_ascii_tolower (*s);
1171 * g_string_ascii_up:
1172 * @string: a GString
1174 * Converts all lower case ASCII letters to upper case ASCII letters.
1176 * Return value: passed-in @string pointer, with all the lower case
1177 * characters converted to upper case in place, with
1178 * semantics that exactly match g_ascii_toupper().
1181 g_string_ascii_up (GString *string)
1186 g_return_val_if_fail (string != NULL, NULL);
1193 *s = g_ascii_toupper (*s);
1203 * @string: a #GString
1205 * Converts a #GString to lowercase.
1207 * Returns: the #GString.
1209 * Deprecated:2.2: This function uses the locale-specific
1210 * tolower() function, which is almost never the right thing.
1211 * Use g_string_ascii_down() or g_utf8_strdown() instead.
1214 g_string_down (GString *string)
1219 g_return_val_if_fail (string != NULL, NULL);
1222 s = (guchar *) string->str;
1237 * @string: a #GString
1239 * Converts a #GString to uppercase.
1241 * Return value: @string
1243 * Deprecated:2.2: This function uses the locale-specific
1244 * toupper() function, which is almost never the right thing.
1245 * Use g_string_ascii_up() or g_utf8_strup() instead.
1248 g_string_up (GString *string)
1253 g_return_val_if_fail (string != NULL, NULL);
1256 s = (guchar *) string->str;
1270 * g_string_append_vprintf:
1271 * @string: a #GString
1272 * @format: the string format. See the printf() documentation
1273 * @args: the list of arguments to insert in the output
1275 * Appends a formatted string onto the end of a #GString.
1276 * This function is is similar to g_string_append_printf()
1277 * except that the arguments to the format string are passed
1283 g_string_append_vprintf (GString *string,
1284 const gchar *format,
1290 g_return_if_fail (string != NULL);
1291 g_return_if_fail (format != NULL);
1293 len = g_vasprintf (&buf, format, args);
1297 g_string_maybe_expand (string, len);
1298 memcpy (string->str + string->len, buf, len + 1);
1306 * @string: a #GString
1307 * @format: the string format. See the printf() documentation
1308 * @args: the parameters to insert into the format string
1310 * Writes a formatted string into a #GString.
1311 * This function is similar to g_string_printf() except that
1312 * the arguments to the format string are passed as a va_list.
1317 g_string_vprintf (GString *string,
1318 const gchar *format,
1321 g_string_truncate (string, 0);
1322 g_string_append_vprintf (string, format, args);
1327 * @string: a #GString
1328 * @format: the string format. See the sprintf() documentation
1329 * @Varargs: the parameters to insert into the format string
1331 * Writes a formatted string into a #GString.
1332 * This is similar to the standard sprintf() function,
1333 * except that the #GString buffer automatically expands
1334 * to contain the results. The previous contents of the
1335 * #GString are destroyed.
1337 * Deprecated: This function has been renamed to g_string_printf().
1342 * @string: a #GString
1343 * @format: the string format. See the printf() documentation
1344 * @Varargs: the parameters to insert into the format string
1346 * Writes a formatted string into a #GString.
1347 * This is similar to the standard sprintf() function,
1348 * except that the #GString buffer automatically expands
1349 * to contain the results. The previous contents of the
1350 * #GString are destroyed.
1353 g_string_printf (GString *string,
1354 const gchar *format,
1359 g_string_truncate (string, 0);
1361 va_start (args, format);
1362 g_string_append_vprintf (string, format, args);
1367 * g_string_sprintfa:
1368 * @string: a #GString
1369 * @format: the string format. See the sprintf() documentation
1370 * @Varargs: the parameters to insert into the format string
1372 * Appends a formatted string onto the end of a #GString.
1373 * This function is is similar to g_string_sprintf() except that
1374 * the text is appended to the #GString.
1376 * Deprecated: This function has been renamed to g_string_append_printf()
1380 * g_string_append_printf:
1381 * @string: a #GString
1382 * @format: the string format. See the printf() documentation
1383 * @Varargs: the parameters to insert into the format string
1385 * Appends a formatted string onto the end of a #GString.
1386 * This function is is similar to g_string_printf() except
1387 * that the text is appended to the #GString.
1390 g_string_append_printf (GString *string,
1391 const gchar *format,
1396 va_start (args, format);
1397 g_string_append_vprintf (string, format, args);
1401 #define SUB_DELIM_CHARS "!$&'()*+,;="
1404 is_valid (char c, const char *reserved_chars_allowed)
1406 if (g_ascii_isalnum (c) ||
1413 if (reserved_chars_allowed &&
1414 strchr (reserved_chars_allowed, c) != NULL)
1421 gunichar_ok (gunichar c)
1424 (c != (gunichar) -2) &&
1425 (c != (gunichar) -1);
1429 * g_string_append_uri_escaped:
1430 * @string: a #GString
1431 * @unescaped: a string
1432 * @reserved_chars_allowed: a string of reserved characters allowed to be used
1433 * @allow_utf8: set %TRUE if the escaped string may include UTF8 characters
1435 * Appends @unescaped to @string, escaped any characters that
1436 * are reserved in URIs using URI-style escape sequences.
1443 g_string_append_uri_escaped (GString *string,
1444 const char *unescaped,
1445 const char *reserved_chars_allowed,
1446 gboolean allow_utf8)
1450 static const gchar hex[16] = "0123456789ABCDEF";
1452 g_return_val_if_fail (string != NULL, NULL);
1453 g_return_val_if_fail (unescaped != NULL, NULL);
1455 end = unescaped + strlen (unescaped);
1457 while ((c = *unescaped) != 0)
1459 if (c >= 0x80 && allow_utf8 &&
1460 gunichar_ok (g_utf8_get_char_validated (unescaped, end - unescaped)))
1462 int len = g_utf8_skip [c];
1463 g_string_append_len (string, unescaped, len);
1466 else if (is_valid (c, reserved_chars_allowed))
1468 g_string_append_c (string, c);
1473 g_string_append_c (string, '%');
1474 g_string_append_c (string, hex[((guchar)c) >> 4]);
1475 g_string_append_c (string, hex[((guchar)c) & 0xf]);
1484 #define __G_STRING_C__
1485 #include "galiasdef.c"