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.
27 typedef struct _GRealStringChunk GRealStringChunk;
28 typedef struct _GRealString GRealString;
30 struct _GRealStringChunk
32 GHashTable *const_table;
47 static GMemChunk *string_mem_chunk = NULL;
53 g_str_equal (gconstpointer v, gconstpointer v2)
55 return strcmp ((const gchar*) v, (const gchar*)v2) == 0;
58 /* a char* hash function from ASU */
60 g_str_hash (gconstpointer v)
62 const char *s = (char*)v;
66 for(p = s; *p != '\0'; p += 1) {
68 if ( ( g = h & 0xf0000000 ) ) {
82 g_string_chunk_new (gint default_size)
84 GRealStringChunk *new_chunk = g_new (GRealStringChunk, 1);
87 while (size < default_size)
90 new_chunk->const_table = NULL;
91 new_chunk->storage_list = NULL;
92 new_chunk->storage_next = size;
93 new_chunk->default_size = size;
94 new_chunk->this_size = size;
96 return (GStringChunk*) new_chunk;
100 g_string_chunk_free (GStringChunk *fchunk)
102 GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
105 g_return_if_fail (chunk != NULL);
107 if (chunk->storage_list)
109 GListAllocator *tmp_allocator = g_slist_set_allocator (NULL);
111 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
112 g_free (tmp_list->data);
114 g_slist_free (chunk->storage_list);
116 g_slist_set_allocator (tmp_allocator);
119 if (chunk->const_table)
120 g_hash_table_destroy (chunk->const_table);
126 g_string_chunk_insert (GStringChunk *fchunk,
129 GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
130 gint len = strlen (string);
133 g_return_val_if_fail (chunk != NULL, NULL);
135 if ((chunk->storage_next + len + 1) > chunk->this_size)
137 GListAllocator *tmp_allocator = g_slist_set_allocator (NULL);
138 gint new_size = chunk->default_size;
140 while (new_size < len+1)
143 chunk->storage_list = g_slist_prepend (chunk->storage_list,
144 g_new (char, new_size));
146 chunk->this_size = new_size;
147 chunk->storage_next = 0;
149 g_slist_set_allocator (tmp_allocator);
152 pos = ((char*)chunk->storage_list->data) + chunk->storage_next;
154 strcpy (pos, string);
156 chunk->storage_next += len + 1;
162 g_string_chunk_insert_const (GStringChunk *fchunk,
165 GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
168 g_return_val_if_fail (chunk != NULL, NULL);
170 if (!chunk->const_table)
171 chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
173 lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
177 lookup = g_string_chunk_insert (fchunk, string);
178 g_hash_table_insert (chunk->const_table, lookup, lookup);
187 nearest_pow (gint num)
198 g_string_maybe_expand (GRealString* string, gint len)
200 if (string->len + len >= string->alloc)
202 string->alloc = nearest_pow (string->len + len + 1);
203 string->str = g_realloc (string->str, string->alloc);
208 g_string_sized_new (guint dfl_size)
212 if (!string_mem_chunk)
213 string_mem_chunk = g_mem_chunk_new ("string mem chunk",
214 sizeof (GRealString),
215 1024, G_ALLOC_AND_FREE);
217 string = g_chunk_new (GRealString, string_mem_chunk);
223 g_string_maybe_expand (string, MAX (dfl_size, 2));
226 return (GString*) string;
230 g_string_new (const gchar *init)
234 string = g_string_sized_new (2);
237 g_string_append (string, init);
243 g_string_free (GString *string,
246 g_return_if_fail (string != NULL);
249 g_free (string->str);
251 g_mem_chunk_free (string_mem_chunk, string);
255 g_string_assign (GString *lval,
258 g_string_truncate (lval, 0);
259 g_string_append (lval, rval);
265 g_string_truncate (GString* fstring,
268 GRealString *string = (GRealString*)fstring;
270 g_return_val_if_fail (string != NULL, NULL);
274 string->str[len] = 0;
280 g_string_append (GString *fstring,
283 GRealString *string = (GRealString*)fstring;
286 g_return_val_if_fail (string != NULL, NULL);
287 g_return_val_if_fail (val != NULL, fstring);
290 g_string_maybe_expand (string, len);
292 strcpy (string->str + string->len, val);
300 g_string_append_c (GString *fstring,
303 GRealString *string = (GRealString*)fstring;
305 g_return_val_if_fail (string != NULL, NULL);
306 g_string_maybe_expand (string, 1);
308 string->str[string->len++] = c;
309 string->str[string->len] = 0;
315 g_string_prepend (GString *fstring,
318 GRealString *string = (GRealString*)fstring;
321 g_return_val_if_fail (string != NULL, NULL);
322 g_return_val_if_fail (val != NULL, fstring);
325 g_string_maybe_expand (string, len);
327 g_memmove (string->str + len, string->str, string->len);
329 strncpy (string->str, val, len);
333 string->str[string->len] = 0;
339 g_string_prepend_c (GString *fstring,
342 GRealString *string = (GRealString*)fstring;
344 g_return_val_if_fail (string != NULL, NULL);
345 g_string_maybe_expand (string, 1);
347 g_memmove (string->str + 1, string->str, string->len);
353 string->str[string->len] = 0;
359 g_string_insert (GString *fstring,
363 GRealString *string = (GRealString*)fstring;
366 g_return_val_if_fail (string != NULL, NULL);
367 g_return_val_if_fail (val != NULL, fstring);
368 g_return_val_if_fail (pos >= 0, fstring);
369 g_return_val_if_fail (pos <= string->len, fstring);
372 g_string_maybe_expand (string, len);
374 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
376 strncpy (string->str + pos, val, len);
380 string->str[string->len] = 0;
386 g_string_insert_c (GString *fstring,
390 GRealString *string = (GRealString*)fstring;
392 g_return_val_if_fail (string != NULL, NULL);
393 g_return_val_if_fail (pos <= string->len, fstring);
395 g_string_maybe_expand (string, 1);
397 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
399 string->str[pos] = c;
403 string->str[string->len] = 0;
409 g_string_erase (GString *fstring,
413 GRealString *string = (GRealString*)fstring;
415 g_return_val_if_fail (string != NULL, NULL);
416 g_return_val_if_fail (len >= 0, fstring);
417 g_return_val_if_fail (pos >= 0, fstring);
418 g_return_val_if_fail (pos <= string->len, fstring);
419 g_return_val_if_fail (pos + len <= string->len, fstring);
421 if (pos + len < string->len)
422 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
426 string->str[string->len] = 0;
432 g_string_down (GString *fstring)
434 GRealString *string = (GRealString*)fstring;
437 g_return_val_if_fail (string != NULL, NULL);
451 g_string_up (GString *fstring)
453 GRealString *string = (GRealString*)fstring;
456 g_return_val_if_fail (string != NULL, NULL);
470 get_length_upper_bound (const gchar* fmt, va_list *args)
488 while (*fmt && !done)
493 len += va_arg(*args, int);
505 len += strtol (fmt, (char **)&fmt, 10);
514 /* I ignore 'q' and 'L', they're not portable anyway. */
517 tmp = va_arg(*args, char *);
521 len += strlen ("(null)");
531 (void)va_arg (*args, long);
533 (void)va_arg (*args, int);
535 (void)va_arg (*args, int);
542 (void)va_arg (*args, long);
550 (void)va_arg (*args, double);
555 (void)va_arg (*args, int);
561 (void)va_arg (*args, void*);
582 g_vsprintf (const gchar *fmt,
586 static gchar *buf = NULL;
587 static gint alloc = 0;
589 gint len = get_length_upper_bound (fmt, args);
596 alloc = nearest_pow (MAX(len + 1, 1024 + 1));
598 buf = g_new (char, alloc);
601 vsprintf (buf, fmt, *args2);
607 g_string_sprintfa_int (GString *string,
612 g_string_append (string, g_vsprintf (fmt, args, args2));
616 g_string_sprintf (GString *string,
623 va_start(args2, fmt);
625 g_string_truncate (string, 0);
627 g_string_sprintfa_int (string, fmt, &args, &args2);
634 g_string_sprintfa (GString *string,
641 va_start(args2, fmt);
643 g_string_sprintfa_int (string, fmt, &args, &args2);