Initial revision
[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 <glib.h>
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.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 int
470 get_length_upper_bound (const gchar* fmt, va_list *args)
471 {
472   int len = 0;
473   int short_int;
474   int long_int;
475   int done;
476   char *tmp;
477
478   while (*fmt)
479     {
480       char c = *fmt++;
481
482       short_int = FALSE;
483       long_int = FALSE;
484
485       if (c == '%')
486         {
487           done = FALSE;
488           while (*fmt && !done)
489             {
490               switch (*fmt++)
491                 {
492                 case '*':
493                   len += va_arg(*args, int);
494                   break;
495                 case '1':
496                 case '2':
497                 case '3':
498                 case '4':
499                 case '5':
500                 case '6':
501                 case '7':
502                 case '8':
503                 case '9':
504                   fmt -= 1;
505                   len += strtol (fmt, (char **)&fmt, 10);
506                   break;
507                 case 'h':
508                   short_int = TRUE;
509                   break;
510                 case 'l':
511                   long_int = TRUE;
512                   break;
513
514                   /* I ignore 'q' and 'L', they're not portable anyway. */
515
516                 case 's':
517                   tmp = va_arg(*args, char *);
518                   if(tmp)
519                     len += strlen (tmp);
520                   else
521                     len += strlen ("(null)");
522                   done = TRUE;
523                   break;
524                 case 'd':
525                 case 'i':
526                 case 'o':
527                 case 'u':
528                 case 'x':
529                 case 'X':
530                   if (long_int)
531                     (void)va_arg (*args, long);
532                   else if (short_int)
533                     (void)va_arg (*args, int);
534                   else
535                     (void)va_arg (*args, int);
536                   len += 32;
537                   done = TRUE;
538                   break;
539                 case 'D':
540                 case 'O':
541                 case 'U':
542                   (void)va_arg (*args, long);
543                   len += 32;
544                   done = TRUE;
545                   break;
546                 case 'e':
547                 case 'E':
548                 case 'f':
549                 case 'g':
550                   (void)va_arg (*args, double);
551                   len += 32;
552                   done = TRUE;
553                   break;
554                 case 'c':
555                   (void)va_arg (*args, int);
556                   len += 1;
557                   done = TRUE;
558                   break;
559                 case 'p':
560                 case 'n':
561                   (void)va_arg (*args, void*);
562                   len += 32;
563                   done = TRUE;
564                   break;
565                 case '%':
566                   len += 1;
567                   done = TRUE;
568                   break;
569                 default:
570                   break;
571                 }
572             }
573         }
574       else
575         len += 1;
576     }
577
578   return len;
579 }
580
581 char*
582 g_vsprintf (const gchar *fmt,
583             va_list *args,
584             va_list *args2)
585 {
586   static gchar *buf = NULL;
587   static gint   alloc = 0;
588
589   gint len = get_length_upper_bound (fmt, args);
590
591   if (len >= alloc)
592     {
593       if (buf)
594         g_free (buf);
595
596       alloc = nearest_pow (MAX(len + 1, 1024));
597
598       buf = g_new (char, alloc);
599     }
600
601   vsprintf (buf, fmt, *args2);
602
603   return buf;
604 }
605
606 static void
607 g_string_sprintfa_int (GString *string,
608                        const gchar *fmt,
609                        va_list *args,
610                        va_list *args2)
611 {
612   g_string_append (string, g_vsprintf (fmt, args, args2));
613 }
614
615 void
616 g_string_sprintf (GString *string,
617                   const gchar *fmt,
618                   ...)
619 {
620   va_list args, args2;
621
622   va_start(args, fmt);
623   va_start(args2, fmt);
624
625   g_string_truncate (string, 0);
626
627   g_string_sprintfa_int (string, fmt, &args, &args2);
628
629   va_end(args);
630   va_end(args2);
631 }
632
633 void
634 g_string_sprintfa (GString *string,
635                    const gchar *fmt,
636                    ...)
637 {
638   va_list args, args2;
639
640   va_start(args, fmt);
641   va_start(args2, fmt);
642
643   g_string_sprintfa_int (string, fmt, &args, &args2);
644
645   va_end(args);
646   va_end(args2);
647 }