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);
519 g_string_append_c (GString *string,
522 g_return_val_if_fail (string != NULL, NULL);
524 return g_string_insert_c (string, -1, c);
528 * g_string_append_unichar:
529 * @string: a #GString
530 * @wc: a Unicode character
532 * Converts a Unicode character into UTF-8, and appends it
535 * Return value: @string
538 g_string_append_unichar (GString *string,
541 g_return_val_if_fail (string != NULL, NULL);
543 return g_string_insert_unichar (string, -1, wc);
547 g_string_prepend (GString *string,
550 g_return_val_if_fail (string != NULL, NULL);
551 g_return_val_if_fail (val != NULL, string);
553 return g_string_insert_len (string, 0, val, -1);
557 g_string_prepend_len (GString *string,
561 g_return_val_if_fail (string != NULL, NULL);
562 g_return_val_if_fail (val != NULL, string);
564 return g_string_insert_len (string, 0, val, len);
568 g_string_prepend_c (GString *string,
571 g_return_val_if_fail (string != NULL, NULL);
573 return g_string_insert_c (string, 0, c);
577 * g_string_prepend_unichar:
578 * @string: a #GString.
579 * @wc: a Unicode character.
581 * Converts a Unicode character into UTF-8, and prepends it
584 * Return value: @string.
587 g_string_prepend_unichar (GString *string,
590 g_return_val_if_fail (string != NULL, NULL);
592 return g_string_insert_unichar (string, 0, wc);
596 g_string_insert (GString *string,
600 g_return_val_if_fail (string != NULL, NULL);
601 g_return_val_if_fail (val != NULL, string);
603 g_return_val_if_fail (pos <= string->len, string);
605 return g_string_insert_len (string, pos, val, -1);
609 g_string_insert_c (GString *string,
613 g_return_val_if_fail (string != NULL, NULL);
615 g_string_maybe_expand (string, 1);
620 g_return_val_if_fail (pos <= string->len, string);
622 /* If not just an append, move the old stuff */
623 if (pos < string->len)
624 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
626 string->str[pos] = c;
630 string->str[string->len] = 0;
636 * g_string_insert_unichar:
637 * @string: a #GString
638 * @pos: the position at which to insert character, or -1 to
639 * append at the end of the string.
640 * @wc: a Unicode character
642 * Converts a Unicode character into UTF-8, and insert it
643 * into the string at the given position.
645 * Return value: @string
648 g_string_insert_unichar (GString *string,
655 /* We could be somewhat more efficient here by computing
656 * the length, adding the space, then converting into that
657 * space, by cut-and-pasting the internals of g_unichar_to_utf8.
659 g_return_val_if_fail (string != NULL, NULL);
661 charlen = g_unichar_to_utf8 (wc, buf);
662 return g_string_insert_len (string, pos, buf, charlen);
666 g_string_erase (GString *string,
670 g_return_val_if_fail (string != NULL, NULL);
671 g_return_val_if_fail (pos >= 0, string);
672 g_return_val_if_fail (pos <= string->len, string);
675 len = string->len - pos;
678 g_return_val_if_fail (pos + len <= string->len, string);
680 if (pos + len < string->len)
681 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
686 string->str[string->len] = 0;
692 * g_string_ascii_down:
695 * Converts all upper case ASCII letters to lower case ASCII letters.
697 * Return value: passed-in @string pointer, with all the upper case
698 * characters converted to lower case in place, with
699 * semantics that exactly match g_ascii_tolower.
702 g_string_ascii_down (GString *string)
707 g_return_val_if_fail (string != NULL, NULL);
714 *s = g_ascii_tolower (*s);
726 * Converts all lower case ASCII letters to upper case ASCII letters.
728 * Return value: passed-in @string pointer, with all the lower case
729 * characters converted to upper case in place, with
730 * semantics that exactly match g_ascii_toupper.
733 g_string_ascii_up (GString *string)
738 g_return_val_if_fail (string != NULL, NULL);
745 *s = g_ascii_toupper (*s);
755 * @string: a #GString
757 * Converts a #GString to lowercase.
759 * Returns: the #GString.
761 * Deprecated: This function uses the locale-specific tolower() function,
762 * which is almost never the right thing. Use g_string_ascii_down() or
763 * g_utf8_strdown() instead.
766 g_string_down (GString *string)
771 g_return_val_if_fail (string != NULL, NULL);
774 s = (guchar *) string->str;
789 * @string: a #GString
791 * Converts a #GString to uppercase.
793 * Return value: the #GString
795 * Deprecated: This function uses the locale-specific toupper() function,
796 * which is almost never the right thing. Use g_string_ascii_up() or
797 * g_utf8_strup() instead.
800 g_string_up (GString *string)
805 g_return_val_if_fail (string != NULL, NULL);
808 s = (guchar *) string->str;
822 g_string_append_printf_internal (GString *string,
829 length = g_vasprintf (&buffer, fmt, args);
830 g_string_append_len (string, buffer, length);
835 g_string_printf (GString *string,
841 g_string_truncate (string, 0);
843 va_start (args, fmt);
844 g_string_append_printf_internal (string, fmt, args);
849 g_string_append_printf (GString *string,
855 va_start (args, fmt);
856 g_string_append_printf_internal (string, fmt, args);