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
27 /** \defgroup mutt_strings Madmutt string API
29 * This module contains the prefered string API to be used in Madmutt.
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
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
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.
50 * \brief Madmutt string API header.
51 * \author Pierre Habouzit <madcoder@debian.org>
54 #define HUGE_STRING 5120 /**< \brief Huge buffers */
55 #define LONG_STRING 1024 /**< \brief Long buffers */
56 #define STRING 256 /**< \brief Usual buffers */
58 #define NONULL(x) (x ? x : "") /**< \brief replace \c NULL strings
60 #define ISSPACE(c) isspace((unsigned char)c) /**< \brief safe isspace */
62 /** \brief Convert ascii digits into ints.
64 * Convert ascii digits into its integer value in base 36.
65 * Non convertible values are converted to 255.
67 * Translating a digit \c c into its numerical value in base \c x is just doing:
69 * return (c & ~127) && __m_strdigits[c] < x ? __m_strdigits[c] : -1;
72 extern unsigned char const __m_strdigits[128];
73 /** \brief Convert an ascii base64 digit into ints.
75 * Convert an a char base64 digit into its int value.
76 * Used by base64val(). Unlike #__m_strdigits, the invalid values are set to
79 extern signed char const __m_b64digits[128];
81 /** \brief Convert ints from 0–64 into the corresponding base64 digit. */
82 extern char const __m_b64chars[64];
83 /** \brief Convert ints from 0–36 into a base36 lowercase digit. */
84 extern char const __m_b36chars_lower[36];
85 /** \brief Convert ints from 0–36 into a base36 uppercase digit. */
86 extern char const __m_b36chars_upper[36];
88 /****************************************************************************/
90 /****************************************************************************/
92 static inline int hexval(int c) {
93 return !(c & ~127) && __m_strdigits[c] < 16 ? __m_strdigits[c] : -1;
96 static inline int base64val(int c) {
97 return (c & ~127) ? -1 : __m_b64digits[c];
100 static inline void m_strtolower(char *p) {
102 *p = tolower((unsigned char)*p);
105 static inline int ascii_toupper(int c) {
106 if ('a' <= c && c <= 'z')
112 static inline int ascii_tolower(int c) {
113 if ('A' <= c && c <= 'Z')
119 /****************************************************************************/
121 /****************************************************************************/
123 static inline int m_strisempty(const char *s) {
127 static inline ssize_t m_strlen(const char *s) {
128 return s ? strlen(s) : 0;
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;
136 /****************************************************************************/
138 /****************************************************************************/
140 static inline int m_strcmp(const char *a, const char *b) {
141 return strcmp(NONULL(a), NONULL(b));
144 static inline int m_strcasecmp(const char *a, const char *b) {
145 return strcasecmp(NONULL(a), NONULL(b));
148 static inline int m_strncmp(const char *a, const char *b, ssize_t n) {
149 return strncmp(NONULL(a), NONULL(b), n);
152 static inline int m_strncasecmp(const char *a, const char *b, ssize_t n) {
153 return strncasecmp(NONULL(a), NONULL(b), n);
156 int ascii_strcasecmp(const char *a, const char *b);
157 int ascii_strncasecmp(const char *a, const char *b, ssize_t n);
159 /****************************************************************************/
161 /****************************************************************************/
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;
168 static inline char *m_substrdup(const char *s, const char *end) {
169 return p_dupstr(s, end ? end - s : m_strlen(s));
172 static inline char *m_strreplace(char **p, const char *s) {
174 return (*p = m_strdup(s));
177 static inline ssize_t m_strputc(char *dst, ssize_t n, int c) {
185 ssize_t m_strcpy(char *dst, ssize_t n, const char *src)
186 __attribute__((nonnull(1)));
188 ssize_t m_strncpy(char *dst, ssize_t n, const char *src, ssize_t l)
189 __attribute__((nonnull(1)));
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);
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);
202 /****************************************************************************/
203 /* parsing related */
204 /****************************************************************************/
206 static inline const char *m_strchrnul(const char *s, int c) {
207 while (*s && *s != c)
212 static inline const char *m_strnextsp(const char *s) {
213 while (*s && !isspace((unsigned char)*s))
218 static inline const char *skipspaces(const char *s) {
219 while (*s && isspace((unsigned char)*s))
223 static inline char *vskipspaces(const char *s) {
224 return (char *)skipspaces(s);
227 char *m_strrtrim(char *s);
229 /****************************************************************************/
231 /****************************************************************************/
234 m_stristrn(const char *haystack, const char *needle, ssize_t nlen);
236 static inline const char *
237 m_stristr(const char *haystack, const char *needle) {
238 return m_stristrn(haystack, needle, m_strlen(needle));
242 #endif /* MUTT_LIB_LIB_STR_H */