sds.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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. if (incr >= 0)
  186. assert(sh->free >= (unsigned int)incr);
  187. else
  188. assert(sh->len >= (unsigned int)(-incr));
  189. sh->len += incr;
  190. sh->free -= incr;
  191. s[sh->len] = '\0';
  192. }
  193. /* Grow the sds to have the specified length. Bytes that were not part of
  194. * the original length of the sds will be set to zero.
  195. *
  196. * if the specified length is smaller than the current length, no operation
  197. * is performed. */
  198. sds sdsgrowzero(sds s, size_t len) {
  199. struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
  200. size_t totlen, curlen = sh->len;
  201. if (len <= curlen) return s;
  202. s = sdsMakeRoomFor(s,len-curlen);
  203. if (s == NULL) return NULL;
  204. /* Make sure added region doesn't contain garbage */
  205. sh = (void*)(s-(sizeof(struct sdshdr)));
  206. memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */
  207. totlen = sh->len+sh->free;
  208. sh->len = len;
  209. sh->free = totlen-sh->len;
  210. return s;
  211. }
  212. /* Append the specified binary-safe string pointed by 't' of 'len' bytes to the
  213. * end of the specified sds string 's'.
  214. *
  215. * After the call, the passed sds string is no longer valid and all the
  216. * references must be substituted with the new pointer returned by the call. */
  217. sds sdscatlen(sds s, const void *t, size_t len) {
  218. struct sdshdr *sh;
  219. size_t curlen = sdslen(s);
  220. s = sdsMakeRoomFor(s,len);
  221. if (s == NULL) return NULL;
  222. sh = (void*) (s-(sizeof(struct sdshdr)));
  223. memcpy(s+curlen, t, len);
  224. sh->len = curlen+len;
  225. sh->free = sh->free-len;
  226. s[curlen+len] = '\0';
  227. return s;
  228. }
  229. /* Append the specified null termianted C string to the sds string 's'.
  230. *
  231. * After the call, the passed sds string is no longer valid and all the
  232. * references must be substituted with the new pointer returned by the call. */
  233. sds sdscat(sds s, const char *t) {
  234. return sdscatlen(s, t, strlen(t));
  235. }
  236. /* Append the specified sds 't' to the existing sds 's'.
  237. *
  238. * After the call, the modified sds string is no longer valid and all the
  239. * references must be substituted with the new pointer returned by the call. */
  240. sds sdscatsds(sds s, const sds t) {
  241. return sdscatlen(s, t, sdslen(t));
  242. }
  243. /* Destructively modify the sds string 's' to hold the specified binary
  244. * safe string pointed by 't' of length 'len' bytes. */
  245. sds sdscpylen(sds s, const char *t, size_t len) {
  246. struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
  247. size_t totlen = sh->free+sh->len;
  248. if (totlen < len) {
  249. s = sdsMakeRoomFor(s,len-sh->len);
  250. if (s == NULL) return NULL;
  251. sh = (void*) (s-(sizeof(struct sdshdr)));
  252. totlen = sh->free+sh->len;
  253. }
  254. memcpy(s, t, len);
  255. s[len] = '\0';
  256. sh->len = len;
  257. sh->free = totlen-len;
  258. return s;
  259. }
  260. /* Like sdscpylen() but 't' must be a null-termined string so that the length
  261. * of the string is obtained with strlen(). */
  262. sds sdscpy(sds s, const char *t) {
  263. return sdscpylen(s, t, strlen(t));
  264. }
  265. /* Helper for sdscatlonglong() doing the actual number -> string
  266. * conversion. 's' must point to a string with room for at least
  267. * SDS_LLSTR_SIZE bytes.
  268. *
  269. * The function returns the lenght of the null-terminated string
  270. * representation stored at 's'. */
  271. #define SDS_LLSTR_SIZE 21
  272. int sdsll2str(char *s, long long value) {
  273. char *p, aux;
  274. unsigned long long v;
  275. size_t l;
  276. /* Generate the string representation, this method produces
  277. * an reversed string. */
  278. v = (value < 0) ? -value : value;
  279. p = s;
  280. do {
  281. *p++ = '0'+(v%10);
  282. v /= 10;
  283. } while(v);
  284. if (value < 0) *p++ = '-';
  285. /* Compute length and add null term. */
  286. l = p-s;
  287. *p = '\0';
  288. /* Reverse the string. */
  289. p--;
  290. while(s < p) {
  291. aux = *s;
  292. *s = *p;
  293. *p = aux;
  294. s++;
  295. p--;
  296. }
  297. return l;
  298. }
  299. /* Identical sdsll2str(), but for unsigned long long type. */
  300. int sdsull2str(char *s, unsigned long long v) {
  301. char *p, aux;
  302. size_t l;
  303. /* Generate the string representation, this method produces
  304. * an reversed string. */
  305. p = s;
  306. do {
  307. *p++ = '0'+(v%10);
  308. v /= 10;
  309. } while(v);
  310. /* Compute length and add null term. */
  311. l = p-s;
  312. *p = '\0';
  313. /* Reverse the string. */
  314. p--;
  315. while(s < p) {
  316. aux = *s;
  317. *s = *p;
  318. *p = aux;
  319. s++;
  320. p--;
  321. }
  322. return l;
  323. }
  324. /* Create an sds string from a long long value. It is much faster than:
  325. *
  326. * sdscatprintf(sdsempty(),"%lld\n", value);
  327. */
  328. sds sdsfromlonglong(long long value) {
  329. char buf[SDS_LLSTR_SIZE];
  330. int len = sdsll2str(buf,value);
  331. return sdsnewlen(buf,len);
  332. }
  333. /* Like sdscatpritf() but gets va_list instead of being variadic. */
  334. sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
  335. va_list cpy;
  336. char staticbuf[1024], *buf = staticbuf, *t;
  337. size_t buflen = strlen(fmt)*2;
  338. /* We try to start using a static buffer for speed.
  339. * If not possible we revert to heap allocation. */
  340. if (buflen > sizeof(staticbuf)) {
  341. buf = zmalloc(buflen);
  342. if (buf == NULL) return NULL;
  343. } else {
  344. buflen = sizeof(staticbuf);
  345. }
  346. /* Try with buffers two times bigger every time we fail to
  347. * fit the string in the current buffer size. */
  348. while(1) {
  349. buf[buflen-2] = '\0';
  350. va_copy(cpy,ap);
  351. vsnprintf(buf, buflen, fmt, cpy);
  352. va_end(ap);
  353. if (buf[buflen-2] != '\0') {
  354. if (buf != staticbuf) zfree(buf);
  355. buflen *= 2;
  356. buf = zmalloc(buflen);
  357. if (buf == NULL) return NULL;
  358. continue;
  359. }
  360. break;
  361. }
  362. /* Finally concat the obtained string to the SDS string and return it. */
  363. t = sdscat(s, buf);
  364. if (buf != staticbuf) zfree(buf);
  365. return t;
  366. }
  367. /* Append to the sds string 's' a string obtained using printf-alike format
  368. * specifier.
  369. *
  370. * After the call, the modified sds string is no longer valid and all the
  371. * references must be substituted with the new pointer returned by the call.
  372. *
  373. * Example:
  374. *
  375. * s = sdsempty("Sum is: ");
  376. * s = sdscatprintf(s,"%d+%d = %d",a,b,a+b).
  377. *
  378. * Often you need to create a string from scratch with the printf-alike
  379. * format. When this is the need, just use sdsempty() as the target string:
  380. *
  381. * s = sdscatprintf(sdsempty(), "... your format ...", args);
  382. */
  383. sds sdscatprintf(sds s, const char *fmt, ...) {
  384. va_list ap;
  385. char *t;
  386. va_start(ap, fmt);
  387. t = sdscatvprintf(s,fmt,ap);
  388. va_end(ap);
  389. return t;
  390. }
  391. /* This function is similar to sdscatprintf, but much faster as it does
  392. * not rely on sprintf() family functions implemented by the libc that
  393. * are often very slow. Moreover directly handling the sds string as
  394. * new data is concatenated provides a performance improvement.
  395. *
  396. * However this function only handles an incompatible subset of printf-alike
  397. * format specifiers:
  398. *
  399. * %s - C String
  400. * %S - SDS string
  401. * %i - signed int
  402. * %I - 64 bit signed integer (long long, int64_t)
  403. * %u - unsigned int
  404. * %U - 64 bit unsigned integer (unsigned long long, uint64_t)
  405. * %% - Verbatim "%" character.
  406. */
  407. sds sdscatfmt(sds s, char const *fmt, ...) {
  408. struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
  409. size_t initlen = sdslen(s);
  410. const char *f = fmt;
  411. int i;
  412. va_list ap;
  413. va_start(ap,fmt);
  414. f = fmt; /* Next format specifier byte to process. */
  415. i = initlen; /* Position of the next byte to write to dest str. */
  416. while(*f) {
  417. char next, *str;
  418. unsigned int l;
  419. long long num;
  420. unsigned long long unum;
  421. /* Make sure there is always space for at least 1 char. */
  422. if (sh->free == 0) {
  423. s = sdsMakeRoomFor(s,1);
  424. sh = (void*) (s-(sizeof(struct sdshdr)));
  425. }
  426. switch(*f) {
  427. case '%':
  428. next = *(f+1);
  429. f++;
  430. switch(next) {
  431. case 's':
  432. case 'S':
  433. str = va_arg(ap,char*);
  434. l = (next == 's') ? strlen(str) : sdslen(str);
  435. if (sh->free < l) {
  436. s = sdsMakeRoomFor(s,l);
  437. sh = (void*) (s-(sizeof(struct sdshdr)));
  438. }
  439. memcpy(s+i,str,l);
  440. sh->len += l;
  441. sh->free -= l;
  442. i += l;
  443. break;
  444. case 'i':
  445. case 'I':
  446. if (next == 'i')
  447. num = va_arg(ap,int);
  448. else
  449. num = va_arg(ap,long long);
  450. {
  451. char buf[SDS_LLSTR_SIZE];
  452. l = sdsll2str(buf,num);
  453. if (sh->free < l) {
  454. s = sdsMakeRoomFor(s,l);
  455. sh = (void*) (s-(sizeof(struct sdshdr)));
  456. }
  457. memcpy(s+i,buf,l);
  458. sh->len += l;
  459. sh->free -= l;
  460. i += l;
  461. }
  462. break;
  463. case 'u':
  464. case 'U':
  465. if (next == 'u')
  466. unum = va_arg(ap,unsigned int);
  467. else
  468. unum = va_arg(ap,unsigned long long);
  469. {
  470. char buf[SDS_LLSTR_SIZE];
  471. l = sdsull2str(buf,unum);
  472. if (sh->free < l) {
  473. s = sdsMakeRoomFor(s,l);
  474. sh = (void*) (s-(sizeof(struct sdshdr)));
  475. }
  476. memcpy(s+i,buf,l);
  477. sh->len += l;
  478. sh->free -= l;
  479. i += l;
  480. }
  481. break;
  482. default: /* Handle %% and generally %<unknown>. */
  483. s[i++] = next;
  484. sh->len += 1;
  485. sh->free -= 1;
  486. break;
  487. }
  488. break;
  489. default:
  490. s[i++] = *f;
  491. sh->len += 1;
  492. sh->free -= 1;
  493. break;
  494. }
  495. f++;
  496. }
  497. va_end(ap);
  498. /* Add null-term */
  499. s[i] = '\0';
  500. return s;
  501. }
  502. /* Remove the part of the string from left and from right composed just of
  503. * contiguous characters found in 'cset', that is a null terminted C string.
  504. *
  505. * After the call, the modified sds string is no longer valid and all the
  506. * references must be substituted with the new pointer returned by the call.
  507. *
  508. * Example:
  509. *
  510. * s = sdsnew("AA...AA.a.aa.aHelloWorld :::");
  511. * s = sdstrim(s,"A. :");
  512. * printf("%s\n", s);
  513. *
  514. * Output will be just "Hello World".
  515. */
  516. sds sdstrim(sds s, const char *cset) {
  517. struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
  518. char *start, *end, *sp, *ep;
  519. size_t len;
  520. sp = start = s;
  521. ep = end = s+sdslen(s)-1;
  522. while(sp <= end && strchr(cset, *sp)) sp++;
  523. while(ep > start && strchr(cset, *ep)) ep--;
  524. len = (sp > ep) ? 0 : ((ep-sp)+1);
  525. if (sh->buf != sp) memmove(sh->buf, sp, len);
  526. sh->buf[len] = '\0';
  527. sh->free = sh->free+(sh->len-len);
  528. sh->len = len;
  529. return s;
  530. }
  531. /* Turn the string into a smaller (or equal) string containing only the
  532. * substring specified by the 'start' and 'end' indexes.
  533. *
  534. * start and end can be negative, where -1 means the last character of the
  535. * string, -2 the penultimate character, and so forth.
  536. *
  537. * The interval is inclusive, so the start and end characters will be part
  538. * of the resulting string.
  539. *
  540. * The string is modified in-place.
  541. *
  542. * Example:
  543. *
  544. * s = sdsnew("Hello World");
  545. * sdsrange(s,1,-1); => "ello World"
  546. */
  547. void sdsrange(sds s, int start, int end) {
  548. struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
  549. size_t newlen, len = sdslen(s);
  550. if (len == 0) return;
  551. if (start < 0) {
  552. start = len+start;
  553. if (start < 0) start = 0;
  554. }
  555. if (end < 0) {
  556. end = len+end;
  557. if (end < 0) end = 0;
  558. }
  559. newlen = (start > end) ? 0 : (end-start)+1;
  560. if (newlen != 0) {
  561. if (start >= (signed)len) {
  562. newlen = 0;
  563. } else if (end >= (signed)len) {
  564. end = len-1;
  565. newlen = (start > end) ? 0 : (end-start)+1;
  566. }
  567. } else {
  568. start = 0;
  569. }
  570. if (start && newlen) memmove(sh->buf, sh->buf+start, newlen);
  571. sh->buf[newlen] = 0;
  572. sh->free = sh->free+(sh->len-newlen);
  573. sh->len = newlen;
  574. }
  575. /* Apply tolower() to every character of the sds string 's'. */
  576. void sdstolower(sds s) {
  577. int len = sdslen(s), j;
  578. for (j = 0; j < len; j++) s[j] = tolower(s[j]);
  579. }
  580. /* Apply toupper() to every character of the sds string 's'. */
  581. void sdstoupper(sds s) {
  582. int len = sdslen(s), j;
  583. for (j = 0; j < len; j++) s[j] = toupper(s[j]);
  584. }
  585. /* Compare two sds strings s1 and s2 with memcmp().
  586. *
  587. * Return value:
  588. *
  589. * 1 if s1 > s2.
  590. * -1 if s1 < s2.
  591. * 0 if s1 and s2 are exactly the same binary string.
  592. *
  593. * If two strings share exactly the same prefix, but one of the two has
  594. * additional characters, the longer string is considered to be greater than
  595. * the smaller one. */
  596. int sdscmp(const sds s1, const sds s2) {
  597. size_t l1, l2, minlen;
  598. int cmp;
  599. l1 = sdslen(s1);
  600. l2 = sdslen(s2);
  601. minlen = (l1 < l2) ? l1 : l2;
  602. cmp = memcmp(s1,s2,minlen);
  603. if (cmp == 0) return l1-l2;
  604. return cmp;
  605. }
  606. /* Split 's' with separator in 'sep'. An array
  607. * of sds strings is returned. *count will be set
  608. * by reference to the number of tokens returned.
  609. *
  610. * On out of memory, zero length string, zero length
  611. * separator, NULL is returned.
  612. *
  613. * Note that 'sep' is able to split a string using
  614. * a multi-character separator. For example
  615. * sdssplit("foo_-_bar","_-_"); will return two
  616. * elements "foo" and "bar".
  617. *
  618. * This version of the function is binary-safe but
  619. * requires length arguments. sdssplit() is just the
  620. * same function but for zero-terminated strings.
  621. */
  622. sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count) {
  623. int elements = 0, slots = 5, start = 0, j;
  624. sds *tokens;
  625. if (seplen < 1 || len < 0) return NULL;
  626. tokens = zmalloc(sizeof(sds)*slots);
  627. if (tokens == NULL) return NULL;
  628. if (len == 0) {
  629. *count = 0;
  630. return tokens;
  631. }
  632. for (j = 0; j < (len-(seplen-1)); j++) {
  633. /* make sure there is room for the next element and the final one */
  634. if (slots < elements+2) {
  635. sds *newtokens;
  636. slots *= 2;
  637. newtokens = zrealloc(tokens,sizeof(sds)*slots);
  638. if (newtokens == NULL) goto cleanup;
  639. tokens = newtokens;
  640. }
  641. /* search the separator */
  642. if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) {
  643. tokens[elements] = sdsnewlen(s+start,j-start);
  644. if (tokens[elements] == NULL) goto cleanup;
  645. elements++;
  646. start = j+seplen;
  647. j = j+seplen-1; /* skip the separator */
  648. }
  649. }
  650. /* Add the final element. We are sure there is room in the tokens array. */
  651. tokens[elements] = sdsnewlen(s+start,len-start);
  652. if (tokens[elements] == NULL) goto cleanup;
  653. elements++;
  654. *count = elements;
  655. return tokens;
  656. cleanup:
  657. {
  658. int i;
  659. for (i = 0; i < elements; i++) sdsfree(tokens[i]);
  660. zfree(tokens);
  661. *count = 0;
  662. return NULL;
  663. }
  664. }
  665. /* Free the result returned by sdssplitlen(), or do nothing if 'tokens' is NULL. */
  666. void sdsfreesplitres(sds *tokens, int count) {
  667. if (!tokens) return;
  668. while(count--)
  669. sdsfree(tokens[count]);
  670. zfree(tokens);
  671. }
  672. /* Append to the sds string "s" an escaped string representation where
  673. * all the non-printable characters (tested with isprint()) are turned into
  674. * escapes in the form "\n\r\a...." or "\x<hex-number>".
  675. *
  676. * After the call, the modified sds string is no longer valid and all the
  677. * references must be substituted with the new pointer returned by the call. */
  678. sds sdscatrepr(sds s, const char *p, size_t len) {
  679. s = sdscatlen(s,"\"",1);
  680. while(len--) {
  681. switch(*p) {
  682. case '\\':
  683. case '"':
  684. s = sdscatprintf(s,"\\%c",*p);
  685. break;
  686. case '\n': s = sdscatlen(s,"\\n",2); break;
  687. case '\r': s = sdscatlen(s,"\\r",2); break;
  688. case '\t': s = sdscatlen(s,"\\t",2); break;
  689. case '\a': s = sdscatlen(s,"\\a",2); break;
  690. case '\b': s = sdscatlen(s,"\\b",2); break;
  691. default:
  692. if (isprint(*p))
  693. s = sdscatprintf(s,"%c",*p);
  694. else
  695. s = sdscatprintf(s,"\\x%02x",(unsigned char)*p);
  696. break;
  697. }
  698. p++;
  699. }
  700. return sdscatlen(s,"\"",1);
  701. }
  702. /* Helper function for sdssplitargs() that returns non zero if 'c'
  703. * is a valid hex digit. */
  704. int is_hex_digit(char c) {
  705. return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
  706. (c >= 'A' && c <= 'F');
  707. }
  708. /* Helper function for sdssplitargs() that converts a hex digit into an
  709. * integer from 0 to 15 */
  710. int hex_digit_to_int(char c) {
  711. switch(c) {
  712. case '0': return 0;
  713. case '1': return 1;
  714. case '2': return 2;
  715. case '3': return 3;
  716. case '4': return 4;
  717. case '5': return 5;
  718. case '6': return 6;
  719. case '7': return 7;
  720. case '8': return 8;
  721. case '9': return 9;
  722. case 'a': case 'A': return 10;
  723. case 'b': case 'B': return 11;
  724. case 'c': case 'C': return 12;
  725. case 'd': case 'D': return 13;
  726. case 'e': case 'E': return 14;
  727. case 'f': case 'F': return 15;
  728. default: return 0;
  729. }
  730. }
  731. /* Split a line into arguments, where every argument can be in the
  732. * following programming-language REPL-alike form:
  733. *
  734. * foo bar "newline are supported\n" and "\xff\x00otherstuff"
  735. *
  736. * The number of arguments is stored into *argc, and an array
  737. * of sds is returned.
  738. *
  739. * The caller should free the resulting array of sds strings with
  740. * sdsfreesplitres().
  741. *
  742. * Note that sdscatrepr() is able to convert back a string into
  743. * a quoted string in the same format sdssplitargs() is able to parse.
  744. *
  745. * The function returns the allocated tokens on success, even when the
  746. * input string is empty, or NULL if the input contains unbalanced
  747. * quotes or closed quotes followed by non space characters
  748. * as in: "foo"bar or "foo'
  749. */
  750. sds *sdssplitargs(const char *line, int *argc) {
  751. const char *p = line;
  752. char *current = NULL;
  753. char **vector = NULL;
  754. *argc = 0;
  755. while(1) {
  756. /* skip blanks */
  757. while(*p && isspace(*p)) p++;
  758. if (*p) {
  759. /* get a token */
  760. int inq=0; /* set to 1 if we are in "quotes" */
  761. int insq=0; /* set to 1 if we are in 'single quotes' */
  762. int done=0;
  763. if (current == NULL) current = sdsempty();
  764. while(!done) {
  765. if (inq) {
  766. if (*p == '\\' && *(p+1) == 'x' &&
  767. is_hex_digit(*(p+2)) &&
  768. is_hex_digit(*(p+3)))
  769. {
  770. unsigned char byte;
  771. byte = (hex_digit_to_int(*(p+2))*16)+
  772. hex_digit_to_int(*(p+3));
  773. current = sdscatlen(current,(char*)&byte,1);
  774. p += 3;
  775. } else if (*p == '\\' && *(p+1)) {
  776. char c;
  777. p++;
  778. switch(*p) {
  779. case 'n': c = '\n'; break;
  780. case 'r': c = '\r'; break;
  781. case 't': c = '\t'; break;
  782. case 'b': c = '\b'; break;
  783. case 'a': c = '\a'; break;
  784. default: c = *p; break;
  785. }
  786. current = sdscatlen(current,&c,1);
  787. } else if (*p == '"') {
  788. /* closing quote must be followed by a space or
  789. * nothing at all. */
  790. if (*(p+1) && !isspace(*(p+1))) goto err;
  791. done=1;
  792. } else if (!*p) {
  793. /* unterminated quotes */
  794. goto err;
  795. } else {
  796. current = sdscatlen(current,p,1);
  797. }
  798. } else if (insq) {
  799. if (*p == '\\' && *(p+1) == '\'') {
  800. p++;
  801. current = sdscatlen(current,"'",1);
  802. } else if (*p == '\'') {
  803. /* closing quote must be followed by a space or
  804. * nothing at all. */
  805. if (*(p+1) && !isspace(*(p+1))) goto err;
  806. done=1;
  807. } else if (!*p) {
  808. /* unterminated quotes */
  809. goto err;
  810. } else {
  811. current = sdscatlen(current,p,1);
  812. }
  813. } else {
  814. switch(*p) {
  815. case ' ':
  816. case '\n':
  817. case '\r':
  818. case '\t':
  819. case '\0':
  820. done=1;
  821. break;
  822. case '"':
  823. inq=1;
  824. break;
  825. case '\'':
  826. insq=1;
  827. break;
  828. default:
  829. current = sdscatlen(current,p,1);
  830. break;
  831. }
  832. }
  833. if (*p) p++;
  834. }
  835. /* add the token to the vector */
  836. vector = zrealloc(vector,((*argc)+1)*sizeof(char*));
  837. vector[*argc] = current;
  838. (*argc)++;
  839. current = NULL;
  840. } else {
  841. /* Even on empty input string return something not NULL. */
  842. if (vector == NULL) vector = zmalloc(sizeof(void*));
  843. return vector;
  844. }
  845. }
  846. err:
  847. while((*argc)--)
  848. sdsfree(vector[*argc]);
  849. zfree(vector);
  850. if (current) sdsfree(current);
  851. *argc = 0;
  852. return NULL;
  853. }
  854. /* Modify the string substituting all the occurrences of the set of
  855. * characters specified in the 'from' string to the corresponding character
  856. * in the 'to' array.
  857. *
  858. * For instance: sdsmapchars(mystring, "ho", "01", 2)
  859. * will have the effect of turning the string "hello" into "0ell1".
  860. *
  861. * The function returns the sds string pointer, that is always the same
  862. * as the input pointer since no resize is needed. */
  863. sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) {
  864. size_t j, i, l = sdslen(s);
  865. for (j = 0; j < l; j++) {
  866. for (i = 0; i < setlen; i++) {
  867. if (s[j] == from[i]) {
  868. s[j] = to[i];
  869. break;
  870. }
  871. }
  872. }
  873. return s;
  874. }
  875. /* Join an array of C strings using the specified separator (also a C string).
  876. * Returns the result as an sds string. */
  877. sds sdsjoin(char **argv, int argc, char *sep) {
  878. sds join = sdsempty();
  879. int j;
  880. for (j = 0; j < argc; j++) {
  881. join = sdscat(join, argv[j]);
  882. if (j != argc-1) join = sdscat(join,sep);
  883. }
  884. return join;
  885. }
  886. #ifdef SDS_TEST_MAIN
  887. #include <stdio.h>
  888. #include "testhelp.h"
  889. #include "limits.h"
  890. int main(void) {
  891. {
  892. struct sdshdr *sh;
  893. sds x = sdsnew("foo"), y;
  894. test_cond("Create a string and obtain the length",
  895. sdslen(x) == 3 && memcmp(x,"foo\0",4) == 0)
  896. sdsfree(x);
  897. x = sdsnewlen("foo",2);
  898. test_cond("Create a string with specified length",
  899. sdslen(x) == 2 && memcmp(x,"fo\0",3) == 0)
  900. x = sdscat(x,"bar");
  901. test_cond("Strings concatenation",
  902. sdslen(x) == 5 && memcmp(x,"fobar\0",6) == 0);
  903. x = sdscpy(x,"a");
  904. test_cond("sdscpy() against an originally longer string",
  905. sdslen(x) == 1 && memcmp(x,"a\0",2) == 0)
  906. x = sdscpy(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk");
  907. test_cond("sdscpy() against an originally shorter string",
  908. sdslen(x) == 33 &&
  909. memcmp(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0)
  910. sdsfree(x);
  911. x = sdscatprintf(sdsempty(),"%d",123);
  912. test_cond("sdscatprintf() seems working in the base case",
  913. sdslen(x) == 3 && memcmp(x,"123\0",4) == 0)
  914. sdsfree(x);
  915. x = sdsnew("--");
  916. x = sdscatfmt(x, "Hello %s World %I,%I--", "Hi!", LLONG_MIN,LLONG_MAX);
  917. test_cond("sdscatfmt() seems working in the base case",
  918. sdslen(x) == 60 &&
  919. memcmp(x,"--Hello Hi! World -9223372036854775808,"
  920. "9223372036854775807--",60) == 0)
  921. sdsfree(x);
  922. x = sdsnew("--");
  923. x = sdscatfmt(x, "%u,%U--", UINT_MAX, ULLONG_MAX);
  924. test_cond("sdscatfmt() seems working with unsigned numbers",
  925. sdslen(x) == 35 &&
  926. memcmp(x,"--4294967295,18446744073709551615--",35) == 0)
  927. sdsfree(x);
  928. x = sdsnew("xxciaoyyy");
  929. sdstrim(x,"xy");
  930. test_cond("sdstrim() correctly trims characters",
  931. sdslen(x) == 4 && memcmp(x,"ciao\0",5) == 0)
  932. y = sdsdup(x);
  933. sdsrange(y,1,1);
  934. test_cond("sdsrange(...,1,1)",
  935. sdslen(y) == 1 && memcmp(y,"i\0",2) == 0)
  936. sdsfree(y);
  937. y = sdsdup(x);
  938. sdsrange(y,1,-1);
  939. test_cond("sdsrange(...,1,-1)",
  940. sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)
  941. sdsfree(y);
  942. y = sdsdup(x);
  943. sdsrange(y,-2,-1);
  944. test_cond("sdsrange(...,-2,-1)",
  945. sdslen(y) == 2 && memcmp(y,"ao\0",3) == 0)
  946. sdsfree(y);
  947. y = sdsdup(x);
  948. sdsrange(y,2,1);
  949. test_cond("sdsrange(...,2,1)",
  950. sdslen(y) == 0 && memcmp(y,"\0",1) == 0)
  951. sdsfree(y);
  952. y = sdsdup(x);
  953. sdsrange(y,1,100);
  954. test_cond("sdsrange(...,1,100)",
  955. sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)
  956. sdsfree(y);
  957. y = sdsdup(x);
  958. sdsrange(y,100,100);
  959. test_cond("sdsrange(...,100,100)",
  960. sdslen(y) == 0 && memcmp(y,"\0",1) == 0)
  961. sdsfree(y);
  962. sdsfree(x);
  963. x = sdsnew("foo");
  964. y = sdsnew("foa");
  965. test_cond("sdscmp(foo,foa)", sdscmp(x,y) > 0)
  966. sdsfree(y);
  967. sdsfree(x);
  968. x = sdsnew("bar");
  969. y = sdsnew("bar");
  970. test_cond("sdscmp(bar,bar)", sdscmp(x,y) == 0)
  971. sdsfree(y);
  972. sdsfree(x);
  973. x = sdsnew("aar");
  974. y = sdsnew("bar");
  975. test_cond("sdscmp(bar,bar)", sdscmp(x,y) < 0)
  976. sdsfree(y);
  977. sdsfree(x);
  978. x = sdsnewlen("\a\n\0foo\r",7);
  979. y = sdscatrepr(sdsempty(),x,sdslen(x));
  980. test_cond("sdscatrepr(...data...)",
  981. memcmp(y,"\"\\a\\n\\x00foo\\r\"",15) == 0)
  982. {
  983. int oldfree;
  984. sdsfree(x);
  985. x = sdsnew("0");
  986. sh = (void*) (x-(sizeof(struct sdshdr)));
  987. test_cond("sdsnew() free/len buffers", sh->len == 1 && sh->free == 0);
  988. x = sdsMakeRoomFor(x,1);
  989. sh = (void*) (x-(sizeof(struct sdshdr)));
  990. test_cond("sdsMakeRoomFor()", sh->len == 1 && sh->free > 0);
  991. oldfree = sh->free;
  992. x[1] = '1';
  993. sdsIncrLen(x,1);
  994. test_cond("sdsIncrLen() -- content", x[0] == '0' && x[1] == '1');
  995. test_cond("sdsIncrLen() -- len", sh->len == 2);
  996. test_cond("sdsIncrLen() -- free", sh->free == oldfree-1);
  997. }
  998. }
  999. test_report()
  1000. return 0;
  1001. }
  1002. #endif