2
0

sds.c 34 KB

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