]>
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.
31 #define SDS_ABORT_ON_OOM
40 static void sdsOomAbort(void) {
41 fprintf(stderr
,"SDS: Out Of Memory (SDS_ABORT_ON_OOM defined)\n");
45 sds
sdsnewlen(const void *init
, size_t initlen
) {
48 sh
= zmalloc(sizeof(struct sdshdr
)+initlen
+1);
49 #ifdef SDS_ABORT_ON_OOM
50 if (sh
== NULL
) sdsOomAbort();
52 if (sh
== NULL
) return NULL
;
57 if (init
) memcpy(sh
->buf
, init
, initlen
);
58 else memset(sh
->buf
,0,initlen
);
60 sh
->buf
[initlen
] = '\0';
61 return (char*)sh
->buf
;
65 return sdsnewlen("",0);
68 sds
sdsnew(const char *init
) {
69 size_t initlen
= (init
== NULL
) ? 0 : strlen(init
);
70 return sdsnewlen(init
, initlen
);
73 size_t sdslen(const sds s
) {
74 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
78 sds
sdsdup(const sds s
) {
79 return sdsnewlen(s
, sdslen(s
));
83 if (s
== NULL
) return;
84 zfree(s
-sizeof(struct sdshdr
));
87 size_t sdsavail(sds s
) {
88 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
92 void sdsupdatelen(sds s
) {
93 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
94 int reallen
= strlen(s
);
95 sh
->free
+= (sh
->len
-reallen
);
99 static sds
sdsMakeRoomFor(sds s
, size_t addlen
) {
100 struct sdshdr
*sh
, *newsh
;
101 size_t free
= sdsavail(s
);
104 if (free
>= addlen
) return s
;
106 sh
= (void*) (s
-(sizeof(struct sdshdr
)));
107 newlen
= (len
+addlen
)*2;
108 newsh
= zrealloc(sh
, sizeof(struct sdshdr
)+newlen
+1);
109 #ifdef SDS_ABORT_ON_OOM
110 if (newsh
== NULL
) sdsOomAbort();
112 if (newsh
== NULL
) return NULL
;
115 newsh
->free
= newlen
- len
;
119 /* Grow the sds to have the specified length. Bytes that were not part of
120 * the original length of the sds will be set to zero. */
121 sds
sdsgrowzero(sds s
, size_t len
) {
122 struct sdshdr
*sh
= (void*)(s
-(sizeof(struct sdshdr
)));
123 size_t totlen
, curlen
= sh
->len
;
125 if (len
<= curlen
) return s
;
126 s
= sdsMakeRoomFor(s
,len
-curlen
);
127 if (s
== NULL
) return NULL
;
129 /* Make sure added region doesn't contain garbage */
130 sh
= (void*)(s
-(sizeof(struct sdshdr
)));
131 memset(s
+curlen
,0,(len
-curlen
+1)); /* also set trailing \0 byte */
132 totlen
= sh
->len
+sh
->free
;
134 sh
->free
= totlen
-sh
->len
;
138 sds
sdscatlen(sds s
, void *t
, size_t len
) {
140 size_t curlen
= sdslen(s
);
142 s
= sdsMakeRoomFor(s
,len
);
143 if (s
== NULL
) return NULL
;
144 sh
= (void*) (s
-(sizeof(struct sdshdr
)));
145 memcpy(s
+curlen
, t
, len
);
146 sh
->len
= curlen
+len
;
147 sh
->free
= sh
->free
-len
;
148 s
[curlen
+len
] = '\0';
152 sds
sdscat(sds s
, char *t
) {
153 return sdscatlen(s
, t
, strlen(t
));
156 sds
sdscpylen(sds s
, char *t
, size_t len
) {
157 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
158 size_t totlen
= sh
->free
+sh
->len
;
161 s
= sdsMakeRoomFor(s
,len
-sh
->len
);
162 if (s
== NULL
) return NULL
;
163 sh
= (void*) (s
-(sizeof(struct sdshdr
)));
164 totlen
= sh
->free
+sh
->len
;
169 sh
->free
= totlen
-len
;
173 sds
sdscpy(sds s
, char *t
) {
174 return sdscpylen(s
, t
, strlen(t
));
177 sds
sdscatvprintf(sds s
, const char *fmt
, va_list ap
) {
183 buf
= zmalloc(buflen
);
184 #ifdef SDS_ABORT_ON_OOM
185 if (buf
== NULL
) sdsOomAbort();
187 if (buf
== NULL
) return NULL
;
189 buf
[buflen
-2] = '\0';
191 vsnprintf(buf
, buflen
, fmt
, cpy
);
192 if (buf
[buflen
-2] != '\0') {
204 sds
sdscatprintf(sds s
, const char *fmt
, ...) {
208 t
= sdscatvprintf(s
,fmt
,ap
);
213 sds
sdstrim(sds s
, const char *cset
) {
214 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
215 char *start
, *end
, *sp
, *ep
;
219 ep
= end
= s
+sdslen(s
)-1;
220 while(sp
<= end
&& strchr(cset
, *sp
)) sp
++;
221 while(ep
> start
&& strchr(cset
, *ep
)) ep
--;
222 len
= (sp
> ep
) ? 0 : ((ep
-sp
)+1);
223 if (sh
->buf
!= sp
) memmove(sh
->buf
, sp
, len
);
225 sh
->free
= sh
->free
+(sh
->len
-len
);
230 sds
sdsrange(sds s
, int start
, int end
) {
231 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
232 size_t newlen
, len
= sdslen(s
);
234 if (len
== 0) return s
;
237 if (start
< 0) start
= 0;
241 if (end
< 0) end
= 0;
243 newlen
= (start
> end
) ? 0 : (end
-start
)+1;
245 if (start
>= (signed)len
) {
247 } else if (end
>= (signed)len
) {
249 newlen
= (start
> end
) ? 0 : (end
-start
)+1;
254 if (start
&& newlen
) memmove(sh
->buf
, sh
->buf
+start
, newlen
);
256 sh
->free
= sh
->free
+(sh
->len
-newlen
);
261 void sdstolower(sds s
) {
262 int len
= sdslen(s
), j
;
264 for (j
= 0; j
< len
; j
++) s
[j
] = tolower(s
[j
]);
267 void sdstoupper(sds s
) {
268 int len
= sdslen(s
), j
;
270 for (j
= 0; j
< len
; j
++) s
[j
] = toupper(s
[j
]);
273 int sdscmp(sds s1
, sds s2
) {
274 size_t l1
, l2
, minlen
;
279 minlen
= (l1
< l2
) ? l1
: l2
;
280 cmp
= memcmp(s1
,s2
,minlen
);
281 if (cmp
== 0) return l1
-l2
;
285 /* Split 's' with separator in 'sep'. An array
286 * of sds strings is returned. *count will be set
287 * by reference to the number of tokens returned.
289 * On out of memory, zero length string, zero length
290 * separator, NULL is returned.
292 * Note that 'sep' is able to split a string using
293 * a multi-character separator. For example
294 * sdssplit("foo_-_bar","_-_"); will return two
295 * elements "foo" and "bar".
297 * This version of the function is binary-safe but
298 * requires length arguments. sdssplit() is just the
299 * same function but for zero-terminated strings.
301 sds
*sdssplitlen(char *s
, int len
, char *sep
, int seplen
, int *count
) {
302 int elements
= 0, slots
= 5, start
= 0, j
;
304 sds
*tokens
= zmalloc(sizeof(sds
)*slots
);
305 #ifdef SDS_ABORT_ON_OOM
306 if (tokens
== NULL
) sdsOomAbort();
308 if (seplen
< 1 || len
< 0 || tokens
== NULL
) return NULL
;
313 for (j
= 0; j
< (len
-(seplen
-1)); j
++) {
314 /* make sure there is room for the next element and the final one */
315 if (slots
< elements
+2) {
319 newtokens
= zrealloc(tokens
,sizeof(sds
)*slots
);
320 if (newtokens
== NULL
) {
321 #ifdef SDS_ABORT_ON_OOM
329 /* search the separator */
330 if ((seplen
== 1 && *(s
+j
) == sep
[0]) || (memcmp(s
+j
,sep
,seplen
) == 0)) {
331 tokens
[elements
] = sdsnewlen(s
+start
,j
-start
);
332 if (tokens
[elements
] == NULL
) {
333 #ifdef SDS_ABORT_ON_OOM
341 j
= j
+seplen
-1; /* skip the separator */
344 /* Add the final element. We are sure there is room in the tokens array. */
345 tokens
[elements
] = sdsnewlen(s
+start
,len
-start
);
346 if (tokens
[elements
] == NULL
) {
347 #ifdef SDS_ABORT_ON_OOM
357 #ifndef SDS_ABORT_ON_OOM
361 for (i
= 0; i
< elements
; i
++) sdsfree(tokens
[i
]);
368 void sdsfreesplitres(sds
*tokens
, int count
) {
371 sdsfree(tokens
[count
]);
375 sds
sdsfromlonglong(long long value
) {
377 unsigned long long v
;
379 v
= (value
< 0) ? -value
: value
;
380 p
= buf
+31; /* point to the last character */
385 if (value
< 0) *p
-- = '-';
387 return sdsnewlen(p
,32-(p
-buf
));
390 sds
sdscatrepr(sds s
, char *p
, size_t len
) {
391 s
= sdscatlen(s
,"\"",1);
396 s
= sdscatprintf(s
,"\\%c",*p
);
398 case '\n': s
= sdscatlen(s
,"\\n",1); break;
399 case '\r': s
= sdscatlen(s
,"\\r",1); break;
400 case '\t': s
= sdscatlen(s
,"\\t",1); break;
401 case '\a': s
= sdscatlen(s
,"\\a",1); break;
402 case '\b': s
= sdscatlen(s
,"\\b",1); break;
405 s
= sdscatprintf(s
,"%c",*p
);
407 s
= sdscatprintf(s
,"\\x%02x",(unsigned char)*p
);
412 return sdscatlen(s
,"\"",1);
415 /* Split a line into arguments, where every argument can be in the
416 * following programming-language REPL-alike form:
418 * foo bar "newline are supported\n" and "\xff\x00otherstuff"
420 * The number of arguments is stored into *argc, and an array
421 * of sds is returned. The caller should sdsfree() all the returned
422 * strings and finally zfree() the array itself.
424 * Note that sdscatrepr() is able to convert back a string into
425 * a quoted string in the same format sdssplitargs() is able to parse.
427 sds
*sdssplitargs(char *line
, int *argc
) {
429 char *current
= NULL
;
430 char **vector
= NULL
;
435 while(*p
&& isspace(*p
)) p
++;
438 int inq
=0; /* set to 1 if we are in "quotes" */
441 if (current
== NULL
) current
= sdsempty();
444 if (*p
== '\\' && *(p
+1)) {
449 case 'n': c
= '\n'; break;
450 case 'r': c
= '\r'; break;
451 case 't': c
= '\t'; break;
452 case 'b': c
= '\b'; break;
453 case 'a': c
= '\a'; break;
454 default: c
= *p
; break;
456 current
= sdscatlen(current
,&c
,1);
457 } else if (*p
== '"') {
458 /* closing quote must be followed by a space */
459 if (*(p
+1) && !isspace(*(p
+1))) goto err
;
462 /* unterminated quotes */
465 current
= sdscatlen(current
,p
,1);
480 current
= sdscatlen(current
,p
,1);
486 /* add the token to the vector */
487 vector
= zrealloc(vector
,((*argc
)+1)*sizeof(char*));
488 vector
[*argc
] = current
;
498 sdsfree(vector
[*argc
]);
500 if (current
) sdsfree(current
);
506 #include "testhelp.h"
510 sds x
= sdsnew("foo"), y
;
512 test_cond("Create a string and obtain the length",
513 sdslen(x
) == 3 && memcmp(x
,"foo\0",4) == 0)
516 x
= sdsnewlen("foo",2);
517 test_cond("Create a string with specified length",
518 sdslen(x
) == 2 && memcmp(x
,"fo\0",3) == 0)
521 test_cond("Strings concatenation",
522 sdslen(x
) == 5 && memcmp(x
,"fobar\0",6) == 0);
525 test_cond("sdscpy() against an originally longer string",
526 sdslen(x
) == 1 && memcmp(x
,"a\0",2) == 0)
528 x
= sdscpy(x
,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk");
529 test_cond("sdscpy() against an originally shorter string",
531 memcmp(x
,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0)
534 x
= sdscatprintf(sdsempty(),"%d",123);
535 test_cond("sdscatprintf() seems working in the base case",
536 sdslen(x
) == 3 && memcmp(x
,"123\0",4) ==0)
539 x
= sdstrim(sdsnew("xxciaoyyy"),"xy");
540 test_cond("sdstrim() correctly trims characters",
541 sdslen(x
) == 4 && memcmp(x
,"ciao\0",5) == 0)
543 y
= sdsrange(sdsdup(x
),1,1);
544 test_cond("sdsrange(...,1,1)",
545 sdslen(y
) == 1 && memcmp(y
,"i\0",2) == 0)
548 y
= sdsrange(sdsdup(x
),1,-1);
549 test_cond("sdsrange(...,1,-1)",
550 sdslen(y
) == 3 && memcmp(y
,"iao\0",4) == 0)
553 y
= sdsrange(sdsdup(x
),-2,-1);
554 test_cond("sdsrange(...,-2,-1)",
555 sdslen(y
) == 2 && memcmp(y
,"ao\0",3) == 0)
558 y
= sdsrange(sdsdup(x
),2,1);
559 test_cond("sdsrange(...,2,1)",
560 sdslen(y
) == 0 && memcmp(y
,"\0",1) == 0)
563 y
= sdsrange(sdsdup(x
),1,100);
564 test_cond("sdsrange(...,1,100)",
565 sdslen(y
) == 3 && memcmp(y
,"iao\0",4) == 0)
568 y
= sdsrange(sdsdup(x
),100,100);
569 test_cond("sdsrange(...,100,100)",
570 sdslen(y
) == 0 && memcmp(y
,"\0",1) == 0)
576 test_cond("sdscmp(foo,foa)", sdscmp(x
,y
) > 0)
582 test_cond("sdscmp(bar,bar)", sdscmp(x
,y
) == 0)
588 test_cond("sdscmp(bar,bar)", sdscmp(x
,y
) < 0)