]>
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 sds
sdscatlen(sds s
, void *t
, size_t len
) {
121 size_t curlen
= sdslen(s
);
123 s
= sdsMakeRoomFor(s
,len
);
124 if (s
== NULL
) return NULL
;
125 sh
= (void*) (s
-(sizeof(struct sdshdr
)));
126 memcpy(s
+curlen
, t
, len
);
127 sh
->len
= curlen
+len
;
128 sh
->free
= sh
->free
-len
;
129 s
[curlen
+len
] = '\0';
133 sds
sdscat(sds s
, char *t
) {
134 return sdscatlen(s
, t
, strlen(t
));
137 sds
sdscpylen(sds s
, char *t
, size_t len
) {
138 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
139 size_t totlen
= sh
->free
+sh
->len
;
142 s
= sdsMakeRoomFor(s
,len
-sh
->len
);
143 if (s
== NULL
) return NULL
;
144 sh
= (void*) (s
-(sizeof(struct sdshdr
)));
145 totlen
= sh
->free
+sh
->len
;
150 sh
->free
= totlen
-len
;
154 sds
sdscpy(sds s
, char *t
) {
155 return sdscpylen(s
, t
, strlen(t
));
158 sds
sdscatvprintf(sds s
, const char *fmt
, va_list ap
) {
164 buf
= zmalloc(buflen
);
165 #ifdef SDS_ABORT_ON_OOM
166 if (buf
== NULL
) sdsOomAbort();
168 if (buf
== NULL
) return NULL
;
170 buf
[buflen
-2] = '\0';
172 vsnprintf(buf
, buflen
, fmt
, cpy
);
173 if (buf
[buflen
-2] != '\0') {
185 sds
sdscatprintf(sds s
, const char *fmt
, ...) {
189 t
= sdscatvprintf(s
,fmt
,ap
);
194 sds
sdstrim(sds s
, const char *cset
) {
195 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
196 char *start
, *end
, *sp
, *ep
;
200 ep
= end
= s
+sdslen(s
)-1;
201 while(sp
<= end
&& strchr(cset
, *sp
)) sp
++;
202 while(ep
> start
&& strchr(cset
, *ep
)) ep
--;
203 len
= (sp
> ep
) ? 0 : ((ep
-sp
)+1);
204 if (sh
->buf
!= sp
) memmove(sh
->buf
, sp
, len
);
206 sh
->free
= sh
->free
+(sh
->len
-len
);
211 sds
sdsrange(sds s
, int start
, int end
) {
212 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
213 size_t newlen
, len
= sdslen(s
);
215 if (len
== 0) return s
;
218 if (start
< 0) start
= 0;
222 if (end
< 0) end
= 0;
224 newlen
= (start
> end
) ? 0 : (end
-start
)+1;
226 if (start
>= (signed)len
) {
228 } else if (end
>= (signed)len
) {
230 newlen
= (start
> end
) ? 0 : (end
-start
)+1;
235 if (start
&& newlen
) memmove(sh
->buf
, sh
->buf
+start
, newlen
);
237 sh
->free
= sh
->free
+(sh
->len
-newlen
);
242 void sdstolower(sds s
) {
243 int len
= sdslen(s
), j
;
245 for (j
= 0; j
< len
; j
++) s
[j
] = tolower(s
[j
]);
248 void sdstoupper(sds s
) {
249 int len
= sdslen(s
), j
;
251 for (j
= 0; j
< len
; j
++) s
[j
] = toupper(s
[j
]);
254 int sdscmp(sds s1
, sds s2
) {
255 size_t l1
, l2
, minlen
;
260 minlen
= (l1
< l2
) ? l1
: l2
;
261 cmp
= memcmp(s1
,s2
,minlen
);
262 if (cmp
== 0) return l1
-l2
;
266 /* Split 's' with separator in 'sep'. An array
267 * of sds strings is returned. *count will be set
268 * by reference to the number of tokens returned.
270 * On out of memory, zero length string, zero length
271 * separator, NULL is returned.
273 * Note that 'sep' is able to split a string using
274 * a multi-character separator. For example
275 * sdssplit("foo_-_bar","_-_"); will return two
276 * elements "foo" and "bar".
278 * This version of the function is binary-safe but
279 * requires length arguments. sdssplit() is just the
280 * same function but for zero-terminated strings.
282 sds
*sdssplitlen(char *s
, int len
, char *sep
, int seplen
, int *count
) {
283 int elements
= 0, slots
= 5, start
= 0, j
;
285 sds
*tokens
= zmalloc(sizeof(sds
)*slots
);
286 #ifdef SDS_ABORT_ON_OOM
287 if (tokens
== NULL
) sdsOomAbort();
289 if (seplen
< 1 || len
< 0 || tokens
== NULL
) return NULL
;
294 for (j
= 0; j
< (len
-(seplen
-1)); j
++) {
295 /* make sure there is room for the next element and the final one */
296 if (slots
< elements
+2) {
300 newtokens
= zrealloc(tokens
,sizeof(sds
)*slots
);
301 if (newtokens
== NULL
) {
302 #ifdef SDS_ABORT_ON_OOM
310 /* search the separator */
311 if ((seplen
== 1 && *(s
+j
) == sep
[0]) || (memcmp(s
+j
,sep
,seplen
) == 0)) {
312 tokens
[elements
] = sdsnewlen(s
+start
,j
-start
);
313 if (tokens
[elements
] == NULL
) {
314 #ifdef SDS_ABORT_ON_OOM
322 j
= j
+seplen
-1; /* skip the separator */
325 /* Add the final element. We are sure there is room in the tokens array. */
326 tokens
[elements
] = sdsnewlen(s
+start
,len
-start
);
327 if (tokens
[elements
] == NULL
) {
328 #ifdef SDS_ABORT_ON_OOM
338 #ifndef SDS_ABORT_ON_OOM
342 for (i
= 0; i
< elements
; i
++) sdsfree(tokens
[i
]);
349 void sdsfreesplitres(sds
*tokens
, int count
) {
352 sdsfree(tokens
[count
]);
356 sds
sdsfromlonglong(long long value
) {
358 unsigned long long v
;
360 v
= (value
< 0) ? -value
: value
;
361 p
= buf
+31; /* point to the last character */
366 if (value
< 0) *p
-- = '-';
368 return sdsnewlen(p
,32-(p
-buf
));
371 sds
sdscatrepr(sds s
, char *p
, size_t len
) {
372 s
= sdscatlen(s
,"\"",1);
377 s
= sdscatprintf(s
,"\\%c",*p
);
379 case '\n': s
= sdscatlen(s
,"\\n",1); break;
380 case '\r': s
= sdscatlen(s
,"\\r",1); break;
381 case '\t': s
= sdscatlen(s
,"\\t",1); break;
382 case '\a': s
= sdscatlen(s
,"\\a",1); break;
383 case '\b': s
= sdscatlen(s
,"\\b",1); break;
386 s
= sdscatprintf(s
,"%c",*p
);
388 s
= sdscatprintf(s
,"\\x%02x",(unsigned char)*p
);
393 return sdscatlen(s
,"\"",1);
396 /* Split a line into arguments, where every argument can be in the
397 * following programming-language REPL-alike form:
399 * foo bar "newline are supported\n" and "\xff\x00otherstuff"
401 * The number of arguments is stored into *argc, and an array
402 * of sds is returned. The caller should sdsfree() all the returned
403 * strings and finally zfree() the array itself.
405 * Note that sdscatrepr() is able to convert back a string into
406 * a quoted string in the same format sdssplitargs() is able to parse.
408 sds
*sdssplitargs(char *line
, int *argc
) {
410 char *current
= NULL
;
411 char **vector
= NULL
;
416 while(*p
&& isspace(*p
)) p
++;
419 int inq
=0; /* set to 1 if we are in "quotes" */
422 if (current
== NULL
) current
= sdsempty();
425 if (*p
== '\\' && *(p
+1)) {
430 case 'n': c
= '\n'; break;
431 case 'r': c
= '\r'; break;
432 case 't': c
= '\t'; break;
433 case 'b': c
= '\b'; break;
434 case 'a': c
= '\a'; break;
435 default: c
= *p
; break;
437 current
= sdscatlen(current
,&c
,1);
438 } else if (*p
== '"') {
439 /* closing quote must be followed by a space */
440 if (*(p
+1) && !isspace(*(p
+1))) goto err
;
443 /* unterminated quotes */
446 current
= sdscatlen(current
,p
,1);
461 current
= sdscatlen(current
,p
,1);
467 /* add the token to the vector */
468 vector
= zrealloc(vector
,((*argc
)+1)*sizeof(char*));
469 vector
[*argc
] = current
;
479 sdsfree(vector
[*argc
]);
481 if (current
) sdsfree(current
);
487 #include "testhelp.h"
491 sds x
= sdsnew("foo"), y
;
493 test_cond("Create a string and obtain the length",
494 sdslen(x
) == 3 && memcmp(x
,"foo\0",4) == 0)
497 x
= sdsnewlen("foo",2);
498 test_cond("Create a string with specified length",
499 sdslen(x
) == 2 && memcmp(x
,"fo\0",3) == 0)
502 test_cond("Strings concatenation",
503 sdslen(x
) == 5 && memcmp(x
,"fobar\0",6) == 0);
506 test_cond("sdscpy() against an originally longer string",
507 sdslen(x
) == 1 && memcmp(x
,"a\0",2) == 0)
509 x
= sdscpy(x
,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk");
510 test_cond("sdscpy() against an originally shorter string",
512 memcmp(x
,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0)
515 x
= sdscatprintf(sdsempty(),"%d",123);
516 test_cond("sdscatprintf() seems working in the base case",
517 sdslen(x
) == 3 && memcmp(x
,"123\0",4) ==0)
520 x
= sdstrim(sdsnew("xxciaoyyy"),"xy");
521 test_cond("sdstrim() correctly trims characters",
522 sdslen(x
) == 4 && memcmp(x
,"ciao\0",5) == 0)
524 y
= sdsrange(sdsdup(x
),1,1);
525 test_cond("sdsrange(...,1,1)",
526 sdslen(y
) == 1 && memcmp(y
,"i\0",2) == 0)
529 y
= sdsrange(sdsdup(x
),1,-1);
530 test_cond("sdsrange(...,1,-1)",
531 sdslen(y
) == 3 && memcmp(y
,"iao\0",4) == 0)
534 y
= sdsrange(sdsdup(x
),-2,-1);
535 test_cond("sdsrange(...,-2,-1)",
536 sdslen(y
) == 2 && memcmp(y
,"ao\0",3) == 0)
539 y
= sdsrange(sdsdup(x
),2,1);
540 test_cond("sdsrange(...,2,1)",
541 sdslen(y
) == 0 && memcmp(y
,"\0",1) == 0)
544 y
= sdsrange(sdsdup(x
),1,100);
545 test_cond("sdsrange(...,1,100)",
546 sdslen(y
) == 3 && memcmp(y
,"iao\0",4) == 0)
549 y
= sdsrange(sdsdup(x
),100,100);
550 test_cond("sdsrange(...,100,100)",
551 sdslen(y
) == 0 && memcmp(y
,"\0",1) == 0)
557 test_cond("sdscmp(foo,foa)", sdscmp(x
,y
) > 0)
563 test_cond("sdscmp(bar,bar)", sdscmp(x
,y
) == 0)
569 test_cond("sdscmp(bar,bar)", sdscmp(x
,y
) < 0)