/[zanavi_public1]/navit/navit/support/glib/gstring.c
ZANavi

Contents of /navit/navit/support/glib/gstring.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations) (download)
Fri Oct 28 21:19:04 2011 UTC (12 years, 5 months ago) by zoff99
File MIME type: text/plain
File size: 33935 byte(s)
import files
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 *
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.
8 *
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.
13 *
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.
18 */
19
20 /*
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/.
25 */
26
27 /*
28 * MT safe
29 */
30
31 #include "config.h"
32
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>
41
42 #include "glib.h"
43 #include "gprintf.h"
44
45 #include "galias.h"
46
47 #if NOT_NEEDED_FOR_NAVIT
48 struct _GStringChunk
49 {
50 GHashTable *const_table;
51 GSList *storage_list;
52 gsize storage_next;
53 gsize this_size;
54 gsize default_size;
55 };
56 #endif /* NOT_NEEDED_FOR_NAVIT */
57
58 /* Hash Functions.
59 */
60
61 /**
62 * g_str_equal:
63 * @v1: a key
64 * @v2: a key to compare with @v1
65 *
66 * Compares two strings for byte-by-byte equality and returns %TRUE
67 * if they are equal. It can be passed to g_hash_table_new() as the
68 * @key_equal_func parameter, when using strings as keys in a #GHashTable.
69 *
70 * Returns: %TRUE if the two keys match
71 */
72 gboolean
73 g_str_equal (gconstpointer v1,
74 gconstpointer v2)
75 {
76 const gchar *string1 = v1;
77 const gchar *string2 = v2;
78
79 return strcmp (string1, string2) == 0;
80 }
81
82 /**
83 * g_str_hash:
84 * @v: a string key
85 *
86 * Converts a string to a hash value.
87 * It can be passed to g_hash_table_new() as the @hash_func
88 * parameter, when using strings as keys in a #GHashTable.
89 *
90 * Returns: a hash value corresponding to the key
91 */
92 guint
93 g_str_hash (gconstpointer v)
94 {
95 /* 31 bit hash function */
96 const signed char *p = v;
97 guint32 h = *p;
98
99 if (h)
100 for (p += 1; *p != '\0'; p++)
101 h = (h << 5) - h + *p;
102
103 return h;
104 }
105
106 #if NOT_NEEDED_FOR_NAVIT
107 #define MY_MAXSIZE ((gsize)-1)
108
109 static inline gsize
110 nearest_power (gsize base, gsize num)
111 {
112 if (num > MY_MAXSIZE / 2)
113 {
114 return MY_MAXSIZE;
115 }
116 else
117 {
118 gsize n = base;
119
120 while (n < num)
121 n <<= 1;
122
123 return n;
124 }
125 }
126
127 /* String Chunks.
128 */
129
130 /**
131 * g_string_chunk_new:
132 * @size: the default size of the blocks of memory which are
133 * allocated to store the strings. If a particular string
134 * is larger than this default size, a larger block of
135 * memory will be allocated for it.
136 *
137 * Creates a new #GStringChunk.
138 *
139 * Returns: a new #GStringChunk
140 */
141 GStringChunk*
142 g_string_chunk_new (gsize size)
143 {
144 GStringChunk *new_chunk = g_new (GStringChunk, 1);
145 gsize actual_size = 1;
146
147 actual_size = nearest_power (1, size);
148
149 new_chunk->const_table = NULL;
150 new_chunk->storage_list = NULL;
151 new_chunk->storage_next = actual_size;
152 new_chunk->default_size = actual_size;
153 new_chunk->this_size = actual_size;
154
155 return new_chunk;
156 }
157
158 /**
159 * g_string_chunk_free:
160 * @chunk: a #GStringChunk
161 *
162 * Frees all memory allocated by the #GStringChunk.
163 * After calling g_string_chunk_free() it is not safe to
164 * access any of the strings which were contained within it.
165 */
166 void
167 g_string_chunk_free (GStringChunk *chunk)
168 {
169 GSList *tmp_list;
170
171 g_return_if_fail (chunk != NULL);
172
173 if (chunk->storage_list)
174 {
175 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
176 g_free (tmp_list->data);
177
178 g_slist_free (chunk->storage_list);
179 }
180
181 if (chunk->const_table)
182 g_hash_table_destroy (chunk->const_table);
183
184 g_free (chunk);
185 }
186
187 /**
188 * g_string_chunk_clear:
189 * @chunk: a #GStringChunk
190 *
191 * Frees all strings contained within the #GStringChunk.
192 * After calling g_string_chunk_clear() it is not safe to
193 * access any of the strings which were contained within it.
194 *
195 * Since: 2.14
196 */
197 void
198 g_string_chunk_clear (GStringChunk *chunk)
199 {
200 GSList *tmp_list;
201
202 g_return_if_fail (chunk != NULL);
203
204 if (chunk->storage_list)
205 {
206 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
207 g_free (tmp_list->data);
208
209 g_slist_free (chunk->storage_list);
210
211 chunk->storage_list = NULL;
212 chunk->storage_next = chunk->default_size;
213 chunk->this_size = chunk->default_size;
214 }
215
216 if (chunk->const_table)
217 g_hash_table_remove_all (chunk->const_table);
218 }
219
220 /**
221 * g_string_chunk_insert:
222 * @chunk: a #GStringChunk
223 * @string: the string to add
224 *
225 * Adds a copy of @string to the #GStringChunk.
226 * It returns a pointer to the new copy of the string
227 * in the #GStringChunk. The characters in the string
228 * can be changed, if necessary, though you should not
229 * change anything after the end of the string.
230 *
231 * Unlike g_string_chunk_insert_const(), this function
232 * does not check for duplicates. Also strings added
233 * with g_string_chunk_insert() will not be searched
234 * by g_string_chunk_insert_const() when looking for
235 * duplicates.
236 *
237 * Returns: a pointer to the copy of @string within
238 * the #GStringChunk
239 */
240 gchar*
241 g_string_chunk_insert (GStringChunk *chunk,
242 const gchar *string)
243 {
244 g_return_val_if_fail (chunk != NULL, NULL);
245
246 return g_string_chunk_insert_len (chunk, string, -1);
247 }
248
249 /**
250 * g_string_chunk_insert_const:
251 * @chunk: a #GStringChunk
252 * @string: the string to add
253 *
254 * Adds a copy of @string to the #GStringChunk, unless the same
255 * string has already been added to the #GStringChunk with
256 * g_string_chunk_insert_const().
257 *
258 * This function is useful if you need to copy a large number
259 * of strings but do not want to waste space storing duplicates.
260 * But you must remember that there may be several pointers to
261 * the same string, and so any changes made to the strings
262 * should be done very carefully.
263 *
264 * Note that g_string_chunk_insert_const() will not return a
265 * pointer to a string added with g_string_chunk_insert(), even
266 * if they do match.
267 *
268 * Returns: a pointer to the new or existing copy of @string
269 * within the #GStringChunk
270 */
271 gchar*
272 g_string_chunk_insert_const (GStringChunk *chunk,
273 const gchar *string)
274 {
275 char* lookup;
276
277 g_return_val_if_fail (chunk != NULL, NULL);
278
279 if (!chunk->const_table)
280 chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
281
282 lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
283
284 if (!lookup)
285 {
286 lookup = g_string_chunk_insert (chunk, string);
287 g_hash_table_insert (chunk->const_table, lookup, lookup);
288 }
289
290 return lookup;
291 }
292
293 /**
294 * g_string_chunk_insert_len:
295 * @chunk: a #GStringChunk
296 * @string: bytes to insert
297 * @len: number of bytes of @string to insert, or -1 to insert a
298 * nul-terminated string
299 *
300 * Adds a copy of the first @len bytes of @string to the #GStringChunk.
301 * The copy is nul-terminated.
302 *
303 * Since this function does not stop at nul bytes, it is the caller's
304 * responsibility to ensure that @string has at least @len addressable
305 * bytes.
306 *
307 * The characters in the returned string can be changed, if necessary,
308 * though you should not change anything after the end of the string.
309 *
310 * Return value: a pointer to the copy of @string within the #GStringChunk
311 *
312 * Since: 2.4
313 **/
314 gchar*
315 g_string_chunk_insert_len (GStringChunk *chunk,
316 const gchar *string,
317 gssize len)
318 {
319 gssize size;
320 gchar* pos;
321
322 g_return_val_if_fail (chunk != NULL, NULL);
323
324 if (len < 0)
325 size = strlen (string);
326 else
327 size = len;
328
329 if ((chunk->storage_next + size + 1) > chunk->this_size)
330 {
331 gsize new_size = nearest_power (chunk->default_size, size + 1);
332
333 chunk->storage_list = g_slist_prepend (chunk->storage_list,
334 g_new (gchar, new_size));
335
336 chunk->this_size = new_size;
337 chunk->storage_next = 0;
338 }
339
340 pos = ((gchar *) chunk->storage_list->data) + chunk->storage_next;
341
342 *(pos + size) = '\0';
343
344 strncpy (pos, string, size);
345 if (len > 0)
346 size = strlen (pos);
347
348 chunk->storage_next += size + 1;
349
350 return pos;
351 }
352
353 /* Strings.
354 */
355 static void
356 g_string_maybe_expand (GString* string,
357 gsize len)
358 {
359 if (string->len + len >= string->allocated_len)
360 {
361 string->allocated_len = nearest_power (1, string->len + len + 1);
362 string->str = g_realloc (string->str, string->allocated_len);
363 }
364 }
365
366 /**
367 * g_string_sized_new:
368 * @dfl_size: the default size of the space allocated to
369 * hold the string
370 *
371 * Creates a new #GString, with enough space for @dfl_size
372 * bytes. This is useful if you are going to add a lot of
373 * text to the string and don't want it to be reallocated
374 * too often.
375 *
376 * Returns: the new #GString
377 */
378 GString*
379 g_string_sized_new (gsize dfl_size)
380 {
381 GString *string = g_slice_new (GString);
382
383 string->allocated_len = 0;
384 string->len = 0;
385 string->str = NULL;
386
387 g_string_maybe_expand (string, MAX (dfl_size, 2));
388 string->str[0] = 0;
389
390 return string;
391 }
392
393 /**
394 * g_string_new:
395 * @init: the initial text to copy into the string
396 *
397 * Creates a new #GString, initialized with the given string.
398 *
399 * Returns: the new #GString
400 */
401 GString*
402 g_string_new (const gchar *init)
403 {
404 GString *string;
405
406 if (init == NULL || *init == '\0')
407 string = g_string_sized_new (2);
408 else
409 {
410 gint len;
411
412 len = strlen (init);
413 string = g_string_sized_new (len + 2);
414
415 g_string_append_len (string, init, len);
416 }
417
418 return string;
419 }
420
421 /**
422 * g_string_new_len:
423 * @init: initial contents of the string
424 * @len: length of @init to use
425 *
426 * Creates a new #GString with @len bytes of the @init buffer.
427 * Because a length is provided, @init need not be nul-terminated,
428 * and can contain embedded nul bytes.
429 *
430 * Since this function does not stop at nul bytes, it is the caller's
431 * responsibility to ensure that @init has at least @len addressable
432 * bytes.
433 *
434 * Returns: a new #GString
435 */
436 GString*
437 g_string_new_len (const gchar *init,
438 gssize len)
439 {
440 GString *string;
441
442 if (len < 0)
443 return g_string_new (init);
444 else
445 {
446 string = g_string_sized_new (len);
447
448 if (init)
449 g_string_append_len (string, init, len);
450
451 return string;
452 }
453 }
454
455 /**
456 * g_string_free:
457 * @string: a #GString
458 * @free_segment: if %TRUE the actual character data is freed as well
459 *
460 * Frees the memory allocated for the #GString.
461 * If @free_segment is %TRUE it also frees the character data.
462 *
463 * Returns: the character data of @string
464 * (i.e. %NULL if @free_segment is %TRUE)
465 */
466 gchar*
467 g_string_free (GString *string,
468 gboolean free_segment)
469 {
470 gchar *segment;
471
472 g_return_val_if_fail (string != NULL, NULL);
473
474 if (free_segment)
475 {
476 g_free (string->str);
477 segment = NULL;
478 }
479 else
480 segment = string->str;
481
482 g_slice_free (GString, string);
483
484 return segment;
485 }
486
487 /**
488 * g_string_equal:
489 * @v: a #GString
490 * @v2: another #GString
491 *
492 * Compares two strings for equality, returning %TRUE if they are equal.
493 * For use with #GHashTable.
494 *
495 * Returns: %TRUE if they strings are the same length and contain the
496 * same bytes
497 */
498 gboolean
499 g_string_equal (const GString *v,
500 const GString *v2)
501 {
502 gchar *p, *q;
503 GString *string1 = (GString *) v;
504 GString *string2 = (GString *) v2;
505 gsize i = string1->len;
506
507 if (i != string2->len)
508 return FALSE;
509
510 p = string1->str;
511 q = string2->str;
512 while (i)
513 {
514 if (*p != *q)
515 return FALSE;
516 p++;
517 q++;
518 i--;
519 }
520 return TRUE;
521 }
522
523 /**
524 * g_string_hash:
525 * @str: a string to hash
526 *
527 * Creates a hash code for @str; for use with #GHashTable.
528 *
529 * Returns: hash code for @str
530 */
531 /* 31 bit hash function */
532 guint
533 g_string_hash (const GString *str)
534 {
535 const gchar *p = str->str;
536 gsize n = str->len;
537 guint h = 0;
538
539 while (n--)
540 {
541 h = (h << 5) - h + *p;
542 p++;
543 }
544
545 return h;
546 }
547
548 /**
549 * g_string_assign:
550 * @string: the destination #GString. Its current contents
551 * are destroyed.
552 * @rval: the string to copy into @string
553 *
554 * Copies the bytes from a string into a #GString,
555 * destroying any previous contents. It is rather like
556 * the standard strcpy() function, except that you do not
557 * have to worry about having enough space to copy the string.
558 *
559 * Returns: @string
560 */
561 GString*
562 g_string_assign (GString *string,
563 const gchar *rval)
564 {
565 g_return_val_if_fail (string != NULL, NULL);
566 g_return_val_if_fail (rval != NULL, string);
567
568 /* Make sure assigning to itself doesn't corrupt the string. */
569 if (string->str != rval)
570 {
571 /* Assigning from substring should be ok since g_string_truncate
572 does not realloc. */
573 g_string_truncate (string, 0);
574 g_string_append (string, rval);
575 }
576
577 return string;
578 }
579
580 /**
581 * g_string_truncate:
582 * @string: a #GString
583 * @len: the new size of @string
584 *
585 * Cuts off the end of the GString, leaving the first @len bytes.
586 *
587 * Returns: @string
588 */
589 GString*
590 g_string_truncate (GString *string,
591 gsize len)
592 {
593 g_return_val_if_fail (string != NULL, NULL);
594
595 string->len = MIN (len, string->len);
596 string->str[string->len] = 0;
597
598 return string;
599 }
600
601 /**
602 * g_string_set_size:
603 * @string: a #GString
604 * @len: the new length
605 *
606 * Sets the length of a #GString. If the length is less than
607 * the current length, the string will be truncated. If the
608 * length is greater than the current length, the contents
609 * of the newly added area are undefined. (However, as
610 * always, string->str[string->len] will be a nul byte.)
611 *
612 * Return value: @string
613 **/
614 GString*
615 g_string_set_size (GString *string,
616 gsize len)
617 {
618 g_return_val_if_fail (string != NULL, NULL);
619
620 if (len >= string->allocated_len)
621 g_string_maybe_expand (string, len - string->len);
622
623 string->len = len;
624 string->str[len] = 0;
625
626 return string;
627 }
628
629 /**
630 * g_string_insert_len:
631 * @string: a #GString
632 * @pos: position in @string where insertion should
633 * happen, or -1 for at the end
634 * @val: bytes to insert
635 * @len: number of bytes of @val to insert
636 *
637 * Inserts @len bytes of @val into @string at @pos.
638 * Because @len is provided, @val may contain embedded
639 * nuls and need not be nul-terminated. If @pos is -1,
640 * bytes are inserted at the end of the string.
641 *
642 * Since this function does not stop at nul bytes, it is
643 * the caller's responsibility to ensure that @val has at
644 * least @len addressable bytes.
645 *
646 * Returns: @string
647 */
648 GString*
649 g_string_insert_len (GString *string,
650 gssize pos,
651 const gchar *val,
652 gssize len)
653 {
654 g_return_val_if_fail (string != NULL, NULL);
655 g_return_val_if_fail (val != NULL, string);
656
657 if (len < 0)
658 len = strlen (val);
659
660 if (pos < 0)
661 pos = string->len;
662 else
663 g_return_val_if_fail (pos <= string->len, string);
664
665 /* Check whether val represents a substring of string. This test
666 probably violates chapter and verse of the C standards, since
667 ">=" and "<=" are only valid when val really is a substring.
668 In practice, it will work on modern archs. */
669 if (val >= string->str && val <= string->str + string->len)
670 {
671 gsize offset = val - string->str;
672 gsize precount = 0;
673
674 g_string_maybe_expand (string, len);
675 val = string->str + offset;
676 /* At this point, val is valid again. */
677
678 /* Open up space where we are going to insert. */
679 if (pos < string->len)
680 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
681
682 /* Move the source part before the gap, if any. */
683 if (offset < pos)
684 {
685 precount = MIN (len, pos - offset);
686 memcpy (string->str + pos, val, precount);
687 }
688
689 /* Move the source part after the gap, if any. */
690 if (len > precount)
691 memcpy (string->str + pos + precount,
692 val + /* Already moved: */ precount + /* Space opened up: */ len,
693 len - precount);
694 }
695 else
696 {
697 g_string_maybe_expand (string, len);
698
699 /* If we aren't appending at the end, move a hunk
700 * of the old string to the end, opening up space
701 */
702 if (pos < string->len)
703 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
704
705 /* insert the new string */
706 if (len == 1)
707 string->str[pos] = *val;
708 else
709 memcpy (string->str + pos, val, len);
710 }
711
712 string->len += len;
713
714 string->str[string->len] = 0;
715
716 return string;
717 }
718
719 #define SUB_DELIM_CHARS "!$&'()*+,;="
720
721 static gboolean
722 is_valid (char c, const char *reserved_chars_allowed)
723 {
724 if (g_ascii_isalnum (c) ||
725 c == '-' ||
726 c == '.' ||
727 c == '_' ||
728 c == '~')
729 return TRUE;
730
731 if (reserved_chars_allowed &&
732 strchr (reserved_chars_allowed, c) != NULL)
733 return TRUE;
734
735 return FALSE;
736 }
737
738 static gboolean
739 gunichar_ok (gunichar c)
740 {
741 return
742 (c != (gunichar) -2) &&
743 (c != (gunichar) -1);
744 }
745
746 /**
747 * g_string_append_uri_escaped:
748 * @string: a #GString
749 * @unescaped: a string
750 * @reserved_chars_allowed: a string of reserved characters allowed to be used
751 * @allow_utf8: set %TRUE if the escaped string may include UTF8 characters
752 *
753 * Appends @unescaped to @string, escaped any characters that
754 * are reserved in URIs using URI-style escape sequences.
755 *
756 * Returns: @string
757 *
758 * Since: 2.16
759 **/
760 GString *
761 g_string_append_uri_escaped (GString *string,
762 const char *unescaped,
763 const char *reserved_chars_allowed,
764 gboolean allow_utf8)
765 {
766 unsigned char c;
767 const char *end;
768 static const gchar hex[16] = "0123456789ABCDEF";
769
770 g_return_val_if_fail (string != NULL, NULL);
771 g_return_val_if_fail (unescaped != NULL, NULL);
772
773 end = unescaped + strlen (unescaped);
774
775 while ((c = *unescaped) != 0)
776 {
777 if (c >= 0x80 && allow_utf8 &&
778 gunichar_ok (g_utf8_get_char_validated (unescaped, end - unescaped)))
779 {
780 int len = g_utf8_skip [c];
781 g_string_append_len (string, unescaped, len);
782 unescaped += len;
783 }
784 else if (is_valid (c, reserved_chars_allowed))
785 {
786 g_string_append_c (string, c);
787 unescaped++;
788 }
789 else
790 {
791 g_string_append_c (string, '%');
792 g_string_append_c (string, hex[((guchar)c) >> 4]);
793 g_string_append_c (string, hex[((guchar)c) & 0xf]);
794 unescaped++;
795 }
796 }
797
798 return string;
799 }
800
801 /**
802 * g_string_append:
803 * @string: a #GString
804 * @val: the string to append onto the end of @string
805 *
806 * Adds a string onto the end of a #GString, expanding
807 * it if necessary.
808 *
809 * Returns: @string
810 */
811 GString*
812 g_string_append (GString *string,
813 const gchar *val)
814 {
815 g_return_val_if_fail (string != NULL, NULL);
816 g_return_val_if_fail (val != NULL, string);
817
818 return g_string_insert_len (string, -1, val, -1);
819 }
820
821 /**
822 * g_string_append_len:
823 * @string: a #GString
824 * @val: bytes to append
825 * @len: number of bytes of @val to use
826 *
827 * Appends @len bytes of @val to @string. Because @len is
828 * provided, @val may contain embedded nuls and need not
829 * be nul-terminated.
830 *
831 * Since this function does not stop at nul bytes, it is
832 * the caller's responsibility to ensure that @val has at
833 * least @len addressable bytes.
834 *
835 * Returns: @string
836 */
837 GString*
838 g_string_append_len (GString *string,
839 const gchar *val,
840 gssize len)
841 {
842 g_return_val_if_fail (string != NULL, NULL);
843 g_return_val_if_fail (val != NULL, string);
844
845 return g_string_insert_len (string, -1, val, len);
846 }
847
848 /**
849 * g_string_append_c:
850 * @string: a #GString
851 * @c: the byte to append onto the end of @string
852 *
853 * Adds a byte onto the end of a #GString, expanding
854 * it if necessary.
855 *
856 * Returns: @string
857 */
858 #undef g_string_append_c
859 GString*
860 g_string_append_c (GString *string,
861 gchar c)
862 {
863 g_return_val_if_fail (string != NULL, NULL);
864
865 return g_string_insert_c (string, -1, c);
866 }
867
868 /**
869 * g_string_append_unichar:
870 * @string: a #GString
871 * @wc: a Unicode character
872 *
873 * Converts a Unicode character into UTF-8, and appends it
874 * to the string.
875 *
876 * Return value: @string
877 **/
878 GString*
879 g_string_append_unichar (GString *string,
880 gunichar wc)
881 {
882 g_return_val_if_fail (string != NULL, NULL);
883
884 return g_string_insert_unichar (string, -1, wc);
885 }
886
887 /**
888 * g_string_prepend:
889 * @string: a #GString
890 * @val: the string to prepend on the start of @string
891 *
892 * Adds a string on to the start of a #GString,
893 * expanding it if necessary.
894 *
895 * Returns: @string
896 */
897 GString*
898 g_string_prepend (GString *string,
899 const gchar *val)
900 {
901 g_return_val_if_fail (string != NULL, NULL);
902 g_return_val_if_fail (val != NULL, string);
903
904 return g_string_insert_len (string, 0, val, -1);
905 }
906
907 /**
908 * g_string_prepend_len:
909 * @string: a #GString
910 * @val: bytes to prepend
911 * @len: number of bytes in @val to prepend
912 *
913 * Prepends @len bytes of @val to @string.
914 * Because @len is provided, @val may contain
915 * embedded nuls and need not be nul-terminated.
916 *
917 * Since this function does not stop at nul bytes,
918 * it is the caller's responsibility to ensure that
919 * @val has at least @len addressable bytes.
920 *
921 * Returns: @string
922 */
923 GString*
924 g_string_prepend_len (GString *string,
925 const gchar *val,
926 gssize len)
927 {
928 g_return_val_if_fail (string != NULL, NULL);
929 g_return_val_if_fail (val != NULL, string);
930
931 return g_string_insert_len (string, 0, val, len);
932 }
933
934 /**
935 * g_string_prepend_c:
936 * @string: a #GString
937 * @c: the byte to prepend on the start of the #GString
938 *
939 * Adds a byte onto the start of a #GString,
940 * expanding it if necessary.
941 *
942 * Returns: @string
943 */
944 GString*
945 g_string_prepend_c (GString *string,
946 gchar c)
947 {
948 g_return_val_if_fail (string != NULL, NULL);
949
950 return g_string_insert_c (string, 0, c);
951 }
952
953 /**
954 * g_string_prepend_unichar:
955 * @string: a #GString
956 * @wc: a Unicode character
957 *
958 * Converts a Unicode character into UTF-8, and prepends it
959 * to the string.
960 *
961 * Return value: @string
962 **/
963 GString*
964 g_string_prepend_unichar (GString *string,
965 gunichar wc)
966 {
967 g_return_val_if_fail (string != NULL, NULL);
968
969 return g_string_insert_unichar (string, 0, wc);
970 }
971
972 /**
973 * g_string_insert:
974 * @string: a #GString
975 * @pos: the position to insert the copy of the string
976 * @val: the string to insert
977 *
978 * Inserts a copy of a string into a #GString,
979 * expanding it if necessary.
980 *
981 * Returns: @string
982 */
983 GString*
984 g_string_insert (GString *string,
985 gssize pos,
986 const gchar *val)
987 {
988 g_return_val_if_fail (string != NULL, NULL);
989 g_return_val_if_fail (val != NULL, string);
990 if (pos >= 0)
991 g_return_val_if_fail (pos <= string->len, string);
992
993 return g_string_insert_len (string, pos, val, -1);
994 }
995
996 /**
997 * g_string_insert_c:
998 * @string: a #GString
999 * @pos: the position to insert the byte
1000 * @c: the byte to insert
1001 *
1002 * Inserts a byte into a #GString, expanding it if necessary.
1003 *
1004 * Returns: @string
1005 */
1006 GString*
1007 g_string_insert_c (GString *string,
1008 gssize pos,
1009 gchar c)
1010 {
1011 g_return_val_if_fail (string != NULL, NULL);
1012
1013 g_string_maybe_expand (string, 1);
1014
1015 if (pos < 0)
1016 pos = string->len;
1017 else
1018 g_return_val_if_fail (pos <= string->len, string);
1019
1020 /* If not just an append, move the old stuff */
1021 if (pos < string->len)
1022 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
1023
1024 string->str[pos] = c;
1025
1026 string->len += 1;
1027
1028 string->str[string->len] = 0;
1029
1030 return string;
1031 }
1032
1033 /**
1034 * g_string_insert_unichar:
1035 * @string: a #GString
1036 * @pos: the position at which to insert character, or -1 to
1037 * append at the end of the string
1038 * @wc: a Unicode character
1039 *
1040 * Converts a Unicode character into UTF-8, and insert it
1041 * into the string at the given position.
1042 *
1043 * Return value: @string
1044 **/
1045 GString*
1046 g_string_insert_unichar (GString *string,
1047 gssize pos,
1048 gunichar wc)
1049 {
1050 gint charlen, first, i;
1051 gchar *dest;
1052
1053 g_return_val_if_fail (string != NULL, NULL);
1054
1055 /* Code copied from g_unichar_to_utf() */
1056 if (wc < 0x80)
1057 {
1058 first = 0;
1059 charlen = 1;
1060 }
1061 else if (wc < 0x800)
1062 {
1063 first = 0xc0;
1064 charlen = 2;
1065 }
1066 else if (wc < 0x10000)
1067 {
1068 first = 0xe0;
1069 charlen = 3;
1070 }
1071 else if (wc < 0x200000)
1072 {
1073 first = 0xf0;
1074 charlen = 4;
1075 }
1076 else if (wc < 0x4000000)
1077 {
1078 first = 0xf8;
1079 charlen = 5;
1080 }
1081 else
1082 {
1083 first = 0xfc;
1084 charlen = 6;
1085 }
1086 /* End of copied code */
1087
1088 g_string_maybe_expand (string, charlen);
1089
1090 if (pos < 0)
1091 pos = string->len;
1092 else
1093 g_return_val_if_fail (pos <= string->len, string);
1094
1095 /* If not just an append, move the old stuff */
1096 if (pos < string->len)
1097 g_memmove (string->str + pos + charlen, string->str + pos, string->len - pos);
1098
1099 dest = string->str + pos;
1100 /* Code copied from g_unichar_to_utf() */
1101 for (i = charlen - 1; i > 0; --i)
1102 {
1103 dest[i] = (wc & 0x3f) | 0x80;
1104 wc >>= 6;
1105 }
1106 dest[0] = wc | first;
1107 /* End of copied code */
1108
1109 string->len += charlen;
1110
1111 string->str[string->len] = 0;
1112
1113 return string;
1114 }
1115
1116 /**
1117 * g_string_overwrite:
1118 * @string: a #GString
1119 * @pos: the position at which to start overwriting
1120 * @val: the string that will overwrite the @string starting at @pos
1121 *
1122 * Overwrites part of a string, lengthening it if necessary.
1123 *
1124 * Return value: @string
1125 *
1126 * Since: 2.14
1127 **/
1128 GString *
1129 g_string_overwrite (GString *string,
1130 gsize pos,
1131 const gchar *val)
1132 {
1133 g_return_val_if_fail (val != NULL, string);
1134 return g_string_overwrite_len (string, pos, val, strlen (val));
1135 }
1136
1137 /**
1138 * g_string_overwrite_len:
1139 * @string: a #GString
1140 * @pos: the position at which to start overwriting
1141 * @val: the string that will overwrite the @string starting at @pos
1142 * @len: the number of bytes to write from @val
1143 *
1144 * Overwrites part of a string, lengthening it if necessary.
1145 * This function will work with embedded nuls.
1146 *
1147 * Return value: @string
1148 *
1149 * Since: 2.14
1150 **/
1151 GString *
1152 g_string_overwrite_len (GString *string,
1153 gsize pos,
1154 const gchar *val,
1155 gssize len)
1156 {
1157 gsize end;
1158
1159 g_return_val_if_fail (string != NULL, NULL);
1160
1161 if (!len)
1162 return string;
1163
1164 g_return_val_if_fail (val != NULL, string);
1165 g_return_val_if_fail (pos <= string->len, string);
1166
1167 if (len < 0)
1168 len = strlen (val);
1169
1170 end = pos + len;
1171
1172 if (end > string->len)
1173 g_string_maybe_expand (string, end - string->len);
1174
1175 memcpy (string->str + pos, val, len);
1176
1177 if (end > string->len)
1178 {
1179 string->str[end] = '\0';
1180 string->len = end;
1181 }
1182
1183 return string;
1184 }
1185
1186 /**
1187 * g_string_erase:
1188 * @string: a #GString
1189 * @pos: the position of the content to remove
1190 * @len: the number of bytes to remove, or -1 to remove all
1191 * following bytes
1192 *
1193 * Removes @len bytes from a #GString, starting at position @pos.
1194 * The rest of the #GString is shifted down to fill the gap.
1195 *
1196 * Returns: @string
1197 */
1198 GString*
1199 g_string_erase (GString *string,
1200 gssize pos,
1201 gssize len)
1202 {
1203 g_return_val_if_fail (string != NULL, NULL);
1204 g_return_val_if_fail (pos >= 0, string);
1205 g_return_val_if_fail (pos <= string->len, string);
1206
1207 if (len < 0)
1208 len = string->len - pos;
1209 else
1210 {
1211 g_return_val_if_fail (pos + len <= string->len, string);
1212
1213 if (pos + len < string->len)
1214 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
1215 }
1216
1217 string->len -= len;
1218
1219 string->str[string->len] = 0;
1220
1221 return string;
1222 }
1223
1224 /**
1225 * g_string_ascii_down:
1226 * @string: a GString
1227 *
1228 * Converts all upper case ASCII letters to lower case ASCII letters.
1229 *
1230 * Return value: passed-in @string pointer, with all the upper case
1231 * characters converted to lower case in place, with
1232 * semantics that exactly match g_ascii_tolower().
1233 **/
1234 GString*
1235 g_string_ascii_down (GString *string)
1236 {
1237 gchar *s;
1238 gint n;
1239
1240 g_return_val_if_fail (string != NULL, NULL);
1241
1242 n = string->len;
1243 s = string->str;
1244
1245 while (n)
1246 {
1247 *s = g_ascii_tolower (*s);
1248 s++;
1249 n--;
1250 }
1251
1252 return string;
1253 }
1254
1255 /**
1256 * g_string_ascii_up:
1257 * @string: a GString
1258 *
1259 * Converts all lower case ASCII letters to upper case ASCII letters.
1260 *
1261 * Return value: passed-in @string pointer, with all the lower case
1262 * characters converted to upper case in place, with
1263 * semantics that exactly match g_ascii_toupper().
1264 **/
1265 GString*
1266 g_string_ascii_up (GString *string)
1267 {
1268 gchar *s;
1269 gint n;
1270
1271 g_return_val_if_fail (string != NULL, NULL);
1272
1273 n = string->len;
1274 s = string->str;
1275
1276 while (n)
1277 {
1278 *s = g_ascii_toupper (*s);
1279 s++;
1280 n--;
1281 }
1282
1283 return string;
1284 }
1285
1286 /**
1287 * g_string_down:
1288 * @string: a #GString
1289 *
1290 * Converts a #GString to lowercase.
1291 *
1292 * Returns: the #GString.
1293 *
1294 * Deprecated:2.2: This function uses the locale-specific
1295 * tolower() function, which is almost never the right thing.
1296 * Use g_string_ascii_down() or g_utf8_strdown() instead.
1297 */
1298 GString*
1299 g_string_down (GString *string)
1300 {
1301 guchar *s;
1302 glong n;
1303
1304 g_return_val_if_fail (string != NULL, NULL);
1305
1306 n = string->len;
1307 s = (guchar *) string->str;
1308
1309 while (n)
1310 {
1311 if (isupper (*s))
1312 *s = tolower (*s);
1313 s++;
1314 n--;
1315 }
1316
1317 return string;
1318 }
1319
1320 /**
1321 * g_string_up:
1322 * @string: a #GString
1323 *
1324 * Converts a #GString to uppercase.
1325 *
1326 * Return value: @string
1327 *
1328 * Deprecated:2.2: This function uses the locale-specific
1329 * toupper() function, which is almost never the right thing.
1330 * Use g_string_ascii_up() or g_utf8_strup() instead.
1331 **/
1332 GString*
1333 g_string_up (GString *string)
1334 {
1335 guchar *s;
1336 glong n;
1337
1338 g_return_val_if_fail (string != NULL, NULL);
1339
1340 n = string->len;
1341 s = (guchar *) string->str;
1342
1343 while (n)
1344 {
1345 if (islower (*s))
1346 *s = toupper (*s);
1347 s++;
1348 n--;
1349 }
1350
1351 return string;
1352 }
1353
1354 /**
1355 * g_string_append_vprintf:
1356 * @string: a #GString
1357 * @format: the string format. See the printf() documentation
1358 * @args: the list of arguments to insert in the output
1359 *
1360 * Appends a formatted string onto the end of a #GString.
1361 * This function is similar to g_string_append_printf()
1362 * except that the arguments to the format string are passed
1363 * as a va_list.
1364 *
1365 * Since: 2.14
1366 */
1367 void
1368 g_string_append_vprintf (GString *string,
1369 const gchar *format,
1370 va_list args)
1371 {
1372 gchar *buf;
1373 gint len;
1374
1375 g_return_if_fail (string != NULL);
1376 g_return_if_fail (format != NULL);
1377
1378 len = g_vasprintf (&buf, format, args);
1379
1380 if (len >= 0)
1381 {
1382 g_string_maybe_expand (string, len);
1383 memcpy (string->str + string->len, buf, len + 1);
1384 string->len += len;
1385 g_free (buf);
1386 }
1387 }
1388
1389 /**
1390 * g_string_vprintf:
1391 * @string: a #GString
1392 * @format: the string format. See the printf() documentation
1393 * @args: the parameters to insert into the format string
1394 *
1395 * Writes a formatted string into a #GString.
1396 * This function is similar to g_string_printf() except that
1397 * the arguments to the format string are passed as a va_list.
1398 *
1399 * Since: 2.14
1400 */
1401 void
1402 g_string_vprintf (GString *string,
1403 const gchar *format,
1404 va_list args)
1405 {
1406 g_string_truncate (string, 0);
1407 g_string_append_vprintf (string, format, args);
1408 }
1409
1410 /**
1411 * g_string_sprintf:
1412 * @string: a #GString
1413 * @format: the string format. See the sprintf() documentation
1414 * @Varargs: the parameters to insert into the format string
1415 *
1416 * Writes a formatted string into a #GString.
1417 * This is similar to the standard sprintf() function,
1418 * except that the #GString buffer automatically expands
1419 * to contain the results. The previous contents of the
1420 * #GString are destroyed.
1421 *
1422 * Deprecated: This function has been renamed to g_string_printf().
1423 */
1424
1425 /**
1426 * g_string_printf:
1427 * @string: a #GString
1428 * @format: the string format. See the printf() documentation
1429 * @Varargs: the parameters to insert into the format string
1430 *
1431 * Writes a formatted string into a #GString.
1432 * This is similar to the standard sprintf() function,
1433 * except that the #GString buffer automatically expands
1434 * to contain the results. The previous contents of the
1435 * #GString are destroyed.
1436 */
1437 void
1438 g_string_printf (GString *string,
1439 const gchar *format,
1440 ...)
1441 {
1442 va_list args;
1443
1444 g_string_truncate (string, 0);
1445
1446 va_start (args, format);
1447 g_string_append_vprintf (string, format, args);
1448 va_end (args);
1449 }
1450
1451 /**
1452 * g_string_sprintfa:
1453 * @string: a #GString
1454 * @format: the string format. See the sprintf() documentation
1455 * @Varargs: the parameters to insert into the format string
1456 *
1457 * Appends a formatted string onto the end of a #GString.
1458 * This function is similar to g_string_sprintf() except that
1459 * the text is appended to the #GString.
1460 *
1461 * Deprecated: This function has been renamed to g_string_append_printf()
1462 */
1463
1464 /**
1465 * g_string_append_printf:
1466 * @string: a #GString
1467 * @format: the string format. See the printf() documentation
1468 * @Varargs: the parameters to insert into the format string
1469 *
1470 * Appends a formatted string onto the end of a #GString.
1471 * This function is similar to g_string_printf() except
1472 * that the text is appended to the #GString.
1473 */
1474 void
1475 g_string_append_printf (GString *string,
1476 const gchar *format,
1477 ...)
1478 {
1479 va_list args;
1480
1481 va_start (args, format);
1482 g_string_append_vprintf (string, format, args);
1483 va_end (args);
1484 }
1485
1486 #endif /* NOT_NEEDED_FOR_NAVIT */
1487 #define __G_STRING_C__
1488 #include "galiasdef.c"

   
Visit the ZANavi Wiki