-
Notifications
You must be signed in to change notification settings - Fork 4
/
str.c
251 lines (219 loc) · 8.29 KB
/
str.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/****************************************************************************/
/* pfixtools: a collection of postfix related tools */
/* ~~~~~~~~~ */
/* ______________________________________________________________________ */
/* */
/* Redistribution and use in source and binary forms, with or without */
/* modification, are permitted provided that the following conditions */
/* are met: */
/* */
/* 1. Redistributions of source code must retain the above copyright */
/* notice, this list of conditions and the following disclaimer. */
/* 2. Redistributions in binary form must reproduce the above copyright */
/* notice, this list of conditions and the following disclaimer in */
/* the documentation and/or other materials provided with the */
/* distribution. */
/* 3. The names of its contributors may not be used to endorse or promote */
/* products derived from this software without specific prior written */
/* permission. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY */
/* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE */
/* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
/* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE */
/* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR */
/* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF */
/* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
/* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, */
/* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE */
/* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
/* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* */
/* Copyright (c) 2006-2014 the Authors */
/* see AUTHORS and source files for details */
/****************************************************************************/
/** \addtogroup mutt_strings */
/*@{*/
/** \file str.c
* \brief String API module implementation.
*/
#include "str.h"
#ifndef __doxygen_skip__
#define XX 255
unsigned char const __m_strdigits[128] = {
XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, XX, XX, XX, XX, XX, XX,
XX, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, XX, XX, XX, XX, XX,
XX, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, XX, XX, XX, XX, XX,
};
#undef XX
#define XX -1
signed char const __m_b64digits[128] = {
XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, 62, XX, XX, XX, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, XX, XX, XX, XX, XX, XX,
XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, XX, XX, XX, XX, XX,
XX, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, XX, XX, XX, XX, XX
};
#undef XX
char const __m_b64chars[64] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', '+', '/'
};
char const __m_b36chars_lower[36] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z'
};
char const __m_b36chars_upper[36] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z'
};
#endif
/** \brief safe strcpy.
*
* Copies at most <tt>n-1</tt> characters from \c src into \c dst, always
* adding a final \c \\0 in \c dst.
*
* \param[in] dst destination buffer.
* \param[in] n size of the buffer. Negative sizes are allowed.
* \param[in] src source string.
*
* \return \c src \e length. If this value is \>= \c n then the copy was
* truncated.
*/
ssize_t m_strcpy(char *dst, ssize_t n, const char *src)
{
ssize_t len = m_strlen(src);
if (n > 0) {
ssize_t dlen = MIN(n - 1, len);
xmemcpy(dst, src, dlen);
dst[dlen] = '\0';
}
return len;
}
/** \brief safe limited strcpy.
*
* Copies at most min(<tt>n-1</tt>, \c l) characters from \c src into \c dst,
* always adding a final \c \\0 in \c dst.
*
* \param[in] dst destination buffer.
* \param[in] n size of the buffer. Negative sizes are allowed.
* \param[in] src source string.
* \param[in] l maximum number of chars to copy.
*
* \return minimum of \c src \e length and \c l.
*/
ssize_t m_strncpy(char *dst, ssize_t n, const char *src, ssize_t l)
{
ssize_t len = m_strnlen(src, l);
if (n > 0) {
ssize_t dlen = MIN(n - 1, len);
xmemcpy(dst, src, dlen);
dst[dlen] = '\0';
}
return len;
}
char *m_strrtrim(char *s)
{
ssize_t len = m_strlen(s);
while (len > 1 && isspace((unsigned char)s[len - 1]))
s[--len] = '\0';
return s + len;
}
const char *m_stristrn(const char *haystack, const char *needle, ssize_t nlen)
{
int nc;
if (!nlen)
return haystack;
nc = tolower(*needle);
for (;;) {
int c = tolower(*haystack);
if (c != nc) {
if (c == '\0')
return NULL;
} else {
ssize_t i;
/* compare the rest of needle */
for (i = 1;; i++) {
if (i == nlen)
return haystack;
if (c == '\0')
return NULL;
c = tolower(haystack[i]);
if (c != tolower(needle[i]))
break;
}
}
haystack++;
}
}
/** \brief \c NULL resistant strcasecmp.
* \param[in] a the first string.
* \param[in] b the second string.
* \return <tt>strcasecmp(a, b)</tt>, and treats \c NULL strings like \c ""
* ones, as if we were in the C locale.
*/
int ascii_strcasecmp(const char *a, const char *b)
{
if (a == b)
return 0;
if (!a)
return -1;
if (!b)
return 1;
while (*a || *b) {
int i;
if ((i = ascii_tolower(*a++) - ascii_tolower(*b++)))
return i;
}
return 0;
}
/** \brief \c NULL resistant strncasecmp.
* \param[in] a the first string.
* \param[in] b the second string.
* \param[in] n the number of maximum chars to compare.
* \return <tt>strncasecmp(a, b)</tt>, and treats \c NULL strings like \c ""
* ones, as if we were in the C locale.
*/
int ascii_strncasecmp(const char *a, const char *b, ssize_t n)
{
if (a == b)
return 0;
if (!a)
return -1;
if (!b)
return 1;
while ((*a || *b) && n > 0) {
int i;
if ((i = ascii_tolower(*a++) - ascii_tolower(*b++)))
return i;
n--;
}
return 0;
}
int m_strcmp(const char* a, const char* b)
{
if (a == NULL) {
a = "";
}
if (b == NULL) {
b = "";
}
return strcmp(a, b);
}
/*@}*/
/* vim:set et sw=4 sts=4 sws=4: */