]>
Commit | Line | Data |
---|---|---|
ed9b544e | 1 | /* SDSLib, A C dynamic strings library |
2 | * | |
12d090d2 | 3 | * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com> |
ed9b544e | 4 | * 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 | * * Redistributions of source code must retain the above copyright notice, | |
10 | * this list of conditions and the following disclaimer. | |
11 | * * Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * * Neither the name of Redis nor the names of its contributors may be used | |
15 | * to endorse or promote products derived from this software without | |
16 | * specific prior written permission. | |
17 | * | |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
28 | * POSSIBILITY OF SUCH DAMAGE. | |
e360e3bb | 29 | * |
30 | * History: | |
31 | * | |
32 | * - 22 March 2011: History section created on top of sds.c | |
33 | * - 22 March 2011: Fixed a problem with "\xab" escapes convertion in | |
34 | * function sdssplitargs(). | |
ed9b544e | 35 | */ |
36 | ||
56906eef | 37 | #define SDS_ABORT_ON_OOM |
38 | ||
ed9b544e | 39 | #include <stdio.h> |
40 | #include <stdlib.h> | |
ed9b544e | 41 | #include <string.h> |
42 | #include <ctype.h> | |
ded614f8 | 43 | #include "sds.h" |
ed9b544e | 44 | #include "zmalloc.h" |
45 | ||
46 | static void sdsOomAbort(void) { | |
47 | fprintf(stderr,"SDS: Out Of Memory (SDS_ABORT_ON_OOM defined)\n"); | |
48 | abort(); | |
49 | } | |
50 | ||
51 | sds sdsnewlen(const void *init, size_t initlen) { | |
52 | struct sdshdr *sh; | |
53 | ||
626f6b2d | 54 | if (init) { |
55 | sh = zmalloc(sizeof(struct sdshdr)+initlen+1); | |
56 | } else { | |
57 | sh = zcalloc(sizeof(struct sdshdr)+initlen+1); | |
58 | } | |
f1017b3f | 59 | #ifdef SDS_ABORT_ON_OOM |
60 | if (sh == NULL) sdsOomAbort(); | |
61 | #else | |
62 | if (sh == NULL) return NULL; | |
63 | #endif | |
64 | sh->len = initlen; | |
ed9b544e | 65 | sh->free = 0; |
626f6b2d | 66 | if (initlen && init) |
67 | memcpy(sh->buf, init, initlen); | |
ed9b544e | 68 | sh->buf[initlen] = '\0'; |
69 | return (char*)sh->buf; | |
70 | } | |
71 | ||
72 | sds sdsempty(void) { | |
73 | return sdsnewlen("",0); | |
74 | } | |
75 | ||
76 | sds sdsnew(const char *init) { | |
77 | size_t initlen = (init == NULL) ? 0 : strlen(init); | |
78 | return sdsnewlen(init, initlen); | |
79 | } | |
80 | ||
ed9b544e | 81 | sds sdsdup(const sds s) { |
82 | return sdsnewlen(s, sdslen(s)); | |
83 | } | |
84 | ||
85 | void sdsfree(sds s) { | |
86 | if (s == NULL) return; | |
f1017b3f | 87 | zfree(s-sizeof(struct sdshdr)); |
ed9b544e | 88 | } |
89 | ||
ed9b544e | 90 | void sdsupdatelen(sds s) { |
91 | struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); | |
92 | int reallen = strlen(s); | |
f1017b3f | 93 | sh->free += (sh->len-reallen); |
94 | sh->len = reallen; | |
ed9b544e | 95 | } |
96 | ||
f990782f PN |
97 | void sdsclear(sds s) { |
98 | struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); | |
99 | sh->free += sh->len; | |
100 | sh->len = 0; | |
101 | sh->buf[0] = '\0'; | |
102 | } | |
103 | ||
ed9b544e | 104 | static sds sdsMakeRoomFor(sds s, size_t addlen) { |
105 | struct sdshdr *sh, *newsh; | |
106 | size_t free = sdsavail(s); | |
f1017b3f | 107 | size_t len, newlen; |
ed9b544e | 108 | |
f1017b3f | 109 | if (free >= addlen) return s; |
ed9b544e | 110 | len = sdslen(s); |
111 | sh = (void*) (s-(sizeof(struct sdshdr))); | |
f1017b3f | 112 | newlen = (len+addlen)*2; |
113 | newsh = zrealloc(sh, sizeof(struct sdshdr)+newlen+1); | |
ed9b544e | 114 | #ifdef SDS_ABORT_ON_OOM |
f1017b3f | 115 | if (newsh == NULL) sdsOomAbort(); |
ed9b544e | 116 | #else |
f1017b3f | 117 | if (newsh == NULL) return NULL; |
ed9b544e | 118 | #endif |
f1017b3f | 119 | |
120 | newsh->free = newlen - len; | |
ed9b544e | 121 | return newsh->buf; |
122 | } | |
123 | ||
eae33c1c | 124 | /* Grow the sds to have the specified length. Bytes that were not part of |
cc209063 PN |
125 | * the original length of the sds will be set to zero. */ |
126 | sds sdsgrowzero(sds s, size_t len) { | |
eae33c1c PN |
127 | struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr))); |
128 | size_t totlen, curlen = sh->len; | |
129 | ||
130 | if (len <= curlen) return s; | |
131 | s = sdsMakeRoomFor(s,len-curlen); | |
132 | if (s == NULL) return NULL; | |
133 | ||
134 | /* Make sure added region doesn't contain garbage */ | |
135 | sh = (void*)(s-(sizeof(struct sdshdr))); | |
cc209063 | 136 | memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */ |
eae33c1c PN |
137 | totlen = sh->len+sh->free; |
138 | sh->len = len; | |
139 | sh->free = totlen-sh->len; | |
140 | return s; | |
141 | } | |
142 | ||
ed9b544e | 143 | sds sdscatlen(sds s, void *t, size_t len) { |
f1017b3f | 144 | struct sdshdr *sh; |
ed9b544e | 145 | size_t curlen = sdslen(s); |
146 | ||
147 | s = sdsMakeRoomFor(s,len); | |
148 | if (s == NULL) return NULL; | |
f1017b3f | 149 | sh = (void*) (s-(sizeof(struct sdshdr))); |
ed9b544e | 150 | memcpy(s+curlen, t, len); |
f1017b3f | 151 | sh->len = curlen+len; |
152 | sh->free = sh->free-len; | |
ed9b544e | 153 | s[curlen+len] = '\0'; |
154 | return s; | |
155 | } | |
156 | ||
157 | sds sdscat(sds s, char *t) { | |
158 | return sdscatlen(s, t, strlen(t)); | |
159 | } | |
160 | ||
161 | sds sdscpylen(sds s, char *t, size_t len) { | |
162 | struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); | |
f1017b3f | 163 | size_t totlen = sh->free+sh->len; |
ed9b544e | 164 | |
165 | if (totlen < len) { | |
b2b5ae80 | 166 | s = sdsMakeRoomFor(s,len-sh->len); |
ed9b544e | 167 | if (s == NULL) return NULL; |
f1017b3f | 168 | sh = (void*) (s-(sizeof(struct sdshdr))); |
169 | totlen = sh->free+sh->len; | |
ed9b544e | 170 | } |
171 | memcpy(s, t, len); | |
172 | s[len] = '\0'; | |
f1017b3f | 173 | sh->len = len; |
174 | sh->free = totlen-len; | |
ed9b544e | 175 | return s; |
176 | } | |
177 | ||
178 | sds sdscpy(sds s, char *t) { | |
179 | return sdscpylen(s, t, strlen(t)); | |
180 | } | |
181 | ||
60361e5a PN |
182 | sds sdscatvprintf(sds s, const char *fmt, va_list ap) { |
183 | va_list cpy; | |
ed9b544e | 184 | char *buf, *t; |
4b00bebd | 185 | size_t buflen = 16; |
ed9b544e | 186 | |
187 | while(1) { | |
188 | buf = zmalloc(buflen); | |
189 | #ifdef SDS_ABORT_ON_OOM | |
190 | if (buf == NULL) sdsOomAbort(); | |
191 | #else | |
192 | if (buf == NULL) return NULL; | |
193 | #endif | |
194 | buf[buflen-2] = '\0'; | |
60361e5a PN |
195 | va_copy(cpy,ap); |
196 | vsnprintf(buf, buflen, fmt, cpy); | |
ed9b544e | 197 | if (buf[buflen-2] != '\0') { |
198 | zfree(buf); | |
199 | buflen *= 2; | |
200 | continue; | |
201 | } | |
202 | break; | |
203 | } | |
204 | t = sdscat(s, buf); | |
205 | zfree(buf); | |
206 | return t; | |
207 | } | |
208 | ||
60361e5a PN |
209 | sds sdscatprintf(sds s, const char *fmt, ...) { |
210 | va_list ap; | |
211 | char *t; | |
212 | va_start(ap, fmt); | |
213 | t = sdscatvprintf(s,fmt,ap); | |
214 | va_end(ap); | |
215 | return t; | |
216 | } | |
217 | ||
ed9b544e | 218 | sds sdstrim(sds s, const char *cset) { |
219 | struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); | |
220 | char *start, *end, *sp, *ep; | |
221 | size_t len; | |
222 | ||
223 | sp = start = s; | |
224 | ep = end = s+sdslen(s)-1; | |
225 | while(sp <= end && strchr(cset, *sp)) sp++; | |
226 | while(ep > start && strchr(cset, *ep)) ep--; | |
227 | len = (sp > ep) ? 0 : ((ep-sp)+1); | |
228 | if (sh->buf != sp) memmove(sh->buf, sp, len); | |
229 | sh->buf[len] = '\0'; | |
f1017b3f | 230 | sh->free = sh->free+(sh->len-len); |
231 | sh->len = len; | |
ed9b544e | 232 | return s; |
233 | } | |
234 | ||
e2641e09 | 235 | sds sdsrange(sds s, int start, int end) { |
ed9b544e | 236 | struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); |
237 | size_t newlen, len = sdslen(s); | |
238 | ||
239 | if (len == 0) return s; | |
240 | if (start < 0) { | |
241 | start = len+start; | |
242 | if (start < 0) start = 0; | |
243 | } | |
244 | if (end < 0) { | |
245 | end = len+end; | |
246 | if (end < 0) end = 0; | |
247 | } | |
248 | newlen = (start > end) ? 0 : (end-start)+1; | |
249 | if (newlen != 0) { | |
136cf53f | 250 | if (start >= (signed)len) { |
251 | newlen = 0; | |
252 | } else if (end >= (signed)len) { | |
253 | end = len-1; | |
254 | newlen = (start > end) ? 0 : (end-start)+1; | |
255 | } | |
ed9b544e | 256 | } else { |
257 | start = 0; | |
258 | } | |
136cf53f | 259 | if (start && newlen) memmove(sh->buf, sh->buf+start, newlen); |
ed9b544e | 260 | sh->buf[newlen] = 0; |
f1017b3f | 261 | sh->free = sh->free+(sh->len-newlen); |
262 | sh->len = newlen; | |
ed9b544e | 263 | return s; |
264 | } | |
265 | ||
266 | void sdstolower(sds s) { | |
267 | int len = sdslen(s), j; | |
268 | ||
269 | for (j = 0; j < len; j++) s[j] = tolower(s[j]); | |
270 | } | |
271 | ||
272 | void sdstoupper(sds s) { | |
273 | int len = sdslen(s), j; | |
274 | ||
275 | for (j = 0; j < len; j++) s[j] = toupper(s[j]); | |
276 | } | |
277 | ||
278 | int sdscmp(sds s1, sds s2) { | |
279 | size_t l1, l2, minlen; | |
280 | int cmp; | |
281 | ||
282 | l1 = sdslen(s1); | |
283 | l2 = sdslen(s2); | |
284 | minlen = (l1 < l2) ? l1 : l2; | |
285 | cmp = memcmp(s1,s2,minlen); | |
286 | if (cmp == 0) return l1-l2; | |
287 | return cmp; | |
288 | } | |
289 | ||
290 | /* Split 's' with separator in 'sep'. An array | |
291 | * of sds strings is returned. *count will be set | |
292 | * by reference to the number of tokens returned. | |
293 | * | |
294 | * On out of memory, zero length string, zero length | |
295 | * separator, NULL is returned. | |
296 | * | |
297 | * Note that 'sep' is able to split a string using | |
298 | * a multi-character separator. For example | |
299 | * sdssplit("foo_-_bar","_-_"); will return two | |
300 | * elements "foo" and "bar". | |
301 | * | |
302 | * This version of the function is binary-safe but | |
303 | * requires length arguments. sdssplit() is just the | |
304 | * same function but for zero-terminated strings. | |
305 | */ | |
306 | sds *sdssplitlen(char *s, int len, char *sep, int seplen, int *count) { | |
307 | int elements = 0, slots = 5, start = 0, j; | |
c040cbd6 | 308 | sds *tokens; |
ed9b544e | 309 | |
c040cbd6 PN |
310 | if (seplen < 1 || len < 0) return NULL; |
311 | ||
312 | tokens = zmalloc(sizeof(sds)*slots); | |
ed9b544e | 313 | #ifdef SDS_ABORT_ON_OOM |
314 | if (tokens == NULL) sdsOomAbort(); | |
c040cbd6 PN |
315 | #else |
316 | if (tokens == NULL) return NULL; | |
ed9b544e | 317 | #endif |
c040cbd6 | 318 | |
ed10f40b | 319 | if (len == 0) { |
320 | *count = 0; | |
321 | return tokens; | |
322 | } | |
ed9b544e | 323 | for (j = 0; j < (len-(seplen-1)); j++) { |
324 | /* make sure there is room for the next element and the final one */ | |
325 | if (slots < elements+2) { | |
a4d1ba9a | 326 | sds *newtokens; |
327 | ||
ed9b544e | 328 | slots *= 2; |
a4d1ba9a | 329 | newtokens = zrealloc(tokens,sizeof(sds)*slots); |
ed9b544e | 330 | if (newtokens == NULL) { |
331 | #ifdef SDS_ABORT_ON_OOM | |
332 | sdsOomAbort(); | |
333 | #else | |
334 | goto cleanup; | |
335 | #endif | |
336 | } | |
337 | tokens = newtokens; | |
338 | } | |
339 | /* search the separator */ | |
340 | if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) { | |
341 | tokens[elements] = sdsnewlen(s+start,j-start); | |
342 | if (tokens[elements] == NULL) { | |
343 | #ifdef SDS_ABORT_ON_OOM | |
344 | sdsOomAbort(); | |
345 | #else | |
346 | goto cleanup; | |
347 | #endif | |
348 | } | |
349 | elements++; | |
350 | start = j+seplen; | |
351 | j = j+seplen-1; /* skip the separator */ | |
352 | } | |
353 | } | |
354 | /* Add the final element. We are sure there is room in the tokens array. */ | |
355 | tokens[elements] = sdsnewlen(s+start,len-start); | |
356 | if (tokens[elements] == NULL) { | |
357 | #ifdef SDS_ABORT_ON_OOM | |
358 | sdsOomAbort(); | |
359 | #else | |
360 | goto cleanup; | |
361 | #endif | |
362 | } | |
363 | elements++; | |
364 | *count = elements; | |
365 | return tokens; | |
366 | ||
367 | #ifndef SDS_ABORT_ON_OOM | |
368 | cleanup: | |
369 | { | |
370 | int i; | |
371 | for (i = 0; i < elements; i++) sdsfree(tokens[i]); | |
372 | zfree(tokens); | |
be86082b | 373 | *count = 0; |
ed9b544e | 374 | return NULL; |
375 | } | |
376 | #endif | |
377 | } | |
a34e0a25 | 378 | |
379 | void sdsfreesplitres(sds *tokens, int count) { | |
380 | if (!tokens) return; | |
381 | while(count--) | |
382 | sdsfree(tokens[count]); | |
383 | zfree(tokens); | |
384 | } | |
ee14da56 | 385 | |
386 | sds sdsfromlonglong(long long value) { | |
387 | char buf[32], *p; | |
388 | unsigned long long v; | |
389 | ||
390 | v = (value < 0) ? -value : value; | |
391 | p = buf+31; /* point to the last character */ | |
392 | do { | |
393 | *p-- = '0'+(v%10); | |
394 | v /= 10; | |
395 | } while(v); | |
396 | if (value < 0) *p-- = '-'; | |
397 | p++; | |
398 | return sdsnewlen(p,32-(p-buf)); | |
399 | } | |
e2641e09 | 400 | |
401 | sds sdscatrepr(sds s, char *p, size_t len) { | |
402 | s = sdscatlen(s,"\"",1); | |
403 | while(len--) { | |
404 | switch(*p) { | |
405 | case '\\': | |
406 | case '"': | |
407 | s = sdscatprintf(s,"\\%c",*p); | |
408 | break; | |
612810af | 409 | case '\n': s = sdscatlen(s,"\\n",2); break; |
410 | case '\r': s = sdscatlen(s,"\\r",2); break; | |
411 | case '\t': s = sdscatlen(s,"\\t",2); break; | |
412 | case '\a': s = sdscatlen(s,"\\a",2); break; | |
413 | case '\b': s = sdscatlen(s,"\\b",2); break; | |
e2641e09 | 414 | default: |
415 | if (isprint(*p)) | |
416 | s = sdscatprintf(s,"%c",*p); | |
417 | else | |
418 | s = sdscatprintf(s,"\\x%02x",(unsigned char)*p); | |
419 | break; | |
420 | } | |
421 | p++; | |
422 | } | |
423 | return sdscatlen(s,"\"",1); | |
424 | } | |
cbce5171 | 425 | |
e360e3bb | 426 | /* Helper function for sdssplitargs() that returns non zero if 'c' |
427 | * is a valid hex digit. */ | |
428 | int is_hex_digit(char c) { | |
429 | return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || | |
430 | (c >= 'A' && c <= 'F'); | |
431 | } | |
432 | ||
433 | /* Helper function for sdssplitargs() that converts an hex digit into an | |
434 | * integer from 0 to 15 */ | |
435 | int hex_digit_to_int(char c) { | |
436 | switch(c) { | |
437 | case '0': return 0; | |
438 | case '1': return 1; | |
439 | case '2': return 2; | |
440 | case '3': return 3; | |
441 | case '4': return 4; | |
442 | case '5': return 5; | |
443 | case '6': return 6; | |
444 | case '7': return 7; | |
445 | case '8': return 8; | |
446 | case '9': return 9; | |
447 | case 'a': case 'A': return 10; | |
448 | case 'b': case 'B': return 11; | |
449 | case 'c': case 'C': return 12; | |
450 | case 'd': case 'D': return 13; | |
451 | case 'e': case 'E': return 14; | |
452 | case 'f': case 'F': return 15; | |
453 | default: return 0; | |
454 | } | |
455 | } | |
456 | ||
cbce5171 | 457 | /* Split a line into arguments, where every argument can be in the |
458 | * following programming-language REPL-alike form: | |
459 | * | |
460 | * foo bar "newline are supported\n" and "\xff\x00otherstuff" | |
461 | * | |
462 | * The number of arguments is stored into *argc, and an array | |
463 | * of sds is returned. The caller should sdsfree() all the returned | |
464 | * strings and finally zfree() the array itself. | |
465 | * | |
466 | * Note that sdscatrepr() is able to convert back a string into | |
467 | * a quoted string in the same format sdssplitargs() is able to parse. | |
468 | */ | |
469 | sds *sdssplitargs(char *line, int *argc) { | |
470 | char *p = line; | |
471 | char *current = NULL; | |
472 | char **vector = NULL; | |
473 | ||
474 | *argc = 0; | |
475 | while(1) { | |
476 | /* skip blanks */ | |
477 | while(*p && isspace(*p)) p++; | |
478 | if (*p) { | |
479 | /* get a token */ | |
e1cf460c | 480 | int inq=0; /* set to 1 if we are in "quotes" */ |
481 | int insq=0; /* set to 1 if we are in 'single quotes' */ | |
4b93e5e2 | 482 | int done=0; |
cbce5171 | 483 | |
484 | if (current == NULL) current = sdsempty(); | |
485 | while(!done) { | |
486 | if (inq) { | |
e360e3bb | 487 | if (*p == '\\' && *(p+1) == 'x' && |
488 | is_hex_digit(*(p+2)) && | |
489 | is_hex_digit(*(p+3))) | |
490 | { | |
491 | unsigned char byte; | |
492 | ||
493 | byte = (hex_digit_to_int(*(p+2))*16)+ | |
494 | hex_digit_to_int(*(p+3)); | |
495 | current = sdscatlen(current,(char*)&byte,1); | |
496 | p += 3; | |
497 | } else if (*p == '\\' && *(p+1)) { | |
cbce5171 | 498 | char c; |
499 | ||
500 | p++; | |
501 | switch(*p) { | |
502 | case 'n': c = '\n'; break; | |
503 | case 'r': c = '\r'; break; | |
504 | case 't': c = '\t'; break; | |
505 | case 'b': c = '\b'; break; | |
506 | case 'a': c = '\a'; break; | |
507 | default: c = *p; break; | |
508 | } | |
509 | current = sdscatlen(current,&c,1); | |
510 | } else if (*p == '"') { | |
e1cf460c | 511 | /* closing quote must be followed by a space or |
512 | * nothing at all. */ | |
513 | if (*(p+1) && !isspace(*(p+1))) goto err; | |
514 | done=1; | |
515 | } else if (!*p) { | |
516 | /* unterminated quotes */ | |
517 | goto err; | |
518 | } else { | |
519 | current = sdscatlen(current,p,1); | |
520 | } | |
521 | } else if (insq) { | |
522 | if (*p == '\\' && *(p+1) == '\'') { | |
523 | p++; | |
524 | current = sdscatlen(current,"'",1); | |
525 | } else if (*p == '\'') { | |
526 | /* closing quote must be followed by a space or | |
527 | * nothing at all. */ | |
4b93e5e2 PN |
528 | if (*(p+1) && !isspace(*(p+1))) goto err; |
529 | done=1; | |
530 | } else if (!*p) { | |
531 | /* unterminated quotes */ | |
532 | goto err; | |
cbce5171 | 533 | } else { |
534 | current = sdscatlen(current,p,1); | |
535 | } | |
536 | } else { | |
537 | switch(*p) { | |
538 | case ' ': | |
539 | case '\n': | |
540 | case '\r': | |
541 | case '\t': | |
542 | case '\0': | |
543 | done=1; | |
544 | break; | |
545 | case '"': | |
546 | inq=1; | |
547 | break; | |
e1cf460c | 548 | case '\'': |
549 | insq=1; | |
550 | break; | |
cbce5171 | 551 | default: |
552 | current = sdscatlen(current,p,1); | |
553 | break; | |
554 | } | |
555 | } | |
556 | if (*p) p++; | |
557 | } | |
558 | /* add the token to the vector */ | |
559 | vector = zrealloc(vector,((*argc)+1)*sizeof(char*)); | |
560 | vector[*argc] = current; | |
561 | (*argc)++; | |
562 | current = NULL; | |
563 | } else { | |
564 | return vector; | |
565 | } | |
566 | } | |
4b93e5e2 PN |
567 | |
568 | err: | |
2929ca97 | 569 | while((*argc)--) |
4b93e5e2 PN |
570 | sdsfree(vector[*argc]); |
571 | zfree(vector); | |
572 | if (current) sdsfree(current); | |
573 | return NULL; | |
cbce5171 | 574 | } |
136cf53f | 575 | |
726a39c1 | 576 | void sdssplitargs_free(sds *argv, int argc) { |
577 | int j; | |
578 | ||
579 | for (j = 0 ;j < argc; j++) sdsfree(argv[j]); | |
580 | zfree(argv); | |
581 | } | |
582 | ||
3bb818df | 583 | /* Modify the string substituting all the occurrences of the set of |
584 | * characters specifed in the 'from' string to the corresponding character | |
585 | * in the 'to' array. | |
586 | * | |
587 | * For instance: sdsmapchars(mystring, "ho", "01", 2) | |
588 | * will have the effect of turning the string "hello" into "0ell1". | |
589 | * | |
590 | * The function returns the sds string pointer, that is always the same | |
591 | * as the input pointer since no resize is needed. */ | |
592 | sds sdsmapchars(sds s, char *from, char *to, size_t setlen) { | |
593 | size_t j, i, l = sdslen(s); | |
594 | ||
595 | for (j = 0; j < l; j++) { | |
596 | for (i = 0; i < setlen; i++) { | |
597 | if (s[j] == from[i]) { | |
598 | s[j] = to[i]; | |
599 | break; | |
600 | } | |
601 | } | |
602 | } | |
603 | return s; | |
604 | } | |
605 | ||
136cf53f | 606 | #ifdef SDS_TEST_MAIN |
607 | #include <stdio.h> | |
608 | #include "testhelp.h" | |
609 | ||
610 | int main(void) { | |
611 | { | |
963238f7 | 612 | sds x = sdsnew("foo"), y; |
613 | ||
614 | test_cond("Create a string and obtain the length", | |
615 | sdslen(x) == 3 && memcmp(x,"foo\0",4) == 0) | |
616 | ||
617 | sdsfree(x); | |
618 | x = sdsnewlen("foo",2); | |
619 | test_cond("Create a string with specified length", | |
620 | sdslen(x) == 2 && memcmp(x,"fo\0",3) == 0) | |
621 | ||
622 | x = sdscat(x,"bar"); | |
623 | test_cond("Strings concatenation", | |
624 | sdslen(x) == 5 && memcmp(x,"fobar\0",6) == 0); | |
625 | ||
626 | x = sdscpy(x,"a"); | |
627 | test_cond("sdscpy() against an originally longer string", | |
628 | sdslen(x) == 1 && memcmp(x,"a\0",2) == 0) | |
629 | ||
630 | x = sdscpy(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk"); | |
631 | test_cond("sdscpy() against an originally shorter string", | |
632 | sdslen(x) == 33 && | |
633 | memcmp(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0) | |
634 | ||
635 | sdsfree(x); | |
636 | x = sdscatprintf(sdsempty(),"%d",123); | |
637 | test_cond("sdscatprintf() seems working in the base case", | |
638 | sdslen(x) == 3 && memcmp(x,"123\0",4) ==0) | |
639 | ||
640 | sdsfree(x); | |
641 | x = sdstrim(sdsnew("xxciaoyyy"),"xy"); | |
642 | test_cond("sdstrim() correctly trims characters", | |
643 | sdslen(x) == 4 && memcmp(x,"ciao\0",5) == 0) | |
644 | ||
645 | y = sdsrange(sdsdup(x),1,1); | |
646 | test_cond("sdsrange(...,1,1)", | |
647 | sdslen(y) == 1 && memcmp(y,"i\0",2) == 0) | |
648 | ||
649 | sdsfree(y); | |
650 | y = sdsrange(sdsdup(x),1,-1); | |
651 | test_cond("sdsrange(...,1,-1)", | |
652 | sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0) | |
653 | ||
654 | sdsfree(y); | |
655 | y = sdsrange(sdsdup(x),-2,-1); | |
656 | test_cond("sdsrange(...,-2,-1)", | |
657 | sdslen(y) == 2 && memcmp(y,"ao\0",3) == 0) | |
658 | ||
659 | sdsfree(y); | |
660 | y = sdsrange(sdsdup(x),2,1); | |
661 | test_cond("sdsrange(...,2,1)", | |
662 | sdslen(y) == 0 && memcmp(y,"\0",1) == 0) | |
663 | ||
664 | sdsfree(y); | |
665 | y = sdsrange(sdsdup(x),1,100); | |
666 | test_cond("sdsrange(...,1,100)", | |
667 | sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0) | |
668 | ||
669 | sdsfree(y); | |
670 | y = sdsrange(sdsdup(x),100,100); | |
671 | test_cond("sdsrange(...,100,100)", | |
672 | sdslen(y) == 0 && memcmp(y,"\0",1) == 0) | |
673 | ||
674 | sdsfree(y); | |
675 | sdsfree(x); | |
676 | x = sdsnew("foo"); | |
677 | y = sdsnew("foa"); | |
678 | test_cond("sdscmp(foo,foa)", sdscmp(x,y) > 0) | |
679 | ||
680 | sdsfree(y); | |
681 | sdsfree(x); | |
682 | x = sdsnew("bar"); | |
683 | y = sdsnew("bar"); | |
684 | test_cond("sdscmp(bar,bar)", sdscmp(x,y) == 0) | |
685 | ||
686 | sdsfree(y); | |
687 | sdsfree(x); | |
688 | x = sdsnew("aar"); | |
689 | y = sdsnew("bar"); | |
690 | test_cond("sdscmp(bar,bar)", sdscmp(x,y) < 0) | |
136cf53f | 691 | } |
692 | test_report() | |
693 | } | |
694 | #endif |