]>
git.saurik.com Git - redis.git/blob - src/sds.c
1 /* SDSLib, A C dynamic strings library
3 * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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.
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.
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().
37 #define SDS_ABORT_ON_OOM
47 static void sdsOomAbort(void) {
48 fprintf(stderr
,"SDS: Out Of Memory (SDS_ABORT_ON_OOM defined)\n");
52 sds
sdsnewlen(const void *init
, size_t initlen
) {
56 sh
= zmalloc(sizeof(struct sdshdr
)+initlen
+1);
58 sh
= zcalloc(sizeof(struct sdshdr
)+initlen
+1);
60 #ifdef SDS_ABORT_ON_OOM
61 if (sh
== NULL
) sdsOomAbort();
63 if (sh
== NULL
) return NULL
;
68 memcpy(sh
->buf
, init
, initlen
);
69 sh
->buf
[initlen
] = '\0';
70 return (char*)sh
->buf
;
74 return sdsnewlen("",0);
77 sds
sdsnew(const char *init
) {
78 size_t initlen
= (init
== NULL
) ? 0 : strlen(init
);
79 return sdsnewlen(init
, initlen
);
82 sds
sdsdup(const sds s
) {
83 return sdsnewlen(s
, sdslen(s
));
87 if (s
== NULL
) return;
88 zfree(s
-sizeof(struct sdshdr
));
91 void sdsupdatelen(sds s
) {
92 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
93 int reallen
= strlen(s
);
94 sh
->free
+= (sh
->len
-reallen
);
98 void sdsclear(sds s
) {
99 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
105 /* Enlarge the free space at the end of the sds string so that the caller
106 * is sure that after calling this function can overwrite up to addlen
107 * bytes after the end of the string, plus one more byte for nul term.
109 * Note: this does not change the *size* of the sds string as returned
110 * by sdslen(), but only the free buffer space we have. */
111 sds
sdsMakeRoomFor(sds s
, size_t addlen
) {
112 struct sdshdr
*sh
, *newsh
;
113 size_t free
= sdsavail(s
);
116 if (free
>= addlen
) return s
;
118 sh
= (void*) (s
-(sizeof(struct sdshdr
)));
119 newlen
= (len
+addlen
);
120 if (newlen
< SDS_MAX_PREALLOC
)
123 newlen
+= SDS_MAX_PREALLOC
;
124 newsh
= zrealloc(sh
, sizeof(struct sdshdr
)+newlen
+1);
125 #ifdef SDS_ABORT_ON_OOM
126 if (newsh
== NULL
) sdsOomAbort();
128 if (newsh
== NULL
) return NULL
;
131 newsh
->free
= newlen
- len
;
135 /* Increment the sds length and decrements the left free space at the
136 * end of the string accordingly to 'incr'. Also set the null term
137 * in the new end of the string.
139 * This function is used in order to fix the string length after the
140 * user calls sdsMakeRoomFor(), writes something after the end of
141 * the current string, and finally needs to set the new length.
143 * Note: it is possible to use a negative increment in order to
144 * right-trim the string.
146 * Using sdsIncrLen() and sdsMakeRoomFor() it is possible to mount the
147 * following schema to cat bytes coming from the kerenl to the end of an
148 * sds string new things without copying into an intermediate buffer:
150 * oldlen = sdslen(s);
151 * s = sdsMakeRoomFor(s, BUFFER_SIZE);
152 * nread = read(fd, s+oldlen, BUFFER_SIZE);
153 * ... check for nread <= 0 and handle it ...
154 * sdsIncrLen(s, nhread);
156 void sdsIncrLen(sds s
, int incr
) {
157 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
159 assert(sh
->free
>= incr
);
162 assert(sh
->free
>= 0);
166 /* Grow the sds to have the specified length. Bytes that were not part of
167 * the original length of the sds will be set to zero. */
168 sds
sdsgrowzero(sds s
, size_t len
) {
169 struct sdshdr
*sh
= (void*)(s
-(sizeof(struct sdshdr
)));
170 size_t totlen
, curlen
= sh
->len
;
172 if (len
<= curlen
) return s
;
173 s
= sdsMakeRoomFor(s
,len
-curlen
);
174 if (s
== NULL
) return NULL
;
176 /* Make sure added region doesn't contain garbage */
177 sh
= (void*)(s
-(sizeof(struct sdshdr
)));
178 memset(s
+curlen
,0,(len
-curlen
+1)); /* also set trailing \0 byte */
179 totlen
= sh
->len
+sh
->free
;
181 sh
->free
= totlen
-sh
->len
;
185 sds
sdscatlen(sds s
, void *t
, size_t len
) {
187 size_t curlen
= sdslen(s
);
189 s
= sdsMakeRoomFor(s
,len
);
190 if (s
== NULL
) return NULL
;
191 sh
= (void*) (s
-(sizeof(struct sdshdr
)));
192 memcpy(s
+curlen
, t
, len
);
193 sh
->len
= curlen
+len
;
194 sh
->free
= sh
->free
-len
;
195 s
[curlen
+len
] = '\0';
199 sds
sdscat(sds s
, char *t
) {
200 return sdscatlen(s
, t
, strlen(t
));
203 sds
sdscatsds(sds s
, sds t
) {
204 return sdscatlen(s
, t
, sdslen(t
));
207 sds
sdscpylen(sds s
, char *t
, size_t len
) {
208 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
209 size_t totlen
= sh
->free
+sh
->len
;
212 s
= sdsMakeRoomFor(s
,len
-sh
->len
);
213 if (s
== NULL
) return NULL
;
214 sh
= (void*) (s
-(sizeof(struct sdshdr
)));
215 totlen
= sh
->free
+sh
->len
;
220 sh
->free
= totlen
-len
;
224 sds
sdscpy(sds s
, char *t
) {
225 return sdscpylen(s
, t
, strlen(t
));
228 sds
sdscatvprintf(sds s
, const char *fmt
, va_list ap
) {
234 buf
= zmalloc(buflen
);
235 #ifdef SDS_ABORT_ON_OOM
236 if (buf
== NULL
) sdsOomAbort();
238 if (buf
== NULL
) return NULL
;
240 buf
[buflen
-2] = '\0';
242 vsnprintf(buf
, buflen
, fmt
, cpy
);
243 if (buf
[buflen
-2] != '\0') {
255 sds
sdscatprintf(sds s
, const char *fmt
, ...) {
259 t
= sdscatvprintf(s
,fmt
,ap
);
264 sds
sdstrim(sds s
, const char *cset
) {
265 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
266 char *start
, *end
, *sp
, *ep
;
270 ep
= end
= s
+sdslen(s
)-1;
271 while(sp
<= end
&& strchr(cset
, *sp
)) sp
++;
272 while(ep
> start
&& strchr(cset
, *ep
)) ep
--;
273 len
= (sp
> ep
) ? 0 : ((ep
-sp
)+1);
274 if (sh
->buf
!= sp
) memmove(sh
->buf
, sp
, len
);
276 sh
->free
= sh
->free
+(sh
->len
-len
);
281 sds
sdsrange(sds s
, int start
, int end
) {
282 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
283 size_t newlen
, len
= sdslen(s
);
285 if (len
== 0) return s
;
288 if (start
< 0) start
= 0;
292 if (end
< 0) end
= 0;
294 newlen
= (start
> end
) ? 0 : (end
-start
)+1;
296 if (start
>= (signed)len
) {
298 } else if (end
>= (signed)len
) {
300 newlen
= (start
> end
) ? 0 : (end
-start
)+1;
305 if (start
&& newlen
) memmove(sh
->buf
, sh
->buf
+start
, newlen
);
307 sh
->free
= sh
->free
+(sh
->len
-newlen
);
312 void sdstolower(sds s
) {
313 int len
= sdslen(s
), j
;
315 for (j
= 0; j
< len
; j
++) s
[j
] = tolower(s
[j
]);
318 void sdstoupper(sds s
) {
319 int len
= sdslen(s
), j
;
321 for (j
= 0; j
< len
; j
++) s
[j
] = toupper(s
[j
]);
324 int sdscmp(sds s1
, sds s2
) {
325 size_t l1
, l2
, minlen
;
330 minlen
= (l1
< l2
) ? l1
: l2
;
331 cmp
= memcmp(s1
,s2
,minlen
);
332 if (cmp
== 0) return l1
-l2
;
336 /* Split 's' with separator in 'sep'. An array
337 * of sds strings is returned. *count will be set
338 * by reference to the number of tokens returned.
340 * On out of memory, zero length string, zero length
341 * separator, NULL is returned.
343 * Note that 'sep' is able to split a string using
344 * a multi-character separator. For example
345 * sdssplit("foo_-_bar","_-_"); will return two
346 * elements "foo" and "bar".
348 * This version of the function is binary-safe but
349 * requires length arguments. sdssplit() is just the
350 * same function but for zero-terminated strings.
352 sds
*sdssplitlen(char *s
, int len
, char *sep
, int seplen
, int *count
) {
353 int elements
= 0, slots
= 5, start
= 0, j
;
356 if (seplen
< 1 || len
< 0) return NULL
;
358 tokens
= zmalloc(sizeof(sds
)*slots
);
359 #ifdef SDS_ABORT_ON_OOM
360 if (tokens
== NULL
) sdsOomAbort();
362 if (tokens
== NULL
) return NULL
;
369 for (j
= 0; j
< (len
-(seplen
-1)); j
++) {
370 /* make sure there is room for the next element and the final one */
371 if (slots
< elements
+2) {
375 newtokens
= zrealloc(tokens
,sizeof(sds
)*slots
);
376 if (newtokens
== NULL
) {
377 #ifdef SDS_ABORT_ON_OOM
385 /* search the separator */
386 if ((seplen
== 1 && *(s
+j
) == sep
[0]) || (memcmp(s
+j
,sep
,seplen
) == 0)) {
387 tokens
[elements
] = sdsnewlen(s
+start
,j
-start
);
388 if (tokens
[elements
] == NULL
) {
389 #ifdef SDS_ABORT_ON_OOM
397 j
= j
+seplen
-1; /* skip the separator */
400 /* Add the final element. We are sure there is room in the tokens array. */
401 tokens
[elements
] = sdsnewlen(s
+start
,len
-start
);
402 if (tokens
[elements
] == NULL
) {
403 #ifdef SDS_ABORT_ON_OOM
413 #ifndef SDS_ABORT_ON_OOM
417 for (i
= 0; i
< elements
; i
++) sdsfree(tokens
[i
]);
425 void sdsfreesplitres(sds
*tokens
, int count
) {
428 sdsfree(tokens
[count
]);
432 sds
sdsfromlonglong(long long value
) {
434 unsigned long long v
;
436 v
= (value
< 0) ? -value
: value
;
437 p
= buf
+31; /* point to the last character */
442 if (value
< 0) *p
-- = '-';
444 return sdsnewlen(p
,32-(p
-buf
));
447 sds
sdscatrepr(sds s
, char *p
, size_t len
) {
448 s
= sdscatlen(s
,"\"",1);
453 s
= sdscatprintf(s
,"\\%c",*p
);
455 case '\n': s
= sdscatlen(s
,"\\n",2); break;
456 case '\r': s
= sdscatlen(s
,"\\r",2); break;
457 case '\t': s
= sdscatlen(s
,"\\t",2); break;
458 case '\a': s
= sdscatlen(s
,"\\a",2); break;
459 case '\b': s
= sdscatlen(s
,"\\b",2); break;
462 s
= sdscatprintf(s
,"%c",*p
);
464 s
= sdscatprintf(s
,"\\x%02x",(unsigned char)*p
);
469 return sdscatlen(s
,"\"",1);
472 /* Helper function for sdssplitargs() that returns non zero if 'c'
473 * is a valid hex digit. */
474 int is_hex_digit(char c
) {
475 return (c
>= '0' && c
<= '9') || (c
>= 'a' && c
<= 'f') ||
476 (c
>= 'A' && c
<= 'F');
479 /* Helper function for sdssplitargs() that converts an hex digit into an
480 * integer from 0 to 15 */
481 int hex_digit_to_int(char c
) {
493 case 'a': case 'A': return 10;
494 case 'b': case 'B': return 11;
495 case 'c': case 'C': return 12;
496 case 'd': case 'D': return 13;
497 case 'e': case 'E': return 14;
498 case 'f': case 'F': return 15;
503 /* Split a line into arguments, where every argument can be in the
504 * following programming-language REPL-alike form:
506 * foo bar "newline are supported\n" and "\xff\x00otherstuff"
508 * The number of arguments is stored into *argc, and an array
509 * of sds is returned. The caller should sdsfree() all the returned
510 * strings and finally zfree() the array itself.
512 * Note that sdscatrepr() is able to convert back a string into
513 * a quoted string in the same format sdssplitargs() is able to parse.
515 sds
*sdssplitargs(char *line
, int *argc
) {
517 char *current
= NULL
;
518 char **vector
= NULL
;
523 while(*p
&& isspace(*p
)) p
++;
526 int inq
=0; /* set to 1 if we are in "quotes" */
527 int insq
=0; /* set to 1 if we are in 'single quotes' */
530 if (current
== NULL
) current
= sdsempty();
533 if (*p
== '\\' && *(p
+1) == 'x' &&
534 is_hex_digit(*(p
+2)) &&
535 is_hex_digit(*(p
+3)))
539 byte
= (hex_digit_to_int(*(p
+2))*16)+
540 hex_digit_to_int(*(p
+3));
541 current
= sdscatlen(current
,(char*)&byte
,1);
543 } else if (*p
== '\\' && *(p
+1)) {
548 case 'n': c
= '\n'; break;
549 case 'r': c
= '\r'; break;
550 case 't': c
= '\t'; break;
551 case 'b': c
= '\b'; break;
552 case 'a': c
= '\a'; break;
553 default: c
= *p
; break;
555 current
= sdscatlen(current
,&c
,1);
556 } else if (*p
== '"') {
557 /* closing quote must be followed by a space or
559 if (*(p
+1) && !isspace(*(p
+1))) goto err
;
562 /* unterminated quotes */
565 current
= sdscatlen(current
,p
,1);
568 if (*p
== '\\' && *(p
+1) == '\'') {
570 current
= sdscatlen(current
,"'",1);
571 } else if (*p
== '\'') {
572 /* closing quote must be followed by a space or
574 if (*(p
+1) && !isspace(*(p
+1))) goto err
;
577 /* unterminated quotes */
580 current
= sdscatlen(current
,p
,1);
598 current
= sdscatlen(current
,p
,1);
604 /* add the token to the vector */
605 vector
= zrealloc(vector
,((*argc
)+1)*sizeof(char*));
606 vector
[*argc
] = current
;
616 sdsfree(vector
[*argc
]);
618 if (current
) sdsfree(current
);
622 void sdssplitargs_free(sds
*argv
, int argc
) {
625 for (j
= 0 ;j
< argc
; j
++) sdsfree(argv
[j
]);
629 /* Modify the string substituting all the occurrences of the set of
630 * characters specifed in the 'from' string to the corresponding character
633 * For instance: sdsmapchars(mystring, "ho", "01", 2)
634 * will have the effect of turning the string "hello" into "0ell1".
636 * The function returns the sds string pointer, that is always the same
637 * as the input pointer since no resize is needed. */
638 sds
sdsmapchars(sds s
, char *from
, char *to
, size_t setlen
) {
639 size_t j
, i
, l
= sdslen(s
);
641 for (j
= 0; j
< l
; j
++) {
642 for (i
= 0; i
< setlen
; i
++) {
643 if (s
[j
] == from
[i
]) {
654 #include "testhelp.h"
659 sds x
= sdsnew("foo"), y
;
661 test_cond("Create a string and obtain the length",
662 sdslen(x
) == 3 && memcmp(x
,"foo\0",4) == 0)
665 x
= sdsnewlen("foo",2);
666 test_cond("Create a string with specified length",
667 sdslen(x
) == 2 && memcmp(x
,"fo\0",3) == 0)
670 test_cond("Strings concatenation",
671 sdslen(x
) == 5 && memcmp(x
,"fobar\0",6) == 0);
674 test_cond("sdscpy() against an originally longer string",
675 sdslen(x
) == 1 && memcmp(x
,"a\0",2) == 0)
677 x
= sdscpy(x
,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk");
678 test_cond("sdscpy() against an originally shorter string",
680 memcmp(x
,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0)
683 x
= sdscatprintf(sdsempty(),"%d",123);
684 test_cond("sdscatprintf() seems working in the base case",
685 sdslen(x
) == 3 && memcmp(x
,"123\0",4) ==0)
688 x
= sdstrim(sdsnew("xxciaoyyy"),"xy");
689 test_cond("sdstrim() correctly trims characters",
690 sdslen(x
) == 4 && memcmp(x
,"ciao\0",5) == 0)
692 y
= sdsrange(sdsdup(x
),1,1);
693 test_cond("sdsrange(...,1,1)",
694 sdslen(y
) == 1 && memcmp(y
,"i\0",2) == 0)
697 y
= sdsrange(sdsdup(x
),1,-1);
698 test_cond("sdsrange(...,1,-1)",
699 sdslen(y
) == 3 && memcmp(y
,"iao\0",4) == 0)
702 y
= sdsrange(sdsdup(x
),-2,-1);
703 test_cond("sdsrange(...,-2,-1)",
704 sdslen(y
) == 2 && memcmp(y
,"ao\0",3) == 0)
707 y
= sdsrange(sdsdup(x
),2,1);
708 test_cond("sdsrange(...,2,1)",
709 sdslen(y
) == 0 && memcmp(y
,"\0",1) == 0)
712 y
= sdsrange(sdsdup(x
),1,100);
713 test_cond("sdsrange(...,1,100)",
714 sdslen(y
) == 3 && memcmp(y
,"iao\0",4) == 0)
717 y
= sdsrange(sdsdup(x
),100,100);
718 test_cond("sdsrange(...,100,100)",
719 sdslen(y
) == 0 && memcmp(y
,"\0",1) == 0)
725 test_cond("sdscmp(foo,foa)", sdscmp(x
,y
) > 0)
731 test_cond("sdscmp(bar,bar)", sdscmp(x
,y
) == 0)
737 test_cond("sdscmp(bar,bar)", sdscmp(x
,y
) < 0)
744 sh
= (void*) (x
-(sizeof(struct sdshdr
)));
745 test_cond("sdsnew() free/len buffers", sh
->len
== 1 && sh
->free
== 0);
746 x
= sdsMakeRoomFor(x
,1);
747 sh
= (void*) (x
-(sizeof(struct sdshdr
)));
748 test_cond("sdsMakeRoomFor()", sh
->len
== 1 && sh
->free
> 0);
752 test_cond("sdsIncrLen() -- content", x
[0] == '0' && x
[1] == '1');
753 test_cond("sdsIncrLen() -- len", sh
->len
== 2);
754 test_cond("sdsIncrLen() -- free", sh
->free
== oldfree
-1);