]>
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
) {
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 /* Split a line into arguments, where every argument can be in the
420 * following programming-language REPL-alike form:
422 * foo bar "newline are supported\n" and "\xff\x00otherstuff"
424 * The number of arguments is stored into *argc, and an array
425 * of sds is returned. The caller should sdsfree() all the returned
426 * strings and finally zfree() the array itself.
428 * Note that sdscatrepr() is able to convert back a string into
429 * a quoted string in the same format sdssplitargs() is able to parse.
431 sds
*sdssplitargs(char *line
, int *argc
) {
433 char *current
= NULL
;
434 char **vector
= NULL
;
439 while(*p
&& isspace(*p
)) p
++;
442 int inq
=0; /* set to 1 if we are in "quotes" */
445 if (current
== NULL
) current
= sdsempty();
448 if (*p
== '\\' && *(p
+1)) {
453 case 'n': c
= '\n'; break;
454 case 'r': c
= '\r'; break;
455 case 't': c
= '\t'; break;
456 case 'b': c
= '\b'; break;
457 case 'a': c
= '\a'; break;
458 default: c
= *p
; break;
460 current
= sdscatlen(current
,&c
,1);
461 } else if (*p
== '"') {
462 /* closing quote must be followed by a space */
463 if (*(p
+1) && !isspace(*(p
+1))) goto err
;
466 /* unterminated quotes */
469 current
= sdscatlen(current
,p
,1);
484 current
= sdscatlen(current
,p
,1);
490 /* add the token to the vector */
491 vector
= zrealloc(vector
,((*argc
)+1)*sizeof(char*));
492 vector
[*argc
] = current
;
502 sdsfree(vector
[*argc
]);
504 if (current
) sdsfree(current
);
510 #include "testhelp.h"
514 sds x
= sdsnew("foo"), y
;
516 test_cond("Create a string and obtain the length",
517 sdslen(x
) == 3 && memcmp(x
,"foo\0",4) == 0)
520 x
= sdsnewlen("foo",2);
521 test_cond("Create a string with specified length",
522 sdslen(x
) == 2 && memcmp(x
,"fo\0",3) == 0)
525 test_cond("Strings concatenation",
526 sdslen(x
) == 5 && memcmp(x
,"fobar\0",6) == 0);
529 test_cond("sdscpy() against an originally longer string",
530 sdslen(x
) == 1 && memcmp(x
,"a\0",2) == 0)
532 x
= sdscpy(x
,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk");
533 test_cond("sdscpy() against an originally shorter string",
535 memcmp(x
,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0)
538 x
= sdscatprintf(sdsempty(),"%d",123);
539 test_cond("sdscatprintf() seems working in the base case",
540 sdslen(x
) == 3 && memcmp(x
,"123\0",4) ==0)
543 x
= sdstrim(sdsnew("xxciaoyyy"),"xy");
544 test_cond("sdstrim() correctly trims characters",
545 sdslen(x
) == 4 && memcmp(x
,"ciao\0",5) == 0)
547 y
= sdsrange(sdsdup(x
),1,1);
548 test_cond("sdsrange(...,1,1)",
549 sdslen(y
) == 1 && memcmp(y
,"i\0",2) == 0)
552 y
= sdsrange(sdsdup(x
),1,-1);
553 test_cond("sdsrange(...,1,-1)",
554 sdslen(y
) == 3 && memcmp(y
,"iao\0",4) == 0)
557 y
= sdsrange(sdsdup(x
),-2,-1);
558 test_cond("sdsrange(...,-2,-1)",
559 sdslen(y
) == 2 && memcmp(y
,"ao\0",3) == 0)
562 y
= sdsrange(sdsdup(x
),2,1);
563 test_cond("sdsrange(...,2,1)",
564 sdslen(y
) == 0 && memcmp(y
,"\0",1) == 0)
567 y
= sdsrange(sdsdup(x
),1,100);
568 test_cond("sdsrange(...,1,100)",
569 sdslen(y
) == 3 && memcmp(y
,"iao\0",4) == 0)
572 y
= sdsrange(sdsdup(x
),100,100);
573 test_cond("sdsrange(...,100,100)",
574 sdslen(y
) == 0 && memcmp(y
,"\0",1) == 0)
580 test_cond("sdscmp(foo,foa)", sdscmp(x
,y
) > 0)
586 test_cond("sdscmp(bar,bar)", sdscmp(x
,y
) == 0)
592 test_cond("sdscmp(bar,bar)", sdscmp(x
,y
) < 0)