Implement and document g_ascii_isxxx.
[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 #include <stdarg.h>
31 #include <glib/gtypes.h>
32
33 G_BEGIN_DECLS
34
35 /* Functions like the ones in <ctype.h> that are not affected by locale. */
36 typedef enum {
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
48 } GAsciiType;
49
50 extern const guint16 g_ascii_table[256];
51
52 #define g_ascii_isalnum(c) \
53   ((g_ascii_table[(guchar) (c)] & G_ASCII_ALNUM) != 0)
54
55 #define g_ascii_isalpha(c) \
56   ((g_ascii_table[(guchar) (c)] & G_ASCII_ALPHA) != 0)
57
58 #define g_ascii_iscntrl(c) \
59   ((g_ascii_table[(guchar) (c)] & G_ASCII_CNTRL) != 0)
60
61 #define g_ascii_isdigit(c) \
62   ((g_ascii_table[(guchar) (c)] & G_ASCII_DIGIT) != 0)
63
64 #define g_ascii_isgraph(c) \
65   ((g_ascii_table[(guchar) (c)] & G_ASCII_GRAPH) != 0)
66
67 #define g_ascii_islower(c) \
68   ((g_ascii_table[(guchar) (c)] & G_ASCII_LOWER) != 0)
69
70 #define g_ascii_isprint(c) \
71   ((g_ascii_table[(guchar) (c)] & G_ASCII_PRINT) != 0)
72
73 #define g_ascii_ispunct(c) \
74   ((g_ascii_table[(guchar) (c)] & G_ASCII_PUNCT) != 0)
75
76 #define g_ascii_isspace(c) \
77   ((g_ascii_table[(guchar) (c)] & G_ASCII_SPACE) != 0)
78
79 #define g_ascii_isupper(c) \
80   ((g_ascii_table[(guchar) (c)] & G_ASCII_UPPER) != 0)
81
82 #define g_ascii_isxdigit(c) \
83   ((g_ascii_table[(guchar) (c)] & G_ASCII_XDIGIT) != 0)
84
85 gchar                 g_ascii_tolower  (gchar        c) G_GNUC_CONST;
86 gchar                 g_ascii_toupper  (gchar        c) G_GNUC_CONST;
87
88 gint                  g_ascii_digit_value  (gchar    c) G_GNUC_CONST;
89 gint                  g_ascii_xdigit_value (gchar    c) G_GNUC_CONST;
90
91 /* String utility functions that modify a string argument or
92  * return a constant string that must not be freed.
93  */
94 #define  G_STR_DELIMITERS       "_-|> <."
95 gchar*                g_strdelimit     (gchar        *string,
96                                         const gchar *delimiters,
97                                         gchar         new_delimiter);
98 gchar*                g_strcanon       (gchar       *string,
99                                         const gchar *valid_chars,
100                                         gchar        substitutor);
101 gdouble               g_strtod         (const gchar *nptr,
102                                         gchar       **endptr);
103 G_CONST_RETURN gchar* g_strerror       (gint          errnum) G_GNUC_CONST;
104 G_CONST_RETURN gchar* g_strsignal      (gint          signum) G_GNUC_CONST;
105 gchar*                g_strreverse     (gchar        *string);
106 gsize                 g_strlcpy        (gchar        *dest,
107                                         const gchar  *src,
108                                         gsize         dest_size);
109 gsize                 g_strlcat        (gchar        *dest,
110                                         const gchar  *src,
111                                         gsize         dest_size);
112 gchar *               g_strstr_len     (const gchar  *haystack,
113                                         gssize        haystack_len,
114                                         const gchar  *needle);
115 gchar *               g_strrstr        (const gchar  *haystack,
116                                         const gchar  *needle);
117 gchar *               g_strrstr_len    (const gchar  *haystack,
118                                         gssize        haystack_len,
119                                         const gchar  *needle);
120
121 /* removes leading spaces */
122 gchar*                g_strchug        (gchar        *string);
123 /* removes trailing spaces */
124 gchar*                g_strchomp       (gchar        *string);
125 /* removes leading & trailing spaces */
126 #define g_strstrip( string )    g_strchomp (g_strchug (string))
127
128 gint                  g_ascii_strcasecmp  (const gchar *s1,
129                                            const gchar *s2);
130 gint                  g_ascii_strncasecmp (const gchar *s1,
131                                            const gchar *s2,
132                                            guint        n);
133 gchar*                g_ascii_strdown     (const gchar *string);
134 gchar*                g_ascii_strup       (const gchar *string);
135
136 #ifndef G_DISABLE_DEPRECATED
137
138 /* The following four functions are deprecated and will be removed in
139  * the next major release. They use the locale-specific tolower and
140  * toupper, which is almost never the right thing.
141  */
142
143 gint                  g_strcasecmp     (const gchar *s1,
144                                         const gchar *s2);
145 gint                  g_strncasecmp    (const gchar *s1,
146                                         const gchar *s2,
147                                         guint        n);
148 gchar*                g_strdown        (gchar        *string);
149 gchar*                g_strup          (gchar        *string);
150
151 #endif /* G_DISABLE_DEPRECATED */
152
153 /* String utility functions that return a newly allocated string which
154  * ought to be freed with g_free from the caller at some point.
155  */
156 gchar*                g_strdup         (const gchar *str);
157 gchar*                g_strdup_printf  (const gchar *format,
158                                         ...) G_GNUC_PRINTF (1, 2);
159 gchar*                g_strdup_vprintf (const gchar *format,
160                                         va_list      args);
161 gchar*                g_strndup        (const gchar *str,
162                                         gsize        n);  
163 gchar*                g_strnfill       (gsize        length,  
164                                         gchar        fill_char);
165 gchar*                g_strconcat      (const gchar *string1,
166                                         ...); /* NULL terminated */
167 gchar*                g_strjoin        (const gchar  *separator,
168                                         ...); /* NULL terminated */
169 /* Make a copy of a string interpreting C string -style escape
170  * sequences. Inverse of g_strescape. The recognized sequences are \b
171  * \f \n \r \t \\ \" and the octal format.
172  */
173 gchar*                g_strcompress    (const gchar *source);
174
175 /* Copy a string escaping nonprintable characters like in C strings.
176  * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points
177  * to a string containing characters that are not to be escaped.
178  *
179  * Deprecated API: gchar* g_strescape (const gchar *source);
180  * Luckily this function wasn't used much, using NULL as second parameter
181  * provides mostly identical semantics.
182  */
183 gchar*                g_strescape      (const gchar *source,
184                                         const gchar *exceptions);
185
186 gpointer              g_memdup         (gconstpointer mem,
187                                         guint          byte_size);
188
189 /* NULL terminated string arrays.
190  * g_strsplit() splits up string into max_tokens tokens at delim and
191  * returns a newly allocated string array.
192  * g_strjoinv() concatenates all of str_array's strings, sliding in an
193  * optional separator, the returned string is newly allocated.
194  * g_strfreev() frees the array itself and all of its strings.
195  * g_strdupv() copies a NULL-terminated array of strings
196  */
197 gchar**               g_strsplit       (const gchar  *string,
198                                         const gchar  *delimiter,
199                                         gint          max_tokens);
200 gchar*                g_strjoinv       (const gchar  *separator,
201                                         gchar       **str_array);
202 void                  g_strfreev       (gchar       **str_array);
203 gchar**               g_strdupv        (gchar       **str_array);
204
205 gchar*                g_stpcpy         (gchar        *dest,
206                                         const char   *src);
207
208 G_END_DECLS
209
210 #endif /* __G_STRFUNCS_H__ */