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