]>
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
46 static void sdsOomAbort(void) {
47 fprintf(stderr
,"SDS: Out Of Memory (SDS_ABORT_ON_OOM defined)\n");
51 sds
sdsnewlen(const void *init
, size_t initlen
) {
55 sh
= zmalloc(sizeof(struct sdshdr
)+initlen
+1);
57 sh
= zcalloc(sizeof(struct sdshdr
)+initlen
+1);
59 #ifdef SDS_ABORT_ON_OOM
60 if (sh
== NULL
) sdsOomAbort();
62 if (sh
== NULL
) return NULL
;
67 memcpy(sh
->buf
, init
, initlen
);
68 sh
->buf
[initlen
] = '\0';
69 return (char*)sh
->buf
;
73 return sdsnewlen("",0);
76 sds
sdsnew(const char *init
) {
77 size_t initlen
= (init
== NULL
) ? 0 : strlen(init
);
78 return sdsnewlen(init
, initlen
);
81 sds
sdsdup(const sds s
) {
82 return sdsnewlen(s
, sdslen(s
));
86 if (s
== NULL
) return;
87 zfree(s
-sizeof(struct sdshdr
));
90 void sdsupdatelen(sds s
) {
91 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
92 int reallen
= strlen(s
);
93 sh
->free
+= (sh
->len
-reallen
);
97 static sds
sdsMakeRoomFor(sds s
, size_t addlen
) {
98 struct sdshdr
*sh
, *newsh
;
99 size_t free
= sdsavail(s
);
102 if (free
>= addlen
) return s
;
104 sh
= (void*) (s
-(sizeof(struct sdshdr
)));
105 newlen
= (len
+addlen
)*2;
106 newsh
= zrealloc(sh
, sizeof(struct sdshdr
)+newlen
+1);
107 #ifdef SDS_ABORT_ON_OOM
108 if (newsh
== NULL
) sdsOomAbort();
110 if (newsh
== NULL
) return NULL
;
113 newsh
->free
= newlen
- len
;
117 /* Grow the sds to have the specified length. Bytes that were not part of
118 * the original length of the sds will be set to zero. */
119 sds
sdsgrowzero(sds s
, size_t len
) {
120 struct sdshdr
*sh
= (void*)(s
-(sizeof(struct sdshdr
)));
121 size_t totlen
, curlen
= sh
->len
;
123 if (len
<= curlen
) return s
;
124 s
= sdsMakeRoomFor(s
,len
-curlen
);
125 if (s
== NULL
) return NULL
;
127 /* Make sure added region doesn't contain garbage */
128 sh
= (void*)(s
-(sizeof(struct sdshdr
)));
129 memset(s
+curlen
,0,(len
-curlen
+1)); /* also set trailing \0 byte */
130 totlen
= sh
->len
+sh
->free
;
132 sh
->free
= totlen
-sh
->len
;
136 sds
sdscatlen(sds s
, void *t
, size_t len
) {
138 size_t curlen
= sdslen(s
);
140 s
= sdsMakeRoomFor(s
,len
);
141 if (s
== NULL
) return NULL
;
142 sh
= (void*) (s
-(sizeof(struct sdshdr
)));
143 memcpy(s
+curlen
, t
, len
);
144 sh
->len
= curlen
+len
;
145 sh
->free
= sh
->free
-len
;
146 s
[curlen
+len
] = '\0';
150 sds
sdscat(sds s
, char *t
) {
151 return sdscatlen(s
, t
, strlen(t
));
154 sds
sdscpylen(sds s
, char *t
, size_t len
) {
155 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
156 size_t totlen
= sh
->free
+sh
->len
;
159 s
= sdsMakeRoomFor(s
,len
-sh
->len
);
160 if (s
== NULL
) return NULL
;
161 sh
= (void*) (s
-(sizeof(struct sdshdr
)));
162 totlen
= sh
->free
+sh
->len
;
167 sh
->free
= totlen
-len
;
171 sds
sdscpy(sds s
, char *t
) {
172 return sdscpylen(s
, t
, strlen(t
));
175 sds
sdscatvprintf(sds s
, const char *fmt
, va_list ap
) {
181 buf
= zmalloc(buflen
);
182 #ifdef SDS_ABORT_ON_OOM
183 if (buf
== NULL
) sdsOomAbort();
185 if (buf
== NULL
) return NULL
;
187 buf
[buflen
-2] = '\0';
189 vsnprintf(buf
, buflen
, fmt
, cpy
);
190 if (buf
[buflen
-2] != '\0') {
202 sds
sdscatprintf(sds s
, const char *fmt
, ...) {
206 t
= sdscatvprintf(s
,fmt
,ap
);
211 sds
sdstrim(sds s
, const char *cset
) {
212 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
213 char *start
, *end
, *sp
, *ep
;
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
);
223 sh
->free
= sh
->free
+(sh
->len
-len
);
228 sds
sdsrange(sds s
, int start
, int end
) {
229 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
230 size_t newlen
, len
= sdslen(s
);
232 if (len
== 0) return s
;
235 if (start
< 0) start
= 0;
239 if (end
< 0) end
= 0;
241 newlen
= (start
> end
) ? 0 : (end
-start
)+1;
243 if (start
>= (signed)len
) {
245 } else if (end
>= (signed)len
) {
247 newlen
= (start
> end
) ? 0 : (end
-start
)+1;
252 if (start
&& newlen
) memmove(sh
->buf
, sh
->buf
+start
, newlen
);
254 sh
->free
= sh
->free
+(sh
->len
-newlen
);
259 void sdstolower(sds s
) {
260 int len
= sdslen(s
), j
;
262 for (j
= 0; j
< len
; j
++) s
[j
] = tolower(s
[j
]);
265 void sdstoupper(sds s
) {
266 int len
= sdslen(s
), j
;
268 for (j
= 0; j
< len
; j
++) s
[j
] = toupper(s
[j
]);
271 int sdscmp(sds s1
, sds s2
) {
272 size_t l1
, l2
, minlen
;
277 minlen
= (l1
< l2
) ? l1
: l2
;
278 cmp
= memcmp(s1
,s2
,minlen
);
279 if (cmp
== 0) return l1
-l2
;
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.
287 * On out of memory, zero length string, zero length
288 * separator, NULL is returned.
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".
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.
299 sds
*sdssplitlen(char *s
, int len
, char *sep
, int seplen
, int *count
) {
300 int elements
= 0, slots
= 5, start
= 0, j
;
303 if (seplen
< 1 || len
< 0) return NULL
;
305 tokens
= zmalloc(sizeof(sds
)*slots
);
306 #ifdef SDS_ABORT_ON_OOM
307 if (tokens
== NULL
) sdsOomAbort();
309 if (tokens
== NULL
) return NULL
;
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) {
322 newtokens
= zrealloc(tokens
,sizeof(sds
)*slots
);
323 if (newtokens
== NULL
) {
324 #ifdef SDS_ABORT_ON_OOM
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
344 j
= j
+seplen
-1; /* skip the separator */
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
360 #ifndef SDS_ABORT_ON_OOM
364 for (i
= 0; i
< elements
; i
++) sdsfree(tokens
[i
]);
372 void sdsfreesplitres(sds
*tokens
, int count
) {
375 sdsfree(tokens
[count
]);
379 sds
sdsfromlonglong(long long value
) {
381 unsigned long long v
;
383 v
= (value
< 0) ? -value
: value
;
384 p
= buf
+31; /* point to the last character */
389 if (value
< 0) *p
-- = '-';
391 return sdsnewlen(p
,32-(p
-buf
));
394 sds
sdscatrepr(sds s
, char *p
, size_t len
) {
395 s
= sdscatlen(s
,"\"",1);
400 s
= sdscatprintf(s
,"\\%c",*p
);
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;
409 s
= sdscatprintf(s
,"%c",*p
);
411 s
= sdscatprintf(s
,"\\x%02x",(unsigned char)*p
);
416 return sdscatlen(s
,"\"",1);
419 /* Helper function for sdssplitargs() that returns non zero if 'c'
420 * is a valid hex digit. */
421 int is_hex_digit(char c
) {
422 return (c
>= '0' && c
<= '9') || (c
>= 'a' && c
<= 'f') ||
423 (c
>= 'A' && c
<= 'F');
426 /* Helper function for sdssplitargs() that converts an hex digit into an
427 * integer from 0 to 15 */
428 int hex_digit_to_int(char c
) {
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;
450 /* Split a line into arguments, where every argument can be in the
451 * following programming-language REPL-alike form:
453 * foo bar "newline are supported\n" and "\xff\x00otherstuff"
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.
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.
462 sds
*sdssplitargs(char *line
, int *argc
) {
464 char *current
= NULL
;
465 char **vector
= NULL
;
470 while(*p
&& isspace(*p
)) p
++;
473 int inq
=0; /* set to 1 if we are in "quotes" */
476 if (current
== NULL
) current
= sdsempty();
479 if (*p
== '\\' && *(p
+1) == 'x' &&
480 is_hex_digit(*(p
+2)) &&
481 is_hex_digit(*(p
+3)))
485 byte
= (hex_digit_to_int(*(p
+2))*16)+
486 hex_digit_to_int(*(p
+3));
487 current
= sdscatlen(current
,(char*)&byte
,1);
489 } else if (*p
== '\\' && *(p
+1)) {
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;
501 current
= sdscatlen(current
,&c
,1);
502 } else if (*p
== '"') {
503 /* closing quote must be followed by a space */
504 if (*(p
+1) && !isspace(*(p
+1))) goto err
;
507 /* unterminated quotes */
510 current
= sdscatlen(current
,p
,1);
525 current
= sdscatlen(current
,p
,1);
531 /* add the token to the vector */
532 vector
= zrealloc(vector
,((*argc
)+1)*sizeof(char*));
533 vector
[*argc
] = current
;
543 sdsfree(vector
[*argc
]);
545 if (current
) sdsfree(current
);
549 void sdssplitargs_free(sds
*argv
, int argc
) {
552 for (j
= 0 ;j
< argc
; j
++) sdsfree(argv
[j
]);
558 #include "testhelp.h"
562 sds x
= sdsnew("foo"), y
;
564 test_cond("Create a string and obtain the length",
565 sdslen(x
) == 3 && memcmp(x
,"foo\0",4) == 0)
568 x
= sdsnewlen("foo",2);
569 test_cond("Create a string with specified length",
570 sdslen(x
) == 2 && memcmp(x
,"fo\0",3) == 0)
573 test_cond("Strings concatenation",
574 sdslen(x
) == 5 && memcmp(x
,"fobar\0",6) == 0);
577 test_cond("sdscpy() against an originally longer string",
578 sdslen(x
) == 1 && memcmp(x
,"a\0",2) == 0)
580 x
= sdscpy(x
,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk");
581 test_cond("sdscpy() against an originally shorter string",
583 memcmp(x
,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0)
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)
591 x
= sdstrim(sdsnew("xxciaoyyy"),"xy");
592 test_cond("sdstrim() correctly trims characters",
593 sdslen(x
) == 4 && memcmp(x
,"ciao\0",5) == 0)
595 y
= sdsrange(sdsdup(x
),1,1);
596 test_cond("sdsrange(...,1,1)",
597 sdslen(y
) == 1 && memcmp(y
,"i\0",2) == 0)
600 y
= sdsrange(sdsdup(x
),1,-1);
601 test_cond("sdsrange(...,1,-1)",
602 sdslen(y
) == 3 && memcmp(y
,"iao\0",4) == 0)
605 y
= sdsrange(sdsdup(x
),-2,-1);
606 test_cond("sdsrange(...,-2,-1)",
607 sdslen(y
) == 2 && memcmp(y
,"ao\0",3) == 0)
610 y
= sdsrange(sdsdup(x
),2,1);
611 test_cond("sdsrange(...,2,1)",
612 sdslen(y
) == 0 && memcmp(y
,"\0",1) == 0)
615 y
= sdsrange(sdsdup(x
),1,100);
616 test_cond("sdsrange(...,1,100)",
617 sdslen(y
) == 3 && memcmp(y
,"iao\0",4) == 0)
620 y
= sdsrange(sdsdup(x
),100,100);
621 test_cond("sdsrange(...,100,100)",
622 sdslen(y
) == 0 && memcmp(y
,"\0",1) == 0)
628 test_cond("sdscmp(foo,foa)", sdscmp(x
,y
) > 0)
634 test_cond("sdscmp(bar,bar)", sdscmp(x
,y
) == 0)
640 test_cond("sdscmp(bar,bar)", sdscmp(x
,y
) < 0)