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 string = g_string_sized_new (init ? strlen (init) + 2 : 2);
241 g_string_append (string, init);
247 g_string_new_len (const gchar *init,
253 return g_string_new (init);
256 string = g_string_sized_new (len);
259 g_string_append_len (string, init, len);
266 g_string_free (GString *string,
267 gboolean free_segment)
271 g_return_val_if_fail (string != NULL, NULL);
275 g_free (string->str);
279 segment = string->str;
281 G_LOCK (string_mem_chunk);
282 g_mem_chunk_free (string_mem_chunk, string);
283 G_UNLOCK (string_mem_chunk);
289 g_string_equal (const GString *v,
293 GString *string1 = (GString *) v;
294 GString *string2 = (GString *) v2;
295 gsize i = string1->len;
297 if (i != string2->len)
313 /* 31 bit hash function */
315 g_string_hash (const GString *str)
317 const gchar *p = str->str;
323 h = (h << 5) - h + *p;
331 g_string_assign (GString *string,
334 g_return_val_if_fail (string != NULL, NULL);
335 g_return_val_if_fail (rval != NULL, string);
337 g_string_truncate (string, 0);
338 g_string_append (string, rval);
344 g_string_truncate (GString *string,
347 g_return_val_if_fail (string != NULL, NULL);
349 string->len = MIN (len, string->len);
350 string->str[string->len] = 0;
357 * @string: a #GString
358 * @len: the new length
360 * Sets the length of a #GString. If the length is less than
361 * the current length, the string will be truncated. If the
362 * length is greater than the current length, the contents
363 * of the newly added area are undefined. (However, as
364 * always, string->str[string->len] will be a nul byte.)
366 * Return value: @string
369 g_string_set_size (GString *string,
372 g_return_val_if_fail (string != NULL, NULL);
374 if (len >= string->allocated_len)
375 g_string_maybe_expand (string, len - string->len);
378 string->str[len] = 0;
384 g_string_insert_len (GString *string,
389 g_return_val_if_fail (string != NULL, NULL);
390 g_return_val_if_fail (val != NULL, string);
398 g_return_val_if_fail (pos <= string->len, string);
400 g_string_maybe_expand (string, len);
402 /* If we aren't appending at the end, move a hunk
403 * of the old string to the end, opening up space
405 if (pos < string->len)
406 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
408 /* insert the new string */
409 g_memmove (string->str + pos, val, len);
413 string->str[string->len] = 0;
419 g_string_append (GString *string,
422 g_return_val_if_fail (string != NULL, NULL);
423 g_return_val_if_fail (val != NULL, string);
425 return g_string_insert_len (string, -1, val, -1);
429 g_string_append_len (GString *string,
433 g_return_val_if_fail (string != NULL, NULL);
434 g_return_val_if_fail (val != NULL, string);
436 return g_string_insert_len (string, -1, val, len);
440 g_string_append_c (GString *string,
443 g_return_val_if_fail (string != NULL, NULL);
445 return g_string_insert_c (string, -1, c);
449 * g_string_append_unichar:
450 * @string: a #GString
451 * @wc: a Unicode character
453 * Converts a Unicode character into UTF-8, and appends it
456 * Return value: @string
459 g_string_append_unichar (GString *string,
462 g_return_val_if_fail (string != NULL, NULL);
464 return g_string_insert_unichar (string, -1, wc);
468 g_string_prepend (GString *string,
471 g_return_val_if_fail (string != NULL, NULL);
472 g_return_val_if_fail (val != NULL, string);
474 return g_string_insert_len (string, 0, val, -1);
478 g_string_prepend_len (GString *string,
482 g_return_val_if_fail (string != NULL, NULL);
483 g_return_val_if_fail (val != NULL, string);
485 return g_string_insert_len (string, 0, val, len);
489 g_string_prepend_c (GString *string,
492 g_return_val_if_fail (string != NULL, NULL);
494 return g_string_insert_c (string, 0, c);
498 * g_string_append_unichar:
499 * @string: a #GString
500 * @wc: a Unicode character
502 * Converts a Unicode character into UTF-8, and prepends it
505 * Return value: @string
508 g_string_prepend_unichar (GString *string,
511 g_return_val_if_fail (string != NULL, NULL);
513 return g_string_insert_unichar (string, 0, wc);
517 g_string_insert (GString *string,
521 g_return_val_if_fail (string != NULL, NULL);
522 g_return_val_if_fail (val != NULL, string);
524 g_return_val_if_fail (pos <= string->len, string);
526 return g_string_insert_len (string, pos, val, -1);
530 g_string_insert_c (GString *string,
534 g_return_val_if_fail (string != NULL, NULL);
536 g_string_maybe_expand (string, 1);
541 g_return_val_if_fail (pos <= string->len, string);
543 /* If not just an append, move the old stuff */
544 if (pos < string->len)
545 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
547 string->str[pos] = c;
551 string->str[string->len] = 0;
557 * g_string_insert_unichar:
558 * @string: a #Gstring
559 * @pos: the position at which to insert character, or -1 to
560 * append at the end of the string.
561 * @wc: a Unicode character
563 * Converts a Unicode character into UTF-8, and insert it
564 * into the string at the given position.
566 * Return value: @string
569 g_string_insert_unichar (GString *string,
576 /* We could be somewhat more efficient here by computing
577 * the length, adding the space, then converting into that
578 * space, by cut-and-pasting the internals of g_unichar_to_utf8.
580 g_return_val_if_fail (string != NULL, NULL);
582 charlen = g_unichar_to_utf8 (wc, buf);
583 return g_string_insert_len (string, pos, buf, charlen);
587 g_string_erase (GString *string,
591 g_return_val_if_fail (string != NULL, NULL);
592 g_return_val_if_fail (pos >= 0, string);
593 g_return_val_if_fail (pos <= string->len, string);
596 len = string->len - pos;
599 g_return_val_if_fail (pos + len <= string->len, string);
601 if (pos + len < string->len)
602 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
607 string->str[string->len] = 0;
613 * g_string_ascii_down:
616 * Converts all upper case ASCII letters to lower case ASCII letters.
618 * Return value: passed-in @string pointer, with all the upper case
619 * characters converted to lower case in place, with
620 * semantics that exactly match g_ascii_tolower.
623 g_string_ascii_down (GString *string)
626 gint n = string->len;
628 g_return_val_if_fail (string != NULL, NULL);
634 *s = g_ascii_tolower (*s);
646 * Converts all lower case ASCII letters to upper case ASCII letters.
648 * Return value: passed-in @string pointer, with all the lower case
649 * characters converted to upper case in place, with
650 * semantics that exactly match g_ascii_toupper.
653 g_string_ascii_up (GString *string)
656 gint n = string->len;
658 g_return_val_if_fail (string != NULL, NULL);
664 *s = g_ascii_toupper (*s);
673 g_string_down (GString *string)
676 glong n = string->len;
678 g_return_val_if_fail (string != NULL, NULL);
680 s = (guchar *) string->str;
694 g_string_up (GString *string)
697 glong n = string->len;
699 g_return_val_if_fail (string != NULL, NULL);
701 s = (guchar *) string->str;
715 g_string_printfa_internal (GString *string,
721 buffer = g_strdup_vprintf (fmt, args);
722 g_string_append (string, buffer);
727 g_string_printf (GString *string,
733 g_string_truncate (string, 0);
735 va_start (args, fmt);
736 g_string_printfa_internal (string, fmt, args);
741 g_string_printfa (GString *string,
747 va_start (args, fmt);
748 g_string_printfa_internal (string, fmt, args);