drop the horrible list2_t for good.
[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 #define HUGE_STRING     5120
28 #define LONG_STRING     1024
29 #define STRING          256
30 #define SHORT_STRING    128
31
32 #define NONULL(x) (x?x:"")
33 #define ISSPACE(c) isspace((unsigned char)c)
34
35 extern unsigned char const __m_strdigits[128];
36 extern signed char const __m_b64digits[128];
37 extern char const __m_b64chars[64];
38
39 extern char const __m_b36chars_lower[36];
40 extern char const __m_b36chars_upper[36];
41
42 /****************************************************************************/
43 /* conversions                                                              */
44 /****************************************************************************/
45
46 static inline int hexval(int c) {
47     return !(c & ~127) && __m_strdigits[c] < 16 ? __m_strdigits[c] : -1;
48 }
49
50 static inline int base64val(int c) {
51     return (c & ~127) ? -1 : __m_b64digits[c];
52 }
53
54 static inline void m_strtolower(char *p) {
55     for (; *p; p++)
56         *p = tolower((unsigned char)*p);
57 }
58
59 static inline int ascii_toupper(int c) {
60     if ('a' <= c && c <= 'z')
61         return c & ~32;
62
63     return c;
64 }
65
66 static inline int ascii_tolower(int c) {
67     if ('A' <= c && c <= 'Z')
68         return c | 32;
69
70     return c;
71 }
72
73 /****************************************************************************/
74 /* length related                                                           */
75 /****************************************************************************/
76
77 static inline int m_strisempty(const char *s) {
78     return !s || !*s;
79 }
80
81 static inline ssize_t m_strlen(const char *s) {
82     return s ? strlen(s) : 0;
83 }
84
85 static inline ssize_t m_strnlen(const char *s, ssize_t n) {
86     const char *p = memchr(s, '\0', n);
87     return p ? p - s : n;
88 }
89
90 /****************************************************************************/
91 /* comparisons                                                              */
92 /****************************************************************************/
93
94 static inline int m_strcmp(const char *a, const char *b) {
95     return strcmp(NONULL(a), NONULL(b));
96 }
97
98 static inline int m_strcasecmp(const char *a, const char *b) {
99     return strcasecmp(NONULL(a), NONULL(b));
100 }
101
102 static inline int m_strncmp(const char *a, const char *b, ssize_t n) {
103     return strncmp (NONULL(a), NONULL(b), n);
104 }
105
106 static inline int m_strncasecmp(const char *a, const char *b, ssize_t n) {
107     return strncasecmp(NONULL(a), NONULL(b), n);
108 }
109
110 int ascii_strcasecmp(const char *a, const char *b);
111 int ascii_strncasecmp(const char *a, const char *b, ssize_t n);
112
113 /****************************************************************************/
114 /* making copies                                                            */
115 /****************************************************************************/
116
117 static inline char *m_strdup(const char *s) {
118     ssize_t len = m_strlen(s);
119     return len ? p_dup(s, len + 1) : NULL;
120 }
121
122 static inline char *m_substrdup(const char *s, const char *end) {
123     return p_dupstr(s, end ? end - s : m_strlen(s));
124 }
125
126 static inline char *m_strreplace(char **p, const char *s) {
127     p_delete(p);
128     return (*p = m_strdup(s));
129 }
130
131 ssize_t m_strcpy(char *dst, ssize_t n, const char *src);
132 ssize_t m_strncpy(char *dst, ssize_t n, const char *src, ssize_t l);
133
134 static inline ssize_t m_strcat(char *dst, ssize_t n, const char *src) {
135     ssize_t dlen = m_strnlen(dst, n - 1);
136     return dlen + m_strcpy(dst + dlen, n - dlen, src);
137 }
138
139 static inline ssize_t
140 m_strncat(char *dst, ssize_t n, const char *src, ssize_t l) {
141     ssize_t dlen = m_strnlen(dst, n - 1);
142     return dlen + m_strncpy(dst + dlen, n - dlen, src, l);
143 }
144
145 /****************************************************************************/
146 /* parsing related                                                          */
147 /****************************************************************************/
148
149 static inline const char *m_strchrnul(const char *s, int c) {
150     while (*s && *s != c)
151         s++;
152     return s;
153 }
154
155 static inline const char *m_strnextsp(const char *s) {
156     while (*s && !isspace((unsigned char)*s))
157         s++;
158     return s;
159 }
160
161 static inline const char *skipspaces(const char *s) {
162     while (*s && isspace((unsigned char)*s))
163         s++;
164     return s;
165 }
166 static inline char *vskipspaces(const char *s) {
167     return (char *)skipspaces(s);
168 }
169
170 char *m_strrtrim(char *s);
171
172 /****************************************************************************/
173 /* search                                                                   */
174 /****************************************************************************/
175
176 const char *
177 m_stristrn(const char *haystack, const char *needle, ssize_t nlen);
178
179 static inline const char *
180 m_stristr(const char *haystack, const char *needle) {
181     return m_stristrn(haystack, needle, m_strlen(needle));
182 }
183
184 #endif /* MUTT_LIB_LIB_STR_H */