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