1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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.
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/>.
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 #ifndef __G_STRFUNCS_H__
28 #define __G_STRFUNCS_H__
30 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
31 #error "Only <glib.h> can be included directly."
35 #include <glib/gmacros.h>
36 #include <glib/gtypes.h>
37 #include <glib/gerror.h>
41 /* Functions like the ones in <ctype.h> that are not affected by locale. */
43 G_ASCII_ALNUM = 1 << 0,
44 G_ASCII_ALPHA = 1 << 1,
45 G_ASCII_CNTRL = 1 << 2,
46 G_ASCII_DIGIT = 1 << 3,
47 G_ASCII_GRAPH = 1 << 4,
48 G_ASCII_LOWER = 1 << 5,
49 G_ASCII_PRINT = 1 << 6,
50 G_ASCII_PUNCT = 1 << 7,
51 G_ASCII_SPACE = 1 << 8,
52 G_ASCII_UPPER = 1 << 9,
53 G_ASCII_XDIGIT = 1 << 10
56 GLIB_VAR const guint16 * const g_ascii_table;
58 #define g_ascii_isalnum(c) \
59 ((g_ascii_table[(guchar) (c)] & G_ASCII_ALNUM) != 0)
61 #define g_ascii_isalpha(c) \
62 ((g_ascii_table[(guchar) (c)] & G_ASCII_ALPHA) != 0)
64 #define g_ascii_iscntrl(c) \
65 ((g_ascii_table[(guchar) (c)] & G_ASCII_CNTRL) != 0)
67 #define g_ascii_isdigit(c) \
68 ((g_ascii_table[(guchar) (c)] & G_ASCII_DIGIT) != 0)
70 #define g_ascii_isgraph(c) \
71 ((g_ascii_table[(guchar) (c)] & G_ASCII_GRAPH) != 0)
73 #define g_ascii_islower(c) \
74 ((g_ascii_table[(guchar) (c)] & G_ASCII_LOWER) != 0)
76 #define g_ascii_isprint(c) \
77 ((g_ascii_table[(guchar) (c)] & G_ASCII_PRINT) != 0)
79 #define g_ascii_ispunct(c) \
80 ((g_ascii_table[(guchar) (c)] & G_ASCII_PUNCT) != 0)
82 #define g_ascii_isspace(c) \
83 ((g_ascii_table[(guchar) (c)] & G_ASCII_SPACE) != 0)
85 #define g_ascii_isupper(c) \
86 ((g_ascii_table[(guchar) (c)] & G_ASCII_UPPER) != 0)
88 #define g_ascii_isxdigit(c) \
89 ((g_ascii_table[(guchar) (c)] & G_ASCII_XDIGIT) != 0)
92 gchar g_ascii_tolower (gchar c) G_GNUC_CONST;
94 gchar g_ascii_toupper (gchar c) G_GNUC_CONST;
97 gint g_ascii_digit_value (gchar c) G_GNUC_CONST;
99 gint g_ascii_xdigit_value (gchar c) G_GNUC_CONST;
101 /* String utility functions that modify a string argument or
102 * return a constant string that must not be freed.
104 #define G_STR_DELIMITERS "_-|> <."
105 GLIB_AVAILABLE_IN_ALL
106 gchar* g_strdelimit (gchar *string,
107 const gchar *delimiters,
108 gchar new_delimiter);
109 GLIB_AVAILABLE_IN_ALL
110 gchar* g_strcanon (gchar *string,
111 const gchar *valid_chars,
113 GLIB_AVAILABLE_IN_ALL
114 const gchar * g_strerror (gint errnum) G_GNUC_CONST;
115 GLIB_AVAILABLE_IN_ALL
116 const gchar * g_strsignal (gint signum) G_GNUC_CONST;
117 GLIB_AVAILABLE_IN_ALL
118 gchar * g_strreverse (gchar *string);
119 GLIB_AVAILABLE_IN_ALL
120 gsize g_strlcpy (gchar *dest,
123 GLIB_AVAILABLE_IN_ALL
124 gsize g_strlcat (gchar *dest,
127 GLIB_AVAILABLE_IN_ALL
128 gchar * g_strstr_len (const gchar *haystack,
130 const gchar *needle);
131 GLIB_AVAILABLE_IN_ALL
132 gchar * g_strrstr (const gchar *haystack,
133 const gchar *needle);
134 GLIB_AVAILABLE_IN_ALL
135 gchar * g_strrstr_len (const gchar *haystack,
137 const gchar *needle);
139 GLIB_AVAILABLE_IN_ALL
140 gboolean g_str_has_suffix (const gchar *str,
141 const gchar *suffix);
142 GLIB_AVAILABLE_IN_ALL
143 gboolean g_str_has_prefix (const gchar *str,
144 const gchar *prefix);
146 /* String to/from double conversion functions */
148 GLIB_AVAILABLE_IN_ALL
149 gdouble g_strtod (const gchar *nptr,
151 GLIB_AVAILABLE_IN_ALL
152 gdouble g_ascii_strtod (const gchar *nptr,
154 GLIB_AVAILABLE_IN_ALL
155 guint64 g_ascii_strtoull (const gchar *nptr,
158 GLIB_AVAILABLE_IN_ALL
159 gint64 g_ascii_strtoll (const gchar *nptr,
162 /* 29 bytes should enough for all possible values that
163 * g_ascii_dtostr can produce.
164 * Then add 10 for good measure */
165 #define G_ASCII_DTOSTR_BUF_SIZE (29 + 10)
166 GLIB_AVAILABLE_IN_ALL
167 gchar * g_ascii_dtostr (gchar *buffer,
170 GLIB_AVAILABLE_IN_ALL
171 gchar * g_ascii_formatd (gchar *buffer,
176 /* removes leading spaces */
177 GLIB_AVAILABLE_IN_ALL
178 gchar* g_strchug (gchar *string);
179 /* removes trailing spaces */
180 GLIB_AVAILABLE_IN_ALL
181 gchar* g_strchomp (gchar *string);
182 /* removes leading & trailing spaces */
183 #define g_strstrip( string ) g_strchomp (g_strchug (string))
185 GLIB_AVAILABLE_IN_ALL
186 gint g_ascii_strcasecmp (const gchar *s1,
188 GLIB_AVAILABLE_IN_ALL
189 gint g_ascii_strncasecmp (const gchar *s1,
192 GLIB_AVAILABLE_IN_ALL
193 gchar* g_ascii_strdown (const gchar *str,
194 gssize len) G_GNUC_MALLOC;
195 GLIB_AVAILABLE_IN_ALL
196 gchar* g_ascii_strup (const gchar *str,
197 gssize len) G_GNUC_MALLOC;
199 GLIB_AVAILABLE_IN_2_40
200 gboolean g_str_is_ascii (const gchar *str);
203 gint g_strcasecmp (const gchar *s1,
206 gint g_strncasecmp (const gchar *s1,
210 gchar* g_strdown (gchar *string);
212 gchar* g_strup (gchar *string);
215 /* String utility functions that return a newly allocated string which
216 * ought to be freed with g_free from the caller at some point.
218 GLIB_AVAILABLE_IN_ALL
219 gchar* g_strdup (const gchar *str) G_GNUC_MALLOC;
220 GLIB_AVAILABLE_IN_ALL
221 gchar* g_strdup_printf (const gchar *format,
222 ...) G_GNUC_PRINTF (1, 2) G_GNUC_MALLOC;
223 GLIB_AVAILABLE_IN_ALL
224 gchar* g_strdup_vprintf (const gchar *format,
225 va_list args) G_GNUC_PRINTF(1, 0) G_GNUC_MALLOC;
226 GLIB_AVAILABLE_IN_ALL
227 gchar* g_strndup (const gchar *str,
228 gsize n) G_GNUC_MALLOC;
229 GLIB_AVAILABLE_IN_ALL
230 gchar* g_strnfill (gsize length,
231 gchar fill_char) G_GNUC_MALLOC;
232 GLIB_AVAILABLE_IN_ALL
233 gchar* g_strconcat (const gchar *string1,
234 ...) G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
235 GLIB_AVAILABLE_IN_ALL
236 gchar* g_strjoin (const gchar *separator,
237 ...) G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
239 /* Make a copy of a string interpreting C string -style escape
240 * sequences. Inverse of g_strescape. The recognized sequences are \b
241 * \f \n \r \t \\ \" and the octal format.
243 GLIB_AVAILABLE_IN_ALL
244 gchar* g_strcompress (const gchar *source) G_GNUC_MALLOC;
246 /* Copy a string escaping nonprintable characters like in C strings.
247 * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points
248 * to a string containing characters that are not to be escaped.
250 * Deprecated API: gchar* g_strescape (const gchar *source);
251 * Luckily this function wasn't used much, using NULL as second parameter
252 * provides mostly identical semantics.
254 GLIB_AVAILABLE_IN_ALL
255 gchar* g_strescape (const gchar *source,
256 const gchar *exceptions) G_GNUC_MALLOC;
258 GLIB_DEPRECATED_IN_2_68_FOR (g_memdup2)
259 gpointer g_memdup (gconstpointer mem,
260 guint byte_size) G_GNUC_ALLOC_SIZE(2);
262 GLIB_AVAILABLE_IN_2_68
263 gpointer g_memdup2 (gconstpointer mem,
264 gsize byte_size) G_GNUC_ALLOC_SIZE(2);
266 /* NULL terminated string arrays.
267 * g_strsplit(), g_strsplit_set() split up string into max_tokens tokens
268 * at delim and return a newly allocated string array.
269 * g_strjoinv() concatenates all of str_array's strings, sliding in an
270 * optional separator, the returned string is newly allocated.
271 * g_strfreev() frees the array itself and all of its strings.
272 * g_strdupv() copies a NULL-terminated array of strings
273 * g_strv_length() returns the length of a NULL-terminated array of strings
275 typedef gchar** GStrv;
276 GLIB_AVAILABLE_IN_ALL
277 gchar** g_strsplit (const gchar *string,
278 const gchar *delimiter,
280 GLIB_AVAILABLE_IN_ALL
281 gchar ** g_strsplit_set (const gchar *string,
282 const gchar *delimiters,
284 GLIB_AVAILABLE_IN_ALL
285 gchar* g_strjoinv (const gchar *separator,
286 gchar **str_array) G_GNUC_MALLOC;
287 GLIB_AVAILABLE_IN_ALL
288 void g_strfreev (gchar **str_array);
289 GLIB_AVAILABLE_IN_ALL
290 gchar** g_strdupv (gchar **str_array);
291 GLIB_AVAILABLE_IN_ALL
292 guint g_strv_length (gchar **str_array);
294 GLIB_AVAILABLE_IN_ALL
295 gchar* g_stpcpy (gchar *dest,
298 GLIB_AVAILABLE_IN_2_40
299 gchar * g_str_to_ascii (const gchar *str,
300 const gchar *from_locale);
302 GLIB_AVAILABLE_IN_2_40
303 gchar ** g_str_tokenize_and_fold (const gchar *string,
304 const gchar *translit_locale,
305 gchar ***ascii_alternates);
307 GLIB_AVAILABLE_IN_2_40
308 gboolean g_str_match_string (const gchar *search_term,
309 const gchar *potential_hit,
310 gboolean accept_alternates);
312 GLIB_AVAILABLE_IN_2_44
313 gboolean g_strv_contains (const gchar * const *strv,
316 GLIB_AVAILABLE_IN_2_60
317 gboolean g_strv_equal (const gchar * const *strv1,
318 const gchar * const *strv2);
320 /* Convenience ASCII string to number API */
323 * GNumberParserError:
324 * @G_NUMBER_PARSER_ERROR_INVALID: String was not a valid number.
325 * @G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS: String was a number, but out of bounds.
327 * Error codes returned by functions converting a string to a number.
333 G_NUMBER_PARSER_ERROR_INVALID,
334 G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS,
335 } GNumberParserError;
338 * G_NUMBER_PARSER_ERROR:
340 * Domain for errors returned by functions converting a string to a
345 #define G_NUMBER_PARSER_ERROR (g_number_parser_error_quark ())
347 GLIB_AVAILABLE_IN_2_54
348 GQuark g_number_parser_error_quark (void);
350 GLIB_AVAILABLE_IN_2_54
351 gboolean g_ascii_string_to_signed (const gchar *str,
358 GLIB_AVAILABLE_IN_2_54
359 gboolean g_ascii_string_to_unsigned (const gchar *str,
368 #endif /* __G_STRFUNCS_H__ */