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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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 v, gconstpointer v2)
73 return strcmp ((const gchar*) v, (const gchar*)v2) == 0;
76 /* a char* hash function from ASU */
78 g_str_hash (gconstpointer v)
80 const char *s = (char*)v;
84 for(p = s; *p != '\0'; p += 1) {
86 if ( ( g = h & 0xf0000000 ) ) {
100 g_string_chunk_new (gint default_size)
102 GRealStringChunk *new_chunk = g_new (GRealStringChunk, 1);
105 while (size < default_size)
108 new_chunk->const_table = NULL;
109 new_chunk->storage_list = NULL;
110 new_chunk->storage_next = size;
111 new_chunk->default_size = size;
112 new_chunk->this_size = size;
114 return (GStringChunk*) new_chunk;
118 g_string_chunk_free (GStringChunk *fchunk)
120 GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
123 g_return_if_fail (chunk != NULL);
125 if (chunk->storage_list)
127 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
128 g_free (tmp_list->data);
130 g_slist_free (chunk->storage_list);
133 if (chunk->const_table)
134 g_hash_table_destroy (chunk->const_table);
140 g_string_chunk_insert (GStringChunk *fchunk,
143 GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
144 gint len = strlen (string);
147 g_return_val_if_fail (chunk != NULL, NULL);
149 if ((chunk->storage_next + len + 1) > chunk->this_size)
151 gint new_size = chunk->default_size;
153 while (new_size < len+1)
156 chunk->storage_list = g_slist_prepend (chunk->storage_list,
157 g_new (char, new_size));
159 chunk->this_size = new_size;
160 chunk->storage_next = 0;
163 pos = ((char*)chunk->storage_list->data) + chunk->storage_next;
165 strcpy (pos, string);
167 chunk->storage_next += len + 1;
173 g_string_chunk_insert_const (GStringChunk *fchunk,
176 GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
179 g_return_val_if_fail (chunk != NULL, NULL);
181 if (!chunk->const_table)
182 chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
184 lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
188 lookup = g_string_chunk_insert (fchunk, string);
189 g_hash_table_insert (chunk->const_table, lookup, lookup);
198 nearest_power (gint num)
209 g_string_maybe_expand (GRealString* string, gint len)
211 if (string->len + len >= string->alloc)
213 string->alloc = nearest_power (string->len + len + 1);
214 string->str = g_realloc (string->str, string->alloc);
219 g_string_sized_new (guint dfl_size)
223 G_LOCK (string_mem_chunk);
224 if (!string_mem_chunk)
225 string_mem_chunk = g_mem_chunk_new ("string mem chunk",
226 sizeof (GRealString),
227 1024, G_ALLOC_AND_FREE);
229 string = g_chunk_new (GRealString, string_mem_chunk);
230 G_UNLOCK (string_mem_chunk);
236 g_string_maybe_expand (string, MAX (dfl_size, 2));
239 return (GString*) string;
243 g_string_new (const gchar *init)
247 string = g_string_sized_new (2);
250 g_string_append (string, init);
256 g_string_free (GString *string,
259 g_return_if_fail (string != NULL);
262 g_free (string->str);
264 G_LOCK (string_mem_chunk);
265 g_mem_chunk_free (string_mem_chunk, string);
266 G_UNLOCK (string_mem_chunk);
270 g_string_assign (GString *lval,
273 g_string_truncate (lval, 0);
274 g_string_append (lval, rval);
280 g_string_truncate (GString* fstring,
283 GRealString *string = (GRealString*)fstring;
285 g_return_val_if_fail (string != NULL, NULL);
289 string->str[len] = 0;
295 g_string_append (GString *fstring,
298 GRealString *string = (GRealString*)fstring;
301 g_return_val_if_fail (string != NULL, NULL);
302 g_return_val_if_fail (val != NULL, fstring);
305 g_string_maybe_expand (string, len);
307 strcpy (string->str + string->len, val);
315 g_string_append_c (GString *fstring,
318 GRealString *string = (GRealString*)fstring;
320 g_return_val_if_fail (string != NULL, NULL);
321 g_string_maybe_expand (string, 1);
323 string->str[string->len++] = c;
324 string->str[string->len] = 0;
330 g_string_prepend (GString *fstring,
333 GRealString *string = (GRealString*)fstring;
336 g_return_val_if_fail (string != NULL, NULL);
337 g_return_val_if_fail (val != NULL, fstring);
340 g_string_maybe_expand (string, len);
342 g_memmove (string->str + len, string->str, string->len);
344 strncpy (string->str, val, len);
348 string->str[string->len] = 0;
354 g_string_prepend_c (GString *fstring,
357 GRealString *string = (GRealString*)fstring;
359 g_return_val_if_fail (string != NULL, NULL);
360 g_string_maybe_expand (string, 1);
362 g_memmove (string->str + 1, string->str, string->len);
368 string->str[string->len] = 0;
374 g_string_insert (GString *fstring,
378 GRealString *string = (GRealString*)fstring;
381 g_return_val_if_fail (string != NULL, NULL);
382 g_return_val_if_fail (val != NULL, fstring);
383 g_return_val_if_fail (pos >= 0, fstring);
384 g_return_val_if_fail (pos <= string->len, fstring);
387 g_string_maybe_expand (string, len);
389 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
391 strncpy (string->str + pos, val, len);
395 string->str[string->len] = 0;
401 g_string_insert_c (GString *fstring,
405 GRealString *string = (GRealString*)fstring;
407 g_return_val_if_fail (string != NULL, NULL);
408 g_return_val_if_fail (pos <= string->len, fstring);
410 g_string_maybe_expand (string, 1);
412 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
414 string->str[pos] = c;
418 string->str[string->len] = 0;
424 g_string_erase (GString *fstring,
428 GRealString *string = (GRealString*)fstring;
430 g_return_val_if_fail (string != NULL, NULL);
431 g_return_val_if_fail (len >= 0, fstring);
432 g_return_val_if_fail (pos >= 0, fstring);
433 g_return_val_if_fail (pos <= string->len, fstring);
434 g_return_val_if_fail (pos + len <= string->len, fstring);
436 if (pos + len < string->len)
437 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
441 string->str[string->len] = 0;
447 g_string_down (GString *fstring)
449 GRealString *string = (GRealString*)fstring;
452 g_return_val_if_fail (string != NULL, NULL);
466 g_string_up (GString *fstring)
468 GRealString *string = (GRealString*)fstring;
471 g_return_val_if_fail (string != NULL, NULL);
485 g_string_sprintfa_int (GString *string,
491 buffer = g_strdup_vprintf (fmt, args);
492 g_string_append (string, buffer);
497 g_string_sprintf (GString *string,
503 g_string_truncate (string, 0);
505 va_start (args, fmt);
506 g_string_sprintfa_int (string, fmt, args);
511 g_string_sprintfa (GString *string,
517 va_start (args, fmt);
518 g_string_sprintfa_int (string, fmt, args);