gstring.c 28 KB
Newer Older
Owen Taylor's avatar
Owen Taylor committed
1 2 3 4
/* GLIB - Library of useful routines for C programming
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Owen Taylor's avatar
Owen Taylor committed
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
Owen Taylor's avatar
Owen Taylor committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Owen Taylor's avatar
Owen Taylor committed
15 16 17 18
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
19

20
/*
21
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * file for a list of people on the GLib Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
 */

27 28 29 30
/* 
 * MT safe
 */

31
#include "config.h"
32 33 34 35

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
Owen Taylor's avatar
Owen Taylor committed
36 37 38 39 40
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
41

42
#include "glib.h"
43
#include "gprintf.h"
Owen Taylor's avatar
Owen Taylor committed
44

45
#include "galias.h"
46

47
struct _GStringChunk
Owen Taylor's avatar
Owen Taylor committed
48 49 50
{
  GHashTable *const_table;
  GSList     *storage_list;
51 52 53
  gsize       storage_next;    
  gsize       this_size;       
  gsize       default_size;    
Owen Taylor's avatar
Owen Taylor committed
54 55 56 57 58
};

/* Hash Functions.
 */

Matthias Clasen's avatar
Matthias Clasen committed
59 60 61 62 63 64 65 66 67 68 69
/**
 * g_str_equal:
 * @v1: a key. 
 * @v2: a key to compare with @v1.
 * 
 * Compares two strings and returns %TRUE if they are equal.
 * It can be passed to g_hash_table_new() as the @key_equal_func
 * parameter, when using strings as keys in a #GHashTable.
 *
 * Returns: %TRUE if the two keys match.
 */
Owen Taylor's avatar
updated  
Owen Taylor committed
70
gboolean
71 72
g_str_equal (gconstpointer v1,
	     gconstpointer v2)
Owen Taylor's avatar
Owen Taylor committed
73
{
74 75 76 77
  const gchar *string1 = v1;
  const gchar *string2 = v2;
  
  return strcmp (string1, string2) == 0;
Owen Taylor's avatar
Owen Taylor committed
78 79
}

Matthias Clasen's avatar
Matthias Clasen committed
80 81 82 83 84 85 86 87 88 89
/**
 * g_str_hash:
 * @v: a string key.
 *
 * Converts a string to a hash value.
 * It can be passed to g_hash_table_new() as the @hash_func parameter, 
 * when using strings as keys in a #GHashTable.
 *
 * Returns: a hash value corresponding to the key.
 */
Owen Taylor's avatar
Owen Taylor committed
90
guint
Matthias Clasen's avatar
Matthias Clasen committed
91
g_str_hash (gconstpointer v)
Owen Taylor's avatar
Owen Taylor committed
92
{
Matthias Clasen's avatar
Matthias Clasen committed
93
  /* 31 bit hash function */
94 95
  const signed char *p = v;
  guint32 h = *p;
96 97 98 99

  if (h)
    for (p += 1; *p != '\0'; p++)
      h = (h << 5) - h + *p;
Owen Taylor's avatar
Owen Taylor committed
100

101
  return h;
Owen Taylor's avatar
Owen Taylor committed
102 103
}

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
#define MY_MAXSIZE ((gsize)-1)

static inline gsize
nearest_power (gsize base, gsize num)    
{
  if (num > MY_MAXSIZE / 2)
    {
      return MY_MAXSIZE;
    }
  else
    {
      gsize n = base;

      while (n < num)
	n <<= 1;
      
      return n;
    }
}
Owen Taylor's avatar
Owen Taylor committed
123 124 125 126

/* String Chunks.
 */

127 128 129 130 131 132 133 134 135 136 137
/**
 * g_string_chunk_new:
 * @size: the default size of the blocks of memory which are 
 *        allocated to store the strings. If a particular string 
 *        is larger than this default size, a larger block of 
 *        memory will be allocated for it.
 * 
 * Creates a new #GStringChunk. 
 * 
 * Returns: a new #GStringChunk
 */
Owen Taylor's avatar
Owen Taylor committed
138
GStringChunk*
139
g_string_chunk_new (gsize size)    
Owen Taylor's avatar
Owen Taylor committed
140
{
141
  GStringChunk *new_chunk = g_new (GStringChunk, 1);
Matthias Clasen's avatar
Matthias Clasen committed
142
  gsize actual_size = 1;    
Owen Taylor's avatar
Owen Taylor committed
143

Matthias Clasen's avatar
Matthias Clasen committed
144
  actual_size = nearest_power (1, size);
Owen Taylor's avatar
Owen Taylor committed
145 146 147

  new_chunk->const_table       = NULL;
  new_chunk->storage_list      = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
148 149 150
  new_chunk->storage_next      = actual_size;
  new_chunk->default_size      = actual_size;
  new_chunk->this_size         = actual_size;
Owen Taylor's avatar
Owen Taylor committed
151

152
  return new_chunk;
Owen Taylor's avatar
Owen Taylor committed
153 154
}

155 156 157 158 159 160 161 162
/**
 * g_string_chunk_free:
 * @chunk: a #GStringChunk 
 * 
 * Frees all memory allocated by the #GStringChunk.
 * After calling g_string_chunk_free() it is not safe to
 * access any of the strings which were contained within it.
 */
Owen Taylor's avatar
Owen Taylor committed
163
void
164
g_string_chunk_free (GStringChunk *chunk)
Owen Taylor's avatar
Owen Taylor committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
{
  GSList *tmp_list;

  g_return_if_fail (chunk != NULL);

  if (chunk->storage_list)
    {
      for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
	g_free (tmp_list->data);

      g_slist_free (chunk->storage_list);
    }

  if (chunk->const_table)
    g_hash_table_destroy (chunk->const_table);

  g_free (chunk);
}

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
/**
 * g_string_chunk_clear:
 * @chunk: a #GStringChunk
 *
 * Frees all strings contained within the #GStringChunk.
 * After calling g_string_chunk_clear() it is not safe to
 * access any of the strings which were contained within it.
 *
 * Since: 2.14
 */
void
g_string_chunk_clear (GStringChunk *chunk)
{
  GSList *tmp_list;

  g_return_if_fail (chunk != NULL);

  if (chunk->storage_list)
    {
      for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
        g_free (tmp_list->data);

      g_slist_free (chunk->storage_list);

      chunk->storage_list       = NULL;
      chunk->storage_next       = chunk->default_size;
      chunk->this_size          = chunk->default_size;
    }

  if (chunk->const_table)
      g_hash_table_remove_all (chunk->const_table);
}

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
/**
 * g_string_chunk_insert:
 * @chunk: a #GStringChunk
 * @string: the string to add
 * 
 * Adds a copy of @string to the #GStringChunk.
 * It returns a pointer to the new copy of the string 
 * in the #GStringChunk. The characters in the string 
 * can be changed, if necessary, though you should not 
 * change anything after the end of the string.
 *
 * Unlike g_string_chunk_insert_const(), this function 
 * does not check for duplicates. Also strings added 
 * with g_string_chunk_insert() will not be searched 
 * by g_string_chunk_insert_const() when looking for 
 * duplicates.
 * 
 * Returns: a pointer to the copy of @string within 
 *          the #GStringChunk.
 */
Owen Taylor's avatar
Owen Taylor committed
237
gchar*
238
g_string_chunk_insert (GStringChunk *chunk,
Owen Taylor's avatar
Owen Taylor committed
239 240 241 242
		       const gchar  *string)
{
  g_return_val_if_fail (chunk != NULL, NULL);

243
  return g_string_chunk_insert_len (chunk, string, -1);
Owen Taylor's avatar
Owen Taylor committed
244 245
}

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
/**
 * g_string_chunk_insert_const:
 * @chunk:  a #GStringChunk
 * @string: the string to add
 * 
 * Adds a copy of @string to the #GStringChunk, unless 
 * the same string has already been added to the #GStringChunk 
 * with g_string_chunk_insert_const().
 * 
 * This function is useful if you need to copy a large number 
 * of strings but do not want to waste space storing duplicates. 
 * But you must remember that there may be several pointers to 
 * the same string, and so any changes made to the strings 
 * should be done very carefully.
 * 
 * Note that g_string_chunk_insert_const() will not return a 
 * pointer to a string added with g_string_chunk_insert(), even 
 * if they do match.
 * 
 * Returns: a pointer to the new or existing copy of @string 
 *          within the #GStringChunk
 */
Owen Taylor's avatar
Owen Taylor committed
268
gchar*
269
g_string_chunk_insert_const (GStringChunk *chunk,
Owen Taylor's avatar
Owen Taylor committed
270 271 272 273 274 275 276 277 278 279 280 281 282
			     const gchar  *string)
{
  char* lookup;

  g_return_val_if_fail (chunk != NULL, NULL);

  if (!chunk->const_table)
    chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);

  lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);

  if (!lookup)
    {
283
      lookup = g_string_chunk_insert (chunk, string);
Owen Taylor's avatar
Owen Taylor committed
284 285 286 287 288 289
      g_hash_table_insert (chunk->const_table, lookup, lookup);
    }

  return lookup;
}

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
/**
 * g_string_chunk_insert_len:
 * @chunk: a #GStringChunk
 * @string: bytes to insert
 * @len: number of bytes of @string to insert, or -1 to insert a 
 *     nul-terminated string. 
 * 
 * Adds a copy of the first @len bytes of @string to the #GStringChunk. The
 * copy is nul-terminated.
 * 
 * The characters in the string can be changed, if necessary, though you
 * should not change anything after the end of the string.
 * 
 * Return value: a pointer to the copy of @string within the #GStringChunk
 * 
 * Since: 2.4
 **/
gchar*
g_string_chunk_insert_len (GStringChunk *chunk,
			   const gchar  *string, 
			   gssize        len)
{
312
  gssize size;
313 314 315 316 317
  gchar* pos;

  g_return_val_if_fail (chunk != NULL, NULL);

  if (len < 0)
318 319 320
    size = strlen (string);
  else
    size = len;
321
  
322
  if ((chunk->storage_next + size + 1) > chunk->this_size)
323
    {
324
      gsize new_size = nearest_power (chunk->default_size, size + 1);
325 326 327 328 329 330 331 332 333 334

      chunk->storage_list = g_slist_prepend (chunk->storage_list,
					     g_new (gchar, new_size));

      chunk->this_size = new_size;
      chunk->storage_next = 0;
    }

  pos = ((gchar *) chunk->storage_list->data) + chunk->storage_next;

335
  *(pos + size) = '\0';
336

337 338 339
  strncpy (pos, string, size);
  if (len > 0)
    size = strlen (pos);
340

341
  chunk->storage_next += size + 1;
342 343 344 345

  return pos;
}

Owen Taylor's avatar
Owen Taylor committed
346 347 348
/* Strings.
 */
static void
349 350
g_string_maybe_expand (GString* string,
		       gsize    len) 
Owen Taylor's avatar
Owen Taylor committed
351
{
352
  if (string->len + len >= string->allocated_len)
Owen Taylor's avatar
Owen Taylor committed
353
    {
354 355
      string->allocated_len = nearest_power (1, string->len + len + 1);
      string->str = g_realloc (string->str, string->allocated_len);
Owen Taylor's avatar
Owen Taylor committed
356 357 358
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
359 360 361 362 363 364 365 366 367 368 369 370
/**
 * g_string_sized_new:
 * @dfl_size: the default size of the space allocated to 
 *            hold the string.
 *
 * Creates a new #GString, with enough space for @dfl_size 
 * bytes. This is useful if you are going to add a lot of 
 * text to the string and don't want it to be reallocated 
 * too often.
 *
 * Returns: the new #GString.
 */
Owen Taylor's avatar
Owen Taylor committed
371
GString*
372
g_string_sized_new (gsize dfl_size)    
Owen Taylor's avatar
Owen Taylor committed
373
{
374
  GString *string = g_slice_new (GString);
Owen Taylor's avatar
Owen Taylor committed
375

376
  string->allocated_len = 0;
Owen Taylor's avatar
Owen Taylor committed
377 378 379 380 381 382
  string->len   = 0;
  string->str   = NULL;

  g_string_maybe_expand (string, MAX (dfl_size, 2));
  string->str[0] = 0;

383
  return string;
Owen Taylor's avatar
Owen Taylor committed
384 385
}

Matthias Clasen's avatar
Matthias Clasen committed
386 387 388 389 390 391 392 393
/**
 * g_string_new:
 * @init: the initial text to copy into the string.
 * 
 * Creates a new #GString, initialized with the given string.
 *
 * Returns: the new #GString.
 */
Owen Taylor's avatar
Owen Taylor committed
394 395 396 397 398
GString*
g_string_new (const gchar *init)
{
  GString *string;

399 400 401 402 403 404 405 406
  if (init == NULL || *init == '\0')
    string = g_string_sized_new (2);
  else 
    {
      gint len;

      len = strlen (init);
      string = g_string_sized_new (len + 2);
Owen Taylor's avatar
Owen Taylor committed
407

408 409
      g_string_append_len (string, init, len);
    }
Owen Taylor's avatar
Owen Taylor committed
410 411 412 413

  return string;
}

Matthias Clasen's avatar
Matthias Clasen committed
414 415 416 417 418 419 420 421 422 423 424 425
/**
 * g_string_new_len:
 * @init: initial contents of string.
 * @len: length of @init to use.
 *
 * Creates a new #GString with @len bytes of the 
 * @init buffer.  Because a length is provided, @init 
 * need not be nul-terminated, and can contain embedded 
 * nul bytes.
 *
 * Returns: a new #GString.
 */
426 427
GString*
g_string_new_len (const gchar *init,
428
                  gssize       len)    
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
{
  GString *string;

  if (len < 0)
    return g_string_new (init);
  else
    {
      string = g_string_sized_new (len);
      
      if (init)
        g_string_append_len (string, init, len);
      
      return string;
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
445 446 447 448 449 450 451 452 453 454 455
/**
 * g_string_free:
 * @string: a #GString
 * @free_segment: if %TRUE the actual character data is freed as well
 *
 * Frees the memory allocated for the #GString.
 * If @free_segment is %TRUE it also frees the character data.
 *
 * Returns: the character data of @string 
 *          (i.e. %NULL if @free_segment is %TRUE)
 */
456
gchar*
Owen Taylor's avatar
Owen Taylor committed
457
g_string_free (GString *string,
458
	       gboolean free_segment)
Owen Taylor's avatar
Owen Taylor committed
459
{
460 461
  gchar *segment;

462
  g_return_val_if_fail (string != NULL, NULL);
Owen Taylor's avatar
Owen Taylor committed
463 464

  if (free_segment)
465 466 467 468 469 470
    {
      g_free (string->str);
      segment = NULL;
    }
  else
    segment = string->str;
Owen Taylor's avatar
Owen Taylor committed
471

472
  g_slice_free (GString, string);
473 474

  return segment;
Owen Taylor's avatar
Owen Taylor committed
475 476
}

Matthias Clasen's avatar
Matthias Clasen committed
477 478 479 480 481 482 483 484 485 486 487
/**
 * g_string_equal:
 * @v: a #GString
 * @v2: another #GString
 *
 * Compares two strings for equality, returning %TRUE if they are equal. 
 * For use with #GHashTable.
 *
 * Returns: %TRUE if they strings are the same 
 *          length and contain the same bytes.
 */
Owen Taylor's avatar
updated  
Owen Taylor committed
488 489 490 491 492
gboolean
g_string_equal (const GString *v,
                const GString *v2)
{
  gchar *p, *q;
493 494
  GString *string1 = (GString *) v;
  GString *string2 = (GString *) v2;
495
  gsize i = string1->len;    
Owen Taylor's avatar
updated  
Owen Taylor committed
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

  if (i != string2->len)
    return FALSE;

  p = string1->str;
  q = string2->str;
  while (i)
    {
      if (*p != *q)
	return FALSE;
      p++;
      q++;
      i--;
    }
  return TRUE;
}

Matthias Clasen's avatar
Matthias Clasen committed
513 514 515 516 517 518 519 520
/**
 * g_string_hash:
 * @str: a string to hash
 *
 * Creates a hash code for @str; for use with #GHashTable.
 *
 * Returns: hash code for @str
 */
Owen Taylor's avatar
updated  
Owen Taylor committed
521 522 523 524 525
/* 31 bit hash function */
guint
g_string_hash (const GString *str)
{
  const gchar *p = str->str;
526
  gsize n = str->len;    
Owen Taylor's avatar
updated  
Owen Taylor committed
527 528 529 530 531 532 533 534 535 536 537
  guint h = 0;

  while (n--)
    {
      h = (h << 5) - h + *p;
      p++;
    }

  return h;
}

Matthias Clasen's avatar
Matthias Clasen committed
538 539 540 541 542 543 544 545 546 547 548 549 550
/**
 * g_string_assign:
 * @string: the destination #GString. Its current contents 
 *          are destroyed.
 * @rval: the string to copy into @string
 *
 * Copies the bytes from a string into a #GString, 
 * destroying any previous contents. It is rather like 
 * the standard strcpy() function, except that you do not 
 * have to worry about having enough space to copy the string.
 *
 * Returns: the destination #GString.
 */
Owen Taylor's avatar
Owen Taylor committed
551
GString*
552
g_string_assign (GString     *string,
Owen Taylor's avatar
Owen Taylor committed
553 554
		 const gchar *rval)
{
555 556
  g_return_val_if_fail (string != NULL, NULL);
  g_return_val_if_fail (rval != NULL, string);
557 558 559 560 561 562 563 564 565

  /* Make sure assigning to itself doesn't corrupt the string.  */
  if (string->str != rval)
    {
      /* Assigning from substring should be ok since g_string_truncate
	 does not realloc.  */
      g_string_truncate (string, 0);
      g_string_append (string, rval);
    }
Owen Taylor's avatar
Owen Taylor committed
566

567
  return string;
Owen Taylor's avatar
Owen Taylor committed
568 569
}

Matthias Clasen's avatar
Matthias Clasen committed
570 571 572 573 574 575 576 577 578
/**
 * g_string_truncate:
 * @string: a #GString
 * @len: the new size of the #GString
 *
 * Cuts off the end of the GString, leaving the first @len bytes. 
 *
 * Returns: the #GString
 */
Owen Taylor's avatar
Owen Taylor committed
579
GString*
580
g_string_truncate (GString *string,
581
		   gsize    len)    
Owen Taylor's avatar
Owen Taylor committed
582 583 584
{
  g_return_val_if_fail (string != NULL, NULL);

585 586
  string->len = MIN (len, string->len);
  string->str[string->len] = 0;
Owen Taylor's avatar
Owen Taylor committed
587

588
  return string;
Owen Taylor's avatar
Owen Taylor committed
589 590
}

591 592
/**
 * g_string_set_size:
Owen Taylor's avatar
Owen Taylor committed
593
 * @string: a #GString
594 595 596 597 598 599 600 601
 * @len: the new length
 * 
 * Sets the length of a #GString. If the length is less than
 * the current length, the string will be truncated. If the
 * length is greater than the current length, the contents
 * of the newly added area are undefined. (However, as
 * always, string->str[string->len] will be a nul byte.) 
 * 
602
 * Return value: @string
603 604
 **/
GString*
Owen Taylor's avatar
Owen Taylor committed
605
g_string_set_size (GString *string,
606 607 608 609
		   gsize    len)    
{
  g_return_val_if_fail (string != NULL, NULL);

610 611
  if (len >= string->allocated_len)
    g_string_maybe_expand (string, len - string->len);
612
  
613 614
  string->len = len;
  string->str[len] = 0;
615

Owen Taylor's avatar
Owen Taylor committed
616
  return string;
617 618
}

Matthias Clasen's avatar
Matthias Clasen committed
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
/**
 * g_string_insert_len:
 * @string: a #GString
 * @pos: position in @string where insertion should 
 *       happen, or -1 for at the end
 * @val: bytes to insert
 * @len: number of bytes of @val to insert
 * 
 * Inserts @len bytes of @val into @string at @pos.  
 * Because @len is provided, @val may contain embedded 
 * nuls and need not be nul-terminated. If @pos is -1, 
 * bytes are inserted at the end of the string.
 *
 * Returns: the #GString
 */
Owen Taylor's avatar
Owen Taylor committed
634
GString*
635
g_string_insert_len (GString     *string,
636
		     gssize       pos,    
637
		     const gchar *val,
638
		     gssize       len)    
Owen Taylor's avatar
Owen Taylor committed
639 640
{
  g_return_val_if_fail (string != NULL, NULL);
641
  g_return_val_if_fail (val != NULL, string);
642 643 644 645 646 647

  if (len < 0)
    len = strlen (val);

  if (pos < 0)
    pos = string->len;
648
  else
649
    g_return_val_if_fail (pos <= string->len, string);
Owen Taylor's avatar
Owen Taylor committed
650

651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
  /* Check whether val represents a substring of string.  This test
     probably violates chapter and verse of the C standards, since
     ">=" and "<=" are only valid when val really is a substring.
     In practice, it will work on modern archs.  */
  if (val >= string->str && val <= string->str + string->len)
    {
      gsize offset = val - string->str;
      gsize precount = 0;

      g_string_maybe_expand (string, len);
      val = string->str + offset;
      /* At this point, val is valid again.  */

      /* Open up space where we are going to insert.  */
      if (pos < string->len)
	g_memmove (string->str + pos + len, string->str + pos, string->len - pos);

      /* Move the source part before the gap, if any.  */
      if (offset < pos)
	{
	  precount = MIN (len, pos - offset);
	  memcpy (string->str + pos, val, precount);
	}

      /* Move the source part after the gap, if any.  */
      if (len > precount)
	memcpy (string->str + pos + precount,
		val + /* Already moved: */ precount + /* Space opened up: */ len,
		len - precount);
    }
  else
    {
      g_string_maybe_expand (string, len);

      /* If we aren't appending at the end, move a hunk
       * of the old string to the end, opening up space
       */
      if (pos < string->len)
	g_memmove (string->str + pos + len, string->str + pos, string->len - pos);

      /* insert the new string */
692 693 694 695
      if (len == 1)
        string->str[pos] = *val;
      else
        memcpy (string->str + pos, val, len);
696
    }
Owen Taylor's avatar
Owen Taylor committed
697 698 699

  string->len += len;

700 701
  string->str[string->len] = 0;

702
  return string;
Owen Taylor's avatar
Owen Taylor committed
703 704
}

Matthias Clasen's avatar
Matthias Clasen committed
705 706 707 708 709 710 711 712 713
/**
 * g_string_append:
 * @string: a #GString.
 * @val: the string to append onto the end of the #GString.
 * 
 * Adds a string onto the end of a #GString, expanding it if necessary.
 *
 * Returns: the #GString.
 */
Owen Taylor's avatar
Owen Taylor committed
714
GString*
715
g_string_append (GString     *string,
716 717
		 const gchar *val)
{  
718 719
  g_return_val_if_fail (string != NULL, NULL);
  g_return_val_if_fail (val != NULL, string);
Owen Taylor's avatar
Owen Taylor committed
720

721
  return g_string_insert_len (string, -1, val, -1);
Owen Taylor's avatar
Owen Taylor committed
722 723
}

Matthias Clasen's avatar
Matthias Clasen committed
724 725 726 727 728 729 730 731 732 733 734 735
/**
 * g_string_append_len:
 * @string: a #GString
 * @val: bytes to append
 * @len: number of bytes of @val to use.
 * 
 * Appends @len bytes of @val to @string. 
 * Because @len is provided, @val may contain 
 * embedded nuls and need not be nul-terminated.
 * 
 * Returns: the #GString
 */
Owen Taylor's avatar
Owen Taylor committed
736
GString*
737 738
g_string_append_len (GString	 *string,
                     const gchar *val,
739
                     gssize       len)    
Owen Taylor's avatar
Owen Taylor committed
740 741
{
  g_return_val_if_fail (string != NULL, NULL);
742
  g_return_val_if_fail (val != NULL, string);
Owen Taylor's avatar
Owen Taylor committed
743

744 745
  return g_string_insert_len (string, -1, val, len);
}
Owen Taylor's avatar
Owen Taylor committed
746

Matthias Clasen's avatar
Matthias Clasen committed
747 748 749 750 751 752 753 754 755
/**
 * g_string_append_c:
 * @string: a #GString.
 * @c: the byte to append onto the end of the #GString.
 *
 * Adds a byte onto the end of a #GString, expanding it if necessary.
 * 
 * Returns: the #GString.
 */
756
#undef g_string_append_c
757
GString*
758
g_string_append_c (GString *string,
759 760
		   gchar    c)
{
761
  g_return_val_if_fail (string != NULL, NULL);
Owen Taylor's avatar
Owen Taylor committed
762

763
  return g_string_insert_c (string, -1, c);
Owen Taylor's avatar
Owen Taylor committed
764 765
}

766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
/**
 * g_string_append_unichar:
 * @string: a #GString
 * @wc: a Unicode character
 * 
 * Converts a Unicode character into UTF-8, and appends it
 * to the string.
 * 
 * Return value: @string
 **/
GString*
g_string_append_unichar (GString  *string,
			 gunichar  wc)
{  
  g_return_val_if_fail (string != NULL, NULL);
  
  return g_string_insert_unichar (string, -1, wc);
}

Matthias Clasen's avatar
Matthias Clasen committed
785 786 787 788 789 790 791 792 793 794
/**
 * g_string_prepend:
 * @string: a #GString
 * @val: the string to prepend on the start of the #GString
 *
 * Adds a string on to the start of a #GString, 
 * expanding it if necessary.
 *
 * Returns: the #GString
 */
Owen Taylor's avatar
Owen Taylor committed
795
GString*
796
g_string_prepend (GString     *string,
797
		  const gchar *val)
Owen Taylor's avatar
Owen Taylor committed
798
{
799 800
  g_return_val_if_fail (string != NULL, NULL);
  g_return_val_if_fail (val != NULL, string);
801
  
802
  return g_string_insert_len (string, 0, val, -1);
803
}
Owen Taylor's avatar
Owen Taylor committed
804

Matthias Clasen's avatar
Matthias Clasen committed
805 806 807 808 809 810 811 812 813 814 815 816
/**
 * g_string_prepend_len:
 * @string: a #GString
 * @val: bytes to prepend
 * @len: number of bytes in @val to prepend
 *
 * Prepends @len bytes of @val to @string. 
 * Because @len is provided, @val may contain 
 * embedded nuls and need not be nul-terminated.
 *
 * Returns: the #GString passed in
 */
817 818 819
GString*
g_string_prepend_len (GString	  *string,
                      const gchar *val,
820
                      gssize       len)    
821
{
Owen Taylor's avatar
Owen Taylor committed
822
  g_return_val_if_fail (string != NULL, NULL);
823
  g_return_val_if_fail (val != NULL, string);
Owen Taylor's avatar
Owen Taylor committed
824

825 826
  return g_string_insert_len (string, 0, val, len);
}
Owen Taylor's avatar
Owen Taylor committed
827

Matthias Clasen's avatar
Matthias Clasen committed
828 829 830 831 832 833 834 835 836 837
/**
 * g_string_prepend_c:
 * @string: a #GString
 * @c: the byte to prepend on the start of the #GString
 *
 * Adds a byte onto the start of a #GString, 
 * expanding it if necessary.
 *
 * Returns: the #GString
 */
838
GString*
839
g_string_prepend_c (GString *string,
840 841
		    gchar    c)
{  
842
  g_return_val_if_fail (string != NULL, NULL);
843
  
844
  return g_string_insert_c (string, 0, c);
Owen Taylor's avatar
Owen Taylor committed
845 846
}

847
/**
848 849 850
 * g_string_prepend_unichar:
 * @string: a #GString.
 * @wc: a Unicode character.
851 852 853 854
 * 
 * Converts a Unicode character into UTF-8, and prepends it
 * to the string.
 * 
855
 * Return value: @string.
856 857 858 859 860 861 862 863 864 865
 **/
GString*
g_string_prepend_unichar (GString  *string,
			  gunichar  wc)
{  
  g_return_val_if_fail (string != NULL, NULL);
  
  return g_string_insert_unichar (string, 0, wc);
}

Matthias Clasen's avatar
Matthias Clasen committed
866 867 868 869 870 871 872 873 874 875 876
/**
 * g_string_insert:
 * @string: a #GString
 * @pos: the position to insert the copy of the string
 * @val: the string to insert
 *
 * Inserts a copy of a string into a #GString, 
 * expanding it if necessary.
 *
 * Returns: the #GString
 */
Owen Taylor's avatar
Owen Taylor committed
877
GString*
878
g_string_insert (GString     *string,
879
		 gssize       pos,    
Owen Taylor's avatar
Owen Taylor committed
880 881
		 const gchar *val)
{
882 883
  g_return_val_if_fail (string != NULL, NULL);
  g_return_val_if_fail (val != NULL, string);
884
  if (pos >= 0)
885
    g_return_val_if_fail (pos <= string->len, string);
886
  
887
  return g_string_insert_len (string, pos, val, -1);
Owen Taylor's avatar
Owen Taylor committed
888 889
}

Matthias Clasen's avatar
Matthias Clasen committed
890 891 892 893 894 895 896 897 898 899
/**
 * g_string_insert_c:
 * @string: a #GString
 * @pos: the position to insert the byte
 * @c: the byte to insert
 *
 * Inserts a byte into a #GString, expanding it if necessary.
 * 
 * Returns: the #GString
 */
900
GString*
901
g_string_insert_c (GString *string,
902
		   gssize   pos,    
Owen Taylor's avatar
Owen Taylor committed
903 904 905 906 907 908
		   gchar    c)
{
  g_return_val_if_fail (string != NULL, NULL);

  g_string_maybe_expand (string, 1);

909 910
  if (pos < 0)
    pos = string->len;
911
  else
912
    g_return_val_if_fail (pos <= string->len, string);
913 914 915 916
  
  /* If not just an append, move the old stuff */
  if (pos < string->len)
    g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
Owen Taylor's avatar
Owen Taylor committed
917 918 919 920 921 922 923

  string->str[pos] = c;

  string->len += 1;

  string->str[string->len] = 0;

924
  return string;
Owen Taylor's avatar
Owen Taylor committed
925 926
}

927 928
/**
 * g_string_insert_unichar:
929
 * @string: a #GString
930 931 932 933 934 935 936 937 938 939 940 941 942
 * @pos: the position at which to insert character, or -1 to
 *       append at the end of the string.
 * @wc: a Unicode character
 * 
 * Converts a Unicode character into UTF-8, and insert it
 * into the string at the given position.
 * 
 * Return value: @string
 **/
GString*
g_string_insert_unichar (GString *string,
			 gssize   pos,    
			 gunichar wc)
943 944 945
{
  gint charlen, first, i;
  gchar *dest;
946 947 948

  g_return_val_if_fail (string != NULL, NULL);

949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
  /* Code copied from g_unichar_to_utf() */
  if (wc < 0x80)
    {
      first = 0;
      charlen = 1;
    }
  else if (wc < 0x800)
    {
      first = 0xc0;
      charlen = 2;
    }
  else if (wc < 0x10000)
    {
      first = 0xe0;
      charlen = 3;
    }
   else if (wc < 0x200000)
    {
      first = 0xf0;
      charlen = 4;
    }
  else if (wc < 0x4000000)
    {
      first = 0xf8;
      charlen = 5;
    }
  else
    {
      first = 0xfc;
      charlen = 6;
    }
  /* End of copied code */

  g_string_maybe_expand (string, charlen);

  if (pos < 0)
    pos = string->len;
  else
    g_return_val_if_fail (pos <= string->len, string);

  /* If not just an append, move the old stuff */
  if (pos < string->len)
    g_memmove (string->str + pos + charlen, string->str + pos, string->len - pos);

  dest = string->str + pos;
  /* Code copied from g_unichar_to_utf() */
  for (i = charlen - 1; i > 0; --i)
    {
      dest[i] = (wc & 0x3f) | 0x80;
      wc >>= 6;
    }
  dest[0] = wc | first;
  /* End of copied code */
  
  string->len += charlen;

  string->str[string->len] = 0;

  return string;
1008 1009
}

Matthias Clasen's avatar
Matthias Clasen committed
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
/**
 * g_string_erase:
 * @string: a #GString
 * @pos: the position of the content to remove
 * @len: the number of bytes to remove, or -1 to remove all
 *       following bytes
 *
 * Removes @len bytes from a #GString, starting at position @pos.
 * The rest of the #GString is shifted down to fill the gap.
 *
 * Returns: the #GString
 */
Owen Taylor's avatar
Owen Taylor committed
1022
GString*
1023
g_string_erase (GString *string,
1024 1025
		gssize   pos,
		gssize   len)
Owen Taylor's avatar
Owen Taylor committed
1026 1027
{
  g_return_val_if_fail (string != NULL, NULL);
1028 1029
  g_return_val_if_fail (pos >= 0, string);
  g_return_val_if_fail (pos <= string->len, string);
Owen Taylor's avatar
Owen Taylor committed
1030

1031 1032 1033 1034
  if (len < 0)
    len = string->len - pos;
  else
    {
1035
      g_return_val_if_fail (pos + len <= string->len, string);
1036 1037 1038 1039

      if (pos + len < string->len)
	g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
    }
Owen Taylor's avatar
Owen Taylor committed
1040 1041 1042 1043 1044

  string->len -= len;
  
  string->str[string->len] = 0;

1045
  return string;
Owen Taylor's avatar
Owen Taylor committed
1046 1047
}

Owen Taylor's avatar
Owen Taylor committed
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
/**
 * g_string_ascii_down:
 * @string: a GString
 * 
 * Converts all upper case ASCII letters to lower case ASCII letters.
 * 
 * Return value: passed-in @string pointer, with all the upper case
 *               characters converted to lower case in place, with
 *               semantics that exactly match g_ascii_tolower.
 **/
GString*
g_string_ascii_down (GString *string)
{
  gchar *s;
1062
  gint n;
Owen Taylor's avatar
Owen Taylor committed
1063 1064 1065

  g_return_val_if_fail (string != NULL, NULL);

1066
  n = string->len;
Owen Taylor's avatar
Owen Taylor committed
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
  s = string->str;

  while (n)
    {
      *s = g_ascii_tolower (*s);
      s++;
      n--;
    }

  return string;
}

/**
 * g_string_ascii_up:
 * @string: a GString
 * 
 * Converts all lower case ASCII letters to upper case ASCII letters.
 * 
 * Return value: passed-in @string pointer, with all the lower case
 *               characters converted to upper case in place, with
 *               semantics that exactly match g_ascii_toupper.
 **/
GString*
g_string_ascii_up (GString *string)
{
  gchar *s;
Matthias Clasen's avatar
Matthias Clasen committed
1093
  gint n;
Owen Taylor's avatar
Owen Taylor committed
1094 1095 1096

  g_return_val_if_fail (string != NULL, NULL);

Matthias Clasen's avatar
Matthias Clasen committed
1097
  n = string->len;
Owen Taylor's avatar
Owen Taylor committed
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
  s = string->str;

  while (n)
    {
      *s = g_ascii_toupper (*s);
      s++;
      n--;
    }

  return string;
}

1110 1111 1112 1113 1114 1115 1116 1117
/**
 * g_string_down:
 * @string: a #GString
 *  
 * Converts a #GString to lowercase.
 *
 * Returns: the #GString.
 *
1118
 * Deprecated:2.2: This function uses the locale-specific tolower() function, 
1119 1120 1121
 * which is almost never the right thing. Use g_string_ascii_down() or 
 * g_utf8_strdown() instead.
 */
Owen Taylor's avatar
Owen Taylor committed
1122
GString*
1123
g_string_down (GString *string)
Owen Taylor's avatar
Owen Taylor committed
1124
{
1125
  guchar *s;
1126
  glong n;
Owen Taylor's avatar
Owen Taylor committed
1127 1128 1129

  g_return_val_if_fail (string != NULL, NULL);

1130
  n = string->len;    
Martin Baulig's avatar
Martin Baulig committed
1131
  s = (guchar *) string->str;
Owen Taylor's avatar
Owen Taylor committed
1132

Owen Taylor's avatar
updated  
Owen Taylor committed
1133
  while (n)
Owen Taylor's avatar
Owen Taylor committed
1134
    {
Owen Taylor's avatar
Owen Taylor committed
1135 1136
      if (isupper (*s))
	*s = tolower (*s);
Owen Taylor's avatar
Owen Taylor committed
1137
      s++;
Owen Taylor's avatar
updated  
Owen Taylor committed
1138
      n--;
Owen Taylor's avatar
Owen Taylor committed
1139 1140
    }

1141
  return string;
Owen Taylor's avatar
Owen Taylor committed
1142 1143
}

1144 1145 1146 1147 1148 1149 1150 1151
/**
 * g_string_up:
 * @string: a #GString 
 * 
 * Converts a #GString to uppercase.
 * 
 * Return value: the #GString
 *
1152
 * Deprecated:2.2: This function uses the locale-specific toupper() function, 
1153 1154 1155
 * which is almost never the right thing. Use g_string_ascii_up() or 
 * g_utf8_strup() instead.
 **/
Owen Taylor's avatar
Owen Taylor committed
1156
GString*
1157
g_string_up (GString *string)
Owen Taylor's avatar
Owen Taylor committed
1158
{
1159
  guchar *s;
1160
  glong n;
Owen Taylor's avatar
Owen Taylor committed
1161 1162 1163

  g_return_val_if_fail (string != NULL, NULL);

1164
  n = string->len;
Martin Baulig's avatar
Martin Baulig committed
1165
  s = (guchar *) string->str;
Owen Taylor's avatar
Owen Taylor committed
1166

Owen Taylor's avatar
updated  
Owen Taylor committed
1167
  while (n)
Owen Taylor's avatar
Owen Taylor committed
1168
    {
Owen Taylor's avatar
Owen Taylor committed
1169 1170
      if (islower (*s))
	*s = toupper (*s);
Owen Taylor's avatar
Owen Taylor committed
1171
      s++;
Owen Taylor's avatar
updated  
Owen Taylor committed
1172
      n--;
Owen Taylor's avatar
Owen Taylor committed
1173 1174
    }

1175
  return string;
Owen Taylor's avatar
Owen Taylor committed
1176 1177 1178
}

static void
1179 1180 1181
g_string_append_printf_internal (GString     *string,
				 const gchar *fmt,
				 va_list      args)
Owen Taylor's avatar
Owen Taylor committed
1182
{
1183
  gchar *buffer;
1184 1185 1186 1187
  gint length;
  
  length = g_vasprintf (&buffer, fmt, args);
  g_string_append_len (string, buffer, length);
1188
  g_free (buffer);
Owen Taylor's avatar
Owen Taylor committed
1189 1190
}

Matthias Clasen's avatar
Matthias Clasen committed
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
/**
 * g_string_sprintf:
 * @string: a #GString.
 * @format: the string format. See the sprintf() documentation.
 * @Varargs: the parameters to insert into the format string.
 *
 * Writes a formatted string into a #GString.
 * This is similar to the standard sprintf() function,
 * except that the #GString buffer automatically expands 
 * to contain the results. The previous contents of the 
 * #GString are destroyed. 
 *
 * Deprecated: This function has been renamed to g_string_printf().
 */

/**
 * g_string_printf:
 * @string: a #GString.
 * @format: the string format. See the printf() documentation.
 * @Varargs: the parameters to insert into the format string.
 *
 * Writes a formatted string into a #GString.
 * This is similar to the standard sprintf() function,
 * except that the #GString buffer automatically expands 
 * to contain the results. The previous contents of the 
 * #GString are destroyed.
 */
Owen Taylor's avatar
Owen Taylor committed
1218
void
1219 1220 1221
g_string_printf (GString *string,
		 const gchar *fmt,
		 ...)
Owen Taylor's avatar
Owen Taylor committed
1222
{
1223
  va_list args;
Owen Taylor's avatar
Owen Taylor committed
1224 1225 1226

  g_string_truncate (string, 0);

1227
  va_start (args, fmt);
1228
  g_string_append_printf_internal (string, fmt, args);
1229
  va_end (args);
Owen Taylor's avatar
Owen Taylor committed
1230 1231
}

Matthias Clasen's avatar
Matthias Clasen committed
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
/**
 * g_string_sprintfa:
 * @string: a #GString.
 * @format: the string format. See the sprintf() documentation.
 * @Varargs: the parameters to insert into the format string.
 *
 * Appends a formatted string onto the end of a #GString.
 * This function is is similar to g_string_sprintf() except that
 * the text is appended to the #GString. 
 *
 * Deprecated: This function has been renamed to 
 * g_string_append_printf().
 */

/**
 * g_string_append_printf:
 * @string: a #GString.
 * @format: the string format. See the printf() documentation.
 * @Varargs: the parameters to insert into the format string.
 *
 * Appends a formatted string onto the end of a #GString.
 * This function is is similar to g_string_printf() except 
 * that the text is appended to the #GString.
 */
Owen Taylor's avatar
Owen Taylor committed
1256
void
1257 1258 1259
g_string_append_printf (GString *string,
			const gchar *fmt,
			...)
Owen Taylor's avatar
Owen Taylor committed
1260
{
1261
  va_list args;
Owen Taylor's avatar
Owen Taylor committed
1262

1263
  va_start (args, fmt);
1264
  g_string_append_printf_internal (string, fmt, args);
1265
  va_end (args);
Owen Taylor's avatar
Owen Taylor committed
1266
}
1267 1268 1269

#define __G_STRING_C__
#include "galiasdef.c"