GSettings docs: clarify what is a good path
[glib.git] / glib / gstring.c
blob9fac86cbd7822ebfbbfa25b3b482333f75c06b90
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/.
28 * MT safe
31 #include "config.h"
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #include <stdarg.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <ctype.h>
42 #include "gstring.h"
44 #include "gprintf.h"
47 /**
48 * SECTION:strings
49 * @title: Strings
50 * @short_description: text buffers which grow automatically
51 * as text is added
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.
61 /**
62 * GString:
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)
77 static inline gsize
78 nearest_power (gsize base, gsize num)
80 if (num > MY_MAXSIZE / 2)
82 return MY_MAXSIZE;
84 else
86 gsize n = base;
88 while (n < num)
89 n <<= 1;
91 return n;
95 static void
96 g_string_maybe_expand (GString *string,
97 gsize len)
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
109 * hold the string
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
114 * too often.
116 * Returns: the new #GString
118 GString *
119 g_string_sized_new (gsize dfl_size)
121 GString *string = g_slice_new (GString);
123 string->allocated_len = 0;
124 string->len = 0;
125 string->str = NULL;
127 g_string_maybe_expand (string, MAX (dfl_size, 2));
128 string->str[0] = 0;
130 return string;
134 * g_string_new:
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
141 GString *
142 g_string_new (const gchar *init)
144 GString *string;
146 if (init == NULL || *init == '\0')
147 string = g_string_sized_new (2);
148 else
150 gint len;
152 len = strlen (init);
153 string = g_string_sized_new (len + 2);
155 g_string_append_len (string, init, len);
158 return string;
162 * g_string_new_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
172 * bytes.
174 * Returns: a new #GString
176 GString *
177 g_string_new_len (const gchar *init,
178 gssize len)
180 GString *string;
182 if (len < 0)
183 return g_string_new (init);
184 else
186 string = g_string_sized_new (len);
188 if (init)
189 g_string_append_len (string, init, len);
191 return string;
196 * g_string_free:
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)
208 gchar *
209 g_string_free (GString *string,
210 gboolean free_segment)
212 gchar *segment;
214 g_return_val_if_fail (string != NULL, NULL);
216 if (free_segment)
218 g_free (string->str);
219 segment = NULL;
221 else
222 segment = string->str;
224 g_slice_free (GString, string);
226 return segment;
230 * g_string_equal:
231 * @v: a #GString
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
238 * same bytes
240 gboolean
241 g_string_equal (const GString *v,
242 const GString *v2)
244 gchar *p, *q;
245 GString *string1 = (GString *) v;
246 GString *string2 = (GString *) v2;
247 gsize i = string1->len;
249 if (i != string2->len)
250 return FALSE;
252 p = string1->str;
253 q = string2->str;
254 while (i)
256 if (*p != *q)
257 return FALSE;
258 p++;
259 q++;
260 i--;
262 return TRUE;
266 * g_string_hash:
267 * @str: a string to hash
269 * Creates a hash code for @str; for use with #GHashTable.
271 * Returns: hash code for @str
273 guint
274 g_string_hash (const GString *str)
276 const gchar *p = str->str;
277 gsize n = str->len;
278 guint h = 0;
280 /* 31 bit hash function */
281 while (n--)
283 h = (h << 5) - h + *p;
284 p++;
287 return h;
291 * g_string_assign:
292 * @string: the destination #GString. Its current contents
293 * are destroyed.
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.
301 * Returns: @string
303 GString *
304 g_string_assign (GString *string,
305 const gchar *rval)
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);
320 return string;
324 * g_string_truncate:
325 * @string: a #GString
326 * @len: the new size of @string
328 * Cuts off the end of the GString, leaving the first @len bytes.
330 * Returns: @string
332 GString *
333 g_string_truncate (GString *string,
334 gsize len)
336 g_return_val_if_fail (string != NULL, NULL);
338 string->len = MIN (len, string->len);
339 string->str[string->len] = 0;
341 return string;
345 * g_string_set_size:
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
357 GString *
358 g_string_set_size (GString *string,
359 gsize len)
361 g_return_val_if_fail (string != NULL, NULL);
363 if (len >= string->allocated_len)
364 g_string_maybe_expand (string, len - string->len);
366 string->len = len;
367 string->str[len] = 0;
369 return string;
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.
389 * Returns: @string
391 GString *
392 g_string_insert_len (GString *string,
393 gssize pos,
394 const gchar *val,
395 gssize len)
397 g_return_val_if_fail (string != NULL, NULL);
398 g_return_val_if_fail (len == 0 || val != NULL, string);
400 if (len == 0)
401 return string;
403 if (len < 0)
404 len = strlen (val);
406 if (pos < 0)
407 pos = string->len;
408 else
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;
419 gsize precount = 0;
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. */
430 if (offset < pos)
432 precount = MIN (len, pos - offset);
433 memcpy (string->str + pos, val, precount);
436 /* Move the source part after the gap, if any. */
437 if (len > precount)
438 memcpy (string->str + pos + precount,
439 val + /* Already moved: */ precount + /* Space opened up: */ len,
440 len - precount);
442 else
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 */
453 if (len == 1)
454 string->str[pos] = *val;
455 else
456 memcpy (string->str + pos, val, len);
459 string->len += len;
461 string->str[string->len] = 0;
463 return string;
466 #define SUB_DELIM_CHARS "!$&'()*+,;="
468 static gboolean
469 is_valid (char c,
470 const char *reserved_chars_allowed)
472 if (g_ascii_isalnum (c) ||
473 c == '-' ||
474 c == '.' ||
475 c == '_' ||
476 c == '~')
477 return TRUE;
479 if (reserved_chars_allowed &&
480 strchr (reserved_chars_allowed, c) != NULL)
481 return TRUE;
483 return FALSE;
486 static gboolean
487 gunichar_ok (gunichar c)
489 return
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.
505 * Returns: @string
507 * Since: 2.16
509 GString *
510 g_string_append_uri_escaped (GString *string,
511 const gchar *unescaped,
512 const gchar *reserved_chars_allowed,
513 gboolean allow_utf8)
515 unsigned char c;
516 const gchar *end;
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);
531 unescaped += len;
533 else if (is_valid (c, reserved_chars_allowed))
535 g_string_append_c (string, c);
536 unescaped++;
538 else
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]);
543 unescaped++;
547 return string;
551 * g_string_append:
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
556 * it if necessary.
558 * Returns: @string
560 GString *
561 g_string_append (GString *string,
562 const gchar *val)
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
578 * be nul-terminated.
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.
584 * Returns: @string
586 GString *
587 g_string_append_len (GString *string,
588 const gchar *val,
589 gssize len)
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);
598 * g_string_append_c:
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
603 * it if necessary.
605 * Returns: @string
607 #undef g_string_append_c
608 GString *
609 g_string_append_c (GString *string,
610 gchar c)
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
623 * to the string.
625 * Return value: @string
627 GString *
628 g_string_append_unichar (GString *string,
629 gunichar wc)
631 g_return_val_if_fail (string != NULL, NULL);
633 return g_string_insert_unichar (string, -1, wc);
637 * g_string_prepend:
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.
644 * Returns: @string
646 GString *
647 g_string_prepend (GString *string,
648 const gchar *val)
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.
670 * Returns: @string
672 GString *
673 g_string_prepend_len (GString *string,
674 const gchar *val,
675 gssize len)
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.
691 * Returns: @string
693 GString *
694 g_string_prepend_c (GString *string,
695 gchar c)
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
708 * to the string.
710 * Return value: @string
712 GString *
713 g_string_prepend_unichar (GString *string,
714 gunichar wc)
716 g_return_val_if_fail (string != NULL, NULL);
718 return g_string_insert_unichar (string, 0, wc);
722 * g_string_insert:
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.
730 * Returns: @string
732 GString *
733 g_string_insert (GString *string,
734 gssize pos,
735 const gchar *val)
737 g_return_val_if_fail (string != NULL, NULL);
738 g_return_val_if_fail (val != NULL, string);
740 if (pos >= 0)
741 g_return_val_if_fail (pos <= string->len, string);
743 return g_string_insert_len (string, pos, val, -1);
747 * g_string_insert_c:
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.
754 * Returns: @string
756 GString *
757 g_string_insert_c (GString *string,
758 gssize pos,
759 gchar c)
761 g_return_val_if_fail (string != NULL, NULL);
763 g_string_maybe_expand (string, 1);
765 if (pos < 0)
766 pos = string->len;
767 else
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;
776 string->len += 1;
778 string->str[string->len] = 0;
780 return string;
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
795 GString *
796 g_string_insert_unichar (GString *string,
797 gssize pos,
798 gunichar wc)
800 gint charlen, first, i;
801 gchar *dest;
803 g_return_val_if_fail (string != NULL, NULL);
805 /* Code copied from g_unichar_to_utf() */
806 if (wc < 0x80)
808 first = 0;
809 charlen = 1;
811 else if (wc < 0x800)
813 first = 0xc0;
814 charlen = 2;
816 else if (wc < 0x10000)
818 first = 0xe0;
819 charlen = 3;
821 else if (wc < 0x200000)
823 first = 0xf0;
824 charlen = 4;
826 else if (wc < 0x4000000)
828 first = 0xf8;
829 charlen = 5;
831 else
833 first = 0xfc;
834 charlen = 6;
836 /* End of copied code */
838 g_string_maybe_expand (string, charlen);
840 if (pos < 0)
841 pos = string->len;
842 else
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;
854 wc >>= 6;
856 dest[0] = wc | first;
857 /* End of copied code */
859 string->len += charlen;
861 string->str[string->len] = 0;
863 return string;
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
876 * Since: 2.14
878 GString *
879 g_string_overwrite (GString *string,
880 gsize pos,
881 const gchar *val)
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
899 * Since: 2.14
901 GString *
902 g_string_overwrite_len (GString *string,
903 gsize pos,
904 const gchar *val,
905 gssize len)
907 gsize end;
909 g_return_val_if_fail (string != NULL, NULL);
911 if (!len)
912 return string;
914 g_return_val_if_fail (val != NULL, string);
915 g_return_val_if_fail (pos <= string->len, string);
917 if (len < 0)
918 len = strlen (val);
920 end = pos + len;
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';
930 string->len = end;
933 return string;
937 * g_string_erase:
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
941 * following bytes
943 * Removes @len bytes from a #GString, starting at position @pos.
944 * The rest of the #GString is shifted down to fill the gap.
946 * Returns: @string
948 GString *
949 g_string_erase (GString *string,
950 gssize pos,
951 gssize len)
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);
957 if (len < 0)
958 len = string->len - pos;
959 else
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));
967 string->len -= len;
969 string->str[string->len] = 0;
971 return string;
975 * g_string_ascii_down:
976 * @string: a GString
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().
984 GString *
985 g_string_ascii_down (GString *string)
987 gchar *s;
988 gint n;
990 g_return_val_if_fail (string != NULL, NULL);
992 n = string->len;
993 s = string->str;
995 while (n)
997 *s = g_ascii_tolower (*s);
998 s++;
999 n--;
1002 return string;
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().
1015 GString *
1016 g_string_ascii_up (GString *string)
1018 gchar *s;
1019 gint n;
1021 g_return_val_if_fail (string != NULL, NULL);
1023 n = string->len;
1024 s = string->str;
1026 while (n)
1028 *s = g_ascii_toupper (*s);
1029 s++;
1030 n--;
1033 return string;
1037 * g_string_down:
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.
1048 GString *
1049 g_string_down (GString *string)
1051 guchar *s;
1052 glong n;
1054 g_return_val_if_fail (string != NULL, NULL);
1056 n = string->len;
1057 s = (guchar *) string->str;
1059 while (n)
1061 if (isupper (*s))
1062 *s = tolower (*s);
1063 s++;
1064 n--;
1067 return string;
1071 * g_string_up:
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.
1082 GString *
1083 g_string_up (GString *string)
1085 guchar *s;
1086 glong n;
1088 g_return_val_if_fail (string != NULL, NULL);
1090 n = string->len;
1091 s = (guchar *) string->str;
1093 while (n)
1095 if (islower (*s))
1096 *s = toupper (*s);
1097 s++;
1098 n--;
1101 return string;
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
1113 * as a va_list.
1115 * Since: 2.14
1117 void
1118 g_string_append_vprintf (GString *string,
1119 const gchar *format,
1120 va_list args)
1122 gchar *buf;
1123 gint len;
1125 g_return_if_fail (string != NULL);
1126 g_return_if_fail (format != NULL);
1128 len = g_vasprintf (&buf, format, args);
1130 if (len >= 0)
1132 g_string_maybe_expand (string, len);
1133 memcpy (string->str + string->len, buf, len + 1);
1134 string->len += len;
1135 g_free (buf);
1140 * g_string_vprintf:
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.
1149 * Since: 2.14
1151 void
1152 g_string_vprintf (GString *string,
1153 const gchar *format,
1154 va_list args)
1156 g_string_truncate (string, 0);
1157 g_string_append_vprintf (string, format, args);
1161 * g_string_sprintf:
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().
1176 * 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.
1187 void
1188 g_string_printf (GString *string,
1189 const gchar *format,
1190 ...)
1192 va_list args;
1194 g_string_truncate (string, 0);
1196 va_start (args, format);
1197 g_string_append_vprintf (string, format, args);
1198 va_end (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.
1224 void
1225 g_string_append_printf (GString *string,
1226 const gchar *format,
1227 ...)
1229 va_list args;
1231 va_start (args, format);
1232 g_string_append_vprintf (string, format, args);
1233 va_end (args);