1/* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
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#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
31#error "Only <glib.h> can be included directly."
32#endif
33
34#include <glib/gtypes.h>
35#include <glib/gunicode.h>
36#include <glib/gbytes.h>
37#include <glib/gstrfuncs.h>
38#include <glib/gutils.h> /* for G_CAN_INLINE */
39#include <string.h>
40
41G_BEGIN_DECLS
42
43typedef struct _GString GString;
44
45struct _GString
46{
47 gchar *str;
48 gsize len;
49 gsize allocated_len;
50};
51
52GLIB_AVAILABLE_IN_ALL
53GString* g_string_new (const gchar *init);
54GLIB_AVAILABLE_IN_2_78
55GString* g_string_new_take (gchar *init);
56GLIB_AVAILABLE_IN_ALL
57GString* g_string_new_len (const gchar *init,
58 gssize len);
59GLIB_AVAILABLE_IN_ALL
60GString* g_string_sized_new (gsize dfl_size);
61GLIB_AVAILABLE_IN_2_86
62GString *g_string_copy (GString *string);
63GLIB_AVAILABLE_IN_ALL
64gchar* (g_string_free) (GString *string,
65 gboolean free_segment);
66GLIB_AVAILABLE_IN_2_90
67void (g_string_free_deep) (GString *string);
68GLIB_AVAILABLE_IN_2_76
69gchar* g_string_free_and_steal (GString *string) G_GNUC_WARN_UNUSED_RESULT;
70
71#if G_GNUC_CHECK_VERSION (2, 0) && (GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_76)
72
73#if !defined(__cplusplus) || !G_GNUC_CHECK_VERSION (6, 1) || G_GNUC_CHECK_VERSION (7, 3)
74
75#define g_string_free(str, free_segment) \
76 (__builtin_constant_p (free_segment) ? \
77 ((free_segment) ? \
78 (g_string_free) ((str), (free_segment)) : \
79 g_string_free_and_steal (str)) \
80 : \
81 (g_string_free) ((str), (free_segment)))
82
83#endif /* !defined(__cplusplus) || !G_GNUC_CHECK_VERSION (6, 1) || G_GNUC_CHECK_VERSION (7, 3) */
84
85#endif /* G_GNUC_CHECK_VERSION (2, 0) && (GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_76) */
86
87GLIB_AVAILABLE_IN_2_34
88GBytes* g_string_free_to_bytes (GString *string);
89GLIB_AVAILABLE_IN_ALL
90gboolean g_string_equal (const GString *v,
91 const GString *v2);
92GLIB_AVAILABLE_IN_ALL
93guint g_string_hash (const GString *str);
94GLIB_AVAILABLE_IN_ALL
95GString* g_string_assign (GString *string,
96 const gchar *rval);
97GLIB_AVAILABLE_IN_ALL
98GString* g_string_truncate (GString *string,
99 gsize len);
100GLIB_AVAILABLE_IN_ALL
101GString* g_string_set_size (GString *string,
102 gsize len);
103GLIB_AVAILABLE_IN_ALL
104GString* g_string_insert_len (GString *string,
105 gssize pos,
106 const gchar *val,
107 gssize len);
108GLIB_AVAILABLE_IN_ALL
109GString* g_string_append (GString *string,
110 const gchar *val);
111GLIB_AVAILABLE_IN_ALL
112GString* g_string_append_len (GString *string,
113 const gchar *val,
114 gssize len);
115GLIB_AVAILABLE_IN_ALL
116GString* g_string_append_c (GString *string,
117 gchar c);
118GLIB_AVAILABLE_IN_ALL
119GString* g_string_append_unichar (GString *string,
120 gunichar wc);
121GLIB_AVAILABLE_IN_ALL
122GString* g_string_prepend (GString *string,
123 const gchar *val);
124GLIB_AVAILABLE_IN_ALL
125GString* g_string_prepend_c (GString *string,
126 gchar c);
127GLIB_AVAILABLE_IN_ALL
128GString* g_string_prepend_unichar (GString *string,
129 gunichar wc);
130GLIB_AVAILABLE_IN_ALL
131GString* g_string_prepend_len (GString *string,
132 const gchar *val,
133 gssize len);
134GLIB_AVAILABLE_IN_ALL
135GString* g_string_insert (GString *string,
136 gssize pos,
137 const gchar *val);
138GLIB_AVAILABLE_IN_ALL
139GString* g_string_insert_c (GString *string,
140 gssize pos,
141 gchar c);
142GLIB_AVAILABLE_IN_ALL
143GString* g_string_insert_unichar (GString *string,
144 gssize pos,
145 gunichar wc);
146GLIB_AVAILABLE_IN_ALL
147GString* g_string_overwrite (GString *string,
148 gsize pos,
149 const gchar *val);
150GLIB_AVAILABLE_IN_ALL
151GString* g_string_overwrite_len (GString *string,
152 gsize pos,
153 const gchar *val,
154 gssize len);
155GLIB_AVAILABLE_IN_ALL
156GString* g_string_erase (GString *string,
157 gssize pos,
158 gssize len);
159GLIB_AVAILABLE_IN_2_68
160guint g_string_replace (GString *string,
161 const gchar *find,
162 const gchar *replace,
163 guint limit);
164GLIB_AVAILABLE_IN_ALL
165GString* g_string_ascii_down (GString *string);
166GLIB_AVAILABLE_IN_ALL
167GString* g_string_ascii_up (GString *string);
168GLIB_AVAILABLE_IN_ALL
169void g_string_vprintf (GString *string,
170 const gchar *format,
171 va_list args)
172 G_GNUC_PRINTF(2, 0);
173GLIB_AVAILABLE_IN_ALL
174void g_string_printf (GString *string,
175 const gchar *format,
176 ...) G_GNUC_PRINTF (2, 3);
177GLIB_AVAILABLE_IN_ALL
178void g_string_append_vprintf (GString *string,
179 const gchar *format,
180 va_list args)
181 G_GNUC_PRINTF(2, 0);
182GLIB_AVAILABLE_IN_ALL
183void g_string_append_printf (GString *string,
184 const gchar *format,
185 ...) G_GNUC_PRINTF (2, 3);
186GLIB_AVAILABLE_IN_ALL
187GString* g_string_append_uri_escaped (GString *string,
188 const gchar *unescaped,
189 const gchar *reserved_chars_allowed,
190 gboolean allow_utf8);
191
192#ifdef G_CAN_INLINE
193
194#if defined (_MSC_VER) && !defined (__clang__)
195#pragma warning (push)
196#pragma warning (disable : 4141) /* silence "warning C4141: 'inline' used more than once" */
197#endif
198
199#ifndef __GTK_DOC_IGNORE__
200
201G_ALWAYS_INLINE
202static inline GString*
203g_string_append_c_inline (GString *gstring,
204 gchar c)
205{
206 if (G_LIKELY (gstring != NULL &&
207 gstring->len + 1 < gstring->allocated_len))
208 {
209 gstring->str[gstring->len++] = c;
210 gstring->str[gstring->len] = 0;
211 }
212 else
213 g_string_insert_c (string: gstring, pos: -1, c);
214 return gstring;
215}
216
217#define g_string_append_c(gstr,c) \
218 g_string_append_c_inline (gstr, c)
219
220G_ALWAYS_INLINE
221static inline GString *
222g_string_append_len_inline (GString *gstring,
223 const char *val,
224 gssize len)
225{
226 gsize len_unsigned;
227
228 if G_UNLIKELY (gstring == NULL)
229 return g_string_append_len (string: gstring, val, len);
230
231 if G_UNLIKELY (val == NULL)
232 return (len != 0) ? g_string_append_len (string: gstring, val, len) : gstring;
233
234 if (len < 0)
235 len_unsigned = strlen (s: val);
236 else
237 len_unsigned = (gsize) len;
238
239 if (G_LIKELY (len_unsigned < gstring->allocated_len - gstring->len))
240 {
241 char *end = gstring->str + gstring->len;
242 if (G_LIKELY (val + len_unsigned <= end || val >= end + len_unsigned))
243 memcpy (dest: end, src: val, n: len_unsigned);
244 else
245 memmove (dest: end, src: val, n: len_unsigned);
246 gstring->len += len_unsigned;
247 gstring->str[gstring->len] = 0;
248 return gstring;
249 }
250 else
251 return g_string_insert_len (string: gstring, pos: -1, val, len);
252}
253
254#define g_string_append_len(gstr, val, len) \
255 g_string_append_len_inline (gstr, val, len)
256
257G_ALWAYS_INLINE
258static inline GString *
259g_string_truncate_inline (GString *gstring,
260 gsize len)
261{
262 gstring->len = MIN (len, gstring->len);
263 gstring->str[gstring->len] = '\0';
264 return gstring;
265}
266
267#define g_string_truncate(gstr, len) \
268 g_string_truncate_inline (gstr, len)
269
270#if G_GNUC_CHECK_VERSION (2, 0)
271
272#define g_string_append(gstr, val) \
273 (__builtin_constant_p (val) ? \
274 G_GNUC_EXTENSION ({ \
275 const char * const _val = (val); \
276 g_string_append_len (gstr, _val, \
277 G_LIKELY (_val != NULL) ? \
278 (gssize) strlen (_G_STR_NONNULL (_val)) \
279 : (gssize) -1); \
280 }) \
281 : \
282 g_string_append_len (gstr, val, (gssize) -1))
283
284#endif /* G_GNUC_CHECK_VERSION (2, 0) */
285
286#endif /* __GTK_DOC_IGNORE__ */
287
288#if defined (_MSC_VER) && !defined (__clang__)
289#pragma warning (pop) /* #pragma warning (disable : 4141) */
290#endif
291
292#endif /* G_CAN_INLINE */
293
294GLIB_DEPRECATED
295GString *g_string_down (GString *string);
296GLIB_DEPRECATED
297GString *g_string_up (GString *string);
298
299#define g_string_sprintf g_string_printf GLIB_DEPRECATED_MACRO_IN_2_26_FOR(g_string_printf)
300#define g_string_sprintfa g_string_append_printf GLIB_DEPRECATED_MACRO_IN_2_26_FOR(g_string_append_printf)
301
302G_END_DECLS
303
304#endif /* __G_STRING_H__ */
305