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