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/.
48 * SECTION:string_chunks
49 * @title: String Chunks
50 * @short_description: efficient storage of groups of strings
52 * String chunks are used to store groups of strings. Memory is
53 * allocated in blocks, and as strings are added to the #GStringChunk
54 * they are copied into the next free position in a block. When a block
55 * is full a new block is allocated.
57 * When storing a large number of strings, string chunks are more
58 * efficient than using g_strdup() since fewer calls to malloc() are
59 * needed, and less memory is wasted in memory allocation overheads.
61 * By adding strings with g_string_chunk_insert_const() it is also
62 * possible to remove duplicates.
64 * To create a new #GStringChunk use g_string_chunk_new().
66 * To add strings to a #GStringChunk use g_string_chunk_insert().
68 * To add strings to a #GStringChunk, but without duplicating strings
69 * which are already in the #GStringChunk, use
70 * g_string_chunk_insert_const().
72 * To free the entire #GStringChunk use g_string_chunk_free(). It is
73 * not possible to free individual strings.
79 * An opaque data structure representing String Chunks. It should only
80 * be accessed by using the following functions.
84 GHashTable *const_table;
97 * @v2: a key to compare with @v1
99 * Compares two strings for byte-by-byte equality and returns %TRUE
100 * if they are equal. It can be passed to g_hash_table_new() as the
101 * @key_equal_func parameter, when using strings as keys in a #GHashTable.
103 * Note that this function is primarily meant as a hash table comparison
104 * function. For a general-purpose, %NULL-safe string comparison function,
107 * Returns: %TRUE if the two keys match
110 g_str_equal (gconstpointer v1,
113 const gchar *string1 = v1;
114 const gchar *string2 = v2;
116 return strcmp (string1, string2) == 0;
123 * Converts a string to a hash value.
125 * This function implements the widely used "djb" hash apparently posted
126 * by Daniel Bernstein to comp.lang.c some time ago. The 32 bit
127 * unsigned hash value starts at 5381 and for each byte 'c' in the
128 * string, is updated: <literal>hash = hash * 33 + c</literal>. This
129 * function uses the signed value of each byte.
131 * It can be passed to g_hash_table_new() as the @hash_func parameter,
132 * when using strings as keys in a #GHashTable.
134 * Returns: a hash value corresponding to the key
137 g_str_hash (gconstpointer v)
139 const signed char *p;
142 for (p = v; *p != '\0'; p++)
143 h = (h << 5) + h + *p;
148 #define MY_MAXSIZE ((gsize)-1)
151 nearest_power (gsize base, gsize num)
153 if (num > MY_MAXSIZE / 2)
172 * g_string_chunk_new:
173 * @size: the default size of the blocks of memory which are
174 * allocated to store the strings. If a particular string
175 * is larger than this default size, a larger block of
176 * memory will be allocated for it.
178 * Creates a new #GStringChunk.
180 * Returns: a new #GStringChunk
183 g_string_chunk_new (gsize size)
185 GStringChunk *new_chunk = g_new (GStringChunk, 1);
186 gsize actual_size = 1;
188 actual_size = nearest_power (1, size);
190 new_chunk->const_table = NULL;
191 new_chunk->storage_list = NULL;
192 new_chunk->storage_next = actual_size;
193 new_chunk->default_size = actual_size;
194 new_chunk->this_size = actual_size;
200 * g_string_chunk_free:
201 * @chunk: a #GStringChunk
203 * Frees all memory allocated by the #GStringChunk.
204 * After calling g_string_chunk_free() it is not safe to
205 * access any of the strings which were contained within it.
208 g_string_chunk_free (GStringChunk *chunk)
212 g_return_if_fail (chunk != NULL);
214 if (chunk->storage_list)
216 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
217 g_free (tmp_list->data);
219 g_slist_free (chunk->storage_list);
222 if (chunk->const_table)
223 g_hash_table_destroy (chunk->const_table);
229 * g_string_chunk_clear:
230 * @chunk: a #GStringChunk
232 * Frees all strings contained within the #GStringChunk.
233 * After calling g_string_chunk_clear() it is not safe to
234 * access any of the strings which were contained within it.
239 g_string_chunk_clear (GStringChunk *chunk)
243 g_return_if_fail (chunk != NULL);
245 if (chunk->storage_list)
247 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
248 g_free (tmp_list->data);
250 g_slist_free (chunk->storage_list);
252 chunk->storage_list = NULL;
253 chunk->storage_next = chunk->default_size;
254 chunk->this_size = chunk->default_size;
257 if (chunk->const_table)
258 g_hash_table_remove_all (chunk->const_table);
262 * g_string_chunk_insert:
263 * @chunk: a #GStringChunk
264 * @string: the string to add
266 * Adds a copy of @string to the #GStringChunk.
267 * It returns a pointer to the new copy of the string
268 * in the #GStringChunk. The characters in the string
269 * can be changed, if necessary, though you should not
270 * change anything after the end of the string.
272 * Unlike g_string_chunk_insert_const(), this function
273 * does not check for duplicates. Also strings added
274 * with g_string_chunk_insert() will not be searched
275 * by g_string_chunk_insert_const() when looking for
278 * Returns: a pointer to the copy of @string within
282 g_string_chunk_insert (GStringChunk *chunk,
285 g_return_val_if_fail (chunk != NULL, NULL);
287 return g_string_chunk_insert_len (chunk, string, -1);
291 * g_string_chunk_insert_const:
292 * @chunk: a #GStringChunk
293 * @string: the string to add
295 * Adds a copy of @string to the #GStringChunk, unless the same
296 * string has already been added to the #GStringChunk with
297 * g_string_chunk_insert_const().
299 * This function is useful if you need to copy a large number
300 * of strings but do not want to waste space storing duplicates.
301 * But you must remember that there may be several pointers to
302 * the same string, and so any changes made to the strings
303 * should be done very carefully.
305 * Note that g_string_chunk_insert_const() will not return a
306 * pointer to a string added with g_string_chunk_insert(), even
309 * Returns: a pointer to the new or existing copy of @string
310 * within the #GStringChunk
313 g_string_chunk_insert_const (GStringChunk *chunk,
318 g_return_val_if_fail (chunk != NULL, NULL);
320 if (!chunk->const_table)
321 chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
323 lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
327 lookup = g_string_chunk_insert (chunk, string);
328 g_hash_table_insert (chunk->const_table, lookup, lookup);
335 * g_string_chunk_insert_len:
336 * @chunk: a #GStringChunk
337 * @string: bytes to insert
338 * @len: number of bytes of @string to insert, or -1 to insert a
339 * nul-terminated string
341 * Adds a copy of the first @len bytes of @string to the #GStringChunk.
342 * The copy is nul-terminated.
344 * Since this function does not stop at nul bytes, it is the caller's
345 * responsibility to ensure that @string has at least @len addressable
348 * The characters in the returned string can be changed, if necessary,
349 * though you should not change anything after the end of the string.
351 * Return value: a pointer to the copy of @string within the #GStringChunk
356 g_string_chunk_insert_len (GStringChunk *chunk,
363 g_return_val_if_fail (chunk != NULL, NULL);
366 size = strlen (string);
370 if ((chunk->storage_next + size + 1) > chunk->this_size)
372 gsize new_size = nearest_power (chunk->default_size, size + 1);
374 chunk->storage_list = g_slist_prepend (chunk->storage_list,
375 g_new (gchar, new_size));
377 chunk->this_size = new_size;
378 chunk->storage_next = 0;
381 pos = ((gchar *) chunk->storage_list->data) + chunk->storage_next;
383 *(pos + size) = '\0';
385 memcpy (pos, string, size);
387 chunk->storage_next += size + 1;
398 * @short_description: text buffers which grow automatically
401 * A #GString is an object that handles the memory management
402 * of a C string for you. You can think of it as similar to a
403 * Java StringBuffer. In addition to the string itself, GString
404 * stores the length of the string, so can be used for binary
405 * data with embedded nul bytes. To access the C string managed
406 * by the GString @string, simply use @string->str.
411 * @str: points to the character data. It may move as text is added.
412 * The @str field is null-terminated and so
413 * can be used as an ordinary C string.
414 * @len: contains the length of the string, not including the
415 * terminating nul byte.
416 * @allocated_len: the number of bytes that can be stored in the
417 * string before it needs to be reallocated. May be larger than @len.
419 * The GString struct contains the public fields of a GString.
424 g_string_maybe_expand (GString *string,
427 if (string->len + len >= string->allocated_len)
429 string->allocated_len = nearest_power (1, string->len + len + 1);
430 string->str = g_realloc (string->str, string->allocated_len);
435 * g_string_sized_new:
436 * @dfl_size: the default size of the space allocated to
439 * Creates a new #GString, with enough space for @dfl_size
440 * bytes. This is useful if you are going to add a lot of
441 * text to the string and don't want it to be reallocated
444 * Returns: the new #GString
447 g_string_sized_new (gsize dfl_size)
449 GString *string = g_slice_new (GString);
451 string->allocated_len = 0;
455 g_string_maybe_expand (string, MAX (dfl_size, 2));
463 * @init: the initial text to copy into the string
465 * Creates a new #GString, initialized with the given string.
467 * Returns: the new #GString
470 g_string_new (const gchar *init)
474 if (init == NULL || *init == '\0')
475 string = g_string_sized_new (2);
481 string = g_string_sized_new (len + 2);
483 g_string_append_len (string, init, len);
491 * @init: initial contents of the string
492 * @len: length of @init to use
494 * Creates a new #GString with @len bytes of the @init buffer.
495 * Because a length is provided, @init need not be nul-terminated,
496 * and can contain embedded nul bytes.
498 * Since this function does not stop at nul bytes, it is the caller's
499 * responsibility to ensure that @init has at least @len addressable
502 * Returns: a new #GString
505 g_string_new_len (const gchar *init,
511 return g_string_new (init);
514 string = g_string_sized_new (len);
517 g_string_append_len (string, init, len);
525 * @string: a #GString
526 * @free_segment: if %TRUE, the actual character data is freed as well
528 * Frees the memory allocated for the #GString.
529 * If @free_segment is %TRUE it also frees the character data. If
530 * it's %FALSE, the caller gains ownership of the buffer and must
531 * free it after use with g_free().
533 * Returns: the character data of @string
534 * (i.e. %NULL if @free_segment is %TRUE)
537 g_string_free (GString *string,
538 gboolean free_segment)
542 g_return_val_if_fail (string != NULL, NULL);
546 g_free (string->str);
550 segment = string->str;
552 g_slice_free (GString, string);
560 * @v2: another #GString
562 * Compares two strings for equality, returning %TRUE if they are equal.
563 * For use with #GHashTable.
565 * Returns: %TRUE if they strings are the same length and contain the
569 g_string_equal (const GString *v,
573 GString *string1 = (GString *) v;
574 GString *string2 = (GString *) v2;
575 gsize i = string1->len;
577 if (i != string2->len)
595 * @str: a string to hash
597 * Creates a hash code for @str; for use with #GHashTable.
599 * Returns: hash code for @str
602 g_string_hash (const GString *str)
604 const gchar *p = str->str;
608 /* 31 bit hash function */
611 h = (h << 5) - h + *p;
620 * @string: the destination #GString. Its current contents
622 * @rval: the string to copy into @string
624 * Copies the bytes from a string into a #GString,
625 * destroying any previous contents. It is rather like
626 * the standard strcpy() function, except that you do not
627 * have to worry about having enough space to copy the string.
632 g_string_assign (GString *string,
635 g_return_val_if_fail (string != NULL, NULL);
636 g_return_val_if_fail (rval != NULL, string);
638 /* Make sure assigning to itself doesn't corrupt the string. */
639 if (string->str != rval)
641 /* Assigning from substring should be ok, since
642 * g_string_truncate() does not reallocate.
644 g_string_truncate (string, 0);
645 g_string_append (string, rval);
653 * @string: a #GString
654 * @len: the new size of @string
656 * Cuts off the end of the GString, leaving the first @len bytes.
661 g_string_truncate (GString *string,
664 g_return_val_if_fail (string != NULL, NULL);
666 string->len = MIN (len, string->len);
667 string->str[string->len] = 0;
674 * @string: a #GString
675 * @len: the new length
677 * Sets the length of a #GString. If the length is less than
678 * the current length, the string will be truncated. If the
679 * length is greater than the current length, the contents
680 * of the newly added area are undefined. (However, as
681 * always, string->str[string->len] will be a nul byte.)
683 * Return value: @string
686 g_string_set_size (GString *string,
689 g_return_val_if_fail (string != NULL, NULL);
691 if (len >= string->allocated_len)
692 g_string_maybe_expand (string, len - string->len);
695 string->str[len] = 0;
701 * g_string_insert_len:
702 * @string: a #GString
703 * @pos: position in @string where insertion should
704 * happen, or -1 for at the end
705 * @val: bytes to insert
706 * @len: number of bytes of @val to insert
708 * Inserts @len bytes of @val into @string at @pos.
709 * Because @len is provided, @val may contain embedded
710 * nuls and need not be nul-terminated. If @pos is -1,
711 * bytes are inserted at the end of the string.
713 * Since this function does not stop at nul bytes, it is
714 * the caller's responsibility to ensure that @val has at
715 * least @len addressable bytes.
720 g_string_insert_len (GString *string,
725 g_return_val_if_fail (string != NULL, NULL);
726 g_return_val_if_fail (len == 0 || val != NULL, string);
737 g_return_val_if_fail (pos <= string->len, string);
739 /* Check whether val represents a substring of string.
740 * This test probably violates chapter and verse of the C standards,
741 * since ">=" and "<=" are only valid when val really is a substring.
742 * In practice, it will work on modern archs.
744 if (val >= string->str && val <= string->str + string->len)
746 gsize offset = val - string->str;
749 g_string_maybe_expand (string, len);
750 val = string->str + offset;
751 /* At this point, val is valid again. */
753 /* Open up space where we are going to insert. */
754 if (pos < string->len)
755 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
757 /* Move the source part before the gap, if any. */
760 precount = MIN (len, pos - offset);
761 memcpy (string->str + pos, val, precount);
764 /* Move the source part after the gap, if any. */
766 memcpy (string->str + pos + precount,
767 val + /* Already moved: */ precount + /* Space opened up: */ len,
772 g_string_maybe_expand (string, len);
774 /* If we aren't appending at the end, move a hunk
775 * of the old string to the end, opening up space
777 if (pos < string->len)
778 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
780 /* insert the new string */
782 string->str[pos] = *val;
784 memcpy (string->str + pos, val, len);
789 string->str[string->len] = 0;
794 #define SUB_DELIM_CHARS "!$&'()*+,;="
798 const char *reserved_chars_allowed)
800 if (g_ascii_isalnum (c) ||
807 if (reserved_chars_allowed &&
808 strchr (reserved_chars_allowed, c) != NULL)
815 gunichar_ok (gunichar c)
818 (c != (gunichar) -2) &&
819 (c != (gunichar) -1);
823 * g_string_append_uri_escaped:
824 * @string: a #GString
825 * @unescaped: a string
826 * @reserved_chars_allowed: a string of reserved characters allowed
827 * to be used, or %NULL
828 * @allow_utf8: set %TRUE if the escaped string may include UTF8 characters
830 * Appends @unescaped to @string, escaped any characters that
831 * are reserved in URIs using URI-style escape sequences.
838 g_string_append_uri_escaped (GString *string,
839 const gchar *unescaped,
840 const gchar *reserved_chars_allowed,
845 static const gchar hex[16] = "0123456789ABCDEF";
847 g_return_val_if_fail (string != NULL, NULL);
848 g_return_val_if_fail (unescaped != NULL, NULL);
850 end = unescaped + strlen (unescaped);
852 while ((c = *unescaped) != 0)
854 if (c >= 0x80 && allow_utf8 &&
855 gunichar_ok (g_utf8_get_char_validated (unescaped, end - unescaped)))
857 int len = g_utf8_skip [c];
858 g_string_append_len (string, unescaped, len);
861 else if (is_valid (c, reserved_chars_allowed))
863 g_string_append_c (string, c);
868 g_string_append_c (string, '%');
869 g_string_append_c (string, hex[((guchar)c) >> 4]);
870 g_string_append_c (string, hex[((guchar)c) & 0xf]);
880 * @string: a #GString
881 * @val: the string to append onto the end of @string
883 * Adds a string onto the end of a #GString, expanding
889 g_string_append (GString *string,
892 g_return_val_if_fail (string != NULL, NULL);
893 g_return_val_if_fail (val != NULL, string);
895 return g_string_insert_len (string, -1, val, -1);
899 * g_string_append_len:
900 * @string: a #GString
901 * @val: bytes to append
902 * @len: number of bytes of @val to use
904 * Appends @len bytes of @val to @string. Because @len is
905 * provided, @val may contain embedded nuls and need not
908 * Since this function does not stop at nul bytes, it is
909 * the caller's responsibility to ensure that @val has at
910 * least @len addressable bytes.
915 g_string_append_len (GString *string,
919 g_return_val_if_fail (string != NULL, NULL);
920 g_return_val_if_fail (len == 0 || val != NULL, string);
922 return g_string_insert_len (string, -1, val, len);
927 * @string: a #GString
928 * @c: the byte to append onto the end of @string
930 * Adds a byte onto the end of a #GString, expanding
935 #undef g_string_append_c
937 g_string_append_c (GString *string,
940 g_return_val_if_fail (string != NULL, NULL);
942 return g_string_insert_c (string, -1, c);
946 * g_string_append_unichar:
947 * @string: a #GString
948 * @wc: a Unicode character
950 * Converts a Unicode character into UTF-8, and appends it
953 * Return value: @string
956 g_string_append_unichar (GString *string,
959 g_return_val_if_fail (string != NULL, NULL);
961 return g_string_insert_unichar (string, -1, wc);
966 * @string: a #GString
967 * @val: the string to prepend on the start of @string
969 * Adds a string on to the start of a #GString,
970 * expanding it if necessary.
975 g_string_prepend (GString *string,
978 g_return_val_if_fail (string != NULL, NULL);
979 g_return_val_if_fail (val != NULL, string);
981 return g_string_insert_len (string, 0, val, -1);
985 * g_string_prepend_len:
986 * @string: a #GString
987 * @val: bytes to prepend
988 * @len: number of bytes in @val to prepend
990 * Prepends @len bytes of @val to @string.
991 * Because @len is provided, @val may contain
992 * embedded nuls and need not be nul-terminated.
994 * Since this function does not stop at nul bytes,
995 * it is the caller's responsibility to ensure that
996 * @val has at least @len addressable bytes.
1001 g_string_prepend_len (GString *string,
1005 g_return_val_if_fail (string != NULL, NULL);
1006 g_return_val_if_fail (val != NULL, string);
1008 return g_string_insert_len (string, 0, val, len);
1012 * g_string_prepend_c:
1013 * @string: a #GString
1014 * @c: the byte to prepend on the start of the #GString
1016 * Adds a byte onto the start of a #GString,
1017 * expanding it if necessary.
1022 g_string_prepend_c (GString *string,
1025 g_return_val_if_fail (string != NULL, NULL);
1027 return g_string_insert_c (string, 0, c);
1031 * g_string_prepend_unichar:
1032 * @string: a #GString
1033 * @wc: a Unicode character
1035 * Converts a Unicode character into UTF-8, and prepends it
1038 * Return value: @string
1041 g_string_prepend_unichar (GString *string,
1044 g_return_val_if_fail (string != NULL, NULL);
1046 return g_string_insert_unichar (string, 0, wc);
1051 * @string: a #GString
1052 * @pos: the position to insert the copy of the string
1053 * @val: the string to insert
1055 * Inserts a copy of a string into a #GString,
1056 * expanding it if necessary.
1061 g_string_insert (GString *string,
1065 g_return_val_if_fail (string != NULL, NULL);
1066 g_return_val_if_fail (val != NULL, string);
1069 g_return_val_if_fail (pos <= string->len, string);
1071 return g_string_insert_len (string, pos, val, -1);
1075 * g_string_insert_c:
1076 * @string: a #GString
1077 * @pos: the position to insert the byte
1078 * @c: the byte to insert
1080 * Inserts a byte into a #GString, expanding it if necessary.
1085 g_string_insert_c (GString *string,
1089 g_return_val_if_fail (string != NULL, NULL);
1091 g_string_maybe_expand (string, 1);
1096 g_return_val_if_fail (pos <= string->len, string);
1098 /* If not just an append, move the old stuff */
1099 if (pos < string->len)
1100 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
1102 string->str[pos] = c;
1106 string->str[string->len] = 0;
1112 * g_string_insert_unichar:
1113 * @string: a #GString
1114 * @pos: the position at which to insert character, or -1
1115 * to append at the end of the string
1116 * @wc: a Unicode character
1118 * Converts a Unicode character into UTF-8, and insert it
1119 * into the string at the given position.
1121 * Return value: @string
1124 g_string_insert_unichar (GString *string,
1128 gint charlen, first, i;
1131 g_return_val_if_fail (string != NULL, NULL);
1133 /* Code copied from g_unichar_to_utf() */
1139 else if (wc < 0x800)
1144 else if (wc < 0x10000)
1149 else if (wc < 0x200000)
1154 else if (wc < 0x4000000)
1164 /* End of copied code */
1166 g_string_maybe_expand (string, charlen);
1171 g_return_val_if_fail (pos <= string->len, string);
1173 /* If not just an append, move the old stuff */
1174 if (pos < string->len)
1175 g_memmove (string->str + pos + charlen, string->str + pos, string->len - pos);
1177 dest = string->str + pos;
1178 /* Code copied from g_unichar_to_utf() */
1179 for (i = charlen - 1; i > 0; --i)
1181 dest[i] = (wc & 0x3f) | 0x80;
1184 dest[0] = wc | first;
1185 /* End of copied code */
1187 string->len += charlen;
1189 string->str[string->len] = 0;
1195 * g_string_overwrite:
1196 * @string: a #GString
1197 * @pos: the position at which to start overwriting
1198 * @val: the string that will overwrite the @string starting at @pos
1200 * Overwrites part of a string, lengthening it if necessary.
1202 * Return value: @string
1207 g_string_overwrite (GString *string,
1211 g_return_val_if_fail (val != NULL, string);
1212 return g_string_overwrite_len (string, pos, val, strlen (val));
1216 * g_string_overwrite_len:
1217 * @string: a #GString
1218 * @pos: the position at which to start overwriting
1219 * @val: the string that will overwrite the @string starting at @pos
1220 * @len: the number of bytes to write from @val
1222 * Overwrites part of a string, lengthening it if necessary.
1223 * This function will work with embedded nuls.
1225 * Return value: @string
1230 g_string_overwrite_len (GString *string,
1237 g_return_val_if_fail (string != NULL, NULL);
1242 g_return_val_if_fail (val != NULL, string);
1243 g_return_val_if_fail (pos <= string->len, string);
1250 if (end > string->len)
1251 g_string_maybe_expand (string, end - string->len);
1253 memcpy (string->str + pos, val, len);
1255 if (end > string->len)
1257 string->str[end] = '\0';
1266 * @string: a #GString
1267 * @pos: the position of the content to remove
1268 * @len: the number of bytes to remove, or -1 to remove all
1271 * Removes @len bytes from a #GString, starting at position @pos.
1272 * The rest of the #GString is shifted down to fill the gap.
1277 g_string_erase (GString *string,
1281 g_return_val_if_fail (string != NULL, NULL);
1282 g_return_val_if_fail (pos >= 0, string);
1283 g_return_val_if_fail (pos <= string->len, string);
1286 len = string->len - pos;
1289 g_return_val_if_fail (pos + len <= string->len, string);
1291 if (pos + len < string->len)
1292 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
1297 string->str[string->len] = 0;
1303 * g_string_ascii_down:
1304 * @string: a GString
1306 * Converts all uppercase ASCII letters to lowercase ASCII letters.
1308 * Return value: passed-in @string pointer, with all the
1309 * uppercase characters converted to lowercase in place,
1310 * with semantics that exactly match g_ascii_tolower().
1313 g_string_ascii_down (GString *string)
1318 g_return_val_if_fail (string != NULL, NULL);
1325 *s = g_ascii_tolower (*s);
1334 * g_string_ascii_up:
1335 * @string: a GString
1337 * Converts all lowercase ASCII letters to uppercase ASCII letters.
1339 * Return value: passed-in @string pointer, with all the
1340 * lowercase characters converted to uppercase in place,
1341 * with semantics that exactly match g_ascii_toupper().
1344 g_string_ascii_up (GString *string)
1349 g_return_val_if_fail (string != NULL, NULL);
1356 *s = g_ascii_toupper (*s);
1366 * @string: a #GString
1368 * Converts a #GString to lowercase.
1370 * Returns: the #GString
1372 * Deprecated:2.2: This function uses the locale-specific
1373 * tolower() function, which is almost never the right thing.
1374 * Use g_string_ascii_down() or g_utf8_strdown() instead.
1377 g_string_down (GString *string)
1382 g_return_val_if_fail (string != NULL, NULL);
1385 s = (guchar *) string->str;
1400 * @string: a #GString
1402 * Converts a #GString to uppercase.
1404 * Return value: @string
1406 * Deprecated:2.2: This function uses the locale-specific
1407 * toupper() function, which is almost never the right thing.
1408 * Use g_string_ascii_up() or g_utf8_strup() instead.
1411 g_string_up (GString *string)
1416 g_return_val_if_fail (string != NULL, NULL);
1419 s = (guchar *) string->str;
1433 * g_string_append_vprintf:
1434 * @string: a #GString
1435 * @format: the string format. See the printf() documentation
1436 * @args: the list of arguments to insert in the output
1438 * Appends a formatted string onto the end of a #GString.
1439 * This function is similar to g_string_append_printf()
1440 * except that the arguments to the format string are passed
1446 g_string_append_vprintf (GString *string,
1447 const gchar *format,
1453 g_return_if_fail (string != NULL);
1454 g_return_if_fail (format != NULL);
1456 len = g_vasprintf (&buf, format, args);
1460 g_string_maybe_expand (string, len);
1461 memcpy (string->str + string->len, buf, len + 1);
1469 * @string: a #GString
1470 * @format: the string format. See the printf() documentation
1471 * @args: the parameters to insert into the format string
1473 * Writes a formatted string into a #GString.
1474 * This function is similar to g_string_printf() except that
1475 * the arguments to the format string are passed as a va_list.
1480 g_string_vprintf (GString *string,
1481 const gchar *format,
1484 g_string_truncate (string, 0);
1485 g_string_append_vprintf (string, format, args);
1490 * @string: a #GString
1491 * @format: the string format. See the sprintf() documentation
1492 * @...: the parameters to insert into the format string
1494 * Writes a formatted string into a #GString.
1495 * This is similar to the standard sprintf() function,
1496 * except that the #GString buffer automatically expands
1497 * to contain the results. The previous contents of the
1498 * #GString are destroyed.
1500 * Deprecated: This function has been renamed to g_string_printf().
1505 * @string: a #GString
1506 * @format: the string format. See the printf() documentation
1507 * @...: the parameters to insert into the format string
1509 * Writes a formatted string into a #GString.
1510 * This is similar to the standard sprintf() function,
1511 * except that the #GString buffer automatically expands
1512 * to contain the results. The previous contents of the
1513 * #GString are destroyed.
1516 g_string_printf (GString *string,
1517 const gchar *format,
1522 g_string_truncate (string, 0);
1524 va_start (args, format);
1525 g_string_append_vprintf (string, format, args);
1530 * g_string_sprintfa:
1531 * @string: a #GString
1532 * @format: the string format. See the sprintf() documentation
1533 * @...: the parameters to insert into the format string
1535 * Appends a formatted string onto the end of a #GString.
1536 * This function is similar to g_string_sprintf() except that
1537 * the text is appended to the #GString.
1539 * Deprecated: This function has been renamed to g_string_append_printf()
1543 * g_string_append_printf:
1544 * @string: a #GString
1545 * @format: the string format. See the printf() documentation
1546 * @...: the parameters to insert into the format string
1548 * Appends a formatted string onto the end of a #GString.
1549 * This function is similar to g_string_printf() except
1550 * that the text is appended to the #GString.
1553 g_string_append_printf (GString *string,
1554 const gchar *format,
1559 va_start (args, format);
1560 g_string_append_vprintf (string, format, args);