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 /* Make sure assigning to itself doesn't corrupt the string. */
378 if (string->str != rval)
380 /* Assigning from substring should be ok since g_string_truncate
382 g_string_truncate (string, 0);
383 g_string_append (string, rval);
390 g_string_truncate (GString *string,
393 g_return_val_if_fail (string != NULL, NULL);
395 string->len = MIN (len, string->len);
396 string->str[string->len] = 0;
403 * @string: a #GString
404 * @len: the new length
406 * Sets the length of a #GString. If the length is less than
407 * the current length, the string will be truncated. If the
408 * length is greater than the current length, the contents
409 * of the newly added area are undefined. (However, as
410 * always, string->str[string->len] will be a nul byte.)
412 * Return value: @string
415 g_string_set_size (GString *string,
418 g_return_val_if_fail (string != NULL, NULL);
420 if (len >= string->allocated_len)
421 g_string_maybe_expand (string, len - string->len);
424 string->str[len] = 0;
430 g_string_insert_len (GString *string,
435 g_return_val_if_fail (string != NULL, NULL);
436 g_return_val_if_fail (val != NULL, string);
444 g_return_val_if_fail (pos <= string->len, string);
446 /* Check whether val represents a substring of string. This test
447 probably violates chapter and verse of the C standards, since
448 ">=" and "<=" are only valid when val really is a substring.
449 In practice, it will work on modern archs. */
450 if (val >= string->str && val <= string->str + string->len)
452 gsize offset = val - string->str;
455 g_string_maybe_expand (string, len);
456 val = string->str + offset;
457 /* At this point, val is valid again. */
459 /* Open up space where we are going to insert. */
460 if (pos < string->len)
461 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
463 /* Move the source part before the gap, if any. */
466 precount = MIN (len, pos - offset);
467 memcpy (string->str + pos, val, precount);
470 /* Move the source part after the gap, if any. */
472 memcpy (string->str + pos + precount,
473 val + /* Already moved: */ precount + /* Space opened up: */ len,
478 g_string_maybe_expand (string, len);
480 /* If we aren't appending at the end, move a hunk
481 * of the old string to the end, opening up space
483 if (pos < string->len)
484 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
486 /* insert the new string */
487 memcpy (string->str + pos, val, len);
492 string->str[string->len] = 0;
498 g_string_append (GString *string,
501 g_return_val_if_fail (string != NULL, NULL);
502 g_return_val_if_fail (val != NULL, string);
504 return g_string_insert_len (string, -1, val, -1);
508 g_string_append_len (GString *string,
512 g_return_val_if_fail (string != NULL, NULL);
513 g_return_val_if_fail (val != NULL, string);
515 return g_string_insert_len (string, -1, val, len);
518 #undef g_string_append_c
520 g_string_append_c (GString *string,
523 g_return_val_if_fail (string != NULL, NULL);
525 return g_string_insert_c (string, -1, c);
529 * g_string_append_unichar:
530 * @string: a #GString
531 * @wc: a Unicode character
533 * Converts a Unicode character into UTF-8, and appends it
536 * Return value: @string
539 g_string_append_unichar (GString *string,
542 g_return_val_if_fail (string != NULL, NULL);
544 return g_string_insert_unichar (string, -1, wc);
548 g_string_prepend (GString *string,
551 g_return_val_if_fail (string != NULL, NULL);
552 g_return_val_if_fail (val != NULL, string);
554 return g_string_insert_len (string, 0, val, -1);
558 g_string_prepend_len (GString *string,
562 g_return_val_if_fail (string != NULL, NULL);
563 g_return_val_if_fail (val != NULL, string);
565 return g_string_insert_len (string, 0, val, len);
569 g_string_prepend_c (GString *string,
572 g_return_val_if_fail (string != NULL, NULL);
574 return g_string_insert_c (string, 0, c);
578 * g_string_prepend_unichar:
579 * @string: a #GString.
580 * @wc: a Unicode character.
582 * Converts a Unicode character into UTF-8, and prepends it
585 * Return value: @string.
588 g_string_prepend_unichar (GString *string,
591 g_return_val_if_fail (string != NULL, NULL);
593 return g_string_insert_unichar (string, 0, wc);
597 g_string_insert (GString *string,
601 g_return_val_if_fail (string != NULL, NULL);
602 g_return_val_if_fail (val != NULL, string);
604 g_return_val_if_fail (pos <= string->len, string);
606 return g_string_insert_len (string, pos, val, -1);
610 g_string_insert_c (GString *string,
614 g_return_val_if_fail (string != NULL, NULL);
616 g_string_maybe_expand (string, 1);
621 g_return_val_if_fail (pos <= string->len, string);
623 /* If not just an append, move the old stuff */
624 if (pos < string->len)
625 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
627 string->str[pos] = c;
631 string->str[string->len] = 0;
637 * g_string_insert_unichar:
638 * @string: a #GString
639 * @pos: the position at which to insert character, or -1 to
640 * append at the end of the string.
641 * @wc: a Unicode character
643 * Converts a Unicode character into UTF-8, and insert it
644 * into the string at the given position.
646 * Return value: @string
649 g_string_insert_unichar (GString *string,
656 /* We could be somewhat more efficient here by computing
657 * the length, adding the space, then converting into that
658 * space, by cut-and-pasting the internals of g_unichar_to_utf8.
660 g_return_val_if_fail (string != NULL, NULL);
662 charlen = g_unichar_to_utf8 (wc, buf);
663 return g_string_insert_len (string, pos, buf, charlen);
667 g_string_erase (GString *string,
671 g_return_val_if_fail (string != NULL, NULL);
672 g_return_val_if_fail (pos >= 0, string);
673 g_return_val_if_fail (pos <= string->len, string);
676 len = string->len - pos;
679 g_return_val_if_fail (pos + len <= string->len, string);
681 if (pos + len < string->len)
682 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
687 string->str[string->len] = 0;
693 * g_string_ascii_down:
696 * Converts all upper case ASCII letters to lower case ASCII letters.
698 * Return value: passed-in @string pointer, with all the upper case
699 * characters converted to lower case in place, with
700 * semantics that exactly match g_ascii_tolower.
703 g_string_ascii_down (GString *string)
708 g_return_val_if_fail (string != NULL, NULL);
715 *s = g_ascii_tolower (*s);
727 * Converts all lower case ASCII letters to upper case ASCII letters.
729 * Return value: passed-in @string pointer, with all the lower case
730 * characters converted to upper case in place, with
731 * semantics that exactly match g_ascii_toupper.
734 g_string_ascii_up (GString *string)
739 g_return_val_if_fail (string != NULL, NULL);
746 *s = g_ascii_toupper (*s);
756 * @string: a #GString
758 * Converts a #GString to lowercase.
760 * Returns: the #GString.
762 * Deprecated: This function uses the locale-specific tolower() function,
763 * which is almost never the right thing. Use g_string_ascii_down() or
764 * g_utf8_strdown() instead.
767 g_string_down (GString *string)
772 g_return_val_if_fail (string != NULL, NULL);
775 s = (guchar *) string->str;
790 * @string: a #GString
792 * Converts a #GString to uppercase.
794 * Return value: the #GString
796 * Deprecated: This function uses the locale-specific toupper() function,
797 * which is almost never the right thing. Use g_string_ascii_up() or
798 * g_utf8_strup() instead.
801 g_string_up (GString *string)
806 g_return_val_if_fail (string != NULL, NULL);
809 s = (guchar *) string->str;
823 g_string_append_printf_internal (GString *string,
830 length = g_vasprintf (&buffer, fmt, args);
831 g_string_append_len (string, buffer, length);
836 g_string_printf (GString *string,
842 g_string_truncate (string, 0);
844 va_start (args, fmt);
845 g_string_append_printf_internal (string, fmt, args);
850 g_string_append_printf (GString *string,
856 va_start (args, fmt);
857 g_string_append_printf_internal (string, fmt, args);