345c16c687385d87e5a28346ba3da56bc5fec671
[apps/madmutt.git] / lib-lib / str.h
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19 /*
20  * Copyright notice from original mutt:
21  * Copyright (C) 2001 Thomas Roessler <roessler@does-not-exist.org>
22  */
23
24 #ifndef MUTT_LIB_LIB_STR_H
25 #define MUTT_LIB_LIB_STR_H
26
27 /** \defgroup mutt_strings Madmutt string API
28  *
29  * This module contains the prefered string API to be used in Madmutt.
30  *
31  * Those function reimplement many usual calls (strlen, strcpy, strcat, …)
32  * It's intended to provide a uniform and consistent API to deal with usual C
33  * strings.
34  *
35  * The strong point that have to be followed are:
36  *  - strings are always \c '\\0' terminated, meaning that we don't have
37  *    stupid semantics à la strncpy.
38  *  - function try to always work on buffers with its size (including the
39  *    ending \c '\\0') to prevent buffer overflows.
40  *  - string and buffers sizes are \c ssize_t, negative values are allowed and
41  *    supported.
42  *  - functions use a à la sprintf semantics (for those that produce strings)
43  *    meaning that they all return the len that could have fit in the buffer
44  *    if it would have been big enough. We never try to reallocate the
45  *    buffers, it's up to the caller if it's needed.
46  */
47 /*@{*/
48
49 /** \file str.h
50  * \brief Madmutt string API header.
51  * \author Pierre Habouzit <madcoder@debian.org>
52  */
53
54 #define HUGE_STRING     5120   /**< \brief Huge buffers */
55 #define LONG_STRING     1024   /**< \brief Long buffers */
56 #define STRING          256    /**< \brief Usual buffers */
57
58 #define NONULL(x)       (x ? x : "")  /**< \brief replace \c NULL strings
59                                                   with emtpy strings */
60 #define ISSPACE(c)      isspace((unsigned char)c) /**< \brief safe isspace */
61
62 /** \brief Convert ascii digits into ints.
63  *
64  * Convert ascii digits into its integer value in base 36.
65  * Non convertible values are converted to 255.
66  *
67  * Translating a digit \c c into its numerical value in base \c x is just doing:
68  * \code
69  *   return (c & ~127) && __m_strdigits[c] < x ? __m_strdigits[c] : -1;
70  * \endcode
71  */
72 extern unsigned char const __m_strdigits[128];
73 /** \brief Convert an ascii base64 digit into ints.
74  *
75  * Convert an a char base64 digit into its int value.
76  * Used by base64val(). Unlike #__m_strdigits, the invalid values are set to
77  * -1 instead of 255.
78  */
79 extern signed char const __m_b64digits[128];
80
81 /** \brief Convert ints from 0&ndash;64 into the corresponding base64 digit. */
82 extern char const __m_b64chars[64];
83 /** \brief Convert ints from 0&ndash;36 into a base36 lowercase digit. */
84 extern char const __m_b36chars_lower[36];
85 /** \brief Convert ints from 0&ndash;36 into a base36 uppercase digit. */
86 extern char const __m_b36chars_upper[36];
87
88 /****************************************************************************/
89 /* conversions                                                              */
90 /****************************************************************************/
91
92 static inline int hexval(int c) {
93     return !(c & ~127) && __m_strdigits[c] < 16 ? __m_strdigits[c] : -1;
94 }
95
96 static inline int base64val(int c) {
97     return (c & ~127) ? -1 : __m_b64digits[c];
98 }
99
100 static inline void m_strtolower(char *p) {
101     for (; *p; p++)
102         *p = tolower((unsigned char)*p);
103 }
104
105 static inline int ascii_toupper(int c) {
106     if ('a' <= c && c <= 'z')
107         return c & ~32;
108
109     return c;
110 }
111
112 static inline int ascii_tolower(int c) {
113     if ('A' <= c && c <= 'Z')
114         return c | 32;
115
116     return c;
117 }
118
119 /****************************************************************************/
120 /* length related                                                           */
121 /****************************************************************************/
122
123 static inline int m_strisempty(const char *s) {
124     return !s || !*s;
125 }
126
127 static inline ssize_t m_strlen(const char *s) {
128     return s ? strlen(s) : 0;
129 }
130
131 static inline ssize_t m_strnlen(const char *s, ssize_t n) {
132     const char *p = memchr(s, '\0', n);
133     return p ? p - s : n;
134 }
135
136 /****************************************************************************/
137 /* comparisons                                                              */
138 /****************************************************************************/
139
140 static inline int m_strcmp(const char *a, const char *b) {
141     return strcmp(NONULL(a), NONULL(b));
142 }
143
144 static inline int m_strcasecmp(const char *a, const char *b) {
145     return strcasecmp(NONULL(a), NONULL(b));
146 }
147
148 static inline int m_strncmp(const char *a, const char *b, ssize_t n) {
149     return strncmp(NONULL(a), NONULL(b), n);
150 }
151
152 static inline int m_strncasecmp(const char *a, const char *b, ssize_t n) {
153     return strncasecmp(NONULL(a), NONULL(b), n);
154 }
155
156 int ascii_strcasecmp(const char *a, const char *b);
157 int ascii_strncasecmp(const char *a, const char *b, ssize_t n);
158
159 /****************************************************************************/
160 /* making copies                                                            */
161 /****************************************************************************/
162
163 static inline char *m_strdup(const char *s) {
164     ssize_t len = m_strlen(s);
165     return len ? p_dup(s, len + 1) : NULL;
166 }
167
168 static inline char *m_substrdup(const char *s, const char *end) {
169     return p_dupstr(s, end ? end - s : m_strlen(s));
170 }
171
172 static inline char *m_strreplace(char **p, const char *s) {
173     p_delete(p);
174     return (*p = m_strdup(s));
175 }
176
177 static inline ssize_t m_strputc(char *dst, ssize_t n, int c) {
178     if (n > 1) {
179         dst[0] = c;
180         dst[1] = '\0';
181     }
182     return 1;
183 }
184
185 ssize_t m_strcpy(char *dst, ssize_t n, const char *src)
186     __attribute__((nonnull(1)));
187
188 ssize_t m_strncpy(char *dst, ssize_t n, const char *src, ssize_t l)
189     __attribute__((nonnull(1)));
190
191 static inline ssize_t m_strcat(char *dst, ssize_t n, const char *src) {
192     ssize_t dlen = m_strnlen(dst, n - 1);
193     return dlen + m_strcpy(dst + dlen, n - dlen, src);
194 }
195
196 static inline ssize_t
197 m_strncat(char *dst, ssize_t n, const char *src, ssize_t l) {
198     ssize_t dlen = m_strnlen(dst, n - 1);
199     return dlen + m_strncpy(dst + dlen, n - dlen, src, l);
200 }
201
202 /****************************************************************************/
203 /* parsing related                                                          */
204 /****************************************************************************/
205
206 static inline const char *m_strchrnul(const char *s, int c) {
207     while (*s && *s != c)
208         s++;
209     return s;
210 }
211
212 static inline const char *m_strnextsp(const char *s) {
213     while (*s && !isspace((unsigned char)*s))
214         s++;
215     return s;
216 }
217
218 static inline const char *skipspaces(const char *s) {
219     while (*s && isspace((unsigned char)*s))
220         s++;
221     return s;
222 }
223 static inline char *vskipspaces(const char *s) {
224     return (char *)skipspaces(s);
225 }
226
227 char *m_strrtrim(char *s)
228     __attribute__((nonnull(1)));
229
230 /****************************************************************************/
231 /* search                                                                   */
232 /****************************************************************************/
233
234 const char *
235 m_stristrn(const char *haystack, const char *needle, ssize_t nlen);
236
237 static inline const char *
238 m_stristr(const char *haystack, const char *needle) {
239     return m_stristrn(haystack, needle, m_strlen(needle));
240 }
241
242 /*@}*/
243 #endif /* MUTT_LIB_LIB_STR_H */