1 /* SDSLib, A C dynamic strings library 2 * 3 * Copyright (c) 2006-2012, 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 #include <stdio.h> 32 #include <stdlib.h> 33 #include <string.h> 34 #include <ctype.h> 35 #include <assert.h> 36 #include "sds.h" 37 #include "zmalloc.h" 38 39 /* Create a new sds string with the content specified by the 'init' pointer 40 * and 'initlen'. 41 * If NULL is used for 'init' the string is initialized with zero bytes. 42 * 43 * The string is always null-termined (all the sds strings are, always) so 44 * even if you create an sds string with: 45 * 46 * mystring = sdsnewlen("abc",3"); 47 * 48 * You can print the string with printf() as there is an implicit \0 at the 49 * end of the string. However the string is binary safe and can contain 50 * \0 characters in the middle, as the length is stored in the sds header. */ 51 sds sdsnewlen(const void *init, size_t initlen) { 52 struct sdshdr *sh; 53 54 if (init) { 55 sh = zmalloc(sizeof(struct sdshdr)+initlen+1); 56 } else { 57 sh = zcalloc(sizeof(struct sdshdr)+initlen+1); 58 } 59 if (sh == NULL) return NULL; 60 sh->len = initlen; 61 sh->free = 0; 62 if (initlen && init) 63 memcpy(sh->buf, init, initlen); 64 sh->buf[initlen] = '\0'; 65 return (char*)sh->buf; 66 } 67 68 /* Create an empty (zero length) sds string. Even in this case the string 69 * always has an implicit null term. */ 70 sds sdsempty(void) { 71 return sdsnewlen("",0); 72 } 73 74 /* Create a new sds string starting from a null termined C string. */ 75 sds sdsnew(const char *init) { 76 size_t initlen = (init == NULL) ? 0 : strlen(init); 77 return sdsnewlen(init, initlen); 78 } 79 80 /* Duplicate an sds string. */ 81 sds sdsdup(const sds s) { 82 return sdsnewlen(s, sdslen(s)); 83 } 84 85 /* Free an sds string. No operation is performed if 's' is NULL. */ 86 void sdsfree(sds s) { 87 if (s == NULL) return; 88 zfree(s-sizeof(struct sdshdr)); 89 } 90 91 /* Set the sds string length to the length as obtained with strlen(), so 92 * considering as content only up to the first null term character. 93 * 94 * This function is useful when the sds string is hacked manually in some 95 * way, like in the following example: 96 * 97 * s = sdsnew("foobar"); 98 * s[2] = '\0'; 99 * sdsupdatelen(s); 100 * printf("%d\n", sdslen(s)); 101 * 102 * The output will be "2", but if we comment out the call to sdsupdatelen() 103 * the output will be "6" as the string was modified but the logical length 104 * remains 6 bytes. */ 105 void sdsupdatelen(sds s) { 106 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); 107 int reallen = strlen(s); 108 sh->free += (sh->len-reallen); 109 sh->len = reallen; 110 } 111 112 /* Modify an sds string on-place to make it empty (zero length). 113 * However all the existing buffer is not discarded but set as free space 114 * so that next append operations will not require allocations up to the 115 * number of bytes previously available. */ 116 void sdsclear(sds s) { 117 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); 118 sh->free += sh->len; 119 sh->len = 0; 120 sh->buf[0] = '\0'; 121 } 122 123 /* Enlarge the free space at the end of the sds string so that the caller 124 * is sure that after calling this function can overwrite up to addlen 125 * bytes after the end of the string, plus one more byte for nul term. 126 * 127 * Note: this does not change the *length* of the sds string as returned 128 * by sdslen(), but only the free buffer space we have. */ 129 sds sdsMakeRoomFor(sds s, size_t addlen) { 130 struct sdshdr *sh, *newsh; 131 size_t free = sdsavail(s); 132 size_t len, newlen; 133 134 if (free >= addlen) return s; 135 len = sdslen(s); 136 sh = (void*) (s-(sizeof(struct sdshdr))); 137 newlen = (len+addlen); 138 if (newlen < SDS_MAX_PREALLOC) 139 newlen *= 2; 140 else 141 newlen += SDS_MAX_PREALLOC; 142 newsh = zrealloc(sh, sizeof(struct sdshdr)+newlen+1); 143 if (newsh == NULL) return NULL; 144 145 newsh->free = newlen - len; 146 return newsh->buf; 147 } 148 149 /* Reallocate the sds string so that it has no free space at the end. The 150 * contained string remains not altered, but next concatenation operations 151 * will require a reallocation. 152 * 153 * After the call, the passed sds string is no longer valid and all the 154 * references must be substituted with the new pointer returned by the call. */ 155 sds sdsRemoveFreeSpace(sds s) { 156 struct sdshdr *sh; 157 158 sh = (void*) (s-(sizeof(struct sdshdr))); 159 sh = zrealloc(sh, sizeof(struct sdshdr)+sh->len+1); 160 sh->free = 0; 161 return sh->buf; 162 } 163 164 /* Return the total size of the allocation of the specifed sds string, 165 * including: 166 * 1) The sds header before the pointer. 167 * 2) The string. 168 * 3) The free buffer at the end if any. 169 * 4) The implicit null term. 170 */ 171 size_t sdsAllocSize(sds s) { 172 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); 173 174 return sizeof(*sh)+sh->len+sh->free+1; 175 } 176 177 /* Increment the sds length and decrements the left free space at the 178 * end of the string according to 'incr'. Also set the null term 179 * in the new end of the string. 180 * 181 * This function is used in order to fix the string length after the 182 * user calls sdsMakeRoomFor(), writes something after the end of 183 * the current string, and finally needs to set the new length. 184 * 185 * Note: it is possible to use a negative increment in order to 186 * right-trim the string. 187 * 188 * Usage example: 189 * 190 * Using sdsIncrLen() and sdsMakeRoomFor() it is possible to mount the 191 * following schema, to cat bytes coming from the kernel to the end of an 192 * sds string without copying into an intermediate buffer: 193 * 194 * oldlen = sdslen(s); 195 * s = sdsMakeRoomFor(s, BUFFER_SIZE); 196 * nread = read(fd, s+oldlen, BUFFER_SIZE); 197 * ... check for nread <= 0 and handle it ... 198 * sdsIncrLen(s, nread); 199 */ 200 void sdsIncrLen(sds s, int incr) { 201 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); 202 203 assert(sh->free >= incr); 204 sh->len += incr; 205 sh->free -= incr; 206 assert(sh->free >= 0); 207 s[sh->len] = '\0'; 208 } 209 210 /* Grow the sds to have the specified length. Bytes that were not part of 211 * the original length of the sds will be set to zero. 212 * 213 * if the specified length is smaller than the current length, no operation 214 * is performed. */ 215 sds sdsgrowzero(sds s, size_t len) { 216 struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr))); 217 size_t totlen, curlen = sh->len; 218 219 if (len <= curlen) return s; 220 s = sdsMakeRoomFor(s,len-curlen); 221 if (s == NULL) return NULL; 222 223 /* Make sure added region doesn't contain garbage */ 224 sh = (void*)(s-(sizeof(struct sdshdr))); 225 memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */ 226 totlen = sh->len+sh->free; 227 sh->len = len; 228 sh->free = totlen-sh->len; 229 return s; 230 } 231 232 /* Append the specified binary-safe string pointed by 't' of 'len' bytes to the 233 * end of the specified sds string 's'. 234 * 235 * After the call, the passed sds string is no longer valid and all the 236 * references must be substituted with the new pointer returned by the call. */ 237 sds sdscatlen(sds s, const void *t, size_t len) { 238 struct sdshdr *sh; 239 size_t curlen = sdslen(s); 240 241 s = sdsMakeRoomFor(s,len); 242 if (s == NULL) return NULL; 243 sh = (void*) (s-(sizeof(struct sdshdr))); 244 memcpy(s+curlen, t, len); 245 sh->len = curlen+len; 246 sh->free = sh->free-len; 247 s[curlen+len] = '\0'; 248 return s; 249 } 250 251 /* Append the specified null termianted C string to the sds string 's'. 252 * 253 * After the call, the passed sds string is no longer valid and all the 254 * references must be substituted with the new pointer returned by the call. */ 255 sds sdscat(sds s, const char *t) { 256 return sdscatlen(s, t, strlen(t)); 257 } 258 259 /* Append the specified sds 't' to the existing sds 's'. 260 * 261 * After the call, the modified sds string is no longer valid and all the 262 * references must be substituted with the new pointer returned by the call. */ 263 sds sdscatsds(sds s, const sds t) { 264 return sdscatlen(s, t, sdslen(t)); 265 } 266 267 /* Destructively modify the sds string 's' to hold the specified binary 268 * safe string pointed by 't' of length 'len' bytes. */ 269 sds sdscpylen(sds s, const char *t, size_t len) { 270 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); 271 size_t totlen = sh->free+sh->len; 272 273 if (totlen < len) { 274 s = sdsMakeRoomFor(s,len-sh->len); 275 if (s == NULL) return NULL; 276 sh = (void*) (s-(sizeof(struct sdshdr))); 277 totlen = sh->free+sh->len; 278 } 279 memcpy(s, t, len); 280 s[len] = '\0'; 281 sh->len = len; 282 sh->free = totlen-len; 283 return s; 284 } 285 286 /* Like sdscpylen() but 't' must be a null-termined string so that the length 287 * of the string is obtained with strlen(). */ 288 sds sdscpy(sds s, const char *t) { 289 return sdscpylen(s, t, strlen(t)); 290 } 291 292 /* Like sdscatpritf() but gets va_list instead of being variadic. */ 293 sds sdscatvprintf(sds s, const char *fmt, va_list ap) { 294 va_list cpy; 295 char staticbuf[1024], *buf = staticbuf, *t; 296 size_t buflen = strlen(fmt)*2; 297 298 /* We try to start using a static buffer for speed. 299 * If not possible we revert to heap allocation. */ 300 if (buflen > sizeof(staticbuf)) { 301 buf = zmalloc(buflen); 302 if (buf == NULL) return NULL; 303 } else { 304 buflen = sizeof(staticbuf); 305 } 306 307 /* Try with buffers two times bigger every time we fail to 308 * fit the string in the current buffer size. */ 309 while(1) { 310 buf[buflen-2] = '\0'; 311 va_copy(cpy,ap); 312 vsnprintf(buf, buflen, fmt, cpy); 313 if (buf[buflen-2] != '\0') { 314 if (buf != staticbuf) zfree(buf); 315 buflen *= 2; 316 buf = zmalloc(buflen); 317 if (buf == NULL) return NULL; 318 continue; 319 } 320 break; 321 } 322 323 /* Finally concat the obtained string to the SDS string and return it. */ 324 t = sdscat(s, buf); 325 if (buf != staticbuf) zfree(buf); 326 return t; 327 } 328 329 /* Append to the sds string 's' a string obtained using printf-alike format 330 * specifier. 331 * 332 * After the call, the modified sds string is no longer valid and all the 333 * references must be substituted with the new pointer returned by the call. 334 * 335 * Example: 336 * 337 * s = sdsempty("Sum is: "); 338 * s = sdscatprintf(s,"%d+%d = %d",a,b,a+b). 339 * 340 * Often you need to create a string from scratch with the printf-alike 341 * format. When this is the need, just use sdsempty() as the target string: 342 * 343 * s = sdscatprintf(sdsempty(), "... your format ...", args); 344 */ 345 sds sdscatprintf(sds s, const char *fmt, ...) { 346 va_list ap; 347 char *t; 348 va_start(ap, fmt); 349 t = sdscatvprintf(s,fmt,ap); 350 va_end(ap); 351 return t; 352 } 353 354 /* Remove the part of the string from left and from right composed just of 355 * contiguous characters found in 'cset', that is a null terminted C string. 356 * 357 * After the call, the modified sds string is no longer valid and all the 358 * references must be substituted with the new pointer returned by the call. 359 * 360 * Example: 361 * 362 * s = sdsnew("AA...AA.a.aa.aHelloWorld :::"); 363 * s = sdstrim(s,"A. :"); 364 * printf("%s\n", s); 365 * 366 * Output will be just "Hello World". 367 */ 368 sds sdstrim(sds s, const char *cset) { 369 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); 370 char *start, *end, *sp, *ep; 371 size_t len; 372 373 sp = start = s; 374 ep = end = s+sdslen(s)-1; 375 while(sp <= end && strchr(cset, *sp)) sp++; 376 while(ep > start && strchr(cset, *ep)) ep--; 377 len = (sp > ep) ? 0 : ((ep-sp)+1); 378 if (sh->buf != sp) memmove(sh->buf, sp, len); 379 sh->buf[len] = '\0'; 380 sh->free = sh->free+(sh->len-len); 381 sh->len = len; 382 return s; 383 } 384 385 /* Turn the string into a smaller (or equal) string containing only the 386 * substring specified by the 'start' and 'end' indexes. 387 * 388 * start and end can be negative, where -1 means the last character of the 389 * string, -2 the penultimate character, and so forth. 390 * 391 * The interval is inclusive, so the start and end characters will be part 392 * of the resulting string. 393 * 394 * The string is modified in-place. 395 * 396 * Example: 397 * 398 * s = sdsnew("Hello World"); 399 * sdsrange(s,1,-1); => "ello World" 400 */ 401 void sdsrange(sds s, int start, int end) { 402 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); 403 size_t newlen, len = sdslen(s); 404 405 if (len == 0) return; 406 if (start < 0) { 407 start = len+start; 408 if (start < 0) start = 0; 409 } 410 if (end < 0) { 411 end = len+end; 412 if (end < 0) end = 0; 413 } 414 newlen = (start > end) ? 0 : (end-start)+1; 415 if (newlen != 0) { 416 if (start >= (signed)len) { 417 newlen = 0; 418 } else if (end >= (signed)len) { 419 end = len-1; 420 newlen = (start > end) ? 0 : (end-start)+1; 421 } 422 } else { 423 start = 0; 424 } 425 if (start && newlen) memmove(sh->buf, sh->buf+start, newlen); 426 sh->buf[newlen] = 0; 427 sh->free = sh->free+(sh->len-newlen); 428 sh->len = newlen; 429 } 430 431 /* Apply tolower() to every character of the sds string 's'. */ 432 void sdstolower(sds s) { 433 int len = sdslen(s), j; 434 435 for (j = 0; j < len; j++) s[j] = tolower(s[j]); 436 } 437 438 /* Apply toupper() to every character of the sds string 's'. */ 439 void sdstoupper(sds s) { 440 int len = sdslen(s), j; 441 442 for (j = 0; j < len; j++) s[j] = toupper(s[j]); 443 } 444 445 /* Compare two sds strings s1 and s2 with memcmp(). 446 * 447 * Return value: 448 * 449 * 1 if s1 > s2. 450 * -1 if s1 < s2. 451 * 0 if s1 and s2 are exactly the same binary string. 452 * 453 * If two strings share exactly the same prefix, but one of the two has 454 * additional characters, the longer string is considered to be greater than 455 * the smaller one. */ 456 int sdscmp(const sds s1, const sds s2) { 457 size_t l1, l2, minlen; 458 int cmp; 459 460 l1 = sdslen(s1); 461 l2 = sdslen(s2); 462 minlen = (l1 < l2) ? l1 : l2; 463 cmp = memcmp(s1,s2,minlen); 464 if (cmp == 0) return l1-l2; 465 return cmp; 466 } 467 468 /* Split 's' with separator in 'sep'. An array 469 * of sds strings is returned. *count will be set 470 * by reference to the number of tokens returned. 471 * 472 * On out of memory, zero length string, zero length 473 * separator, NULL is returned. 474 * 475 * Note that 'sep' is able to split a string using 476 * a multi-character separator. For example 477 * sdssplit("foo_-_bar","_-_"); will return two 478 * elements "foo" and "bar". 479 * 480 * This version of the function is binary-safe but 481 * requires length arguments. sdssplit() is just the 482 * same function but for zero-terminated strings. 483 */ 484 sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count) { 485 int elements = 0, slots = 5, start = 0, j; 486 sds *tokens; 487 488 if (seplen < 1 || len < 0) return NULL; 489 490 tokens = zmalloc(sizeof(sds)*slots); 491 if (tokens == NULL) return NULL; 492 493 if (len == 0) { 494 *count = 0; 495 return tokens; 496 } 497 for (j = 0; j < (len-(seplen-1)); j++) { 498 /* make sure there is room for the next element and the final one */ 499 if (slots < elements+2) { 500 sds *newtokens; 501 502 slots *= 2; 503 newtokens = zrealloc(tokens,sizeof(sds)*slots); 504 if (newtokens == NULL) goto cleanup; 505 tokens = newtokens; 506 } 507 /* search the separator */ 508 if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) { 509 tokens[elements] = sdsnewlen(s+start,j-start); 510 if (tokens[elements] == NULL) goto cleanup; 511 elements++; 512 start = j+seplen; 513 j = j+seplen-1; /* skip the separator */ 514 } 515 } 516 /* Add the final element. We are sure there is room in the tokens array. */ 517 tokens[elements] = sdsnewlen(s+start,len-start); 518 if (tokens[elements] == NULL) goto cleanup; 519 elements++; 520 *count = elements; 521 return tokens; 522 523 cleanup: 524 { 525 int i; 526 for (i = 0; i < elements; i++) sdsfree(tokens[i]); 527 zfree(tokens); 528 *count = 0; 529 return NULL; 530 } 531 } 532 533 /* Free the result returned by sdssplitlen(), or do nothing if 'tokens' is NULL. */ 534 void sdsfreesplitres(sds *tokens, int count) { 535 if (!tokens) return; 536 while(count--) 537 sdsfree(tokens[count]); 538 zfree(tokens); 539 } 540 541 /* Create an sds string from a long long value. It is much faster than: 542 * 543 * sdscatprintf(sdsempty(),"%lld\n", value); 544 */ 545 sds sdsfromlonglong(long long value) { 546 char buf[32], *p; 547 unsigned long long v; 548 549 v = (value < 0) ? -value : value; 550 p = buf+31; /* point to the last character */ 551 do { 552 *p-- = '0'+(v%10); 553 v /= 10; 554 } while(v); 555 if (value < 0) *p-- = '-'; 556 p++; 557 return sdsnewlen(p,32-(p-buf)); 558 } 559 560 /* Append to the sds string "s" an escaped string representation where 561 * all the non-printable characters (tested with isprint()) are turned into 562 * escapes in the form "\n\r\a...." or "\x<hex-number>". 563 * 564 * After the call, the modified sds string is no longer valid and all the 565 * references must be substituted with the new pointer returned by the call. */ 566 sds sdscatrepr(sds s, const char *p, size_t len) { 567 s = sdscatlen(s,"\"",1); 568 while(len--) { 569 switch(*p) { 570 case '\\': 571 case '"': 572 s = sdscatprintf(s,"\\%c",*p); 573 break; 574 case '\n': s = sdscatlen(s,"\\n",2); break; 575 case '\r': s = sdscatlen(s,"\\r",2); break; 576 case '\t': s = sdscatlen(s,"\\t",2); break; 577 case '\a': s = sdscatlen(s,"\\a",2); break; 578 case '\b': s = sdscatlen(s,"\\b",2); break; 579 default: 580 if (isprint(*p)) 581 s = sdscatprintf(s,"%c",*p); 582 else 583 s = sdscatprintf(s,"\\x%02x",(unsigned char)*p); 584 break; 585 } 586 p++; 587 } 588 return sdscatlen(s,"\"",1); 589 } 590 591 /* Helper function for sdssplitargs() that returns non zero if 'c' 592 * is a valid hex digit. */ 593 int is_hex_digit(char c) { 594 return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || 595 (c >= 'A' && c <= 'F'); 596 } 597 598 /* Helper function for sdssplitargs() that converts a hex digit into an 599 * integer from 0 to 15 */ 600 int hex_digit_to_int(char c) { 601 switch(c) { 602 case '0': return 0; 603 case '1': return 1; 604 case '2': return 2; 605 case '3': return 3; 606 case '4': return 4; 607 case '5': return 5; 608 case '6': return 6; 609 case '7': return 7; 610 case '8': return 8; 611 case '9': return 9; 612 case 'a': case 'A': return 10; 613 case 'b': case 'B': return 11; 614 case 'c': case 'C': return 12; 615 case 'd': case 'D': return 13; 616 case 'e': case 'E': return 14; 617 case 'f': case 'F': return 15; 618 default: return 0; 619 } 620 } 621 622 /* Split a line into arguments, where every argument can be in the 623 * following programming-language REPL-alike form: 624 * 625 * foo bar "newline are supported\n" and "\xff\x00otherstuff" 626 * 627 * The number of arguments is stored into *argc, and an array 628 * of sds is returned. 629 * 630 * The caller should free the resulting array of sds strings with 631 * sdsfreesplitres(). 632 * 633 * Note that sdscatrepr() is able to convert back a string into 634 * a quoted string in the same format sdssplitargs() is able to parse. 635 * 636 * The function returns the allocated tokens on success, even when the 637 * input string is empty, or NULL if the input contains unbalanced 638 * quotes or closed quotes followed by non space characters 639 * as in: "foo"bar or "foo' 640 */ 641 sds *sdssplitargs(const char *line, int *argc) { 642 const char *p = line; 643 char *current = NULL; 644 char **vector = NULL; 645 646 *argc = 0; 647 while(1) { 648 /* skip blanks */ 649 while(*p && isspace(*p)) p++; 650 if (*p) { 651 /* get a token */ 652 int inq=0; /* set to 1 if we are in "quotes" */ 653 int insq=0; /* set to 1 if we are in 'single quotes' */ 654 int done=0; 655 656 if (current == NULL) current = sdsempty(); 657 while(!done) { 658 if (inq) { 659 if (*p == '\\' && *(p+1) == 'x' && 660 is_hex_digit(*(p+2)) && 661 is_hex_digit(*(p+3))) 662 { 663 unsigned char byte; 664 665 byte = (hex_digit_to_int(*(p+2))*16)+ 666 hex_digit_to_int(*(p+3)); 667 current = sdscatlen(current,(char*)&byte,1); 668 p += 3; 669 } else if (*p == '\\' && *(p+1)) { 670 char c; 671 672 p++; 673 switch(*p) { 674 case 'n': c = '\n'; break; 675 case 'r': c = '\r'; break; 676 case 't': c = '\t'; break; 677 case 'b': c = '\b'; break; 678 case 'a': c = '\a'; break; 679 default: c = *p; break; 680 } 681 current = sdscatlen(current,&c,1); 682 } else if (*p == '"') { 683 /* closing quote must be followed by a space or 684 * nothing at all. */ 685 if (*(p+1) && !isspace(*(p+1))) goto err; 686 done=1; 687 } else if (!*p) { 688 /* unterminated quotes */ 689 goto err; 690 } else { 691 current = sdscatlen(current,p,1); 692 } 693 } else if (insq) { 694 if (*p == '\\' && *(p+1) == '\'') { 695 p++; 696 current = sdscatlen(current,"'",1); 697 } else if (*p == '\'') { 698 /* closing quote must be followed by a space or 699 * nothing at all. */ 700 if (*(p+1) && !isspace(*(p+1))) goto err; 701 done=1; 702 } else if (!*p) { 703 /* unterminated quotes */ 704 goto err; 705 } else { 706 current = sdscatlen(current,p,1); 707 } 708 } else { 709 switch(*p) { 710 case ' ': 711 case '\n': 712 case '\r': 713 case '\t': 714 case '\0': 715 done=1; 716 break; 717 case '"': 718 inq=1; 719 break; 720 case '\'': 721 insq=1; 722 break; 723 default: 724 current = sdscatlen(current,p,1); 725 break; 726 } 727 } 728 if (*p) p++; 729 } 730 /* add the token to the vector */ 731 vector = zrealloc(vector,((*argc)+1)*sizeof(char*)); 732 vector[*argc] = current; 733 (*argc)++; 734 current = NULL; 735 } else { 736 /* Even on empty input string return something not NULL. */ 737 if (vector == NULL) vector = zmalloc(sizeof(void*)); 738 return vector; 739 } 740 } 741 742 err: 743 while((*argc)--) 744 sdsfree(vector[*argc]); 745 zfree(vector); 746 if (current) sdsfree(current); 747 *argc = 0; 748 return NULL; 749 } 750 751 /* Modify the string substituting all the occurrences of the set of 752 * characters specified in the 'from' string to the corresponding character 753 * in the 'to' array. 754 * 755 * For instance: sdsmapchars(mystring, "ho", "01", 2) 756 * will have the effect of turning the string "hello" into "0ell1". 757 * 758 * The function returns the sds string pointer, that is always the same 759 * as the input pointer since no resize is needed. */ 760 sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) { 761 size_t j, i, l = sdslen(s); 762 763 for (j = 0; j < l; j++) { 764 for (i = 0; i < setlen; i++) { 765 if (s[j] == from[i]) { 766 s[j] = to[i]; 767 break; 768 } 769 } 770 } 771 return s; 772 } 773 774 /* Join an array of C strings using the specified separator (also a C string). 775 * Returns the result as an sds string. */ 776 sds sdsjoin(char **argv, int argc, char *sep) { 777 sds join = sdsempty(); 778 int j; 779 780 for (j = 0; j < argc; j++) { 781 join = sdscat(join, argv[j]); 782 if (j != argc-1) join = sdscat(join,sep); 783 } 784 return join; 785 } 786 787 #ifdef SDS_TEST_MAIN 788 #include <stdio.h> 789 #include "testhelp.h" 790 791 int main(void) { 792 { 793 struct sdshdr *sh; 794 sds x = sdsnew("foo"), y; 795 796 test_cond("Create a string and obtain the length", 797 sdslen(x) == 3 && memcmp(x,"foo\0",4) == 0) 798 799 sdsfree(x); 800 x = sdsnewlen("foo",2); 801 test_cond("Create a string with specified length", 802 sdslen(x) == 2 && memcmp(x,"fo\0",3) == 0) 803 804 x = sdscat(x,"bar"); 805 test_cond("Strings concatenation", 806 sdslen(x) == 5 && memcmp(x,"fobar\0",6) == 0); 807 808 x = sdscpy(x,"a"); 809 test_cond("sdscpy() against an originally longer string", 810 sdslen(x) == 1 && memcmp(x,"a\0",2) == 0) 811 812 x = sdscpy(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk"); 813 test_cond("sdscpy() against an originally shorter string", 814 sdslen(x) == 33 && 815 memcmp(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0) 816 817 sdsfree(x); 818 x = sdscatprintf(sdsempty(),"%d",123); 819 test_cond("sdscatprintf() seems working in the base case", 820 sdslen(x) == 3 && memcmp(x,"123\0",4) ==0) 821 822 sdsfree(x); 823 x = sdstrim(sdsnew("xxciaoyyy"),"xy"); 824 test_cond("sdstrim() correctly trims characters", 825 sdslen(x) == 4 && memcmp(x,"ciao\0",5) == 0) 826 827 y = sdsrange(sdsdup(x),1,1); 828 test_cond("sdsrange(...,1,1)", 829 sdslen(y) == 1 && memcmp(y,"i\0",2) == 0) 830 831 sdsfree(y); 832 y = sdsrange(sdsdup(x),1,-1); 833 test_cond("sdsrange(...,1,-1)", 834 sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0) 835 836 sdsfree(y); 837 y = sdsrange(sdsdup(x),-2,-1); 838 test_cond("sdsrange(...,-2,-1)", 839 sdslen(y) == 2 && memcmp(y,"ao\0",3) == 0) 840 841 sdsfree(y); 842 y = sdsrange(sdsdup(x),2,1); 843 test_cond("sdsrange(...,2,1)", 844 sdslen(y) == 0 && memcmp(y,"\0",1) == 0) 845 846 sdsfree(y); 847 y = sdsrange(sdsdup(x),1,100); 848 test_cond("sdsrange(...,1,100)", 849 sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0) 850 851 sdsfree(y); 852 y = sdsrange(sdsdup(x),100,100); 853 test_cond("sdsrange(...,100,100)", 854 sdslen(y) == 0 && memcmp(y,"\0",1) == 0) 855 856 sdsfree(y); 857 sdsfree(x); 858 x = sdsnew("foo"); 859 y = sdsnew("foa"); 860 test_cond("sdscmp(foo,foa)", sdscmp(x,y) > 0) 861 862 sdsfree(y); 863 sdsfree(x); 864 x = sdsnew("bar"); 865 y = sdsnew("bar"); 866 test_cond("sdscmp(bar,bar)", sdscmp(x,y) == 0) 867 868 sdsfree(y); 869 sdsfree(x); 870 x = sdsnew("aar"); 871 y = sdsnew("bar"); 872 test_cond("sdscmp(bar,bar)", sdscmp(x,y) < 0) 873 874 { 875 int oldfree; 876 877 sdsfree(x); 878 x = sdsnew("0"); 879 sh = (void*) (x-(sizeof(struct sdshdr))); 880 test_cond("sdsnew() free/len buffers", sh->len == 1 && sh->free == 0); 881 x = sdsMakeRoomFor(x,1); 882 sh = (void*) (x-(sizeof(struct sdshdr))); 883 test_cond("sdsMakeRoomFor()", sh->len == 1 && sh->free > 0); 884 oldfree = sh->free; 885 x[1] = '1'; 886 sdsIncrLen(x,1); 887 test_cond("sdsIncrLen() -- content", x[0] == '0' && x[1] == '1'); 888 test_cond("sdsIncrLen() -- len", sh->len == 2); 889 test_cond("sdsIncrLen() -- free", sh->free == oldfree-1); 890 } 891 } 892 test_report() 893 return 0; 894 } 895 #endif 896