]> git.saurik.com Git - redis.git/blob - src/sds.c
Merge branch 'unstable'
[redis.git] / src / sds.c
1 /* SDSLib, A C dynamic strings library
2 *
3 * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
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 * 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().
35 */
36
37 #define SDS_ABORT_ON_OOM
38
39 #include "sds.h"
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
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
54 sh = zmalloc(sizeof(struct sdshdr)+initlen+1);
55 #ifdef SDS_ABORT_ON_OOM
56 if (sh == NULL) sdsOomAbort();
57 #else
58 if (sh == NULL) return NULL;
59 #endif
60 sh->len = initlen;
61 sh->free = 0;
62 if (initlen) {
63 if (init) memcpy(sh->buf, init, initlen);
64 else memset(sh->buf,0,initlen);
65 }
66 sh->buf[initlen] = '\0';
67 return (char*)sh->buf;
68 }
69
70 sds sdsempty(void) {
71 return sdsnewlen("",0);
72 }
73
74 sds sdsnew(const char *init) {
75 size_t initlen = (init == NULL) ? 0 : strlen(init);
76 return sdsnewlen(init, initlen);
77 }
78
79 size_t sdslen(const sds s) {
80 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
81 return sh->len;
82 }
83
84 sds sdsdup(const sds s) {
85 return sdsnewlen(s, sdslen(s));
86 }
87
88 void sdsfree(sds s) {
89 if (s == NULL) return;
90 zfree(s-sizeof(struct sdshdr));
91 }
92
93 size_t sdsavail(sds s) {
94 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
95 return sh->free;
96 }
97
98 void sdsupdatelen(sds s) {
99 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
100 int reallen = strlen(s);
101 sh->free += (sh->len-reallen);
102 sh->len = reallen;
103 }
104
105 static sds sdsMakeRoomFor(sds s, size_t addlen) {
106 struct sdshdr *sh, *newsh;
107 size_t free = sdsavail(s);
108 size_t len, newlen;
109
110 if (free >= addlen) return s;
111 len = sdslen(s);
112 sh = (void*) (s-(sizeof(struct sdshdr)));
113 newlen = (len+addlen)*2;
114 newsh = zrealloc(sh, sizeof(struct sdshdr)+newlen+1);
115 #ifdef SDS_ABORT_ON_OOM
116 if (newsh == NULL) sdsOomAbort();
117 #else
118 if (newsh == NULL) return NULL;
119 #endif
120
121 newsh->free = newlen - len;
122 return newsh->buf;
123 }
124
125 /* Grow the sds to have the specified length. Bytes that were not part of
126 * the original length of the sds will be set to zero. */
127 sds sdsgrowzero(sds s, size_t len) {
128 struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
129 size_t totlen, curlen = sh->len;
130
131 if (len <= curlen) return s;
132 s = sdsMakeRoomFor(s,len-curlen);
133 if (s == NULL) return NULL;
134
135 /* Make sure added region doesn't contain garbage */
136 sh = (void*)(s-(sizeof(struct sdshdr)));
137 memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */
138 totlen = sh->len+sh->free;
139 sh->len = len;
140 sh->free = totlen-sh->len;
141 return s;
142 }
143
144 sds sdscatlen(sds s, void *t, size_t len) {
145 struct sdshdr *sh;
146 size_t curlen = sdslen(s);
147
148 s = sdsMakeRoomFor(s,len);
149 if (s == NULL) return NULL;
150 sh = (void*) (s-(sizeof(struct sdshdr)));
151 memcpy(s+curlen, t, len);
152 sh->len = curlen+len;
153 sh->free = sh->free-len;
154 s[curlen+len] = '\0';
155 return s;
156 }
157
158 sds sdscat(sds s, char *t) {
159 return sdscatlen(s, t, strlen(t));
160 }
161
162 sds sdscpylen(sds s, char *t, size_t len) {
163 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
164 size_t totlen = sh->free+sh->len;
165
166 if (totlen < len) {
167 s = sdsMakeRoomFor(s,len-sh->len);
168 if (s == NULL) return NULL;
169 sh = (void*) (s-(sizeof(struct sdshdr)));
170 totlen = sh->free+sh->len;
171 }
172 memcpy(s, t, len);
173 s[len] = '\0';
174 sh->len = len;
175 sh->free = totlen-len;
176 return s;
177 }
178
179 sds sdscpy(sds s, char *t) {
180 return sdscpylen(s, t, strlen(t));
181 }
182
183 sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
184 va_list cpy;
185 char *buf, *t;
186 size_t buflen = 16;
187
188 while(1) {
189 buf = zmalloc(buflen);
190 #ifdef SDS_ABORT_ON_OOM
191 if (buf == NULL) sdsOomAbort();
192 #else
193 if (buf == NULL) return NULL;
194 #endif
195 buf[buflen-2] = '\0';
196 va_copy(cpy,ap);
197 vsnprintf(buf, buflen, fmt, cpy);
198 if (buf[buflen-2] != '\0') {
199 zfree(buf);
200 buflen *= 2;
201 continue;
202 }
203 break;
204 }
205 t = sdscat(s, buf);
206 zfree(buf);
207 return t;
208 }
209
210 sds sdscatprintf(sds s, const char *fmt, ...) {
211 va_list ap;
212 char *t;
213 va_start(ap, fmt);
214 t = sdscatvprintf(s,fmt,ap);
215 va_end(ap);
216 return t;
217 }
218
219 sds sdstrim(sds s, const char *cset) {
220 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
221 char *start, *end, *sp, *ep;
222 size_t len;
223
224 sp = start = s;
225 ep = end = s+sdslen(s)-1;
226 while(sp <= end && strchr(cset, *sp)) sp++;
227 while(ep > start && strchr(cset, *ep)) ep--;
228 len = (sp > ep) ? 0 : ((ep-sp)+1);
229 if (sh->buf != sp) memmove(sh->buf, sp, len);
230 sh->buf[len] = '\0';
231 sh->free = sh->free+(sh->len-len);
232 sh->len = len;
233 return s;
234 }
235
236 sds sdsrange(sds s, int start, int end) {
237 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
238 size_t newlen, len = sdslen(s);
239
240 if (len == 0) return s;
241 if (start < 0) {
242 start = len+start;
243 if (start < 0) start = 0;
244 }
245 if (end < 0) {
246 end = len+end;
247 if (end < 0) end = 0;
248 }
249 newlen = (start > end) ? 0 : (end-start)+1;
250 if (newlen != 0) {
251 if (start >= (signed)len) {
252 newlen = 0;
253 } else if (end >= (signed)len) {
254 end = len-1;
255 newlen = (start > end) ? 0 : (end-start)+1;
256 }
257 } else {
258 start = 0;
259 }
260 if (start && newlen) memmove(sh->buf, sh->buf+start, newlen);
261 sh->buf[newlen] = 0;
262 sh->free = sh->free+(sh->len-newlen);
263 sh->len = newlen;
264 return s;
265 }
266
267 void sdstolower(sds s) {
268 int len = sdslen(s), j;
269
270 for (j = 0; j < len; j++) s[j] = tolower(s[j]);
271 }
272
273 void sdstoupper(sds s) {
274 int len = sdslen(s), j;
275
276 for (j = 0; j < len; j++) s[j] = toupper(s[j]);
277 }
278
279 int sdscmp(sds s1, sds s2) {
280 size_t l1, l2, minlen;
281 int cmp;
282
283 l1 = sdslen(s1);
284 l2 = sdslen(s2);
285 minlen = (l1 < l2) ? l1 : l2;
286 cmp = memcmp(s1,s2,minlen);
287 if (cmp == 0) return l1-l2;
288 return cmp;
289 }
290
291 /* Split 's' with separator in 'sep'. An array
292 * of sds strings is returned. *count will be set
293 * by reference to the number of tokens returned.
294 *
295 * On out of memory, zero length string, zero length
296 * separator, NULL is returned.
297 *
298 * Note that 'sep' is able to split a string using
299 * a multi-character separator. For example
300 * sdssplit("foo_-_bar","_-_"); will return two
301 * elements "foo" and "bar".
302 *
303 * This version of the function is binary-safe but
304 * requires length arguments. sdssplit() is just the
305 * same function but for zero-terminated strings.
306 */
307 sds *sdssplitlen(char *s, int len, char *sep, int seplen, int *count) {
308 int elements = 0, slots = 5, start = 0, j;
309
310 sds *tokens = zmalloc(sizeof(sds)*slots);
311 #ifdef SDS_ABORT_ON_OOM
312 if (tokens == NULL) sdsOomAbort();
313 #endif
314 if (seplen < 1 || len < 0 || tokens == NULL) {
315 *count = 0;
316 return NULL;
317 }
318 if (len == 0) {
319 *count = 0;
320 return tokens;
321 }
322 for (j = 0; j < (len-(seplen-1)); j++) {
323 /* make sure there is room for the next element and the final one */
324 if (slots < elements+2) {
325 sds *newtokens;
326
327 slots *= 2;
328 newtokens = zrealloc(tokens,sizeof(sds)*slots);
329 if (newtokens == NULL) {
330 #ifdef SDS_ABORT_ON_OOM
331 sdsOomAbort();
332 #else
333 goto cleanup;
334 #endif
335 }
336 tokens = newtokens;
337 }
338 /* search the separator */
339 if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) {
340 tokens[elements] = sdsnewlen(s+start,j-start);
341 if (tokens[elements] == NULL) {
342 #ifdef SDS_ABORT_ON_OOM
343 sdsOomAbort();
344 #else
345 goto cleanup;
346 #endif
347 }
348 elements++;
349 start = j+seplen;
350 j = j+seplen-1; /* skip the separator */
351 }
352 }
353 /* Add the final element. We are sure there is room in the tokens array. */
354 tokens[elements] = sdsnewlen(s+start,len-start);
355 if (tokens[elements] == NULL) {
356 #ifdef SDS_ABORT_ON_OOM
357 sdsOomAbort();
358 #else
359 goto cleanup;
360 #endif
361 }
362 elements++;
363 *count = elements;
364 return tokens;
365
366 #ifndef SDS_ABORT_ON_OOM
367 cleanup:
368 {
369 int i;
370 for (i = 0; i < elements; i++) sdsfree(tokens[i]);
371 zfree(tokens);
372 *count = 0;
373 return NULL;
374 }
375 #endif
376 }
377
378 void sdsfreesplitres(sds *tokens, int count) {
379 if (!tokens) return;
380 while(count--)
381 sdsfree(tokens[count]);
382 zfree(tokens);
383 }
384
385 sds sdsfromlonglong(long long value) {
386 char buf[32], *p;
387 unsigned long long v;
388
389 v = (value < 0) ? -value : value;
390 p = buf+31; /* point to the last character */
391 do {
392 *p-- = '0'+(v%10);
393 v /= 10;
394 } while(v);
395 if (value < 0) *p-- = '-';
396 p++;
397 return sdsnewlen(p,32-(p-buf));
398 }
399
400 sds sdscatrepr(sds s, char *p, size_t len) {
401 s = sdscatlen(s,"\"",1);
402 while(len--) {
403 switch(*p) {
404 case '\\':
405 case '"':
406 s = sdscatprintf(s,"\\%c",*p);
407 break;
408 case '\n': s = sdscatlen(s,"\\n",2); break;
409 case '\r': s = sdscatlen(s,"\\r",2); break;
410 case '\t': s = sdscatlen(s,"\\t",2); break;
411 case '\a': s = sdscatlen(s,"\\a",2); break;
412 case '\b': s = sdscatlen(s,"\\b",2); break;
413 default:
414 if (isprint(*p))
415 s = sdscatprintf(s,"%c",*p);
416 else
417 s = sdscatprintf(s,"\\x%02x",(unsigned char)*p);
418 break;
419 }
420 p++;
421 }
422 return sdscatlen(s,"\"",1);
423 }
424
425 /* Helper function for sdssplitargs() that returns non zero if 'c'
426 * is a valid hex digit. */
427 int is_hex_digit(char c) {
428 return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
429 (c >= 'A' && c <= 'F');
430 }
431
432 /* Helper function for sdssplitargs() that converts an hex digit into an
433 * integer from 0 to 15 */
434 int hex_digit_to_int(char c) {
435 switch(c) {
436 case '0': return 0;
437 case '1': return 1;
438 case '2': return 2;
439 case '3': return 3;
440 case '4': return 4;
441 case '5': return 5;
442 case '6': return 6;
443 case '7': return 7;
444 case '8': return 8;
445 case '9': return 9;
446 case 'a': case 'A': return 10;
447 case 'b': case 'B': return 11;
448 case 'c': case 'C': return 12;
449 case 'd': case 'D': return 13;
450 case 'e': case 'E': return 14;
451 case 'f': case 'F': return 15;
452 default: return 0;
453 }
454 }
455
456 /* Split a line into arguments, where every argument can be in the
457 * following programming-language REPL-alike form:
458 *
459 * foo bar "newline are supported\n" and "\xff\x00otherstuff"
460 *
461 * The number of arguments is stored into *argc, and an array
462 * of sds is returned. The caller should sdsfree() all the returned
463 * strings and finally zfree() the array itself.
464 *
465 * Note that sdscatrepr() is able to convert back a string into
466 * a quoted string in the same format sdssplitargs() is able to parse.
467 */
468 sds *sdssplitargs(char *line, int *argc) {
469 char *p = line;
470 char *current = NULL;
471 char **vector = NULL;
472
473 *argc = 0;
474 while(1) {
475 /* skip blanks */
476 while(*p && isspace(*p)) p++;
477 if (*p) {
478 /* get a token */
479 int inq=0; /* set to 1 if we are in "quotes" */
480 int done=0;
481
482 if (current == NULL) current = sdsempty();
483 while(!done) {
484 if (inq) {
485 if (*p == '\\' && *(p+1) == 'x' &&
486 is_hex_digit(*(p+2)) &&
487 is_hex_digit(*(p+3)))
488 {
489 unsigned char byte;
490
491 byte = (hex_digit_to_int(*(p+2))*16)+
492 hex_digit_to_int(*(p+3));
493 current = sdscatlen(current,(char*)&byte,1);
494 p += 3;
495 } else if (*p == '\\' && *(p+1)) {
496 char c;
497
498 p++;
499 switch(*p) {
500 case 'n': c = '\n'; break;
501 case 'r': c = '\r'; break;
502 case 't': c = '\t'; break;
503 case 'b': c = '\b'; break;
504 case 'a': c = '\a'; break;
505 default: c = *p; break;
506 }
507 current = sdscatlen(current,&c,1);
508 } else if (*p == '"') {
509 /* closing quote must be followed by a space */
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 {
519 switch(*p) {
520 case ' ':
521 case '\n':
522 case '\r':
523 case '\t':
524 case '\0':
525 done=1;
526 break;
527 case '"':
528 inq=1;
529 break;
530 default:
531 current = sdscatlen(current,p,1);
532 break;
533 }
534 }
535 if (*p) p++;
536 }
537 /* add the token to the vector */
538 vector = zrealloc(vector,((*argc)+1)*sizeof(char*));
539 vector[*argc] = current;
540 (*argc)++;
541 current = NULL;
542 } else {
543 return vector;
544 }
545 }
546
547 err:
548 while((*argc)--)
549 sdsfree(vector[*argc]);
550 zfree(vector);
551 if (current) sdsfree(current);
552 return NULL;
553 }
554
555 #ifdef SDS_TEST_MAIN
556 #include <stdio.h>
557 #include "testhelp.h"
558
559 int main(void) {
560 {
561 sds x = sdsnew("foo"), y;
562
563 test_cond("Create a string and obtain the length",
564 sdslen(x) == 3 && memcmp(x,"foo\0",4) == 0)
565
566 sdsfree(x);
567 x = sdsnewlen("foo",2);
568 test_cond("Create a string with specified length",
569 sdslen(x) == 2 && memcmp(x,"fo\0",3) == 0)
570
571 x = sdscat(x,"bar");
572 test_cond("Strings concatenation",
573 sdslen(x) == 5 && memcmp(x,"fobar\0",6) == 0);
574
575 x = sdscpy(x,"a");
576 test_cond("sdscpy() against an originally longer string",
577 sdslen(x) == 1 && memcmp(x,"a\0",2) == 0)
578
579 x = sdscpy(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk");
580 test_cond("sdscpy() against an originally shorter string",
581 sdslen(x) == 33 &&
582 memcmp(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0)
583
584 sdsfree(x);
585 x = sdscatprintf(sdsempty(),"%d",123);
586 test_cond("sdscatprintf() seems working in the base case",
587 sdslen(x) == 3 && memcmp(x,"123\0",4) ==0)
588
589 sdsfree(x);
590 x = sdstrim(sdsnew("xxciaoyyy"),"xy");
591 test_cond("sdstrim() correctly trims characters",
592 sdslen(x) == 4 && memcmp(x,"ciao\0",5) == 0)
593
594 y = sdsrange(sdsdup(x),1,1);
595 test_cond("sdsrange(...,1,1)",
596 sdslen(y) == 1 && memcmp(y,"i\0",2) == 0)
597
598 sdsfree(y);
599 y = sdsrange(sdsdup(x),1,-1);
600 test_cond("sdsrange(...,1,-1)",
601 sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)
602
603 sdsfree(y);
604 y = sdsrange(sdsdup(x),-2,-1);
605 test_cond("sdsrange(...,-2,-1)",
606 sdslen(y) == 2 && memcmp(y,"ao\0",3) == 0)
607
608 sdsfree(y);
609 y = sdsrange(sdsdup(x),2,1);
610 test_cond("sdsrange(...,2,1)",
611 sdslen(y) == 0 && memcmp(y,"\0",1) == 0)
612
613 sdsfree(y);
614 y = sdsrange(sdsdup(x),1,100);
615 test_cond("sdsrange(...,1,100)",
616 sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)
617
618 sdsfree(y);
619 y = sdsrange(sdsdup(x),100,100);
620 test_cond("sdsrange(...,100,100)",
621 sdslen(y) == 0 && memcmp(y,"\0",1) == 0)
622
623 sdsfree(y);
624 sdsfree(x);
625 x = sdsnew("foo");
626 y = sdsnew("foa");
627 test_cond("sdscmp(foo,foa)", sdscmp(x,y) > 0)
628
629 sdsfree(y);
630 sdsfree(x);
631 x = sdsnew("bar");
632 y = sdsnew("bar");
633 test_cond("sdscmp(bar,bar)", sdscmp(x,y) == 0)
634
635 sdsfree(y);
636 sdsfree(x);
637 x = sdsnew("aar");
638 y = sdsnew("bar");
639 test_cond("sdscmp(bar,bar)", sdscmp(x,y) < 0)
640 }
641 test_report()
642 }
643 #endif