1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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__
31 #include <glib/gtypes.h>
35 /* Functions like the ones in <ctype.h> that are not affected by locale. */
37 G_ASCII_ALNUM = 1 << 0,
38 G_ASCII_ALPHA = 1 << 1,
39 G_ASCII_CNTRL = 1 << 2,
40 G_ASCII_DIGIT = 1 << 3,
41 G_ASCII_GRAPH = 1 << 4,
42 G_ASCII_LOWER = 1 << 5,
43 G_ASCII_PRINT = 1 << 6,
44 G_ASCII_PUNCT = 1 << 7,
45 G_ASCII_SPACE = 1 << 8,
46 G_ASCII_UPPER = 1 << 9,
47 G_ASCII_XDIGIT = 1 << 10
50 GLIB_VAR const guint16 * const g_ascii_table;
52 #define g_ascii_isalnum(c) \
53 ((g_ascii_table[(guchar) (c)] & G_ASCII_ALNUM) != 0)
55 #define g_ascii_isalpha(c) \
56 ((g_ascii_table[(guchar) (c)] & G_ASCII_ALPHA) != 0)
58 #define g_ascii_iscntrl(c) \
59 ((g_ascii_table[(guchar) (c)] & G_ASCII_CNTRL) != 0)
61 #define g_ascii_isdigit(c) \
62 ((g_ascii_table[(guchar) (c)] & G_ASCII_DIGIT) != 0)
64 #define g_ascii_isgraph(c) \
65 ((g_ascii_table[(guchar) (c)] & G_ASCII_GRAPH) != 0)
67 #define g_ascii_islower(c) \
68 ((g_ascii_table[(guchar) (c)] & G_ASCII_LOWER) != 0)
70 #define g_ascii_isprint(c) \
71 ((g_ascii_table[(guchar) (c)] & G_ASCII_PRINT) != 0)
73 #define g_ascii_ispunct(c) \
74 ((g_ascii_table[(guchar) (c)] & G_ASCII_PUNCT) != 0)
76 #define g_ascii_isspace(c) \
77 ((g_ascii_table[(guchar) (c)] & G_ASCII_SPACE) != 0)
79 #define g_ascii_isupper(c) \
80 ((g_ascii_table[(guchar) (c)] & G_ASCII_UPPER) != 0)
82 #define g_ascii_isxdigit(c) \
83 ((g_ascii_table[(guchar) (c)] & G_ASCII_XDIGIT) != 0)
85 gchar g_ascii_tolower (gchar c) G_GNUC_CONST;
86 gchar g_ascii_toupper (gchar c) G_GNUC_CONST;
88 gint g_ascii_digit_value (gchar c) G_GNUC_CONST;
89 gint g_ascii_xdigit_value (gchar c) G_GNUC_CONST;
91 /* String utility functions that modify a string argument or
92 * return a constant string that must not be freed.
94 #define G_STR_DELIMITERS "_-|> <."
95 gchar* g_strdelimit (gchar *string,
96 const gchar *delimiters,
98 gchar* g_strcanon (gchar *string,
99 const gchar *valid_chars,
101 G_CONST_RETURN gchar* g_strerror (gint errnum) G_GNUC_CONST;
102 G_CONST_RETURN gchar* g_strsignal (gint signum) G_GNUC_CONST;
103 gchar* g_strreverse (gchar *string);
104 gsize g_strlcpy (gchar *dest,
107 gsize g_strlcat (gchar *dest,
110 gchar * g_strstr_len (const gchar *haystack,
112 const gchar *needle);
113 gchar * g_strrstr (const gchar *haystack,
114 const gchar *needle);
115 gchar * g_strrstr_len (const gchar *haystack,
117 const gchar *needle);
119 gboolean g_str_has_suffix (const gchar *str,
120 const gchar *suffix);
121 gboolean g_str_has_prefix (const gchar *str,
122 const gchar *prefix);
124 /* String to/from double conversion functions */
126 gdouble g_strtod (const gchar *nptr,
128 gdouble g_ascii_strtod (const gchar *nptr,
130 guint64 g_ascii_strtoull (const gchar *nptr,
133 /* 29 bytes should enough for all possible values that
134 * g_ascii_dtostr can produce.
135 * Then add 10 for good measure */
136 #define G_ASCII_DTOSTR_BUF_SIZE (29 + 10)
137 gchar * g_ascii_dtostr (gchar *buffer,
140 gchar * g_ascii_formatd (gchar *buffer,
145 /* removes leading spaces */
146 gchar* g_strchug (gchar *string);
147 /* removes trailing spaces */
148 gchar* g_strchomp (gchar *string);
149 /* removes leading & trailing spaces */
150 #define g_strstrip( string ) g_strchomp (g_strchug (string))
152 gint g_ascii_strcasecmp (const gchar *s1,
154 gint g_ascii_strncasecmp (const gchar *s1,
157 gchar* g_ascii_strdown (const gchar *str,
158 gssize len) G_GNUC_MALLOC;
159 gchar* g_ascii_strup (const gchar *str,
160 gssize len) G_GNUC_MALLOC;
162 #ifndef G_DISABLE_DEPRECATED
164 /* The following four functions are deprecated and will be removed in
165 * the next major release. They use the locale-specific tolower and
166 * toupper, which is almost never the right thing.
169 gint g_strcasecmp (const gchar *s1,
171 gint g_strncasecmp (const gchar *s1,
174 gchar* g_strdown (gchar *string);
175 gchar* g_strup (gchar *string);
177 #endif /* G_DISABLE_DEPRECATED */
179 /* String utility functions that return a newly allocated string which
180 * ought to be freed with g_free from the caller at some point.
182 gchar* g_strdup (const gchar *str) G_GNUC_MALLOC;
183 gchar* g_strdup_printf (const gchar *format,
184 ...) G_GNUC_PRINTF (1, 2) G_GNUC_MALLOC;
185 gchar* g_strdup_vprintf (const gchar *format,
186 va_list args) G_GNUC_MALLOC;
187 gchar* g_strndup (const gchar *str,
188 gsize n) G_GNUC_MALLOC;
189 gchar* g_strnfill (gsize length,
190 gchar fill_char) G_GNUC_MALLOC;
191 gchar* g_strconcat (const gchar *string1,
192 ...) G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
193 gchar* g_strjoin (const gchar *separator,
194 ...) G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
196 /* Make a copy of a string interpreting C string -style escape
197 * sequences. Inverse of g_strescape. The recognized sequences are \b
198 * \f \n \r \t \\ \" and the octal format.
200 gchar* g_strcompress (const gchar *source) G_GNUC_MALLOC;
202 /* Copy a string escaping nonprintable characters like in C strings.
203 * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points
204 * to a string containing characters that are not to be escaped.
206 * Deprecated API: gchar* g_strescape (const gchar *source);
207 * Luckily this function wasn't used much, using NULL as second parameter
208 * provides mostly identical semantics.
210 gchar* g_strescape (const gchar *source,
211 const gchar *exceptions) G_GNUC_MALLOC;
213 gpointer g_memdup (gconstpointer mem,
214 guint byte_size) G_GNUC_MALLOC;
216 /* NULL terminated string arrays.
217 * g_strsplit(), g_strsplit_set() split up string into max_tokens tokens
218 * at delim and return a newly allocated string array.
219 * g_strjoinv() concatenates all of str_array's strings, sliding in an
220 * optional separator, the returned string is newly allocated.
221 * g_strfreev() frees the array itself and all of its strings.
222 * g_strdupv() copies a NULL-terminated array of strings
223 * g_strv_length() returns the length of a NULL-terminated array of strings
225 gchar** g_strsplit (const gchar *string,
226 const gchar *delimiter,
227 gint max_tokens) G_GNUC_MALLOC;
228 gchar ** g_strsplit_set (const gchar *string,
229 const gchar *delimiters,
230 gint max_tokens) G_GNUC_MALLOC;
231 gchar* g_strjoinv (const gchar *separator,
232 gchar **str_array) G_GNUC_MALLOC;
233 void g_strfreev (gchar **str_array);
234 gchar** g_strdupv (gchar **str_array) G_GNUC_MALLOC;
235 guint g_strv_length (gchar **str_array);
237 gchar* g_stpcpy (gchar *dest,
240 G_CONST_RETURN gchar *g_strip_context (const gchar *msgid,
241 const gchar *msgval);
245 #endif /* __G_STRFUNCS_H__ */