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