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 *s) { |
|
37 sstr_t string; |
|
38 string.ptr = s; |
|
39 string.length = strlen(s); |
|
40 return string; |
|
41 } |
|
42 |
|
43 sstr_t sstrn(char *s, size_t n) { |
|
44 sstr_t string; |
|
45 string.ptr = s; |
|
46 string.length = n; |
|
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 = 0 ; i < n-1 ; 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(size_t n, sstr_t s, 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 = 0 ; i < n-1 ; 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* sstrsplit(sstr_t s, sstr_t d, size_t *n) { |
|
123 if (d.length == 0) { |
|
124 return NULL; |
|
125 } |
|
126 |
|
127 sstr_t* result; |
|
128 size_t nmax = *n; |
|
129 *n = 1; |
|
130 |
|
131 /* special case: exact match - no processing needed */ |
|
132 if (s.length == d.length && strncmp(s.ptr, d.ptr, s.length) == 0) { |
|
133 *n = 0; |
|
134 return NULL; |
|
135 } |
|
136 sstr_t sv = sstrdup(s); |
|
137 |
|
138 for (size_t i = 0 ; i < s.length ; i++) { |
|
139 if (sv.ptr[i] == d.ptr[0]) { |
|
140 _Bool match = 1; |
|
141 for (size_t j = 1 ; j < d.length ; j++) { |
|
142 if (j+i < s.length) { |
|
143 match &= (sv.ptr[i+j] == d.ptr[j]); |
|
144 } else { |
|
145 match = 0; |
|
146 break; |
|
147 } |
|
148 } |
|
149 if (match) { |
|
150 (*n)++; |
|
151 for (size_t j = 0 ; j < d.length ; j++) { |
|
152 sv.ptr[i+j] = 0; |
|
153 } |
|
154 i += d.length; |
|
155 } |
|
156 } |
|
157 if ((*n) == nmax) break; |
|
158 } |
|
159 result = (sstr_t*) malloc(sizeof(sstr_t) * (*n)); |
|
160 |
|
161 char *pptr = sv.ptr; |
|
162 for (size_t i = 0 ; i < *n ; i++) { |
|
163 size_t l = strlen(pptr); |
|
164 char* ptr = (char*) malloc(l + 1); |
|
165 memcpy(ptr, pptr, l); |
|
166 ptr[l] = 0; |
|
167 |
|
168 result[i] = sstrn(ptr, l); |
|
169 pptr += l + d.length; |
|
170 } |
|
171 |
|
172 free(sv.ptr); |
|
173 |
|
174 return result; |
|
175 } |
|
176 |
|
177 int sstrcmp(sstr_t s1, sstr_t s2) { |
|
178 return strncmp(s1.ptr, s2.ptr, s1.length>s2.length ? s2.length: s1.length); |
|
179 } |
|
180 |
|
181 sstr_t sstrdup(sstr_t s) { |
|
182 sstr_t newstring; |
|
183 newstring.ptr = (char*) malloc(s.length + 1); |
|
184 if (newstring.ptr) { |
|
185 newstring.length = s.length; |
|
186 newstring.ptr[newstring.length] = 0; |
|
187 |
|
188 memcpy(newstring.ptr, s.ptr, s.length); |
|
189 } else { |
|
190 newstring.length = 0; |
|
191 } |
|
192 |
|
193 return newstring; |
|
194 } |
|
195 |
|
196 sstr_t sstrdup_alloc(UcxAllocator *allocator, sstr_t s) { |
|
197 sstr_t newstring; |
|
198 newstring.ptr = (char*)allocator->malloc(allocator->pool, s.length + 1); |
|
199 if (newstring.ptr) { |
|
200 newstring.length = s.length; |
|
201 newstring.ptr[newstring.length] = 0; |
|
202 |
|
203 memcpy(newstring.ptr, s.ptr, s.length); |
|
204 } else { |
|
205 newstring.length = 0; |
|
206 } |
|
207 |
|
208 return newstring; |
|
209 } |
|
210 |
|
211 sstr_t sstrtrim(sstr_t string) { |
|
212 sstr_t newstr = string; |
|
213 if (string.length == 0) { |
|
214 return newstr; |
|
215 } |
|
216 |
|
217 size_t i; |
|
218 for(i=0;i<string.length;i++) { |
|
219 char c = string.ptr[i]; |
|
220 if(c > 32) { |
|
221 break; |
|
222 } |
|
223 } |
|
224 newstr.ptr = &string.ptr[i]; |
|
225 newstr.length = string.length - i; |
|
226 |
|
227 if(newstr.length == 0) { |
|
228 return newstr; |
|
229 } |
|
230 |
|
231 i = newstr.length - 1; |
|
232 for(;;) { |
|
233 char c = newstr.ptr[i]; |
|
234 if(c > 32) { |
|
235 break; |
|
236 } |
|
237 if(i > 0) { |
|
238 i--; |
|
239 } else { |
|
240 break; |
|
241 } |
|
242 } |
|
243 newstr.length = i + 1; |
|
244 |
|
245 return newstr; |
|
246 } |
|
247 |
|
248 // webserver extension |
|
249 |
|
250 int sstr_startswith(sstr_t string, sstr_t cmp) { |
|
251 sstr_t sub = sstrsubsl(string, 0, cmp.length); |
|
252 if(!sstrcmp(sub, cmp)) { |
|
253 return 1; |
|
254 } else { |
|
255 return 0; |
|
256 } |
|
257 } |
|
258 |
|
259 sstr_t sstrdup_mp(UcxMempool *pool, sstr_t s) { |
|
260 sstr_t newstring; |
|
261 newstring.ptr = (char*)ucx_mempool_malloc(pool, s.length + 1); |
|
262 if (newstring.ptr != NULL) { |
|
263 newstring.length = s.length; |
|
264 newstring.ptr[newstring.length] = 0; |
|
265 |
|
266 memcpy(newstring.ptr, s.ptr, s.length); |
|
267 } |
|
268 |
|
269 return newstring; |
|
270 } |
|
271 |
|
272 sstr_t sstrdup_pool(pool_handle_t *pool, sstr_t s) { |
|
273 sstr_t newstring; |
|
274 newstring.ptr = (char*)pool_malloc(pool, s.length + 1); |
|
275 if (newstring.ptr != NULL) { |
|
276 newstring.length = s.length; |
|
277 newstring.ptr[newstring.length] = 0; |
|
278 |
|
279 memcpy(newstring.ptr, s.ptr, s.length); |
|
280 } |
|
281 |
|
282 return newstring; |
|
283 } |
|