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/.
39 typedef struct _GRealStringChunk GRealStringChunk;
40 typedef struct _GRealString GRealString;
42 struct _GRealStringChunk
44 GHashTable *const_table;
58 G_LOCK_DEFINE_STATIC (string_mem_chunk);
59 static GMemChunk *string_mem_chunk = NULL;
65 g_str_equal (gconstpointer v, gconstpointer v2)
67 return strcmp ((const gchar*) v, (const gchar*)v2) == 0;
70 /* a char* hash function from ASU */
72 g_str_hash (gconstpointer v)
74 const char *s = (char*)v;
78 for(p = s; *p != '\0'; p += 1) {
80 if ( ( g = h & 0xf0000000 ) ) {
94 g_string_chunk_new (gint default_size)
96 GRealStringChunk *new_chunk = g_new (GRealStringChunk, 1);
99 while (size < default_size)
102 new_chunk->const_table = NULL;
103 new_chunk->storage_list = NULL;
104 new_chunk->storage_next = size;
105 new_chunk->default_size = size;
106 new_chunk->this_size = size;
108 return (GStringChunk*) new_chunk;
112 g_string_chunk_free (GStringChunk *fchunk)
114 GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
117 g_return_if_fail (chunk != NULL);
119 if (chunk->storage_list)
121 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
122 g_free (tmp_list->data);
124 g_slist_free (chunk->storage_list);
127 if (chunk->const_table)
128 g_hash_table_destroy (chunk->const_table);
134 g_string_chunk_insert (GStringChunk *fchunk,
137 GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
138 gint len = strlen (string);
141 g_return_val_if_fail (chunk != NULL, NULL);
143 if ((chunk->storage_next + len + 1) > chunk->this_size)
145 gint new_size = chunk->default_size;
147 while (new_size < len+1)
150 chunk->storage_list = g_slist_prepend (chunk->storage_list,
151 g_new (char, new_size));
153 chunk->this_size = new_size;
154 chunk->storage_next = 0;
157 pos = ((char*)chunk->storage_list->data) + chunk->storage_next;
159 strcpy (pos, string);
161 chunk->storage_next += len + 1;
167 g_string_chunk_insert_const (GStringChunk *fchunk,
170 GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
173 g_return_val_if_fail (chunk != NULL, NULL);
175 if (!chunk->const_table)
176 chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
178 lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
182 lookup = g_string_chunk_insert (fchunk, string);
183 g_hash_table_insert (chunk->const_table, lookup, lookup);
192 nearest_pow (gint num)
203 g_string_maybe_expand (GRealString* string, gint len)
205 if (string->len + len >= string->alloc)
207 string->alloc = nearest_pow (string->len + len + 1);
208 string->str = g_realloc (string->str, string->alloc);
213 g_string_sized_new (guint dfl_size)
217 G_LOCK (string_mem_chunk);
218 if (!string_mem_chunk)
219 string_mem_chunk = g_mem_chunk_new ("string mem chunk",
220 sizeof (GRealString),
221 1024, G_ALLOC_AND_FREE);
223 string = g_chunk_new (GRealString, string_mem_chunk);
224 G_UNLOCK (string_mem_chunk);
230 g_string_maybe_expand (string, MAX (dfl_size, 2));
233 return (GString*) string;
237 g_string_new (const gchar *init)
241 string = g_string_sized_new (2);
244 g_string_append (string, init);
250 g_string_free (GString *string,
253 g_return_if_fail (string != NULL);
256 g_free (string->str);
258 G_LOCK (string_mem_chunk);
259 g_mem_chunk_free (string_mem_chunk, string);
260 G_UNLOCK (string_mem_chunk);
264 g_string_assign (GString *lval,
267 g_string_truncate (lval, 0);
268 g_string_append (lval, rval);
274 g_string_truncate (GString* fstring,
277 GRealString *string = (GRealString*)fstring;
279 g_return_val_if_fail (string != NULL, NULL);
283 string->str[len] = 0;
289 g_string_append (GString *fstring,
292 GRealString *string = (GRealString*)fstring;
295 g_return_val_if_fail (string != NULL, NULL);
296 g_return_val_if_fail (val != NULL, fstring);
299 g_string_maybe_expand (string, len);
301 strcpy (string->str + string->len, val);
309 g_string_append_c (GString *fstring,
312 GRealString *string = (GRealString*)fstring;
314 g_return_val_if_fail (string != NULL, NULL);
315 g_string_maybe_expand (string, 1);
317 string->str[string->len++] = c;
318 string->str[string->len] = 0;
324 g_string_prepend (GString *fstring,
327 GRealString *string = (GRealString*)fstring;
330 g_return_val_if_fail (string != NULL, NULL);
331 g_return_val_if_fail (val != NULL, fstring);
334 g_string_maybe_expand (string, len);
336 g_memmove (string->str + len, string->str, string->len);
338 strncpy (string->str, val, len);
342 string->str[string->len] = 0;
348 g_string_prepend_c (GString *fstring,
351 GRealString *string = (GRealString*)fstring;
353 g_return_val_if_fail (string != NULL, NULL);
354 g_string_maybe_expand (string, 1);
356 g_memmove (string->str + 1, string->str, string->len);
362 string->str[string->len] = 0;
368 g_string_insert (GString *fstring,
372 GRealString *string = (GRealString*)fstring;
375 g_return_val_if_fail (string != NULL, NULL);
376 g_return_val_if_fail (val != NULL, fstring);
377 g_return_val_if_fail (pos >= 0, fstring);
378 g_return_val_if_fail (pos <= string->len, fstring);
381 g_string_maybe_expand (string, len);
383 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
385 strncpy (string->str + pos, val, len);
389 string->str[string->len] = 0;
395 g_string_insert_c (GString *fstring,
399 GRealString *string = (GRealString*)fstring;
401 g_return_val_if_fail (string != NULL, NULL);
402 g_return_val_if_fail (pos <= string->len, fstring);
404 g_string_maybe_expand (string, 1);
406 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
408 string->str[pos] = c;
412 string->str[string->len] = 0;
418 g_string_erase (GString *fstring,
422 GRealString *string = (GRealString*)fstring;
424 g_return_val_if_fail (string != NULL, NULL);
425 g_return_val_if_fail (len >= 0, fstring);
426 g_return_val_if_fail (pos >= 0, fstring);
427 g_return_val_if_fail (pos <= string->len, fstring);
428 g_return_val_if_fail (pos + len <= string->len, fstring);
430 if (pos + len < string->len)
431 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
435 string->str[string->len] = 0;
441 g_string_down (GString *fstring)
443 GRealString *string = (GRealString*)fstring;
446 g_return_val_if_fail (string != NULL, NULL);
460 g_string_up (GString *fstring)
462 GRealString *string = (GRealString*)fstring;
465 g_return_val_if_fail (string != NULL, NULL);
479 g_string_sprintfa_int (GString *string,
485 buffer = g_strdup_vprintf (fmt, args);
486 g_string_append (string, buffer);
491 g_string_sprintf (GString *string,
497 g_string_truncate (string, 0);
499 va_start (args, fmt);
500 g_string_sprintfa_int (string, fmt, args);
505 g_string_sprintfa (GString *string,
511 va_start (args, fmt);
512 g_string_sprintfa_int (string, fmt, args);