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 #define HUGE_STRING 5120
28 #define LONG_STRING 1024
30 #define SHORT_STRING 128
32 #define NONULL(x) (x?x:"")
33 #define ISSPACE(c) isspace((unsigned char)c)
35 extern unsigned char const __m_strdigits[128];
36 extern signed char const __m_b64digits[128];
37 extern char const __m_b64chars[64];
39 extern char const __m_b36chars_lower[36];
40 extern char const __m_b36chars_upper[36];
42 /****************************************************************************/
44 /****************************************************************************/
46 static inline int hexval(int c) {
47 return !(c & ~127) && __m_strdigits[c] < 16 ? __m_strdigits[c] : -1;
50 static inline int base64val(int c) {
51 return (c & ~127) ? -1 : __m_b64digits[c];
54 static inline void m_strtolower(char *p) {
56 *p = tolower((unsigned char)*p);
59 static inline int ascii_toupper(int c) {
60 if ('a' <= c && c <= 'z')
66 static inline int ascii_tolower(int c) {
67 if ('A' <= c && c <= 'Z')
73 /****************************************************************************/
75 /****************************************************************************/
77 static inline int m_strisempty(const char *s) {
81 static inline ssize_t m_strlen(const char *s) {
82 return s ? strlen(s) : 0;
85 static inline ssize_t m_strnlen(const char *s, ssize_t n) {
86 const char *p = memchr(s, '\0', n);
90 /****************************************************************************/
92 /****************************************************************************/
94 static inline int m_strcmp(const char *a, const char *b) {
95 return strcmp(NONULL(a), NONULL(b));
98 static inline int m_strcasecmp(const char *a, const char *b) {
99 return strcasecmp(NONULL(a), NONULL(b));
102 static inline int m_strncmp(const char *a, const char *b, ssize_t n) {
103 return strncmp (NONULL(a), NONULL(b), n);
106 static inline int m_strncasecmp(const char *a, const char *b, ssize_t n) {
107 return strncasecmp(NONULL(a), NONULL(b), n);
110 int ascii_strcasecmp(const char *a, const char *b);
111 int ascii_strncasecmp(const char *a, const char *b, ssize_t n);
113 /****************************************************************************/
115 /****************************************************************************/
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;
122 static inline char *m_substrdup(const char *s, const char *end) {
123 return p_dupstr(s, end ? end - s : m_strlen(s));
126 static inline char *m_strreplace(char **p, const char *s) {
128 return (*p = m_strdup(s));
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);
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);
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);
145 /****************************************************************************/
146 /* parsing related */
147 /****************************************************************************/
149 static inline const char *m_strchrnul(const char *s, int c) {
150 while (*s && *s != c)
155 static inline const char *m_strnextsp(const char *s) {
156 while (*s && !isspace((unsigned char)*s))
161 static inline const char *skipspaces(const char *s) {
162 while (*s && isspace((unsigned char)*s))
166 static inline char *vskipspaces(const char *s) {
167 return (char *)skipspaces(s);
170 char *m_strrtrim(char *s);
172 /****************************************************************************/
174 /****************************************************************************/
177 m_stristrn(const char *haystack, const char *needle, ssize_t nlen);
179 static inline const char *
180 m_stristr(const char *haystack, const char *needle) {
181 return m_stristrn(haystack, needle, m_strlen(needle));
184 #endif /* MUTT_LIB_LIB_STR_H */