1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
28 * Modified by Bruno Haible for use as a gnulib module.
54 GHashTable *const_table;
68 * @v2: a key to compare with @v1.
70 * Compares two strings and returns %TRUE if they are equal.
71 * It can be passed to g_hash_table_new() as the @key_equal_func
72 * parameter, when using strings as keys in a #GHashTable.
74 * Returns: %TRUE if the two keys match.
77 g_str_equal (gconstpointer v1,
80 const gchar *string1 = v1;
81 const gchar *string2 = v2;
83 return strcmp (string1, string2) == 0;
90 * Converts a string to a hash value.
91 * It can be passed to g_hash_table_new() as the @hash_func parameter,
92 * when using strings as keys in a #GHashTable.
94 * Returns: a hash value corresponding to the key.
97 g_str_hash (gconstpointer v)
99 /* 31 bit hash function */
100 const signed char *p = v;
104 for (p += 1; *p != '\0'; p++)
105 h = (h << 5) - h + *p;
110 #define MY_MAXSIZE ((gsize)-1)
113 nearest_power (gsize base, gsize num)
115 if (num > MY_MAXSIZE / 2)
136 g_string_chunk_new (gsize default_size)
138 GStringChunk *new_chunk = g_new (GStringChunk, 1);
141 size = nearest_power (1, default_size);
143 new_chunk->const_table = NULL;
144 new_chunk->storage_list = NULL;
145 new_chunk->storage_next = size;
146 new_chunk->default_size = size;
147 new_chunk->this_size = size;
153 g_string_chunk_free (GStringChunk *chunk)
157 g_return_if_fail (chunk != NULL);
159 if (chunk->storage_list)
161 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
162 g_free (tmp_list->data);
164 g_slist_free (chunk->storage_list);
167 if (chunk->const_table)
168 g_hash_table_destroy (chunk->const_table);
174 g_string_chunk_insert (GStringChunk *chunk,
177 g_return_val_if_fail (chunk != NULL, NULL);
179 return g_string_chunk_insert_len (chunk, string, -1);
183 g_string_chunk_insert_const (GStringChunk *chunk,
188 g_return_val_if_fail (chunk != NULL, NULL);
190 if (!chunk->const_table)
191 chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
193 lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
197 lookup = g_string_chunk_insert (chunk, string);
198 g_hash_table_insert (chunk->const_table, lookup, lookup);
205 * g_string_chunk_insert_len:
206 * @chunk: a #GStringChunk
207 * @string: bytes to insert
208 * @len: number of bytes of @string to insert, or -1 to insert a
209 * nul-terminated string.
211 * Adds a copy of the first @len bytes of @string to the #GStringChunk. The
212 * copy is nul-terminated.
214 * The characters in the string can be changed, if necessary, though you
215 * should not change anything after the end of the string.
217 * Return value: a pointer to the copy of @string within the #GStringChunk
222 g_string_chunk_insert_len (GStringChunk *chunk,
229 g_return_val_if_fail (chunk != NULL, NULL);
232 size = strlen (string);
236 if ((chunk->storage_next + size + 1) > chunk->this_size)
238 gsize new_size = nearest_power (chunk->default_size, size + 1);
240 chunk->storage_list = g_slist_prepend (chunk->storage_list,
241 g_new (gchar, new_size));
243 chunk->this_size = new_size;
244 chunk->storage_next = 0;
247 pos = ((gchar *) chunk->storage_list->data) + chunk->storage_next;
249 *(pos + size) = '\0';
251 strncpy (pos, string, size);
255 chunk->storage_next += size + 1;
265 g_string_maybe_expand (GString* string,
268 if (string->len + len >= string->allocated_len)
270 string->allocated_len = nearest_power (1, string->len + len + 1);
271 string->str = g_realloc (string->str, string->allocated_len);
276 g_string_sized_new (gsize dfl_size)
278 GString *string = g_slice_new (GString);
280 string->allocated_len = 0;
284 g_string_maybe_expand (string, MAX (dfl_size, 2));
291 g_string_new (const gchar *init)
295 if (init == NULL || *init == '\0')
296 string = g_string_sized_new (2);
302 string = g_string_sized_new (len + 2);
304 g_string_append_len (string, init, len);
311 g_string_new_len (const gchar *init,
317 return g_string_new (init);
320 string = g_string_sized_new (len);
323 g_string_append_len (string, init, len);
330 g_string_free (GString *string,
331 gboolean free_segment)
335 g_return_val_if_fail (string != NULL, NULL);
339 g_free (string->str);
343 segment = string->str;
345 g_slice_free (GString, string);
353 g_string_equal (const GString *v,
357 GString *string1 = (GString *) v;
358 GString *string2 = (GString *) v2;
359 gsize i = string1->len;
361 if (i != string2->len)
377 /* 31 bit hash function */
379 g_string_hash (const GString *str)
381 const gchar *p = str->str;
387 h = (h << 5) - h + *p;
395 g_string_assign (GString *string,
398 g_return_val_if_fail (string != NULL, NULL);
399 g_return_val_if_fail (rval != NULL, string);
401 /* Make sure assigning to itself doesn't corrupt the string. */
402 if (string->str != rval)
404 /* Assigning from substring should be ok since g_string_truncate
406 g_string_truncate (string, 0);
407 g_string_append (string, rval);
414 g_string_truncate (GString *string,
417 g_return_val_if_fail (string != NULL, NULL);
419 string->len = MIN (len, string->len);
420 string->str[string->len] = 0;
427 * @string: a #GString
428 * @len: the new length
430 * Sets the length of a #GString. If the length is less than
431 * the current length, the string will be truncated. If the
432 * length is greater than the current length, the contents
433 * of the newly added area are undefined. (However, as
434 * always, string->str[string->len] will be a nul byte.)
436 * Return value: @string
439 g_string_set_size (GString *string,
442 g_return_val_if_fail (string != NULL, NULL);
444 if (len >= string->allocated_len)
445 g_string_maybe_expand (string, len - string->len);
448 string->str[len] = 0;
456 g_string_insert_len (GString *string,
461 g_return_val_if_fail (string != NULL, NULL);
462 g_return_val_if_fail (val != NULL, string);
470 g_return_val_if_fail (pos <= string->len, string);
472 /* Check whether val represents a substring of string. This test
473 probably violates chapter and verse of the C standards, since
474 ">=" and "<=" are only valid when val really is a substring.
475 In practice, it will work on modern archs. */
476 if (val >= string->str && val <= string->str + string->len)
478 gsize offset = val - string->str;
481 g_string_maybe_expand (string, len);
482 val = string->str + offset;
483 /* At this point, val is valid again. */
485 /* Open up space where we are going to insert. */
486 if (pos < string->len)
487 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
489 /* Move the source part before the gap, if any. */
492 precount = MIN (len, pos - offset);
493 memcpy (string->str + pos, val, precount);
496 /* Move the source part after the gap, if any. */
498 memcpy (string->str + pos + precount,
499 val + /* Already moved: */ precount + /* Space opened up: */ len,
504 g_string_maybe_expand (string, len);
506 /* If we aren't appending at the end, move a hunk
507 * of the old string to the end, opening up space
509 if (pos < string->len)
510 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
512 /* insert the new string */
514 string->str[pos] = *val;
516 memcpy (string->str + pos, val, len);
521 string->str[string->len] = 0;
527 g_string_append (GString *string,
530 g_return_val_if_fail (string != NULL, NULL);
531 g_return_val_if_fail (val != NULL, string);
533 return g_string_insert_len (string, -1, val, -1);
537 g_string_append_len (GString *string,
541 g_return_val_if_fail (string != NULL, NULL);
542 g_return_val_if_fail (val != NULL, string);
544 return g_string_insert_len (string, -1, val, len);
547 #undef g_string_append_c
549 g_string_append_c (GString *string,
552 g_return_val_if_fail (string != NULL, NULL);
554 return g_string_insert_c (string, -1, c);
558 * g_string_append_unichar:
559 * @string: a #GString
560 * @wc: a Unicode character
562 * Converts a Unicode character into UTF-8, and appends it
565 * Return value: @string
568 g_string_append_unichar (GString *string,
571 g_return_val_if_fail (string != NULL, NULL);
573 return g_string_insert_unichar (string, -1, wc);
579 g_string_prepend (GString *string,
582 g_return_val_if_fail (string != NULL, NULL);
583 g_return_val_if_fail (val != NULL, string);
585 return g_string_insert_len (string, 0, val, -1);
589 g_string_prepend_len (GString *string,
593 g_return_val_if_fail (string != NULL, NULL);
594 g_return_val_if_fail (val != NULL, string);
596 return g_string_insert_len (string, 0, val, len);
600 g_string_prepend_c (GString *string,
603 g_return_val_if_fail (string != NULL, NULL);
605 return g_string_insert_c (string, 0, c);
609 * g_string_prepend_unichar:
610 * @string: a #GString.
611 * @wc: a Unicode character.
613 * Converts a Unicode character into UTF-8, and prepends it
616 * Return value: @string.
619 g_string_prepend_unichar (GString *string,
622 g_return_val_if_fail (string != NULL, NULL);
624 return g_string_insert_unichar (string, 0, wc);
628 g_string_insert (GString *string,
632 g_return_val_if_fail (string != NULL, NULL);
633 g_return_val_if_fail (val != NULL, string);
635 g_return_val_if_fail (pos <= string->len, string);
637 return g_string_insert_len (string, pos, val, -1);
643 g_string_insert_c (GString *string,
647 g_return_val_if_fail (string != NULL, NULL);
649 g_string_maybe_expand (string, 1);
654 g_return_val_if_fail (pos <= string->len, string);
656 /* If not just an append, move the old stuff */
657 if (pos < string->len)
658 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
660 string->str[pos] = c;
664 string->str[string->len] = 0;
670 * g_string_insert_unichar:
671 * @string: a #GString
672 * @pos: the position at which to insert character, or -1 to
673 * append at the end of the string.
674 * @wc: a Unicode character
676 * Converts a Unicode character into UTF-8, and insert it
677 * into the string at the given position.
679 * Return value: @string
682 g_string_insert_unichar (GString *string,
686 gint charlen, first, i;
689 g_return_val_if_fail (string != NULL, NULL);
691 /* Code copied from g_unichar_to_utf() */
702 else if (wc < 0x10000)
707 else if (wc < 0x200000)
712 else if (wc < 0x4000000)
722 /* End of copied code */
724 g_string_maybe_expand (string, charlen);
729 g_return_val_if_fail (pos <= string->len, string);
731 /* If not just an append, move the old stuff */
732 if (pos < string->len)
733 g_memmove (string->str + pos + charlen, string->str + pos, string->len - pos);
735 dest = string->str + pos;
736 /* Code copied from g_unichar_to_utf() */
737 for (i = charlen - 1; i > 0; --i)
739 dest[i] = (wc & 0x3f) | 0x80;
742 dest[0] = wc | first;
743 /* End of copied code */
745 string->len += charlen;
747 string->str[string->len] = 0;
755 g_string_erase (GString *string,
759 g_return_val_if_fail (string != NULL, NULL);
760 g_return_val_if_fail (pos >= 0, string);
761 g_return_val_if_fail (pos <= string->len, string);
764 len = string->len - pos;
767 g_return_val_if_fail (pos + len <= string->len, string);
769 if (pos + len < string->len)
770 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
775 string->str[string->len] = 0;
781 * g_string_ascii_down:
784 * Converts all upper case ASCII letters to lower case ASCII letters.
786 * Return value: passed-in @string pointer, with all the upper case
787 * characters converted to lower case in place, with
788 * semantics that exactly match g_ascii_tolower.
791 g_string_ascii_down (GString *string)
796 g_return_val_if_fail (string != NULL, NULL);
803 *s = g_ascii_tolower (*s);
815 * Converts all lower case ASCII letters to upper case ASCII letters.
817 * Return value: passed-in @string pointer, with all the lower case
818 * characters converted to upper case in place, with
819 * semantics that exactly match g_ascii_toupper.
822 g_string_ascii_up (GString *string)
827 g_return_val_if_fail (string != NULL, NULL);
834 *s = g_ascii_toupper (*s);
844 * @string: a #GString
846 * Converts a #GString to lowercase.
848 * Returns: the #GString.
850 * Deprecated:2.2: This function uses the locale-specific tolower() function,
851 * which is almost never the right thing. Use g_string_ascii_down() or
852 * g_utf8_strdown() instead.
855 g_string_down (GString *string)
860 g_return_val_if_fail (string != NULL, NULL);
863 s = (guchar *) string->str;
878 * @string: a #GString
880 * Converts a #GString to uppercase.
882 * Return value: the #GString
884 * Deprecated:2.2: This function uses the locale-specific toupper() function,
885 * which is almost never the right thing. Use g_string_ascii_up() or
886 * g_utf8_strup() instead.
889 g_string_up (GString *string)
894 g_return_val_if_fail (string != NULL, NULL);
897 s = (guchar *) string->str;
913 g_string_append_printf_internal (GString *string,
920 length = g_vasprintf (&buffer, fmt, args);
921 g_string_append_len (string, buffer, length);
928 g_string_printf (GString *string,
934 g_string_truncate (string, 0);
936 va_start (args, fmt);
937 g_string_append_printf_internal (string, fmt, args);
944 g_string_append_printf (GString *string,
950 va_start (args, fmt);
951 g_string_append_printf_internal (string, fmt, args);
956 #define __G_STRING_C__
957 #include "galiasdef.c"