ab603da1ab8e760f8f4f250ac757985427c6418a
[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_ME
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 ssize_t m_strncpy(char *dst, ssize_t n, const char *src, ssize_t l)
106 {
107     ssize_t len = MIN(m_strlen(src), l);
108
109     if (n > 0) {
110         ssize_t dlen = MIN(n - 1, len);
111         memcpy(dst, src, dlen);
112         dst[dlen] = '\0';
113     }
114
115     return len;
116 }
117
118 char *m_strrtrim(char *s)
119 {
120     ssize_t len = m_strlen(s);
121
122     while (len > 1 && ISSPACE(s[len - 1]))
123         s[--len] = '\0';
124
125     return s + len;
126 }
127
128 const char *m_stristrn(const char *haystack, const char *needle, ssize_t nlen)
129 {
130     int nc;
131
132     if (!nlen)
133         return haystack;
134
135     nc = tolower(*needle);
136     for (;;) {
137         int c = tolower(*haystack);
138
139         if (c != nc) {
140             if (c == '\0')
141                 return NULL;
142         } else {
143             ssize_t i;
144
145             /* compare the rest of needle */
146             for (i = 1;; i++) {
147                 if (i == nlen)
148                     return haystack;
149                 if (c == '\0')
150                     return NULL;
151                 c = tolower(haystack[i]);
152                 if (c != tolower(needle[i]))
153                     break;
154             }
155         }
156
157         haystack++;
158     }
159 }
160
161 int ascii_strcasecmp(const char *a, const char *b)
162 {
163     int i;
164
165     if (a == b)
166         return 0;
167     if (a == NULL && b)
168         return -1;
169     if (b == NULL && a)
170         return 1;
171
172     for (; *a || *b; a++, b++) {
173         if ((i = ascii_tolower(*a) - ascii_tolower(*b)))
174             return i;
175     }
176
177     return 0;
178 }
179
180 int ascii_strncasecmp (const char *a, const char *b, ssize_t n)
181 {
182     int i, j;
183
184     if (a == b)
185         return 0;
186     if (a == NULL && b)
187         return -1;
188     if (b == NULL && a)
189         return 1;
190
191     for (j = 0; (*a || *b) && j < n; a++, b++, j++) {
192         if ((i = ascii_tolower(*a) - ascii_tolower(*b)))
193             return i;
194     }
195
196     return 0;
197 }
198
199 /*@}*/