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