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 (gsize default_size)
130 GStringChunk *new_chunk = g_new (GStringChunk, 1);
133 size = nearest_power (1, default_size);
135 new_chunk->const_table = NULL;
136 new_chunk->storage_list = NULL;
137 new_chunk->storage_next = size;
138 new_chunk->default_size = size;
139 new_chunk->this_size = size;
145 g_string_chunk_free (GStringChunk *chunk)
149 g_return_if_fail (chunk != NULL);
151 if (chunk->storage_list)
153 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
154 g_free (tmp_list->data);
156 g_slist_free (chunk->storage_list);
159 if (chunk->const_table)
160 g_hash_table_destroy (chunk->const_table);
166 * g_string_chunk_clear:
167 * @chunk: a #GStringChunk
169 * Frees all strings contained within the #GStringChunk.
170 * After calling g_string_chunk_clear() it is not safe to
171 * access any of the strings which were contained within it.
176 g_string_chunk_clear (GStringChunk *chunk)
180 g_return_if_fail (chunk != NULL);
182 if (chunk->storage_list)
184 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
185 g_free (tmp_list->data);
187 g_slist_free (chunk->storage_list);
189 chunk->storage_list = NULL;
190 chunk->storage_next = chunk->default_size;
191 chunk->this_size = chunk->default_size;
194 if (chunk->const_table)
195 g_hash_table_remove_all (chunk->const_table);
199 g_string_chunk_insert (GStringChunk *chunk,
202 g_return_val_if_fail (chunk != NULL, NULL);
204 return g_string_chunk_insert_len (chunk, string, -1);
208 g_string_chunk_insert_const (GStringChunk *chunk,
213 g_return_val_if_fail (chunk != NULL, NULL);
215 if (!chunk->const_table)
216 chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
218 lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
222 lookup = g_string_chunk_insert (chunk, string);
223 g_hash_table_insert (chunk->const_table, lookup, lookup);
230 * g_string_chunk_insert_len:
231 * @chunk: a #GStringChunk
232 * @string: bytes to insert
233 * @len: number of bytes of @string to insert, or -1 to insert a
234 * nul-terminated string.
236 * Adds a copy of the first @len bytes of @string to the #GStringChunk. The
237 * copy is nul-terminated.
239 * The characters in the string can be changed, if necessary, though you
240 * should not change anything after the end of the string.
242 * Return value: a pointer to the copy of @string within the #GStringChunk
247 g_string_chunk_insert_len (GStringChunk *chunk,
254 g_return_val_if_fail (chunk != NULL, NULL);
257 size = strlen (string);
261 if ((chunk->storage_next + size + 1) > chunk->this_size)
263 gsize new_size = nearest_power (chunk->default_size, size + 1);
265 chunk->storage_list = g_slist_prepend (chunk->storage_list,
266 g_new (gchar, new_size));
268 chunk->this_size = new_size;
269 chunk->storage_next = 0;
272 pos = ((gchar *) chunk->storage_list->data) + chunk->storage_next;
274 *(pos + size) = '\0';
276 strncpy (pos, string, size);
280 chunk->storage_next += size + 1;
288 g_string_maybe_expand (GString* string,
291 if (string->len + len >= string->allocated_len)
293 string->allocated_len = nearest_power (1, string->len + len + 1);
294 string->str = g_realloc (string->str, string->allocated_len);
299 * g_string_sized_new:
300 * @dfl_size: the default size of the space allocated to
303 * Creates a new #GString, with enough space for @dfl_size
304 * bytes. This is useful if you are going to add a lot of
305 * text to the string and don't want it to be reallocated
308 * Returns: the new #GString.
311 g_string_sized_new (gsize dfl_size)
313 GString *string = g_slice_new (GString);
315 string->allocated_len = 0;
319 g_string_maybe_expand (string, MAX (dfl_size, 2));
327 * @init: the initial text to copy into the string.
329 * Creates a new #GString, initialized with the given string.
331 * Returns: the new #GString.
334 g_string_new (const gchar *init)
338 if (init == NULL || *init == '\0')
339 string = g_string_sized_new (2);
345 string = g_string_sized_new (len + 2);
347 g_string_append_len (string, init, len);
355 * @init: initial contents of string.
356 * @len: length of @init to use.
358 * Creates a new #GString with @len bytes of the
359 * @init buffer. Because a length is provided, @init
360 * need not be nul-terminated, and can contain embedded
363 * Returns: a new #GString.
366 g_string_new_len (const gchar *init,
372 return g_string_new (init);
375 string = g_string_sized_new (len);
378 g_string_append_len (string, init, len);
386 * @string: a #GString
387 * @free_segment: if %TRUE the actual character data is freed as well
389 * Frees the memory allocated for the #GString.
390 * If @free_segment is %TRUE it also frees the character data.
392 * Returns: the character data of @string
393 * (i.e. %NULL if @free_segment is %TRUE)
396 g_string_free (GString *string,
397 gboolean free_segment)
401 g_return_val_if_fail (string != NULL, NULL);
405 g_free (string->str);
409 segment = string->str;
411 g_slice_free (GString, string);
419 * @v2: another #GString
421 * Compares two strings for equality, returning %TRUE if they are equal.
422 * For use with #GHashTable.
424 * Returns: %TRUE if they strings are the same
425 * length and contain the same bytes.
428 g_string_equal (const GString *v,
432 GString *string1 = (GString *) v;
433 GString *string2 = (GString *) v2;
434 gsize i = string1->len;
436 if (i != string2->len)
454 * @str: a string to hash
456 * Creates a hash code for @str; for use with #GHashTable.
458 * Returns: hash code for @str
460 /* 31 bit hash function */
462 g_string_hash (const GString *str)
464 const gchar *p = str->str;
470 h = (h << 5) - h + *p;
479 * @string: the destination #GString. Its current contents
481 * @rval: the string to copy into @string
483 * Copies the bytes from a string into a #GString,
484 * destroying any previous contents. It is rather like
485 * the standard strcpy() function, except that you do not
486 * have to worry about having enough space to copy the string.
488 * Returns: the destination #GString.
491 g_string_assign (GString *string,
494 g_return_val_if_fail (string != NULL, NULL);
495 g_return_val_if_fail (rval != NULL, string);
497 /* Make sure assigning to itself doesn't corrupt the string. */
498 if (string->str != rval)
500 /* Assigning from substring should be ok since g_string_truncate
502 g_string_truncate (string, 0);
503 g_string_append (string, rval);
511 * @string: a #GString
512 * @len: the new size of the #GString
514 * Cuts off the end of the GString, leaving the first @len bytes.
516 * Returns: the #GString
519 g_string_truncate (GString *string,
522 g_return_val_if_fail (string != NULL, NULL);
524 string->len = MIN (len, string->len);
525 string->str[string->len] = 0;
532 * @string: a #GString
533 * @len: the new length
535 * Sets the length of a #GString. If the length is less than
536 * the current length, the string will be truncated. If the
537 * length is greater than the current length, the contents
538 * of the newly added area are undefined. (However, as
539 * always, string->str[string->len] will be a nul byte.)
541 * Return value: @string
544 g_string_set_size (GString *string,
547 g_return_val_if_fail (string != NULL, NULL);
549 if (len >= string->allocated_len)
550 g_string_maybe_expand (string, len - string->len);
553 string->str[len] = 0;
559 * g_string_insert_len:
560 * @string: a #GString
561 * @pos: position in @string where insertion should
562 * happen, or -1 for at the end
563 * @val: bytes to insert
564 * @len: number of bytes of @val to insert
566 * Inserts @len bytes of @val into @string at @pos.
567 * Because @len is provided, @val may contain embedded
568 * nuls and need not be nul-terminated. If @pos is -1,
569 * bytes are inserted at the end of the string.
571 * Returns: the #GString
574 g_string_insert_len (GString *string,
579 g_return_val_if_fail (string != NULL, NULL);
580 g_return_val_if_fail (val != NULL, string);
588 g_return_val_if_fail (pos <= string->len, string);
590 /* Check whether val represents a substring of string. This test
591 probably violates chapter and verse of the C standards, since
592 ">=" and "<=" are only valid when val really is a substring.
593 In practice, it will work on modern archs. */
594 if (val >= string->str && val <= string->str + string->len)
596 gsize offset = val - string->str;
599 g_string_maybe_expand (string, len);
600 val = string->str + offset;
601 /* At this point, val is valid again. */
603 /* Open up space where we are going to insert. */
604 if (pos < string->len)
605 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
607 /* Move the source part before the gap, if any. */
610 precount = MIN (len, pos - offset);
611 memcpy (string->str + pos, val, precount);
614 /* Move the source part after the gap, if any. */
616 memcpy (string->str + pos + precount,
617 val + /* Already moved: */ precount + /* Space opened up: */ len,
622 g_string_maybe_expand (string, len);
624 /* If we aren't appending at the end, move a hunk
625 * of the old string to the end, opening up space
627 if (pos < string->len)
628 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
630 /* insert the new string */
632 string->str[pos] = *val;
634 memcpy (string->str + pos, val, len);
639 string->str[string->len] = 0;
646 * @string: a #GString.
647 * @val: the string to append onto the end of the #GString.
649 * Adds a string onto the end of a #GString, expanding it if necessary.
651 * Returns: the #GString.
654 g_string_append (GString *string,
657 g_return_val_if_fail (string != NULL, NULL);
658 g_return_val_if_fail (val != NULL, string);
660 return g_string_insert_len (string, -1, val, -1);
664 * g_string_append_len:
665 * @string: a #GString
666 * @val: bytes to append
667 * @len: number of bytes of @val to use.
669 * Appends @len bytes of @val to @string.
670 * Because @len is provided, @val may contain
671 * embedded nuls and need not be nul-terminated.
673 * Returns: the #GString
676 g_string_append_len (GString *string,
680 g_return_val_if_fail (string != NULL, NULL);
681 g_return_val_if_fail (val != NULL, string);
683 return g_string_insert_len (string, -1, val, len);
688 * @string: a #GString.
689 * @c: the byte to append onto the end of the #GString.
691 * Adds a byte onto the end of a #GString, expanding it if necessary.
693 * Returns: the #GString.
695 #undef g_string_append_c
697 g_string_append_c (GString *string,
700 g_return_val_if_fail (string != NULL, NULL);
702 return g_string_insert_c (string, -1, c);
706 * g_string_append_unichar:
707 * @string: a #GString
708 * @wc: a Unicode character
710 * Converts a Unicode character into UTF-8, and appends it
713 * Return value: @string
716 g_string_append_unichar (GString *string,
719 g_return_val_if_fail (string != NULL, NULL);
721 return g_string_insert_unichar (string, -1, wc);
726 * @string: a #GString
727 * @val: the string to prepend on the start of the #GString
729 * Adds a string on to the start of a #GString,
730 * expanding it if necessary.
732 * Returns: the #GString
735 g_string_prepend (GString *string,
738 g_return_val_if_fail (string != NULL, NULL);
739 g_return_val_if_fail (val != NULL, string);
741 return g_string_insert_len (string, 0, val, -1);
745 * g_string_prepend_len:
746 * @string: a #GString
747 * @val: bytes to prepend
748 * @len: number of bytes in @val to prepend
750 * Prepends @len bytes of @val to @string.
751 * Because @len is provided, @val may contain
752 * embedded nuls and need not be nul-terminated.
754 * Returns: the #GString passed in
757 g_string_prepend_len (GString *string,
761 g_return_val_if_fail (string != NULL, NULL);
762 g_return_val_if_fail (val != NULL, string);
764 return g_string_insert_len (string, 0, val, len);
768 * g_string_prepend_c:
769 * @string: a #GString
770 * @c: the byte to prepend on the start of the #GString
772 * Adds a byte onto the start of a #GString,
773 * expanding it if necessary.
775 * Returns: the #GString
778 g_string_prepend_c (GString *string,
781 g_return_val_if_fail (string != NULL, NULL);
783 return g_string_insert_c (string, 0, c);
787 * g_string_prepend_unichar:
788 * @string: a #GString.
789 * @wc: a Unicode character.
791 * Converts a Unicode character into UTF-8, and prepends it
794 * Return value: @string.
797 g_string_prepend_unichar (GString *string,
800 g_return_val_if_fail (string != NULL, NULL);
802 return g_string_insert_unichar (string, 0, wc);
807 * @string: a #GString
808 * @pos: the position to insert the copy of the string
809 * @val: the string to insert
811 * Inserts a copy of a string into a #GString,
812 * expanding it if necessary.
814 * Returns: the #GString
817 g_string_insert (GString *string,
821 g_return_val_if_fail (string != NULL, NULL);
822 g_return_val_if_fail (val != NULL, string);
824 g_return_val_if_fail (pos <= string->len, string);
826 return g_string_insert_len (string, pos, val, -1);
831 * @string: a #GString
832 * @pos: the position to insert the byte
833 * @c: the byte to insert
835 * Inserts a byte into a #GString, expanding it if necessary.
837 * Returns: the #GString
840 g_string_insert_c (GString *string,
844 g_return_val_if_fail (string != NULL, NULL);
846 g_string_maybe_expand (string, 1);
851 g_return_val_if_fail (pos <= string->len, string);
853 /* If not just an append, move the old stuff */
854 if (pos < string->len)
855 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
857 string->str[pos] = c;
861 string->str[string->len] = 0;
867 * g_string_insert_unichar:
868 * @string: a #GString
869 * @pos: the position at which to insert character, or -1 to
870 * append at the end of the string.
871 * @wc: a Unicode character
873 * Converts a Unicode character into UTF-8, and insert it
874 * into the string at the given position.
876 * Return value: @string
879 g_string_insert_unichar (GString *string,
883 gint charlen, first, i;
886 g_return_val_if_fail (string != NULL, NULL);
888 /* Code copied from g_unichar_to_utf() */
899 else if (wc < 0x10000)
904 else if (wc < 0x200000)
909 else if (wc < 0x4000000)
919 /* End of copied code */
921 g_string_maybe_expand (string, charlen);
926 g_return_val_if_fail (pos <= string->len, string);
928 /* If not just an append, move the old stuff */
929 if (pos < string->len)
930 g_memmove (string->str + pos + charlen, string->str + pos, string->len - pos);
932 dest = string->str + pos;
933 /* Code copied from g_unichar_to_utf() */
934 for (i = charlen - 1; i > 0; --i)
936 dest[i] = (wc & 0x3f) | 0x80;
939 dest[0] = wc | first;
940 /* End of copied code */
942 string->len += charlen;
944 string->str[string->len] = 0;
951 * @string: a #GString
952 * @pos: the position of the content to remove
953 * @len: the number of bytes to remove, or -1 to remove all
956 * Removes @len bytes from a #GString, starting at position @pos.
957 * The rest of the #GString is shifted down to fill the gap.
959 * Returns: the #GString
962 g_string_erase (GString *string,
966 g_return_val_if_fail (string != NULL, NULL);
967 g_return_val_if_fail (pos >= 0, string);
968 g_return_val_if_fail (pos <= string->len, string);
971 len = string->len - pos;
974 g_return_val_if_fail (pos + len <= string->len, string);
976 if (pos + len < string->len)
977 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
982 string->str[string->len] = 0;
988 * g_string_ascii_down:
991 * Converts all upper case ASCII letters to lower case ASCII letters.
993 * Return value: passed-in @string pointer, with all the upper case
994 * characters converted to lower case in place, with
995 * semantics that exactly match g_ascii_tolower.
998 g_string_ascii_down (GString *string)
1003 g_return_val_if_fail (string != NULL, NULL);
1010 *s = g_ascii_tolower (*s);
1019 * g_string_ascii_up:
1020 * @string: a GString
1022 * Converts all lower case ASCII letters to upper case ASCII letters.
1024 * Return value: passed-in @string pointer, with all the lower case
1025 * characters converted to upper case in place, with
1026 * semantics that exactly match g_ascii_toupper.
1029 g_string_ascii_up (GString *string)
1034 g_return_val_if_fail (string != NULL, NULL);
1041 *s = g_ascii_toupper (*s);
1051 * @string: a #GString
1053 * Converts a #GString to lowercase.
1055 * Returns: the #GString.
1057 * Deprecated:2.2: This function uses the locale-specific tolower() function,
1058 * which is almost never the right thing. Use g_string_ascii_down() or
1059 * g_utf8_strdown() instead.
1062 g_string_down (GString *string)
1067 g_return_val_if_fail (string != NULL, NULL);
1070 s = (guchar *) string->str;
1085 * @string: a #GString
1087 * Converts a #GString to uppercase.
1089 * Return value: the #GString
1091 * Deprecated:2.2: This function uses the locale-specific toupper() function,
1092 * which is almost never the right thing. Use g_string_ascii_up() or
1093 * g_utf8_strup() instead.
1096 g_string_up (GString *string)
1101 g_return_val_if_fail (string != NULL, NULL);
1104 s = (guchar *) string->str;
1118 g_string_append_printf_internal (GString *string,
1125 length = g_vasprintf (&buffer, fmt, args);
1126 g_string_append_len (string, buffer, length);
1132 * @string: a #GString.
1133 * @format: the string format. See the sprintf() documentation.
1134 * @Varargs: the parameters to insert into the format string.
1136 * Writes a formatted string into a #GString.
1137 * This is similar to the standard sprintf() function,
1138 * except that the #GString buffer automatically expands
1139 * to contain the results. The previous contents of the
1140 * #GString are destroyed.
1142 * Deprecated: This function has been renamed to g_string_printf().
1147 * @string: a #GString.
1148 * @format: the string format. See the printf() documentation.
1149 * @Varargs: the parameters to insert into the format string.
1151 * Writes a formatted string into a #GString.
1152 * This is similar to the standard sprintf() function,
1153 * except that the #GString buffer automatically expands
1154 * to contain the results. The previous contents of the
1155 * #GString are destroyed.
1158 g_string_printf (GString *string,
1164 g_string_truncate (string, 0);
1166 va_start (args, fmt);
1167 g_string_append_printf_internal (string, fmt, args);
1172 * g_string_sprintfa:
1173 * @string: a #GString.
1174 * @format: the string format. See the sprintf() documentation.
1175 * @Varargs: the parameters to insert into the format string.
1177 * Appends a formatted string onto the end of a #GString.
1178 * This function is is similar to g_string_sprintf() except that
1179 * the text is appended to the #GString.
1181 * Deprecated: This function has been renamed to
1182 * g_string_append_printf().
1186 * g_string_append_printf:
1187 * @string: a #GString.
1188 * @format: the string format. See the printf() documentation.
1189 * @Varargs: the parameters to insert into the format string.
1191 * Appends a formatted string onto the end of a #GString.
1192 * This function is is similar to g_string_printf() except
1193 * that the text is appended to the #GString.
1196 g_string_append_printf (GString *string,
1202 va_start (args, fmt);
1203 g_string_append_printf_internal (string, fmt, args);
1207 #define __G_STRING_C__
1208 #include "galiasdef.c"