1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
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/.
40 #include "guriprivate.h"
42 #include "gutilsprivate.h"
48 * @short_description: text buffers which grow automatically
51 * A #GString is an object that handles the memory management of a C
52 * string for you. The emphasis of #GString is on text, typically
53 * UTF-8. Crucially, the "str" member of a #GString is guaranteed to
54 * have a trailing nul character, and it is therefore always safe to
55 * call functions such as strchr() or g_strdup() on it.
57 * However, a #GString can also hold arbitrary binary data, because it
58 * has a "len" member, which includes any possible embedded nul
59 * characters in the data. Conceptually then, #GString is like a
60 * #GByteArray with the addition of many convenience methods for text,
61 * and a guaranteed nul terminator.
66 * @str: points to the character data. It may move as text is added.
67 * The @str field is null-terminated and so
68 * can be used as an ordinary C string.
69 * @len: contains the length of the string, not including the
70 * terminating nul byte.
71 * @allocated_len: the number of bytes that can be stored in the
72 * string before it needs to be reallocated. May be larger than @len.
74 * The GString struct contains the public fields of a GString.
78 g_string_expand (GString *string,
81 /* Detect potential overflow */
82 if G_UNLIKELY ((G_MAXSIZE - string->len - 1) < len)
83 g_error ("adding %" G_GSIZE_FORMAT " to string would overflow", len);
85 string->allocated_len = g_nearest_pow (string->len + len + 1);
86 /* If the new size is bigger than G_MAXSIZE / 2, only allocate enough
87 * memory for this string and don't over-allocate.
89 if (string->allocated_len == 0)
90 string->allocated_len = string->len + len + 1;
92 string->str = g_realloc (string->str, string->allocated_len);
96 g_string_maybe_expand (GString *string,
99 if (G_UNLIKELY (string->len + len >= string->allocated_len))
100 g_string_expand (string, len);
104 * g_string_sized_new: (constructor)
105 * @dfl_size: the default size of the space allocated to hold the string
107 * Creates a new #GString, with enough space for @dfl_size
108 * bytes. This is useful if you are going to add a lot of
109 * text to the string and don't want it to be reallocated
112 * Returns: (transfer full): the new #GString
115 g_string_sized_new (gsize dfl_size)
117 GString *string = g_slice_new (GString);
119 string->allocated_len = 0;
123 g_string_expand (string, MAX (dfl_size, 64));
130 * g_string_new: (constructor)
131 * @init: (nullable): the initial text to copy into the string, or %NULL to
132 * start with an empty string
134 * Creates a new #GString, initialized with the given string.
136 * Returns: (transfer full): the new #GString
139 g_string_new (const gchar *init)
143 if (init == NULL || *init == '\0')
144 string = g_string_sized_new (2);
150 string = g_string_sized_new (len + 2);
152 g_string_append_len (string, init, len);
159 * g_string_new_take: (constructor)
160 * @init: (nullable) (transfer full): initial text used as the string.
161 * Ownership of the string is transferred to the #GString.
162 * Passing %NULL creates an empty string.
164 * Creates a new #GString, initialized with the given string.
166 * After this call, @init belongs to the #GString and may no longer be
167 * modified by the caller. The memory of @data has to be dynamically
168 * allocated and will eventually be freed with g_free().
170 * Returns: (transfer full): the new #GString
175 g_string_new_take (gchar *init)
181 return g_string_new (NULL);
184 string = g_slice_new (GString);
187 string->len = strlen (string->str);
188 string->allocated_len = string->len + 1;
194 * g_string_new_len: (constructor)
195 * @init: initial contents of the string
196 * @len: length of @init to use
198 * Creates a new #GString with @len bytes of the @init buffer.
199 * Because a length is provided, @init need not be nul-terminated,
200 * and can contain embedded nul bytes.
202 * Since this function does not stop at nul bytes, it is the caller's
203 * responsibility to ensure that @init has at least @len addressable
206 * Returns: (transfer full): a new #GString
209 g_string_new_len (const gchar *init,
215 return g_string_new (init);
218 string = g_string_sized_new (len);
221 g_string_append_len (string, init, len);
229 * @string: (transfer full): a #GString
230 * @free_segment: if %TRUE, the actual character data is freed as well
232 * Frees the memory allocated for the #GString.
233 * If @free_segment is %TRUE it also frees the character data. If
234 * it's %FALSE, the caller gains ownership of the buffer and must
235 * free it after use with g_free().
237 * Instead of passing %FALSE to this function, consider using
238 * g_string_free_and_steal().
240 * Returns: (nullable): the character data of @string
241 * (i.e. %NULL if @free_segment is %TRUE)
244 (g_string_free) (GString *string,
245 gboolean free_segment)
249 g_return_val_if_fail (string != NULL, NULL);
253 g_free (string->str);
257 segment = string->str;
259 g_slice_free (GString, string);
265 * g_string_free_and_steal:
266 * @string: (transfer full): a #GString
268 * Frees the memory allocated for the #GString.
270 * The caller gains ownership of the buffer and
271 * must free it after use with g_free().
273 * Returns: (transfer full): the character data of @string
278 g_string_free_and_steal (GString *string)
280 return (g_string_free) (string, FALSE);
284 * g_string_free_to_bytes:
285 * @string: (transfer full): a #GString
287 * Transfers ownership of the contents of @string to a newly allocated
288 * #GBytes. The #GString structure itself is deallocated, and it is
289 * therefore invalid to use @string after invoking this function.
291 * Note that while #GString ensures that its buffer always has a
292 * trailing nul character (not reflected in its "len"), the returned
293 * #GBytes does not include this extra nul; i.e. it has length exactly
294 * equal to the "len" member.
296 * Returns: (transfer full): A newly allocated #GBytes containing contents of @string; @string itself is freed
300 g_string_free_to_bytes (GString *string)
305 g_return_val_if_fail (string != NULL, NULL);
309 buf = g_string_free (string, FALSE);
311 return g_bytes_new_take (buf, len);
317 * @v2: another #GString
319 * Compares two strings for equality, returning %TRUE if they are equal.
320 * For use with #GHashTable.
322 * Returns: %TRUE if the strings are the same length and contain the
326 g_string_equal (const GString *v,
330 GString *string1 = (GString *) v;
331 GString *string2 = (GString *) v2;
332 gsize i = string1->len;
334 if (i != string2->len)
352 * @str: a string to hash
354 * Creates a hash code for @str; for use with #GHashTable.
356 * Returns: hash code for @str
359 g_string_hash (const GString *str)
361 const gchar *p = str->str;
365 /* 31 bit hash function */
368 h = (h << 5) - h + *p;
377 * @string: the destination #GString. Its current contents
379 * @rval: the string to copy into @string
381 * Copies the bytes from a string into a #GString,
382 * destroying any previous contents. It is rather like
383 * the standard strcpy() function, except that you do not
384 * have to worry about having enough space to copy the string.
386 * Returns: (transfer none): @string
389 g_string_assign (GString *string,
392 g_return_val_if_fail (string != NULL, NULL);
393 g_return_val_if_fail (rval != NULL, string);
395 /* Make sure assigning to itself doesn't corrupt the string. */
396 if (string->str != rval)
398 /* Assigning from substring should be ok, since
399 * g_string_truncate() does not reallocate.
401 g_string_truncate (string, 0);
402 g_string_append (string, rval);
410 * @string: a #GString
411 * @len: the new size of @string
413 * Cuts off the end of the GString, leaving the first @len bytes.
415 * Returns: (transfer none): @string
418 (g_string_truncate) (GString *string,
421 g_return_val_if_fail (string != NULL, NULL);
423 string->len = MIN (len, string->len);
424 string->str[string->len] = 0;
431 * @string: a #GString
432 * @len: the new length
434 * Sets the length of a #GString. If the length is less than
435 * the current length, the string will be truncated. If the
436 * length is greater than the current length, the contents
437 * of the newly added area are undefined. (However, as
438 * always, string->str[string->len] will be a nul byte.)
440 * Returns: (transfer none): @string
443 g_string_set_size (GString *string,
446 g_return_val_if_fail (string != NULL, NULL);
448 if (len >= string->allocated_len)
449 g_string_maybe_expand (string, len - string->len);
452 string->str[len] = 0;
458 * g_string_insert_len:
459 * @string: a #GString
460 * @pos: position in @string where insertion should
461 * happen, or -1 for at the end
462 * @val: bytes to insert
463 * @len: number of bytes of @val to insert, or -1 for all of @val
465 * Inserts @len bytes of @val into @string at @pos.
467 * If @len is positive, @val may contain embedded nuls and need
468 * not be nul-terminated. It is the caller's responsibility to
469 * ensure that @val has at least @len addressable bytes.
471 * If @len is negative, @val must be nul-terminated and @len
472 * is considered to request the entire string length.
474 * If @pos is -1, bytes are inserted at the end of the string.
476 * Returns: (transfer none): @string
479 g_string_insert_len (GString *string,
484 gsize len_unsigned, pos_unsigned;
486 g_return_val_if_fail (string != NULL, NULL);
487 g_return_val_if_fail (len == 0 || val != NULL, string);
497 pos_unsigned = string->len;
501 g_return_val_if_fail (pos_unsigned <= string->len, string);
504 /* Check whether val represents a substring of string.
505 * This test probably violates chapter and verse of the C standards,
506 * since ">=" and "<=" are only valid when val really is a substring.
507 * In practice, it will work on modern archs.
509 if (G_UNLIKELY (val >= string->str && val <= string->str + string->len))
511 gsize offset = val - string->str;
514 g_string_maybe_expand (string, len_unsigned);
515 val = string->str + offset;
516 /* At this point, val is valid again. */
518 /* Open up space where we are going to insert. */
519 if (pos_unsigned < string->len)
520 memmove (string->str + pos_unsigned + len_unsigned,
521 string->str + pos_unsigned, string->len - pos_unsigned);
523 /* Move the source part before the gap, if any. */
524 if (offset < pos_unsigned)
526 precount = MIN (len_unsigned, pos_unsigned - offset);
527 memcpy (string->str + pos_unsigned, val, precount);
530 /* Move the source part after the gap, if any. */
531 if (len_unsigned > precount)
532 memcpy (string->str + pos_unsigned + precount,
533 val + /* Already moved: */ precount +
534 /* Space opened up: */ len_unsigned,
535 len_unsigned - precount);
539 g_string_maybe_expand (string, len_unsigned);
541 /* If we aren't appending at the end, move a hunk
542 * of the old string to the end, opening up space
544 if (pos_unsigned < string->len)
545 memmove (string->str + pos_unsigned + len_unsigned,
546 string->str + pos_unsigned, string->len - pos_unsigned);
548 /* insert the new string */
549 if (len_unsigned == 1)
550 string->str[pos_unsigned] = *val;
552 memcpy (string->str + pos_unsigned, val, len_unsigned);
555 string->len += len_unsigned;
557 string->str[string->len] = 0;
563 * g_string_append_uri_escaped:
564 * @string: a #GString
565 * @unescaped: a string
566 * @reserved_chars_allowed: a string of reserved characters allowed
567 * to be used, or %NULL
568 * @allow_utf8: set %TRUE if the escaped string may include UTF8 characters
570 * Appends @unescaped to @string, escaping any characters that
571 * are reserved in URIs using URI-style escape sequences.
573 * Returns: (transfer none): @string
578 g_string_append_uri_escaped (GString *string,
579 const gchar *unescaped,
580 const gchar *reserved_chars_allowed,
583 _uri_encoder (string, (const guchar *) unescaped, strlen (unescaped),
584 reserved_chars_allowed, allow_utf8);
590 * @string: a #GString
591 * @val: the string to append onto the end of @string
593 * Adds a string onto the end of a #GString, expanding
596 * Returns: (transfer none): @string
599 (g_string_append) (GString *string,
602 return g_string_insert_len (string, -1, val, -1);
606 * g_string_append_len:
607 * @string: a #GString
608 * @val: bytes to append
609 * @len: number of bytes of @val to use, or -1 for all of @val
611 * Appends @len bytes of @val to @string.
613 * If @len is positive, @val may contain embedded nuls and need
614 * not be nul-terminated. It is the caller's responsibility to
615 * ensure that @val has at least @len addressable bytes.
617 * If @len is negative, @val must be nul-terminated and @len
618 * is considered to request the entire string length. This
619 * makes g_string_append_len() equivalent to g_string_append().
621 * Returns: (transfer none): @string
624 (g_string_append_len) (GString *string,
628 return g_string_insert_len (string, -1, val, len);
633 * @string: a #GString
634 * @c: the byte to append onto the end of @string
636 * Adds a byte onto the end of a #GString, expanding
639 * Returns: (transfer none): @string
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 * Returns: (transfer none): @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.
677 * Returns: (transfer none): @string
680 g_string_prepend (GString *string,
683 return g_string_insert_len (string, 0, val, -1);
687 * g_string_prepend_len:
688 * @string: a #GString
689 * @val: bytes to prepend
690 * @len: number of bytes in @val to prepend, or -1 for all of @val
692 * Prepends @len bytes of @val to @string.
694 * If @len is positive, @val may contain embedded nuls and need
695 * not be nul-terminated. It is the caller's responsibility to
696 * ensure that @val has at least @len addressable bytes.
698 * If @len is negative, @val must be nul-terminated and @len
699 * is considered to request the entire string length. This
700 * makes g_string_prepend_len() equivalent to g_string_prepend().
702 * Returns: (transfer none): @string
705 g_string_prepend_len (GString *string,
709 return g_string_insert_len (string, 0, val, len);
713 * g_string_prepend_c:
714 * @string: a #GString
715 * @c: the byte to prepend on the start of the #GString
717 * Adds a byte onto the start of a #GString,
718 * expanding it if necessary.
720 * Returns: (transfer none): @string
723 g_string_prepend_c (GString *string,
726 g_return_val_if_fail (string != NULL, NULL);
728 return g_string_insert_c (string, 0, c);
732 * g_string_prepend_unichar:
733 * @string: a #GString
734 * @wc: a Unicode character
736 * Converts a Unicode character into UTF-8, and prepends it
739 * Returns: (transfer none): @string
742 g_string_prepend_unichar (GString *string,
745 g_return_val_if_fail (string != NULL, NULL);
747 return g_string_insert_unichar (string, 0, wc);
752 * @string: a #GString
753 * @pos: the position to insert the copy of the string
754 * @val: the string to insert
756 * Inserts a copy of a string into a #GString,
757 * expanding it if necessary.
759 * Returns: (transfer none): @string
762 g_string_insert (GString *string,
766 return g_string_insert_len (string, pos, val, -1);
771 * @string: a #GString
772 * @pos: the position to insert the byte
773 * @c: the byte to insert
775 * Inserts a byte into a #GString, expanding it if necessary.
777 * Returns: (transfer none): @string
780 g_string_insert_c (GString *string,
786 g_return_val_if_fail (string != NULL, NULL);
788 g_string_maybe_expand (string, 1);
793 g_return_val_if_fail ((gsize) pos <= string->len, string);
796 /* If not just an append, move the old stuff */
797 if (pos_unsigned < string->len)
798 memmove (string->str + pos_unsigned + 1,
799 string->str + pos_unsigned, string->len - pos_unsigned);
801 string->str[pos_unsigned] = c;
805 string->str[string->len] = 0;
811 * g_string_insert_unichar:
812 * @string: a #GString
813 * @pos: the position at which to insert character, or -1
814 * to append at the end of the string
815 * @wc: a Unicode character
817 * Converts a Unicode character into UTF-8, and insert it
818 * into the string at the given position.
820 * Returns: (transfer none): @string
823 g_string_insert_unichar (GString *string,
827 gint charlen, first, i;
830 g_return_val_if_fail (string != NULL, NULL);
832 /* Code copied from g_unichar_to_utf() */
843 else if (wc < 0x10000)
848 else if (wc < 0x200000)
853 else if (wc < 0x4000000)
863 /* End of copied code */
865 g_string_maybe_expand (string, charlen);
870 g_return_val_if_fail ((gsize) pos <= string->len, string);
872 /* If not just an append, move the old stuff */
873 if ((gsize) pos < string->len)
874 memmove (string->str + pos + charlen, string->str + pos, string->len - pos);
876 dest = string->str + pos;
877 /* Code copied from g_unichar_to_utf() */
878 for (i = charlen - 1; i > 0; --i)
880 dest[i] = (wc & 0x3f) | 0x80;
883 dest[0] = wc | first;
884 /* End of copied code */
886 string->len += charlen;
888 string->str[string->len] = 0;
894 * g_string_overwrite:
895 * @string: a #GString
896 * @pos: the position at which to start overwriting
897 * @val: the string that will overwrite the @string starting at @pos
899 * Overwrites part of a string, lengthening it if necessary.
901 * Returns: (transfer none): @string
906 g_string_overwrite (GString *string,
910 g_return_val_if_fail (val != NULL, string);
911 return g_string_overwrite_len (string, pos, val, strlen (val));
915 * g_string_overwrite_len:
916 * @string: a #GString
917 * @pos: the position at which to start overwriting
918 * @val: the string that will overwrite the @string starting at @pos
919 * @len: the number of bytes to write from @val
921 * Overwrites part of a string, lengthening it if necessary.
922 * This function will work with embedded nuls.
924 * Returns: (transfer none): @string
929 g_string_overwrite_len (GString *string,
936 g_return_val_if_fail (string != NULL, NULL);
941 g_return_val_if_fail (val != NULL, string);
942 g_return_val_if_fail (pos <= string->len, string);
949 if (end > string->len)
950 g_string_maybe_expand (string, end - string->len);
952 memcpy (string->str + pos, val, len);
954 if (end > string->len)
956 string->str[end] = '\0';
965 * @string: a #GString
966 * @pos: the position of the content to remove
967 * @len: the number of bytes to remove, or -1 to remove all
970 * Removes @len bytes from a #GString, starting at position @pos.
971 * The rest of the #GString is shifted down to fill the gap.
973 * Returns: (transfer none): @string
976 g_string_erase (GString *string,
980 gsize len_unsigned, pos_unsigned;
982 g_return_val_if_fail (string != NULL, NULL);
983 g_return_val_if_fail (pos >= 0, string);
986 g_return_val_if_fail (pos_unsigned <= string->len, string);
989 len_unsigned = string->len - pos_unsigned;
993 g_return_val_if_fail (pos_unsigned + len_unsigned <= string->len, string);
995 if (pos_unsigned + len_unsigned < string->len)
996 memmove (string->str + pos_unsigned,
997 string->str + pos_unsigned + len_unsigned,
998 string->len - (pos_unsigned + len_unsigned));
1001 string->len -= len_unsigned;
1003 string->str[string->len] = 0;
1010 * @string: a #GString
1011 * @find: the string to find in @string
1012 * @replace: the string to insert in place of @find
1013 * @limit: the maximum instances of @find to replace with @replace, or `0` for
1016 * Replaces the string @find with the string @replace in a #GString up to
1017 * @limit times. If the number of instances of @find in the #GString is
1018 * less than @limit, all instances are replaced. If @limit is `0`,
1019 * all instances of @find are replaced.
1021 * If @find is the empty string, since versions 2.69.1 and 2.68.4 the
1022 * replacement will be inserted no more than once per possible position
1023 * (beginning of string, end of string and between characters). This did
1024 * not work correctly in earlier versions.
1026 * Returns: the number of find and replace operations performed.
1031 g_string_replace (GString *string,
1033 const gchar *replace,
1036 gsize f_len, r_len, pos;
1040 g_return_val_if_fail (string != NULL, 0);
1041 g_return_val_if_fail (find != NULL, 0);
1042 g_return_val_if_fail (replace != NULL, 0);
1044 f_len = strlen (find);
1045 r_len = strlen (replace);
1048 while ((next = strstr (cur, find)) != NULL)
1050 pos = next - string->str;
1051 g_string_erase (string, pos, f_len);
1052 g_string_insert (string, pos, replace);
1053 cur = string->str + pos + r_len;
1055 /* Only match the empty string once at any given position, to
1056 * avoid infinite loops */
1072 * g_string_ascii_down:
1073 * @string: a GString
1075 * Converts all uppercase ASCII letters to lowercase ASCII letters.
1077 * Returns: (transfer none): passed-in @string pointer, with all the
1078 * uppercase characters converted to lowercase in place,
1079 * with semantics that exactly match g_ascii_tolower().
1082 g_string_ascii_down (GString *string)
1087 g_return_val_if_fail (string != NULL, NULL);
1094 *s = g_ascii_tolower (*s);
1103 * g_string_ascii_up:
1104 * @string: a GString
1106 * Converts all lowercase ASCII letters to uppercase ASCII letters.
1108 * Returns: (transfer none): passed-in @string pointer, with all the
1109 * lowercase characters converted to uppercase in place,
1110 * with semantics that exactly match g_ascii_toupper().
1113 g_string_ascii_up (GString *string)
1118 g_return_val_if_fail (string != NULL, NULL);
1125 *s = g_ascii_toupper (*s);
1135 * @string: a #GString
1137 * Converts a #GString to lowercase.
1139 * Returns: (transfer none): the #GString
1141 * Deprecated:2.2: This function uses the locale-specific
1142 * tolower() function, which is almost never the right thing.
1143 * Use g_string_ascii_down() or g_utf8_strdown() instead.
1146 g_string_down (GString *string)
1151 g_return_val_if_fail (string != NULL, NULL);
1154 s = (guchar *) string->str;
1169 * @string: a #GString
1171 * Converts a #GString to uppercase.
1173 * Returns: (transfer none): @string
1175 * Deprecated:2.2: This function uses the locale-specific
1176 * toupper() function, which is almost never the right thing.
1177 * Use g_string_ascii_up() or g_utf8_strup() instead.
1180 g_string_up (GString *string)
1185 g_return_val_if_fail (string != NULL, NULL);
1188 s = (guchar *) string->str;
1202 * g_string_append_vprintf:
1203 * @string: a #GString
1204 * @format: (not nullable): the string format. See the printf() documentation
1205 * @args: the list of arguments to insert in the output
1207 * Appends a formatted string onto the end of a #GString.
1208 * This function is similar to g_string_append_printf()
1209 * except that the arguments to the format string are passed
1215 g_string_append_vprintf (GString *string,
1216 const gchar *format,
1222 g_return_if_fail (string != NULL);
1223 g_return_if_fail (format != NULL);
1225 len = g_vasprintf (&buf, format, args);
1229 g_string_maybe_expand (string, len);
1230 memcpy (string->str + string->len, buf, len + 1);
1238 * @string: a #GString
1239 * @format: (not nullable): the string format. See the printf() documentation
1240 * @args: the parameters to insert into the format string
1242 * Writes a formatted string into a #GString.
1243 * This function is similar to g_string_printf() except that
1244 * the arguments to the format string are passed as a va_list.
1249 g_string_vprintf (GString *string,
1250 const gchar *format,
1253 g_string_truncate (string, 0);
1254 g_string_append_vprintf (string, format, args);
1259 * @string: a #GString
1260 * @format: the string format. See the sprintf() documentation
1261 * @...: the parameters to insert into the format string
1263 * Writes a formatted string into a #GString.
1264 * This is similar to the standard sprintf() function,
1265 * except that the #GString buffer automatically expands
1266 * to contain the results. The previous contents of the
1267 * #GString are destroyed.
1269 * Deprecated: This function has been renamed to g_string_printf().
1274 * @string: a #GString
1275 * @format: the string format. See the printf() documentation
1276 * @...: the parameters to insert into the format string
1278 * Writes a formatted string into a #GString.
1279 * This is similar to the standard sprintf() function,
1280 * except that the #GString buffer automatically expands
1281 * to contain the results. The previous contents of the
1282 * #GString are destroyed.
1285 g_string_printf (GString *string,
1286 const gchar *format,
1291 g_string_truncate (string, 0);
1293 va_start (args, format);
1294 g_string_append_vprintf (string, format, args);
1299 * g_string_sprintfa:
1300 * @string: a #GString
1301 * @format: the string format. See the sprintf() documentation
1302 * @...: the parameters to insert into the format string
1304 * Appends a formatted string onto the end of a #GString.
1305 * This function is similar to g_string_sprintf() except that
1306 * the text is appended to the #GString.
1308 * Deprecated: This function has been renamed to g_string_append_printf()
1312 * g_string_append_printf:
1313 * @string: a #GString
1314 * @format: the string format. See the printf() documentation
1315 * @...: the parameters to insert into the format string
1317 * Appends a formatted string onto the end of a #GString.
1318 * This function is similar to g_string_printf() except
1319 * that the text is appended to the #GString.
1322 g_string_append_printf (GString *string,
1323 const gchar *format,
1328 va_start (args, format);
1329 g_string_append_vprintf (string, format, args);