1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
49 GHashTable *const_table;
62 * @v2: a key to compare with @v1.
64 * Compares two strings and returns %TRUE if they are equal.
65 * It can be passed to g_hash_table_new() as the @key_equal_func
66 * parameter, when using strings as keys in a #GHashTable.
68 * Returns: %TRUE if the two keys match.
71 g_str_equal (gconstpointer v1,
74 const gchar *string1 = v1;
75 const gchar *string2 = v2;
77 return strcmp (string1, string2) == 0;
84 * Converts a string to a hash value.
85 * It can be passed to g_hash_table_new() as the @hash_func parameter,
86 * when using strings as keys in a #GHashTable.
88 * Returns: a hash value corresponding to the key.
91 g_str_hash (gconstpointer v)
93 /* 31 bit hash function */
94 const signed char *p = v;
98 for (p += 1; *p != '\0'; p++)
99 h = (h << 5) - h + *p;
104 #define MY_MAXSIZE ((gsize)-1)
107 nearest_power (gsize base, gsize num)
109 if (num > MY_MAXSIZE / 2)
128 * g_string_chunk_new:
129 * @size: the default size of the blocks of memory which are
130 * allocated to store the strings. If a particular string
131 * is larger than this default size, a larger block of
132 * memory will be allocated for it.
134 * Creates a new #GStringChunk.
136 * Returns: a new #GStringChunk
139 g_string_chunk_new (gsize size)
141 GStringChunk *new_chunk = g_new (GStringChunk, 1);
144 size = nearest_power (1, size);
146 new_chunk->const_table = NULL;
147 new_chunk->storage_list = NULL;
148 new_chunk->storage_next = size;
149 new_chunk->default_size = size;
150 new_chunk->this_size = size;
156 * g_string_chunk_free:
157 * @chunk: a #GStringChunk
159 * Frees all memory allocated by the #GStringChunk.
160 * After calling g_string_chunk_free() it is not safe to
161 * access any of the strings which were contained within it.
164 g_string_chunk_free (GStringChunk *chunk)
168 g_return_if_fail (chunk != NULL);
170 if (chunk->storage_list)
172 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
173 g_free (tmp_list->data);
175 g_slist_free (chunk->storage_list);
178 if (chunk->const_table)
179 g_hash_table_destroy (chunk->const_table);
185 * g_string_chunk_clear:
186 * @chunk: a #GStringChunk
188 * Frees all strings contained within the #GStringChunk.
189 * After calling g_string_chunk_clear() it is not safe to
190 * access any of the strings which were contained within it.
195 g_string_chunk_clear (GStringChunk *chunk)
199 g_return_if_fail (chunk != NULL);
201 if (chunk->storage_list)
203 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
204 g_free (tmp_list->data);
206 g_slist_free (chunk->storage_list);
208 chunk->storage_list = NULL;
209 chunk->storage_next = chunk->default_size;
210 chunk->this_size = chunk->default_size;
213 if (chunk->const_table)
214 g_hash_table_remove_all (chunk->const_table);
218 * g_string_chunk_insert:
219 * @chunk: a #GStringChunk
220 * @string: the string to add
222 * Adds a copy of @string to the #GStringChunk.
223 * It returns a pointer to the new copy of the string
224 * in the #GStringChunk. The characters in the string
225 * can be changed, if necessary, though you should not
226 * change anything after the end of the string.
228 * Unlike g_string_chunk_insert_const(), this function
229 * does not check for duplicates. Also strings added
230 * with g_string_chunk_insert() will not be searched
231 * by g_string_chunk_insert_const() when looking for
234 * Returns: a pointer to the copy of @string within
238 g_string_chunk_insert (GStringChunk *chunk,
241 g_return_val_if_fail (chunk != NULL, NULL);
243 return g_string_chunk_insert_len (chunk, string, -1);
247 * g_string_chunk_insert_const:
248 * @chunk: a #GStringChunk
249 * @string: the string to add
251 * Adds a copy of @string to the #GStringChunk, unless
252 * the same string has already been added to the #GStringChunk
253 * with g_string_chunk_insert_const().
255 * This function is useful if you need to copy a large number
256 * of strings but do not want to waste space storing duplicates.
257 * But you must remember that there may be several pointers to
258 * the same string, and so any changes made to the strings
259 * should be done very carefully.
261 * Note that g_string_chunk_insert_const() will not return a
262 * pointer to a string added with g_string_chunk_insert(), even
265 * Returns: a pointer to the new or existing copy of @string
266 * within the #GStringChunk
269 g_string_chunk_insert_const (GStringChunk *chunk,
274 g_return_val_if_fail (chunk != NULL, NULL);
276 if (!chunk->const_table)
277 chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
279 lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
283 lookup = g_string_chunk_insert (chunk, string);
284 g_hash_table_insert (chunk->const_table, lookup, lookup);
291 * g_string_chunk_insert_len:
292 * @chunk: a #GStringChunk
293 * @string: bytes to insert
294 * @len: number of bytes of @string to insert, or -1 to insert a
295 * nul-terminated string.
297 * Adds a copy of the first @len bytes of @string to the #GStringChunk. The
298 * copy is nul-terminated.
300 * The characters in the string can be changed, if necessary, though you
301 * should not change anything after the end of the string.
303 * Return value: a pointer to the copy of @string within the #GStringChunk
308 g_string_chunk_insert_len (GStringChunk *chunk,
315 g_return_val_if_fail (chunk != NULL, NULL);
318 size = strlen (string);
322 if ((chunk->storage_next + size + 1) > chunk->this_size)
324 gsize new_size = nearest_power (chunk->default_size, size + 1);
326 chunk->storage_list = g_slist_prepend (chunk->storage_list,
327 g_new (gchar, new_size));
329 chunk->this_size = new_size;
330 chunk->storage_next = 0;
333 pos = ((gchar *) chunk->storage_list->data) + chunk->storage_next;
335 *(pos + size) = '\0';
337 strncpy (pos, string, size);
341 chunk->storage_next += size + 1;
349 g_string_maybe_expand (GString* string,
352 if (string->len + len >= string->allocated_len)
354 string->allocated_len = nearest_power (1, string->len + len + 1);
355 string->str = g_realloc (string->str, string->allocated_len);
360 * g_string_sized_new:
361 * @dfl_size: the default size of the space allocated to
364 * Creates a new #GString, with enough space for @dfl_size
365 * bytes. This is useful if you are going to add a lot of
366 * text to the string and don't want it to be reallocated
369 * Returns: the new #GString.
372 g_string_sized_new (gsize dfl_size)
374 GString *string = g_slice_new (GString);
376 string->allocated_len = 0;
380 g_string_maybe_expand (string, MAX (dfl_size, 2));
388 * @init: the initial text to copy into the string.
390 * Creates a new #GString, initialized with the given string.
392 * Returns: the new #GString.
395 g_string_new (const gchar *init)
399 if (init == NULL || *init == '\0')
400 string = g_string_sized_new (2);
406 string = g_string_sized_new (len + 2);
408 g_string_append_len (string, init, len);
416 * @init: initial contents of string.
417 * @len: length of @init to use.
419 * Creates a new #GString with @len bytes of the
420 * @init buffer. Because a length is provided, @init
421 * need not be nul-terminated, and can contain embedded
424 * Returns: a new #GString.
427 g_string_new_len (const gchar *init,
433 return g_string_new (init);
436 string = g_string_sized_new (len);
439 g_string_append_len (string, init, len);
447 * @string: a #GString
448 * @free_segment: if %TRUE the actual character data is freed as well
450 * Frees the memory allocated for the #GString.
451 * If @free_segment is %TRUE it also frees the character data.
453 * Returns: the character data of @string
454 * (i.e. %NULL if @free_segment is %TRUE)
457 g_string_free (GString *string,
458 gboolean free_segment)
462 g_return_val_if_fail (string != NULL, NULL);
466 g_free (string->str);
470 segment = string->str;
472 g_slice_free (GString, string);
480 * @v2: another #GString
482 * Compares two strings for equality, returning %TRUE if they are equal.
483 * For use with #GHashTable.
485 * Returns: %TRUE if they strings are the same
486 * length and contain the same bytes.
489 g_string_equal (const GString *v,
493 GString *string1 = (GString *) v;
494 GString *string2 = (GString *) v2;
495 gsize i = string1->len;
497 if (i != string2->len)
515 * @str: a string to hash
517 * Creates a hash code for @str; for use with #GHashTable.
519 * Returns: hash code for @str
521 /* 31 bit hash function */
523 g_string_hash (const GString *str)
525 const gchar *p = str->str;
531 h = (h << 5) - h + *p;
540 * @string: the destination #GString. Its current contents
542 * @rval: the string to copy into @string
544 * Copies the bytes from a string into a #GString,
545 * destroying any previous contents. It is rather like
546 * the standard strcpy() function, except that you do not
547 * have to worry about having enough space to copy the string.
549 * Returns: the destination #GString.
552 g_string_assign (GString *string,
555 g_return_val_if_fail (string != NULL, NULL);
556 g_return_val_if_fail (rval != NULL, string);
558 /* Make sure assigning to itself doesn't corrupt the string. */
559 if (string->str != rval)
561 /* Assigning from substring should be ok since g_string_truncate
563 g_string_truncate (string, 0);
564 g_string_append (string, rval);
572 * @string: a #GString
573 * @len: the new size of the #GString
575 * Cuts off the end of the GString, leaving the first @len bytes.
577 * Returns: the #GString
580 g_string_truncate (GString *string,
583 g_return_val_if_fail (string != NULL, NULL);
585 string->len = MIN (len, string->len);
586 string->str[string->len] = 0;
593 * @string: a #GString
594 * @len: the new length
596 * Sets the length of a #GString. If the length is less than
597 * the current length, the string will be truncated. If the
598 * length is greater than the current length, the contents
599 * of the newly added area are undefined. (However, as
600 * always, string->str[string->len] will be a nul byte.)
602 * Return value: @string
605 g_string_set_size (GString *string,
608 g_return_val_if_fail (string != NULL, NULL);
610 if (len >= string->allocated_len)
611 g_string_maybe_expand (string, len - string->len);
614 string->str[len] = 0;
620 * g_string_insert_len:
621 * @string: a #GString
622 * @pos: position in @string where insertion should
623 * happen, or -1 for at the end
624 * @val: bytes to insert
625 * @len: number of bytes of @val to insert
627 * Inserts @len bytes of @val into @string at @pos.
628 * Because @len is provided, @val may contain embedded
629 * nuls and need not be nul-terminated. If @pos is -1,
630 * bytes are inserted at the end of the string.
632 * Returns: the #GString
635 g_string_insert_len (GString *string,
640 g_return_val_if_fail (string != NULL, NULL);
641 g_return_val_if_fail (val != NULL, string);
649 g_return_val_if_fail (pos <= string->len, string);
651 /* Check whether val represents a substring of string. This test
652 probably violates chapter and verse of the C standards, since
653 ">=" and "<=" are only valid when val really is a substring.
654 In practice, it will work on modern archs. */
655 if (val >= string->str && val <= string->str + string->len)
657 gsize offset = val - string->str;
660 g_string_maybe_expand (string, len);
661 val = string->str + offset;
662 /* At this point, val is valid again. */
664 /* Open up space where we are going to insert. */
665 if (pos < string->len)
666 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
668 /* Move the source part before the gap, if any. */
671 precount = MIN (len, pos - offset);
672 memcpy (string->str + pos, val, precount);
675 /* Move the source part after the gap, if any. */
677 memcpy (string->str + pos + precount,
678 val + /* Already moved: */ precount + /* Space opened up: */ len,
683 g_string_maybe_expand (string, len);
685 /* If we aren't appending at the end, move a hunk
686 * of the old string to the end, opening up space
688 if (pos < string->len)
689 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
691 /* insert the new string */
693 string->str[pos] = *val;
695 memcpy (string->str + pos, val, len);
700 string->str[string->len] = 0;
707 * @string: a #GString.
708 * @val: the string to append onto the end of the #GString.
710 * Adds a string onto the end of a #GString, expanding it if necessary.
712 * Returns: the #GString.
715 g_string_append (GString *string,
718 g_return_val_if_fail (string != NULL, NULL);
719 g_return_val_if_fail (val != NULL, string);
721 return g_string_insert_len (string, -1, val, -1);
725 * g_string_append_len:
726 * @string: a #GString
727 * @val: bytes to append
728 * @len: number of bytes of @val to use.
730 * Appends @len bytes of @val to @string.
731 * Because @len is provided, @val may contain
732 * embedded nuls and need not be nul-terminated.
734 * Returns: the #GString
737 g_string_append_len (GString *string,
741 g_return_val_if_fail (string != NULL, NULL);
742 g_return_val_if_fail (val != NULL, string);
744 return g_string_insert_len (string, -1, val, len);
749 * @string: a #GString.
750 * @c: the byte to append onto the end of the #GString.
752 * Adds a byte onto the end of a #GString, expanding it if necessary.
754 * Returns: the #GString.
756 #undef g_string_append_c
758 g_string_append_c (GString *string,
761 g_return_val_if_fail (string != NULL, NULL);
763 return g_string_insert_c (string, -1, c);
767 * g_string_append_unichar:
768 * @string: a #GString
769 * @wc: a Unicode character
771 * Converts a Unicode character into UTF-8, and appends it
774 * Return value: @string
777 g_string_append_unichar (GString *string,
780 g_return_val_if_fail (string != NULL, NULL);
782 return g_string_insert_unichar (string, -1, wc);
787 * @string: a #GString
788 * @val: the string to prepend on the start of the #GString
790 * Adds a string on to the start of a #GString,
791 * expanding it if necessary.
793 * Returns: the #GString
796 g_string_prepend (GString *string,
799 g_return_val_if_fail (string != NULL, NULL);
800 g_return_val_if_fail (val != NULL, string);
802 return g_string_insert_len (string, 0, val, -1);
806 * g_string_prepend_len:
807 * @string: a #GString
808 * @val: bytes to prepend
809 * @len: number of bytes in @val to prepend
811 * Prepends @len bytes of @val to @string.
812 * Because @len is provided, @val may contain
813 * embedded nuls and need not be nul-terminated.
815 * Returns: the #GString passed in
818 g_string_prepend_len (GString *string,
822 g_return_val_if_fail (string != NULL, NULL);
823 g_return_val_if_fail (val != NULL, string);
825 return g_string_insert_len (string, 0, val, len);
829 * g_string_prepend_c:
830 * @string: a #GString
831 * @c: the byte to prepend on the start of the #GString
833 * Adds a byte onto the start of a #GString,
834 * expanding it if necessary.
836 * Returns: the #GString
839 g_string_prepend_c (GString *string,
842 g_return_val_if_fail (string != NULL, NULL);
844 return g_string_insert_c (string, 0, c);
848 * g_string_prepend_unichar:
849 * @string: a #GString.
850 * @wc: a Unicode character.
852 * Converts a Unicode character into UTF-8, and prepends it
855 * Return value: @string.
858 g_string_prepend_unichar (GString *string,
861 g_return_val_if_fail (string != NULL, NULL);
863 return g_string_insert_unichar (string, 0, wc);
868 * @string: a #GString
869 * @pos: the position to insert the copy of the string
870 * @val: the string to insert
872 * Inserts a copy of a string into a #GString,
873 * expanding it if necessary.
875 * Returns: the #GString
878 g_string_insert (GString *string,
882 g_return_val_if_fail (string != NULL, NULL);
883 g_return_val_if_fail (val != NULL, string);
885 g_return_val_if_fail (pos <= string->len, string);
887 return g_string_insert_len (string, pos, val, -1);
892 * @string: a #GString
893 * @pos: the position to insert the byte
894 * @c: the byte to insert
896 * Inserts a byte into a #GString, expanding it if necessary.
898 * Returns: the #GString
901 g_string_insert_c (GString *string,
905 g_return_val_if_fail (string != NULL, NULL);
907 g_string_maybe_expand (string, 1);
912 g_return_val_if_fail (pos <= string->len, string);
914 /* If not just an append, move the old stuff */
915 if (pos < string->len)
916 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
918 string->str[pos] = c;
922 string->str[string->len] = 0;
928 * g_string_insert_unichar:
929 * @string: a #GString
930 * @pos: the position at which to insert character, or -1 to
931 * append at the end of the string.
932 * @wc: a Unicode character
934 * Converts a Unicode character into UTF-8, and insert it
935 * into the string at the given position.
937 * Return value: @string
940 g_string_insert_unichar (GString *string,
944 gint charlen, first, i;
947 g_return_val_if_fail (string != NULL, NULL);
949 /* Code copied from g_unichar_to_utf() */
960 else if (wc < 0x10000)
965 else if (wc < 0x200000)
970 else if (wc < 0x4000000)
980 /* End of copied code */
982 g_string_maybe_expand (string, charlen);
987 g_return_val_if_fail (pos <= string->len, string);
989 /* If not just an append, move the old stuff */
990 if (pos < string->len)
991 g_memmove (string->str + pos + charlen, string->str + pos, string->len - pos);
993 dest = string->str + pos;
994 /* Code copied from g_unichar_to_utf() */
995 for (i = charlen - 1; i > 0; --i)
997 dest[i] = (wc & 0x3f) | 0x80;
1000 dest[0] = wc | first;
1001 /* End of copied code */
1003 string->len += charlen;
1005 string->str[string->len] = 0;
1012 * @string: a #GString
1013 * @pos: the position of the content to remove
1014 * @len: the number of bytes to remove, or -1 to remove all
1017 * Removes @len bytes from a #GString, starting at position @pos.
1018 * The rest of the #GString is shifted down to fill the gap.
1020 * Returns: the #GString
1023 g_string_erase (GString *string,
1027 g_return_val_if_fail (string != NULL, NULL);
1028 g_return_val_if_fail (pos >= 0, string);
1029 g_return_val_if_fail (pos <= string->len, string);
1032 len = string->len - pos;
1035 g_return_val_if_fail (pos + len <= string->len, string);
1037 if (pos + len < string->len)
1038 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
1043 string->str[string->len] = 0;
1049 * g_string_ascii_down:
1050 * @string: a GString
1052 * Converts all upper case ASCII letters to lower case ASCII letters.
1054 * Return value: passed-in @string pointer, with all the upper case
1055 * characters converted to lower case in place, with
1056 * semantics that exactly match g_ascii_tolower.
1059 g_string_ascii_down (GString *string)
1064 g_return_val_if_fail (string != NULL, NULL);
1071 *s = g_ascii_tolower (*s);
1080 * g_string_ascii_up:
1081 * @string: a GString
1083 * Converts all lower case ASCII letters to upper case ASCII letters.
1085 * Return value: passed-in @string pointer, with all the lower case
1086 * characters converted to upper case in place, with
1087 * semantics that exactly match g_ascii_toupper.
1090 g_string_ascii_up (GString *string)
1095 g_return_val_if_fail (string != NULL, NULL);
1102 *s = g_ascii_toupper (*s);
1112 * @string: a #GString
1114 * Converts a #GString to lowercase.
1116 * Returns: the #GString.
1118 * Deprecated:2.2: This function uses the locale-specific tolower() function,
1119 * which is almost never the right thing. Use g_string_ascii_down() or
1120 * g_utf8_strdown() instead.
1123 g_string_down (GString *string)
1128 g_return_val_if_fail (string != NULL, NULL);
1131 s = (guchar *) string->str;
1146 * @string: a #GString
1148 * Converts a #GString to uppercase.
1150 * Return value: the #GString
1152 * Deprecated:2.2: This function uses the locale-specific toupper() function,
1153 * which is almost never the right thing. Use g_string_ascii_up() or
1154 * g_utf8_strup() instead.
1157 g_string_up (GString *string)
1162 g_return_val_if_fail (string != NULL, NULL);
1165 s = (guchar *) string->str;
1179 g_string_append_printf_internal (GString *string,
1186 length = g_vasprintf (&buffer, fmt, args);
1187 g_string_append_len (string, buffer, length);
1193 * @string: a #GString.
1194 * @format: the string format. See the sprintf() documentation.
1195 * @Varargs: the parameters to insert into the format string.
1197 * Writes a formatted string into a #GString.
1198 * This is similar to the standard sprintf() function,
1199 * except that the #GString buffer automatically expands
1200 * to contain the results. The previous contents of the
1201 * #GString are destroyed.
1203 * Deprecated: This function has been renamed to g_string_printf().
1208 * @string: a #GString.
1209 * @format: the string format. See the printf() documentation.
1210 * @Varargs: the parameters to insert into the format string.
1212 * Writes a formatted string into a #GString.
1213 * This is similar to the standard sprintf() function,
1214 * except that the #GString buffer automatically expands
1215 * to contain the results. The previous contents of the
1216 * #GString are destroyed.
1219 g_string_printf (GString *string,
1225 g_string_truncate (string, 0);
1227 va_start (args, fmt);
1228 g_string_append_printf_internal (string, fmt, args);
1233 * g_string_sprintfa:
1234 * @string: a #GString.
1235 * @format: the string format. See the sprintf() documentation.
1236 * @Varargs: the parameters to insert into the format string.
1238 * Appends a formatted string onto the end of a #GString.
1239 * This function is is similar to g_string_sprintf() except that
1240 * the text is appended to the #GString.
1242 * Deprecated: This function has been renamed to
1243 * g_string_append_printf().
1247 * g_string_append_printf:
1248 * @string: a #GString.
1249 * @format: the string format. See the printf() documentation.
1250 * @Varargs: the parameters to insert into the format string.
1252 * Appends a formatted string onto the end of a #GString.
1253 * This function is is similar to g_string_printf() except
1254 * that the text is appended to the #GString.
1257 g_string_append_printf (GString *string,
1263 va_start (args, fmt);
1264 g_string_append_printf_internal (string, fmt, args);
1268 #define __G_STRING_C__
1269 #include "galiasdef.c"