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 /** \addtogroup mutt_strings */
24 * \brief Madmutt string API module implementation.
25 * \author Pierre Habouzit <madcoder@debian.org>
30 #ifndef __doxygen_skip__
32 unsigned char const __m_strdigits[128] = {
33 XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
34 XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
35 XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
36 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, XX, XX, XX, XX, XX, XX,
37 XX, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
38 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, XX, XX, XX, XX, XX,
39 XX, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
40 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, XX, XX, XX, XX, XX,
45 signed char const __m_b64digits[128] = {
46 XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
47 XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
48 XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, 62, XX, XX, XX, 63,
49 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, XX, XX, XX, XX, XX, XX,
50 XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
51 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, XX, XX, XX, XX, XX,
52 XX, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
53 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, XX, XX, XX, XX, XX
57 char const __m_b64chars[64] = {
58 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
59 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
60 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
61 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
65 char const __m_b36chars_lower[36] = {
66 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
67 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
68 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
69 'u', 'v', 'w', 'x', 'y', 'z'
72 char const __m_b36chars_upper[36] = {
73 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
74 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
75 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
76 'U', 'V', 'W', 'X', 'Y', 'Z'
80 /** \brief safe strcpy.
82 * Copies at most <tt>n-1</tt> characters from \c src into \c dst, always
83 * adding a final \c \\0 in \c dst.
85 * \param[in] dst destination buffer.
86 * \param[in] n size of the buffer. Negative sizes are allowed.
87 * \param[in] src source string.
89 * \return \c src \e length. If this value is \>= \c n then the copy was
92 ssize_t m_strcpy(char *dst, ssize_t n, const char *src)
94 ssize_t len = m_strlen(src);
97 ssize_t dlen = MIN(n - 1, len);
98 memcpy(dst, src, dlen);
105 /** \brief safe limited strcpy.
107 * Copies at most min(<tt>n-1</tt>, \c l) characters from \c src into \c dst,
108 * always adding a final \c \\0 in \c dst.
110 * \param[in] dst destination buffer.
111 * \param[in] n size of the buffer. Negative sizes are allowed.
112 * \param[in] src source string.
113 * \param[in] l maximum number of chars to copy.
115 * \return minimum of \c src \e length and \c l.
117 ssize_t m_strncpy(char *dst, ssize_t n, const char *src, ssize_t l)
119 ssize_t len = MIN(m_strlen(src), l);
122 ssize_t dlen = MIN(n - 1, len);
123 memcpy(dst, src, dlen);
130 char *m_strrtrim(char *s)
132 ssize_t len = m_strlen(s);
134 while (len > 1 && ISSPACE(s[len - 1]))
140 const char *m_stristrn(const char *haystack, const char *needle, ssize_t nlen)
147 nc = tolower(*needle);
149 int c = tolower(*haystack);
157 /* compare the rest of needle */
163 c = tolower(haystack[i]);
164 if (c != tolower(needle[i]))
173 /** \brief \c NULL resistant strcasecmp.
174 * \param[in] a the first string.
175 * \param[in] b the second string.
176 * \return <tt>strcasecmp(a, b)</tt>, and treats \c NULL strings like \c ""
177 * ones, as if we were in the C locale.
179 int ascii_strcasecmp(const char *a, const char *b)
190 if ((i = ascii_tolower(*a++) - ascii_tolower(*b++)))
197 /** \brief \c NULL resistant strncasecmp.
198 * \param[in] a the first string.
199 * \param[in] b the second string.
200 * \param[in] n the number of maximum chars to compare.
201 * \return <tt>strncasecmp(a, b)</tt>, and treats \c NULL strings like \c ""
202 * ones, as if we were in the C locale.
204 int ascii_strncasecmp(const char *a, const char *b, ssize_t n)
213 while ((*a || *b) && n > 0) {
215 if ((i = ascii_tolower(*a++) - ascii_tolower(*b++)))