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/.
47 GHashTable *const_table;
54 G_LOCK_DEFINE_STATIC (string_mem_chunk);
55 static GMemChunk *string_mem_chunk = NULL;
61 g_str_equal (gconstpointer v1,
64 const gchar *string1 = v1;
65 const gchar *string2 = v2;
67 return strcmp (string1, string2) == 0;
70 /* 31 bit hash function */
72 g_str_hash (gconstpointer key)
78 for (p += 1; *p != '\0'; p++)
79 h = (h << 5) - h + *p;
84 #define MY_MAXSIZE ((gsize)-1)
87 nearest_power (gsize base, gsize num)
89 if (num > MY_MAXSIZE / 2)
108 g_string_chunk_new (gsize default_size)
110 GStringChunk *new_chunk = g_new (GStringChunk, 1);
113 size = nearest_power (1, default_size);
115 new_chunk->const_table = NULL;
116 new_chunk->storage_list = NULL;
117 new_chunk->storage_next = size;
118 new_chunk->default_size = size;
119 new_chunk->this_size = size;
125 g_string_chunk_free (GStringChunk *chunk)
129 g_return_if_fail (chunk != NULL);
131 if (chunk->storage_list)
133 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
134 g_free (tmp_list->data);
136 g_slist_free (chunk->storage_list);
139 if (chunk->const_table)
140 g_hash_table_destroy (chunk->const_table);
146 g_string_chunk_insert (GStringChunk *chunk,
149 gsize len = strlen (string);
152 g_return_val_if_fail (chunk != NULL, NULL);
154 if ((chunk->storage_next + len + 1) > chunk->this_size)
156 gsize new_size = nearest_power (chunk->default_size, len + 1);
158 chunk->storage_list = g_slist_prepend (chunk->storage_list,
159 g_new (char, new_size));
161 chunk->this_size = new_size;
162 chunk->storage_next = 0;
165 pos = ((char *) chunk->storage_list->data) + chunk->storage_next;
167 strcpy (pos, string);
169 chunk->storage_next += len + 1;
175 g_string_chunk_insert_const (GStringChunk *chunk,
180 g_return_val_if_fail (chunk != NULL, NULL);
182 if (!chunk->const_table)
183 chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
185 lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
189 lookup = g_string_chunk_insert (chunk, string);
190 g_hash_table_insert (chunk->const_table, lookup, lookup);
199 g_string_maybe_expand (GString* string,
202 if (string->len + len >= string->allocated_len)
204 string->allocated_len = nearest_power (1, string->len + len + 1);
205 string->str = g_realloc (string->str, string->allocated_len);
210 g_string_sized_new (gsize dfl_size)
214 G_LOCK (string_mem_chunk);
215 if (!string_mem_chunk)
216 string_mem_chunk = g_mem_chunk_new ("string mem chunk",
218 1024, G_ALLOC_AND_FREE);
220 string = g_chunk_new (GString, string_mem_chunk);
221 G_UNLOCK (string_mem_chunk);
223 string->allocated_len = 0;
227 g_string_maybe_expand (string, MAX (dfl_size, 2));
234 g_string_new (const gchar *init)
238 if (init == NULL || *init == '\0')
239 string = g_string_sized_new (2);
245 string = g_string_sized_new (len + 2);
247 g_string_append_len (string, init, len);
254 g_string_new_len (const gchar *init,
260 return g_string_new (init);
263 string = g_string_sized_new (len);
266 g_string_append_len (string, init, len);
273 g_string_free (GString *string,
274 gboolean free_segment)
278 g_return_val_if_fail (string != NULL, NULL);
282 g_free (string->str);
286 segment = string->str;
288 G_LOCK (string_mem_chunk);
289 g_mem_chunk_free (string_mem_chunk, string);
290 G_UNLOCK (string_mem_chunk);
296 g_string_equal (const GString *v,
300 GString *string1 = (GString *) v;
301 GString *string2 = (GString *) v2;
302 gsize i = string1->len;
304 if (i != string2->len)
320 /* 31 bit hash function */
322 g_string_hash (const GString *str)
324 const gchar *p = str->str;
330 h = (h << 5) - h + *p;
338 g_string_assign (GString *string,
341 g_return_val_if_fail (string != NULL, NULL);
342 g_return_val_if_fail (rval != NULL, string);
344 g_string_truncate (string, 0);
345 g_string_append (string, rval);
351 g_string_truncate (GString *string,
354 g_return_val_if_fail (string != NULL, NULL);
356 string->len = MIN (len, string->len);
357 string->str[string->len] = 0;
364 * @string: a #GString
365 * @len: the new length
367 * Sets the length of a #GString. If the length is less than
368 * the current length, the string will be truncated. If the
369 * length is greater than the current length, the contents
370 * of the newly added area are undefined. (However, as
371 * always, string->str[string->len] will be a nul byte.)
373 * Return value: @string
376 g_string_set_size (GString *string,
379 g_return_val_if_fail (string != NULL, NULL);
381 if (len >= string->allocated_len)
382 g_string_maybe_expand (string, len - string->len);
385 string->str[len] = 0;
391 g_string_insert_len (GString *string,
396 g_return_val_if_fail (string != NULL, NULL);
397 g_return_val_if_fail (val != NULL, string);
405 g_return_val_if_fail (pos <= string->len, string);
407 g_string_maybe_expand (string, len);
409 /* If we aren't appending at the end, move a hunk
410 * of the old string to the end, opening up space
412 if (pos < string->len)
413 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
415 /* insert the new string */
416 g_memmove (string->str + pos, val, len);
420 string->str[string->len] = 0;
426 g_string_append (GString *string,
429 g_return_val_if_fail (string != NULL, NULL);
430 g_return_val_if_fail (val != NULL, string);
432 return g_string_insert_len (string, -1, val, -1);
436 g_string_append_len (GString *string,
440 g_return_val_if_fail (string != NULL, NULL);
441 g_return_val_if_fail (val != NULL, string);
443 return g_string_insert_len (string, -1, val, len);
447 g_string_append_c (GString *string,
450 g_return_val_if_fail (string != NULL, NULL);
452 return g_string_insert_c (string, -1, c);
456 * g_string_append_unichar:
457 * @string: a #GString
458 * @wc: a Unicode character
460 * Converts a Unicode character into UTF-8, and appends it
463 * Return value: @string
466 g_string_append_unichar (GString *string,
469 g_return_val_if_fail (string != NULL, NULL);
471 return g_string_insert_unichar (string, -1, wc);
475 g_string_prepend (GString *string,
478 g_return_val_if_fail (string != NULL, NULL);
479 g_return_val_if_fail (val != NULL, string);
481 return g_string_insert_len (string, 0, val, -1);
485 g_string_prepend_len (GString *string,
489 g_return_val_if_fail (string != NULL, NULL);
490 g_return_val_if_fail (val != NULL, string);
492 return g_string_insert_len (string, 0, val, len);
496 g_string_prepend_c (GString *string,
499 g_return_val_if_fail (string != NULL, NULL);
501 return g_string_insert_c (string, 0, c);
505 * g_string_prepend_unichar:
506 * @string: a #GString.
507 * @wc: a Unicode character.
509 * Converts a Unicode character into UTF-8, and prepends it
512 * Return value: @string.
515 g_string_prepend_unichar (GString *string,
518 g_return_val_if_fail (string != NULL, NULL);
520 return g_string_insert_unichar (string, 0, wc);
524 g_string_insert (GString *string,
528 g_return_val_if_fail (string != NULL, NULL);
529 g_return_val_if_fail (val != NULL, string);
531 g_return_val_if_fail (pos <= string->len, string);
533 return g_string_insert_len (string, pos, val, -1);
537 g_string_insert_c (GString *string,
541 g_return_val_if_fail (string != NULL, NULL);
543 g_string_maybe_expand (string, 1);
548 g_return_val_if_fail (pos <= string->len, string);
550 /* If not just an append, move the old stuff */
551 if (pos < string->len)
552 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
554 string->str[pos] = c;
558 string->str[string->len] = 0;
564 * g_string_insert_unichar:
565 * @string: a #GString
566 * @pos: the position at which to insert character, or -1 to
567 * append at the end of the string.
568 * @wc: a Unicode character
570 * Converts a Unicode character into UTF-8, and insert it
571 * into the string at the given position.
573 * Return value: @string
576 g_string_insert_unichar (GString *string,
583 /* We could be somewhat more efficient here by computing
584 * the length, adding the space, then converting into that
585 * space, by cut-and-pasting the internals of g_unichar_to_utf8.
587 g_return_val_if_fail (string != NULL, NULL);
589 charlen = g_unichar_to_utf8 (wc, buf);
590 return g_string_insert_len (string, pos, buf, charlen);
594 g_string_erase (GString *string,
598 g_return_val_if_fail (string != NULL, NULL);
599 g_return_val_if_fail (pos >= 0, string);
600 g_return_val_if_fail (pos <= string->len, string);
603 len = string->len - pos;
606 g_return_val_if_fail (pos + len <= string->len, string);
608 if (pos + len < string->len)
609 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
614 string->str[string->len] = 0;
620 * g_string_ascii_down:
623 * Converts all upper case ASCII letters to lower case ASCII letters.
625 * Return value: passed-in @string pointer, with all the upper case
626 * characters converted to lower case in place, with
627 * semantics that exactly match g_ascii_tolower.
630 g_string_ascii_down (GString *string)
633 gint n = string->len;
635 g_return_val_if_fail (string != NULL, NULL);
641 *s = g_ascii_tolower (*s);
653 * Converts all lower case ASCII letters to upper case ASCII letters.
655 * Return value: passed-in @string pointer, with all the lower case
656 * characters converted to upper case in place, with
657 * semantics that exactly match g_ascii_toupper.
660 g_string_ascii_up (GString *string)
663 gint n = string->len;
665 g_return_val_if_fail (string != NULL, NULL);
671 *s = g_ascii_toupper (*s);
681 * @string: a #GString
683 * Converts a #GString to lowercase.
685 * Returns: the #GString.
687 * Deprecated: This function uses the locale-specific tolower() function,
688 * which is almost never the right thing. Use g_string_ascii_down() or
689 * g_utf8_strdown() instead.
692 g_string_down (GString *string)
695 glong n = string->len;
697 g_return_val_if_fail (string != NULL, NULL);
699 s = (guchar *) string->str;
714 * @string: a #GString
716 * Converts a #GString to uppercase.
718 * Return value: the #GString
720 * Deprecated: This function uses the locale-specific toupper() function,
721 * which is almost never the right thing. Use g_string_ascii_up() or
722 * g_utf8_strup() instead.
725 g_string_up (GString *string)
728 glong n = string->len;
730 g_return_val_if_fail (string != NULL, NULL);
732 s = (guchar *) string->str;
746 g_string_append_printf_internal (GString *string,
752 buffer = g_strdup_vprintf (fmt, args);
753 g_string_append (string, buffer);
758 g_string_printf (GString *string,
764 g_string_truncate (string, 0);
766 va_start (args, fmt);
767 g_string_append_printf_internal (string, fmt, args);
772 g_string_append_printf (GString *string,
778 va_start (args, fmt);
779 g_string_append_printf_internal (string, fmt, args);