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