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 * @fstring: 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 *fstring,
388 GRealString *string = (GRealString *) fstring;
390 g_return_val_if_fail (string != NULL, NULL);
392 if (len >= string->allocated_len)
393 g_string_maybe_expand (string, len - fstring->len);
396 string->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_prepend (GString *fstring,
472 g_return_val_if_fail (fstring != NULL, NULL);
473 g_return_val_if_fail (val != NULL, fstring);
475 return g_string_insert_len (fstring, 0, val, -1);
479 g_string_prepend_len (GString *string,
483 g_return_val_if_fail (string != NULL, NULL);
484 g_return_val_if_fail (val != NULL, string);
486 return g_string_insert_len (string, 0, val, len);
490 g_string_prepend_c (GString *fstring,
493 g_return_val_if_fail (fstring != NULL, NULL);
495 return g_string_insert_c (fstring, 0, c);
499 g_string_insert (GString *fstring,
503 g_return_val_if_fail (fstring != NULL, NULL);
504 g_return_val_if_fail (val != NULL, fstring);
506 g_return_val_if_fail (pos <= fstring->len, fstring);
508 return g_string_insert_len (fstring, pos, val, -1);
512 g_string_insert_c (GString *fstring,
516 GRealString *string = (GRealString *) fstring;
518 g_return_val_if_fail (string != NULL, NULL);
520 g_string_maybe_expand (string, 1);
525 g_return_val_if_fail (pos <= string->len, fstring);
527 /* If not just an append, move the old stuff */
528 if (pos < string->len)
529 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
531 string->str[pos] = c;
535 string->str[string->len] = 0;
541 g_string_erase (GString *fstring,
545 GRealString *string = (GRealString*)fstring;
547 g_return_val_if_fail (string != NULL, NULL);
548 g_return_val_if_fail (pos >= 0, fstring);
549 g_return_val_if_fail (pos <= string->len, fstring);
552 len = string->len - pos;
555 g_return_val_if_fail (pos + len <= string->len, fstring);
557 if (pos + len < string->len)
558 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
563 string->str[string->len] = 0;
569 * g_string_ascii_down:
572 * Converts all upper case ASCII letters to lower case ASCII letters.
574 * Return value: passed-in @string pointer, with all the upper case
575 * characters converted to lower case in place, with
576 * semantics that exactly match g_ascii_tolower.
579 g_string_ascii_down (GString *string)
582 gint n = string->len;
584 g_return_val_if_fail (string != NULL, NULL);
590 *s = g_ascii_tolower (*s);
602 * Converts all lower case ASCII letters to upper case ASCII letters.
604 * Return value: passed-in @string pointer, with all the lower case
605 * characters converted to upper case in place, with
606 * semantics that exactly match g_ascii_toupper.
609 g_string_ascii_up (GString *string)
612 gint n = string->len;
614 g_return_val_if_fail (string != NULL, NULL);
620 *s = g_ascii_toupper (*s);
629 g_string_down (GString *fstring)
631 GRealString *string = (GRealString *) fstring;
633 glong n = string->len;
635 g_return_val_if_fail (string != NULL, NULL);
637 s = (guchar *) string->str;
651 g_string_up (GString *fstring)
653 GRealString *string = (GRealString *) fstring;
655 glong n = string->len;
657 g_return_val_if_fail (string != NULL, NULL);
659 s = (guchar *) string->str;
673 g_string_printfa_internal (GString *string,
679 buffer = g_strdup_vprintf (fmt, args);
680 g_string_append (string, buffer);
685 g_string_printf (GString *string,
691 g_string_truncate (string, 0);
693 va_start (args, fmt);
694 g_string_printfa_internal (string, fmt, args);
699 g_string_printfa (GString *string,
705 va_start (args, fmt);
706 g_string_printfa_internal (string, fmt, args);