]>
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 | ||
56906eef | 31 | #define SDS_ABORT_ON_OOM |
32 | ||
ed9b544e | 33 | #include "sds.h" |
34 | #include <stdio.h> | |
35 | #include <stdlib.h> | |
ed9b544e | 36 | #include <string.h> |
37 | #include <ctype.h> | |
38 | #include "zmalloc.h" | |
39 | ||
40 | static void sdsOomAbort(void) { | |
41 | fprintf(stderr,"SDS: Out Of Memory (SDS_ABORT_ON_OOM defined)\n"); | |
42 | abort(); | |
43 | } | |
44 | ||
45 | sds sdsnewlen(const void *init, size_t initlen) { | |
46 | struct sdshdr *sh; | |
47 | ||
f1017b3f | 48 | sh = zmalloc(sizeof(struct sdshdr)+initlen+1); |
49 | #ifdef SDS_ABORT_ON_OOM | |
50 | if (sh == NULL) sdsOomAbort(); | |
51 | #else | |
52 | if (sh == NULL) return NULL; | |
53 | #endif | |
54 | sh->len = initlen; | |
ed9b544e | 55 | sh->free = 0; |
56 | if (initlen) { | |
57 | if (init) memcpy(sh->buf, init, initlen); | |
58 | else memset(sh->buf,0,initlen); | |
59 | } | |
60 | sh->buf[initlen] = '\0'; | |
61 | return (char*)sh->buf; | |
62 | } | |
63 | ||
64 | sds sdsempty(void) { | |
65 | return sdsnewlen("",0); | |
66 | } | |
67 | ||
68 | sds sdsnew(const char *init) { | |
69 | size_t initlen = (init == NULL) ? 0 : strlen(init); | |
70 | return sdsnewlen(init, initlen); | |
71 | } | |
72 | ||
73 | size_t sdslen(const sds s) { | |
74 | struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); | |
f1017b3f | 75 | return sh->len; |
ed9b544e | 76 | } |
77 | ||
78 | sds sdsdup(const sds s) { | |
79 | return sdsnewlen(s, sdslen(s)); | |
80 | } | |
81 | ||
82 | void sdsfree(sds s) { | |
83 | if (s == NULL) return; | |
f1017b3f | 84 | zfree(s-sizeof(struct sdshdr)); |
ed9b544e | 85 | } |
86 | ||
87 | size_t sdsavail(sds s) { | |
88 | struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); | |
89 | return sh->free; | |
90 | } | |
91 | ||
92 | void sdsupdatelen(sds s) { | |
93 | struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); | |
94 | int reallen = strlen(s); | |
f1017b3f | 95 | sh->free += (sh->len-reallen); |
96 | sh->len = reallen; | |
ed9b544e | 97 | } |
98 | ||
99 | static sds sdsMakeRoomFor(sds s, size_t addlen) { | |
100 | struct sdshdr *sh, *newsh; | |
101 | size_t free = sdsavail(s); | |
f1017b3f | 102 | size_t len, newlen; |
ed9b544e | 103 | |
f1017b3f | 104 | if (free >= addlen) return s; |
ed9b544e | 105 | len = sdslen(s); |
106 | sh = (void*) (s-(sizeof(struct sdshdr))); | |
f1017b3f | 107 | newlen = (len+addlen)*2; |
108 | newsh = zrealloc(sh, sizeof(struct sdshdr)+newlen+1); | |
ed9b544e | 109 | #ifdef SDS_ABORT_ON_OOM |
f1017b3f | 110 | if (newsh == NULL) sdsOomAbort(); |
ed9b544e | 111 | #else |
f1017b3f | 112 | if (newsh == NULL) return NULL; |
ed9b544e | 113 | #endif |
f1017b3f | 114 | |
115 | newsh->free = newlen - len; | |
ed9b544e | 116 | return newsh->buf; |
117 | } | |
118 | ||
119 | sds sdscatlen(sds s, void *t, size_t len) { | |
f1017b3f | 120 | struct sdshdr *sh; |
ed9b544e | 121 | size_t curlen = sdslen(s); |
122 | ||
123 | s = sdsMakeRoomFor(s,len); | |
124 | if (s == NULL) return NULL; | |
f1017b3f | 125 | sh = (void*) (s-(sizeof(struct sdshdr))); |
ed9b544e | 126 | memcpy(s+curlen, t, len); |
f1017b3f | 127 | sh->len = curlen+len; |
128 | sh->free = sh->free-len; | |
ed9b544e | 129 | s[curlen+len] = '\0'; |
130 | return s; | |
131 | } | |
132 | ||
133 | sds sdscat(sds s, char *t) { | |
134 | return sdscatlen(s, t, strlen(t)); | |
135 | } | |
136 | ||
137 | sds sdscpylen(sds s, char *t, size_t len) { | |
138 | struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); | |
f1017b3f | 139 | size_t totlen = sh->free+sh->len; |
ed9b544e | 140 | |
141 | if (totlen < len) { | |
b2b5ae80 | 142 | s = sdsMakeRoomFor(s,len-sh->len); |
ed9b544e | 143 | if (s == NULL) return NULL; |
f1017b3f | 144 | sh = (void*) (s-(sizeof(struct sdshdr))); |
145 | totlen = sh->free+sh->len; | |
ed9b544e | 146 | } |
147 | memcpy(s, t, len); | |
148 | s[len] = '\0'; | |
f1017b3f | 149 | sh->len = len; |
150 | sh->free = totlen-len; | |
ed9b544e | 151 | return s; |
152 | } | |
153 | ||
154 | sds sdscpy(sds s, char *t) { | |
155 | return sdscpylen(s, t, strlen(t)); | |
156 | } | |
157 | ||
60361e5a PN |
158 | sds sdscatvprintf(sds s, const char *fmt, va_list ap) { |
159 | va_list cpy; | |
ed9b544e | 160 | char *buf, *t; |
4b00bebd | 161 | size_t buflen = 16; |
ed9b544e | 162 | |
163 | while(1) { | |
164 | buf = zmalloc(buflen); | |
165 | #ifdef SDS_ABORT_ON_OOM | |
166 | if (buf == NULL) sdsOomAbort(); | |
167 | #else | |
168 | if (buf == NULL) return NULL; | |
169 | #endif | |
170 | buf[buflen-2] = '\0'; | |
60361e5a PN |
171 | va_copy(cpy,ap); |
172 | vsnprintf(buf, buflen, fmt, cpy); | |
ed9b544e | 173 | if (buf[buflen-2] != '\0') { |
174 | zfree(buf); | |
175 | buflen *= 2; | |
176 | continue; | |
177 | } | |
178 | break; | |
179 | } | |
180 | t = sdscat(s, buf); | |
181 | zfree(buf); | |
182 | return t; | |
183 | } | |
184 | ||
60361e5a PN |
185 | sds sdscatprintf(sds s, const char *fmt, ...) { |
186 | va_list ap; | |
187 | char *t; | |
188 | va_start(ap, fmt); | |
189 | t = sdscatvprintf(s,fmt,ap); | |
190 | va_end(ap); | |
191 | return t; | |
192 | } | |
193 | ||
ed9b544e | 194 | sds sdstrim(sds s, const char *cset) { |
195 | struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); | |
196 | char *start, *end, *sp, *ep; | |
197 | size_t len; | |
198 | ||
199 | sp = start = s; | |
200 | ep = end = s+sdslen(s)-1; | |
201 | while(sp <= end && strchr(cset, *sp)) sp++; | |
202 | while(ep > start && strchr(cset, *ep)) ep--; | |
203 | len = (sp > ep) ? 0 : ((ep-sp)+1); | |
204 | if (sh->buf != sp) memmove(sh->buf, sp, len); | |
205 | sh->buf[len] = '\0'; | |
f1017b3f | 206 | sh->free = sh->free+(sh->len-len); |
207 | sh->len = len; | |
ed9b544e | 208 | return s; |
209 | } | |
210 | ||
e2641e09 | 211 | sds sdsrange(sds s, int start, int end) { |
ed9b544e | 212 | struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); |
213 | size_t newlen, len = sdslen(s); | |
214 | ||
215 | if (len == 0) return s; | |
216 | if (start < 0) { | |
217 | start = len+start; | |
218 | if (start < 0) start = 0; | |
219 | } | |
220 | if (end < 0) { | |
221 | end = len+end; | |
222 | if (end < 0) end = 0; | |
223 | } | |
224 | newlen = (start > end) ? 0 : (end-start)+1; | |
225 | if (newlen != 0) { | |
226 | if (start >= (signed)len) start = len-1; | |
227 | if (end >= (signed)len) end = len-1; | |
228 | newlen = (start > end) ? 0 : (end-start)+1; | |
229 | } else { | |
230 | start = 0; | |
231 | } | |
232 | if (start != 0) memmove(sh->buf, sh->buf+start, newlen); | |
233 | sh->buf[newlen] = 0; | |
f1017b3f | 234 | sh->free = sh->free+(sh->len-newlen); |
235 | sh->len = newlen; | |
ed9b544e | 236 | return s; |
237 | } | |
238 | ||
239 | void sdstolower(sds s) { | |
240 | int len = sdslen(s), j; | |
241 | ||
242 | for (j = 0; j < len; j++) s[j] = tolower(s[j]); | |
243 | } | |
244 | ||
245 | void sdstoupper(sds s) { | |
246 | int len = sdslen(s), j; | |
247 | ||
248 | for (j = 0; j < len; j++) s[j] = toupper(s[j]); | |
249 | } | |
250 | ||
251 | int sdscmp(sds s1, sds s2) { | |
252 | size_t l1, l2, minlen; | |
253 | int cmp; | |
254 | ||
255 | l1 = sdslen(s1); | |
256 | l2 = sdslen(s2); | |
257 | minlen = (l1 < l2) ? l1 : l2; | |
258 | cmp = memcmp(s1,s2,minlen); | |
259 | if (cmp == 0) return l1-l2; | |
260 | return cmp; | |
261 | } | |
262 | ||
263 | /* Split 's' with separator in 'sep'. An array | |
264 | * of sds strings is returned. *count will be set | |
265 | * by reference to the number of tokens returned. | |
266 | * | |
267 | * On out of memory, zero length string, zero length | |
268 | * separator, NULL is returned. | |
269 | * | |
270 | * Note that 'sep' is able to split a string using | |
271 | * a multi-character separator. For example | |
272 | * sdssplit("foo_-_bar","_-_"); will return two | |
273 | * elements "foo" and "bar". | |
274 | * | |
275 | * This version of the function is binary-safe but | |
276 | * requires length arguments. sdssplit() is just the | |
277 | * same function but for zero-terminated strings. | |
278 | */ | |
279 | sds *sdssplitlen(char *s, int len, char *sep, int seplen, int *count) { | |
280 | int elements = 0, slots = 5, start = 0, j; | |
281 | ||
282 | sds *tokens = zmalloc(sizeof(sds)*slots); | |
283 | #ifdef SDS_ABORT_ON_OOM | |
284 | if (tokens == NULL) sdsOomAbort(); | |
285 | #endif | |
286 | if (seplen < 1 || len < 0 || tokens == NULL) return NULL; | |
ed10f40b | 287 | if (len == 0) { |
288 | *count = 0; | |
289 | return tokens; | |
290 | } | |
ed9b544e | 291 | for (j = 0; j < (len-(seplen-1)); j++) { |
292 | /* make sure there is room for the next element and the final one */ | |
293 | if (slots < elements+2) { | |
a4d1ba9a | 294 | sds *newtokens; |
295 | ||
ed9b544e | 296 | slots *= 2; |
a4d1ba9a | 297 | newtokens = zrealloc(tokens,sizeof(sds)*slots); |
ed9b544e | 298 | if (newtokens == NULL) { |
299 | #ifdef SDS_ABORT_ON_OOM | |
300 | sdsOomAbort(); | |
301 | #else | |
302 | goto cleanup; | |
303 | #endif | |
304 | } | |
305 | tokens = newtokens; | |
306 | } | |
307 | /* search the separator */ | |
308 | if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) { | |
309 | tokens[elements] = sdsnewlen(s+start,j-start); | |
310 | if (tokens[elements] == NULL) { | |
311 | #ifdef SDS_ABORT_ON_OOM | |
312 | sdsOomAbort(); | |
313 | #else | |
314 | goto cleanup; | |
315 | #endif | |
316 | } | |
317 | elements++; | |
318 | start = j+seplen; | |
319 | j = j+seplen-1; /* skip the separator */ | |
320 | } | |
321 | } | |
322 | /* Add the final element. We are sure there is room in the tokens array. */ | |
323 | tokens[elements] = sdsnewlen(s+start,len-start); | |
324 | if (tokens[elements] == NULL) { | |
325 | #ifdef SDS_ABORT_ON_OOM | |
326 | sdsOomAbort(); | |
327 | #else | |
328 | goto cleanup; | |
329 | #endif | |
330 | } | |
331 | elements++; | |
332 | *count = elements; | |
333 | return tokens; | |
334 | ||
335 | #ifndef SDS_ABORT_ON_OOM | |
336 | cleanup: | |
337 | { | |
338 | int i; | |
339 | for (i = 0; i < elements; i++) sdsfree(tokens[i]); | |
340 | zfree(tokens); | |
341 | return NULL; | |
342 | } | |
343 | #endif | |
344 | } | |
a34e0a25 | 345 | |
346 | void sdsfreesplitres(sds *tokens, int count) { | |
347 | if (!tokens) return; | |
348 | while(count--) | |
349 | sdsfree(tokens[count]); | |
350 | zfree(tokens); | |
351 | } | |
ee14da56 | 352 | |
353 | sds sdsfromlonglong(long long value) { | |
354 | char buf[32], *p; | |
355 | unsigned long long v; | |
356 | ||
357 | v = (value < 0) ? -value : value; | |
358 | p = buf+31; /* point to the last character */ | |
359 | do { | |
360 | *p-- = '0'+(v%10); | |
361 | v /= 10; | |
362 | } while(v); | |
363 | if (value < 0) *p-- = '-'; | |
364 | p++; | |
365 | return sdsnewlen(p,32-(p-buf)); | |
366 | } | |
e2641e09 | 367 | |
368 | sds sdscatrepr(sds s, char *p, size_t len) { | |
369 | s = sdscatlen(s,"\"",1); | |
370 | while(len--) { | |
371 | switch(*p) { | |
372 | case '\\': | |
373 | case '"': | |
374 | s = sdscatprintf(s,"\\%c",*p); | |
375 | break; | |
376 | case '\n': s = sdscatlen(s,"\\n",1); break; | |
377 | case '\r': s = sdscatlen(s,"\\r",1); break; | |
378 | case '\t': s = sdscatlen(s,"\\t",1); break; | |
379 | case '\a': s = sdscatlen(s,"\\a",1); break; | |
380 | case '\b': s = sdscatlen(s,"\\b",1); break; | |
381 | default: | |
382 | if (isprint(*p)) | |
383 | s = sdscatprintf(s,"%c",*p); | |
384 | else | |
385 | s = sdscatprintf(s,"\\x%02x",(unsigned char)*p); | |
386 | break; | |
387 | } | |
388 | p++; | |
389 | } | |
390 | return sdscatlen(s,"\"",1); | |
391 | } | |
cbce5171 | 392 | |
393 | /* Split a line into arguments, where every argument can be in the | |
394 | * following programming-language REPL-alike form: | |
395 | * | |
396 | * foo bar "newline are supported\n" and "\xff\x00otherstuff" | |
397 | * | |
398 | * The number of arguments is stored into *argc, and an array | |
399 | * of sds is returned. The caller should sdsfree() all the returned | |
400 | * strings and finally zfree() the array itself. | |
401 | * | |
402 | * Note that sdscatrepr() is able to convert back a string into | |
403 | * a quoted string in the same format sdssplitargs() is able to parse. | |
404 | */ | |
405 | sds *sdssplitargs(char *line, int *argc) { | |
406 | char *p = line; | |
407 | char *current = NULL; | |
408 | char **vector = NULL; | |
409 | ||
410 | *argc = 0; | |
411 | while(1) { | |
412 | /* skip blanks */ | |
413 | while(*p && isspace(*p)) p++; | |
414 | if (*p) { | |
415 | /* get a token */ | |
416 | int inq=0; /* set to 1 if we are in "quotes" */ | |
4b93e5e2 | 417 | int done=0; |
cbce5171 | 418 | |
419 | if (current == NULL) current = sdsempty(); | |
420 | while(!done) { | |
421 | if (inq) { | |
422 | if (*p == '\\' && *(p+1)) { | |
423 | char c; | |
424 | ||
425 | p++; | |
426 | switch(*p) { | |
427 | case 'n': c = '\n'; break; | |
428 | case 'r': c = '\r'; break; | |
429 | case 't': c = '\t'; break; | |
430 | case 'b': c = '\b'; break; | |
431 | case 'a': c = '\a'; break; | |
432 | default: c = *p; break; | |
433 | } | |
434 | current = sdscatlen(current,&c,1); | |
435 | } else if (*p == '"') { | |
4b93e5e2 PN |
436 | /* closing quote must be followed by a space */ |
437 | if (*(p+1) && !isspace(*(p+1))) goto err; | |
438 | done=1; | |
439 | } else if (!*p) { | |
440 | /* unterminated quotes */ | |
441 | goto err; | |
cbce5171 | 442 | } else { |
443 | current = sdscatlen(current,p,1); | |
444 | } | |
445 | } else { | |
446 | switch(*p) { | |
447 | case ' ': | |
448 | case '\n': | |
449 | case '\r': | |
450 | case '\t': | |
451 | case '\0': | |
452 | done=1; | |
453 | break; | |
454 | case '"': | |
455 | inq=1; | |
456 | break; | |
457 | default: | |
458 | current = sdscatlen(current,p,1); | |
459 | break; | |
460 | } | |
461 | } | |
462 | if (*p) p++; | |
463 | } | |
464 | /* add the token to the vector */ | |
465 | vector = zrealloc(vector,((*argc)+1)*sizeof(char*)); | |
466 | vector[*argc] = current; | |
467 | (*argc)++; | |
468 | current = NULL; | |
469 | } else { | |
470 | return vector; | |
471 | } | |
472 | } | |
4b93e5e2 PN |
473 | |
474 | err: | |
2929ca97 | 475 | while((*argc)--) |
4b93e5e2 PN |
476 | sdsfree(vector[*argc]); |
477 | zfree(vector); | |
478 | if (current) sdsfree(current); | |
479 | return NULL; | |
cbce5171 | 480 | } |