Properly store changes for arrays. (#308528, Roger Leigh)
[platform/upstream/glib.git] / glib / gstring.h
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 #ifndef __G_STRING_H__
28 #define __G_STRING_H__
29
30 #include <glib/gtypes.h>
31 #include <glib/gunicode.h>
32 #include <glib/gutils.h>  /* for G_CAN_INLINE */
33
34 G_BEGIN_DECLS
35
36 typedef struct _GString         GString;
37 typedef struct _GStringChunk    GStringChunk;
38
39 struct _GString
40 {
41   gchar  *str;
42   gsize len;    
43   gsize allocated_len;
44 };
45
46 /* String Chunks
47  */
48 GStringChunk* g_string_chunk_new           (gsize size);  
49 void          g_string_chunk_free          (GStringChunk *chunk);
50 gchar*        g_string_chunk_insert        (GStringChunk *chunk,
51                                             const gchar  *string);
52 gchar*        g_string_chunk_insert_len    (GStringChunk *chunk,
53                                             const gchar  *string,
54                                             gssize        len);
55 gchar*        g_string_chunk_insert_const  (GStringChunk *chunk,
56                                             const gchar  *string);
57
58
59 /* Strings
60  */
61 GString*     g_string_new               (const gchar     *init);
62 GString*     g_string_new_len           (const gchar     *init,
63                                          gssize           len);   
64 GString*     g_string_sized_new         (gsize            dfl_size);  
65 gchar*       g_string_free              (GString         *string,
66                                          gboolean         free_segment);
67 gboolean     g_string_equal             (const GString   *v,
68                                          const GString   *v2);
69 guint        g_string_hash              (const GString   *str);
70 GString*     g_string_assign            (GString         *string,
71                                          const gchar     *rval);
72 GString*     g_string_truncate          (GString         *string,
73                                          gsize            len);    
74 GString*     g_string_set_size          (GString         *string,
75                                          gsize            len);
76 GString*     g_string_insert_len        (GString         *string,
77                                          gssize           pos,   
78                                          const gchar     *val,
79                                          gssize           len);  
80 GString*     g_string_append            (GString         *string,
81                                          const gchar     *val);
82 GString*     g_string_append_len        (GString         *string,
83                                          const gchar     *val,
84                                          gssize           len);  
85 GString*     g_string_append_c          (GString         *string,
86                                          gchar            c);
87 GString*     g_string_append_unichar    (GString         *string,
88                                          gunichar         wc);
89 GString*     g_string_prepend           (GString         *string,
90                                          const gchar     *val);
91 GString*     g_string_prepend_c         (GString         *string,
92                                          gchar            c);
93 GString*     g_string_prepend_unichar   (GString         *string,
94                                          gunichar         wc);
95 GString*     g_string_prepend_len       (GString         *string,
96                                          const gchar     *val,
97                                          gssize           len);  
98 GString*     g_string_insert            (GString         *string,
99                                          gssize           pos,    
100                                          const gchar     *val);
101 GString*     g_string_insert_c          (GString         *string,
102                                          gssize           pos,    
103                                          gchar            c);
104 GString*     g_string_insert_unichar    (GString         *string,
105                                          gssize           pos,    
106                                          gunichar         wc);
107 GString*     g_string_erase             (GString         *string,
108                                          gssize           pos,
109                                          gssize           len);
110 GString*     g_string_ascii_down        (GString         *string);
111 GString*     g_string_ascii_up          (GString         *string);
112 void         g_string_printf            (GString         *string,
113                                          const gchar     *format,
114                                          ...) G_GNUC_PRINTF (2, 3);
115 void         g_string_append_printf     (GString         *string,
116                                          const gchar     *format,
117                                          ...) G_GNUC_PRINTF (2, 3);
118
119 /* -- optimize g_strig_append_c --- */
120 #ifdef G_CAN_INLINE
121 static inline GString*
122 g_string_append_c_inline (GString *gstring,
123                           gchar    c)
124 {
125   if (gstring->len + 1 < gstring->allocated_len)
126     {
127       gstring->str[gstring->len++] = c;
128       gstring->str[gstring->len] = 0;
129     }
130   else
131     g_string_insert_c (gstring, -1, c);
132   return gstring;
133 }
134 #define g_string_append_c(gstr,c)       g_string_append_c_inline (gstr, c)
135 #endif /* G_CAN_INLINE */
136
137
138 #ifndef G_DISABLE_DEPRECATED
139
140 /* The following two functions are deprecated and will be removed in
141  * the next major release. They use the locale-specific tolower and
142  * toupper, which is almost never the right thing.
143  */
144
145 GString*     g_string_down              (GString         *string);
146 GString*     g_string_up                (GString         *string);
147
148 /* These aliases are included for compatibility. */
149 #define g_string_sprintf        g_string_printf
150 #define g_string_sprintfa       g_string_append_printf
151
152 #endif /* G_DISABLE_DEPRECATED */
153
154 G_END_DECLS
155
156 #endif /* __G_STRING_H__ */
157