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