add m_strpad to the string functions.
[apps/madmutt.git] / lib-lib / str.c
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 /** \addtogroup mutt_strings */
21 /*@{*/
22
23 /** \file str.c
24  * \brief Madmutt string API module implementation.
25  * \author Pierre Habouzit <madcoder@debian.org>
26  */
27
28 #include "lib-lib.h"
29
30 #ifndef __doxygen_skip__
31 #define XX 255
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,
41 };
42 #undef XX
43
44 #define XX -1
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
54 };
55 #undef XX
56
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',
62     '8', '9', '+', '/'
63 };
64
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'
70 };
71
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'
77 };
78 #endif
79
80 /** \brief safe strcpy.
81  *
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.
84  *
85  * \param[in]  dst      destination buffer.
86  * \param[in]  n        size of the buffer. Negative sizes are allowed.
87  * \param[in]  src      source string.
88  *
89  * \return \c src \e length. If this value is \>= \c n then the copy was
90  *         truncated.
91  */
92 ssize_t m_strcpy(char *dst, ssize_t n, const char *src)
93 {
94     ssize_t len = m_strlen(src);
95
96     if (n > 0) {
97         ssize_t dlen = MIN(n - 1, len);
98         memcpy(dst, src, dlen);
99         dst[dlen] = '\0';
100     }
101
102     return len;
103 }
104
105 /** \brief safe limited strcpy.
106  *
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.
109  *
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.
114  *
115  * \return minimum of  \c src \e length and \c l.
116  */
117 ssize_t m_strncpy(char *dst, ssize_t n, const char *src, ssize_t l)
118 {
119     ssize_t len = MIN(m_strlen(src), l);
120
121     if (n > 0) {
122         ssize_t dlen = MIN(n - 1, len);
123         memcpy(dst, src, dlen);
124         dst[dlen] = '\0';
125     }
126
127     return len;
128 }
129
130 char *m_strrtrim(char *s)
131 {
132     ssize_t len = m_strlen(s);
133
134     while (len > 1 && ISSPACE(s[len - 1]))
135         s[--len] = '\0';
136
137     return s + len;
138 }
139
140 const char *m_stristrn(const char *haystack, const char *needle, ssize_t nlen)
141 {
142     int nc;
143
144     if (!nlen)
145         return haystack;
146
147     nc = tolower(*needle);
148     for (;;) {
149         int c = tolower(*haystack);
150
151         if (c != nc) {
152             if (c == '\0')
153                 return NULL;
154         } else {
155             ssize_t i;
156
157             /* compare the rest of needle */
158             for (i = 1;; i++) {
159                 if (i == nlen)
160                     return haystack;
161                 if (c == '\0')
162                     return NULL;
163                 c = tolower(haystack[i]);
164                 if (c != tolower(needle[i]))
165                     break;
166             }
167         }
168
169         haystack++;
170     }
171 }
172
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.
178  */
179 int ascii_strcasecmp(const char *a, const char *b)
180 {
181     if (a == b)
182         return 0;
183     if (!a)
184         return -1;
185     if (!b)
186         return 1;
187
188     while (*a || *b) {
189         int i;
190         if ((i = ascii_tolower(*a++) - ascii_tolower(*b++)))
191             return i;
192     }
193
194     return 0;
195 }
196
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.
203  */
204 int ascii_strncasecmp(const char *a, const char *b, ssize_t n)
205 {
206     if (a == b)
207         return 0;
208     if (!a)
209         return -1;
210     if (!b)
211         return 1;
212
213     while ((*a || *b) && n > 0) {
214         int i;
215         if ((i = ascii_tolower(*a++) - ascii_tolower(*b++)))
216             return i;
217         n--;
218     }
219
220     return 0;
221 }
222
223 /*@}*/