GString: cosmetic cleanups
[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 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
28 #error "Only <glib.h> can be included directly."
29 #endif
30
31 #ifndef __G_STRING_H__
32 #define __G_STRING_H__
33
34 #include <glib/gtypes.h>
35 #include <glib/gunicode.h>
36 #include <glib/gutils.h>  /* for G_CAN_INLINE */
37
38 G_BEGIN_DECLS
39
40 typedef struct _GString         GString;
41 typedef struct _GStringChunk    GStringChunk;
42
43 /**
44  * GString:
45  * @str: points to the character data. It may move as text is added.
46  *   The @str field is null-terminated and so
47  *   can be used as an ordinary C string.
48  * @len: contains the length of the string, not including the
49  *   terminating nul byte.
50  * @allocated_len: the number of bytes that can be stored in the
51  *   string before it needs to be reallocated. May be larger than @len.
52  *
53  * The #GString struct contains the public fields of a #GString.
54  */
55 struct _GString
56 {
57   gchar  *str;
58   gsize len;
59   gsize allocated_len;
60 };
61
62 /* String Chunks
63  */
64 GStringChunk* g_string_chunk_new           (gsize size);
65 void          g_string_chunk_free          (GStringChunk *chunk);
66 void          g_string_chunk_clear         (GStringChunk *chunk);
67 gchar*        g_string_chunk_insert        (GStringChunk *chunk,
68                                             const gchar  *string);
69 gchar*        g_string_chunk_insert_len    (GStringChunk *chunk,
70                                             const gchar  *string,
71                                             gssize        len);
72 gchar*        g_string_chunk_insert_const  (GStringChunk *chunk,
73                                             const gchar  *string);
74
75
76 /* Strings
77  */
78 GString*     g_string_new               (const gchar     *init);
79 GString*     g_string_new_len           (const gchar     *init,
80                                          gssize           len);
81 GString*     g_string_sized_new         (gsize            dfl_size);
82 gchar*       g_string_free              (GString         *string,
83                                          gboolean         free_segment);
84 gboolean     g_string_equal             (const GString   *v,
85                                          const GString   *v2);
86 guint        g_string_hash              (const GString   *str);
87 GString*     g_string_assign            (GString         *string,
88                                          const gchar     *rval);
89 GString*     g_string_truncate          (GString         *string,
90                                          gsize            len);
91 GString*     g_string_set_size          (GString         *string,
92                                          gsize            len);
93 GString*     g_string_insert_len        (GString         *string,
94                                          gssize           pos,
95                                          const gchar     *val,
96                                          gssize           len);
97 GString*     g_string_append            (GString         *string,
98                                          const gchar     *val);
99 GString*     g_string_append_len        (GString         *string,
100                                          const gchar     *val,
101                                          gssize           len);
102 GString*     g_string_append_c          (GString         *string,
103                                          gchar            c);
104 GString*     g_string_append_unichar    (GString         *string,
105                                          gunichar         wc);
106 GString*     g_string_prepend           (GString         *string,
107                                          const gchar     *val);
108 GString*     g_string_prepend_c         (GString         *string,
109                                          gchar            c);
110 GString*     g_string_prepend_unichar   (GString         *string,
111                                          gunichar         wc);
112 GString*     g_string_prepend_len       (GString         *string,
113                                          const gchar     *val,
114                                          gssize           len);
115 GString*     g_string_insert            (GString         *string,
116                                          gssize           pos,
117                                          const gchar     *val);
118 GString*     g_string_insert_c          (GString         *string,
119                                          gssize           pos,
120                                          gchar            c);
121 GString*     g_string_insert_unichar    (GString         *string,
122                                          gssize           pos,
123                                          gunichar         wc);
124 GString*     g_string_overwrite         (GString         *string,
125                                          gsize            pos,
126                                          const gchar     *val);
127 GString*     g_string_overwrite_len     (GString         *string,
128                                          gsize            pos,
129                                          const gchar     *val,
130                                          gssize           len);
131 GString*     g_string_erase             (GString         *string,
132                                          gssize           pos,
133                                          gssize           len);
134 GString*     g_string_ascii_down        (GString         *string);
135 GString*     g_string_ascii_up          (GString         *string);
136 void         g_string_vprintf           (GString         *string,
137                                          const gchar     *format,
138                                          va_list          args);
139 void         g_string_printf            (GString         *string,
140                                          const gchar     *format,
141                                          ...) G_GNUC_PRINTF (2, 3);
142 void         g_string_append_vprintf    (GString         *string,
143                                          const gchar     *format,
144                                          va_list          args);
145 void         g_string_append_printf     (GString         *string,
146                                          const gchar     *format,
147                                          ...) G_GNUC_PRINTF (2, 3);
148 GString*     g_string_append_uri_escaped (GString         *string,
149                                           const gchar     *unescaped,
150                                           const gchar     *reserved_chars_allowed,
151                                           gboolean         allow_utf8);
152
153 /* -- optimize g_strig_append_c --- */
154 #ifdef G_CAN_INLINE
155 static inline GString*
156 g_string_append_c_inline (GString *gstring,
157                           gchar    c)
158 {
159   if (gstring->len + 1 < gstring->allocated_len)
160     {
161       gstring->str[gstring->len++] = c;
162       gstring->str[gstring->len] = 0;
163     }
164   else
165     g_string_insert_c (gstring, -1, c);
166   return gstring;
167 }
168 #define g_string_append_c(gstr,c)       g_string_append_c_inline (gstr, c)
169 #endif /* G_CAN_INLINE */
170
171
172 #ifndef G_DISABLE_DEPRECATED
173
174 GString* g_string_down (GString *string);
175 GString* g_string_up   (GString *string);
176
177 #define  g_string_sprintf  g_string_printf
178 #define  g_string_sprintfa g_string_append_printf
179
180 #endif /* G_DISABLE_DEPRECATED */
181
182 G_END_DECLS
183
184 #endif /* __G_STRING_H__ */