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