removed this function which was not publically exported in glib.h. to
[platform/upstream/glib.git] / glib / gstring.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19 #include <stdarg.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <ctype.h>
24 #include "glib.h"
25
26
27 typedef struct _GRealStringChunk GRealStringChunk;
28 typedef struct _GRealString      GRealString;
29
30 struct _GRealStringChunk
31 {
32   GHashTable *const_table;
33   GSList     *storage_list;
34   gint        storage_next;
35   gint        this_size;
36   gint        default_size;
37 };
38
39 struct _GRealString
40 {
41   gchar *str;
42   gint   len;
43   gint   alloc;
44 };
45
46
47 static GMemChunk *string_mem_chunk = NULL;
48
49 /* Hash Functions.
50  */
51
52 gint
53 g_str_equal (gconstpointer v, gconstpointer v2)
54 {
55   return strcmp ((const gchar*) v, (const gchar*)v2) == 0;
56 }
57
58 /* a char* hash function from ASU */
59 guint
60 g_str_hash (gconstpointer v)
61 {
62   const char *s = (char*)v;
63   const char *p;
64   guint h=0, g;
65
66   for(p = s; *p != '\0'; p += 1) {
67     h = ( h << 4 ) + *p;
68     if ( ( g = h & 0xf0000000 ) ) {
69       h = h ^ (g >> 24);
70       h = h ^ g;
71     }
72   }
73
74   return h /* % M */;
75 }
76
77
78 /* String Chunks.
79  */
80
81 GStringChunk*
82 g_string_chunk_new (gint default_size)
83 {
84   GRealStringChunk *new_chunk = g_new (GRealStringChunk, 1);
85   gint size = 1;
86
87   while (size < default_size)
88     size <<= 1;
89
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;
95
96   return (GStringChunk*) new_chunk;
97 }
98
99 void
100 g_string_chunk_free (GStringChunk *fchunk)
101 {
102   GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
103   GSList *tmp_list;
104
105   g_return_if_fail (chunk != NULL);
106
107   if (chunk->storage_list)
108     {
109       GListAllocator *tmp_allocator = g_slist_set_allocator (NULL);
110
111       for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
112         g_free (tmp_list->data);
113
114       g_slist_free (chunk->storage_list);
115
116       g_slist_set_allocator (tmp_allocator);
117     }
118
119   if (chunk->const_table)
120     g_hash_table_destroy (chunk->const_table);
121
122   g_free (chunk);
123 }
124
125 gchar*
126 g_string_chunk_insert (GStringChunk *fchunk,
127                        const gchar  *string)
128 {
129   GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
130   gint len = strlen (string);
131   char* pos;
132
133   g_return_val_if_fail (chunk != NULL, NULL);
134
135   if ((chunk->storage_next + len + 1) > chunk->this_size)
136     {
137       GListAllocator *tmp_allocator = g_slist_set_allocator (NULL);
138       gint new_size = chunk->default_size;
139
140       while (new_size < len+1)
141         new_size <<= 1;
142
143       chunk->storage_list = g_slist_prepend (chunk->storage_list,
144                                              g_new (char, new_size));
145
146       chunk->this_size = new_size;
147       chunk->storage_next = 0;
148
149       g_slist_set_allocator (tmp_allocator);
150     }
151
152   pos = ((char*)chunk->storage_list->data) + chunk->storage_next;
153
154   strcpy (pos, string);
155
156   chunk->storage_next += len + 1;
157
158   return pos;
159 }
160
161 gchar*
162 g_string_chunk_insert_const (GStringChunk *fchunk,
163                              const gchar  *string)
164 {
165   GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
166   char* lookup;
167
168   g_return_val_if_fail (chunk != NULL, NULL);
169
170   if (!chunk->const_table)
171     chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
172
173   lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
174
175   if (!lookup)
176     {
177       lookup = g_string_chunk_insert (fchunk, string);
178       g_hash_table_insert (chunk->const_table, lookup, lookup);
179     }
180
181   return lookup;
182 }
183
184 /* Strings.
185  */
186 static gint
187 nearest_pow (gint num)
188 {
189   gint n = 1;
190
191   while (n < num)
192     n <<= 1;
193
194   return n;
195 }
196
197 static void
198 g_string_maybe_expand (GRealString* string, gint len)
199 {
200   if (string->len + len >= string->alloc)
201     {
202       string->alloc = nearest_pow (string->len + len + 1);
203       string->str = g_realloc (string->str, string->alloc);
204     }
205 }
206
207 GString*
208 g_string_sized_new (guint dfl_size)
209 {
210   GRealString *string;
211
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);
216
217   string = g_chunk_new (GRealString, string_mem_chunk);
218
219   string->alloc = 0;
220   string->len   = 0;
221   string->str   = NULL;
222
223   g_string_maybe_expand (string, MAX (dfl_size, 2));
224   string->str[0] = 0;
225
226   return (GString*) string;
227 }
228
229 GString*
230 g_string_new (const gchar *init)
231 {
232   GString *string;
233
234   string = g_string_sized_new (2);
235
236   if (init)
237     g_string_append (string, init);
238
239   return string;
240 }
241
242 void
243 g_string_free (GString *string,
244                gint free_segment)
245 {
246   g_return_if_fail (string != NULL);
247
248   if (free_segment)
249     g_free (string->str);
250
251   g_mem_chunk_free (string_mem_chunk, string);
252 }
253
254 GString*
255 g_string_assign (GString *lval,
256                  const gchar *rval)
257 {
258   g_string_truncate (lval, 0);
259   g_string_append (lval, rval);
260
261   return lval;
262 }
263
264 GString*
265 g_string_truncate (GString* fstring,
266                    gint len)
267 {
268   GRealString *string = (GRealString*)fstring;
269
270   g_return_val_if_fail (string != NULL, NULL);
271
272   string->len = len;
273
274   string->str[len] = 0;
275
276   return fstring;
277 }
278
279 GString*
280 g_string_append (GString *fstring,
281                  const gchar *val)
282 {
283   GRealString *string = (GRealString*)fstring;
284   int len;
285
286   g_return_val_if_fail (string != NULL, NULL);
287   g_return_val_if_fail (val != NULL, fstring);
288   
289   len = strlen (val);
290   g_string_maybe_expand (string, len);
291
292   strcpy (string->str + string->len, val);
293
294   string->len += len;
295
296   return fstring;
297 }
298
299 GString*
300 g_string_append_c (GString *fstring,
301                    gchar c)
302 {
303   GRealString *string = (GRealString*)fstring;
304
305   g_return_val_if_fail (string != NULL, NULL);
306   g_string_maybe_expand (string, 1);
307
308   string->str[string->len++] = c;
309   string->str[string->len] = 0;
310
311   return fstring;
312 }
313
314 GString*
315 g_string_prepend (GString *fstring,
316                   const gchar *val)
317 {
318   GRealString *string = (GRealString*)fstring;
319   gint len;
320
321   g_return_val_if_fail (string != NULL, NULL);
322   g_return_val_if_fail (val != NULL, fstring);
323
324   len = strlen (val);
325   g_string_maybe_expand (string, len);
326
327   g_memmove (string->str + len, string->str, string->len);
328
329   strncpy (string->str, val, len);
330
331   string->len += len;
332
333   string->str[string->len] = 0;
334
335   return fstring;
336 }
337
338 GString*
339 g_string_prepend_c (GString *fstring,
340                     gchar    c)
341 {
342   GRealString *string = (GRealString*)fstring;
343
344   g_return_val_if_fail (string != NULL, NULL);
345   g_string_maybe_expand (string, 1);
346
347   g_memmove (string->str + 1, string->str, string->len);
348
349   string->str[0] = c;
350
351   string->len += 1;
352
353   string->str[string->len] = 0;
354
355   return fstring;
356 }
357
358 GString*
359 g_string_insert (GString     *fstring,
360                  gint         pos,
361                  const gchar *val)
362 {
363   GRealString *string = (GRealString*)fstring;
364   gint len;
365
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);
370
371   len = strlen (val);
372   g_string_maybe_expand (string, len);
373
374   g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
375
376   strncpy (string->str + pos, val, len);
377
378   string->len += len;
379
380   string->str[string->len] = 0;
381
382   return fstring;
383 }
384
385 GString *
386 g_string_insert_c (GString *fstring,
387                    gint     pos,
388                    gchar    c)
389 {
390   GRealString *string = (GRealString*)fstring;
391
392   g_return_val_if_fail (string != NULL, NULL);
393   g_return_val_if_fail (pos <= string->len, fstring);
394
395   g_string_maybe_expand (string, 1);
396
397   g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
398
399   string->str[pos] = c;
400
401   string->len += 1;
402
403   string->str[string->len] = 0;
404
405   return fstring;
406 }
407
408 GString*
409 g_string_erase (GString *fstring,
410                 gint pos,
411                 gint len)
412 {
413   GRealString *string = (GRealString*)fstring;
414
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);
420
421   if (pos + len < string->len)
422     g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
423
424   string->len -= len;
425   
426   string->str[string->len] = 0;
427
428   return fstring;
429 }
430
431 GString*
432 g_string_down (GString *fstring)
433 {
434   GRealString *string = (GRealString*)fstring;
435   gchar *s;
436
437   g_return_val_if_fail (string != NULL, NULL);
438
439   s = string->str;
440
441   while (*s)
442     {
443       *s = tolower (*s);
444       s++;
445     }
446
447   return fstring;
448 }
449
450 GString*
451 g_string_up (GString *fstring)
452 {
453   GRealString *string = (GRealString*)fstring;
454   gchar *s;
455
456   g_return_val_if_fail (string != NULL, NULL);
457
458   s = string->str;
459
460   while (*s)
461     {
462       *s = toupper (*s);
463       s++;
464     }
465
466   return fstring;
467 }
468
469 static void
470 g_string_sprintfa_int (GString     *string,
471                        const gchar *fmt,
472                        va_list      args)
473 {
474   gchar *buffer;
475
476   buffer = g_strdup_vprintf (fmt, args);
477   g_string_append (string, buffer);
478   g_free (buffer);
479 }
480
481 void
482 g_string_sprintf (GString *string,
483                   const gchar *fmt,
484                   ...)
485 {
486   va_list args;
487
488   g_string_truncate (string, 0);
489
490   va_start (args, fmt);
491   g_string_sprintfa_int (string, fmt, args);
492   va_end (args);
493 }
494
495 void
496 g_string_sprintfa (GString *string,
497                    const gchar *fmt,
498                    ...)
499 {
500   va_list args;
501
502   va_start (args, fmt);
503   g_string_sprintfa_int (string, fmt, args);
504   va_end (args);
505 }