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.1 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, see <http://www.gnu.org/licenses/>.
16 */
17
18/*
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
23 */
24
25#ifndef __G_STRFUNCS_H__
26#define __G_STRFUNCS_H__
27
28#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
29#error "Only <glib.h> can be included directly."
30#endif
31
32#include <stdarg.h>
33#include <glib/gmacros.h>
34#include <glib/gtypes.h>
35#include <glib/gerror.h>
36
37G_BEGIN_DECLS
38
39/* Functions like the ones in <ctype.h> that are not affected by locale. */
40typedef enum {
41 G_ASCII_ALNUM = 1 << 0,
42 G_ASCII_ALPHA = 1 << 1,
43 G_ASCII_CNTRL = 1 << 2,
44 G_ASCII_DIGIT = 1 << 3,
45 G_ASCII_GRAPH = 1 << 4,
46 G_ASCII_LOWER = 1 << 5,
47 G_ASCII_PRINT = 1 << 6,
48 G_ASCII_PUNCT = 1 << 7,
49 G_ASCII_SPACE = 1 << 8,
50 G_ASCII_UPPER = 1 << 9,
51 G_ASCII_XDIGIT = 1 << 10
52} GAsciiType;
53
54GLIB_VAR const guint16 * const g_ascii_table;
55
56#define g_ascii_isalnum(c) \
57 ((g_ascii_table[(guchar) (c)] & G_ASCII_ALNUM) != 0)
58
59#define g_ascii_isalpha(c) \
60 ((g_ascii_table[(guchar) (c)] & G_ASCII_ALPHA) != 0)
61
62#define g_ascii_iscntrl(c) \
63 ((g_ascii_table[(guchar) (c)] & G_ASCII_CNTRL) != 0)
64
65#define g_ascii_isdigit(c) \
66 ((g_ascii_table[(guchar) (c)] & G_ASCII_DIGIT) != 0)
67
68#define g_ascii_isgraph(c) \
69 ((g_ascii_table[(guchar) (c)] & G_ASCII_GRAPH) != 0)
70
71#define g_ascii_islower(c) \
72 ((g_ascii_table[(guchar) (c)] & G_ASCII_LOWER) != 0)
73
74#define g_ascii_isprint(c) \
75 ((g_ascii_table[(guchar) (c)] & G_ASCII_PRINT) != 0)
76
77#define g_ascii_ispunct(c) \
78 ((g_ascii_table[(guchar) (c)] & G_ASCII_PUNCT) != 0)
79
80#define g_ascii_isspace(c) \
81 ((g_ascii_table[(guchar) (c)] & G_ASCII_SPACE) != 0)
82
83#define g_ascii_isupper(c) \
84 ((g_ascii_table[(guchar) (c)] & G_ASCII_UPPER) != 0)
85
86#define g_ascii_isxdigit(c) \
87 ((g_ascii_table[(guchar) (c)] & G_ASCII_XDIGIT) != 0)
88
89GLIB_AVAILABLE_IN_ALL
90gchar g_ascii_tolower (gchar c) G_GNUC_CONST;
91GLIB_AVAILABLE_IN_ALL
92gchar g_ascii_toupper (gchar c) G_GNUC_CONST;
93
94GLIB_AVAILABLE_IN_ALL
95gint g_ascii_digit_value (gchar c) G_GNUC_CONST;
96GLIB_AVAILABLE_IN_ALL
97gint g_ascii_xdigit_value (gchar c) G_GNUC_CONST;
98
99/* String utility functions that modify a string argument or
100 * return a constant string that must not be freed.
101 */
102#define G_STR_DELIMITERS "_-|> <."
103GLIB_AVAILABLE_IN_ALL
104gchar* g_strdelimit (gchar *string,
105 const gchar *delimiters,
106 gchar new_delimiter);
107GLIB_AVAILABLE_IN_ALL
108gchar* g_strcanon (gchar *string,
109 const gchar *valid_chars,
110 gchar substitutor);
111GLIB_AVAILABLE_IN_ALL
112const gchar * g_strerror (gint errnum) G_GNUC_CONST;
113GLIB_AVAILABLE_IN_ALL
114const gchar * g_strsignal (gint signum) G_GNUC_CONST;
115GLIB_AVAILABLE_IN_ALL
116gchar * g_strreverse (gchar *string);
117GLIB_AVAILABLE_IN_ALL
118gsize g_strlcpy (gchar *dest,
119 const gchar *src,
120 gsize dest_size);
121GLIB_AVAILABLE_IN_ALL
122gsize g_strlcat (gchar *dest,
123 const gchar *src,
124 gsize dest_size);
125GLIB_AVAILABLE_IN_ALL
126gchar * g_strstr_len (const gchar *haystack,
127 gssize haystack_len,
128 const gchar *needle);
129GLIB_AVAILABLE_IN_ALL
130gchar * g_strrstr (const gchar *haystack,
131 const gchar *needle);
132GLIB_AVAILABLE_IN_ALL
133gchar * g_strrstr_len (const gchar *haystack,
134 gssize haystack_len,
135 const gchar *needle);
136
137GLIB_AVAILABLE_IN_ALL
138gboolean g_str_has_suffix (const gchar *str,
139 const gchar *suffix);
140GLIB_AVAILABLE_IN_ALL
141gboolean g_str_has_prefix (const gchar *str,
142 const gchar *prefix);
143
144/* String to/from double conversion functions */
145
146GLIB_AVAILABLE_IN_ALL
147gdouble g_strtod (const gchar *nptr,
148 gchar **endptr);
149GLIB_AVAILABLE_IN_ALL
150gdouble g_ascii_strtod (const gchar *nptr,
151 gchar **endptr);
152GLIB_AVAILABLE_IN_ALL
153guint64 g_ascii_strtoull (const gchar *nptr,
154 gchar **endptr,
155 guint base);
156GLIB_AVAILABLE_IN_ALL
157gint64 g_ascii_strtoll (const gchar *nptr,
158 gchar **endptr,
159 guint base);
160/* 29 bytes should enough for all possible values that
161 * g_ascii_dtostr can produce.
162 * Then add 10 for good measure */
163#define G_ASCII_DTOSTR_BUF_SIZE (29 + 10)
164GLIB_AVAILABLE_IN_ALL
165gchar * g_ascii_dtostr (gchar *buffer,
166 gint buf_len,
167 gdouble d);
168GLIB_AVAILABLE_IN_ALL
169gchar * g_ascii_formatd (gchar *buffer,
170 gint buf_len,
171 const gchar *format,
172 gdouble d);
173
174/* removes leading spaces */
175GLIB_AVAILABLE_IN_ALL
176gchar* g_strchug (gchar *string);
177/* removes trailing spaces */
178GLIB_AVAILABLE_IN_ALL
179gchar* g_strchomp (gchar *string);
180/* removes leading & trailing spaces */
181#define g_strstrip( string ) g_strchomp (g_strchug (string))
182
183GLIB_AVAILABLE_IN_ALL
184gint g_ascii_strcasecmp (const gchar *s1,
185 const gchar *s2);
186GLIB_AVAILABLE_IN_ALL
187gint g_ascii_strncasecmp (const gchar *s1,
188 const gchar *s2,
189 gsize n);
190GLIB_AVAILABLE_IN_ALL
191gchar* g_ascii_strdown (const gchar *str,
192 gssize len) G_GNUC_MALLOC;
193GLIB_AVAILABLE_IN_ALL
194gchar* g_ascii_strup (const gchar *str,
195 gssize len) G_GNUC_MALLOC;
196
197GLIB_AVAILABLE_IN_2_40
198gboolean g_str_is_ascii (const gchar *str);
199
200GLIB_DEPRECATED
201gint g_strcasecmp (const gchar *s1,
202 const gchar *s2);
203GLIB_DEPRECATED
204gint g_strncasecmp (const gchar *s1,
205 const gchar *s2,
206 guint n);
207GLIB_DEPRECATED
208gchar* g_strdown (gchar *string);
209GLIB_DEPRECATED
210gchar* g_strup (gchar *string);
211
212
213/* String utility functions that return a newly allocated string which
214 * ought to be freed with g_free from the caller at some point.
215 */
216GLIB_AVAILABLE_IN_ALL
217gchar* g_strdup (const gchar *str) G_GNUC_MALLOC;
218GLIB_AVAILABLE_IN_ALL
219gchar* g_strdup_printf (const gchar *format,
220 ...) G_GNUC_PRINTF (1, 2) G_GNUC_MALLOC;
221GLIB_AVAILABLE_IN_ALL
222gchar* g_strdup_vprintf (const gchar *format,
223 va_list args) G_GNUC_PRINTF(1, 0) G_GNUC_MALLOC;
224GLIB_AVAILABLE_IN_ALL
225gchar* g_strndup (const gchar *str,
226 gsize n) G_GNUC_MALLOC;
227GLIB_AVAILABLE_IN_ALL
228gchar* g_strnfill (gsize length,
229 gchar fill_char) G_GNUC_MALLOC;
230GLIB_AVAILABLE_IN_ALL
231gchar* g_strconcat (const gchar *string1,
232 ...) G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
233GLIB_AVAILABLE_IN_ALL
234gchar* g_strjoin (const gchar *separator,
235 ...) G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
236
237/* Make a copy of a string interpreting C string -style escape
238 * sequences. Inverse of g_strescape. The recognized sequences are \b
239 * \f \n \r \t \\ \" and the octal format.
240 */
241GLIB_AVAILABLE_IN_ALL
242gchar* g_strcompress (const gchar *source) G_GNUC_MALLOC;
243
244/* Copy a string escaping nonprintable characters like in C strings.
245 * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points
246 * to a string containing characters that are not to be escaped.
247 *
248 * Deprecated API: gchar* g_strescape (const gchar *source);
249 * Luckily this function wasn't used much, using NULL as second parameter
250 * provides mostly identical semantics.
251 */
252GLIB_AVAILABLE_IN_ALL
253gchar* g_strescape (const gchar *source,
254 const gchar *exceptions) G_GNUC_MALLOC;
255
256GLIB_AVAILABLE_IN_ALL
257gpointer g_memdup (gconstpointer mem,
258 guint byte_size) G_GNUC_ALLOC_SIZE(2);
259
260/* NULL terminated string arrays.
261 * g_strsplit(), g_strsplit_set() split up string into max_tokens tokens
262 * at delim and return a newly allocated string array.
263 * g_strjoinv() concatenates all of str_array's strings, sliding in an
264 * optional separator, the returned string is newly allocated.
265 * g_strfreev() frees the array itself and all of its strings.
266 * g_strdupv() copies a NULL-terminated array of strings
267 * g_strv_length() returns the length of a NULL-terminated array of strings
268 */
269typedef gchar** GStrv;
270GLIB_AVAILABLE_IN_ALL
271gchar** g_strsplit (const gchar *string,
272 const gchar *delimiter,
273 gint max_tokens);
274GLIB_AVAILABLE_IN_ALL
275gchar ** g_strsplit_set (const gchar *string,
276 const gchar *delimiters,
277 gint max_tokens);
278GLIB_AVAILABLE_IN_ALL
279gchar* g_strjoinv (const gchar *separator,
280 gchar **str_array) G_GNUC_MALLOC;
281GLIB_AVAILABLE_IN_ALL
282void g_strfreev (gchar **str_array);
283GLIB_AVAILABLE_IN_ALL
284gchar** g_strdupv (gchar **str_array);
285GLIB_AVAILABLE_IN_ALL
286guint g_strv_length (gchar **str_array);
287
288GLIB_AVAILABLE_IN_ALL
289gchar* g_stpcpy (gchar *dest,
290 const char *src);
291
292GLIB_AVAILABLE_IN_2_40
293gchar * g_str_to_ascii (const gchar *str,
294 const gchar *from_locale);
295
296GLIB_AVAILABLE_IN_2_40
297gchar ** g_str_tokenize_and_fold (const gchar *string,
298 const gchar *translit_locale,
299 gchar ***ascii_alternates);
300
301GLIB_AVAILABLE_IN_2_40
302gboolean g_str_match_string (const gchar *search_term,
303 const gchar *potential_hit,
304 gboolean accept_alternates);
305
306GLIB_AVAILABLE_IN_2_44
307gboolean g_strv_contains (const gchar * const *strv,
308 const gchar *str);
309
310GLIB_AVAILABLE_IN_2_60
311gboolean g_strv_equal (const gchar * const *strv1,
312 const gchar * const *strv2);
313
314/* Convenience ASCII string to number API */
315
316/**
317 * GNumberParserError:
318 * @G_NUMBER_PARSER_ERROR_INVALID: String was not a valid number.
319 * @G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS: String was a number, but out of bounds.
320 *
321 * Error codes returned by functions converting a string to a number.
322 *
323 * Since: 2.54
324 */
325typedef enum
326 {
327 G_NUMBER_PARSER_ERROR_INVALID,
328 G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS,
329 } GNumberParserError;
330
331/**
332 * G_NUMBER_PARSER_ERROR:
333 *
334 * Domain for errors returned by functions converting a string to a
335 * number.
336 *
337 * Since: 2.54
338 */
339#define G_NUMBER_PARSER_ERROR (g_number_parser_error_quark ())
340
341GLIB_AVAILABLE_IN_2_54
342GQuark g_number_parser_error_quark (void);
343
344GLIB_AVAILABLE_IN_2_54
345gboolean g_ascii_string_to_signed (const gchar *str,
346 guint base,
347 gint64 min,
348 gint64 max,
349 gint64 *out_num,
350 GError **error);
351
352GLIB_AVAILABLE_IN_2_54
353gboolean g_ascii_string_to_unsigned (const gchar *str,
354 guint base,
355 guint64 min,
356 guint64 max,
357 guint64 *out_num,
358 GError **error);
359
360G_END_DECLS
361
362#endif /* __G_STRFUNCS_H__ */
363