Improve docs
[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 Lesser 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser 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
20 /*
21  * Modified by the GLib Team and others 1997-2000.  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/. 
25  */
26
27 /* 
28  * MT safe
29  */
30
31 #include "config.h"
32
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #include <stdarg.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <ctype.h>
41
42 #include "glib.h"
43 #include "gprintf.h"
44
45 #include "galias.h"
46
47 /**
48  * SECTION: string_chunks
49  * @title: String Chunks
50  * @short_description: efficient storage of groups of strings
51  *
52  * String chunks are used to store groups of strings. Memory is
53  * allocated in blocks, and as strings are added to the #GStringChunk
54  * they are copied into the next free position in a block. When a block
55  * is full a new block is allocated.
56  *
57  * When storing a large number of strings, string chunks are more
58  * efficient than using g_strdup() since fewer calls to malloc() are
59  * needed, and less memory is wasted in memory allocation overheads.
60  *
61  * By adding strings with g_string_chunk_insert_const() it is also
62  * possible to remove duplicates.
63  *
64  * To create a new #GStringChunk use g_string_chunk_new().
65  *
66  * To add strings to a #GStringChunk use g_string_chunk_insert().
67  *
68  * To add strings to a #GStringChunk, but without duplicating strings
69  * which are already in the #GStringChunk, use
70  * g_string_chunk_insert_const().
71  *
72  * To free the entire #GStringChunk use g_string_chunk_free(). It is
73  * not possible to free individual strings.
74  **/
75
76 /**
77  * GStringChunk:
78  *
79  * An opaque data structure representing String Chunks. It should only
80  * be accessed by using the following functions.
81  **/
82 struct _GStringChunk
83 {
84   GHashTable *const_table;
85   GSList     *storage_list;
86   gsize       storage_next;    
87   gsize       this_size;       
88   gsize       default_size;    
89 };
90
91 /* Hash Functions.
92  */
93
94 /**
95  * g_str_equal:
96  * @v1: a key
97  * @v2: a key to compare with @v1
98  *
99  * Compares two strings for byte-by-byte equality and returns %TRUE
100  * if they are equal. It can be passed to g_hash_table_new() as the
101  * @key_equal_func parameter, when using strings as keys in a #GHashTable.
102  *
103  * Note that this function is primarily meant as a hash table comparison
104  * function. For a general-purpose, %NULL-safe string comparison function,
105  * see g_strcmp0().
106  *
107  * Returns: %TRUE if the two keys match
108  */
109 gboolean
110 g_str_equal (gconstpointer v1,
111              gconstpointer v2)
112 {
113   const gchar *string1 = v1;
114   const gchar *string2 = v2;
115   
116   return strcmp (string1, string2) == 0;
117 }
118
119 /**
120  * g_str_hash:
121  * @v: a string key
122  *
123  * Converts a string to a hash value.
124  * It can be passed to g_hash_table_new() as the @hash_func 
125  * parameter, when using strings as keys in a #GHashTable.
126  *
127  * Returns: a hash value corresponding to the key
128  */
129 guint
130 g_str_hash (gconstpointer v)
131 {
132   /* 31 bit hash function */
133   const signed char *p = v;
134   guint32 h = *p;
135
136   if (h)
137     for (p += 1; *p != '\0'; p++)
138       h = (h << 5) - h + *p;
139
140   return h;
141 }
142
143 #define MY_MAXSIZE ((gsize)-1)
144
145 static inline gsize
146 nearest_power (gsize base, gsize num)    
147 {
148   if (num > MY_MAXSIZE / 2)
149     {
150       return MY_MAXSIZE;
151     }
152   else
153     {
154       gsize n = base;
155
156       while (n < num)
157         n <<= 1;
158       
159       return n;
160     }
161 }
162
163 /* String Chunks.
164  */
165
166 /**
167  * g_string_chunk_new:
168  * @size: the default size of the blocks of memory which are 
169  *        allocated to store the strings. If a particular string 
170  *        is larger than this default size, a larger block of 
171  *        memory will be allocated for it.
172  * 
173  * Creates a new #GStringChunk. 
174  * 
175  * Returns: a new #GStringChunk
176  */
177 GStringChunk*
178 g_string_chunk_new (gsize size)    
179 {
180   GStringChunk *new_chunk = g_new (GStringChunk, 1);
181   gsize actual_size = 1;    
182
183   actual_size = nearest_power (1, size);
184
185   new_chunk->const_table       = NULL;
186   new_chunk->storage_list      = NULL;
187   new_chunk->storage_next      = actual_size;
188   new_chunk->default_size      = actual_size;
189   new_chunk->this_size         = actual_size;
190
191   return new_chunk;
192 }
193
194 /**
195  * g_string_chunk_free:
196  * @chunk: a #GStringChunk 
197  * 
198  * Frees all memory allocated by the #GStringChunk.
199  * After calling g_string_chunk_free() it is not safe to
200  * access any of the strings which were contained within it.
201  */
202 void
203 g_string_chunk_free (GStringChunk *chunk)
204 {
205   GSList *tmp_list;
206
207   g_return_if_fail (chunk != NULL);
208
209   if (chunk->storage_list)
210     {
211       for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
212         g_free (tmp_list->data);
213
214       g_slist_free (chunk->storage_list);
215     }
216
217   if (chunk->const_table)
218     g_hash_table_destroy (chunk->const_table);
219
220   g_free (chunk);
221 }
222
223 /**
224  * g_string_chunk_clear:
225  * @chunk: a #GStringChunk
226  *
227  * Frees all strings contained within the #GStringChunk.
228  * After calling g_string_chunk_clear() it is not safe to
229  * access any of the strings which were contained within it.
230  *
231  * Since: 2.14
232  */
233 void
234 g_string_chunk_clear (GStringChunk *chunk)
235 {
236   GSList *tmp_list;
237
238   g_return_if_fail (chunk != NULL);
239
240   if (chunk->storage_list)
241     {
242       for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
243         g_free (tmp_list->data);
244
245       g_slist_free (chunk->storage_list);
246
247       chunk->storage_list       = NULL;
248       chunk->storage_next       = chunk->default_size;
249       chunk->this_size          = chunk->default_size;
250     }
251
252   if (chunk->const_table)
253       g_hash_table_remove_all (chunk->const_table);
254 }
255
256 /**
257  * g_string_chunk_insert:
258  * @chunk: a #GStringChunk
259  * @string: the string to add
260  * 
261  * Adds a copy of @string to the #GStringChunk.
262  * It returns a pointer to the new copy of the string 
263  * in the #GStringChunk. The characters in the string 
264  * can be changed, if necessary, though you should not 
265  * change anything after the end of the string.
266  *
267  * Unlike g_string_chunk_insert_const(), this function 
268  * does not check for duplicates. Also strings added 
269  * with g_string_chunk_insert() will not be searched 
270  * by g_string_chunk_insert_const() when looking for 
271  * duplicates.
272  * 
273  * Returns: a pointer to the copy of @string within 
274  *          the #GStringChunk
275  */
276 gchar*
277 g_string_chunk_insert (GStringChunk *chunk,
278                        const gchar  *string)
279 {
280   g_return_val_if_fail (chunk != NULL, NULL);
281
282   return g_string_chunk_insert_len (chunk, string, -1);
283 }
284
285 /**
286  * g_string_chunk_insert_const:
287  * @chunk: a #GStringChunk
288  * @string: the string to add
289  *
290  * Adds a copy of @string to the #GStringChunk, unless the same
291  * string has already been added to the #GStringChunk with
292  * g_string_chunk_insert_const().
293  *
294  * This function is useful if you need to copy a large number
295  * of strings but do not want to waste space storing duplicates.
296  * But you must remember that there may be several pointers to
297  * the same string, and so any changes made to the strings
298  * should be done very carefully.
299  *
300  * Note that g_string_chunk_insert_const() will not return a
301  * pointer to a string added with g_string_chunk_insert(), even
302  * if they do match.
303  *
304  * Returns: a pointer to the new or existing copy of @string
305  *          within the #GStringChunk
306  */
307 gchar*
308 g_string_chunk_insert_const (GStringChunk *chunk,
309                              const gchar  *string)
310 {
311   char* lookup;
312
313   g_return_val_if_fail (chunk != NULL, NULL);
314
315   if (!chunk->const_table)
316     chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
317
318   lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
319
320   if (!lookup)
321     {
322       lookup = g_string_chunk_insert (chunk, string);
323       g_hash_table_insert (chunk->const_table, lookup, lookup);
324     }
325
326   return lookup;
327 }
328
329 /**
330  * g_string_chunk_insert_len:
331  * @chunk: a #GStringChunk
332  * @string: bytes to insert
333  * @len: number of bytes of @string to insert, or -1 to insert a
334  *     nul-terminated string
335  *
336  * Adds a copy of the first @len bytes of @string to the #GStringChunk.
337  * The copy is nul-terminated.
338  *
339  * Since this function does not stop at nul bytes, it is the caller's
340  * responsibility to ensure that @string has at least @len addressable
341  * bytes.
342  *
343  * The characters in the returned string can be changed, if necessary,
344  * though you should not change anything after the end of the string.
345  *
346  * Return value: a pointer to the copy of @string within the #GStringChunk
347  *
348  * Since: 2.4
349  */
350 gchar*
351 g_string_chunk_insert_len (GStringChunk *chunk,
352                            const gchar  *string,
353                            gssize        len)
354 {
355   gssize size;
356   gchar* pos;
357
358   g_return_val_if_fail (chunk != NULL, NULL);
359
360   if (len < 0)
361     size = strlen (string);
362   else
363     size = len;
364
365   if ((chunk->storage_next + size + 1) > chunk->this_size)
366     {
367       gsize new_size = nearest_power (chunk->default_size, size + 1);
368
369       chunk->storage_list = g_slist_prepend (chunk->storage_list,
370                                              g_new (gchar, new_size));
371
372       chunk->this_size = new_size;
373       chunk->storage_next = 0;
374     }
375
376   pos = ((gchar *) chunk->storage_list->data) + chunk->storage_next;
377
378   *(pos + size) = '\0';
379
380   memcpy (pos, string, size);
381
382   chunk->storage_next += size + 1;
383
384   return pos;
385 }
386
387 /* Strings.
388  */
389 static void
390 g_string_maybe_expand (GString* string,
391                        gsize    len) 
392 {
393   if (string->len + len >= string->allocated_len)
394     {
395       string->allocated_len = nearest_power (1, string->len + len + 1);
396       string->str = g_realloc (string->str, string->allocated_len);
397     }
398 }
399
400 /**
401  * g_string_sized_new:
402  * @dfl_size: the default size of the space allocated to 
403  *            hold the string
404  *
405  * Creates a new #GString, with enough space for @dfl_size 
406  * bytes. This is useful if you are going to add a lot of 
407  * text to the string and don't want it to be reallocated 
408  * too often.
409  *
410  * Returns: the new #GString
411  */
412 GString*
413 g_string_sized_new (gsize dfl_size)    
414 {
415   GString *string = g_slice_new (GString);
416
417   string->allocated_len = 0;
418   string->len   = 0;
419   string->str   = NULL;
420
421   g_string_maybe_expand (string, MAX (dfl_size, 2));
422   string->str[0] = 0;
423
424   return string;
425 }
426
427 /**
428  * g_string_new:
429  * @init: the initial text to copy into the string
430  * 
431  * Creates a new #GString, initialized with the given string.
432  *
433  * Returns: the new #GString
434  */
435 GString*
436 g_string_new (const gchar *init)
437 {
438   GString *string;
439
440   if (init == NULL || *init == '\0')
441     string = g_string_sized_new (2);
442   else 
443     {
444       gint len;
445
446       len = strlen (init);
447       string = g_string_sized_new (len + 2);
448
449       g_string_append_len (string, init, len);
450     }
451
452   return string;
453 }
454
455 /**
456  * g_string_new_len:
457  * @init: initial contents of the string
458  * @len: length of @init to use
459  *
460  * Creates a new #GString with @len bytes of the @init buffer.  
461  * Because a length is provided, @init need not be nul-terminated,
462  * and can contain embedded nul bytes.
463  *
464  * Since this function does not stop at nul bytes, it is the caller's
465  * responsibility to ensure that @init has at least @len addressable 
466  * bytes.
467  *
468  * Returns: a new #GString
469  */
470 GString*
471 g_string_new_len (const gchar *init,
472                   gssize       len)    
473 {
474   GString *string;
475
476   if (len < 0)
477     return g_string_new (init);
478   else
479     {
480       string = g_string_sized_new (len);
481       
482       if (init)
483         g_string_append_len (string, init, len);
484       
485       return string;
486     }
487 }
488
489 /**
490  * g_string_free:
491  * @string: a #GString
492  * @free_segment: if %TRUE the actual character data is freed as well
493  *
494  * Frees the memory allocated for the #GString.
495  * If @free_segment is %TRUE it also frees the character data.
496  *
497  * Returns: the character data of @string 
498  *          (i.e. %NULL if @free_segment is %TRUE)
499  */
500 gchar*
501 g_string_free (GString *string,
502                gboolean free_segment)
503 {
504   gchar *segment;
505
506   g_return_val_if_fail (string != NULL, NULL);
507
508   if (free_segment)
509     {
510       g_free (string->str);
511       segment = NULL;
512     }
513   else
514     segment = string->str;
515
516   g_slice_free (GString, string);
517
518   return segment;
519 }
520
521 /**
522  * g_string_equal:
523  * @v: a #GString
524  * @v2: another #GString
525  *
526  * Compares two strings for equality, returning %TRUE if they are equal. 
527  * For use with #GHashTable.
528  *
529  * Returns: %TRUE if they strings are the same length and contain the 
530  *   same bytes
531  */
532 gboolean
533 g_string_equal (const GString *v,
534                 const GString *v2)
535 {
536   gchar *p, *q;
537   GString *string1 = (GString *) v;
538   GString *string2 = (GString *) v2;
539   gsize i = string1->len;    
540
541   if (i != string2->len)
542     return FALSE;
543
544   p = string1->str;
545   q = string2->str;
546   while (i)
547     {
548       if (*p != *q)
549         return FALSE;
550       p++;
551       q++;
552       i--;
553     }
554   return TRUE;
555 }
556
557 /**
558  * g_string_hash:
559  * @str: a string to hash
560  *
561  * Creates a hash code for @str; for use with #GHashTable.
562  *
563  * Returns: hash code for @str
564  */
565 /* 31 bit hash function */
566 guint
567 g_string_hash (const GString *str)
568 {
569   const gchar *p = str->str;
570   gsize n = str->len;    
571   guint h = 0;
572
573   while (n--)
574     {
575       h = (h << 5) - h + *p;
576       p++;
577     }
578
579   return h;
580 }
581
582 /**
583  * g_string_assign:
584  * @string: the destination #GString. Its current contents 
585  *          are destroyed.
586  * @rval: the string to copy into @string
587  *
588  * Copies the bytes from a string into a #GString, 
589  * destroying any previous contents. It is rather like 
590  * the standard strcpy() function, except that you do not 
591  * have to worry about having enough space to copy the string.
592  *
593  * Returns: @string
594  */
595 GString*
596 g_string_assign (GString     *string,
597                  const gchar *rval)
598 {
599   g_return_val_if_fail (string != NULL, NULL);
600   g_return_val_if_fail (rval != NULL, string);
601
602   /* Make sure assigning to itself doesn't corrupt the string.  */
603   if (string->str != rval)
604     {
605       /* Assigning from substring should be ok since g_string_truncate
606          does not realloc.  */
607       g_string_truncate (string, 0);
608       g_string_append (string, rval);
609     }
610
611   return string;
612 }
613
614 /**
615  * g_string_truncate:
616  * @string: a #GString
617  * @len: the new size of @string
618  *
619  * Cuts off the end of the GString, leaving the first @len bytes. 
620  *
621  * Returns: @string
622  */
623 GString*
624 g_string_truncate (GString *string,
625                    gsize    len)    
626 {
627   g_return_val_if_fail (string != NULL, NULL);
628
629   string->len = MIN (len, string->len);
630   string->str[string->len] = 0;
631
632   return string;
633 }
634
635 /**
636  * g_string_set_size:
637  * @string: a #GString
638  * @len: the new length
639  * 
640  * Sets the length of a #GString. If the length is less than
641  * the current length, the string will be truncated. If the
642  * length is greater than the current length, the contents
643  * of the newly added area are undefined. (However, as
644  * always, string->str[string->len] will be a nul byte.) 
645  * 
646  * Return value: @string
647  **/
648 GString*
649 g_string_set_size (GString *string,
650                    gsize    len)    
651 {
652   g_return_val_if_fail (string != NULL, NULL);
653
654   if (len >= string->allocated_len)
655     g_string_maybe_expand (string, len - string->len);
656   
657   string->len = len;
658   string->str[len] = 0;
659
660   return string;
661 }
662
663 /**
664  * g_string_insert_len:
665  * @string: a #GString
666  * @pos: position in @string where insertion should 
667  *       happen, or -1 for at the end
668  * @val: bytes to insert
669  * @len: number of bytes of @val to insert
670  * 
671  * Inserts @len bytes of @val into @string at @pos.  
672  * Because @len is provided, @val may contain embedded 
673  * nuls and need not be nul-terminated. If @pos is -1, 
674  * bytes are inserted at the end of the string.
675  *
676  * Since this function does not stop at nul bytes, it is 
677  * the caller's responsibility to ensure that @val has at 
678  * least @len addressable bytes.
679  *
680  * Returns: @string
681  */
682 GString*
683 g_string_insert_len (GString     *string,
684                      gssize       pos,    
685                      const gchar *val,
686                      gssize       len)    
687 {
688   g_return_val_if_fail (string != NULL, NULL);
689   g_return_val_if_fail (val != NULL, string);
690
691   if (len < 0)
692     len = strlen (val);
693
694   if (pos < 0)
695     pos = string->len;
696   else
697     g_return_val_if_fail (pos <= string->len, string);
698
699   /* Check whether val represents a substring of string.  This test
700      probably violates chapter and verse of the C standards, since
701      ">=" and "<=" are only valid when val really is a substring.
702      In practice, it will work on modern archs.  */
703   if (val >= string->str && val <= string->str + string->len)
704     {
705       gsize offset = val - string->str;
706       gsize precount = 0;
707
708       g_string_maybe_expand (string, len);
709       val = string->str + offset;
710       /* At this point, val is valid again.  */
711
712       /* Open up space where we are going to insert.  */
713       if (pos < string->len)
714         g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
715
716       /* Move the source part before the gap, if any.  */
717       if (offset < pos)
718         {
719           precount = MIN (len, pos - offset);
720           memcpy (string->str + pos, val, precount);
721         }
722
723       /* Move the source part after the gap, if any.  */
724       if (len > precount)
725         memcpy (string->str + pos + precount,
726                 val + /* Already moved: */ precount + /* Space opened up: */ len,
727                 len - precount);
728     }
729   else
730     {
731       g_string_maybe_expand (string, len);
732
733       /* If we aren't appending at the end, move a hunk
734        * of the old string to the end, opening up space
735        */
736       if (pos < string->len)
737         g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
738
739       /* insert the new string */
740       if (len == 1)
741         string->str[pos] = *val;
742       else
743         memcpy (string->str + pos, val, len);
744     }
745
746   string->len += len;
747
748   string->str[string->len] = 0;
749
750   return string;
751 }
752
753 #define SUB_DELIM_CHARS  "!$&'()*+,;="
754
755 static gboolean
756 is_valid (char c, const char *reserved_chars_allowed)
757 {
758   if (g_ascii_isalnum (c) ||
759       c == '-' ||
760       c == '.' ||
761       c == '_' ||
762       c == '~')
763     return TRUE;
764
765   if (reserved_chars_allowed &&
766       strchr (reserved_chars_allowed, c) != NULL)
767     return TRUE;
768   
769   return FALSE;
770 }
771
772 static gboolean 
773 gunichar_ok (gunichar c)
774 {
775   return
776     (c != (gunichar) -2) &&
777     (c != (gunichar) -1);
778 }
779
780 /**
781  * g_string_append_uri_escaped:
782  * @string: a #GString
783  * @unescaped: a string
784  * @reserved_chars_allowed: a string of reserved characters allowed to be used, or %NULL
785  * @allow_utf8: set %TRUE if the escaped string may include UTF8 characters
786  * 
787  * Appends @unescaped to @string, escaped any characters that
788  * are reserved in URIs using URI-style escape sequences.
789  * 
790  * Returns: @string
791  *
792  * Since: 2.16
793  **/
794 GString *
795 g_string_append_uri_escaped (GString *string,
796                              const char *unescaped,
797                              const char *reserved_chars_allowed,
798                              gboolean allow_utf8)
799 {
800   unsigned char c;
801   const char *end;
802   static const gchar hex[16] = "0123456789ABCDEF";
803
804   g_return_val_if_fail (string != NULL, NULL);
805   g_return_val_if_fail (unescaped != NULL, NULL);
806
807   end = unescaped + strlen (unescaped);
808   
809   while ((c = *unescaped) != 0)
810     {
811       if (c >= 0x80 && allow_utf8 &&
812           gunichar_ok (g_utf8_get_char_validated (unescaped, end - unescaped)))
813         {
814           int len = g_utf8_skip [c];
815           g_string_append_len (string, unescaped, len);
816           unescaped += len;
817         }
818       else if (is_valid (c, reserved_chars_allowed))
819         {
820           g_string_append_c (string, c);
821           unescaped++;
822         }
823       else
824         {
825           g_string_append_c (string, '%');
826           g_string_append_c (string, hex[((guchar)c) >> 4]);
827           g_string_append_c (string, hex[((guchar)c) & 0xf]);
828           unescaped++;
829         }
830     }
831
832   return string;
833 }
834
835 /**
836  * g_string_append:
837  * @string: a #GString
838  * @val: the string to append onto the end of @string
839  * 
840  * Adds a string onto the end of a #GString, expanding 
841  * it if necessary.
842  *
843  * Returns: @string
844  */
845 GString*
846 g_string_append (GString     *string,
847                  const gchar *val)
848 {  
849   g_return_val_if_fail (string != NULL, NULL);
850   g_return_val_if_fail (val != NULL, string);
851
852   return g_string_insert_len (string, -1, val, -1);
853 }
854
855 /**
856  * g_string_append_len:
857  * @string: a #GString
858  * @val: bytes to append
859  * @len: number of bytes of @val to use
860  * 
861  * Appends @len bytes of @val to @string. Because @len is 
862  * provided, @val may contain embedded nuls and need not 
863  * be nul-terminated.
864  * 
865  * Since this function does not stop at nul bytes, it is 
866  * the caller's responsibility to ensure that @val has at 
867  * least @len addressable bytes.
868  *
869  * Returns: @string
870  */
871 GString*
872 g_string_append_len (GString     *string,
873                      const gchar *val,
874                      gssize       len)    
875 {
876   g_return_val_if_fail (string != NULL, NULL);
877   g_return_val_if_fail (val != NULL, string);
878
879   return g_string_insert_len (string, -1, val, len);
880 }
881
882 /**
883  * g_string_append_c:
884  * @string: a #GString
885  * @c: the byte to append onto the end of @string
886  *
887  * Adds a byte onto the end of a #GString, expanding 
888  * it if necessary.
889  * 
890  * Returns: @string
891  */
892 #undef g_string_append_c
893 GString*
894 g_string_append_c (GString *string,
895                    gchar    c)
896 {
897   g_return_val_if_fail (string != NULL, NULL);
898
899   return g_string_insert_c (string, -1, c);
900 }
901
902 /**
903  * g_string_append_unichar:
904  * @string: a #GString
905  * @wc: a Unicode character
906  * 
907  * Converts a Unicode character into UTF-8, and appends it
908  * to the string.
909  * 
910  * Return value: @string
911  **/
912 GString*
913 g_string_append_unichar (GString  *string,
914                          gunichar  wc)
915 {  
916   g_return_val_if_fail (string != NULL, NULL);
917   
918   return g_string_insert_unichar (string, -1, wc);
919 }
920
921 /**
922  * g_string_prepend:
923  * @string: a #GString
924  * @val: the string to prepend on the start of @string
925  *
926  * Adds a string on to the start of a #GString, 
927  * expanding it if necessary.
928  *
929  * Returns: @string
930  */
931 GString*
932 g_string_prepend (GString     *string,
933                   const gchar *val)
934 {
935   g_return_val_if_fail (string != NULL, NULL);
936   g_return_val_if_fail (val != NULL, string);
937   
938   return g_string_insert_len (string, 0, val, -1);
939 }
940
941 /**
942  * g_string_prepend_len:
943  * @string: a #GString
944  * @val: bytes to prepend
945  * @len: number of bytes in @val to prepend
946  *
947  * Prepends @len bytes of @val to @string. 
948  * Because @len is provided, @val may contain 
949  * embedded nuls and need not be nul-terminated.
950  *
951  * Since this function does not stop at nul bytes, 
952  * it is the caller's responsibility to ensure that 
953  * @val has at least @len addressable bytes.
954  *
955  * Returns: @string
956  */
957 GString*
958 g_string_prepend_len (GString     *string,
959                       const gchar *val,
960                       gssize       len)    
961 {
962   g_return_val_if_fail (string != NULL, NULL);
963   g_return_val_if_fail (val != NULL, string);
964
965   return g_string_insert_len (string, 0, val, len);
966 }
967
968 /**
969  * g_string_prepend_c:
970  * @string: a #GString
971  * @c: the byte to prepend on the start of the #GString
972  *
973  * Adds a byte onto the start of a #GString, 
974  * expanding it if necessary.
975  *
976  * Returns: @string
977  */
978 GString*
979 g_string_prepend_c (GString *string,
980                     gchar    c)
981 {  
982   g_return_val_if_fail (string != NULL, NULL);
983   
984   return g_string_insert_c (string, 0, c);
985 }
986
987 /**
988  * g_string_prepend_unichar:
989  * @string: a #GString
990  * @wc: a Unicode character
991  * 
992  * Converts a Unicode character into UTF-8, and prepends it
993  * to the string.
994  * 
995  * Return value: @string
996  **/
997 GString*
998 g_string_prepend_unichar (GString  *string,
999                           gunichar  wc)
1000 {  
1001   g_return_val_if_fail (string != NULL, NULL);
1002   
1003   return g_string_insert_unichar (string, 0, wc);
1004 }
1005
1006 /**
1007  * g_string_insert:
1008  * @string: a #GString
1009  * @pos: the position to insert the copy of the string
1010  * @val: the string to insert
1011  *
1012  * Inserts a copy of a string into a #GString, 
1013  * expanding it if necessary.
1014  *
1015  * Returns: @string
1016  */
1017 GString*
1018 g_string_insert (GString     *string,
1019                  gssize       pos,    
1020                  const gchar *val)
1021 {
1022   g_return_val_if_fail (string != NULL, NULL);
1023   g_return_val_if_fail (val != NULL, string);
1024   if (pos >= 0)
1025     g_return_val_if_fail (pos <= string->len, string);
1026   
1027   return g_string_insert_len (string, pos, val, -1);
1028 }
1029
1030 /**
1031  * g_string_insert_c:
1032  * @string: a #GString
1033  * @pos: the position to insert the byte
1034  * @c: the byte to insert
1035  *
1036  * Inserts a byte into a #GString, expanding it if necessary.
1037  * 
1038  * Returns: @string
1039  */
1040 GString*
1041 g_string_insert_c (GString *string,
1042                    gssize   pos,    
1043                    gchar    c)
1044 {
1045   g_return_val_if_fail (string != NULL, NULL);
1046
1047   g_string_maybe_expand (string, 1);
1048
1049   if (pos < 0)
1050     pos = string->len;
1051   else
1052     g_return_val_if_fail (pos <= string->len, string);
1053   
1054   /* If not just an append, move the old stuff */
1055   if (pos < string->len)
1056     g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
1057
1058   string->str[pos] = c;
1059
1060   string->len += 1;
1061
1062   string->str[string->len] = 0;
1063
1064   return string;
1065 }
1066
1067 /**
1068  * g_string_insert_unichar:
1069  * @string: a #GString
1070  * @pos: the position at which to insert character, or -1 to
1071  *       append at the end of the string
1072  * @wc: a Unicode character
1073  * 
1074  * Converts a Unicode character into UTF-8, and insert it
1075  * into the string at the given position.
1076  * 
1077  * Return value: @string
1078  **/
1079 GString*
1080 g_string_insert_unichar (GString *string,
1081                          gssize   pos,    
1082                          gunichar wc)
1083 {
1084   gint charlen, first, i;
1085   gchar *dest;
1086
1087   g_return_val_if_fail (string != NULL, NULL);
1088
1089   /* Code copied from g_unichar_to_utf() */
1090   if (wc < 0x80)
1091     {
1092       first = 0;
1093       charlen = 1;
1094     }
1095   else if (wc < 0x800)
1096     {
1097       first = 0xc0;
1098       charlen = 2;
1099     }
1100   else if (wc < 0x10000)
1101     {
1102       first = 0xe0;
1103       charlen = 3;
1104     }
1105    else if (wc < 0x200000)
1106     {
1107       first = 0xf0;
1108       charlen = 4;
1109     }
1110   else if (wc < 0x4000000)
1111     {
1112       first = 0xf8;
1113       charlen = 5;
1114     }
1115   else
1116     {
1117       first = 0xfc;
1118       charlen = 6;
1119     }
1120   /* End of copied code */
1121
1122   g_string_maybe_expand (string, charlen);
1123
1124   if (pos < 0)
1125     pos = string->len;
1126   else
1127     g_return_val_if_fail (pos <= string->len, string);
1128
1129   /* If not just an append, move the old stuff */
1130   if (pos < string->len)
1131     g_memmove (string->str + pos + charlen, string->str + pos, string->len - pos);
1132
1133   dest = string->str + pos;
1134   /* Code copied from g_unichar_to_utf() */
1135   for (i = charlen - 1; i > 0; --i)
1136     {
1137       dest[i] = (wc & 0x3f) | 0x80;
1138       wc >>= 6;
1139     }
1140   dest[0] = wc | first;
1141   /* End of copied code */
1142   
1143   string->len += charlen;
1144
1145   string->str[string->len] = 0;
1146
1147   return string;
1148 }
1149
1150 /**
1151  * g_string_overwrite:
1152  * @string: a #GString
1153  * @pos: the position at which to start overwriting
1154  * @val: the string that will overwrite the @string starting at @pos
1155  * 
1156  * Overwrites part of a string, lengthening it if necessary.
1157  * 
1158  * Return value: @string
1159  *
1160  * Since: 2.14
1161  **/
1162 GString *
1163 g_string_overwrite (GString     *string,
1164                     gsize        pos,
1165                     const gchar *val)
1166 {
1167   g_return_val_if_fail (val != NULL, string);
1168   return g_string_overwrite_len (string, pos, val, strlen (val));
1169 }
1170
1171 /**
1172  * g_string_overwrite_len:
1173  * @string: a #GString
1174  * @pos: the position at which to start overwriting
1175  * @val: the string that will overwrite the @string starting at @pos
1176  * @len: the number of bytes to write from @val
1177  * 
1178  * Overwrites part of a string, lengthening it if necessary. 
1179  * This function will work with embedded nuls.
1180  * 
1181  * Return value: @string
1182  *
1183  * Since: 2.14
1184  **/
1185 GString *
1186 g_string_overwrite_len (GString     *string,
1187                         gsize        pos,
1188                         const gchar *val,
1189                         gssize       len)
1190 {
1191   gsize end;
1192
1193   g_return_val_if_fail (string != NULL, NULL);
1194
1195   if (!len)
1196     return string;
1197
1198   g_return_val_if_fail (val != NULL, string);
1199   g_return_val_if_fail (pos <= string->len, string);
1200
1201   if (len < 0)
1202     len = strlen (val);
1203
1204   end = pos + len;
1205
1206   if (end > string->len)
1207     g_string_maybe_expand (string, end - string->len);
1208
1209   memcpy (string->str + pos, val, len);
1210
1211   if (end > string->len)
1212     {
1213       string->str[end] = '\0';
1214       string->len = end;
1215     }
1216
1217   return string;
1218 }
1219
1220 /**
1221  * g_string_erase:
1222  * @string: a #GString
1223  * @pos: the position of the content to remove
1224  * @len: the number of bytes to remove, or -1 to remove all
1225  *       following bytes
1226  *
1227  * Removes @len bytes from a #GString, starting at position @pos.
1228  * The rest of the #GString is shifted down to fill the gap.
1229  *
1230  * Returns: @string
1231  */
1232 GString*
1233 g_string_erase (GString *string,
1234                 gssize   pos,
1235                 gssize   len)
1236 {
1237   g_return_val_if_fail (string != NULL, NULL);
1238   g_return_val_if_fail (pos >= 0, string);
1239   g_return_val_if_fail (pos <= string->len, string);
1240
1241   if (len < 0)
1242     len = string->len - pos;
1243   else
1244     {
1245       g_return_val_if_fail (pos + len <= string->len, string);
1246
1247       if (pos + len < string->len)
1248         g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
1249     }
1250
1251   string->len -= len;
1252   
1253   string->str[string->len] = 0;
1254
1255   return string;
1256 }
1257
1258 /**
1259  * g_string_ascii_down:
1260  * @string: a GString
1261  * 
1262  * Converts all upper case ASCII letters to lower case ASCII letters.
1263  * 
1264  * Return value: passed-in @string pointer, with all the upper case
1265  *               characters converted to lower case in place, with
1266  *               semantics that exactly match g_ascii_tolower().
1267  **/
1268 GString*
1269 g_string_ascii_down (GString *string)
1270 {
1271   gchar *s;
1272   gint n;
1273
1274   g_return_val_if_fail (string != NULL, NULL);
1275
1276   n = string->len;
1277   s = string->str;
1278
1279   while (n)
1280     {
1281       *s = g_ascii_tolower (*s);
1282       s++;
1283       n--;
1284     }
1285
1286   return string;
1287 }
1288
1289 /**
1290  * g_string_ascii_up:
1291  * @string: a GString
1292  * 
1293  * Converts all lower case ASCII letters to upper case ASCII letters.
1294  * 
1295  * Return value: passed-in @string pointer, with all the lower case
1296  *               characters converted to upper case in place, with
1297  *               semantics that exactly match g_ascii_toupper().
1298  **/
1299 GString*
1300 g_string_ascii_up (GString *string)
1301 {
1302   gchar *s;
1303   gint n;
1304
1305   g_return_val_if_fail (string != NULL, NULL);
1306
1307   n = string->len;
1308   s = string->str;
1309
1310   while (n)
1311     {
1312       *s = g_ascii_toupper (*s);
1313       s++;
1314       n--;
1315     }
1316
1317   return string;
1318 }
1319
1320 /**
1321  * g_string_down:
1322  * @string: a #GString
1323  *  
1324  * Converts a #GString to lowercase.
1325  *
1326  * Returns: the #GString.
1327  *
1328  * Deprecated:2.2: This function uses the locale-specific 
1329  *   tolower() function, which is almost never the right thing. 
1330  *   Use g_string_ascii_down() or g_utf8_strdown() instead.
1331  */
1332 GString*
1333 g_string_down (GString *string)
1334 {
1335   guchar *s;
1336   glong n;
1337
1338   g_return_val_if_fail (string != NULL, NULL);
1339
1340   n = string->len;    
1341   s = (guchar *) string->str;
1342
1343   while (n)
1344     {
1345       if (isupper (*s))
1346         *s = tolower (*s);
1347       s++;
1348       n--;
1349     }
1350
1351   return string;
1352 }
1353
1354 /**
1355  * g_string_up:
1356  * @string: a #GString 
1357  * 
1358  * Converts a #GString to uppercase.
1359  * 
1360  * Return value: @string
1361  *
1362  * Deprecated:2.2: This function uses the locale-specific 
1363  *   toupper() function, which is almost never the right thing. 
1364  *   Use g_string_ascii_up() or g_utf8_strup() instead.
1365  **/
1366 GString*
1367 g_string_up (GString *string)
1368 {
1369   guchar *s;
1370   glong n;
1371
1372   g_return_val_if_fail (string != NULL, NULL);
1373
1374   n = string->len;
1375   s = (guchar *) string->str;
1376
1377   while (n)
1378     {
1379       if (islower (*s))
1380         *s = toupper (*s);
1381       s++;
1382       n--;
1383     }
1384
1385   return string;
1386 }
1387
1388 /**
1389  * g_string_append_vprintf:
1390  * @string: a #GString
1391  * @format: the string format. See the printf() documentation
1392  * @args: the list of arguments to insert in the output
1393  *
1394  * Appends a formatted string onto the end of a #GString.
1395  * This function is similar to g_string_append_printf()
1396  * except that the arguments to the format string are passed
1397  * as a va_list.
1398  *
1399  * Since: 2.14
1400  */
1401 void
1402 g_string_append_vprintf (GString     *string,
1403                          const gchar *format,
1404                          va_list      args)
1405 {
1406   gchar *buf;
1407   gint len;
1408   
1409   g_return_if_fail (string != NULL);
1410   g_return_if_fail (format != NULL);
1411
1412   len = g_vasprintf (&buf, format, args);
1413
1414   if (len >= 0)
1415     {
1416       g_string_maybe_expand (string, len);
1417       memcpy (string->str + string->len, buf, len + 1);
1418       string->len += len;
1419       g_free (buf);
1420     }
1421 }
1422
1423 /**
1424  * g_string_vprintf:
1425  * @string: a #GString
1426  * @format: the string format. See the printf() documentation
1427  * @args: the parameters to insert into the format string
1428  *
1429  * Writes a formatted string into a #GString. 
1430  * This function is similar to g_string_printf() except that 
1431  * the arguments to the format string are passed as a va_list.
1432  *
1433  * Since: 2.14
1434  */
1435 void
1436 g_string_vprintf (GString     *string,
1437                   const gchar *format,
1438                   va_list      args)
1439 {
1440   g_string_truncate (string, 0);
1441   g_string_append_vprintf (string, format, args);
1442 }
1443
1444 /**
1445  * g_string_sprintf:
1446  * @string: a #GString
1447  * @format: the string format. See the sprintf() documentation
1448  * @Varargs: the parameters to insert into the format string
1449  *
1450  * Writes a formatted string into a #GString.
1451  * This is similar to the standard sprintf() function,
1452  * except that the #GString buffer automatically expands 
1453  * to contain the results. The previous contents of the 
1454  * #GString are destroyed. 
1455  *
1456  * Deprecated: This function has been renamed to g_string_printf().
1457  */
1458
1459 /**
1460  * g_string_printf:
1461  * @string: a #GString
1462  * @format: the string format. See the printf() documentation
1463  * @Varargs: the parameters to insert into the format string
1464  *
1465  * Writes a formatted string into a #GString.
1466  * This is similar to the standard sprintf() function,
1467  * except that the #GString buffer automatically expands 
1468  * to contain the results. The previous contents of the 
1469  * #GString are destroyed.
1470  */
1471 void
1472 g_string_printf (GString     *string,
1473                  const gchar *format,
1474                  ...)
1475 {
1476   va_list args;
1477
1478   g_string_truncate (string, 0);
1479
1480   va_start (args, format);
1481   g_string_append_vprintf (string, format, args);
1482   va_end (args);
1483 }
1484
1485 /**
1486  * g_string_sprintfa:
1487  * @string: a #GString
1488  * @format: the string format. See the sprintf() documentation
1489  * @Varargs: the parameters to insert into the format string
1490  *
1491  * Appends a formatted string onto the end of a #GString.
1492  * This function is similar to g_string_sprintf() except that
1493  * the text is appended to the #GString. 
1494  *
1495  * Deprecated: This function has been renamed to g_string_append_printf()
1496  */
1497
1498 /**
1499  * g_string_append_printf:
1500  * @string: a #GString
1501  * @format: the string format. See the printf() documentation
1502  * @Varargs: the parameters to insert into the format string
1503  *
1504  * Appends a formatted string onto the end of a #GString.
1505  * This function is similar to g_string_printf() except 
1506  * that the text is appended to the #GString.
1507  */
1508 void
1509 g_string_append_printf (GString     *string,
1510                         const gchar *format,
1511                         ...)
1512 {
1513   va_list args;
1514
1515   va_start (args, format);
1516   g_string_append_vprintf (string, format, args);
1517   va_end (args);
1518 }
1519
1520 #define __G_STRING_C__
1521 #include "galiasdef.c"