Imported Upstream version 2.74.3
[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  * 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_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 #include <glib/gerror.h>
38
39 G_BEGIN_DECLS
40
41 /* Functions like the ones in <ctype.h> that are not affected by locale. */
42 typedef enum {
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
54 } GAsciiType;
55
56 GLIB_VAR const guint16 * const g_ascii_table;
57
58 #define g_ascii_isalnum(c) \
59   ((g_ascii_table[(guchar) (c)] & G_ASCII_ALNUM) != 0)
60
61 #define g_ascii_isalpha(c) \
62   ((g_ascii_table[(guchar) (c)] & G_ASCII_ALPHA) != 0)
63
64 #define g_ascii_iscntrl(c) \
65   ((g_ascii_table[(guchar) (c)] & G_ASCII_CNTRL) != 0)
66
67 #define g_ascii_isdigit(c) \
68   ((g_ascii_table[(guchar) (c)] & G_ASCII_DIGIT) != 0)
69
70 #define g_ascii_isgraph(c) \
71   ((g_ascii_table[(guchar) (c)] & G_ASCII_GRAPH) != 0)
72
73 #define g_ascii_islower(c) \
74   ((g_ascii_table[(guchar) (c)] & G_ASCII_LOWER) != 0)
75
76 #define g_ascii_isprint(c) \
77   ((g_ascii_table[(guchar) (c)] & G_ASCII_PRINT) != 0)
78
79 #define g_ascii_ispunct(c) \
80   ((g_ascii_table[(guchar) (c)] & G_ASCII_PUNCT) != 0)
81
82 #define g_ascii_isspace(c) \
83   ((g_ascii_table[(guchar) (c)] & G_ASCII_SPACE) != 0)
84
85 #define g_ascii_isupper(c) \
86   ((g_ascii_table[(guchar) (c)] & G_ASCII_UPPER) != 0)
87
88 #define g_ascii_isxdigit(c) \
89   ((g_ascii_table[(guchar) (c)] & G_ASCII_XDIGIT) != 0)
90
91 GLIB_AVAILABLE_IN_ALL
92 gchar                 g_ascii_tolower  (gchar        c) G_GNUC_CONST;
93 GLIB_AVAILABLE_IN_ALL
94 gchar                 g_ascii_toupper  (gchar        c) G_GNUC_CONST;
95
96 GLIB_AVAILABLE_IN_ALL
97 gint                  g_ascii_digit_value  (gchar    c) G_GNUC_CONST;
98 GLIB_AVAILABLE_IN_ALL
99 gint                  g_ascii_xdigit_value (gchar    c) G_GNUC_CONST;
100
101 /* String utility functions that modify a string argument or
102  * return a constant string that must not be freed.
103  */
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,
112                                         gchar         substitutor);
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,
121                                         const gchar  *src,
122                                         gsize         dest_size);
123 GLIB_AVAILABLE_IN_ALL
124 gsize                 g_strlcat        (gchar        *dest,
125                                         const gchar  *src,
126                                         gsize         dest_size);
127 GLIB_AVAILABLE_IN_ALL
128 gchar *               g_strstr_len     (const gchar  *haystack,
129                                         gssize        haystack_len,
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,
136                                         gssize        haystack_len,
137                                         const gchar  *needle);
138
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);
145
146 /* String to/from double conversion functions */
147
148 GLIB_AVAILABLE_IN_ALL
149 gdouble               g_strtod         (const gchar  *nptr,
150                                         gchar       **endptr);
151 GLIB_AVAILABLE_IN_ALL
152 gdouble               g_ascii_strtod   (const gchar  *nptr,
153                                         gchar       **endptr);
154 GLIB_AVAILABLE_IN_ALL
155 guint64               g_ascii_strtoull (const gchar *nptr,
156                                         gchar      **endptr,
157                                         guint        base);
158 GLIB_AVAILABLE_IN_ALL
159 gint64                g_ascii_strtoll  (const gchar *nptr,
160                                         gchar      **endptr,
161                                         guint        base);
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,
168                                         gint          buf_len,
169                                         gdouble       d);
170 GLIB_AVAILABLE_IN_ALL
171 gchar *               g_ascii_formatd  (gchar        *buffer,
172                                         gint          buf_len,
173                                         const gchar  *format,
174                                         gdouble       d);
175
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))
184
185 GLIB_AVAILABLE_IN_ALL
186 gint                  g_ascii_strcasecmp  (const gchar *s1,
187                                            const gchar *s2);
188 GLIB_AVAILABLE_IN_ALL
189 gint                  g_ascii_strncasecmp (const gchar *s1,
190                                            const gchar *s2,
191                                            gsize        n);
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;
198
199 GLIB_AVAILABLE_IN_2_40
200 gboolean              g_str_is_ascii      (const gchar *str);
201
202 GLIB_DEPRECATED
203 gint                  g_strcasecmp     (const gchar *s1,
204                                         const gchar *s2);
205 GLIB_DEPRECATED
206 gint                  g_strncasecmp    (const gchar *s1,
207                                         const gchar *s2,
208                                         guint        n);
209 GLIB_DEPRECATED
210 gchar*                g_strdown        (gchar       *string);
211 GLIB_DEPRECATED
212 gchar*                g_strup          (gchar       *string);
213
214
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.
217  */
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;
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_DEPRECATED_IN_2_68_FOR (g_memdup2)
259 gpointer              g_memdup         (gconstpointer mem,
260                                         guint         byte_size) G_GNUC_ALLOC_SIZE(2);
261
262 GLIB_AVAILABLE_IN_2_68
263 gpointer              g_memdup2        (gconstpointer mem,
264                                         gsize         byte_size) G_GNUC_ALLOC_SIZE(2);
265
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
274  */
275 typedef gchar** GStrv;
276 GLIB_AVAILABLE_IN_ALL
277 gchar**               g_strsplit       (const gchar  *string,
278                                         const gchar  *delimiter,
279                                         gint          max_tokens);
280 GLIB_AVAILABLE_IN_ALL
281 gchar **              g_strsplit_set   (const gchar *string,
282                                         const gchar *delimiters,
283                                         gint         max_tokens);
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);
293
294 GLIB_AVAILABLE_IN_ALL
295 gchar*                g_stpcpy         (gchar        *dest,
296                                         const char   *src);
297
298 GLIB_AVAILABLE_IN_2_40
299 gchar *                 g_str_to_ascii                                  (const gchar   *str,
300                                                                          const gchar   *from_locale);
301
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);
306
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);
311
312 GLIB_AVAILABLE_IN_2_44
313 gboolean              g_strv_contains  (const gchar * const *strv,
314                                         const gchar         *str);
315
316 GLIB_AVAILABLE_IN_2_60
317 gboolean              g_strv_equal     (const gchar * const *strv1,
318                                         const gchar * const *strv2);
319
320 /* Convenience ASCII string to number API */
321
322 /**
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.
326  *
327  * Error codes returned by functions converting a string to a number.
328  *
329  * Since: 2.54
330  */
331 typedef enum
332   {
333     G_NUMBER_PARSER_ERROR_INVALID,
334     G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS,
335   } GNumberParserError;
336
337 /**
338  * G_NUMBER_PARSER_ERROR:
339  *
340  * Domain for errors returned by functions converting a string to a
341  * number.
342  *
343  * Since: 2.54
344  */
345 #define G_NUMBER_PARSER_ERROR (g_number_parser_error_quark ())
346
347 GLIB_AVAILABLE_IN_2_54
348 GQuark                g_number_parser_error_quark  (void);
349
350 GLIB_AVAILABLE_IN_2_54
351 gboolean              g_ascii_string_to_signed     (const gchar  *str,
352                                                     guint         base,
353                                                     gint64        min,
354                                                     gint64        max,
355                                                     gint64       *out_num,
356                                                     GError      **error);
357
358 GLIB_AVAILABLE_IN_2_54
359 gboolean              g_ascii_string_to_unsigned   (const gchar  *str,
360                                                     guint         base,
361                                                     guint64       min,
362                                                     guint64       max,
363                                                     guint64      *out_num,
364                                                     GError      **error);
365
366 G_END_DECLS
367
368 #endif /* __G_STRFUNCS_H__ */