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