no more SHORT_STRING's. begin some doc.
[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 /** \file str.h.
28  * \brief Madmutt string API.
29  *
30  * \author Pierre Habouzit <madcoder@debian.org>
31  *
32  * This header contains the prefered string API to be used in Madmutt.
33  *
34  * Those function reimplement many usual calls (strlen, strcpy, strcat, …)
35  * It's intended to provide a uniform and consistent API to deal with usual C
36  * strings.
37  *
38  * The strong point that have to be followed are:
39  *  - strings are always \c '\\0' terminated, meaning that we don't have
40  *    stupid semantics à la strncpy.
41  *  - function try to always work on buffers with its size (including the
42  *    ending \c '\\0') to prevent buffer overflows.
43  *  - string and buffers sizes are ssize_t, negative values are allowed and
44  *    supported.
45  *  - functions use a à la sprintf semantics (for those that produce strings)
46  *    meaning that they all return the len that could have fit in the buffer
47  *    if it would have been big enough. We never try to reallocate the
48  *    buffers, it's up to the caller if it's needed.
49  */
50
51
52 #define HUGE_STRING     5120   /**< \brief Huge buffers */
53 #define LONG_STRING     1024   /**< \brief Long buffers */
54 #define STRING          256    /**< \brief Usual buffers */
55
56 #define NONULL(x)       (x ? x : "")  /**< \brief replace \c NULL strings
57                                                   with emtpy strings */
58 #define ISSPACE(c)      isspace((unsigned char)c) /**< \brief safe isspace */
59
60
61 extern unsigned char const __m_strdigits[128];
62 extern signed char const __m_b64digits[128];
63 extern char const __m_b64chars[64];
64
65 extern char const __m_b36chars_lower[36];
66 extern char const __m_b36chars_upper[36];
67
68 /****************************************************************************/
69 /* conversions                                                              */
70 /****************************************************************************/
71
72 static inline int hexval(int c) {
73     return !(c & ~127) && __m_strdigits[c] < 16 ? __m_strdigits[c] : -1;
74 }
75
76 static inline int base64val(int c) {
77     return (c & ~127) ? -1 : __m_b64digits[c];
78 }
79
80 static inline void m_strtolower(char *p) {
81     for (; *p; p++)
82         *p = tolower((unsigned char)*p);
83 }
84
85 static inline int ascii_toupper(int c) {
86     if ('a' <= c && c <= 'z')
87         return c & ~32;
88
89     return c;
90 }
91
92 static inline int ascii_tolower(int c) {
93     if ('A' <= c && c <= 'Z')
94         return c | 32;
95
96     return c;
97 }
98
99 /****************************************************************************/
100 /* length related                                                           */
101 /****************************************************************************/
102
103 static inline int m_strisempty(const char *s) {
104     return !s || !*s;
105 }
106
107 static inline ssize_t m_strlen(const char *s) {
108     return s ? strlen(s) : 0;
109 }
110
111 static inline ssize_t m_strnlen(const char *s, ssize_t n) {
112     const char *p = memchr(s, '\0', n);
113     return p ? p - s : n;
114 }
115
116 /****************************************************************************/
117 /* comparisons                                                              */
118 /****************************************************************************/
119
120 static inline int m_strcmp(const char *a, const char *b) {
121     return strcmp(NONULL(a), NONULL(b));
122 }
123
124 static inline int m_strcasecmp(const char *a, const char *b) {
125     return strcasecmp(NONULL(a), NONULL(b));
126 }
127
128 static inline int m_strncmp(const char *a, const char *b, ssize_t n) {
129     return strncmp(NONULL(a), NONULL(b), n);
130 }
131
132 static inline int m_strncasecmp(const char *a, const char *b, ssize_t n) {
133     return strncasecmp(NONULL(a), NONULL(b), n);
134 }
135
136 int ascii_strcasecmp(const char *a, const char *b);
137 int ascii_strncasecmp(const char *a, const char *b, ssize_t n);
138
139 /****************************************************************************/
140 /* making copies                                                            */
141 /****************************************************************************/
142
143 static inline char *m_strdup(const char *s) {
144     ssize_t len = m_strlen(s);
145     return len ? p_dup(s, len + 1) : NULL;
146 }
147
148 static inline char *m_substrdup(const char *s, const char *end) {
149     return p_dupstr(s, end ? end - s : m_strlen(s));
150 }
151
152 static inline char *m_strreplace(char **p, const char *s) {
153     p_delete(p);
154     return (*p = m_strdup(s));
155 }
156
157 static inline ssize_t m_strputc(char *dst, ssize_t n, int c) {
158     if (n > 1) {
159         dst[0] = c;
160         dst[1] = '\0';
161     }
162     return 1;
163 }
164
165 ssize_t m_strcpy(char *dst, ssize_t n, const char *src);
166 ssize_t m_strncpy(char *dst, ssize_t n, const char *src, ssize_t l);
167
168 static inline ssize_t m_strcat(char *dst, ssize_t n, const char *src) {
169     ssize_t dlen = m_strnlen(dst, n - 1);
170     return dlen + m_strcpy(dst + dlen, n - dlen, src);
171 }
172
173 static inline ssize_t
174 m_strncat(char *dst, ssize_t n, const char *src, ssize_t l) {
175     ssize_t dlen = m_strnlen(dst, n - 1);
176     return dlen + m_strncpy(dst + dlen, n - dlen, src, l);
177 }
178
179 /****************************************************************************/
180 /* parsing related                                                          */
181 /****************************************************************************/
182
183 static inline const char *m_strchrnul(const char *s, int c) {
184     while (*s && *s != c)
185         s++;
186     return s;
187 }
188
189 static inline const char *m_strnextsp(const char *s) {
190     while (*s && !isspace((unsigned char)*s))
191         s++;
192     return s;
193 }
194
195 static inline const char *skipspaces(const char *s) {
196     while (*s && isspace((unsigned char)*s))
197         s++;
198     return s;
199 }
200 static inline char *vskipspaces(const char *s) {
201     return (char *)skipspaces(s);
202 }
203
204 char *m_strrtrim(char *s);
205
206 /****************************************************************************/
207 /* search                                                                   */
208 /****************************************************************************/
209
210 const char *
211 m_stristrn(const char *haystack, const char *needle, ssize_t nlen);
212
213 static inline const char *
214 m_stristr(const char *haystack, const char *needle) {
215     return m_stristrn(haystack, needle, m_strlen(needle));
216 }
217
218 #endif /* MUTT_LIB_LIB_STR_H */