]> git.saurik.com Git - redis.git/blob - src/sds.c
Add commands SETBIT/GETBIT
[redis.git] / src / sds.c
1 /* SDSLib, A C dynamic strings library
2 *
3 * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
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.
17 *
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.
29 */
30
31 #define SDS_ABORT_ON_OOM
32
33 #include "sds.h"
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include "zmalloc.h"
39
40 static void sdsOomAbort(void) {
41 fprintf(stderr,"SDS: Out Of Memory (SDS_ABORT_ON_OOM defined)\n");
42 abort();
43 }
44
45 sds sdsnewlen(const void *init, size_t initlen) {
46 struct sdshdr *sh;
47
48 sh = zmalloc(sizeof(struct sdshdr)+initlen+1);
49 #ifdef SDS_ABORT_ON_OOM
50 if (sh == NULL) sdsOomAbort();
51 #else
52 if (sh == NULL) return NULL;
53 #endif
54 sh->len = initlen;
55 sh->free = 0;
56 if (initlen) {
57 if (init) memcpy(sh->buf, init, initlen);
58 else memset(sh->buf,0,initlen);
59 }
60 sh->buf[initlen] = '\0';
61 return (char*)sh->buf;
62 }
63
64 sds sdsempty(void) {
65 return sdsnewlen("",0);
66 }
67
68 sds sdsnew(const char *init) {
69 size_t initlen = (init == NULL) ? 0 : strlen(init);
70 return sdsnewlen(init, initlen);
71 }
72
73 size_t sdslen(const sds s) {
74 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
75 return sh->len;
76 }
77
78 sds sdsdup(const sds s) {
79 return sdsnewlen(s, sdslen(s));
80 }
81
82 void sdsfree(sds s) {
83 if (s == NULL) return;
84 zfree(s-sizeof(struct sdshdr));
85 }
86
87 size_t sdsavail(sds s) {
88 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
89 return sh->free;
90 }
91
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);
96 sh->len = reallen;
97 }
98
99 static sds sdsMakeRoomFor(sds s, size_t addlen) {
100 struct sdshdr *sh, *newsh;
101 size_t free = sdsavail(s);
102 size_t len, newlen;
103
104 if (free >= addlen) return s;
105 len = sdslen(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();
111 #else
112 if (newsh == NULL) return NULL;
113 #endif
114
115 newsh->free = newlen - len;
116 return newsh->buf;
117 }
118
119 sds sdscatlen(sds s, void *t, size_t len) {
120 struct sdshdr *sh;
121 size_t curlen = sdslen(s);
122
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';
130 return s;
131 }
132
133 sds sdscat(sds s, char *t) {
134 return sdscatlen(s, t, strlen(t));
135 }
136
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;
140
141 if (totlen < 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;
146 }
147 memcpy(s, t, len);
148 s[len] = '\0';
149 sh->len = len;
150 sh->free = totlen-len;
151 return s;
152 }
153
154 sds sdscpy(sds s, char *t) {
155 return sdscpylen(s, t, strlen(t));
156 }
157
158 sds sdssetbit(sds s, size_t bit, int on) {
159 struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
160 int byte = bit >> 3;
161 int reqlen = byte+1;
162
163 if (reqlen > sh->len) {
164 size_t totlen;
165
166 s = sdsMakeRoomFor(s,reqlen-sh->len);
167 if (s == NULL) return NULL;
168 sh = (void*)(s-(sizeof(struct sdshdr)));
169
170 /* Make sure added region doesn't contain garbage */
171 totlen = sh->len+sh->free;
172 memset(s+sh->len,0,sh->free+1);
173 sh->len = reqlen;
174 sh->free = totlen-sh->len;
175 }
176
177 bit = 7 - (bit & 0x7);
178 on &= 0x1;
179 s[byte] |= on << bit;
180 s[byte] &= ~((!on) << bit);
181 return s;
182 }
183
184 sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
185 va_list cpy;
186 char *buf, *t;
187 size_t buflen = 16;
188
189 while(1) {
190 buf = zmalloc(buflen);
191 #ifdef SDS_ABORT_ON_OOM
192 if (buf == NULL) sdsOomAbort();
193 #else
194 if (buf == NULL) return NULL;
195 #endif
196 buf[buflen-2] = '\0';
197 va_copy(cpy,ap);
198 vsnprintf(buf, buflen, fmt, cpy);
199 if (buf[buflen-2] != '\0') {
200 zfree(buf);
201 buflen *= 2;
202 continue;
203 }
204 break;
205 }
206 t = sdscat(s, buf);
207 zfree(buf);
208 return t;
209 }
210
211 sds sdscatprintf(sds s, const char *fmt, ...) {
212 va_list ap;
213 char *t;
214 va_start(ap, fmt);
215 t = sdscatvprintf(s,fmt,ap);
216 va_end(ap);
217 return t;
218 }
219
220 sds sdstrim(sds s, const char *cset) {
221 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
222 char *start, *end, *sp, *ep;
223 size_t len;
224
225 sp = start = s;
226 ep = end = s+sdslen(s)-1;
227 while(sp <= end && strchr(cset, *sp)) sp++;
228 while(ep > start && strchr(cset, *ep)) ep--;
229 len = (sp > ep) ? 0 : ((ep-sp)+1);
230 if (sh->buf != sp) memmove(sh->buf, sp, len);
231 sh->buf[len] = '\0';
232 sh->free = sh->free+(sh->len-len);
233 sh->len = len;
234 return s;
235 }
236
237 sds sdsrange(sds s, int start, int end) {
238 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
239 size_t newlen, len = sdslen(s);
240
241 if (len == 0) return s;
242 if (start < 0) {
243 start = len+start;
244 if (start < 0) start = 0;
245 }
246 if (end < 0) {
247 end = len+end;
248 if (end < 0) end = 0;
249 }
250 newlen = (start > end) ? 0 : (end-start)+1;
251 if (newlen != 0) {
252 if (start >= (signed)len) {
253 newlen = 0;
254 } else if (end >= (signed)len) {
255 end = len-1;
256 newlen = (start > end) ? 0 : (end-start)+1;
257 }
258 } else {
259 start = 0;
260 }
261 if (start && newlen) memmove(sh->buf, sh->buf+start, newlen);
262 sh->buf[newlen] = 0;
263 sh->free = sh->free+(sh->len-newlen);
264 sh->len = newlen;
265 return s;
266 }
267
268 void sdstolower(sds s) {
269 int len = sdslen(s), j;
270
271 for (j = 0; j < len; j++) s[j] = tolower(s[j]);
272 }
273
274 void sdstoupper(sds s) {
275 int len = sdslen(s), j;
276
277 for (j = 0; j < len; j++) s[j] = toupper(s[j]);
278 }
279
280 int sdscmp(sds s1, sds s2) {
281 size_t l1, l2, minlen;
282 int cmp;
283
284 l1 = sdslen(s1);
285 l2 = sdslen(s2);
286 minlen = (l1 < l2) ? l1 : l2;
287 cmp = memcmp(s1,s2,minlen);
288 if (cmp == 0) return l1-l2;
289 return cmp;
290 }
291
292 /* Split 's' with separator in 'sep'. An array
293 * of sds strings is returned. *count will be set
294 * by reference to the number of tokens returned.
295 *
296 * On out of memory, zero length string, zero length
297 * separator, NULL is returned.
298 *
299 * Note that 'sep' is able to split a string using
300 * a multi-character separator. For example
301 * sdssplit("foo_-_bar","_-_"); will return two
302 * elements "foo" and "bar".
303 *
304 * This version of the function is binary-safe but
305 * requires length arguments. sdssplit() is just the
306 * same function but for zero-terminated strings.
307 */
308 sds *sdssplitlen(char *s, int len, char *sep, int seplen, int *count) {
309 int elements = 0, slots = 5, start = 0, j;
310
311 sds *tokens = zmalloc(sizeof(sds)*slots);
312 #ifdef SDS_ABORT_ON_OOM
313 if (tokens == NULL) sdsOomAbort();
314 #endif
315 if (seplen < 1 || len < 0 || tokens == NULL) return NULL;
316 if (len == 0) {
317 *count = 0;
318 return tokens;
319 }
320 for (j = 0; j < (len-(seplen-1)); j++) {
321 /* make sure there is room for the next element and the final one */
322 if (slots < elements+2) {
323 sds *newtokens;
324
325 slots *= 2;
326 newtokens = zrealloc(tokens,sizeof(sds)*slots);
327 if (newtokens == NULL) {
328 #ifdef SDS_ABORT_ON_OOM
329 sdsOomAbort();
330 #else
331 goto cleanup;
332 #endif
333 }
334 tokens = newtokens;
335 }
336 /* search the separator */
337 if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) {
338 tokens[elements] = sdsnewlen(s+start,j-start);
339 if (tokens[elements] == NULL) {
340 #ifdef SDS_ABORT_ON_OOM
341 sdsOomAbort();
342 #else
343 goto cleanup;
344 #endif
345 }
346 elements++;
347 start = j+seplen;
348 j = j+seplen-1; /* skip the separator */
349 }
350 }
351 /* Add the final element. We are sure there is room in the tokens array. */
352 tokens[elements] = sdsnewlen(s+start,len-start);
353 if (tokens[elements] == NULL) {
354 #ifdef SDS_ABORT_ON_OOM
355 sdsOomAbort();
356 #else
357 goto cleanup;
358 #endif
359 }
360 elements++;
361 *count = elements;
362 return tokens;
363
364 #ifndef SDS_ABORT_ON_OOM
365 cleanup:
366 {
367 int i;
368 for (i = 0; i < elements; i++) sdsfree(tokens[i]);
369 zfree(tokens);
370 return NULL;
371 }
372 #endif
373 }
374
375 void sdsfreesplitres(sds *tokens, int count) {
376 if (!tokens) return;
377 while(count--)
378 sdsfree(tokens[count]);
379 zfree(tokens);
380 }
381
382 sds sdsfromlonglong(long long value) {
383 char buf[32], *p;
384 unsigned long long v;
385
386 v = (value < 0) ? -value : value;
387 p = buf+31; /* point to the last character */
388 do {
389 *p-- = '0'+(v%10);
390 v /= 10;
391 } while(v);
392 if (value < 0) *p-- = '-';
393 p++;
394 return sdsnewlen(p,32-(p-buf));
395 }
396
397 sds sdscatrepr(sds s, char *p, size_t len) {
398 s = sdscatlen(s,"\"",1);
399 while(len--) {
400 switch(*p) {
401 case '\\':
402 case '"':
403 s = sdscatprintf(s,"\\%c",*p);
404 break;
405 case '\n': s = sdscatlen(s,"\\n",1); break;
406 case '\r': s = sdscatlen(s,"\\r",1); break;
407 case '\t': s = sdscatlen(s,"\\t",1); break;
408 case '\a': s = sdscatlen(s,"\\a",1); break;
409 case '\b': s = sdscatlen(s,"\\b",1); break;
410 default:
411 if (isprint(*p))
412 s = sdscatprintf(s,"%c",*p);
413 else
414 s = sdscatprintf(s,"\\x%02x",(unsigned char)*p);
415 break;
416 }
417 p++;
418 }
419 return sdscatlen(s,"\"",1);
420 }
421
422 /* Split a line into arguments, where every argument can be in the
423 * following programming-language REPL-alike form:
424 *
425 * foo bar "newline are supported\n" and "\xff\x00otherstuff"
426 *
427 * The number of arguments is stored into *argc, and an array
428 * of sds is returned. The caller should sdsfree() all the returned
429 * strings and finally zfree() the array itself.
430 *
431 * Note that sdscatrepr() is able to convert back a string into
432 * a quoted string in the same format sdssplitargs() is able to parse.
433 */
434 sds *sdssplitargs(char *line, int *argc) {
435 char *p = line;
436 char *current = NULL;
437 char **vector = NULL;
438
439 *argc = 0;
440 while(1) {
441 /* skip blanks */
442 while(*p && isspace(*p)) p++;
443 if (*p) {
444 /* get a token */
445 int inq=0; /* set to 1 if we are in "quotes" */
446 int done=0;
447
448 if (current == NULL) current = sdsempty();
449 while(!done) {
450 if (inq) {
451 if (*p == '\\' && *(p+1)) {
452 char c;
453
454 p++;
455 switch(*p) {
456 case 'n': c = '\n'; break;
457 case 'r': c = '\r'; break;
458 case 't': c = '\t'; break;
459 case 'b': c = '\b'; break;
460 case 'a': c = '\a'; break;
461 default: c = *p; break;
462 }
463 current = sdscatlen(current,&c,1);
464 } else if (*p == '"') {
465 /* closing quote must be followed by a space */
466 if (*(p+1) && !isspace(*(p+1))) goto err;
467 done=1;
468 } else if (!*p) {
469 /* unterminated quotes */
470 goto err;
471 } else {
472 current = sdscatlen(current,p,1);
473 }
474 } else {
475 switch(*p) {
476 case ' ':
477 case '\n':
478 case '\r':
479 case '\t':
480 case '\0':
481 done=1;
482 break;
483 case '"':
484 inq=1;
485 break;
486 default:
487 current = sdscatlen(current,p,1);
488 break;
489 }
490 }
491 if (*p) p++;
492 }
493 /* add the token to the vector */
494 vector = zrealloc(vector,((*argc)+1)*sizeof(char*));
495 vector[*argc] = current;
496 (*argc)++;
497 current = NULL;
498 } else {
499 return vector;
500 }
501 }
502
503 err:
504 while((*argc)--)
505 sdsfree(vector[*argc]);
506 zfree(vector);
507 if (current) sdsfree(current);
508 return NULL;
509 }
510
511 #ifdef SDS_TEST_MAIN
512 #include <stdio.h>
513 #include "testhelp.h"
514
515 int main(void) {
516 {
517 sds x = sdsnew("foo"), y;
518
519 test_cond("Create a string and obtain the length",
520 sdslen(x) == 3 && memcmp(x,"foo\0",4) == 0)
521
522 sdsfree(x);
523 x = sdsnewlen("foo",2);
524 test_cond("Create a string with specified length",
525 sdslen(x) == 2 && memcmp(x,"fo\0",3) == 0)
526
527 x = sdscat(x,"bar");
528 test_cond("Strings concatenation",
529 sdslen(x) == 5 && memcmp(x,"fobar\0",6) == 0);
530
531 x = sdscpy(x,"a");
532 test_cond("sdscpy() against an originally longer string",
533 sdslen(x) == 1 && memcmp(x,"a\0",2) == 0)
534
535 x = sdscpy(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk");
536 test_cond("sdscpy() against an originally shorter string",
537 sdslen(x) == 33 &&
538 memcmp(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0)
539
540 sdsfree(x);
541 x = sdscatprintf(sdsempty(),"%d",123);
542 test_cond("sdscatprintf() seems working in the base case",
543 sdslen(x) == 3 && memcmp(x,"123\0",4) ==0)
544
545 sdsfree(x);
546 x = sdstrim(sdsnew("xxciaoyyy"),"xy");
547 test_cond("sdstrim() correctly trims characters",
548 sdslen(x) == 4 && memcmp(x,"ciao\0",5) == 0)
549
550 y = sdsrange(sdsdup(x),1,1);
551 test_cond("sdsrange(...,1,1)",
552 sdslen(y) == 1 && memcmp(y,"i\0",2) == 0)
553
554 sdsfree(y);
555 y = sdsrange(sdsdup(x),1,-1);
556 test_cond("sdsrange(...,1,-1)",
557 sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)
558
559 sdsfree(y);
560 y = sdsrange(sdsdup(x),-2,-1);
561 test_cond("sdsrange(...,-2,-1)",
562 sdslen(y) == 2 && memcmp(y,"ao\0",3) == 0)
563
564 sdsfree(y);
565 y = sdsrange(sdsdup(x),2,1);
566 test_cond("sdsrange(...,2,1)",
567 sdslen(y) == 0 && memcmp(y,"\0",1) == 0)
568
569 sdsfree(y);
570 y = sdsrange(sdsdup(x),1,100);
571 test_cond("sdsrange(...,1,100)",
572 sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)
573
574 sdsfree(y);
575 y = sdsrange(sdsdup(x),100,100);
576 test_cond("sdsrange(...,100,100)",
577 sdslen(y) == 0 && memcmp(y,"\0",1) == 0)
578
579 sdsfree(y);
580 sdsfree(x);
581 x = sdsnew("foo");
582 y = sdsnew("foa");
583 test_cond("sdscmp(foo,foa)", sdscmp(x,y) > 0)
584
585 sdsfree(y);
586 sdsfree(x);
587 x = sdsnew("bar");
588 y = sdsnew("bar");
589 test_cond("sdscmp(bar,bar)", sdscmp(x,y) == 0)
590
591 sdsfree(y);
592 sdsfree(x);
593 x = sdsnew("aar");
594 y = sdsnew("bar");
595 test_cond("sdscmp(bar,bar)", sdscmp(x,y) < 0)
596 }
597 test_report()
598 }
599 #endif