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/.
49 GHashTable *const_table;
56 G_LOCK_DEFINE_STATIC (string_mem_chunk);
57 static GMemChunk *string_mem_chunk = NULL;
63 g_str_equal (gconstpointer v1,
66 const gchar *string1 = v1;
67 const gchar *string2 = v2;
69 return strcmp (string1, string2) == 0;
72 /* 31 bit hash function */
74 g_str_hash (gconstpointer key)
80 for (p += 1; *p != '\0'; p++)
81 h = (h << 5) - h + *p;
86 #define MY_MAXSIZE ((gsize)-1)
89 nearest_power (gsize base, gsize num)
91 if (num > MY_MAXSIZE / 2)
110 g_string_chunk_new (gsize default_size)
112 GStringChunk *new_chunk = g_new (GStringChunk, 1);
115 size = nearest_power (1, default_size);
117 new_chunk->const_table = NULL;
118 new_chunk->storage_list = NULL;
119 new_chunk->storage_next = size;
120 new_chunk->default_size = size;
121 new_chunk->this_size = size;
127 g_string_chunk_free (GStringChunk *chunk)
131 g_return_if_fail (chunk != NULL);
133 if (chunk->storage_list)
135 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
136 g_free (tmp_list->data);
138 g_slist_free (chunk->storage_list);
141 if (chunk->const_table)
142 g_hash_table_destroy (chunk->const_table);
148 g_string_chunk_insert (GStringChunk *chunk,
151 g_return_val_if_fail (chunk != NULL, NULL);
153 return g_string_chunk_insert_len (chunk, string, -1);
157 g_string_chunk_insert_const (GStringChunk *chunk,
162 g_return_val_if_fail (chunk != NULL, NULL);
164 if (!chunk->const_table)
165 chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
167 lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
171 lookup = g_string_chunk_insert (chunk, string);
172 g_hash_table_insert (chunk->const_table, lookup, lookup);
179 * g_string_chunk_insert_len:
180 * @chunk: a #GStringChunk
181 * @string: bytes to insert
182 * @len: number of bytes of @string to insert, or -1 to insert a
183 * nul-terminated string.
185 * Adds a copy of the first @len bytes of @string to the #GStringChunk. The
186 * copy is nul-terminated.
188 * The characters in the string can be changed, if necessary, though you
189 * should not change anything after the end of the string.
191 * Return value: a pointer to the copy of @string within the #GStringChunk
196 g_string_chunk_insert_len (GStringChunk *chunk,
202 g_return_val_if_fail (chunk != NULL, NULL);
205 len = strlen (string);
207 if ((chunk->storage_next + len + 1) > chunk->this_size)
209 gsize new_size = nearest_power (chunk->default_size, len + 1);
211 chunk->storage_list = g_slist_prepend (chunk->storage_list,
212 g_new (gchar, new_size));
214 chunk->this_size = new_size;
215 chunk->storage_next = 0;
218 pos = ((gchar *) chunk->storage_list->data) + chunk->storage_next;
222 strncpy (pos, string, len);
225 chunk->storage_next += len + 1;
233 g_string_maybe_expand (GString* string,
236 if (string->len + len >= string->allocated_len)
238 string->allocated_len = nearest_power (1, string->len + len + 1);
239 string->str = g_realloc (string->str, string->allocated_len);
244 g_string_sized_new (gsize dfl_size)
248 G_LOCK (string_mem_chunk);
249 if (!string_mem_chunk)
250 string_mem_chunk = g_mem_chunk_new ("string mem chunk",
252 1024, G_ALLOC_AND_FREE);
254 string = g_chunk_new (GString, string_mem_chunk);
255 G_UNLOCK (string_mem_chunk);
257 string->allocated_len = 0;
261 g_string_maybe_expand (string, MAX (dfl_size, 2));
268 g_string_new (const gchar *init)
272 if (init == NULL || *init == '\0')
273 string = g_string_sized_new (2);
279 string = g_string_sized_new (len + 2);
281 g_string_append_len (string, init, len);
288 g_string_new_len (const gchar *init,
294 return g_string_new (init);
297 string = g_string_sized_new (len);
300 g_string_append_len (string, init, len);
307 g_string_free (GString *string,
308 gboolean free_segment)
312 g_return_val_if_fail (string != NULL, NULL);
316 g_free (string->str);
320 segment = string->str;
322 G_LOCK (string_mem_chunk);
323 g_mem_chunk_free (string_mem_chunk, string);
324 G_UNLOCK (string_mem_chunk);
330 g_string_equal (const GString *v,
334 GString *string1 = (GString *) v;
335 GString *string2 = (GString *) v2;
336 gsize i = string1->len;
338 if (i != string2->len)
354 /* 31 bit hash function */
356 g_string_hash (const GString *str)
358 const gchar *p = str->str;
364 h = (h << 5) - h + *p;
372 g_string_assign (GString *string,
375 g_return_val_if_fail (string != NULL, NULL);
376 g_return_val_if_fail (rval != NULL, string);
378 /* Make sure assigning to itself doesn't corrupt the string. */
379 if (string->str != rval)
381 /* Assigning from substring should be ok since g_string_truncate
383 g_string_truncate (string, 0);
384 g_string_append (string, rval);
391 g_string_truncate (GString *string,
394 g_return_val_if_fail (string != NULL, NULL);
396 string->len = MIN (len, string->len);
397 string->str[string->len] = 0;
404 * @string: a #GString
405 * @len: the new length
407 * Sets the length of a #GString. If the length is less than
408 * the current length, the string will be truncated. If the
409 * length is greater than the current length, the contents
410 * of the newly added area are undefined. (However, as
411 * always, string->str[string->len] will be a nul byte.)
413 * Return value: @string
416 g_string_set_size (GString *string,
419 g_return_val_if_fail (string != NULL, NULL);
421 if (len >= string->allocated_len)
422 g_string_maybe_expand (string, len - string->len);
425 string->str[len] = 0;
431 g_string_insert_len (GString *string,
436 g_return_val_if_fail (string != NULL, NULL);
437 g_return_val_if_fail (val != NULL, string);
445 g_return_val_if_fail (pos <= string->len, string);
447 /* Check whether val represents a substring of string. This test
448 probably violates chapter and verse of the C standards, since
449 ">=" and "<=" are only valid when val really is a substring.
450 In practice, it will work on modern archs. */
451 if (val >= string->str && val <= string->str + string->len)
453 gsize offset = val - string->str;
456 g_string_maybe_expand (string, len);
457 val = string->str + offset;
458 /* At this point, val is valid again. */
460 /* Open up space where we are going to insert. */
461 if (pos < string->len)
462 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
464 /* Move the source part before the gap, if any. */
467 precount = MIN (len, pos - offset);
468 memcpy (string->str + pos, val, precount);
471 /* Move the source part after the gap, if any. */
473 memcpy (string->str + pos + precount,
474 val + /* Already moved: */ precount + /* Space opened up: */ len,
479 g_string_maybe_expand (string, len);
481 /* If we aren't appending at the end, move a hunk
482 * of the old string to the end, opening up space
484 if (pos < string->len)
485 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
487 /* insert the new string */
488 memcpy (string->str + pos, val, len);
493 string->str[string->len] = 0;
499 g_string_append (GString *string,
502 g_return_val_if_fail (string != NULL, NULL);
503 g_return_val_if_fail (val != NULL, string);
505 return g_string_insert_len (string, -1, val, -1);
509 g_string_append_len (GString *string,
513 g_return_val_if_fail (string != NULL, NULL);
514 g_return_val_if_fail (val != NULL, string);
516 return g_string_insert_len (string, -1, val, len);
519 #undef g_string_append_c
521 g_string_append_c (GString *string,
524 g_return_val_if_fail (string != NULL, NULL);
526 return g_string_insert_c (string, -1, c);
530 * g_string_append_unichar:
531 * @string: a #GString
532 * @wc: a Unicode character
534 * Converts a Unicode character into UTF-8, and appends it
537 * Return value: @string
540 g_string_append_unichar (GString *string,
543 g_return_val_if_fail (string != NULL, NULL);
545 return g_string_insert_unichar (string, -1, wc);
549 g_string_prepend (GString *string,
552 g_return_val_if_fail (string != NULL, NULL);
553 g_return_val_if_fail (val != NULL, string);
555 return g_string_insert_len (string, 0, val, -1);
559 g_string_prepend_len (GString *string,
563 g_return_val_if_fail (string != NULL, NULL);
564 g_return_val_if_fail (val != NULL, string);
566 return g_string_insert_len (string, 0, val, len);
570 g_string_prepend_c (GString *string,
573 g_return_val_if_fail (string != NULL, NULL);
575 return g_string_insert_c (string, 0, c);
579 * g_string_prepend_unichar:
580 * @string: a #GString.
581 * @wc: a Unicode character.
583 * Converts a Unicode character into UTF-8, and prepends it
586 * Return value: @string.
589 g_string_prepend_unichar (GString *string,
592 g_return_val_if_fail (string != NULL, NULL);
594 return g_string_insert_unichar (string, 0, wc);
598 g_string_insert (GString *string,
602 g_return_val_if_fail (string != NULL, NULL);
603 g_return_val_if_fail (val != NULL, string);
605 g_return_val_if_fail (pos <= string->len, string);
607 return g_string_insert_len (string, pos, val, -1);
611 g_string_insert_c (GString *string,
615 g_return_val_if_fail (string != NULL, NULL);
617 g_string_maybe_expand (string, 1);
622 g_return_val_if_fail (pos <= string->len, string);
624 /* If not just an append, move the old stuff */
625 if (pos < string->len)
626 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
628 string->str[pos] = c;
632 string->str[string->len] = 0;
638 * g_string_insert_unichar:
639 * @string: a #GString
640 * @pos: the position at which to insert character, or -1 to
641 * append at the end of the string.
642 * @wc: a Unicode character
644 * Converts a Unicode character into UTF-8, and insert it
645 * into the string at the given position.
647 * Return value: @string
650 g_string_insert_unichar (GString *string,
657 /* We could be somewhat more efficient here by computing
658 * the length, adding the space, then converting into that
659 * space, by cut-and-pasting the internals of g_unichar_to_utf8.
661 g_return_val_if_fail (string != NULL, NULL);
663 charlen = g_unichar_to_utf8 (wc, buf);
664 return g_string_insert_len (string, pos, buf, charlen);
668 g_string_erase (GString *string,
672 g_return_val_if_fail (string != NULL, NULL);
673 g_return_val_if_fail (pos >= 0, string);
674 g_return_val_if_fail (pos <= string->len, string);
677 len = string->len - pos;
680 g_return_val_if_fail (pos + len <= string->len, string);
682 if (pos + len < string->len)
683 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
688 string->str[string->len] = 0;
694 * g_string_ascii_down:
697 * Converts all upper case ASCII letters to lower case ASCII letters.
699 * Return value: passed-in @string pointer, with all the upper case
700 * characters converted to lower case in place, with
701 * semantics that exactly match g_ascii_tolower.
704 g_string_ascii_down (GString *string)
709 g_return_val_if_fail (string != NULL, NULL);
716 *s = g_ascii_tolower (*s);
728 * Converts all lower case ASCII letters to upper case ASCII letters.
730 * Return value: passed-in @string pointer, with all the lower case
731 * characters converted to upper case in place, with
732 * semantics that exactly match g_ascii_toupper.
735 g_string_ascii_up (GString *string)
740 g_return_val_if_fail (string != NULL, NULL);
747 *s = g_ascii_toupper (*s);
757 * @string: a #GString
759 * Converts a #GString to lowercase.
761 * Returns: the #GString.
763 * Deprecated: This function uses the locale-specific tolower() function,
764 * which is almost never the right thing. Use g_string_ascii_down() or
765 * g_utf8_strdown() instead.
768 g_string_down (GString *string)
773 g_return_val_if_fail (string != NULL, NULL);
776 s = (guchar *) string->str;
791 * @string: a #GString
793 * Converts a #GString to uppercase.
795 * Return value: the #GString
797 * Deprecated: This function uses the locale-specific toupper() function,
798 * which is almost never the right thing. Use g_string_ascii_up() or
799 * g_utf8_strup() instead.
802 g_string_up (GString *string)
807 g_return_val_if_fail (string != NULL, NULL);
810 s = (guchar *) string->str;
824 g_string_append_printf_internal (GString *string,
831 length = g_vasprintf (&buffer, fmt, args);
832 g_string_append_len (string, buffer, length);
837 g_string_printf (GString *string,
843 g_string_truncate (string, 0);
845 va_start (args, fmt);
846 g_string_append_printf_internal (string, fmt, args);
851 g_string_append_printf (GString *string,
857 va_start (args, fmt);
858 g_string_append_printf_internal (string, fmt, args);