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/.
48 GHashTable *const_table;
55 G_LOCK_DEFINE_STATIC (string_mem_chunk);
56 static GMemChunk *string_mem_chunk = NULL;
62 g_str_equal (gconstpointer v1,
65 const gchar *string1 = v1;
66 const gchar *string2 = v2;
68 return strcmp (string1, string2) == 0;
71 /* 31 bit hash function */
73 g_str_hash (gconstpointer key)
79 for (p += 1; *p != '\0'; p++)
80 h = (h << 5) - h + *p;
85 #define MY_MAXSIZE ((gsize)-1)
88 nearest_power (gsize base, gsize num)
90 if (num > MY_MAXSIZE / 2)
109 g_string_chunk_new (gsize default_size)
111 GStringChunk *new_chunk = g_new (GStringChunk, 1);
114 size = nearest_power (1, default_size);
116 new_chunk->const_table = NULL;
117 new_chunk->storage_list = NULL;
118 new_chunk->storage_next = size;
119 new_chunk->default_size = size;
120 new_chunk->this_size = size;
126 g_string_chunk_free (GStringChunk *chunk)
130 g_return_if_fail (chunk != NULL);
132 if (chunk->storage_list)
134 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
135 g_free (tmp_list->data);
137 g_slist_free (chunk->storage_list);
140 if (chunk->const_table)
141 g_hash_table_destroy (chunk->const_table);
147 g_string_chunk_insert (GStringChunk *chunk,
150 g_return_val_if_fail (chunk != NULL, NULL);
152 return g_string_chunk_insert_len (chunk, string, -1);
156 g_string_chunk_insert_const (GStringChunk *chunk,
161 g_return_val_if_fail (chunk != NULL, NULL);
163 if (!chunk->const_table)
164 chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
166 lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
170 lookup = g_string_chunk_insert (chunk, string);
171 g_hash_table_insert (chunk->const_table, lookup, lookup);
178 * g_string_chunk_insert_len:
179 * @chunk: a #GStringChunk
180 * @string: bytes to insert
181 * @len: number of bytes of @string to insert, or -1 to insert a
182 * nul-terminated string.
184 * Adds a copy of the first @len bytes of @string to the #GStringChunk. The
185 * copy is nul-terminated.
187 * The characters in the string can be changed, if necessary, though you
188 * should not change anything after the end of the string.
190 * Return value: a pointer to the copy of @string within the #GStringChunk
195 g_string_chunk_insert_len (GStringChunk *chunk,
201 g_return_val_if_fail (chunk != NULL, NULL);
204 len = strlen (string);
206 if ((chunk->storage_next + len + 1) > chunk->this_size)
208 gsize new_size = nearest_power (chunk->default_size, len + 1);
210 chunk->storage_list = g_slist_prepend (chunk->storage_list,
211 g_new (gchar, new_size));
213 chunk->this_size = new_size;
214 chunk->storage_next = 0;
217 pos = ((gchar *) chunk->storage_list->data) + chunk->storage_next;
221 strncpy (pos, string, len);
224 chunk->storage_next += len + 1;
232 g_string_maybe_expand (GString* string,
235 if (string->len + len >= string->allocated_len)
237 string->allocated_len = nearest_power (1, string->len + len + 1);
238 string->str = g_realloc (string->str, string->allocated_len);
243 g_string_sized_new (gsize dfl_size)
247 G_LOCK (string_mem_chunk);
248 if (!string_mem_chunk)
249 string_mem_chunk = g_mem_chunk_new ("string mem chunk",
251 1024, G_ALLOC_AND_FREE);
253 string = g_chunk_new (GString, string_mem_chunk);
254 G_UNLOCK (string_mem_chunk);
256 string->allocated_len = 0;
260 g_string_maybe_expand (string, MAX (dfl_size, 2));
267 g_string_new (const gchar *init)
271 if (init == NULL || *init == '\0')
272 string = g_string_sized_new (2);
278 string = g_string_sized_new (len + 2);
280 g_string_append_len (string, init, len);
287 g_string_new_len (const gchar *init,
293 return g_string_new (init);
296 string = g_string_sized_new (len);
299 g_string_append_len (string, init, len);
306 g_string_free (GString *string,
307 gboolean free_segment)
311 g_return_val_if_fail (string != NULL, NULL);
315 g_free (string->str);
319 segment = string->str;
321 G_LOCK (string_mem_chunk);
322 g_mem_chunk_free (string_mem_chunk, string);
323 G_UNLOCK (string_mem_chunk);
329 g_string_equal (const GString *v,
333 GString *string1 = (GString *) v;
334 GString *string2 = (GString *) v2;
335 gsize i = string1->len;
337 if (i != string2->len)
353 /* 31 bit hash function */
355 g_string_hash (const GString *str)
357 const gchar *p = str->str;
363 h = (h << 5) - h + *p;
371 g_string_assign (GString *string,
374 g_return_val_if_fail (string != NULL, NULL);
375 g_return_val_if_fail (rval != NULL, string);
377 g_string_truncate (string, 0);
378 g_string_append (string, rval);
384 g_string_truncate (GString *string,
387 g_return_val_if_fail (string != NULL, NULL);
389 string->len = MIN (len, string->len);
390 string->str[string->len] = 0;
397 * @string: a #GString
398 * @len: the new length
400 * Sets the length of a #GString. If the length is less than
401 * the current length, the string will be truncated. If the
402 * length is greater than the current length, the contents
403 * of the newly added area are undefined. (However, as
404 * always, string->str[string->len] will be a nul byte.)
406 * Return value: @string
409 g_string_set_size (GString *string,
412 g_return_val_if_fail (string != NULL, NULL);
414 if (len >= string->allocated_len)
415 g_string_maybe_expand (string, len - string->len);
418 string->str[len] = 0;
424 g_string_insert_len (GString *string,
429 g_return_val_if_fail (string != NULL, NULL);
430 g_return_val_if_fail (val != NULL, string);
438 g_return_val_if_fail (pos <= string->len, string);
440 g_string_maybe_expand (string, len);
442 /* If we aren't appending at the end, move a hunk
443 * of the old string to the end, opening up space
445 if (pos < string->len)
446 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
448 /* insert the new string */
449 g_memmove (string->str + pos, val, len);
453 string->str[string->len] = 0;
459 g_string_append (GString *string,
462 g_return_val_if_fail (string != NULL, NULL);
463 g_return_val_if_fail (val != NULL, string);
465 return g_string_insert_len (string, -1, val, -1);
469 g_string_append_len (GString *string,
473 g_return_val_if_fail (string != NULL, NULL);
474 g_return_val_if_fail (val != NULL, string);
476 return g_string_insert_len (string, -1, val, len);
480 g_string_append_c (GString *string,
483 g_return_val_if_fail (string != NULL, NULL);
485 return g_string_insert_c (string, -1, c);
489 * g_string_append_unichar:
490 * @string: a #GString
491 * @wc: a Unicode character
493 * Converts a Unicode character into UTF-8, and appends it
496 * Return value: @string
499 g_string_append_unichar (GString *string,
502 g_return_val_if_fail (string != NULL, NULL);
504 return g_string_insert_unichar (string, -1, wc);
508 g_string_prepend (GString *string,
511 g_return_val_if_fail (string != NULL, NULL);
512 g_return_val_if_fail (val != NULL, string);
514 return g_string_insert_len (string, 0, val, -1);
518 g_string_prepend_len (GString *string,
522 g_return_val_if_fail (string != NULL, NULL);
523 g_return_val_if_fail (val != NULL, string);
525 return g_string_insert_len (string, 0, val, len);
529 g_string_prepend_c (GString *string,
532 g_return_val_if_fail (string != NULL, NULL);
534 return g_string_insert_c (string, 0, c);
538 * g_string_prepend_unichar:
539 * @string: a #GString.
540 * @wc: a Unicode character.
542 * Converts a Unicode character into UTF-8, and prepends it
545 * Return value: @string.
548 g_string_prepend_unichar (GString *string,
551 g_return_val_if_fail (string != NULL, NULL);
553 return g_string_insert_unichar (string, 0, wc);
557 g_string_insert (GString *string,
561 g_return_val_if_fail (string != NULL, NULL);
562 g_return_val_if_fail (val != NULL, string);
564 g_return_val_if_fail (pos <= string->len, string);
566 return g_string_insert_len (string, pos, val, -1);
570 g_string_insert_c (GString *string,
574 g_return_val_if_fail (string != NULL, NULL);
576 g_string_maybe_expand (string, 1);
581 g_return_val_if_fail (pos <= string->len, string);
583 /* If not just an append, move the old stuff */
584 if (pos < string->len)
585 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
587 string->str[pos] = c;
591 string->str[string->len] = 0;
597 * g_string_insert_unichar:
598 * @string: a #GString
599 * @pos: the position at which to insert character, or -1 to
600 * append at the end of the string.
601 * @wc: a Unicode character
603 * Converts a Unicode character into UTF-8, and insert it
604 * into the string at the given position.
606 * Return value: @string
609 g_string_insert_unichar (GString *string,
616 /* We could be somewhat more efficient here by computing
617 * the length, adding the space, then converting into that
618 * space, by cut-and-pasting the internals of g_unichar_to_utf8.
620 g_return_val_if_fail (string != NULL, NULL);
622 charlen = g_unichar_to_utf8 (wc, buf);
623 return g_string_insert_len (string, pos, buf, charlen);
627 g_string_erase (GString *string,
631 g_return_val_if_fail (string != NULL, NULL);
632 g_return_val_if_fail (pos >= 0, string);
633 g_return_val_if_fail (pos <= string->len, string);
636 len = string->len - pos;
639 g_return_val_if_fail (pos + len <= string->len, string);
641 if (pos + len < string->len)
642 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
647 string->str[string->len] = 0;
653 * g_string_ascii_down:
656 * Converts all upper case ASCII letters to lower case ASCII letters.
658 * Return value: passed-in @string pointer, with all the upper case
659 * characters converted to lower case in place, with
660 * semantics that exactly match g_ascii_tolower.
663 g_string_ascii_down (GString *string)
666 gint n = string->len;
668 g_return_val_if_fail (string != NULL, NULL);
674 *s = g_ascii_tolower (*s);
686 * Converts all lower case ASCII letters to upper case ASCII letters.
688 * Return value: passed-in @string pointer, with all the lower case
689 * characters converted to upper case in place, with
690 * semantics that exactly match g_ascii_toupper.
693 g_string_ascii_up (GString *string)
696 gint n = string->len;
698 g_return_val_if_fail (string != NULL, NULL);
704 *s = g_ascii_toupper (*s);
714 * @string: a #GString
716 * Converts a #GString to lowercase.
718 * Returns: the #GString.
720 * Deprecated: This function uses the locale-specific tolower() function,
721 * which is almost never the right thing. Use g_string_ascii_down() or
722 * g_utf8_strdown() instead.
725 g_string_down (GString *string)
728 glong n = string->len;
730 g_return_val_if_fail (string != NULL, NULL);
732 s = (guchar *) string->str;
747 * @string: a #GString
749 * Converts a #GString to uppercase.
751 * Return value: the #GString
753 * Deprecated: This function uses the locale-specific toupper() function,
754 * which is almost never the right thing. Use g_string_ascii_up() or
755 * g_utf8_strup() instead.
758 g_string_up (GString *string)
761 glong n = string->len;
763 g_return_val_if_fail (string != NULL, NULL);
765 s = (guchar *) string->str;
779 g_string_append_printf_internal (GString *string,
786 length = g_vasprintf (&buffer, fmt, args);
787 g_string_append_len (string, buffer, length);
792 g_string_printf (GString *string,
798 g_string_truncate (string, 0);
800 va_start (args, fmt);
801 g_string_append_printf_internal (string, fmt, args);
806 g_string_append_printf (GString *string,
812 va_start (args, fmt);
813 g_string_append_printf_internal (string, fmt, args);