]> git.saurik.com Git - redis.git/blame - src/sds.c
Merge pull request #209 from woowenjie/unstable
[redis.git] / src / sds.c
CommitLineData
ed9b544e 1/* SDSLib, A C dynamic strings library
2 *
12d090d2 3 * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
ed9b544e 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.
e360e3bb 29 *
30 * History:
31 *
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().
ed9b544e 35 */
36
56906eef 37#define SDS_ABORT_ON_OOM
38
ed9b544e 39#include <stdio.h>
40#include <stdlib.h>
ed9b544e 41#include <string.h>
42#include <ctype.h>
d0b2a9b2 43#include <assert.h>
ded614f8 44#include "sds.h"
ed9b544e 45#include "zmalloc.h"
46
47static void sdsOomAbort(void) {
48 fprintf(stderr,"SDS: Out Of Memory (SDS_ABORT_ON_OOM defined)\n");
49 abort();
50}
51
52sds sdsnewlen(const void *init, size_t initlen) {
53 struct sdshdr *sh;
54
626f6b2d 55 if (init) {
56 sh = zmalloc(sizeof(struct sdshdr)+initlen+1);
57 } else {
58 sh = zcalloc(sizeof(struct sdshdr)+initlen+1);
59 }
f1017b3f 60#ifdef SDS_ABORT_ON_OOM
61 if (sh == NULL) sdsOomAbort();
62#else
63 if (sh == NULL) return NULL;
64#endif
65 sh->len = initlen;
ed9b544e 66 sh->free = 0;
626f6b2d 67 if (initlen && init)
68 memcpy(sh->buf, init, initlen);
ed9b544e 69 sh->buf[initlen] = '\0';
70 return (char*)sh->buf;
71}
72
73sds sdsempty(void) {
74 return sdsnewlen("",0);
75}
76
77sds sdsnew(const char *init) {
78 size_t initlen = (init == NULL) ? 0 : strlen(init);
79 return sdsnewlen(init, initlen);
80}
81
ed9b544e 82sds sdsdup(const sds s) {
83 return sdsnewlen(s, sdslen(s));
84}
85
86void sdsfree(sds s) {
87 if (s == NULL) return;
f1017b3f 88 zfree(s-sizeof(struct sdshdr));
ed9b544e 89}
90
ed9b544e 91void sdsupdatelen(sds s) {
92 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
93 int reallen = strlen(s);
f1017b3f 94 sh->free += (sh->len-reallen);
95 sh->len = reallen;
ed9b544e 96}
97
f990782f
PN
98void sdsclear(sds s) {
99 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
100 sh->free += sh->len;
101 sh->len = 0;
102 sh->buf[0] = '\0';
103}
104
35267245 105/* Enlarge the free space at the end of the sds string so that the caller
106 * is sure that after calling this function can overwrite up to addlen
107 * bytes after the end of the string, plus one more byte for nul term.
108 *
109 * Note: this does not change the *size* of the sds string as returned
110 * by sdslen(), but only the free buffer space we have. */
d0b2a9b2 111sds sdsMakeRoomFor(sds s, size_t addlen) {
ed9b544e 112 struct sdshdr *sh, *newsh;
113 size_t free = sdsavail(s);
f1017b3f 114 size_t len, newlen;
ed9b544e 115
f1017b3f 116 if (free >= addlen) return s;
ed9b544e 117 len = sdslen(s);
118 sh = (void*) (s-(sizeof(struct sdshdr)));
f1017b3f 119 newlen = (len+addlen)*2;
120 newsh = zrealloc(sh, sizeof(struct sdshdr)+newlen+1);
ed9b544e 121#ifdef SDS_ABORT_ON_OOM
f1017b3f 122 if (newsh == NULL) sdsOomAbort();
ed9b544e 123#else
f1017b3f 124 if (newsh == NULL) return NULL;
ed9b544e 125#endif
f1017b3f 126
127 newsh->free = newlen - len;
ed9b544e 128 return newsh->buf;
129}
130
d0b2a9b2 131/* Increment the sds length and decrements the left free space at the
132 * end of the string accordingly to 'incr'. Also set the null term
133 * in the new end of the string.
134 *
135 * This function is used in order to fix the string length after the
136 * user calls sdsMakeRoomFor(), writes something after the end of
137 * the current string, and finally needs to set the new length.
138 *
139 * Note: it is possible to use a negative increment in order to
140 * right-trim the string.
141 *
142 * Using sdsIncrLen() and sdsMakeRoomFor() it is possible to mount the
143 * following schema to cat bytes coming from the kerenl to the end of an
144 * sds string new things without copying into an intermediate buffer:
145 *
146 * oldlen = sdslen(s);
147 * s = sdsMakeRoomFor(s, BUFFER_SIZE);
148 * nread = read(fd, s+oldlen, BUFFER_SIZE);
149 * ... check for nread <= 0 and handle it ...
150 * sdsIncrLen(s, nhread);
151 */
152void sdsIncrLen(sds s, int incr) {
153 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
154
155 assert(sh->free >= incr);
156 sh->len += incr;
157 sh->free -= incr;
158 assert(sh->free >= 0);
159 s[sh->len] = '\0';
160}
161
eae33c1c 162/* Grow the sds to have the specified length. Bytes that were not part of
cc209063
PN
163 * the original length of the sds will be set to zero. */
164sds sdsgrowzero(sds s, size_t len) {
eae33c1c
PN
165 struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
166 size_t totlen, curlen = sh->len;
167
168 if (len <= curlen) return s;
169 s = sdsMakeRoomFor(s,len-curlen);
170 if (s == NULL) return NULL;
171
172 /* Make sure added region doesn't contain garbage */
173 sh = (void*)(s-(sizeof(struct sdshdr)));
cc209063 174 memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */
eae33c1c
PN
175 totlen = sh->len+sh->free;
176 sh->len = len;
177 sh->free = totlen-sh->len;
178 return s;
179}
180
ed9b544e 181sds sdscatlen(sds s, void *t, size_t len) {
f1017b3f 182 struct sdshdr *sh;
ed9b544e 183 size_t curlen = sdslen(s);
184
185 s = sdsMakeRoomFor(s,len);
186 if (s == NULL) return NULL;
f1017b3f 187 sh = (void*) (s-(sizeof(struct sdshdr)));
ed9b544e 188 memcpy(s+curlen, t, len);
f1017b3f 189 sh->len = curlen+len;
190 sh->free = sh->free-len;
ed9b544e 191 s[curlen+len] = '\0';
192 return s;
193}
194
195sds sdscat(sds s, char *t) {
196 return sdscatlen(s, t, strlen(t));
197}
198
08a879af 199sds sdscatsds(sds s, sds t) {
200 return sdscatlen(s, t, sdslen(t));
201}
202
ed9b544e 203sds sdscpylen(sds s, char *t, size_t len) {
204 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
f1017b3f 205 size_t totlen = sh->free+sh->len;
ed9b544e 206
207 if (totlen < len) {
b2b5ae80 208 s = sdsMakeRoomFor(s,len-sh->len);
ed9b544e 209 if (s == NULL) return NULL;
f1017b3f 210 sh = (void*) (s-(sizeof(struct sdshdr)));
211 totlen = sh->free+sh->len;
ed9b544e 212 }
213 memcpy(s, t, len);
214 s[len] = '\0';
f1017b3f 215 sh->len = len;
216 sh->free = totlen-len;
ed9b544e 217 return s;
218}
219
220sds sdscpy(sds s, char *t) {
221 return sdscpylen(s, t, strlen(t));
222}
223
60361e5a
PN
224sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
225 va_list cpy;
ed9b544e 226 char *buf, *t;
4b00bebd 227 size_t buflen = 16;
ed9b544e 228
229 while(1) {
230 buf = zmalloc(buflen);
231#ifdef SDS_ABORT_ON_OOM
232 if (buf == NULL) sdsOomAbort();
233#else
234 if (buf == NULL) return NULL;
235#endif
236 buf[buflen-2] = '\0';
60361e5a
PN
237 va_copy(cpy,ap);
238 vsnprintf(buf, buflen, fmt, cpy);
ed9b544e 239 if (buf[buflen-2] != '\0') {
240 zfree(buf);
241 buflen *= 2;
242 continue;
243 }
244 break;
245 }
246 t = sdscat(s, buf);
247 zfree(buf);
248 return t;
249}
250
60361e5a
PN
251sds sdscatprintf(sds s, const char *fmt, ...) {
252 va_list ap;
253 char *t;
254 va_start(ap, fmt);
255 t = sdscatvprintf(s,fmt,ap);
256 va_end(ap);
257 return t;
258}
259
ed9b544e 260sds sdstrim(sds s, const char *cset) {
261 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
262 char *start, *end, *sp, *ep;
263 size_t len;
264
265 sp = start = s;
266 ep = end = s+sdslen(s)-1;
267 while(sp <= end && strchr(cset, *sp)) sp++;
268 while(ep > start && strchr(cset, *ep)) ep--;
269 len = (sp > ep) ? 0 : ((ep-sp)+1);
270 if (sh->buf != sp) memmove(sh->buf, sp, len);
271 sh->buf[len] = '\0';
f1017b3f 272 sh->free = sh->free+(sh->len-len);
273 sh->len = len;
ed9b544e 274 return s;
275}
276
e2641e09 277sds sdsrange(sds s, int start, int end) {
ed9b544e 278 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
279 size_t newlen, len = sdslen(s);
280
281 if (len == 0) return s;
282 if (start < 0) {
283 start = len+start;
284 if (start < 0) start = 0;
285 }
286 if (end < 0) {
287 end = len+end;
288 if (end < 0) end = 0;
289 }
290 newlen = (start > end) ? 0 : (end-start)+1;
291 if (newlen != 0) {
136cf53f 292 if (start >= (signed)len) {
293 newlen = 0;
294 } else if (end >= (signed)len) {
295 end = len-1;
296 newlen = (start > end) ? 0 : (end-start)+1;
297 }
ed9b544e 298 } else {
299 start = 0;
300 }
136cf53f 301 if (start && newlen) memmove(sh->buf, sh->buf+start, newlen);
ed9b544e 302 sh->buf[newlen] = 0;
f1017b3f 303 sh->free = sh->free+(sh->len-newlen);
304 sh->len = newlen;
ed9b544e 305 return s;
306}
307
308void sdstolower(sds s) {
309 int len = sdslen(s), j;
310
311 for (j = 0; j < len; j++) s[j] = tolower(s[j]);
312}
313
314void sdstoupper(sds s) {
315 int len = sdslen(s), j;
316
317 for (j = 0; j < len; j++) s[j] = toupper(s[j]);
318}
319
320int sdscmp(sds s1, sds s2) {
321 size_t l1, l2, minlen;
322 int cmp;
323
324 l1 = sdslen(s1);
325 l2 = sdslen(s2);
326 minlen = (l1 < l2) ? l1 : l2;
327 cmp = memcmp(s1,s2,minlen);
328 if (cmp == 0) return l1-l2;
329 return cmp;
330}
331
332/* Split 's' with separator in 'sep'. An array
333 * of sds strings is returned. *count will be set
334 * by reference to the number of tokens returned.
335 *
336 * On out of memory, zero length string, zero length
337 * separator, NULL is returned.
338 *
339 * Note that 'sep' is able to split a string using
340 * a multi-character separator. For example
341 * sdssplit("foo_-_bar","_-_"); will return two
342 * elements "foo" and "bar".
343 *
344 * This version of the function is binary-safe but
345 * requires length arguments. sdssplit() is just the
346 * same function but for zero-terminated strings.
347 */
348sds *sdssplitlen(char *s, int len, char *sep, int seplen, int *count) {
349 int elements = 0, slots = 5, start = 0, j;
c040cbd6 350 sds *tokens;
ed9b544e 351
c040cbd6
PN
352 if (seplen < 1 || len < 0) return NULL;
353
354 tokens = zmalloc(sizeof(sds)*slots);
ed9b544e 355#ifdef SDS_ABORT_ON_OOM
356 if (tokens == NULL) sdsOomAbort();
c040cbd6
PN
357#else
358 if (tokens == NULL) return NULL;
ed9b544e 359#endif
c040cbd6 360
ed10f40b 361 if (len == 0) {
362 *count = 0;
363 return tokens;
364 }
ed9b544e 365 for (j = 0; j < (len-(seplen-1)); j++) {
366 /* make sure there is room for the next element and the final one */
367 if (slots < elements+2) {
a4d1ba9a 368 sds *newtokens;
369
ed9b544e 370 slots *= 2;
a4d1ba9a 371 newtokens = zrealloc(tokens,sizeof(sds)*slots);
ed9b544e 372 if (newtokens == NULL) {
373#ifdef SDS_ABORT_ON_OOM
374 sdsOomAbort();
375#else
376 goto cleanup;
377#endif
378 }
379 tokens = newtokens;
380 }
381 /* search the separator */
382 if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) {
383 tokens[elements] = sdsnewlen(s+start,j-start);
384 if (tokens[elements] == NULL) {
385#ifdef SDS_ABORT_ON_OOM
386 sdsOomAbort();
387#else
388 goto cleanup;
389#endif
390 }
391 elements++;
392 start = j+seplen;
393 j = j+seplen-1; /* skip the separator */
394 }
395 }
396 /* Add the final element. We are sure there is room in the tokens array. */
397 tokens[elements] = sdsnewlen(s+start,len-start);
398 if (tokens[elements] == NULL) {
399#ifdef SDS_ABORT_ON_OOM
400 sdsOomAbort();
401#else
402 goto cleanup;
403#endif
404 }
405 elements++;
406 *count = elements;
407 return tokens;
408
409#ifndef SDS_ABORT_ON_OOM
410cleanup:
411 {
412 int i;
413 for (i = 0; i < elements; i++) sdsfree(tokens[i]);
414 zfree(tokens);
be86082b 415 *count = 0;
ed9b544e 416 return NULL;
417 }
418#endif
419}
a34e0a25 420
421void sdsfreesplitres(sds *tokens, int count) {
422 if (!tokens) return;
423 while(count--)
424 sdsfree(tokens[count]);
425 zfree(tokens);
426}
ee14da56 427
428sds sdsfromlonglong(long long value) {
429 char buf[32], *p;
430 unsigned long long v;
431
432 v = (value < 0) ? -value : value;
433 p = buf+31; /* point to the last character */
434 do {
435 *p-- = '0'+(v%10);
436 v /= 10;
437 } while(v);
438 if (value < 0) *p-- = '-';
439 p++;
440 return sdsnewlen(p,32-(p-buf));
441}
e2641e09 442
443sds sdscatrepr(sds s, char *p, size_t len) {
444 s = sdscatlen(s,"\"",1);
445 while(len--) {
446 switch(*p) {
447 case '\\':
448 case '"':
449 s = sdscatprintf(s,"\\%c",*p);
450 break;
612810af 451 case '\n': s = sdscatlen(s,"\\n",2); break;
452 case '\r': s = sdscatlen(s,"\\r",2); break;
453 case '\t': s = sdscatlen(s,"\\t",2); break;
454 case '\a': s = sdscatlen(s,"\\a",2); break;
455 case '\b': s = sdscatlen(s,"\\b",2); break;
e2641e09 456 default:
457 if (isprint(*p))
458 s = sdscatprintf(s,"%c",*p);
459 else
460 s = sdscatprintf(s,"\\x%02x",(unsigned char)*p);
461 break;
462 }
463 p++;
464 }
465 return sdscatlen(s,"\"",1);
466}
cbce5171 467
e360e3bb 468/* Helper function for sdssplitargs() that returns non zero if 'c'
469 * is a valid hex digit. */
470int is_hex_digit(char c) {
471 return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
472 (c >= 'A' && c <= 'F');
473}
474
475/* Helper function for sdssplitargs() that converts an hex digit into an
476 * integer from 0 to 15 */
477int hex_digit_to_int(char c) {
478 switch(c) {
479 case '0': return 0;
480 case '1': return 1;
481 case '2': return 2;
482 case '3': return 3;
483 case '4': return 4;
484 case '5': return 5;
485 case '6': return 6;
486 case '7': return 7;
487 case '8': return 8;
488 case '9': return 9;
489 case 'a': case 'A': return 10;
490 case 'b': case 'B': return 11;
491 case 'c': case 'C': return 12;
492 case 'd': case 'D': return 13;
493 case 'e': case 'E': return 14;
494 case 'f': case 'F': return 15;
495 default: return 0;
496 }
497}
498
cbce5171 499/* Split a line into arguments, where every argument can be in the
500 * following programming-language REPL-alike form:
501 *
502 * foo bar "newline are supported\n" and "\xff\x00otherstuff"
503 *
504 * The number of arguments is stored into *argc, and an array
505 * of sds is returned. The caller should sdsfree() all the returned
506 * strings and finally zfree() the array itself.
507 *
508 * Note that sdscatrepr() is able to convert back a string into
509 * a quoted string in the same format sdssplitargs() is able to parse.
510 */
511sds *sdssplitargs(char *line, int *argc) {
512 char *p = line;
513 char *current = NULL;
514 char **vector = NULL;
515
516 *argc = 0;
517 while(1) {
518 /* skip blanks */
519 while(*p && isspace(*p)) p++;
520 if (*p) {
521 /* get a token */
e1cf460c 522 int inq=0; /* set to 1 if we are in "quotes" */
523 int insq=0; /* set to 1 if we are in 'single quotes' */
4b93e5e2 524 int done=0;
cbce5171 525
526 if (current == NULL) current = sdsempty();
527 while(!done) {
528 if (inq) {
e360e3bb 529 if (*p == '\\' && *(p+1) == 'x' &&
530 is_hex_digit(*(p+2)) &&
531 is_hex_digit(*(p+3)))
532 {
533 unsigned char byte;
534
535 byte = (hex_digit_to_int(*(p+2))*16)+
536 hex_digit_to_int(*(p+3));
537 current = sdscatlen(current,(char*)&byte,1);
538 p += 3;
539 } else if (*p == '\\' && *(p+1)) {
cbce5171 540 char c;
541
542 p++;
543 switch(*p) {
544 case 'n': c = '\n'; break;
545 case 'r': c = '\r'; break;
546 case 't': c = '\t'; break;
547 case 'b': c = '\b'; break;
548 case 'a': c = '\a'; break;
549 default: c = *p; break;
550 }
551 current = sdscatlen(current,&c,1);
552 } else if (*p == '"') {
e1cf460c 553 /* closing quote must be followed by a space or
554 * nothing at all. */
555 if (*(p+1) && !isspace(*(p+1))) goto err;
556 done=1;
557 } else if (!*p) {
558 /* unterminated quotes */
559 goto err;
560 } else {
561 current = sdscatlen(current,p,1);
562 }
563 } else if (insq) {
564 if (*p == '\\' && *(p+1) == '\'') {
565 p++;
566 current = sdscatlen(current,"'",1);
567 } else if (*p == '\'') {
568 /* closing quote must be followed by a space or
569 * nothing at all. */
4b93e5e2
PN
570 if (*(p+1) && !isspace(*(p+1))) goto err;
571 done=1;
572 } else if (!*p) {
573 /* unterminated quotes */
574 goto err;
cbce5171 575 } else {
576 current = sdscatlen(current,p,1);
577 }
578 } else {
579 switch(*p) {
580 case ' ':
581 case '\n':
582 case '\r':
583 case '\t':
584 case '\0':
585 done=1;
586 break;
587 case '"':
588 inq=1;
589 break;
e1cf460c 590 case '\'':
591 insq=1;
592 break;
cbce5171 593 default:
594 current = sdscatlen(current,p,1);
595 break;
596 }
597 }
598 if (*p) p++;
599 }
600 /* add the token to the vector */
601 vector = zrealloc(vector,((*argc)+1)*sizeof(char*));
602 vector[*argc] = current;
603 (*argc)++;
604 current = NULL;
605 } else {
606 return vector;
607 }
608 }
4b93e5e2
PN
609
610err:
2929ca97 611 while((*argc)--)
4b93e5e2
PN
612 sdsfree(vector[*argc]);
613 zfree(vector);
614 if (current) sdsfree(current);
615 return NULL;
cbce5171 616}
136cf53f 617
726a39c1 618void sdssplitargs_free(sds *argv, int argc) {
619 int j;
620
621 for (j = 0 ;j < argc; j++) sdsfree(argv[j]);
622 zfree(argv);
623}
624
3bb818df 625/* Modify the string substituting all the occurrences of the set of
626 * characters specifed in the 'from' string to the corresponding character
627 * in the 'to' array.
628 *
629 * For instance: sdsmapchars(mystring, "ho", "01", 2)
630 * will have the effect of turning the string "hello" into "0ell1".
631 *
632 * The function returns the sds string pointer, that is always the same
633 * as the input pointer since no resize is needed. */
634sds sdsmapchars(sds s, char *from, char *to, size_t setlen) {
635 size_t j, i, l = sdslen(s);
636
637 for (j = 0; j < l; j++) {
638 for (i = 0; i < setlen; i++) {
639 if (s[j] == from[i]) {
640 s[j] = to[i];
641 break;
642 }
643 }
644 }
645 return s;
646}
647
136cf53f 648#ifdef SDS_TEST_MAIN
649#include <stdio.h>
650#include "testhelp.h"
651
652int main(void) {
653 {
d0b2a9b2 654 struct sdshdr *sh;
963238f7 655 sds x = sdsnew("foo"), y;
656
657 test_cond("Create a string and obtain the length",
658 sdslen(x) == 3 && memcmp(x,"foo\0",4) == 0)
659
660 sdsfree(x);
661 x = sdsnewlen("foo",2);
662 test_cond("Create a string with specified length",
663 sdslen(x) == 2 && memcmp(x,"fo\0",3) == 0)
664
665 x = sdscat(x,"bar");
666 test_cond("Strings concatenation",
667 sdslen(x) == 5 && memcmp(x,"fobar\0",6) == 0);
668
669 x = sdscpy(x,"a");
670 test_cond("sdscpy() against an originally longer string",
671 sdslen(x) == 1 && memcmp(x,"a\0",2) == 0)
672
673 x = sdscpy(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk");
674 test_cond("sdscpy() against an originally shorter string",
675 sdslen(x) == 33 &&
676 memcmp(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0)
677
678 sdsfree(x);
679 x = sdscatprintf(sdsempty(),"%d",123);
680 test_cond("sdscatprintf() seems working in the base case",
681 sdslen(x) == 3 && memcmp(x,"123\0",4) ==0)
682
683 sdsfree(x);
684 x = sdstrim(sdsnew("xxciaoyyy"),"xy");
685 test_cond("sdstrim() correctly trims characters",
686 sdslen(x) == 4 && memcmp(x,"ciao\0",5) == 0)
687
688 y = sdsrange(sdsdup(x),1,1);
689 test_cond("sdsrange(...,1,1)",
690 sdslen(y) == 1 && memcmp(y,"i\0",2) == 0)
691
692 sdsfree(y);
693 y = sdsrange(sdsdup(x),1,-1);
694 test_cond("sdsrange(...,1,-1)",
695 sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)
696
697 sdsfree(y);
698 y = sdsrange(sdsdup(x),-2,-1);
699 test_cond("sdsrange(...,-2,-1)",
700 sdslen(y) == 2 && memcmp(y,"ao\0",3) == 0)
701
702 sdsfree(y);
703 y = sdsrange(sdsdup(x),2,1);
704 test_cond("sdsrange(...,2,1)",
705 sdslen(y) == 0 && memcmp(y,"\0",1) == 0)
706
707 sdsfree(y);
708 y = sdsrange(sdsdup(x),1,100);
709 test_cond("sdsrange(...,1,100)",
710 sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)
711
712 sdsfree(y);
713 y = sdsrange(sdsdup(x),100,100);
714 test_cond("sdsrange(...,100,100)",
715 sdslen(y) == 0 && memcmp(y,"\0",1) == 0)
716
717 sdsfree(y);
718 sdsfree(x);
719 x = sdsnew("foo");
720 y = sdsnew("foa");
721 test_cond("sdscmp(foo,foa)", sdscmp(x,y) > 0)
722
723 sdsfree(y);
724 sdsfree(x);
725 x = sdsnew("bar");
726 y = sdsnew("bar");
727 test_cond("sdscmp(bar,bar)", sdscmp(x,y) == 0)
728
729 sdsfree(y);
730 sdsfree(x);
731 x = sdsnew("aar");
732 y = sdsnew("bar");
733 test_cond("sdscmp(bar,bar)", sdscmp(x,y) < 0)
d0b2a9b2 734
735 {
736 int oldfree;
737
738 sdsfree(x);
739 x = sdsnew("0");
740 sh = (void*) (x-(sizeof(struct sdshdr)));
741 test_cond("sdsnew() free/len buffers", sh->len == 1 && sh->free == 0);
742 x = sdsMakeRoomFor(x,1);
743 sh = (void*) (x-(sizeof(struct sdshdr)));
744 test_cond("sdsMakeRoomFor()", sh->len == 1 && sh->free > 0);
745 oldfree = sh->free;
746 x[1] = '1';
747 sdsIncrLen(x,1);
748 test_cond("sdsIncrLen() -- content", x[0] == '0' && x[1] == '1');
749 test_cond("sdsIncrLen() -- len", sh->len == 2);
750 test_cond("sdsIncrLen() -- free", sh->free == oldfree-1);
751 }
136cf53f 752 }
753 test_report()
d0b2a9b2 754 return 0;
136cf53f 755}
756#endif