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 */
514 string->str[pos] = *val;
516 memcpy (string->str + pos, val, len);
521 string->str[string->len] = 0;
527 g_string_append (GString *string,
530 g_return_val_if_fail (string != NULL, NULL);
531 g_return_val_if_fail (val != NULL, string);
533 return g_string_insert_len (string, -1, val, -1);
537 g_string_append_len (GString *string,
541 g_return_val_if_fail (string != NULL, NULL);
542 g_return_val_if_fail (val != NULL, string);
544 return g_string_insert_len (string, -1, val, len);
547 #undef g_string_append_c
549 g_string_append_c (GString *string,
552 g_return_val_if_fail (string != NULL, NULL);
554 return g_string_insert_c (string, -1, c);
558 * g_string_append_unichar:
559 * @string: a #GString
560 * @wc: a Unicode character
562 * Converts a Unicode character into UTF-8, and appends it
565 * Return value: @string
568 g_string_append_unichar (GString *string,
571 g_return_val_if_fail (string != NULL, NULL);
573 return g_string_insert_unichar (string, -1, wc);
577 g_string_prepend (GString *string,
580 g_return_val_if_fail (string != NULL, NULL);
581 g_return_val_if_fail (val != NULL, string);
583 return g_string_insert_len (string, 0, val, -1);
587 g_string_prepend_len (GString *string,
591 g_return_val_if_fail (string != NULL, NULL);
592 g_return_val_if_fail (val != NULL, string);
594 return g_string_insert_len (string, 0, val, len);
598 g_string_prepend_c (GString *string,
601 g_return_val_if_fail (string != NULL, NULL);
603 return g_string_insert_c (string, 0, c);
607 * g_string_prepend_unichar:
608 * @string: a #GString.
609 * @wc: a Unicode character.
611 * Converts a Unicode character into UTF-8, and prepends it
614 * Return value: @string.
617 g_string_prepend_unichar (GString *string,
620 g_return_val_if_fail (string != NULL, NULL);
622 return g_string_insert_unichar (string, 0, wc);
626 g_string_insert (GString *string,
630 g_return_val_if_fail (string != NULL, NULL);
631 g_return_val_if_fail (val != NULL, string);
633 g_return_val_if_fail (pos <= string->len, string);
635 return g_string_insert_len (string, pos, val, -1);
639 g_string_insert_c (GString *string,
643 g_return_val_if_fail (string != NULL, NULL);
645 g_string_maybe_expand (string, 1);
650 g_return_val_if_fail (pos <= string->len, string);
652 /* If not just an append, move the old stuff */
653 if (pos < string->len)
654 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
656 string->str[pos] = c;
660 string->str[string->len] = 0;
666 * g_string_insert_unichar:
667 * @string: a #GString
668 * @pos: the position at which to insert character, or -1 to
669 * append at the end of the string.
670 * @wc: a Unicode character
672 * Converts a Unicode character into UTF-8, and insert it
673 * into the string at the given position.
675 * Return value: @string
678 g_string_insert_unichar (GString *string,
682 gint charlen, first, i;
685 g_return_val_if_fail (string != NULL, NULL);
687 /* Code copied from g_unichar_to_utf() */
698 else if (wc < 0x10000)
703 else if (wc < 0x200000)
708 else if (wc < 0x4000000)
718 /* End of copied code */
720 g_string_maybe_expand (string, charlen);
725 g_return_val_if_fail (pos <= string->len, string);
727 /* If not just an append, move the old stuff */
728 if (pos < string->len)
729 g_memmove (string->str + pos + charlen, string->str + pos, string->len - pos);
731 dest = string->str + pos;
732 /* Code copied from g_unichar_to_utf() */
733 for (i = charlen - 1; i > 0; --i)
735 dest[i] = (wc & 0x3f) | 0x80;
738 dest[0] = wc | first;
739 /* End of copied code */
741 string->len += charlen;
743 string->str[string->len] = 0;
749 g_string_erase (GString *string,
753 g_return_val_if_fail (string != NULL, NULL);
754 g_return_val_if_fail (pos >= 0, string);
755 g_return_val_if_fail (pos <= string->len, string);
758 len = string->len - pos;
761 g_return_val_if_fail (pos + len <= string->len, string);
763 if (pos + len < string->len)
764 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
769 string->str[string->len] = 0;
775 * g_string_ascii_down:
778 * Converts all upper case ASCII letters to lower case ASCII letters.
780 * Return value: passed-in @string pointer, with all the upper case
781 * characters converted to lower case in place, with
782 * semantics that exactly match g_ascii_tolower.
785 g_string_ascii_down (GString *string)
790 g_return_val_if_fail (string != NULL, NULL);
797 *s = g_ascii_tolower (*s);
809 * Converts all lower case ASCII letters to upper case ASCII letters.
811 * Return value: passed-in @string pointer, with all the lower case
812 * characters converted to upper case in place, with
813 * semantics that exactly match g_ascii_toupper.
816 g_string_ascii_up (GString *string)
821 g_return_val_if_fail (string != NULL, NULL);
828 *s = g_ascii_toupper (*s);
838 * @string: a #GString
840 * Converts a #GString to lowercase.
842 * Returns: the #GString.
844 * Deprecated: This function uses the locale-specific tolower() function,
845 * which is almost never the right thing. Use g_string_ascii_down() or
846 * g_utf8_strdown() instead.
849 g_string_down (GString *string)
854 g_return_val_if_fail (string != NULL, NULL);
857 s = (guchar *) string->str;
872 * @string: a #GString
874 * Converts a #GString to uppercase.
876 * Return value: the #GString
878 * Deprecated: This function uses the locale-specific toupper() function,
879 * which is almost never the right thing. Use g_string_ascii_up() or
880 * g_utf8_strup() instead.
883 g_string_up (GString *string)
888 g_return_val_if_fail (string != NULL, NULL);
891 s = (guchar *) string->str;
905 g_string_append_printf_internal (GString *string,
912 length = g_vasprintf (&buffer, fmt, args);
913 g_string_append_len (string, buffer, length);
918 g_string_printf (GString *string,
924 g_string_truncate (string, 0);
926 va_start (args, fmt);
927 g_string_append_printf_internal (string, fmt, args);
932 g_string_append_printf (GString *string,
938 va_start (args, fmt);
939 g_string_append_printf_internal (string, fmt, args);
943 #define __G_STRING_C__
944 #include "galiasdef.c"