Signal waiting threads, problem noticed by Christian Kellner.
[glib.git] / glib / gstring.c
blob621c61d1bc40dc48bc32d0632f4ee0cd185b7eef
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/.
27 /*
28 * MT safe
31 #include "config.h"
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #include <stdarg.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <ctype.h>
42 #include "glib.h"
43 #include "gprintf.h"
45 #include "galias.h"
47 struct _GStringChunk
49 GHashTable *const_table;
50 GSList *storage_list;
51 gsize storage_next;
52 gsize this_size;
53 gsize default_size;
56 /* Hash Functions.
59 /**
60 * g_str_equal:
61 * @v1: a key.
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.
70 gboolean
71 g_str_equal (gconstpointer v1,
72 gconstpointer v2)
74 const gchar *string1 = v1;
75 const gchar *string2 = v2;
77 return strcmp (string1, string2) == 0;
80 /**
81 * g_str_hash:
82 * @v: a string key.
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.
90 guint
91 g_str_hash (gconstpointer v)
93 /* 31 bit hash function */
94 const signed char *p = v;
95 guint32 h = *p;
97 if (h)
98 for (p += 1; *p != '\0'; p++)
99 h = (h << 5) - h + *p;
101 return h;
104 #define MY_MAXSIZE ((gsize)-1)
106 static inline gsize
107 nearest_power (gsize base, gsize num)
109 if (num > MY_MAXSIZE / 2)
111 return MY_MAXSIZE;
113 else
115 gsize n = base;
117 while (n < num)
118 n <<= 1;
120 return n;
124 /* String Chunks.
127 GStringChunk*
128 g_string_chunk_new (gsize default_size)
130 GStringChunk *new_chunk = g_new (GStringChunk, 1);
131 gsize size = 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;
141 return new_chunk;
144 void
145 g_string_chunk_free (GStringChunk *chunk)
147 GSList *tmp_list;
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);
162 g_free (chunk);
165 gchar*
166 g_string_chunk_insert (GStringChunk *chunk,
167 const gchar *string)
169 g_return_val_if_fail (chunk != NULL, NULL);
171 return g_string_chunk_insert_len (chunk, string, -1);
174 gchar*
175 g_string_chunk_insert_const (GStringChunk *chunk,
176 const gchar *string)
178 char* lookup;
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);
187 if (!lookup)
189 lookup = g_string_chunk_insert (chunk, string);
190 g_hash_table_insert (chunk->const_table, lookup, lookup);
193 return 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
211 * Since: 2.4
213 gchar*
214 g_string_chunk_insert_len (GStringChunk *chunk,
215 const gchar *string,
216 gssize len)
218 gssize size;
219 gchar* pos;
221 g_return_val_if_fail (chunk != NULL, NULL);
223 if (len < 0)
224 size = strlen (string);
225 else
226 size = len;
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);
244 if (len > 0)
245 size = strlen (pos);
247 chunk->storage_next += size + 1;
249 return pos;
252 /* Strings.
254 static void
255 g_string_maybe_expand (GString* string,
256 gsize len)
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);
265 GString*
266 g_string_sized_new (gsize dfl_size)
268 GString *string = g_slice_new (GString);
270 string->allocated_len = 0;
271 string->len = 0;
272 string->str = NULL;
274 g_string_maybe_expand (string, MAX (dfl_size, 2));
275 string->str[0] = 0;
277 return string;
280 GString*
281 g_string_new (const gchar *init)
283 GString *string;
285 if (init == NULL || *init == '\0')
286 string = g_string_sized_new (2);
287 else
289 gint len;
291 len = strlen (init);
292 string = g_string_sized_new (len + 2);
294 g_string_append_len (string, init, len);
297 return string;
300 GString*
301 g_string_new_len (const gchar *init,
302 gssize len)
304 GString *string;
306 if (len < 0)
307 return g_string_new (init);
308 else
310 string = g_string_sized_new (len);
312 if (init)
313 g_string_append_len (string, init, len);
315 return string;
319 gchar*
320 g_string_free (GString *string,
321 gboolean free_segment)
323 gchar *segment;
325 g_return_val_if_fail (string != NULL, NULL);
327 if (free_segment)
329 g_free (string->str);
330 segment = NULL;
332 else
333 segment = string->str;
335 g_slice_free (GString, string);
337 return segment;
340 gboolean
341 g_string_equal (const GString *v,
342 const GString *v2)
344 gchar *p, *q;
345 GString *string1 = (GString *) v;
346 GString *string2 = (GString *) v2;
347 gsize i = string1->len;
349 if (i != string2->len)
350 return FALSE;
352 p = string1->str;
353 q = string2->str;
354 while (i)
356 if (*p != *q)
357 return FALSE;
358 p++;
359 q++;
360 i--;
362 return TRUE;
365 /* 31 bit hash function */
366 guint
367 g_string_hash (const GString *str)
369 const gchar *p = str->str;
370 gsize n = str->len;
371 guint h = 0;
373 while (n--)
375 h = (h << 5) - h + *p;
376 p++;
379 return h;
382 GString*
383 g_string_assign (GString *string,
384 const gchar *rval)
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
393 does not realloc. */
394 g_string_truncate (string, 0);
395 g_string_append (string, rval);
398 return string;
401 GString*
402 g_string_truncate (GString *string,
403 gsize len)
405 g_return_val_if_fail (string != NULL, NULL);
407 string->len = MIN (len, string->len);
408 string->str[string->len] = 0;
410 return string;
414 * g_string_set_size:
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
426 GString*
427 g_string_set_size (GString *string,
428 gsize len)
430 g_return_val_if_fail (string != NULL, NULL);
432 if (len >= string->allocated_len)
433 g_string_maybe_expand (string, len - string->len);
435 string->len = len;
436 string->str[len] = 0;
438 return string;
441 GString*
442 g_string_insert_len (GString *string,
443 gssize pos,
444 const gchar *val,
445 gssize len)
447 g_return_val_if_fail (string != NULL, NULL);
448 g_return_val_if_fail (val != NULL, string);
450 if (len < 0)
451 len = strlen (val);
453 if (pos < 0)
454 pos = string->len;
455 else
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;
465 gsize precount = 0;
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. */
476 if (offset < pos)
478 precount = MIN (len, pos - offset);
479 memcpy (string->str + pos, val, precount);
482 /* Move the source part after the gap, if any. */
483 if (len > precount)
484 memcpy (string->str + pos + precount,
485 val + /* Already moved: */ precount + /* Space opened up: */ len,
486 len - precount);
488 else
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 */
499 if (len == 1)
500 string->str[pos] = *val;
501 else
502 memcpy (string->str + pos, val, len);
505 string->len += len;
507 string->str[string->len] = 0;
509 return string;
512 GString*
513 g_string_append (GString *string,
514 const gchar *val)
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);
522 GString*
523 g_string_append_len (GString *string,
524 const gchar *val,
525 gssize len)
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
534 GString*
535 g_string_append_c (GString *string,
536 gchar c)
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
549 * to the string.
551 * Return value: @string
553 GString*
554 g_string_append_unichar (GString *string,
555 gunichar wc)
557 g_return_val_if_fail (string != NULL, NULL);
559 return g_string_insert_unichar (string, -1, wc);
562 GString*
563 g_string_prepend (GString *string,
564 const gchar *val)
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);
572 GString*
573 g_string_prepend_len (GString *string,
574 const gchar *val,
575 gssize len)
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);
583 GString*
584 g_string_prepend_c (GString *string,
585 gchar c)
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
598 * to the string.
600 * Return value: @string.
602 GString*
603 g_string_prepend_unichar (GString *string,
604 gunichar wc)
606 g_return_val_if_fail (string != NULL, NULL);
608 return g_string_insert_unichar (string, 0, wc);
611 GString*
612 g_string_insert (GString *string,
613 gssize pos,
614 const gchar *val)
616 g_return_val_if_fail (string != NULL, NULL);
617 g_return_val_if_fail (val != NULL, string);
618 if (pos >= 0)
619 g_return_val_if_fail (pos <= string->len, string);
621 return g_string_insert_len (string, pos, val, -1);
624 GString*
625 g_string_insert_c (GString *string,
626 gssize pos,
627 gchar c)
629 g_return_val_if_fail (string != NULL, NULL);
631 g_string_maybe_expand (string, 1);
633 if (pos < 0)
634 pos = string->len;
635 else
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;
644 string->len += 1;
646 string->str[string->len] = 0;
648 return string;
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
663 GString*
664 g_string_insert_unichar (GString *string,
665 gssize pos,
666 gunichar wc)
668 gint charlen, first, i;
669 gchar *dest;
671 g_return_val_if_fail (string != NULL, NULL);
673 /* Code copied from g_unichar_to_utf() */
674 if (wc < 0x80)
676 first = 0;
677 charlen = 1;
679 else if (wc < 0x800)
681 first = 0xc0;
682 charlen = 2;
684 else if (wc < 0x10000)
686 first = 0xe0;
687 charlen = 3;
689 else if (wc < 0x200000)
691 first = 0xf0;
692 charlen = 4;
694 else if (wc < 0x4000000)
696 first = 0xf8;
697 charlen = 5;
699 else
701 first = 0xfc;
702 charlen = 6;
704 /* End of copied code */
706 g_string_maybe_expand (string, charlen);
708 if (pos < 0)
709 pos = string->len;
710 else
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;
722 wc >>= 6;
724 dest[0] = wc | first;
725 /* End of copied code */
727 string->len += charlen;
729 string->str[string->len] = 0;
731 return string;
734 GString*
735 g_string_erase (GString *string,
736 gssize pos,
737 gssize len)
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);
743 if (len < 0)
744 len = string->len - pos;
745 else
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));
753 string->len -= len;
755 string->str[string->len] = 0;
757 return string;
761 * g_string_ascii_down:
762 * @string: a GString
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.
770 GString*
771 g_string_ascii_down (GString *string)
773 gchar *s;
774 gint n;
776 g_return_val_if_fail (string != NULL, NULL);
778 n = string->len;
779 s = string->str;
781 while (n)
783 *s = g_ascii_tolower (*s);
784 s++;
785 n--;
788 return string;
792 * g_string_ascii_up:
793 * @string: a GString
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.
801 GString*
802 g_string_ascii_up (GString *string)
804 gchar *s;
805 gint n;
807 g_return_val_if_fail (string != NULL, NULL);
809 n = string->len;
810 s = string->str;
812 while (n)
814 *s = g_ascii_toupper (*s);
815 s++;
816 n--;
819 return string;
823 * g_string_down:
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.
834 GString*
835 g_string_down (GString *string)
837 guchar *s;
838 glong n;
840 g_return_val_if_fail (string != NULL, NULL);
842 n = string->len;
843 s = (guchar *) string->str;
845 while (n)
847 if (isupper (*s))
848 *s = tolower (*s);
849 s++;
850 n--;
853 return string;
857 * g_string_up:
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.
868 GString*
869 g_string_up (GString *string)
871 guchar *s;
872 glong n;
874 g_return_val_if_fail (string != NULL, NULL);
876 n = string->len;
877 s = (guchar *) string->str;
879 while (n)
881 if (islower (*s))
882 *s = toupper (*s);
883 s++;
884 n--;
887 return string;
890 static void
891 g_string_append_printf_internal (GString *string,
892 const gchar *fmt,
893 va_list args)
895 gchar *buffer;
896 gint length;
898 length = g_vasprintf (&buffer, fmt, args);
899 g_string_append_len (string, buffer, length);
900 g_free (buffer);
903 void
904 g_string_printf (GString *string,
905 const gchar *fmt,
906 ...)
908 va_list args;
910 g_string_truncate (string, 0);
912 va_start (args, fmt);
913 g_string_append_printf_internal (string, fmt, args);
914 va_end (args);
917 void
918 g_string_append_printf (GString *string,
919 const gchar *fmt,
920 ...)
922 va_list args;
924 va_start (args, fmt);
925 g_string_append_printf_internal (string, fmt, args);
926 va_end (args);
929 #define __G_STRING_C__
930 #include "galiasdef.c"