Move documentation inline.
[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 struct _GStringChunk
48 {
49   GHashTable *const_table;
50   GSList     *storage_list;
51   gsize       storage_next;    
52   gsize       this_size;       
53   gsize       default_size;    
54 };
55
56 /* Hash Functions.
57  */
58
59 /**
60  * g_str_equal:
61  * @v1: a key. 
62  * @v2: a key to compare with @v1.
63  * 
64  * Compares two strings and returns %TRUE if they are equal.
65  * It can be passed to g_hash_table_new() as the @key_equal_func
66  * parameter, when using strings as keys in a #GHashTable.
67  *
68  * Returns: %TRUE if the two keys match.
69  */
70 gboolean
71 g_str_equal (gconstpointer v1,
72              gconstpointer v2)
73 {
74   const gchar *string1 = v1;
75   const gchar *string2 = v2;
76   
77   return strcmp (string1, string2) == 0;
78 }
79
80 /**
81  * g_str_hash:
82  * @v: a string key.
83  *
84  * Converts a string to a hash value.
85  * It can be passed to g_hash_table_new() as the @hash_func parameter, 
86  * when using strings as keys in a #GHashTable.
87  *
88  * Returns: a hash value corresponding to the key.
89  */
90 guint
91 g_str_hash (gconstpointer v)
92 {
93   /* 31 bit hash function */
94   const signed char *p = v;
95   guint32 h = *p;
96
97   if (h)
98     for (p += 1; *p != '\0'; p++)
99       h = (h << 5) - h + *p;
100
101   return h;
102 }
103
104 #define MY_MAXSIZE ((gsize)-1)
105
106 static inline gsize
107 nearest_power (gsize base, gsize num)    
108 {
109   if (num > MY_MAXSIZE / 2)
110     {
111       return MY_MAXSIZE;
112     }
113   else
114     {
115       gsize n = base;
116
117       while (n < num)
118         n <<= 1;
119       
120       return n;
121     }
122 }
123
124 /* String Chunks.
125  */
126
127 GStringChunk*
128 g_string_chunk_new (gsize default_size)    
129 {
130   GStringChunk *new_chunk = g_new (GStringChunk, 1);
131   gsize size = 1;    
132
133   size = nearest_power (1, default_size);
134
135   new_chunk->const_table       = NULL;
136   new_chunk->storage_list      = NULL;
137   new_chunk->storage_next      = size;
138   new_chunk->default_size      = size;
139   new_chunk->this_size         = size;
140
141   return new_chunk;
142 }
143
144 void
145 g_string_chunk_free (GStringChunk *chunk)
146 {
147   GSList *tmp_list;
148
149   g_return_if_fail (chunk != NULL);
150
151   if (chunk->storage_list)
152     {
153       for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
154         g_free (tmp_list->data);
155
156       g_slist_free (chunk->storage_list);
157     }
158
159   if (chunk->const_table)
160     g_hash_table_destroy (chunk->const_table);
161
162   g_free (chunk);
163 }
164
165 /**
166  * g_string_chunk_clear:
167  * @chunk: a #GStringChunk
168  *
169  * Frees all strings contained within the #GStringChunk.
170  * After calling g_string_chunk_clear() it is not safe to
171  * access any of the strings which were contained within it.
172  *
173  * Since: 2.14
174  */
175 void
176 g_string_chunk_clear (GStringChunk *chunk)
177 {
178   GSList *tmp_list;
179
180   g_return_if_fail (chunk != NULL);
181
182   if (chunk->storage_list)
183     {
184       for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
185         g_free (tmp_list->data);
186
187       g_slist_free (chunk->storage_list);
188
189       chunk->storage_list       = NULL;
190       chunk->storage_next       = chunk->default_size;
191       chunk->this_size          = chunk->default_size;
192     }
193
194   if (chunk->const_table)
195       g_hash_table_remove_all (chunk->const_table);
196 }
197
198 gchar*
199 g_string_chunk_insert (GStringChunk *chunk,
200                        const gchar  *string)
201 {
202   g_return_val_if_fail (chunk != NULL, NULL);
203
204   return g_string_chunk_insert_len (chunk, string, -1);
205 }
206
207 gchar*
208 g_string_chunk_insert_const (GStringChunk *chunk,
209                              const gchar  *string)
210 {
211   char* lookup;
212
213   g_return_val_if_fail (chunk != NULL, NULL);
214
215   if (!chunk->const_table)
216     chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
217
218   lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
219
220   if (!lookup)
221     {
222       lookup = g_string_chunk_insert (chunk, string);
223       g_hash_table_insert (chunk->const_table, lookup, lookup);
224     }
225
226   return lookup;
227 }
228
229 /**
230  * g_string_chunk_insert_len:
231  * @chunk: a #GStringChunk
232  * @string: bytes to insert
233  * @len: number of bytes of @string to insert, or -1 to insert a 
234  *     nul-terminated string. 
235  * 
236  * Adds a copy of the first @len bytes of @string to the #GStringChunk. The
237  * copy is nul-terminated.
238  * 
239  * The characters in the string can be changed, if necessary, though you
240  * should not change anything after the end of the string.
241  * 
242  * Return value: a pointer to the copy of @string within the #GStringChunk
243  * 
244  * Since: 2.4
245  **/
246 gchar*
247 g_string_chunk_insert_len (GStringChunk *chunk,
248                            const gchar  *string, 
249                            gssize        len)
250 {
251   gssize size;
252   gchar* pos;
253
254   g_return_val_if_fail (chunk != NULL, NULL);
255
256   if (len < 0)
257     size = strlen (string);
258   else
259     size = len;
260   
261   if ((chunk->storage_next + size + 1) > chunk->this_size)
262     {
263       gsize new_size = nearest_power (chunk->default_size, size + 1);
264
265       chunk->storage_list = g_slist_prepend (chunk->storage_list,
266                                              g_new (gchar, new_size));
267
268       chunk->this_size = new_size;
269       chunk->storage_next = 0;
270     }
271
272   pos = ((gchar *) chunk->storage_list->data) + chunk->storage_next;
273
274   *(pos + size) = '\0';
275
276   strncpy (pos, string, size);
277   if (len > 0)
278     size = strlen (pos);
279
280   chunk->storage_next += size + 1;
281
282   return pos;
283 }
284
285 /* Strings.
286  */
287 static void
288 g_string_maybe_expand (GString* string,
289                        gsize    len) 
290 {
291   if (string->len + len >= string->allocated_len)
292     {
293       string->allocated_len = nearest_power (1, string->len + len + 1);
294       string->str = g_realloc (string->str, string->allocated_len);
295     }
296 }
297
298 /**
299  * g_string_sized_new:
300  * @dfl_size: the default size of the space allocated to 
301  *            hold the string.
302  *
303  * Creates a new #GString, with enough space for @dfl_size 
304  * bytes. This is useful if you are going to add a lot of 
305  * text to the string and don't want it to be reallocated 
306  * too often.
307  *
308  * Returns: the new #GString.
309  */
310 GString*
311 g_string_sized_new (gsize dfl_size)    
312 {
313   GString *string = g_slice_new (GString);
314
315   string->allocated_len = 0;
316   string->len   = 0;
317   string->str   = NULL;
318
319   g_string_maybe_expand (string, MAX (dfl_size, 2));
320   string->str[0] = 0;
321
322   return string;
323 }
324
325 /**
326  * g_string_new:
327  * @init: the initial text to copy into the string.
328  * 
329  * Creates a new #GString, initialized with the given string.
330  *
331  * Returns: the new #GString.
332  */
333 GString*
334 g_string_new (const gchar *init)
335 {
336   GString *string;
337
338   if (init == NULL || *init == '\0')
339     string = g_string_sized_new (2);
340   else 
341     {
342       gint len;
343
344       len = strlen (init);
345       string = g_string_sized_new (len + 2);
346
347       g_string_append_len (string, init, len);
348     }
349
350   return string;
351 }
352
353 /**
354  * g_string_new_len:
355  * @init: initial contents of string.
356  * @len: length of @init to use.
357  *
358  * Creates a new #GString with @len bytes of the 
359  * @init buffer.  Because a length is provided, @init 
360  * need not be nul-terminated, and can contain embedded 
361  * nul bytes.
362  *
363  * Returns: a new #GString.
364  */
365 GString*
366 g_string_new_len (const gchar *init,
367                   gssize       len)    
368 {
369   GString *string;
370
371   if (len < 0)
372     return g_string_new (init);
373   else
374     {
375       string = g_string_sized_new (len);
376       
377       if (init)
378         g_string_append_len (string, init, len);
379       
380       return string;
381     }
382 }
383
384 /**
385  * g_string_free:
386  * @string: a #GString
387  * @free_segment: if %TRUE the actual character data is freed as well
388  *
389  * Frees the memory allocated for the #GString.
390  * If @free_segment is %TRUE it also frees the character data.
391  *
392  * Returns: the character data of @string 
393  *          (i.e. %NULL if @free_segment is %TRUE)
394  */
395 gchar*
396 g_string_free (GString *string,
397                gboolean free_segment)
398 {
399   gchar *segment;
400
401   g_return_val_if_fail (string != NULL, NULL);
402
403   if (free_segment)
404     {
405       g_free (string->str);
406       segment = NULL;
407     }
408   else
409     segment = string->str;
410
411   g_slice_free (GString, string);
412
413   return segment;
414 }
415
416 /**
417  * g_string_equal:
418  * @v: a #GString
419  * @v2: another #GString
420  *
421  * Compares two strings for equality, returning %TRUE if they are equal. 
422  * For use with #GHashTable.
423  *
424  * Returns: %TRUE if they strings are the same 
425  *          length and contain the same bytes.
426  */
427 gboolean
428 g_string_equal (const GString *v,
429                 const GString *v2)
430 {
431   gchar *p, *q;
432   GString *string1 = (GString *) v;
433   GString *string2 = (GString *) v2;
434   gsize i = string1->len;    
435
436   if (i != string2->len)
437     return FALSE;
438
439   p = string1->str;
440   q = string2->str;
441   while (i)
442     {
443       if (*p != *q)
444         return FALSE;
445       p++;
446       q++;
447       i--;
448     }
449   return TRUE;
450 }
451
452 /**
453  * g_string_hash:
454  * @str: a string to hash
455  *
456  * Creates a hash code for @str; for use with #GHashTable.
457  *
458  * Returns: hash code for @str
459  */
460 /* 31 bit hash function */
461 guint
462 g_string_hash (const GString *str)
463 {
464   const gchar *p = str->str;
465   gsize n = str->len;    
466   guint h = 0;
467
468   while (n--)
469     {
470       h = (h << 5) - h + *p;
471       p++;
472     }
473
474   return h;
475 }
476
477 /**
478  * g_string_assign:
479  * @string: the destination #GString. Its current contents 
480  *          are destroyed.
481  * @rval: the string to copy into @string
482  *
483  * Copies the bytes from a string into a #GString, 
484  * destroying any previous contents. It is rather like 
485  * the standard strcpy() function, except that you do not 
486  * have to worry about having enough space to copy the string.
487  *
488  * Returns: the destination #GString.
489  */
490 GString*
491 g_string_assign (GString     *string,
492                  const gchar *rval)
493 {
494   g_return_val_if_fail (string != NULL, NULL);
495   g_return_val_if_fail (rval != NULL, string);
496
497   /* Make sure assigning to itself doesn't corrupt the string.  */
498   if (string->str != rval)
499     {
500       /* Assigning from substring should be ok since g_string_truncate
501          does not realloc.  */
502       g_string_truncate (string, 0);
503       g_string_append (string, rval);
504     }
505
506   return string;
507 }
508
509 /**
510  * g_string_truncate:
511  * @string: a #GString
512  * @len: the new size of the #GString
513  *
514  * Cuts off the end of the GString, leaving the first @len bytes. 
515  *
516  * Returns: the #GString
517  */
518 GString*
519 g_string_truncate (GString *string,
520                    gsize    len)    
521 {
522   g_return_val_if_fail (string != NULL, NULL);
523
524   string->len = MIN (len, string->len);
525   string->str[string->len] = 0;
526
527   return string;
528 }
529
530 /**
531  * g_string_set_size:
532  * @string: a #GString
533  * @len: the new length
534  * 
535  * Sets the length of a #GString. If the length is less than
536  * the current length, the string will be truncated. If the
537  * length is greater than the current length, the contents
538  * of the newly added area are undefined. (However, as
539  * always, string->str[string->len] will be a nul byte.) 
540  * 
541  * Return value: @string
542  **/
543 GString*
544 g_string_set_size (GString *string,
545                    gsize    len)    
546 {
547   g_return_val_if_fail (string != NULL, NULL);
548
549   if (len >= string->allocated_len)
550     g_string_maybe_expand (string, len - string->len);
551   
552   string->len = len;
553   string->str[len] = 0;
554
555   return string;
556 }
557
558 /**
559  * g_string_insert_len:
560  * @string: a #GString
561  * @pos: position in @string where insertion should 
562  *       happen, or -1 for at the end
563  * @val: bytes to insert
564  * @len: number of bytes of @val to insert
565  * 
566  * Inserts @len bytes of @val into @string at @pos.  
567  * Because @len is provided, @val may contain embedded 
568  * nuls and need not be nul-terminated. If @pos is -1, 
569  * bytes are inserted at the end of the string.
570  *
571  * Returns: the #GString
572  */
573 GString*
574 g_string_insert_len (GString     *string,
575                      gssize       pos,    
576                      const gchar *val,
577                      gssize       len)    
578 {
579   g_return_val_if_fail (string != NULL, NULL);
580   g_return_val_if_fail (val != NULL, string);
581
582   if (len < 0)
583     len = strlen (val);
584
585   if (pos < 0)
586     pos = string->len;
587   else
588     g_return_val_if_fail (pos <= string->len, string);
589
590   /* Check whether val represents a substring of string.  This test
591      probably violates chapter and verse of the C standards, since
592      ">=" and "<=" are only valid when val really is a substring.
593      In practice, it will work on modern archs.  */
594   if (val >= string->str && val <= string->str + string->len)
595     {
596       gsize offset = val - string->str;
597       gsize precount = 0;
598
599       g_string_maybe_expand (string, len);
600       val = string->str + offset;
601       /* At this point, val is valid again.  */
602
603       /* Open up space where we are going to insert.  */
604       if (pos < string->len)
605         g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
606
607       /* Move the source part before the gap, if any.  */
608       if (offset < pos)
609         {
610           precount = MIN (len, pos - offset);
611           memcpy (string->str + pos, val, precount);
612         }
613
614       /* Move the source part after the gap, if any.  */
615       if (len > precount)
616         memcpy (string->str + pos + precount,
617                 val + /* Already moved: */ precount + /* Space opened up: */ len,
618                 len - precount);
619     }
620   else
621     {
622       g_string_maybe_expand (string, len);
623
624       /* If we aren't appending at the end, move a hunk
625        * of the old string to the end, opening up space
626        */
627       if (pos < string->len)
628         g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
629
630       /* insert the new string */
631       if (len == 1)
632         string->str[pos] = *val;
633       else
634         memcpy (string->str + pos, val, len);
635     }
636
637   string->len += len;
638
639   string->str[string->len] = 0;
640
641   return string;
642 }
643
644 /**
645  * g_string_append:
646  * @string: a #GString.
647  * @val: the string to append onto the end of the #GString.
648  * 
649  * Adds a string onto the end of a #GString, expanding it if necessary.
650  *
651  * Returns: the #GString.
652  */
653 GString*
654 g_string_append (GString     *string,
655                  const gchar *val)
656 {  
657   g_return_val_if_fail (string != NULL, NULL);
658   g_return_val_if_fail (val != NULL, string);
659
660   return g_string_insert_len (string, -1, val, -1);
661 }
662
663 /**
664  * g_string_append_len:
665  * @string: a #GString
666  * @val: bytes to append
667  * @len: number of bytes of @val to use.
668  * 
669  * Appends @len bytes of @val to @string. 
670  * Because @len is provided, @val may contain 
671  * embedded nuls and need not be nul-terminated.
672  * 
673  * Returns: the #GString
674  */
675 GString*
676 g_string_append_len (GString     *string,
677                      const gchar *val,
678                      gssize       len)    
679 {
680   g_return_val_if_fail (string != NULL, NULL);
681   g_return_val_if_fail (val != NULL, string);
682
683   return g_string_insert_len (string, -1, val, len);
684 }
685
686 /**
687  * g_string_append_c:
688  * @string: a #GString.
689  * @c: the byte to append onto the end of the #GString.
690  *
691  * Adds a byte onto the end of a #GString, expanding it if necessary.
692  * 
693  * Returns: the #GString.
694  */
695 #undef g_string_append_c
696 GString*
697 g_string_append_c (GString *string,
698                    gchar    c)
699 {
700   g_return_val_if_fail (string != NULL, NULL);
701
702   return g_string_insert_c (string, -1, c);
703 }
704
705 /**
706  * g_string_append_unichar:
707  * @string: a #GString
708  * @wc: a Unicode character
709  * 
710  * Converts a Unicode character into UTF-8, and appends it
711  * to the string.
712  * 
713  * Return value: @string
714  **/
715 GString*
716 g_string_append_unichar (GString  *string,
717                          gunichar  wc)
718 {  
719   g_return_val_if_fail (string != NULL, NULL);
720   
721   return g_string_insert_unichar (string, -1, wc);
722 }
723
724 /**
725  * g_string_prepend:
726  * @string: a #GString
727  * @val: the string to prepend on the start of the #GString
728  *
729  * Adds a string on to the start of a #GString, 
730  * expanding it if necessary.
731  *
732  * Returns: the #GString
733  */
734 GString*
735 g_string_prepend (GString     *string,
736                   const gchar *val)
737 {
738   g_return_val_if_fail (string != NULL, NULL);
739   g_return_val_if_fail (val != NULL, string);
740   
741   return g_string_insert_len (string, 0, val, -1);
742 }
743
744 /**
745  * g_string_prepend_len:
746  * @string: a #GString
747  * @val: bytes to prepend
748  * @len: number of bytes in @val to prepend
749  *
750  * Prepends @len bytes of @val to @string. 
751  * Because @len is provided, @val may contain 
752  * embedded nuls and need not be nul-terminated.
753  *
754  * Returns: the #GString passed in
755  */
756 GString*
757 g_string_prepend_len (GString     *string,
758                       const gchar *val,
759                       gssize       len)    
760 {
761   g_return_val_if_fail (string != NULL, NULL);
762   g_return_val_if_fail (val != NULL, string);
763
764   return g_string_insert_len (string, 0, val, len);
765 }
766
767 /**
768  * g_string_prepend_c:
769  * @string: a #GString
770  * @c: the byte to prepend on the start of the #GString
771  *
772  * Adds a byte onto the start of a #GString, 
773  * expanding it if necessary.
774  *
775  * Returns: the #GString
776  */
777 GString*
778 g_string_prepend_c (GString *string,
779                     gchar    c)
780 {  
781   g_return_val_if_fail (string != NULL, NULL);
782   
783   return g_string_insert_c (string, 0, c);
784 }
785
786 /**
787  * g_string_prepend_unichar:
788  * @string: a #GString.
789  * @wc: a Unicode character.
790  * 
791  * Converts a Unicode character into UTF-8, and prepends it
792  * to the string.
793  * 
794  * Return value: @string.
795  **/
796 GString*
797 g_string_prepend_unichar (GString  *string,
798                           gunichar  wc)
799 {  
800   g_return_val_if_fail (string != NULL, NULL);
801   
802   return g_string_insert_unichar (string, 0, wc);
803 }
804
805 /**
806  * g_string_insert:
807  * @string: a #GString
808  * @pos: the position to insert the copy of the string
809  * @val: the string to insert
810  *
811  * Inserts a copy of a string into a #GString, 
812  * expanding it if necessary.
813  *
814  * Returns: the #GString
815  */
816 GString*
817 g_string_insert (GString     *string,
818                  gssize       pos,    
819                  const gchar *val)
820 {
821   g_return_val_if_fail (string != NULL, NULL);
822   g_return_val_if_fail (val != NULL, string);
823   if (pos >= 0)
824     g_return_val_if_fail (pos <= string->len, string);
825   
826   return g_string_insert_len (string, pos, val, -1);
827 }
828
829 /**
830  * g_string_insert_c:
831  * @string: a #GString
832  * @pos: the position to insert the byte
833  * @c: the byte to insert
834  *
835  * Inserts a byte into a #GString, expanding it if necessary.
836  * 
837  * Returns: the #GString
838  */
839 GString*
840 g_string_insert_c (GString *string,
841                    gssize   pos,    
842                    gchar    c)
843 {
844   g_return_val_if_fail (string != NULL, NULL);
845
846   g_string_maybe_expand (string, 1);
847
848   if (pos < 0)
849     pos = string->len;
850   else
851     g_return_val_if_fail (pos <= string->len, string);
852   
853   /* If not just an append, move the old stuff */
854   if (pos < string->len)
855     g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
856
857   string->str[pos] = c;
858
859   string->len += 1;
860
861   string->str[string->len] = 0;
862
863   return string;
864 }
865
866 /**
867  * g_string_insert_unichar:
868  * @string: a #GString
869  * @pos: the position at which to insert character, or -1 to
870  *       append at the end of the string.
871  * @wc: a Unicode character
872  * 
873  * Converts a Unicode character into UTF-8, and insert it
874  * into the string at the given position.
875  * 
876  * Return value: @string
877  **/
878 GString*
879 g_string_insert_unichar (GString *string,
880                          gssize   pos,    
881                          gunichar wc)
882 {
883   gint charlen, first, i;
884   gchar *dest;
885
886   g_return_val_if_fail (string != NULL, NULL);
887
888   /* Code copied from g_unichar_to_utf() */
889   if (wc < 0x80)
890     {
891       first = 0;
892       charlen = 1;
893     }
894   else if (wc < 0x800)
895     {
896       first = 0xc0;
897       charlen = 2;
898     }
899   else if (wc < 0x10000)
900     {
901       first = 0xe0;
902       charlen = 3;
903     }
904    else if (wc < 0x200000)
905     {
906       first = 0xf0;
907       charlen = 4;
908     }
909   else if (wc < 0x4000000)
910     {
911       first = 0xf8;
912       charlen = 5;
913     }
914   else
915     {
916       first = 0xfc;
917       charlen = 6;
918     }
919   /* End of copied code */
920
921   g_string_maybe_expand (string, charlen);
922
923   if (pos < 0)
924     pos = string->len;
925   else
926     g_return_val_if_fail (pos <= string->len, string);
927
928   /* If not just an append, move the old stuff */
929   if (pos < string->len)
930     g_memmove (string->str + pos + charlen, string->str + pos, string->len - pos);
931
932   dest = string->str + pos;
933   /* Code copied from g_unichar_to_utf() */
934   for (i = charlen - 1; i > 0; --i)
935     {
936       dest[i] = (wc & 0x3f) | 0x80;
937       wc >>= 6;
938     }
939   dest[0] = wc | first;
940   /* End of copied code */
941   
942   string->len += charlen;
943
944   string->str[string->len] = 0;
945
946   return string;
947 }
948
949 /**
950  * g_string_erase:
951  * @string: a #GString
952  * @pos: the position of the content to remove
953  * @len: the number of bytes to remove, or -1 to remove all
954  *       following bytes
955  *
956  * Removes @len bytes from a #GString, starting at position @pos.
957  * The rest of the #GString is shifted down to fill the gap.
958  *
959  * Returns: the #GString
960  */
961 GString*
962 g_string_erase (GString *string,
963                 gssize   pos,
964                 gssize   len)
965 {
966   g_return_val_if_fail (string != NULL, NULL);
967   g_return_val_if_fail (pos >= 0, string);
968   g_return_val_if_fail (pos <= string->len, string);
969
970   if (len < 0)
971     len = string->len - pos;
972   else
973     {
974       g_return_val_if_fail (pos + len <= string->len, string);
975
976       if (pos + len < string->len)
977         g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
978     }
979
980   string->len -= len;
981   
982   string->str[string->len] = 0;
983
984   return string;
985 }
986
987 /**
988  * g_string_ascii_down:
989  * @string: a GString
990  * 
991  * Converts all upper case ASCII letters to lower case ASCII letters.
992  * 
993  * Return value: passed-in @string pointer, with all the upper case
994  *               characters converted to lower case in place, with
995  *               semantics that exactly match g_ascii_tolower.
996  **/
997 GString*
998 g_string_ascii_down (GString *string)
999 {
1000   gchar *s;
1001   gint n;
1002
1003   g_return_val_if_fail (string != NULL, NULL);
1004
1005   n = string->len;
1006   s = string->str;
1007
1008   while (n)
1009     {
1010       *s = g_ascii_tolower (*s);
1011       s++;
1012       n--;
1013     }
1014
1015   return string;
1016 }
1017
1018 /**
1019  * g_string_ascii_up:
1020  * @string: a GString
1021  * 
1022  * Converts all lower case ASCII letters to upper case ASCII letters.
1023  * 
1024  * Return value: passed-in @string pointer, with all the lower case
1025  *               characters converted to upper case in place, with
1026  *               semantics that exactly match g_ascii_toupper.
1027  **/
1028 GString*
1029 g_string_ascii_up (GString *string)
1030 {
1031   gchar *s;
1032   gint n;
1033
1034   g_return_val_if_fail (string != NULL, NULL);
1035
1036   n = string->len;
1037   s = string->str;
1038
1039   while (n)
1040     {
1041       *s = g_ascii_toupper (*s);
1042       s++;
1043       n--;
1044     }
1045
1046   return string;
1047 }
1048
1049 /**
1050  * g_string_down:
1051  * @string: a #GString
1052  *  
1053  * Converts a #GString to lowercase.
1054  *
1055  * Returns: the #GString.
1056  *
1057  * Deprecated:2.2: This function uses the locale-specific tolower() function, 
1058  * which is almost never the right thing. Use g_string_ascii_down() or 
1059  * g_utf8_strdown() instead.
1060  */
1061 GString*
1062 g_string_down (GString *string)
1063 {
1064   guchar *s;
1065   glong n;
1066
1067   g_return_val_if_fail (string != NULL, NULL);
1068
1069   n = string->len;    
1070   s = (guchar *) string->str;
1071
1072   while (n)
1073     {
1074       if (isupper (*s))
1075         *s = tolower (*s);
1076       s++;
1077       n--;
1078     }
1079
1080   return string;
1081 }
1082
1083 /**
1084  * g_string_up:
1085  * @string: a #GString 
1086  * 
1087  * Converts a #GString to uppercase.
1088  * 
1089  * Return value: the #GString
1090  *
1091  * Deprecated:2.2: This function uses the locale-specific toupper() function, 
1092  * which is almost never the right thing. Use g_string_ascii_up() or 
1093  * g_utf8_strup() instead.
1094  **/
1095 GString*
1096 g_string_up (GString *string)
1097 {
1098   guchar *s;
1099   glong n;
1100
1101   g_return_val_if_fail (string != NULL, NULL);
1102
1103   n = string->len;
1104   s = (guchar *) string->str;
1105
1106   while (n)
1107     {
1108       if (islower (*s))
1109         *s = toupper (*s);
1110       s++;
1111       n--;
1112     }
1113
1114   return string;
1115 }
1116
1117 static void
1118 g_string_append_printf_internal (GString     *string,
1119                                  const gchar *fmt,
1120                                  va_list      args)
1121 {
1122   gchar *buffer;
1123   gint length;
1124   
1125   length = g_vasprintf (&buffer, fmt, args);
1126   g_string_append_len (string, buffer, length);
1127   g_free (buffer);
1128 }
1129
1130 /**
1131  * g_string_sprintf:
1132  * @string: a #GString.
1133  * @format: the string format. See the sprintf() documentation.
1134  * @Varargs: the parameters to insert into the format string.
1135  *
1136  * Writes a formatted string into a #GString.
1137  * This is similar to the standard sprintf() function,
1138  * except that the #GString buffer automatically expands 
1139  * to contain the results. The previous contents of the 
1140  * #GString are destroyed. 
1141  *
1142  * Deprecated: This function has been renamed to g_string_printf().
1143  */
1144
1145 /**
1146  * g_string_printf:
1147  * @string: a #GString.
1148  * @format: the string format. See the printf() documentation.
1149  * @Varargs: the parameters to insert into the format string.
1150  *
1151  * Writes a formatted string into a #GString.
1152  * This is similar to the standard sprintf() function,
1153  * except that the #GString buffer automatically expands 
1154  * to contain the results. The previous contents of the 
1155  * #GString are destroyed.
1156  */
1157 void
1158 g_string_printf (GString *string,
1159                  const gchar *fmt,
1160                  ...)
1161 {
1162   va_list args;
1163
1164   g_string_truncate (string, 0);
1165
1166   va_start (args, fmt);
1167   g_string_append_printf_internal (string, fmt, args);
1168   va_end (args);
1169 }
1170
1171 /**
1172  * g_string_sprintfa:
1173  * @string: a #GString.
1174  * @format: the string format. See the sprintf() documentation.
1175  * @Varargs: the parameters to insert into the format string.
1176  *
1177  * Appends a formatted string onto the end of a #GString.
1178  * This function is is similar to g_string_sprintf() except that
1179  * the text is appended to the #GString. 
1180  *
1181  * Deprecated: This function has been renamed to 
1182  * g_string_append_printf().
1183  */
1184
1185 /**
1186  * g_string_append_printf:
1187  * @string: a #GString.
1188  * @format: the string format. See the printf() documentation.
1189  * @Varargs: the parameters to insert into the format string.
1190  *
1191  * Appends a formatted string onto the end of a #GString.
1192  * This function is is similar to g_string_printf() except 
1193  * that the text is appended to the #GString.
1194  */
1195 void
1196 g_string_append_printf (GString *string,
1197                         const gchar *fmt,
1198                         ...)
1199 {
1200   va_list args;
1201
1202   va_start (args, fmt);
1203   g_string_append_printf_internal (string, fmt, args);
1204   va_end (args);
1205 }
1206
1207 #define __G_STRING_C__
1208 #include "galiasdef.c"