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 g_return_val_if_fail (chunk != NULL, NULL);
151 return g_string_chunk_insert_len (chunk, string, -1);
155 g_string_chunk_insert_const (GStringChunk *chunk,
160 g_return_val_if_fail (chunk != NULL, NULL);
162 if (!chunk->const_table)
163 chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
165 lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
169 lookup = g_string_chunk_insert (chunk, string);
170 g_hash_table_insert (chunk->const_table, lookup, lookup);
177 * g_string_chunk_insert_len:
178 * @chunk: a #GStringChunk
179 * @string: bytes to insert
180 * @len: number of bytes of @string to insert, or -1 to insert a
181 * nul-terminated string.
183 * Adds a copy of the first @len bytes of @string to the #GStringChunk. The
184 * copy is nul-terminated.
186 * The characters in the string can be changed, if necessary, though you
187 * should not change anything after the end of the string.
189 * Return value: a pointer to the copy of @string within the #GStringChunk
194 g_string_chunk_insert_len (GStringChunk *chunk,
200 g_return_val_if_fail (chunk != NULL, NULL);
203 len = strlen (string);
205 if ((chunk->storage_next + len + 1) > chunk->this_size)
207 gsize new_size = nearest_power (chunk->default_size, len + 1);
209 chunk->storage_list = g_slist_prepend (chunk->storage_list,
210 g_new (gchar, new_size));
212 chunk->this_size = new_size;
213 chunk->storage_next = 0;
216 pos = ((gchar *) chunk->storage_list->data) + chunk->storage_next;
220 strncpy (pos, string, len);
223 chunk->storage_next += len + 1;
231 g_string_maybe_expand (GString* string,
234 if (string->len + len >= string->allocated_len)
236 string->allocated_len = nearest_power (1, string->len + len + 1);
237 string->str = g_realloc (string->str, string->allocated_len);
242 g_string_sized_new (gsize dfl_size)
246 G_LOCK (string_mem_chunk);
247 if (!string_mem_chunk)
248 string_mem_chunk = g_mem_chunk_new ("string mem chunk",
250 1024, G_ALLOC_AND_FREE);
252 string = g_chunk_new (GString, string_mem_chunk);
253 G_UNLOCK (string_mem_chunk);
255 string->allocated_len = 0;
259 g_string_maybe_expand (string, MAX (dfl_size, 2));
266 g_string_new (const gchar *init)
270 if (init == NULL || *init == '\0')
271 string = g_string_sized_new (2);
277 string = g_string_sized_new (len + 2);
279 g_string_append_len (string, init, len);
286 g_string_new_len (const gchar *init,
292 return g_string_new (init);
295 string = g_string_sized_new (len);
298 g_string_append_len (string, init, len);
305 g_string_free (GString *string,
306 gboolean free_segment)
310 g_return_val_if_fail (string != NULL, NULL);
314 g_free (string->str);
318 segment = string->str;
320 G_LOCK (string_mem_chunk);
321 g_mem_chunk_free (string_mem_chunk, string);
322 G_UNLOCK (string_mem_chunk);
328 g_string_equal (const GString *v,
332 GString *string1 = (GString *) v;
333 GString *string2 = (GString *) v2;
334 gsize i = string1->len;
336 if (i != string2->len)
352 /* 31 bit hash function */
354 g_string_hash (const GString *str)
356 const gchar *p = str->str;
362 h = (h << 5) - h + *p;
370 g_string_assign (GString *string,
373 g_return_val_if_fail (string != NULL, NULL);
374 g_return_val_if_fail (rval != NULL, string);
376 g_string_truncate (string, 0);
377 g_string_append (string, rval);
383 g_string_truncate (GString *string,
386 g_return_val_if_fail (string != NULL, NULL);
388 string->len = MIN (len, string->len);
389 string->str[string->len] = 0;
396 * @string: a #GString
397 * @len: the new length
399 * Sets the length of a #GString. If the length is less than
400 * the current length, the string will be truncated. If the
401 * length is greater than the current length, the contents
402 * of the newly added area are undefined. (However, as
403 * always, string->str[string->len] will be a nul byte.)
405 * Return value: @string
408 g_string_set_size (GString *string,
411 g_return_val_if_fail (string != NULL, NULL);
413 if (len >= string->allocated_len)
414 g_string_maybe_expand (string, len - string->len);
417 string->str[len] = 0;
423 g_string_insert_len (GString *string,
428 g_return_val_if_fail (string != NULL, NULL);
429 g_return_val_if_fail (val != NULL, string);
437 g_return_val_if_fail (pos <= string->len, string);
439 g_string_maybe_expand (string, len);
441 /* If we aren't appending at the end, move a hunk
442 * of the old string to the end, opening up space
444 if (pos < string->len)
445 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
447 /* insert the new string */
448 g_memmove (string->str + pos, val, len);
452 string->str[string->len] = 0;
458 g_string_append (GString *string,
461 g_return_val_if_fail (string != NULL, NULL);
462 g_return_val_if_fail (val != NULL, string);
464 return g_string_insert_len (string, -1, val, -1);
468 g_string_append_len (GString *string,
472 g_return_val_if_fail (string != NULL, NULL);
473 g_return_val_if_fail (val != NULL, string);
475 return g_string_insert_len (string, -1, val, len);
479 g_string_append_c (GString *string,
482 g_return_val_if_fail (string != NULL, NULL);
484 return g_string_insert_c (string, -1, c);
488 * g_string_append_unichar:
489 * @string: a #GString
490 * @wc: a Unicode character
492 * Converts a Unicode character into UTF-8, and appends it
495 * Return value: @string
498 g_string_append_unichar (GString *string,
501 g_return_val_if_fail (string != NULL, NULL);
503 return g_string_insert_unichar (string, -1, wc);
507 g_string_prepend (GString *string,
510 g_return_val_if_fail (string != NULL, NULL);
511 g_return_val_if_fail (val != NULL, string);
513 return g_string_insert_len (string, 0, val, -1);
517 g_string_prepend_len (GString *string,
521 g_return_val_if_fail (string != NULL, NULL);
522 g_return_val_if_fail (val != NULL, string);
524 return g_string_insert_len (string, 0, val, len);
528 g_string_prepend_c (GString *string,
531 g_return_val_if_fail (string != NULL, NULL);
533 return g_string_insert_c (string, 0, c);
537 * g_string_prepend_unichar:
538 * @string: a #GString.
539 * @wc: a Unicode character.
541 * Converts a Unicode character into UTF-8, and prepends it
544 * Return value: @string.
547 g_string_prepend_unichar (GString *string,
550 g_return_val_if_fail (string != NULL, NULL);
552 return g_string_insert_unichar (string, 0, wc);
556 g_string_insert (GString *string,
560 g_return_val_if_fail (string != NULL, NULL);
561 g_return_val_if_fail (val != NULL, string);
563 g_return_val_if_fail (pos <= string->len, string);
565 return g_string_insert_len (string, pos, val, -1);
569 g_string_insert_c (GString *string,
573 g_return_val_if_fail (string != NULL, NULL);
575 g_string_maybe_expand (string, 1);
580 g_return_val_if_fail (pos <= string->len, string);
582 /* If not just an append, move the old stuff */
583 if (pos < string->len)
584 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
586 string->str[pos] = c;
590 string->str[string->len] = 0;
596 * g_string_insert_unichar:
597 * @string: a #GString
598 * @pos: the position at which to insert character, or -1 to
599 * append at the end of the string.
600 * @wc: a Unicode character
602 * Converts a Unicode character into UTF-8, and insert it
603 * into the string at the given position.
605 * Return value: @string
608 g_string_insert_unichar (GString *string,
615 /* We could be somewhat more efficient here by computing
616 * the length, adding the space, then converting into that
617 * space, by cut-and-pasting the internals of g_unichar_to_utf8.
619 g_return_val_if_fail (string != NULL, NULL);
621 charlen = g_unichar_to_utf8 (wc, buf);
622 return g_string_insert_len (string, pos, buf, charlen);
626 g_string_erase (GString *string,
630 g_return_val_if_fail (string != NULL, NULL);
631 g_return_val_if_fail (pos >= 0, string);
632 g_return_val_if_fail (pos <= string->len, string);
635 len = string->len - pos;
638 g_return_val_if_fail (pos + len <= string->len, string);
640 if (pos + len < string->len)
641 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
646 string->str[string->len] = 0;
652 * g_string_ascii_down:
655 * Converts all upper case ASCII letters to lower case ASCII letters.
657 * Return value: passed-in @string pointer, with all the upper case
658 * characters converted to lower case in place, with
659 * semantics that exactly match g_ascii_tolower.
662 g_string_ascii_down (GString *string)
665 gint n = string->len;
667 g_return_val_if_fail (string != NULL, NULL);
673 *s = g_ascii_tolower (*s);
685 * Converts all lower case ASCII letters to upper case ASCII letters.
687 * Return value: passed-in @string pointer, with all the lower case
688 * characters converted to upper case in place, with
689 * semantics that exactly match g_ascii_toupper.
692 g_string_ascii_up (GString *string)
695 gint n = string->len;
697 g_return_val_if_fail (string != NULL, NULL);
703 *s = g_ascii_toupper (*s);
713 * @string: a #GString
715 * Converts a #GString to lowercase.
717 * Returns: the #GString.
719 * Deprecated: This function uses the locale-specific tolower() function,
720 * which is almost never the right thing. Use g_string_ascii_down() or
721 * g_utf8_strdown() instead.
724 g_string_down (GString *string)
727 glong n = string->len;
729 g_return_val_if_fail (string != NULL, NULL);
731 s = (guchar *) string->str;
746 * @string: a #GString
748 * Converts a #GString to uppercase.
750 * Return value: the #GString
752 * Deprecated: This function uses the locale-specific toupper() function,
753 * which is almost never the right thing. Use g_string_ascii_up() or
754 * g_utf8_strup() instead.
757 g_string_up (GString *string)
760 glong n = string->len;
762 g_return_val_if_fail (string != NULL, NULL);
764 s = (guchar *) string->str;
778 g_string_append_printf_internal (GString *string,
784 buffer = g_strdup_vprintf (fmt, args);
785 g_string_append (string, buffer);
790 g_string_printf (GString *string,
796 g_string_truncate (string, 0);
798 va_start (args, fmt);
799 g_string_append_printf_internal (string, fmt, args);
804 g_string_append_printf (GString *string,
810 va_start (args, fmt);
811 g_string_append_printf_internal (string, fmt, args);