move debug.c
[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 #include "macros.h"
21 #include "str.h"
22
23 #define XX 255
24 unsigned char const __m_strdigits[128] = {
25     XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
26     XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
27     XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
28      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, XX, XX, XX, XX, XX, XX,
29     XX, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
30     25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, XX, XX, XX, XX, XX,
31     XX, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
32     25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, XX, XX, XX, XX, XX,
33 };
34 #undef XX
35
36 #define XX -1
37 signed char const __m_b64digits[128] = {
38     XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
39     XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
40     XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, 62, XX, XX, XX, 63,
41     52, 53, 54, 55, 56, 57, 58, 59, 60, 61, XX, XX, XX, XX, XX, XX,
42     XX,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
43     15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, XX, XX, XX, XX, XX,
44     XX, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
45     41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, XX, XX, XX, XX, XX
46 };
47 #undef XX
48
49 char const __m_b64chars[64] = {
50     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
51     'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
52     'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
53     't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
54     '8', '9', '+', '/'
55 };
56
57 char const __m_b36chars_lower[36] = {
58     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
59     'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
60     'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
61     'u', 'v', 'w', 'x', 'y', 'z'
62 };
63
64 char const __m_b36chars_upper[36] = {
65     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
66     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
67     'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
68     'U', 'V', 'W', 'X', 'Y', 'Z'
69 };
70
71
72 ssize_t m_strcpy(char *dst, ssize_t n, const char *src)
73 {
74     ssize_t len = m_strlen(src);
75
76     if (dst && n > 0) {
77         ssize_t dlen = MIN(n - 1, len);
78         memcpy(dst, src, dlen);
79         dst[dlen] = '\0';
80     }
81
82     return len;
83 }
84
85 ssize_t m_strncpy(char *dst, ssize_t n, const char *src, ssize_t l)
86 {
87     ssize_t len = MIN(m_strlen(src), l);
88
89     if (dst && n > 0) {
90         ssize_t dlen = MIN(n - 1, len);
91         memcpy(dst, src, dlen);
92         dst[dlen] = '\0';
93     }
94
95     return len;
96 }
97
98 char *m_strrtrim(char *s)
99 {
100     if (s) {
101         char *p = s + m_strlen(s);
102
103         while (p > s && ISSPACE(p[-1])) {
104             *--p = '\0';
105         }
106         return p;
107     }
108
109     return NULL;
110 }
111
112 const char *m_stristrn(const char *haystack, const char *needle, ssize_t nlen)
113 {
114     int nc;
115
116     if (!nlen)
117         return haystack;
118
119     nc = tolower(*needle);
120     for (;;) {
121         int c = tolower(*haystack);
122
123         if (c != nc) {
124             if (c == '\0')
125                 return NULL;
126         } else {
127             ssize_t i;
128
129             /* compare the rest of needle */
130             for (i = 1;; i++) {
131                 if (i == nlen)
132                     return haystack;
133                 if (c == '\0')
134                     return NULL;
135                 c = tolower(haystack[i]);
136                 if (c != tolower(needle[i]))
137                     break;
138             }
139         }
140
141         haystack++;
142     }
143 }