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/.
45 typedef struct _GRealStringChunk GRealStringChunk;
46 typedef struct _GRealString GRealString;
48 struct _GRealStringChunk
50 GHashTable *const_table;
64 G_LOCK_DEFINE_STATIC (string_mem_chunk);
65 static GMemChunk *string_mem_chunk = NULL;
71 g_str_equal (gconstpointer v1,
74 const gchar *string1 = v1;
75 const gchar *string2 = v2;
77 return strcmp (string1, string2) == 0;
80 /* 31 bit hash function */
82 g_str_hash (gconstpointer key)
88 for (p += 1; *p != '\0'; p++)
89 h = (h << 5) - h + *p;
94 #define MY_MAXSIZE ((gsize)-1)
97 nearest_power (gsize base, gsize num)
99 if (num > MY_MAXSIZE / 2)
118 g_string_chunk_new (gsize default_size)
120 GRealStringChunk *new_chunk = g_new (GRealStringChunk, 1);
123 size = nearest_power (1, default_size);
125 new_chunk->const_table = NULL;
126 new_chunk->storage_list = NULL;
127 new_chunk->storage_next = size;
128 new_chunk->default_size = size;
129 new_chunk->this_size = size;
131 return (GStringChunk*) new_chunk;
135 g_string_chunk_free (GStringChunk *fchunk)
137 GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
140 g_return_if_fail (chunk != NULL);
142 if (chunk->storage_list)
144 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
145 g_free (tmp_list->data);
147 g_slist_free (chunk->storage_list);
150 if (chunk->const_table)
151 g_hash_table_destroy (chunk->const_table);
157 g_string_chunk_insert (GStringChunk *fchunk,
160 GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
161 gsize len = strlen (string);
164 g_return_val_if_fail (chunk != NULL, NULL);
166 if ((chunk->storage_next + len + 1) > chunk->this_size)
168 gsize new_size = nearest_power (chunk->default_size, len + 1);
170 chunk->storage_list = g_slist_prepend (chunk->storage_list,
171 g_new (char, new_size));
173 chunk->this_size = new_size;
174 chunk->storage_next = 0;
177 pos = ((char *) chunk->storage_list->data) + chunk->storage_next;
179 strcpy (pos, string);
181 chunk->storage_next += len + 1;
187 g_string_chunk_insert_const (GStringChunk *fchunk,
190 GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
193 g_return_val_if_fail (chunk != NULL, NULL);
195 if (!chunk->const_table)
196 chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
198 lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
202 lookup = g_string_chunk_insert (fchunk, string);
203 g_hash_table_insert (chunk->const_table, lookup, lookup);
212 g_string_maybe_expand (GRealString* string,
215 if (string->len + len >= string->allocated_len)
217 string->allocated_len = nearest_power (1, string->len + len + 1);
218 string->str = g_realloc (string->str, string->allocated_len);
223 g_string_sized_new (gsize dfl_size)
227 G_LOCK (string_mem_chunk);
228 if (!string_mem_chunk)
229 string_mem_chunk = g_mem_chunk_new ("string mem chunk",
230 sizeof (GRealString),
231 1024, G_ALLOC_AND_FREE);
233 string = g_chunk_new (GRealString, string_mem_chunk);
234 G_UNLOCK (string_mem_chunk);
236 string->allocated_len = 0;
240 g_string_maybe_expand (string, MAX (dfl_size, 2));
243 return (GString*) string;
247 g_string_new (const gchar *init)
251 string = g_string_sized_new (init ? strlen (init) + 2 : 2);
254 g_string_append (string, init);
260 g_string_new_len (const gchar *init,
266 return g_string_new (init);
269 string = g_string_sized_new (len);
272 g_string_append_len (string, init, len);
279 g_string_free (GString *string,
280 gboolean free_segment)
284 g_return_val_if_fail (string != NULL, NULL);
288 g_free (string->str);
292 segment = string->str;
294 G_LOCK (string_mem_chunk);
295 g_mem_chunk_free (string_mem_chunk, string);
296 G_UNLOCK (string_mem_chunk);
302 g_string_equal (const GString *v,
306 GRealString *string1 = (GRealString *) v;
307 GRealString *string2 = (GRealString *) v2;
308 gsize i = string1->len;
310 if (i != string2->len)
326 /* 31 bit hash function */
328 g_string_hash (const GString *str)
330 const gchar *p = str->str;
336 h = (h << 5) - h + *p;
344 g_string_assign (GString *string,
347 g_return_val_if_fail (string != NULL, NULL);
348 g_return_val_if_fail (rval != NULL, string);
350 g_string_truncate (string, 0);
351 g_string_append (string, rval);
357 g_string_truncate (GString *fstring,
360 GRealString *string = (GRealString *) fstring;
362 g_return_val_if_fail (string != NULL, NULL);
364 string->len = MIN (len, string->len);
366 string->str[string->len] = 0;
373 * @string: a #GString
374 * @len: the new length
376 * Sets the length of a #GString. If the length is less than
377 * the current length, the string will be truncated. If the
378 * length is greater than the current length, the contents
379 * of the newly added area are undefined. (However, as
380 * always, string->str[string->len] will be a nul byte.)
382 * Return value: @fstring
385 g_string_set_size (GString *string,
388 GRealString *rstring = (GRealString *) string;
390 g_return_val_if_fail (string != NULL, NULL);
392 if (len >= rstring->allocated_len)
393 g_string_maybe_expand (rstring, len - string->len);
396 rstring->str[len] = 0;
402 g_string_insert_len (GString *fstring,
407 GRealString *string = (GRealString *) fstring;
409 g_return_val_if_fail (string != NULL, NULL);
410 g_return_val_if_fail (val != NULL, fstring);
418 g_return_val_if_fail (pos <= string->len, fstring);
420 g_string_maybe_expand (string, len);
422 /* If we aren't appending at the end, move a hunk
423 * of the old string to the end, opening up space
425 if (pos < string->len)
426 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
428 /* insert the new string */
429 g_memmove (string->str + pos, val, len);
433 string->str[string->len] = 0;
439 g_string_append (GString *fstring,
442 g_return_val_if_fail (fstring != NULL, NULL);
443 g_return_val_if_fail (val != NULL, fstring);
445 return g_string_insert_len (fstring, -1, val, -1);
449 g_string_append_len (GString *string,
453 g_return_val_if_fail (string != NULL, NULL);
454 g_return_val_if_fail (val != NULL, string);
456 return g_string_insert_len (string, -1, val, len);
460 g_string_append_c (GString *fstring,
463 g_return_val_if_fail (fstring != NULL, NULL);
465 return g_string_insert_c (fstring, -1, c);
469 * g_string_append_unichar:
470 * @string: a #GString
471 * @wc: a Unicode character
473 * Converts a Unicode character into UTF-8, and appends it
476 * Return value: @string
479 g_string_append_unichar (GString *string,
482 g_return_val_if_fail (string != NULL, NULL);
484 return g_string_insert_unichar (string, -1, wc);
488 g_string_prepend (GString *fstring,
491 g_return_val_if_fail (fstring != NULL, NULL);
492 g_return_val_if_fail (val != NULL, fstring);
494 return g_string_insert_len (fstring, 0, val, -1);
498 g_string_prepend_len (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, 0, val, len);
509 g_string_prepend_c (GString *fstring,
512 g_return_val_if_fail (fstring != NULL, NULL);
514 return g_string_insert_c (fstring, 0, c);
518 * g_string_append_unichar:
519 * @string: a #GString
520 * @wc: a Unicode character
522 * Converts a Unicode character into UTF-8, and prepends it
525 * Return value: @string
528 g_string_prepend_unichar (GString *string,
531 g_return_val_if_fail (string != NULL, NULL);
533 return g_string_insert_unichar (string, 0, wc);
537 g_string_insert (GString *fstring,
541 g_return_val_if_fail (fstring != NULL, NULL);
542 g_return_val_if_fail (val != NULL, fstring);
544 g_return_val_if_fail (pos <= fstring->len, fstring);
546 return g_string_insert_len (fstring, pos, val, -1);
550 g_string_insert_c (GString *fstring,
554 GRealString *string = (GRealString *) fstring;
556 g_return_val_if_fail (string != NULL, NULL);
558 g_string_maybe_expand (string, 1);
563 g_return_val_if_fail (pos <= string->len, fstring);
565 /* If not just an append, move the old stuff */
566 if (pos < string->len)
567 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
569 string->str[pos] = c;
573 string->str[string->len] = 0;
579 * g_string_insert_unichar:
580 * @string: a #Gstring
581 * @pos: the position at which to insert character, or -1 to
582 * append at the end of the string.
583 * @wc: a Unicode character
585 * Converts a Unicode character into UTF-8, and insert it
586 * into the string at the given position.
588 * Return value: @string
591 g_string_insert_unichar (GString *string,
598 /* We could be somewhat more efficient here by computing
599 * the length, adding the space, then converting into that
600 * space, by cut-and-pasting the internals of g_unichar_to_utf8.
602 g_return_val_if_fail (string != NULL, NULL);
604 charlen = g_unichar_to_utf8 (wc, buf);
605 return g_string_insert_len (string, pos, buf, charlen);
609 g_string_erase (GString *fstring,
613 GRealString *string = (GRealString*)fstring;
615 g_return_val_if_fail (string != NULL, NULL);
616 g_return_val_if_fail (pos >= 0, fstring);
617 g_return_val_if_fail (pos <= string->len, fstring);
620 len = string->len - pos;
623 g_return_val_if_fail (pos + len <= string->len, fstring);
625 if (pos + len < string->len)
626 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
631 string->str[string->len] = 0;
637 * g_string_ascii_down:
640 * Converts all upper case ASCII letters to lower case ASCII letters.
642 * Return value: passed-in @string pointer, with all the upper case
643 * characters converted to lower case in place, with
644 * semantics that exactly match g_ascii_tolower.
647 g_string_ascii_down (GString *string)
650 gint n = string->len;
652 g_return_val_if_fail (string != NULL, NULL);
658 *s = g_ascii_tolower (*s);
670 * Converts all lower case ASCII letters to upper case ASCII letters.
672 * Return value: passed-in @string pointer, with all the lower case
673 * characters converted to upper case in place, with
674 * semantics that exactly match g_ascii_toupper.
677 g_string_ascii_up (GString *string)
680 gint n = string->len;
682 g_return_val_if_fail (string != NULL, NULL);
688 *s = g_ascii_toupper (*s);
697 g_string_down (GString *fstring)
699 GRealString *string = (GRealString *) fstring;
701 glong n = string->len;
703 g_return_val_if_fail (string != NULL, NULL);
705 s = (guchar *) string->str;
719 g_string_up (GString *fstring)
721 GRealString *string = (GRealString *) fstring;
723 glong n = string->len;
725 g_return_val_if_fail (string != NULL, NULL);
727 s = (guchar *) string->str;
741 g_string_printfa_internal (GString *string,
747 buffer = g_strdup_vprintf (fmt, args);
748 g_string_append (string, buffer);
753 g_string_printf (GString *string,
759 g_string_truncate (string, 0);
761 va_start (args, fmt);
762 g_string_printfa_internal (string, fmt, args);
767 g_string_printfa (GString *string,
773 va_start (args, fmt);
774 g_string_printfa_internal (string, fmt, args);