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;
56 G_LOCK_DEFINE_STATIC (string_mem_chunk);
57 static GMemChunk *string_mem_chunk = NULL;
65 * @v2: a key to compare with @v1.
67 * Compares two strings and returns %TRUE if they are equal.
68 * It can be passed to g_hash_table_new() as the @key_equal_func
69 * parameter, when using strings as keys in a #GHashTable.
71 * Returns: %TRUE if the two keys match.
74 g_str_equal (gconstpointer v1,
77 const gchar *string1 = v1;
78 const gchar *string2 = v2;
80 return strcmp (string1, string2) == 0;
87 * Converts a string to a hash value.
88 * It can be passed to g_hash_table_new() as the @hash_func parameter,
89 * when using strings as keys in a #GHashTable.
91 * Returns: a hash value corresponding to the key.
94 g_str_hash (gconstpointer v)
96 /* 31 bit hash function */
97 const signed char *p = v;
101 for (p += 1; *p != '\0'; p++)
102 h = (h << 5) - h + *p;
107 #define MY_MAXSIZE ((gsize)-1)
110 nearest_power (gsize base, gsize num)
112 if (num > MY_MAXSIZE / 2)
131 g_string_chunk_new (gsize default_size)
133 GStringChunk *new_chunk = g_new (GStringChunk, 1);
136 size = nearest_power (1, default_size);
138 new_chunk->const_table = NULL;
139 new_chunk->storage_list = NULL;
140 new_chunk->storage_next = size;
141 new_chunk->default_size = size;
142 new_chunk->this_size = size;
148 g_string_chunk_free (GStringChunk *chunk)
152 g_return_if_fail (chunk != NULL);
154 if (chunk->storage_list)
156 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
157 g_free (tmp_list->data);
159 g_slist_free (chunk->storage_list);
162 if (chunk->const_table)
163 g_hash_table_destroy (chunk->const_table);
169 g_string_chunk_insert (GStringChunk *chunk,
172 g_return_val_if_fail (chunk != NULL, NULL);
174 return g_string_chunk_insert_len (chunk, string, -1);
178 g_string_chunk_insert_const (GStringChunk *chunk,
183 g_return_val_if_fail (chunk != NULL, NULL);
185 if (!chunk->const_table)
186 chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
188 lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
192 lookup = g_string_chunk_insert (chunk, string);
193 g_hash_table_insert (chunk->const_table, lookup, lookup);
200 * g_string_chunk_insert_len:
201 * @chunk: a #GStringChunk
202 * @string: bytes to insert
203 * @len: number of bytes of @string to insert, or -1 to insert a
204 * nul-terminated string.
206 * Adds a copy of the first @len bytes of @string to the #GStringChunk. The
207 * copy is nul-terminated.
209 * The characters in the string can be changed, if necessary, though you
210 * should not change anything after the end of the string.
212 * Return value: a pointer to the copy of @string within the #GStringChunk
217 g_string_chunk_insert_len (GStringChunk *chunk,
224 g_return_val_if_fail (chunk != NULL, NULL);
227 size = strlen (string);
231 if ((chunk->storage_next + size + 1) > chunk->this_size)
233 gsize new_size = nearest_power (chunk->default_size, size + 1);
235 chunk->storage_list = g_slist_prepend (chunk->storage_list,
236 g_new (gchar, new_size));
238 chunk->this_size = new_size;
239 chunk->storage_next = 0;
242 pos = ((gchar *) chunk->storage_list->data) + chunk->storage_next;
244 *(pos + size) = '\0';
246 strncpy (pos, string, size);
250 chunk->storage_next += size + 1;
258 g_string_maybe_expand (GString* string,
261 if (string->len + len >= string->allocated_len)
263 string->allocated_len = nearest_power (1, string->len + len + 1);
264 string->str = g_realloc (string->str, string->allocated_len);
269 g_string_sized_new (gsize dfl_size)
273 G_LOCK (string_mem_chunk);
274 if (!string_mem_chunk)
275 string_mem_chunk = g_mem_chunk_new ("string mem chunk",
277 1024, G_ALLOC_AND_FREE);
279 string = g_chunk_new (GString, string_mem_chunk);
280 G_UNLOCK (string_mem_chunk);
282 string->allocated_len = 0;
286 g_string_maybe_expand (string, MAX (dfl_size, 2));
293 g_string_new (const gchar *init)
297 if (init == NULL || *init == '\0')
298 string = g_string_sized_new (2);
304 string = g_string_sized_new (len + 2);
306 g_string_append_len (string, init, len);
313 g_string_new_len (const gchar *init,
319 return g_string_new (init);
322 string = g_string_sized_new (len);
325 g_string_append_len (string, init, len);
332 g_string_free (GString *string,
333 gboolean free_segment)
337 g_return_val_if_fail (string != NULL, NULL);
341 g_free (string->str);
345 segment = string->str;
347 G_LOCK (string_mem_chunk);
348 g_mem_chunk_free (string_mem_chunk, string);
349 G_UNLOCK (string_mem_chunk);
355 g_string_equal (const GString *v,
359 GString *string1 = (GString *) v;
360 GString *string2 = (GString *) v2;
361 gsize i = string1->len;
363 if (i != string2->len)
379 /* 31 bit hash function */
381 g_string_hash (const GString *str)
383 const gchar *p = str->str;
389 h = (h << 5) - h + *p;
397 g_string_assign (GString *string,
400 g_return_val_if_fail (string != NULL, NULL);
401 g_return_val_if_fail (rval != NULL, string);
403 /* Make sure assigning to itself doesn't corrupt the string. */
404 if (string->str != rval)
406 /* Assigning from substring should be ok since g_string_truncate
408 g_string_truncate (string, 0);
409 g_string_append (string, rval);
416 g_string_truncate (GString *string,
419 g_return_val_if_fail (string != NULL, NULL);
421 string->len = MIN (len, string->len);
422 string->str[string->len] = 0;
429 * @string: a #GString
430 * @len: the new length
432 * Sets the length of a #GString. If the length is less than
433 * the current length, the string will be truncated. If the
434 * length is greater than the current length, the contents
435 * of the newly added area are undefined. (However, as
436 * always, string->str[string->len] will be a nul byte.)
438 * Return value: @string
441 g_string_set_size (GString *string,
444 g_return_val_if_fail (string != NULL, NULL);
446 if (len >= string->allocated_len)
447 g_string_maybe_expand (string, len - string->len);
450 string->str[len] = 0;
456 g_string_insert_len (GString *string,
461 g_return_val_if_fail (string != NULL, NULL);
462 g_return_val_if_fail (val != NULL, string);
470 g_return_val_if_fail (pos <= string->len, string);
472 /* Check whether val represents a substring of string. This test
473 probably violates chapter and verse of the C standards, since
474 ">=" and "<=" are only valid when val really is a substring.
475 In practice, it will work on modern archs. */
476 if (val >= string->str && val <= string->str + string->len)
478 gsize offset = val - string->str;
481 g_string_maybe_expand (string, len);
482 val = string->str + offset;
483 /* At this point, val is valid again. */
485 /* Open up space where we are going to insert. */
486 if (pos < string->len)
487 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
489 /* Move the source part before the gap, if any. */
492 precount = MIN (len, pos - offset);
493 memcpy (string->str + pos, val, precount);
496 /* Move the source part after the gap, if any. */
498 memcpy (string->str + pos + precount,
499 val + /* Already moved: */ precount + /* Space opened up: */ len,
504 g_string_maybe_expand (string, len);
506 /* If we aren't appending at the end, move a hunk
507 * of the old string to the end, opening up space
509 if (pos < string->len)
510 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
512 /* insert the new string */
513 memcpy (string->str + pos, val, len);
518 string->str[string->len] = 0;
524 g_string_append (GString *string,
527 g_return_val_if_fail (string != NULL, NULL);
528 g_return_val_if_fail (val != NULL, string);
530 return g_string_insert_len (string, -1, val, -1);
534 g_string_append_len (GString *string,
538 g_return_val_if_fail (string != NULL, NULL);
539 g_return_val_if_fail (val != NULL, string);
541 return g_string_insert_len (string, -1, val, len);
544 #undef g_string_append_c
546 g_string_append_c (GString *string,
549 g_return_val_if_fail (string != NULL, NULL);
551 return g_string_insert_c (string, -1, c);
555 * g_string_append_unichar:
556 * @string: a #GString
557 * @wc: a Unicode character
559 * Converts a Unicode character into UTF-8, and appends it
562 * Return value: @string
565 g_string_append_unichar (GString *string,
568 g_return_val_if_fail (string != NULL, NULL);
570 return g_string_insert_unichar (string, -1, wc);
574 g_string_prepend (GString *string,
577 g_return_val_if_fail (string != NULL, NULL);
578 g_return_val_if_fail (val != NULL, string);
580 return g_string_insert_len (string, 0, val, -1);
584 g_string_prepend_len (GString *string,
588 g_return_val_if_fail (string != NULL, NULL);
589 g_return_val_if_fail (val != NULL, string);
591 return g_string_insert_len (string, 0, val, len);
595 g_string_prepend_c (GString *string,
598 g_return_val_if_fail (string != NULL, NULL);
600 return g_string_insert_c (string, 0, c);
604 * g_string_prepend_unichar:
605 * @string: a #GString.
606 * @wc: a Unicode character.
608 * Converts a Unicode character into UTF-8, and prepends it
611 * Return value: @string.
614 g_string_prepend_unichar (GString *string,
617 g_return_val_if_fail (string != NULL, NULL);
619 return g_string_insert_unichar (string, 0, wc);
623 g_string_insert (GString *string,
627 g_return_val_if_fail (string != NULL, NULL);
628 g_return_val_if_fail (val != NULL, string);
630 g_return_val_if_fail (pos <= string->len, string);
632 return g_string_insert_len (string, pos, val, -1);
636 g_string_insert_c (GString *string,
640 g_return_val_if_fail (string != NULL, NULL);
642 g_string_maybe_expand (string, 1);
647 g_return_val_if_fail (pos <= string->len, string);
649 /* If not just an append, move the old stuff */
650 if (pos < string->len)
651 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
653 string->str[pos] = c;
657 string->str[string->len] = 0;
663 * g_string_insert_unichar:
664 * @string: a #GString
665 * @pos: the position at which to insert character, or -1 to
666 * append at the end of the string.
667 * @wc: a Unicode character
669 * Converts a Unicode character into UTF-8, and insert it
670 * into the string at the given position.
672 * Return value: @string
675 g_string_insert_unichar (GString *string,
682 /* We could be somewhat more efficient here by computing
683 * the length, adding the space, then converting into that
684 * space, by cut-and-pasting the internals of g_unichar_to_utf8.
686 g_return_val_if_fail (string != NULL, NULL);
688 charlen = g_unichar_to_utf8 (wc, buf);
689 return g_string_insert_len (string, pos, buf, charlen);
693 g_string_erase (GString *string,
697 g_return_val_if_fail (string != NULL, NULL);
698 g_return_val_if_fail (pos >= 0, string);
699 g_return_val_if_fail (pos <= string->len, string);
702 len = string->len - pos;
705 g_return_val_if_fail (pos + len <= string->len, string);
707 if (pos + len < string->len)
708 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
713 string->str[string->len] = 0;
719 * g_string_ascii_down:
722 * Converts all upper case ASCII letters to lower case ASCII letters.
724 * Return value: passed-in @string pointer, with all the upper case
725 * characters converted to lower case in place, with
726 * semantics that exactly match g_ascii_tolower.
729 g_string_ascii_down (GString *string)
734 g_return_val_if_fail (string != NULL, NULL);
741 *s = g_ascii_tolower (*s);
753 * Converts all lower case ASCII letters to upper case ASCII letters.
755 * Return value: passed-in @string pointer, with all the lower case
756 * characters converted to upper case in place, with
757 * semantics that exactly match g_ascii_toupper.
760 g_string_ascii_up (GString *string)
765 g_return_val_if_fail (string != NULL, NULL);
772 *s = g_ascii_toupper (*s);
782 * @string: a #GString
784 * Converts a #GString to lowercase.
786 * Returns: the #GString.
788 * Deprecated: This function uses the locale-specific tolower() function,
789 * which is almost never the right thing. Use g_string_ascii_down() or
790 * g_utf8_strdown() instead.
793 g_string_down (GString *string)
798 g_return_val_if_fail (string != NULL, NULL);
801 s = (guchar *) string->str;
816 * @string: a #GString
818 * Converts a #GString to uppercase.
820 * Return value: the #GString
822 * Deprecated: This function uses the locale-specific toupper() function,
823 * which is almost never the right thing. Use g_string_ascii_up() or
824 * g_utf8_strup() instead.
827 g_string_up (GString *string)
832 g_return_val_if_fail (string != NULL, NULL);
835 s = (guchar *) string->str;
849 g_string_append_printf_internal (GString *string,
856 length = g_vasprintf (&buffer, fmt, args);
857 g_string_append_len (string, buffer, length);
862 g_string_printf (GString *string,
868 g_string_truncate (string, 0);
870 va_start (args, fmt);
871 g_string_append_printf_internal (string, fmt, args);
876 g_string_append_printf (GString *string,
882 va_start (args, fmt);
883 g_string_append_printf_internal (string, fmt, args);
887 #define __G_STRING_C__
888 #include "galiasdef.c"