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.
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.
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,
17 * Copyright © 2006 Pierre Habouzit
20 * Copyright notice from original mutt:
21 * Copyright (C) 2001 Thomas Roessler <roessler@does-not-exist.org>
24 #ifndef MUTT_LIB_LIB_STR_H
25 #define MUTT_LIB_LIB_STR_H
28 * \brief Madmutt string API.
30 * \author Pierre Habouzit <madcoder@debian.org>
32 * This header contains the prefered string API to be used in Madmutt.
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
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
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.
52 #define HUGE_STRING 5120 /**< \brief Huge buffers */
53 #define LONG_STRING 1024 /**< \brief Long buffers */
54 #define STRING 256 /**< \brief Usual buffers */
56 #define NONULL(x) (x ? x : "") /**< \brief replace \c NULL strings
58 #define ISSPACE(c) isspace((unsigned char)c) /**< \brief safe isspace */
61 extern unsigned char const __m_strdigits[128];
62 extern signed char const __m_b64digits[128];
63 extern char const __m_b64chars[64];
65 extern char const __m_b36chars_lower[36];
66 extern char const __m_b36chars_upper[36];
68 /****************************************************************************/
70 /****************************************************************************/
72 static inline int hexval(int c) {
73 return !(c & ~127) && __m_strdigits[c] < 16 ? __m_strdigits[c] : -1;
76 static inline int base64val(int c) {
77 return (c & ~127) ? -1 : __m_b64digits[c];
80 static inline void m_strtolower(char *p) {
82 *p = tolower((unsigned char)*p);
85 static inline int ascii_toupper(int c) {
86 if ('a' <= c && c <= 'z')
92 static inline int ascii_tolower(int c) {
93 if ('A' <= c && c <= 'Z')
99 /****************************************************************************/
101 /****************************************************************************/
103 static inline int m_strisempty(const char *s) {
107 static inline ssize_t m_strlen(const char *s) {
108 return s ? strlen(s) : 0;
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;
116 /****************************************************************************/
118 /****************************************************************************/
120 static inline int m_strcmp(const char *a, const char *b) {
121 return strcmp(NONULL(a), NONULL(b));
124 static inline int m_strcasecmp(const char *a, const char *b) {
125 return strcasecmp(NONULL(a), NONULL(b));
128 static inline int m_strncmp(const char *a, const char *b, ssize_t n) {
129 return strncmp(NONULL(a), NONULL(b), n);
132 static inline int m_strncasecmp(const char *a, const char *b, ssize_t n) {
133 return strncasecmp(NONULL(a), NONULL(b), n);
136 int ascii_strcasecmp(const char *a, const char *b);
137 int ascii_strncasecmp(const char *a, const char *b, ssize_t n);
139 /****************************************************************************/
141 /****************************************************************************/
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;
148 static inline char *m_substrdup(const char *s, const char *end) {
149 return p_dupstr(s, end ? end - s : m_strlen(s));
152 static inline char *m_strreplace(char **p, const char *s) {
154 return (*p = m_strdup(s));
157 static inline ssize_t m_strputc(char *dst, ssize_t n, int c) {
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);
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);
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);
179 /****************************************************************************/
180 /* parsing related */
181 /****************************************************************************/
183 static inline const char *m_strchrnul(const char *s, int c) {
184 while (*s && *s != c)
189 static inline const char *m_strnextsp(const char *s) {
190 while (*s && !isspace((unsigned char)*s))
195 static inline const char *skipspaces(const char *s) {
196 while (*s && isspace((unsigned char)*s))
200 static inline char *vskipspaces(const char *s) {
201 return (char *)skipspaces(s);
204 char *m_strrtrim(char *s);
206 /****************************************************************************/
208 /****************************************************************************/
211 m_stristrn(const char *haystack, const char *needle, ssize_t nlen);
213 static inline const char *
214 m_stristr(const char *haystack, const char *needle) {
215 return m_stristrn(haystack, needle, m_strlen(needle));
218 #endif /* MUTT_LIB_LIB_STR_H */