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