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/.
49 GHashTable *const_table;
62 * @v2: a key to compare with @v1.
64 * Compares two strings and returns %TRUE if they are equal.
65 * It can be passed to g_hash_table_new() as the @key_equal_func
66 * parameter, when using strings as keys in a #GHashTable.
68 * Returns: %TRUE if the two keys match.
71 g_str_equal (gconstpointer v1,
74 const gchar *string1 = v1;
75 const gchar *string2 = v2;
77 return strcmp (string1, string2) == 0;
84 * Converts a string to a hash value.
85 * It can be passed to g_hash_table_new() as the @hash_func parameter,
86 * when using strings as keys in a #GHashTable.
88 * Returns: a hash value corresponding to the key.
91 g_str_hash (gconstpointer v)
93 /* 31 bit hash function */
94 const signed char *p = v;
98 for (p += 1; *p != '\0'; p++)
99 h = (h << 5) - h + *p;
104 #define MY_MAXSIZE ((gsize)-1)
107 nearest_power (gsize base, gsize num)
109 if (num > MY_MAXSIZE / 2)
128 g_string_chunk_new (gsize default_size)
130 GStringChunk *new_chunk = g_new (GStringChunk, 1);
133 size = nearest_power (1, default_size);
135 new_chunk->const_table = NULL;
136 new_chunk->storage_list = NULL;
137 new_chunk->storage_next = size;
138 new_chunk->default_size = size;
139 new_chunk->this_size = size;
145 g_string_chunk_free (GStringChunk *chunk)
149 g_return_if_fail (chunk != NULL);
151 if (chunk->storage_list)
153 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
154 g_free (tmp_list->data);
156 g_slist_free (chunk->storage_list);
159 if (chunk->const_table)
160 g_hash_table_destroy (chunk->const_table);
166 * g_string_chunk_clear:
167 * @chunk: a #GStringChunk
169 * Frees all strings contained within the #GStringChunk.
170 * After calling g_string_chunk_clear() it is not safe to
171 * access any of the strings which were contained within it.
176 g_string_chunk_clear (GStringChunk *chunk)
180 g_return_if_fail (chunk != NULL);
182 if (chunk->storage_list)
184 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
185 g_free (tmp_list->data);
187 g_slist_free (chunk->storage_list);
189 chunk->storage_list = NULL;
190 chunk->storage_next = chunk->default_size;
191 chunk->this_size = chunk->default_size;
194 if (chunk->const_table)
195 g_hash_table_remove_all (chunk->const_table);
199 g_string_chunk_insert (GStringChunk *chunk,
202 g_return_val_if_fail (chunk != NULL, NULL);
204 return g_string_chunk_insert_len (chunk, string, -1);
208 g_string_chunk_insert_const (GStringChunk *chunk,
213 g_return_val_if_fail (chunk != NULL, NULL);
215 if (!chunk->const_table)
216 chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
218 lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
222 lookup = g_string_chunk_insert (chunk, string);
223 g_hash_table_insert (chunk->const_table, lookup, lookup);
230 * g_string_chunk_insert_len:
231 * @chunk: a #GStringChunk
232 * @string: bytes to insert
233 * @len: number of bytes of @string to insert, or -1 to insert a
234 * nul-terminated string.
236 * Adds a copy of the first @len bytes of @string to the #GStringChunk. The
237 * copy is nul-terminated.
239 * The characters in the string can be changed, if necessary, though you
240 * should not change anything after the end of the string.
242 * Return value: a pointer to the copy of @string within the #GStringChunk
247 g_string_chunk_insert_len (GStringChunk *chunk,
254 g_return_val_if_fail (chunk != NULL, NULL);
257 size = strlen (string);
261 if ((chunk->storage_next + size + 1) > chunk->this_size)
263 gsize new_size = nearest_power (chunk->default_size, size + 1);
265 chunk->storage_list = g_slist_prepend (chunk->storage_list,
266 g_new (gchar, new_size));
268 chunk->this_size = new_size;
269 chunk->storage_next = 0;
272 pos = ((gchar *) chunk->storage_list->data) + chunk->storage_next;
274 *(pos + size) = '\0';
276 strncpy (pos, string, size);
280 chunk->storage_next += size + 1;
288 g_string_maybe_expand (GString* string,
291 if (string->len + len >= string->allocated_len)
293 string->allocated_len = nearest_power (1, string->len + len + 1);
294 string->str = g_realloc (string->str, string->allocated_len);
299 g_string_sized_new (gsize dfl_size)
301 GString *string = g_slice_new (GString);
303 string->allocated_len = 0;
307 g_string_maybe_expand (string, MAX (dfl_size, 2));
314 g_string_new (const gchar *init)
318 if (init == NULL || *init == '\0')
319 string = g_string_sized_new (2);
325 string = g_string_sized_new (len + 2);
327 g_string_append_len (string, init, len);
334 g_string_new_len (const gchar *init,
340 return g_string_new (init);
343 string = g_string_sized_new (len);
346 g_string_append_len (string, init, len);
353 g_string_free (GString *string,
354 gboolean free_segment)
358 g_return_val_if_fail (string != NULL, NULL);
362 g_free (string->str);
366 segment = string->str;
368 g_slice_free (GString, string);
374 g_string_equal (const GString *v,
378 GString *string1 = (GString *) v;
379 GString *string2 = (GString *) v2;
380 gsize i = string1->len;
382 if (i != string2->len)
398 /* 31 bit hash function */
400 g_string_hash (const GString *str)
402 const gchar *p = str->str;
408 h = (h << 5) - h + *p;
416 g_string_assign (GString *string,
419 g_return_val_if_fail (string != NULL, NULL);
420 g_return_val_if_fail (rval != NULL, string);
422 /* Make sure assigning to itself doesn't corrupt the string. */
423 if (string->str != rval)
425 /* Assigning from substring should be ok since g_string_truncate
427 g_string_truncate (string, 0);
428 g_string_append (string, rval);
435 g_string_truncate (GString *string,
438 g_return_val_if_fail (string != NULL, NULL);
440 string->len = MIN (len, string->len);
441 string->str[string->len] = 0;
448 * @string: a #GString
449 * @len: the new length
451 * Sets the length of a #GString. If the length is less than
452 * the current length, the string will be truncated. If the
453 * length is greater than the current length, the contents
454 * of the newly added area are undefined. (However, as
455 * always, string->str[string->len] will be a nul byte.)
457 * Return value: @string
460 g_string_set_size (GString *string,
463 g_return_val_if_fail (string != NULL, NULL);
465 if (len >= string->allocated_len)
466 g_string_maybe_expand (string, len - string->len);
469 string->str[len] = 0;
475 g_string_insert_len (GString *string,
480 g_return_val_if_fail (string != NULL, NULL);
481 g_return_val_if_fail (val != NULL, string);
489 g_return_val_if_fail (pos <= string->len, string);
491 /* Check whether val represents a substring of string. This test
492 probably violates chapter and verse of the C standards, since
493 ">=" and "<=" are only valid when val really is a substring.
494 In practice, it will work on modern archs. */
495 if (val >= string->str && val <= string->str + string->len)
497 gsize offset = val - string->str;
500 g_string_maybe_expand (string, len);
501 val = string->str + offset;
502 /* At this point, val is valid again. */
504 /* Open up space where we are going to insert. */
505 if (pos < string->len)
506 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
508 /* Move the source part before the gap, if any. */
511 precount = MIN (len, pos - offset);
512 memcpy (string->str + pos, val, precount);
515 /* Move the source part after the gap, if any. */
517 memcpy (string->str + pos + precount,
518 val + /* Already moved: */ precount + /* Space opened up: */ len,
523 g_string_maybe_expand (string, len);
525 /* If we aren't appending at the end, move a hunk
526 * of the old string to the end, opening up space
528 if (pos < string->len)
529 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
531 /* insert the new string */
533 string->str[pos] = *val;
535 memcpy (string->str + pos, val, len);
540 string->str[string->len] = 0;
546 g_string_append (GString *string,
549 g_return_val_if_fail (string != NULL, NULL);
550 g_return_val_if_fail (val != NULL, string);
552 return g_string_insert_len (string, -1, val, -1);
556 g_string_append_len (GString *string,
560 g_return_val_if_fail (string != NULL, NULL);
561 g_return_val_if_fail (val != NULL, string);
563 return g_string_insert_len (string, -1, val, len);
566 #undef g_string_append_c
568 g_string_append_c (GString *string,
571 g_return_val_if_fail (string != NULL, NULL);
573 return g_string_insert_c (string, -1, c);
577 * g_string_append_unichar:
578 * @string: a #GString
579 * @wc: a Unicode character
581 * Converts a Unicode character into UTF-8, and appends it
584 * Return value: @string
587 g_string_append_unichar (GString *string,
590 g_return_val_if_fail (string != NULL, NULL);
592 return g_string_insert_unichar (string, -1, wc);
596 g_string_prepend (GString *string,
599 g_return_val_if_fail (string != NULL, NULL);
600 g_return_val_if_fail (val != NULL, string);
602 return g_string_insert_len (string, 0, val, -1);
606 g_string_prepend_len (GString *string,
610 g_return_val_if_fail (string != NULL, NULL);
611 g_return_val_if_fail (val != NULL, string);
613 return g_string_insert_len (string, 0, val, len);
617 g_string_prepend_c (GString *string,
620 g_return_val_if_fail (string != NULL, NULL);
622 return g_string_insert_c (string, 0, c);
626 * g_string_prepend_unichar:
627 * @string: a #GString.
628 * @wc: a Unicode character.
630 * Converts a Unicode character into UTF-8, and prepends it
633 * Return value: @string.
636 g_string_prepend_unichar (GString *string,
639 g_return_val_if_fail (string != NULL, NULL);
641 return g_string_insert_unichar (string, 0, wc);
645 g_string_insert (GString *string,
649 g_return_val_if_fail (string != NULL, NULL);
650 g_return_val_if_fail (val != NULL, string);
652 g_return_val_if_fail (pos <= string->len, string);
654 return g_string_insert_len (string, pos, val, -1);
658 g_string_insert_c (GString *string,
662 g_return_val_if_fail (string != NULL, NULL);
664 g_string_maybe_expand (string, 1);
669 g_return_val_if_fail (pos <= string->len, string);
671 /* If not just an append, move the old stuff */
672 if (pos < string->len)
673 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
675 string->str[pos] = c;
679 string->str[string->len] = 0;
685 * g_string_insert_unichar:
686 * @string: a #GString
687 * @pos: the position at which to insert character, or -1 to
688 * append at the end of the string.
689 * @wc: a Unicode character
691 * Converts a Unicode character into UTF-8, and insert it
692 * into the string at the given position.
694 * Return value: @string
697 g_string_insert_unichar (GString *string,
701 gint charlen, first, i;
704 g_return_val_if_fail (string != NULL, NULL);
706 /* Code copied from g_unichar_to_utf() */
717 else if (wc < 0x10000)
722 else if (wc < 0x200000)
727 else if (wc < 0x4000000)
737 /* End of copied code */
739 g_string_maybe_expand (string, charlen);
744 g_return_val_if_fail (pos <= string->len, string);
746 /* If not just an append, move the old stuff */
747 if (pos < string->len)
748 g_memmove (string->str + pos + charlen, string->str + pos, string->len - pos);
750 dest = string->str + pos;
751 /* Code copied from g_unichar_to_utf() */
752 for (i = charlen - 1; i > 0; --i)
754 dest[i] = (wc & 0x3f) | 0x80;
757 dest[0] = wc | first;
758 /* End of copied code */
760 string->len += charlen;
762 string->str[string->len] = 0;
768 g_string_erase (GString *string,
772 g_return_val_if_fail (string != NULL, NULL);
773 g_return_val_if_fail (pos >= 0, string);
774 g_return_val_if_fail (pos <= string->len, string);
777 len = string->len - pos;
780 g_return_val_if_fail (pos + len <= string->len, string);
782 if (pos + len < string->len)
783 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
788 string->str[string->len] = 0;
794 * g_string_ascii_down:
797 * Converts all upper case ASCII letters to lower case ASCII letters.
799 * Return value: passed-in @string pointer, with all the upper case
800 * characters converted to lower case in place, with
801 * semantics that exactly match g_ascii_tolower.
804 g_string_ascii_down (GString *string)
809 g_return_val_if_fail (string != NULL, NULL);
816 *s = g_ascii_tolower (*s);
828 * Converts all lower case ASCII letters to upper case ASCII letters.
830 * Return value: passed-in @string pointer, with all the lower case
831 * characters converted to upper case in place, with
832 * semantics that exactly match g_ascii_toupper.
835 g_string_ascii_up (GString *string)
840 g_return_val_if_fail (string != NULL, NULL);
847 *s = g_ascii_toupper (*s);
857 * @string: a #GString
859 * Converts a #GString to lowercase.
861 * Returns: the #GString.
863 * Deprecated:2.2: This function uses the locale-specific tolower() function,
864 * which is almost never the right thing. Use g_string_ascii_down() or
865 * g_utf8_strdown() instead.
868 g_string_down (GString *string)
873 g_return_val_if_fail (string != NULL, NULL);
876 s = (guchar *) string->str;
891 * @string: a #GString
893 * Converts a #GString to uppercase.
895 * Return value: the #GString
897 * Deprecated:2.2: This function uses the locale-specific toupper() function,
898 * which is almost never the right thing. Use g_string_ascii_up() or
899 * g_utf8_strup() instead.
902 g_string_up (GString *string)
907 g_return_val_if_fail (string != NULL, NULL);
910 s = (guchar *) string->str;
924 g_string_append_printf_internal (GString *string,
931 length = g_vasprintf (&buffer, fmt, args);
932 g_string_append_len (string, buffer, length);
937 g_string_printf (GString *string,
943 g_string_truncate (string, 0);
945 va_start (args, fmt);
946 g_string_append_printf_internal (string, fmt, args);
951 g_string_append_printf (GString *string,
957 va_start (args, fmt);
958 g_string_append_printf_internal (string, fmt, args);
962 #define __G_STRING_C__
963 #include "galiasdef.c"