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