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 */
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,
223 g_return_val_if_fail (chunk != NULL, NULL);
226 len = strlen (string);
228 if ((chunk->storage_next + len + 1) > chunk->this_size)
230 gsize new_size = nearest_power (chunk->default_size, len + 1);
232 chunk->storage_list = g_slist_prepend (chunk->storage_list,
233 g_new (gchar, new_size));
235 chunk->this_size = new_size;
236 chunk->storage_next = 0;
239 pos = ((gchar *) chunk->storage_list->data) + chunk->storage_next;
243 strncpy (pos, string, len);
246 chunk->storage_next += len + 1;
254 g_string_maybe_expand (GString* string,
257 if (string->len + len >= string->allocated_len)
259 string->allocated_len = nearest_power (1, string->len + len + 1);
260 string->str = g_realloc (string->str, string->allocated_len);
265 g_string_sized_new (gsize dfl_size)
269 G_LOCK (string_mem_chunk);
270 if (!string_mem_chunk)
271 string_mem_chunk = g_mem_chunk_new ("string mem chunk",
273 1024, G_ALLOC_AND_FREE);
275 string = g_chunk_new (GString, string_mem_chunk);
276 G_UNLOCK (string_mem_chunk);
278 string->allocated_len = 0;
282 g_string_maybe_expand (string, MAX (dfl_size, 2));
289 g_string_new (const gchar *init)
293 if (init == NULL || *init == '\0')
294 string = g_string_sized_new (2);
300 string = g_string_sized_new (len + 2);
302 g_string_append_len (string, init, len);
309 g_string_new_len (const gchar *init,
315 return g_string_new (init);
318 string = g_string_sized_new (len);
321 g_string_append_len (string, init, len);
328 g_string_free (GString *string,
329 gboolean free_segment)
333 g_return_val_if_fail (string != NULL, NULL);
337 g_free (string->str);
341 segment = string->str;
343 G_LOCK (string_mem_chunk);
344 g_mem_chunk_free (string_mem_chunk, string);
345 G_UNLOCK (string_mem_chunk);
351 g_string_equal (const GString *v,
355 GString *string1 = (GString *) v;
356 GString *string2 = (GString *) v2;
357 gsize i = string1->len;
359 if (i != string2->len)
375 /* 31 bit hash function */
377 g_string_hash (const GString *str)
379 const gchar *p = str->str;
385 h = (h << 5) - h + *p;
393 g_string_assign (GString *string,
396 g_return_val_if_fail (string != NULL, NULL);
397 g_return_val_if_fail (rval != NULL, string);
399 /* Make sure assigning to itself doesn't corrupt the string. */
400 if (string->str != rval)
402 /* Assigning from substring should be ok since g_string_truncate
404 g_string_truncate (string, 0);
405 g_string_append (string, rval);
412 g_string_truncate (GString *string,
415 g_return_val_if_fail (string != NULL, NULL);
417 string->len = MIN (len, string->len);
418 string->str[string->len] = 0;
425 * @string: a #GString
426 * @len: the new length
428 * Sets the length of a #GString. If the length is less than
429 * the current length, the string will be truncated. If the
430 * length is greater than the current length, the contents
431 * of the newly added area are undefined. (However, as
432 * always, string->str[string->len] will be a nul byte.)
434 * Return value: @string
437 g_string_set_size (GString *string,
440 g_return_val_if_fail (string != NULL, NULL);
442 if (len >= string->allocated_len)
443 g_string_maybe_expand (string, len - string->len);
446 string->str[len] = 0;
452 g_string_insert_len (GString *string,
457 g_return_val_if_fail (string != NULL, NULL);
458 g_return_val_if_fail (val != NULL, string);
466 g_return_val_if_fail (pos <= string->len, string);
468 /* Check whether val represents a substring of string. This test
469 probably violates chapter and verse of the C standards, since
470 ">=" and "<=" are only valid when val really is a substring.
471 In practice, it will work on modern archs. */
472 if (val >= string->str && val <= string->str + string->len)
474 gsize offset = val - string->str;
477 g_string_maybe_expand (string, len);
478 val = string->str + offset;
479 /* At this point, val is valid again. */
481 /* Open up space where we are going to insert. */
482 if (pos < string->len)
483 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
485 /* Move the source part before the gap, if any. */
488 precount = MIN (len, pos - offset);
489 memcpy (string->str + pos, val, precount);
492 /* Move the source part after the gap, if any. */
494 memcpy (string->str + pos + precount,
495 val + /* Already moved: */ precount + /* Space opened up: */ len,
500 g_string_maybe_expand (string, len);
502 /* If we aren't appending at the end, move a hunk
503 * of the old string to the end, opening up space
505 if (pos < string->len)
506 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
508 /* insert the new string */
509 memcpy (string->str + pos, val, len);
514 string->str[string->len] = 0;
520 g_string_append (GString *string,
523 g_return_val_if_fail (string != NULL, NULL);
524 g_return_val_if_fail (val != NULL, string);
526 return g_string_insert_len (string, -1, val, -1);
530 g_string_append_len (GString *string,
534 g_return_val_if_fail (string != NULL, NULL);
535 g_return_val_if_fail (val != NULL, string);
537 return g_string_insert_len (string, -1, val, len);
540 #undef g_string_append_c
542 g_string_append_c (GString *string,
545 g_return_val_if_fail (string != NULL, NULL);
547 return g_string_insert_c (string, -1, c);
551 * g_string_append_unichar:
552 * @string: a #GString
553 * @wc: a Unicode character
555 * Converts a Unicode character into UTF-8, and appends it
558 * Return value: @string
561 g_string_append_unichar (GString *string,
564 g_return_val_if_fail (string != NULL, NULL);
566 return g_string_insert_unichar (string, -1, wc);
570 g_string_prepend (GString *string,
573 g_return_val_if_fail (string != NULL, NULL);
574 g_return_val_if_fail (val != NULL, string);
576 return g_string_insert_len (string, 0, val, -1);
580 g_string_prepend_len (GString *string,
584 g_return_val_if_fail (string != NULL, NULL);
585 g_return_val_if_fail (val != NULL, string);
587 return g_string_insert_len (string, 0, val, len);
591 g_string_prepend_c (GString *string,
594 g_return_val_if_fail (string != NULL, NULL);
596 return g_string_insert_c (string, 0, c);
600 * g_string_prepend_unichar:
601 * @string: a #GString.
602 * @wc: a Unicode character.
604 * Converts a Unicode character into UTF-8, and prepends it
607 * Return value: @string.
610 g_string_prepend_unichar (GString *string,
613 g_return_val_if_fail (string != NULL, NULL);
615 return g_string_insert_unichar (string, 0, wc);
619 g_string_insert (GString *string,
623 g_return_val_if_fail (string != NULL, NULL);
624 g_return_val_if_fail (val != NULL, string);
626 g_return_val_if_fail (pos <= string->len, string);
628 return g_string_insert_len (string, pos, val, -1);
632 g_string_insert_c (GString *string,
636 g_return_val_if_fail (string != NULL, NULL);
638 g_string_maybe_expand (string, 1);
643 g_return_val_if_fail (pos <= string->len, string);
645 /* If not just an append, move the old stuff */
646 if (pos < string->len)
647 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
649 string->str[pos] = c;
653 string->str[string->len] = 0;
659 * g_string_insert_unichar:
660 * @string: a #GString
661 * @pos: the position at which to insert character, or -1 to
662 * append at the end of the string.
663 * @wc: a Unicode character
665 * Converts a Unicode character into UTF-8, and insert it
666 * into the string at the given position.
668 * Return value: @string
671 g_string_insert_unichar (GString *string,
678 /* We could be somewhat more efficient here by computing
679 * the length, adding the space, then converting into that
680 * space, by cut-and-pasting the internals of g_unichar_to_utf8.
682 g_return_val_if_fail (string != NULL, NULL);
684 charlen = g_unichar_to_utf8 (wc, buf);
685 return g_string_insert_len (string, pos, buf, charlen);
689 g_string_erase (GString *string,
693 g_return_val_if_fail (string != NULL, NULL);
694 g_return_val_if_fail (pos >= 0, string);
695 g_return_val_if_fail (pos <= string->len, string);
698 len = string->len - pos;
701 g_return_val_if_fail (pos + len <= string->len, string);
703 if (pos + len < string->len)
704 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
709 string->str[string->len] = 0;
715 * g_string_ascii_down:
718 * Converts all upper case ASCII letters to lower case ASCII letters.
720 * Return value: passed-in @string pointer, with all the upper case
721 * characters converted to lower case in place, with
722 * semantics that exactly match g_ascii_tolower.
725 g_string_ascii_down (GString *string)
730 g_return_val_if_fail (string != NULL, NULL);
737 *s = g_ascii_tolower (*s);
749 * Converts all lower case ASCII letters to upper case ASCII letters.
751 * Return value: passed-in @string pointer, with all the lower case
752 * characters converted to upper case in place, with
753 * semantics that exactly match g_ascii_toupper.
756 g_string_ascii_up (GString *string)
761 g_return_val_if_fail (string != NULL, NULL);
768 *s = g_ascii_toupper (*s);
778 * @string: a #GString
780 * Converts a #GString to lowercase.
782 * Returns: the #GString.
784 * Deprecated: This function uses the locale-specific tolower() function,
785 * which is almost never the right thing. Use g_string_ascii_down() or
786 * g_utf8_strdown() instead.
789 g_string_down (GString *string)
794 g_return_val_if_fail (string != NULL, NULL);
797 s = (guchar *) string->str;
812 * @string: a #GString
814 * Converts a #GString to uppercase.
816 * Return value: the #GString
818 * Deprecated: This function uses the locale-specific toupper() function,
819 * which is almost never the right thing. Use g_string_ascii_up() or
820 * g_utf8_strup() instead.
823 g_string_up (GString *string)
828 g_return_val_if_fail (string != NULL, NULL);
831 s = (guchar *) string->str;
845 g_string_append_printf_internal (GString *string,
852 length = g_vasprintf (&buffer, fmt, args);
853 g_string_append_len (string, buffer, length);
858 g_string_printf (GString *string,
864 g_string_truncate (string, 0);
866 va_start (args, fmt);
867 g_string_append_printf_internal (string, fmt, args);
872 g_string_append_printf (GString *string,
878 va_start (args, fmt);
879 g_string_append_printf_internal (string, fmt, args);
883 #define __G_STRING_C__
884 #include "galiasdef.c"