sds.c 28 KB

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