|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2013 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include <stdlib.h> |
|
30 #include <string.h> |
|
31 #include <stdarg.h> |
|
32 |
|
33 #include "string.h" |
|
34 #include "allocator.h" |
|
35 |
|
36 sstr_t sstr(char *cstring) { |
|
37 sstr_t string; |
|
38 string.ptr = cstring; |
|
39 string.length = strlen(cstring); |
|
40 return string; |
|
41 } |
|
42 |
|
43 sstr_t sstrn(char *cstring, size_t length) { |
|
44 sstr_t string; |
|
45 string.ptr = cstring; |
|
46 string.length = length; |
|
47 return string; |
|
48 } |
|
49 |
|
50 size_t sstrnlen(size_t n, sstr_t s, ...) { |
|
51 va_list ap; |
|
52 size_t size = s.length; |
|
53 va_start(ap, s); |
|
54 |
|
55 for (size_t i = 1 ; i < n ; i++) { |
|
56 sstr_t str = va_arg(ap, sstr_t); |
|
57 size += str.length; |
|
58 } |
|
59 va_end(ap); |
|
60 |
|
61 return size; |
|
62 } |
|
63 |
|
64 sstr_t sstrncat(sstr_t s, size_t n, sstr_t c1, ...) { |
|
65 va_list ap; |
|
66 va_start(ap, c1); |
|
67 s.ptr[0] = 0; |
|
68 |
|
69 size_t len = s.length; |
|
70 size_t cplen = c1.length > len ? len : c1.length; |
|
71 char *ptr = s.ptr; |
|
72 |
|
73 memcpy(ptr, c1.ptr, cplen); |
|
74 len -= cplen; |
|
75 ptr += cplen; |
|
76 for (size_t i = 1 ; i < n ; i++) { |
|
77 sstr_t str = va_arg (ap, sstr_t); |
|
78 cplen = str.length > len ? len : str.length; |
|
79 if(cplen <= 0) { |
|
80 va_end(ap); |
|
81 return s; |
|
82 } |
|
83 memcpy(ptr, str.ptr, cplen); |
|
84 len -= cplen; |
|
85 ptr += cplen; |
|
86 } |
|
87 va_end(ap); |
|
88 s.length = ptr - s.ptr; |
|
89 |
|
90 return s; |
|
91 } |
|
92 |
|
93 sstr_t sstrsubs(sstr_t s, size_t start) { |
|
94 return sstrsubsl (s, start, s.length-start); |
|
95 } |
|
96 |
|
97 sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) { |
|
98 sstr_t new_sstr; |
|
99 if (start >= s.length) { |
|
100 return s; |
|
101 } |
|
102 if (length > s.length-start) { |
|
103 length = s.length-start; |
|
104 } |
|
105 new_sstr.ptr = &s.ptr[start]; |
|
106 new_sstr.length = length; |
|
107 return new_sstr; |
|
108 } |
|
109 |
|
110 sstr_t sstrchr(sstr_t s, int c) { |
|
111 for(size_t i=0;i<s.length;i++) { |
|
112 if(s.ptr[i] == c) { |
|
113 return sstrsubs(s, i); |
|
114 } |
|
115 } |
|
116 sstr_t n; |
|
117 n.ptr = NULL; |
|
118 n.length = 0; |
|
119 return n; |
|
120 } |
|
121 |
|
122 sstr_t sstrrchr(sstr_t s, int c) { |
|
123 if (s.length > 0) { |
|
124 for(size_t i=s.length;i>0;i--) { |
|
125 if(s.ptr[i-1] == c) { |
|
126 return sstrsubs(s, i-1); |
|
127 } |
|
128 } |
|
129 } |
|
130 sstr_t n; |
|
131 n.ptr = NULL; |
|
132 n.length = 0; |
|
133 return n; |
|
134 } |
|
135 |
|
136 sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n) { |
|
137 return sstrsplit_a(ucx_default_allocator(), s, d, n); |
|
138 } |
|
139 |
|
140 sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t s, sstr_t d, size_t *n) { |
|
141 if (s.length == 0 || d.length == 0) { |
|
142 *n = -1; |
|
143 return NULL; |
|
144 } |
|
145 |
|
146 sstr_t* result; |
|
147 size_t nmax = *n; |
|
148 *n = 1; |
|
149 |
|
150 /* special case: exact match - no processing needed */ |
|
151 if (sstrcmp(s, d) == 0) { |
|
152 *n = 0; |
|
153 return NULL; |
|
154 } |
|
155 sstr_t sv = sstrdup(s); |
|
156 if (sv.length == 0) { |
|
157 *n = -2; |
|
158 return NULL; |
|
159 } |
|
160 |
|
161 for (size_t i = 0 ; i < s.length ; i++) { |
|
162 if (sv.ptr[i] == d.ptr[0]) { |
|
163 _Bool match = 1; |
|
164 for (size_t j = 1 ; j < d.length ; j++) { |
|
165 if (j+i < s.length) { |
|
166 match &= (sv.ptr[i+j] == d.ptr[j]); |
|
167 } else { |
|
168 match = 0; |
|
169 break; |
|
170 } |
|
171 } |
|
172 if (match) { |
|
173 (*n)++; |
|
174 for (size_t j = 0 ; j < d.length ; j++) { |
|
175 sv.ptr[i+j] = 0; |
|
176 } |
|
177 i += d.length; |
|
178 } |
|
179 } |
|
180 if ((*n) == nmax) break; |
|
181 } |
|
182 result = (sstr_t*) allocator->malloc(allocator->pool, sizeof(sstr_t)*(*n)); |
|
183 |
|
184 if (result) { |
|
185 char *pptr = sv.ptr; |
|
186 for (size_t i = 0 ; i < *n ; i++) { |
|
187 size_t l = strlen(pptr); |
|
188 char* ptr = (char*) allocator->malloc(allocator->pool, l + 1); |
|
189 memcpy(ptr, pptr, l); |
|
190 ptr[l] = 0; |
|
191 |
|
192 result[i] = sstrn(ptr, l); |
|
193 pptr += l + d.length; |
|
194 } |
|
195 } else { |
|
196 *n = -2; |
|
197 } |
|
198 |
|
199 free(sv.ptr); |
|
200 |
|
201 return result; |
|
202 } |
|
203 |
|
204 int sstrcmp(sstr_t s1, sstr_t s2) { |
|
205 if (s1.length == s2.length) { |
|
206 return memcmp(s1.ptr, s2.ptr, s1.length); |
|
207 } else if (s1.length > s2.length) { |
|
208 return 1; |
|
209 } else { |
|
210 return -1; |
|
211 } |
|
212 } |
|
213 |
|
214 int sstrcasecmp(sstr_t s1, sstr_t s2) { |
|
215 if (s1.length == s2.length) { |
|
216 #ifdef _WIN32 |
|
217 return _strnicmp(s1.ptr, s2.ptr, s1.length); |
|
218 #else |
|
219 return strncasecmp(s1.ptr, s2.ptr, s1.length); |
|
220 #endif |
|
221 } else if (s1.length > s2.length) { |
|
222 return 1; |
|
223 } else { |
|
224 return -1; |
|
225 } |
|
226 } |
|
227 |
|
228 sstr_t sstrdup(sstr_t s) { |
|
229 return sstrdup_a(ucx_default_allocator(), s); |
|
230 } |
|
231 |
|
232 sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t s) { |
|
233 sstr_t newstring; |
|
234 newstring.ptr = (char*)allocator->malloc(allocator->pool, s.length + 1); |
|
235 if (newstring.ptr) { |
|
236 newstring.length = s.length; |
|
237 newstring.ptr[newstring.length] = 0; |
|
238 |
|
239 memcpy(newstring.ptr, s.ptr, s.length); |
|
240 } else { |
|
241 newstring.length = 0; |
|
242 } |
|
243 |
|
244 return newstring; |
|
245 } |
|
246 |
|
247 sstr_t sstrtrim(sstr_t string) { |
|
248 sstr_t newstr = string; |
|
249 if (string.length == 0) { |
|
250 return newstr; |
|
251 } |
|
252 |
|
253 size_t i; |
|
254 for(i=0;i<string.length;i++) { |
|
255 char c = string.ptr[i]; |
|
256 if(c > 32) { |
|
257 break; |
|
258 } |
|
259 } |
|
260 newstr.ptr = &string.ptr[i]; |
|
261 newstr.length = string.length - i; |
|
262 |
|
263 if(newstr.length == 0) { |
|
264 return newstr; |
|
265 } |
|
266 |
|
267 i = newstr.length - 1; |
|
268 for(;;) { |
|
269 char c = newstr.ptr[i]; |
|
270 if(c > 32) { |
|
271 break; |
|
272 } |
|
273 if(i > 0) { |
|
274 i--; |
|
275 } else { |
|
276 break; |
|
277 } |
|
278 } |
|
279 newstr.length = i + 1; |
|
280 |
|
281 return newstr; |
|
282 } |
|
283 |
|
284 int sstrprefix(sstr_t string, sstr_t prefix) { |
|
285 if (string.length == 0) { |
|
286 return prefix.length == 0; |
|
287 } |
|
288 if (prefix.length == 0) { |
|
289 return 1; |
|
290 } |
|
291 |
|
292 if (prefix.length > string.length) { |
|
293 return 0; |
|
294 } else { |
|
295 return memcmp(string.ptr, prefix.ptr, prefix.length) == 0; |
|
296 } |
|
297 } |
|
298 |
|
299 int sstrsuffix(sstr_t string, sstr_t suffix) { |
|
300 if (string.length == 0) { |
|
301 return suffix.length == 0; |
|
302 } |
|
303 if (suffix.length == 0) { |
|
304 return 1; |
|
305 } |
|
306 |
|
307 if (suffix.length > string.length) { |
|
308 return 0; |
|
309 } else { |
|
310 return memcmp(string.ptr+string.length-suffix.length, |
|
311 suffix.ptr, suffix.length) == 0; |
|
312 } |
|
313 } |