application: Use printerr for runtime errors
[glib.git] / glib / gstring.c
blob782fd4a00a0536d58f22c51796eac709e6ffcd5d
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 #include <stdarg.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <ctype.h>
39 #include "gstring.h"
41 #include "gprintf.h"
44 /**
45 * SECTION:strings
46 * @title: Strings
47 * @short_description: text buffers which grow automatically
48 * as text is added
50 * A #GString is an object that handles the memory management of a C
51 * string for you. The emphasis of #GString is on text, typically
52 * UTF-8. Crucially, the "str" member of a #GString is guaranteed to
53 * have a trailing nul character, and it is therefore always safe to
54 * call functions such as strchr() or g_strdup() on it.
56 * However, a #GString can also hold arbitrary binary data, because it
57 * has a "len" member, which includes any possible embedded nul
58 * characters in the data. Conceptually then, #GString is like a
59 * #GByteArray with the addition of many convenience methods for text,
60 * and a guaranteed nul terminator.
63 /**
64 * GString:
65 * @str: points to the character data. It may move as text is added.
66 * The @str field is null-terminated and so
67 * can be used as an ordinary C string.
68 * @len: contains the length of the string, not including the
69 * terminating nul byte.
70 * @allocated_len: the number of bytes that can be stored in the
71 * string before it needs to be reallocated. May be larger than @len.
73 * The GString struct contains the public fields of a GString.
77 #define MY_MAXSIZE ((gsize)-1)
79 static inline gsize
80 nearest_power (gsize base, gsize num)
82 if (num > MY_MAXSIZE / 2)
84 return MY_MAXSIZE;
86 else
88 gsize n = base;
90 while (n < num)
91 n <<= 1;
93 return n;
97 static void
98 g_string_maybe_expand (GString *string,
99 gsize len)
101 if (string->len + len >= string->allocated_len)
103 string->allocated_len = nearest_power (1, string->len + len + 1);
104 string->str = g_realloc (string->str, string->allocated_len);
109 * g_string_sized_new:
110 * @dfl_size: the default size of the space allocated to
111 * hold the string
113 * Creates a new #GString, with enough space for @dfl_size
114 * bytes. This is useful if you are going to add a lot of
115 * text to the string and don't want it to be reallocated
116 * too often.
118 * Returns: the new #GString
120 GString *
121 g_string_sized_new (gsize dfl_size)
123 GString *string = g_slice_new (GString);
125 string->allocated_len = 0;
126 string->len = 0;
127 string->str = NULL;
129 g_string_maybe_expand (string, MAX (dfl_size, 2));
130 string->str[0] = 0;
132 return string;
136 * g_string_new:
137 * @init: the initial text to copy into the string
139 * Creates a new #GString, initialized with the given string.
141 * Returns: the new #GString
143 GString *
144 g_string_new (const gchar *init)
146 GString *string;
148 if (init == NULL || *init == '\0')
149 string = g_string_sized_new (2);
150 else
152 gint len;
154 len = strlen (init);
155 string = g_string_sized_new (len + 2);
157 g_string_append_len (string, init, len);
160 return string;
164 * g_string_new_len:
165 * @init: initial contents of the string
166 * @len: length of @init to use
168 * Creates a new #GString with @len bytes of the @init buffer.
169 * Because a length is provided, @init need not be nul-terminated,
170 * and can contain embedded nul bytes.
172 * Since this function does not stop at nul bytes, it is the caller's
173 * responsibility to ensure that @init has at least @len addressable
174 * bytes.
176 * Returns: a new #GString
178 GString *
179 g_string_new_len (const gchar *init,
180 gssize len)
182 GString *string;
184 if (len < 0)
185 return g_string_new (init);
186 else
188 string = g_string_sized_new (len);
190 if (init)
191 g_string_append_len (string, init, len);
193 return string;
198 * g_string_free:
199 * @string: a #GString
200 * @free_segment: if %TRUE, the actual character data is freed as well
202 * Frees the memory allocated for the #GString.
203 * If @free_segment is %TRUE it also frees the character data. If
204 * it's %FALSE, the caller gains ownership of the buffer and must
205 * free it after use with g_free().
207 * Returns: the character data of @string
208 * (i.e. %NULL if @free_segment is %TRUE)
210 gchar *
211 g_string_free (GString *string,
212 gboolean free_segment)
214 gchar *segment;
216 g_return_val_if_fail (string != NULL, NULL);
218 if (free_segment)
220 g_free (string->str);
221 segment = NULL;
223 else
224 segment = string->str;
226 g_slice_free (GString, string);
228 return segment;
232 * g_string_free_to_bytes:
233 * @string: (transfer full): a #GString
235 * Transfers ownership of the contents of @string to a newly allocated
236 * #GBytes. The #GString structure itself is deallocated, and it is
237 * therefore invalid to use @string after invoking this function.
239 * Note that while #GString ensures that its buffer always has a
240 * trailing nul character (not reflected in its "len"), the returned
241 * #GBytes does not include this extra nul; i.e. it has length exactly
242 * equal to the "len" member.
244 * Returns: A newly allocated #GBytes containing contents of @string; @string itself is freed
245 * Since: 2.34
247 GBytes*
248 g_string_free_to_bytes (GString *string)
250 gsize len;
251 gchar *buf;
253 g_return_val_if_fail (string != NULL, NULL);
255 len = string->len;
257 buf = g_string_free (string, FALSE);
259 return g_bytes_new_take (buf, len);
263 * g_string_equal:
264 * @v: a #GString
265 * @v2: another #GString
267 * Compares two strings for equality, returning %TRUE if they are equal.
268 * For use with #GHashTable.
270 * Returns: %TRUE if the strings are the same length and contain the
271 * same bytes
273 gboolean
274 g_string_equal (const GString *v,
275 const GString *v2)
277 gchar *p, *q;
278 GString *string1 = (GString *) v;
279 GString *string2 = (GString *) v2;
280 gsize i = string1->len;
282 if (i != string2->len)
283 return FALSE;
285 p = string1->str;
286 q = string2->str;
287 while (i)
289 if (*p != *q)
290 return FALSE;
291 p++;
292 q++;
293 i--;
295 return TRUE;
299 * g_string_hash:
300 * @str: a string to hash
302 * Creates a hash code for @str; for use with #GHashTable.
304 * Returns: hash code for @str
306 guint
307 g_string_hash (const GString *str)
309 const gchar *p = str->str;
310 gsize n = str->len;
311 guint h = 0;
313 /* 31 bit hash function */
314 while (n--)
316 h = (h << 5) - h + *p;
317 p++;
320 return h;
324 * g_string_assign:
325 * @string: the destination #GString. Its current contents
326 * are destroyed.
327 * @rval: the string to copy into @string
329 * Copies the bytes from a string into a #GString,
330 * destroying any previous contents. It is rather like
331 * the standard strcpy() function, except that you do not
332 * have to worry about having enough space to copy the string.
334 * Returns: @string
336 GString *
337 g_string_assign (GString *string,
338 const gchar *rval)
340 g_return_val_if_fail (string != NULL, NULL);
341 g_return_val_if_fail (rval != NULL, string);
343 /* Make sure assigning to itself doesn't corrupt the string. */
344 if (string->str != rval)
346 /* Assigning from substring should be ok, since
347 * g_string_truncate() does not reallocate.
349 g_string_truncate (string, 0);
350 g_string_append (string, rval);
353 return string;
357 * g_string_truncate:
358 * @string: a #GString
359 * @len: the new size of @string
361 * Cuts off the end of the GString, leaving the first @len bytes.
363 * Returns: @string
365 GString *
366 g_string_truncate (GString *string,
367 gsize len)
369 g_return_val_if_fail (string != NULL, NULL);
371 string->len = MIN (len, string->len);
372 string->str[string->len] = 0;
374 return string;
378 * g_string_set_size:
379 * @string: a #GString
380 * @len: the new length
382 * Sets the length of a #GString. If the length is less than
383 * the current length, the string will be truncated. If the
384 * length is greater than the current length, the contents
385 * of the newly added area are undefined. (However, as
386 * always, string->str[string->len] will be a nul byte.)
388 * Return value: @string
390 GString *
391 g_string_set_size (GString *string,
392 gsize len)
394 g_return_val_if_fail (string != NULL, NULL);
396 if (len >= string->allocated_len)
397 g_string_maybe_expand (string, len - string->len);
399 string->len = len;
400 string->str[len] = 0;
402 return string;
406 * g_string_insert_len:
407 * @string: a #GString
408 * @pos: position in @string where insertion should
409 * happen, or -1 for at the end
410 * @val: bytes to insert
411 * @len: number of bytes of @val to insert
413 * Inserts @len bytes of @val into @string at @pos.
414 * Because @len is provided, @val may contain embedded
415 * nuls and need not be nul-terminated. If @pos is -1,
416 * bytes are inserted at the end of the string.
418 * Since this function does not stop at nul bytes, it is
419 * the caller's responsibility to ensure that @val has at
420 * least @len addressable bytes.
422 * Returns: @string
424 GString *
425 g_string_insert_len (GString *string,
426 gssize pos,
427 const gchar *val,
428 gssize len)
430 g_return_val_if_fail (string != NULL, NULL);
431 g_return_val_if_fail (len == 0 || val != NULL, string);
433 if (len == 0)
434 return string;
436 if (len < 0)
437 len = strlen (val);
439 if (pos < 0)
440 pos = string->len;
441 else
442 g_return_val_if_fail (pos <= string->len, string);
444 /* Check whether val represents a substring of string.
445 * This test probably violates chapter and verse of the C standards,
446 * since ">=" and "<=" are only valid when val really is a substring.
447 * In practice, it will work on modern archs.
449 if (val >= string->str && val <= string->str + string->len)
451 gsize offset = val - string->str;
452 gsize precount = 0;
454 g_string_maybe_expand (string, len);
455 val = string->str + offset;
456 /* At this point, val is valid again. */
458 /* Open up space where we are going to insert. */
459 if (pos < string->len)
460 memmove (string->str + pos + len, string->str + pos, string->len - pos);
462 /* Move the source part before the gap, if any. */
463 if (offset < pos)
465 precount = MIN (len, pos - offset);
466 memcpy (string->str + pos, val, precount);
469 /* Move the source part after the gap, if any. */
470 if (len > precount)
471 memcpy (string->str + pos + precount,
472 val + /* Already moved: */ precount + /* Space opened up: */ len,
473 len - precount);
475 else
477 g_string_maybe_expand (string, len);
479 /* If we aren't appending at the end, move a hunk
480 * of the old string to the end, opening up space
482 if (pos < string->len)
483 memmove (string->str + pos + len, string->str + pos, string->len - pos);
485 /* insert the new string */
486 if (len == 1)
487 string->str[pos] = *val;
488 else
489 memcpy (string->str + pos, val, len);
492 string->len += len;
494 string->str[string->len] = 0;
496 return string;
499 #define SUB_DELIM_CHARS "!$&'()*+,;="
501 static gboolean
502 is_valid (char c,
503 const char *reserved_chars_allowed)
505 if (g_ascii_isalnum (c) ||
506 c == '-' ||
507 c == '.' ||
508 c == '_' ||
509 c == '~')
510 return TRUE;
512 if (reserved_chars_allowed &&
513 strchr (reserved_chars_allowed, c) != NULL)
514 return TRUE;
516 return FALSE;
519 static gboolean
520 gunichar_ok (gunichar c)
522 return
523 (c != (gunichar) -2) &&
524 (c != (gunichar) -1);
528 * g_string_append_uri_escaped:
529 * @string: a #GString
530 * @unescaped: a string
531 * @reserved_chars_allowed: a string of reserved characters allowed
532 * to be used, or %NULL
533 * @allow_utf8: set %TRUE if the escaped string may include UTF8 characters
535 * Appends @unescaped to @string, escaped any characters that
536 * are reserved in URIs using URI-style escape sequences.
538 * Returns: @string
540 * Since: 2.16
542 GString *
543 g_string_append_uri_escaped (GString *string,
544 const gchar *unescaped,
545 const gchar *reserved_chars_allowed,
546 gboolean allow_utf8)
548 unsigned char c;
549 const gchar *end;
550 static const gchar hex[16] = "0123456789ABCDEF";
552 g_return_val_if_fail (string != NULL, NULL);
553 g_return_val_if_fail (unescaped != NULL, NULL);
555 end = unescaped + strlen (unescaped);
557 while ((c = *unescaped) != 0)
559 if (c >= 0x80 && allow_utf8 &&
560 gunichar_ok (g_utf8_get_char_validated (unescaped, end - unescaped)))
562 int len = g_utf8_skip [c];
563 g_string_append_len (string, unescaped, len);
564 unescaped += len;
566 else if (is_valid (c, reserved_chars_allowed))
568 g_string_append_c (string, c);
569 unescaped++;
571 else
573 g_string_append_c (string, '%');
574 g_string_append_c (string, hex[((guchar)c) >> 4]);
575 g_string_append_c (string, hex[((guchar)c) & 0xf]);
576 unescaped++;
580 return string;
584 * g_string_append:
585 * @string: a #GString
586 * @val: the string to append onto the end of @string
588 * Adds a string onto the end of a #GString, expanding
589 * it if necessary.
591 * Returns: @string
593 GString *
594 g_string_append (GString *string,
595 const gchar *val)
597 g_return_val_if_fail (string != NULL, NULL);
598 g_return_val_if_fail (val != NULL, string);
600 return g_string_insert_len (string, -1, val, -1);
604 * g_string_append_len:
605 * @string: a #GString
606 * @val: bytes to append
607 * @len: number of bytes of @val to use
609 * Appends @len bytes of @val to @string. Because @len is
610 * provided, @val may contain embedded nuls and need not
611 * be nul-terminated.
613 * Since this function does not stop at nul bytes, it is
614 * the caller's responsibility to ensure that @val has at
615 * least @len addressable bytes.
617 * Returns: @string
619 GString *
620 g_string_append_len (GString *string,
621 const gchar *val,
622 gssize len)
624 g_return_val_if_fail (string != NULL, NULL);
625 g_return_val_if_fail (len == 0 || val != NULL, string);
627 return g_string_insert_len (string, -1, val, len);
631 * g_string_append_c:
632 * @string: a #GString
633 * @c: the byte to append onto the end of @string
635 * Adds a byte onto the end of a #GString, expanding
636 * it if necessary.
638 * Returns: @string
640 #undef g_string_append_c
641 GString *
642 g_string_append_c (GString *string,
643 gchar c)
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
656 * to the string.
658 * Return value: @string
660 GString *
661 g_string_append_unichar (GString *string,
662 gunichar wc)
664 g_return_val_if_fail (string != NULL, NULL);
666 return g_string_insert_unichar (string, -1, wc);
670 * g_string_prepend:
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: @string
679 GString *
680 g_string_prepend (GString *string,
681 const gchar *val)
683 g_return_val_if_fail (string != NULL, NULL);
684 g_return_val_if_fail (val != NULL, string);
686 return g_string_insert_len (string, 0, val, -1);
690 * g_string_prepend_len:
691 * @string: a #GString
692 * @val: bytes to prepend
693 * @len: number of bytes in @val to prepend
695 * Prepends @len bytes of @val to @string.
696 * Because @len is provided, @val may contain
697 * embedded nuls and need not be nul-terminated.
699 * Since this function does not stop at nul bytes,
700 * it is the caller's responsibility to ensure that
701 * @val has at least @len addressable bytes.
703 * Returns: @string
705 GString *
706 g_string_prepend_len (GString *string,
707 const gchar *val,
708 gssize len)
710 g_return_val_if_fail (string != NULL, NULL);
711 g_return_val_if_fail (val != NULL, string);
713 return g_string_insert_len (string, 0, val, len);
717 * g_string_prepend_c:
718 * @string: a #GString
719 * @c: the byte to prepend on the start of the #GString
721 * Adds a byte onto the start of a #GString,
722 * expanding it if necessary.
724 * Returns: @string
726 GString *
727 g_string_prepend_c (GString *string,
728 gchar c)
730 g_return_val_if_fail (string != NULL, NULL);
732 return g_string_insert_c (string, 0, c);
736 * g_string_prepend_unichar:
737 * @string: a #GString
738 * @wc: a Unicode character
740 * Converts a Unicode character into UTF-8, and prepends it
741 * to the string.
743 * Return value: @string
745 GString *
746 g_string_prepend_unichar (GString *string,
747 gunichar wc)
749 g_return_val_if_fail (string != NULL, NULL);
751 return g_string_insert_unichar (string, 0, wc);
755 * g_string_insert:
756 * @string: a #GString
757 * @pos: the position to insert the copy of the string
758 * @val: the string to insert
760 * Inserts a copy of a string into a #GString,
761 * expanding it if necessary.
763 * Returns: @string
765 GString *
766 g_string_insert (GString *string,
767 gssize pos,
768 const gchar *val)
770 g_return_val_if_fail (string != NULL, NULL);
771 g_return_val_if_fail (val != NULL, string);
773 if (pos >= 0)
774 g_return_val_if_fail (pos <= string->len, string);
776 return g_string_insert_len (string, pos, val, -1);
780 * g_string_insert_c:
781 * @string: a #GString
782 * @pos: the position to insert the byte
783 * @c: the byte to insert
785 * Inserts a byte into a #GString, expanding it if necessary.
787 * Returns: @string
789 GString *
790 g_string_insert_c (GString *string,
791 gssize pos,
792 gchar c)
794 g_return_val_if_fail (string != NULL, NULL);
796 g_string_maybe_expand (string, 1);
798 if (pos < 0)
799 pos = string->len;
800 else
801 g_return_val_if_fail (pos <= string->len, string);
803 /* If not just an append, move the old stuff */
804 if (pos < string->len)
805 memmove (string->str + pos + 1, string->str + pos, string->len - pos);
807 string->str[pos] = c;
809 string->len += 1;
811 string->str[string->len] = 0;
813 return string;
817 * g_string_insert_unichar:
818 * @string: a #GString
819 * @pos: the position at which to insert character, or -1
820 * to append at the end of the string
821 * @wc: a Unicode character
823 * Converts a Unicode character into UTF-8, and insert it
824 * into the string at the given position.
826 * Return value: @string
828 GString *
829 g_string_insert_unichar (GString *string,
830 gssize pos,
831 gunichar wc)
833 gint charlen, first, i;
834 gchar *dest;
836 g_return_val_if_fail (string != NULL, NULL);
838 /* Code copied from g_unichar_to_utf() */
839 if (wc < 0x80)
841 first = 0;
842 charlen = 1;
844 else if (wc < 0x800)
846 first = 0xc0;
847 charlen = 2;
849 else if (wc < 0x10000)
851 first = 0xe0;
852 charlen = 3;
854 else if (wc < 0x200000)
856 first = 0xf0;
857 charlen = 4;
859 else if (wc < 0x4000000)
861 first = 0xf8;
862 charlen = 5;
864 else
866 first = 0xfc;
867 charlen = 6;
869 /* End of copied code */
871 g_string_maybe_expand (string, charlen);
873 if (pos < 0)
874 pos = string->len;
875 else
876 g_return_val_if_fail (pos <= string->len, string);
878 /* If not just an append, move the old stuff */
879 if (pos < string->len)
880 memmove (string->str + pos + charlen, string->str + pos, string->len - pos);
882 dest = string->str + pos;
883 /* Code copied from g_unichar_to_utf() */
884 for (i = charlen - 1; i > 0; --i)
886 dest[i] = (wc & 0x3f) | 0x80;
887 wc >>= 6;
889 dest[0] = wc | first;
890 /* End of copied code */
892 string->len += charlen;
894 string->str[string->len] = 0;
896 return string;
900 * g_string_overwrite:
901 * @string: a #GString
902 * @pos: the position at which to start overwriting
903 * @val: the string that will overwrite the @string starting at @pos
905 * Overwrites part of a string, lengthening it if necessary.
907 * Return value: @string
909 * Since: 2.14
911 GString *
912 g_string_overwrite (GString *string,
913 gsize pos,
914 const gchar *val)
916 g_return_val_if_fail (val != NULL, string);
917 return g_string_overwrite_len (string, pos, val, strlen (val));
921 * g_string_overwrite_len:
922 * @string: a #GString
923 * @pos: the position at which to start overwriting
924 * @val: the string that will overwrite the @string starting at @pos
925 * @len: the number of bytes to write from @val
927 * Overwrites part of a string, lengthening it if necessary.
928 * This function will work with embedded nuls.
930 * Return value: @string
932 * Since: 2.14
934 GString *
935 g_string_overwrite_len (GString *string,
936 gsize pos,
937 const gchar *val,
938 gssize len)
940 gsize end;
942 g_return_val_if_fail (string != NULL, NULL);
944 if (!len)
945 return string;
947 g_return_val_if_fail (val != NULL, string);
948 g_return_val_if_fail (pos <= string->len, string);
950 if (len < 0)
951 len = strlen (val);
953 end = pos + len;
955 if (end > string->len)
956 g_string_maybe_expand (string, end - string->len);
958 memcpy (string->str + pos, val, len);
960 if (end > string->len)
962 string->str[end] = '\0';
963 string->len = end;
966 return string;
970 * g_string_erase:
971 * @string: a #GString
972 * @pos: the position of the content to remove
973 * @len: the number of bytes to remove, or -1 to remove all
974 * following bytes
976 * Removes @len bytes from a #GString, starting at position @pos.
977 * The rest of the #GString is shifted down to fill the gap.
979 * Returns: @string
981 GString *
982 g_string_erase (GString *string,
983 gssize pos,
984 gssize len)
986 g_return_val_if_fail (string != NULL, NULL);
987 g_return_val_if_fail (pos >= 0, string);
988 g_return_val_if_fail (pos <= string->len, string);
990 if (len < 0)
991 len = string->len - pos;
992 else
994 g_return_val_if_fail (pos + len <= string->len, string);
996 if (pos + len < string->len)
997 memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
1000 string->len -= len;
1002 string->str[string->len] = 0;
1004 return string;
1008 * g_string_ascii_down:
1009 * @string: a GString
1011 * Converts all uppercase ASCII letters to lowercase ASCII letters.
1013 * Return value: passed-in @string pointer, with all the
1014 * uppercase characters converted to lowercase in place,
1015 * with semantics that exactly match g_ascii_tolower().
1017 GString *
1018 g_string_ascii_down (GString *string)
1020 gchar *s;
1021 gint n;
1023 g_return_val_if_fail (string != NULL, NULL);
1025 n = string->len;
1026 s = string->str;
1028 while (n)
1030 *s = g_ascii_tolower (*s);
1031 s++;
1032 n--;
1035 return string;
1039 * g_string_ascii_up:
1040 * @string: a GString
1042 * Converts all lowercase ASCII letters to uppercase ASCII letters.
1044 * Return value: passed-in @string pointer, with all the
1045 * lowercase characters converted to uppercase in place,
1046 * with semantics that exactly match g_ascii_toupper().
1048 GString *
1049 g_string_ascii_up (GString *string)
1051 gchar *s;
1052 gint n;
1054 g_return_val_if_fail (string != NULL, NULL);
1056 n = string->len;
1057 s = string->str;
1059 while (n)
1061 *s = g_ascii_toupper (*s);
1062 s++;
1063 n--;
1066 return string;
1070 * g_string_down:
1071 * @string: a #GString
1073 * Converts a #GString to lowercase.
1075 * Returns: the #GString
1077 * Deprecated:2.2: This function uses the locale-specific
1078 * tolower() function, which is almost never the right thing.
1079 * Use g_string_ascii_down() or g_utf8_strdown() instead.
1081 GString *
1082 g_string_down (GString *string)
1084 guchar *s;
1085 glong n;
1087 g_return_val_if_fail (string != NULL, NULL);
1089 n = string->len;
1090 s = (guchar *) string->str;
1092 while (n)
1094 if (isupper (*s))
1095 *s = tolower (*s);
1096 s++;
1097 n--;
1100 return string;
1104 * g_string_up:
1105 * @string: a #GString
1107 * Converts a #GString to uppercase.
1109 * Return value: @string
1111 * Deprecated:2.2: This function uses the locale-specific
1112 * toupper() function, which is almost never the right thing.
1113 * Use g_string_ascii_up() or g_utf8_strup() instead.
1115 GString *
1116 g_string_up (GString *string)
1118 guchar *s;
1119 glong n;
1121 g_return_val_if_fail (string != NULL, NULL);
1123 n = string->len;
1124 s = (guchar *) string->str;
1126 while (n)
1128 if (islower (*s))
1129 *s = toupper (*s);
1130 s++;
1131 n--;
1134 return string;
1138 * g_string_append_vprintf:
1139 * @string: a #GString
1140 * @format: the string format. See the printf() documentation
1141 * @args: the list of arguments to insert in the output
1143 * Appends a formatted string onto the end of a #GString.
1144 * This function is similar to g_string_append_printf()
1145 * except that the arguments to the format string are passed
1146 * as a va_list.
1148 * Since: 2.14
1150 void
1151 g_string_append_vprintf (GString *string,
1152 const gchar *format,
1153 va_list args)
1155 gchar *buf;
1156 gint len;
1158 g_return_if_fail (string != NULL);
1159 g_return_if_fail (format != NULL);
1161 len = g_vasprintf (&buf, format, args);
1163 if (len >= 0)
1165 g_string_maybe_expand (string, len);
1166 memcpy (string->str + string->len, buf, len + 1);
1167 string->len += len;
1168 g_free (buf);
1173 * g_string_vprintf:
1174 * @string: a #GString
1175 * @format: the string format. See the printf() documentation
1176 * @args: the parameters to insert into the format string
1178 * Writes a formatted string into a #GString.
1179 * This function is similar to g_string_printf() except that
1180 * the arguments to the format string are passed as a va_list.
1182 * Since: 2.14
1184 void
1185 g_string_vprintf (GString *string,
1186 const gchar *format,
1187 va_list args)
1189 g_string_truncate (string, 0);
1190 g_string_append_vprintf (string, format, args);
1194 * g_string_sprintf:
1195 * @string: a #GString
1196 * @format: the string format. See the sprintf() documentation
1197 * @...: the parameters to insert into the format string
1199 * Writes a formatted string into a #GString.
1200 * This is similar to the standard sprintf() function,
1201 * except that the #GString buffer automatically expands
1202 * to contain the results. The previous contents of the
1203 * #GString are destroyed.
1205 * Deprecated: This function has been renamed to g_string_printf().
1209 * g_string_printf:
1210 * @string: a #GString
1211 * @format: the string format. See the printf() documentation
1212 * @...: the parameters to insert into the format string
1214 * Writes a formatted string into a #GString.
1215 * This is similar to the standard sprintf() function,
1216 * except that the #GString buffer automatically expands
1217 * to contain the results. The previous contents of the
1218 * #GString are destroyed.
1220 void
1221 g_string_printf (GString *string,
1222 const gchar *format,
1223 ...)
1225 va_list args;
1227 g_string_truncate (string, 0);
1229 va_start (args, format);
1230 g_string_append_vprintf (string, format, args);
1231 va_end (args);
1235 * g_string_sprintfa:
1236 * @string: a #GString
1237 * @format: the string format. See the sprintf() documentation
1238 * @...: the parameters to insert into the format string
1240 * Appends a formatted string onto the end of a #GString.
1241 * This function is similar to g_string_sprintf() except that
1242 * the text is appended to the #GString.
1244 * Deprecated: This function has been renamed to g_string_append_printf()
1248 * g_string_append_printf:
1249 * @string: a #GString
1250 * @format: the string format. See the printf() documentation
1251 * @...: the parameters to insert into the format string
1253 * Appends a formatted string onto the end of a #GString.
1254 * This function is similar to g_string_printf() except
1255 * that the text is appended to the #GString.
1257 void
1258 g_string_append_printf (GString *string,
1259 const gchar *format,
1260 ...)
1262 va_list args;
1264 va_start (args, format);
1265 g_string_append_vprintf (string, format, args);
1266 va_end (args);