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_insert (GStringChunk
*chunk
,
169 g_return_val_if_fail (chunk
!= NULL
, NULL
);
171 return g_string_chunk_insert_len (chunk
, string
, -1);
175 g_string_chunk_insert_const (GStringChunk
*chunk
,
180 g_return_val_if_fail (chunk
!= NULL
, NULL
);
182 if (!chunk
->const_table
)
183 chunk
->const_table
= g_hash_table_new (g_str_hash
, g_str_equal
);
185 lookup
= (char*) g_hash_table_lookup (chunk
->const_table
, (gchar
*)string
);
189 lookup
= g_string_chunk_insert (chunk
, string
);
190 g_hash_table_insert (chunk
->const_table
, lookup
, lookup
);
197 * g_string_chunk_insert_len:
198 * @chunk: a #GStringChunk
199 * @string: bytes to insert
200 * @len: number of bytes of @string to insert, or -1 to insert a
201 * nul-terminated string.
203 * Adds a copy of the first @len bytes of @string to the #GStringChunk. The
204 * copy is nul-terminated.
206 * The characters in the string can be changed, if necessary, though you
207 * should not change anything after the end of the string.
209 * Return value: a pointer to the copy of @string within the #GStringChunk
214 g_string_chunk_insert_len (GStringChunk
*chunk
,
221 g_return_val_if_fail (chunk
!= NULL
, NULL
);
224 size
= strlen (string
);
228 if ((chunk
->storage_next
+ size
+ 1) > chunk
->this_size
)
230 gsize new_size
= nearest_power (chunk
->default_size
, size
+ 1);
232 chunk
->storage_list
= g_slist_prepend (chunk
->storage_list
,
233 g_new (gchar
, new_size
));
235 chunk
->this_size
= new_size
;
236 chunk
->storage_next
= 0;
239 pos
= ((gchar
*) chunk
->storage_list
->data
) + chunk
->storage_next
;
241 *(pos
+ size
) = '\0';
243 strncpy (pos
, string
, size
);
247 chunk
->storage_next
+= size
+ 1;
255 g_string_maybe_expand (GString
* string
,
258 if (string
->len
+ len
>= string
->allocated_len
)
260 string
->allocated_len
= nearest_power (1, string
->len
+ len
+ 1);
261 string
->str
= g_realloc (string
->str
, string
->allocated_len
);
266 g_string_sized_new (gsize dfl_size
)
268 GString
*string
= g_slice_new (GString
);
270 string
->allocated_len
= 0;
274 g_string_maybe_expand (string
, MAX (dfl_size
, 2));
281 g_string_new (const gchar
*init
)
285 if (init
== NULL
|| *init
== '\0')
286 string
= g_string_sized_new (2);
292 string
= g_string_sized_new (len
+ 2);
294 g_string_append_len (string
, init
, len
);
301 g_string_new_len (const gchar
*init
,
307 return g_string_new (init
);
310 string
= g_string_sized_new (len
);
313 g_string_append_len (string
, init
, len
);
320 g_string_free (GString
*string
,
321 gboolean free_segment
)
325 g_return_val_if_fail (string
!= NULL
, NULL
);
329 g_free (string
->str
);
333 segment
= string
->str
;
335 g_slice_free (GString
, string
);
341 g_string_equal (const GString
*v
,
345 GString
*string1
= (GString
*) v
;
346 GString
*string2
= (GString
*) v2
;
347 gsize i
= string1
->len
;
349 if (i
!= string2
->len
)
365 /* 31 bit hash function */
367 g_string_hash (const GString
*str
)
369 const gchar
*p
= str
->str
;
375 h
= (h
<< 5) - h
+ *p
;
383 g_string_assign (GString
*string
,
386 g_return_val_if_fail (string
!= NULL
, NULL
);
387 g_return_val_if_fail (rval
!= NULL
, string
);
389 /* Make sure assigning to itself doesn't corrupt the string. */
390 if (string
->str
!= rval
)
392 /* Assigning from substring should be ok since g_string_truncate
394 g_string_truncate (string
, 0);
395 g_string_append (string
, rval
);
402 g_string_truncate (GString
*string
,
405 g_return_val_if_fail (string
!= NULL
, NULL
);
407 string
->len
= MIN (len
, string
->len
);
408 string
->str
[string
->len
] = 0;
415 * @string: a #GString
416 * @len: the new length
418 * Sets the length of a #GString. If the length is less than
419 * the current length, the string will be truncated. If the
420 * length is greater than the current length, the contents
421 * of the newly added area are undefined. (However, as
422 * always, string->str[string->len] will be a nul byte.)
424 * Return value: @string
427 g_string_set_size (GString
*string
,
430 g_return_val_if_fail (string
!= NULL
, NULL
);
432 if (len
>= string
->allocated_len
)
433 g_string_maybe_expand (string
, len
- string
->len
);
436 string
->str
[len
] = 0;
442 g_string_insert_len (GString
*string
,
447 g_return_val_if_fail (string
!= NULL
, NULL
);
448 g_return_val_if_fail (val
!= NULL
, string
);
456 g_return_val_if_fail (pos
<= string
->len
, string
);
458 /* Check whether val represents a substring of string. This test
459 probably violates chapter and verse of the C standards, since
460 ">=" and "<=" are only valid when val really is a substring.
461 In practice, it will work on modern archs. */
462 if (val
>= string
->str
&& val
<= string
->str
+ string
->len
)
464 gsize offset
= val
- string
->str
;
467 g_string_maybe_expand (string
, len
);
468 val
= string
->str
+ offset
;
469 /* At this point, val is valid again. */
471 /* Open up space where we are going to insert. */
472 if (pos
< string
->len
)
473 g_memmove (string
->str
+ pos
+ len
, string
->str
+ pos
, string
->len
- pos
);
475 /* Move the source part before the gap, if any. */
478 precount
= MIN (len
, pos
- offset
);
479 memcpy (string
->str
+ pos
, val
, precount
);
482 /* Move the source part after the gap, if any. */
484 memcpy (string
->str
+ pos
+ precount
,
485 val
+ /* Already moved: */ precount
+ /* Space opened up: */ len
,
490 g_string_maybe_expand (string
, len
);
492 /* If we aren't appending at the end, move a hunk
493 * of the old string to the end, opening up space
495 if (pos
< string
->len
)
496 g_memmove (string
->str
+ pos
+ len
, string
->str
+ pos
, string
->len
- pos
);
498 /* insert the new string */
500 string
->str
[pos
] = *val
;
502 memcpy (string
->str
+ pos
, val
, len
);
507 string
->str
[string
->len
] = 0;
513 g_string_append (GString
*string
,
516 g_return_val_if_fail (string
!= NULL
, NULL
);
517 g_return_val_if_fail (val
!= NULL
, string
);
519 return g_string_insert_len (string
, -1, val
, -1);
523 g_string_append_len (GString
*string
,
527 g_return_val_if_fail (string
!= NULL
, NULL
);
528 g_return_val_if_fail (val
!= NULL
, string
);
530 return g_string_insert_len (string
, -1, val
, len
);
533 #undef g_string_append_c
535 g_string_append_c (GString
*string
,
538 g_return_val_if_fail (string
!= NULL
, NULL
);
540 return g_string_insert_c (string
, -1, c
);
544 * g_string_append_unichar:
545 * @string: a #GString
546 * @wc: a Unicode character
548 * Converts a Unicode character into UTF-8, and appends it
551 * Return value: @string
554 g_string_append_unichar (GString
*string
,
557 g_return_val_if_fail (string
!= NULL
, NULL
);
559 return g_string_insert_unichar (string
, -1, wc
);
563 g_string_prepend (GString
*string
,
566 g_return_val_if_fail (string
!= NULL
, NULL
);
567 g_return_val_if_fail (val
!= NULL
, string
);
569 return g_string_insert_len (string
, 0, val
, -1);
573 g_string_prepend_len (GString
*string
,
577 g_return_val_if_fail (string
!= NULL
, NULL
);
578 g_return_val_if_fail (val
!= NULL
, string
);
580 return g_string_insert_len (string
, 0, val
, len
);
584 g_string_prepend_c (GString
*string
,
587 g_return_val_if_fail (string
!= NULL
, NULL
);
589 return g_string_insert_c (string
, 0, c
);
593 * g_string_prepend_unichar:
594 * @string: a #GString.
595 * @wc: a Unicode character.
597 * Converts a Unicode character into UTF-8, and prepends it
600 * Return value: @string.
603 g_string_prepend_unichar (GString
*string
,
606 g_return_val_if_fail (string
!= NULL
, NULL
);
608 return g_string_insert_unichar (string
, 0, wc
);
612 g_string_insert (GString
*string
,
616 g_return_val_if_fail (string
!= NULL
, NULL
);
617 g_return_val_if_fail (val
!= NULL
, string
);
619 g_return_val_if_fail (pos
<= string
->len
, string
);
621 return g_string_insert_len (string
, pos
, val
, -1);
625 g_string_insert_c (GString
*string
,
629 g_return_val_if_fail (string
!= NULL
, NULL
);
631 g_string_maybe_expand (string
, 1);
636 g_return_val_if_fail (pos
<= string
->len
, string
);
638 /* If not just an append, move the old stuff */
639 if (pos
< string
->len
)
640 g_memmove (string
->str
+ pos
+ 1, string
->str
+ pos
, string
->len
- pos
);
642 string
->str
[pos
] = c
;
646 string
->str
[string
->len
] = 0;
652 * g_string_insert_unichar:
653 * @string: a #GString
654 * @pos: the position at which to insert character, or -1 to
655 * append at the end of the string.
656 * @wc: a Unicode character
658 * Converts a Unicode character into UTF-8, and insert it
659 * into the string at the given position.
661 * Return value: @string
664 g_string_insert_unichar (GString
*string
,
668 gint charlen
, first
, i
;
671 g_return_val_if_fail (string
!= NULL
, NULL
);
673 /* Code copied from g_unichar_to_utf() */
684 else if (wc
< 0x10000)
689 else if (wc
< 0x200000)
694 else if (wc
< 0x4000000)
704 /* End of copied code */
706 g_string_maybe_expand (string
, charlen
);
711 g_return_val_if_fail (pos
<= string
->len
, string
);
713 /* If not just an append, move the old stuff */
714 if (pos
< string
->len
)
715 g_memmove (string
->str
+ pos
+ charlen
, string
->str
+ pos
, string
->len
- pos
);
717 dest
= string
->str
+ pos
;
718 /* Code copied from g_unichar_to_utf() */
719 for (i
= charlen
- 1; i
> 0; --i
)
721 dest
[i
] = (wc
& 0x3f) | 0x80;
724 dest
[0] = wc
| first
;
725 /* End of copied code */
727 string
->len
+= charlen
;
729 string
->str
[string
->len
] = 0;
735 g_string_erase (GString
*string
,
739 g_return_val_if_fail (string
!= NULL
, NULL
);
740 g_return_val_if_fail (pos
>= 0, string
);
741 g_return_val_if_fail (pos
<= string
->len
, string
);
744 len
= string
->len
- pos
;
747 g_return_val_if_fail (pos
+ len
<= string
->len
, string
);
749 if (pos
+ len
< string
->len
)
750 g_memmove (string
->str
+ pos
, string
->str
+ pos
+ len
, string
->len
- (pos
+ len
));
755 string
->str
[string
->len
] = 0;
761 * g_string_ascii_down:
764 * Converts all upper case ASCII letters to lower case ASCII letters.
766 * Return value: passed-in @string pointer, with all the upper case
767 * characters converted to lower case in place, with
768 * semantics that exactly match g_ascii_tolower.
771 g_string_ascii_down (GString
*string
)
776 g_return_val_if_fail (string
!= NULL
, NULL
);
783 *s
= g_ascii_tolower (*s
);
795 * Converts all lower case ASCII letters to upper case ASCII letters.
797 * Return value: passed-in @string pointer, with all the lower case
798 * characters converted to upper case in place, with
799 * semantics that exactly match g_ascii_toupper.
802 g_string_ascii_up (GString
*string
)
807 g_return_val_if_fail (string
!= NULL
, NULL
);
814 *s
= g_ascii_toupper (*s
);
824 * @string: a #GString
826 * Converts a #GString to lowercase.
828 * Returns: the #GString.
830 * Deprecated:2.2: This function uses the locale-specific tolower() function,
831 * which is almost never the right thing. Use g_string_ascii_down() or
832 * g_utf8_strdown() instead.
835 g_string_down (GString
*string
)
840 g_return_val_if_fail (string
!= NULL
, NULL
);
843 s
= (guchar
*) string
->str
;
858 * @string: a #GString
860 * Converts a #GString to uppercase.
862 * Return value: the #GString
864 * Deprecated:2.2: This function uses the locale-specific toupper() function,
865 * which is almost never the right thing. Use g_string_ascii_up() or
866 * g_utf8_strup() instead.
869 g_string_up (GString
*string
)
874 g_return_val_if_fail (string
!= NULL
, NULL
);
877 s
= (guchar
*) string
->str
;
891 g_string_append_printf_internal (GString
*string
,
898 length
= g_vasprintf (&buffer
, fmt
, args
);
899 g_string_append_len (string
, buffer
, length
);
904 g_string_printf (GString
*string
,
910 g_string_truncate (string
, 0);
912 va_start (args
, fmt
);
913 g_string_append_printf_internal (string
, fmt
, args
);
918 g_string_append_printf (GString
*string
,
924 va_start (args
, fmt
);
925 g_string_append_printf_internal (string
, fmt
, args
);
929 #define __G_STRING_C__
930 #include "galiasdef.c"