2
0

sds.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. /* SDSLib 2.0 -- A C dynamic strings library
  2. *
  3. * Copyright (c) 2006-2015, Salvatore Sanfilippo <antirez at gmail dot com>
  4. * Copyright (c) 2015, Oran Agra
  5. * Copyright (c) 2015, Redis Labs, Inc
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * * Neither the name of Redis nor the names of its contributors may be used
  17. * to endorse or promote products derived from this software without
  18. * specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <ctype.h>
  36. #include <assert.h>
  37. #include "sds.h"
  38. #include "sdsalloc.h"
  39. static inline int sdsHdrSize(char type) {
  40. switch(type&SDS_TYPE_MASK) {
  41. case SDS_TYPE_5:
  42. return sizeof(struct sdshdr5);
  43. case SDS_TYPE_8:
  44. return sizeof(struct sdshdr8);
  45. case SDS_TYPE_16:
  46. return sizeof(struct sdshdr16);
  47. case SDS_TYPE_32:
  48. return sizeof(struct sdshdr32);
  49. case SDS_TYPE_64:
  50. return sizeof(struct sdshdr64);
  51. }
  52. return 0;
  53. }
  54. static inline char sdsReqType(size_t string_size) {
  55. if (string_size < 32)
  56. return SDS_TYPE_5;
  57. if (string_size < 0xff)
  58. return SDS_TYPE_8;
  59. if (string_size < 0xffff)
  60. return SDS_TYPE_16;
  61. if (string_size < 0xffffffff)
  62. return SDS_TYPE_32;
  63. return SDS_TYPE_64;
  64. }
  65. /* Create a new sds string with the content specified by the 'init' pointer
  66. * and 'initlen'.
  67. * If NULL is used for 'init' the string is initialized with zero bytes.
  68. *
  69. * The string is always null-termined (all the sds strings are, always) so
  70. * even if you create an sds string with:
  71. *
  72. * mystring = sdsnewlen("abc",3);
  73. *
  74. * You can print the string with printf() as there is an implicit \0 at the
  75. * end of the string. However the string is binary safe and can contain
  76. * \0 characters in the middle, as the length is stored in the sds header. */
  77. sds sdsnewlen(const void *init, size_t initlen) {
  78. void *sh;
  79. sds s;
  80. char type = sdsReqType(initlen);
  81. /* Empty strings are usually created in order to append. Use type 8
  82. * since type 5 is not good at this. */
  83. if (type == SDS_TYPE_5 && initlen == 0) type = SDS_TYPE_8;
  84. int hdrlen = sdsHdrSize(type);
  85. unsigned char *fp; /* flags pointer. */
  86. sh = s_malloc(hdrlen+initlen+1);
  87. if (sh == NULL) return NULL;
  88. if (!init)
  89. memset(sh, 0, hdrlen+initlen+1);
  90. s = (char*)sh+hdrlen;
  91. fp = ((unsigned char*)s)-1;
  92. switch(type) {
  93. case SDS_TYPE_5: {
  94. *fp = type | (initlen << SDS_TYPE_BITS);
  95. break;
  96. }
  97. case SDS_TYPE_8: {
  98. SDS_HDR_VAR(8,s);
  99. sh->len = initlen;
  100. sh->alloc = initlen;
  101. *fp = type;
  102. break;
  103. }
  104. case SDS_TYPE_16: {
  105. SDS_HDR_VAR(16,s);
  106. sh->len = initlen;
  107. sh->alloc = initlen;
  108. *fp = type;
  109. break;
  110. }
  111. case SDS_TYPE_32: {
  112. SDS_HDR_VAR(32,s);
  113. sh->len = initlen;
  114. sh->alloc = initlen;
  115. *fp = type;
  116. break;
  117. }
  118. case SDS_TYPE_64: {
  119. SDS_HDR_VAR(64,s);
  120. sh->len = initlen;
  121. sh->alloc = initlen;
  122. *fp = type;
  123. break;
  124. }
  125. }
  126. if (initlen && init)
  127. memcpy(s, init, initlen);
  128. s[initlen] = '\0';
  129. return s;
  130. }
  131. /* Create an empty (zero length) sds string. Even in this case the string
  132. * always has an implicit null term. */
  133. sds sdsempty(void) {
  134. return sdsnewlen("",0);
  135. }
  136. /* Create a new sds string starting from a null terminated C string. */
  137. sds sdsnew(const char *init) {
  138. size_t initlen = (init == NULL) ? 0 : strlen(init);
  139. return sdsnewlen(init, initlen);
  140. }
  141. /* Duplicate an sds string. */
  142. sds sdsdup(const sds s) {
  143. return sdsnewlen(s, sdslen(s));
  144. }
  145. /* Free an sds string. No operation is performed if 's' is NULL. */
  146. void sdsfree(sds s) {
  147. if (s == NULL) return;
  148. s_free((char*)s-sdsHdrSize(s[-1]));
  149. }
  150. /* Set the sds string length to the length as obtained with strlen(), so
  151. * considering as content only up to the first null term character.
  152. *
  153. * This function is useful when the sds string is hacked manually in some
  154. * way, like in the following example:
  155. *
  156. * s = sdsnew("foobar");
  157. * s[2] = '\0';
  158. * sdsupdatelen(s);
  159. * printf("%d\n", sdslen(s));
  160. *
  161. * The output will be "2", but if we comment out the call to sdsupdatelen()
  162. * the output will be "6" as the string was modified but the logical length
  163. * remains 6 bytes. */
  164. void sdsupdatelen(sds s) {
  165. int reallen = strlen(s);
  166. sdssetlen(s, reallen);
  167. }
  168. /* Modify an sds string in-place to make it empty (zero length).
  169. * However all the existing buffer is not discarded but set as free space
  170. * so that next append operations will not require allocations up to the
  171. * number of bytes previously available. */
  172. void sdsclear(sds s) {
  173. sdssetlen(s, 0);
  174. s[0] = '\0';
  175. }
  176. /* Enlarge the free space at the end of the sds string so that the caller
  177. * is sure that after calling this function can overwrite up to addlen
  178. * bytes after the end of the string, plus one more byte for nul term.
  179. *
  180. * Note: this does not change the *length* of the sds string as returned
  181. * by sdslen(), but only the free buffer space we have. */
  182. sds sdsMakeRoomFor(sds s, size_t addlen) {
  183. void *sh, *newsh;
  184. size_t avail = sdsavail(s);
  185. size_t len, newlen;
  186. char type, oldtype = s[-1] & SDS_TYPE_MASK;
  187. int hdrlen;
  188. /* Return ASAP if there is enough space left. */
  189. if (avail >= addlen) return s;
  190. len = sdslen(s);
  191. sh = (char*)s-sdsHdrSize(oldtype);
  192. newlen = (len+addlen);
  193. if (newlen < SDS_MAX_PREALLOC)
  194. newlen *= 2;
  195. else
  196. newlen += SDS_MAX_PREALLOC;
  197. type = sdsReqType(newlen);
  198. /* Don't use type 5: the user is appending to the string and type 5 is
  199. * not able to remember empty space, so sdsMakeRoomFor() must be called
  200. * at every appending operation. */
  201. if (type == SDS_TYPE_5) type = SDS_TYPE_8;
  202. hdrlen = sdsHdrSize(type);
  203. if (oldtype==type) {
  204. newsh = s_realloc(sh, hdrlen+newlen+1);
  205. if (newsh == NULL) return NULL;
  206. s = (char*)newsh+hdrlen;
  207. } else {
  208. /* Since the header size changes, need to move the string forward,
  209. * and can't use realloc */
  210. newsh = s_malloc(hdrlen+newlen+1);
  211. if (newsh == NULL) return NULL;
  212. memcpy((char*)newsh+hdrlen, s, len+1);
  213. s_free(sh);
  214. s = (char*)newsh+hdrlen;
  215. s[-1] = type;
  216. sdssetlen(s, len);
  217. }
  218. sdssetalloc(s, newlen);
  219. return s;
  220. }
  221. /* Reallocate the sds string so that it has no free space at the end. The
  222. * contained string remains not altered, but next concatenation operations
  223. * will require a reallocation.
  224. *
  225. * After the call, the passed sds string is no longer valid and all the
  226. * references must be substituted with the new pointer returned by the call. */
  227. sds sdsRemoveFreeSpace(sds s) {
  228. void *sh, *newsh;
  229. char type, oldtype = s[-1] & SDS_TYPE_MASK;
  230. int hdrlen;
  231. size_t len = sdslen(s);
  232. sh = (char*)s-sdsHdrSize(oldtype);
  233. type = sdsReqType(len);
  234. hdrlen = sdsHdrSize(type);
  235. if (oldtype==type) {
  236. newsh = s_realloc(sh, hdrlen+len+1);
  237. if (newsh == NULL) return NULL;
  238. s = (char*)newsh+hdrlen;
  239. } else {
  240. newsh = s_malloc(hdrlen+len+1);
  241. if (newsh == NULL) return NULL;
  242. memcpy((char*)newsh+hdrlen, s, len+1);
  243. s_free(sh);
  244. s = (char*)newsh+hdrlen;
  245. s[-1] = type;
  246. sdssetlen(s, len);
  247. }
  248. sdssetalloc(s, len);
  249. return s;
  250. }
  251. /* Return the total size of the allocation of the specifed sds string,
  252. * including:
  253. * 1) The sds header before the pointer.
  254. * 2) The string.
  255. * 3) The free buffer at the end if any.
  256. * 4) The implicit null term.
  257. */
  258. size_t sdsAllocSize(sds s) {
  259. size_t alloc = sdsalloc(s);
  260. return sdsHdrSize(s[-1])+alloc+1;
  261. }
  262. /* Return the pointer of the actual SDS allocation (normally SDS strings
  263. * are referenced by the start of the string buffer). */
  264. void *sdsAllocPtr(sds s) {
  265. return (void*) (s-sdsHdrSize(s[-1]));
  266. }
  267. /* Increment the sds length and decrements the left free space at the
  268. * end of the string according to 'incr'. Also set the null term
  269. * in the new end of the string.
  270. *
  271. * This function is used in order to fix the string length after the
  272. * user calls sdsMakeRoomFor(), writes something after the end of
  273. * the current string, and finally needs to set the new length.
  274. *
  275. * Note: it is possible to use a negative increment in order to
  276. * right-trim the string.
  277. *
  278. * Usage example:
  279. *
  280. * Using sdsIncrLen() and sdsMakeRoomFor() it is possible to mount the
  281. * following schema, to cat bytes coming from the kernel to the end of an
  282. * sds string without copying into an intermediate buffer:
  283. *
  284. * oldlen = sdslen(s);
  285. * s = sdsMakeRoomFor(s, BUFFER_SIZE);
  286. * nread = read(fd, s+oldlen, BUFFER_SIZE);
  287. * ... check for nread <= 0 and handle it ...
  288. * sdsIncrLen(s, nread);
  289. */
  290. void sdsIncrLen(sds s, int incr) {
  291. unsigned char flags = s[-1];
  292. size_t len;
  293. switch(flags&SDS_TYPE_MASK) {
  294. case SDS_TYPE_5: {
  295. unsigned char *fp = ((unsigned char*)s)-1;
  296. unsigned char oldlen = SDS_TYPE_5_LEN(flags);
  297. assert((incr > 0 && oldlen+incr < 32) || (incr < 0 && oldlen >= (unsigned int)(-incr)));
  298. *fp = SDS_TYPE_5 | ((oldlen+incr) << SDS_TYPE_BITS);
  299. len = oldlen+incr;
  300. break;
  301. }
  302. case SDS_TYPE_8: {
  303. SDS_HDR_VAR(8,s);
  304. assert((incr >= 0 && sh->alloc-sh->len >= incr) || (incr < 0 && sh->len >= (unsigned int)(-incr)));
  305. len = (sh->len += incr);
  306. break;
  307. }
  308. case SDS_TYPE_16: {
  309. SDS_HDR_VAR(16,s);
  310. assert((incr >= 0 && sh->alloc-sh->len >= incr) || (incr < 0 && sh->len >= (unsigned int)(-incr)));
  311. len = (sh->len += incr);
  312. break;
  313. }
  314. case SDS_TYPE_32: {
  315. SDS_HDR_VAR(32,s);
  316. assert((incr >= 0 && sh->alloc-sh->len >= (unsigned int)incr) || (incr < 0 && sh->len >= (unsigned int)(-incr)));
  317. len = (sh->len += incr);
  318. break;
  319. }
  320. case SDS_TYPE_64: {
  321. SDS_HDR_VAR(64,s);
  322. assert((incr >= 0 && sh->alloc-sh->len >= (uint64_t)incr) || (incr < 0 && sh->len >= (uint64_t)(-incr)));
  323. len = (sh->len += incr);
  324. break;
  325. }
  326. default: len = 0; /* Just to avoid compilation warnings. */
  327. }
  328. s[len] = '\0';
  329. }
  330. /* Grow the sds to have the specified length. Bytes that were not part of
  331. * the original length of the sds will be set to zero.
  332. *
  333. * if the specified length is smaller than the current length, no operation
  334. * is performed. */
  335. sds sdsgrowzero(sds s, size_t len) {
  336. size_t curlen = sdslen(s);
  337. if (len <= curlen) return s;
  338. s = sdsMakeRoomFor(s,len-curlen);
  339. if (s == NULL) return NULL;
  340. /* Make sure added region doesn't contain garbage */
  341. memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */
  342. sdssetlen(s, len);
  343. return s;
  344. }
  345. /* Append the specified binary-safe string pointed by 't' of 'len' bytes to the
  346. * end of the specified sds string 's'.
  347. *
  348. * After the call, the passed sds string is no longer valid and all the
  349. * references must be substituted with the new pointer returned by the call. */
  350. sds sdscatlen(sds s, const void *t, size_t len) {
  351. size_t curlen = sdslen(s);
  352. s = sdsMakeRoomFor(s,len);
  353. if (s == NULL) return NULL;
  354. memcpy(s+curlen, t, len);
  355. sdssetlen(s, curlen+len);
  356. s[curlen+len] = '\0';
  357. return s;
  358. }
  359. /* Append the specified null termianted C string to the sds string 's'.
  360. *
  361. * After the call, the passed sds string is no longer valid and all the
  362. * references must be substituted with the new pointer returned by the call. */
  363. sds sdscat(sds s, const char *t) {
  364. return sdscatlen(s, t, strlen(t));
  365. }
  366. /* Append the specified sds 't' to the existing sds 's'.
  367. *
  368. * After the call, the modified sds string is no longer valid and all the
  369. * references must be substituted with the new pointer returned by the call. */
  370. sds sdscatsds(sds s, const sds t) {
  371. return sdscatlen(s, t, sdslen(t));
  372. }
  373. /* Destructively modify the sds string 's' to hold the specified binary
  374. * safe string pointed by 't' of length 'len' bytes. */
  375. sds sdscpylen(sds s, const char *t, size_t len) {
  376. if (sdsalloc(s) < len) {
  377. s = sdsMakeRoomFor(s,len-sdslen(s));
  378. if (s == NULL) return NULL;
  379. }
  380. memcpy(s, t, len);
  381. s[len] = '\0';
  382. sdssetlen(s, len);
  383. return s;
  384. }
  385. /* Like sdscpylen() but 't' must be a null-termined string so that the length
  386. * of the string is obtained with strlen(). */
  387. sds sdscpy(sds s, const char *t) {
  388. return sdscpylen(s, t, strlen(t));
  389. }
  390. /* Helper for sdscatlonglong() doing the actual number -> string
  391. * conversion. 's' must point to a string with room for at least
  392. * SDS_LLSTR_SIZE bytes.
  393. *
  394. * The function returns the length of the null-terminated string
  395. * representation stored at 's'. */
  396. #define SDS_LLSTR_SIZE 21
  397. int sdsll2str(char *s, long long value) {
  398. char *p, aux;
  399. unsigned long long v;
  400. size_t l;
  401. /* Generate the string representation, this method produces
  402. * an reversed string. */
  403. v = (value < 0) ? -value : value;
  404. p = s;
  405. do {
  406. *p++ = '0'+(v%10);
  407. v /= 10;
  408. } while(v);
  409. if (value < 0) *p++ = '-';
  410. /* Compute length and add null term. */
  411. l = p-s;
  412. *p = '\0';
  413. /* Reverse the string. */
  414. p--;
  415. while(s < p) {
  416. aux = *s;
  417. *s = *p;
  418. *p = aux;
  419. s++;
  420. p--;
  421. }
  422. return l;
  423. }
  424. /* Identical sdsll2str(), but for unsigned long long type. */
  425. int sdsull2str(char *s, unsigned long long v) {
  426. char *p, aux;
  427. size_t l;
  428. /* Generate the string representation, this method produces
  429. * an reversed string. */
  430. p = s;
  431. do {
  432. *p++ = '0'+(v%10);
  433. v /= 10;
  434. } while(v);
  435. /* Compute length and add null term. */
  436. l = p-s;
  437. *p = '\0';
  438. /* Reverse the string. */
  439. p--;
  440. while(s < p) {
  441. aux = *s;
  442. *s = *p;
  443. *p = aux;
  444. s++;
  445. p--;
  446. }
  447. return l;
  448. }
  449. /* Create an sds string from a long long value. It is much faster than:
  450. *
  451. * sdscatprintf(sdsempty(),"%lld\n", value);
  452. */
  453. sds sdsfromlonglong(long long value) {
  454. char buf[SDS_LLSTR_SIZE];
  455. int len = sdsll2str(buf,value);
  456. return sdsnewlen(buf,len);
  457. }
  458. /* Like sdscatprintf() but gets va_list instead of being variadic. */
  459. sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
  460. va_list cpy;
  461. char staticbuf[1024], *buf = staticbuf, *t;
  462. size_t buflen = strlen(fmt)*2;
  463. /* We try to start using a static buffer for speed.
  464. * If not possible we revert to heap allocation. */
  465. if (buflen > sizeof(staticbuf)) {
  466. buf = s_malloc(buflen);
  467. if (buf == NULL) return NULL;
  468. } else {
  469. buflen = sizeof(staticbuf);
  470. }
  471. /* Try with buffers two times bigger every time we fail to
  472. * fit the string in the current buffer size. */
  473. while(1) {
  474. buf[buflen-2] = '\0';
  475. va_copy(cpy,ap);
  476. vsnprintf(buf, buflen, fmt, cpy);
  477. va_end(cpy);
  478. if (buf[buflen-2] != '\0') {
  479. if (buf != staticbuf) s_free(buf);
  480. buflen *= 2;
  481. buf = s_malloc(buflen);
  482. if (buf == NULL) return NULL;
  483. continue;
  484. }
  485. break;
  486. }
  487. /* Finally concat the obtained string to the SDS string and return it. */
  488. t = sdscat(s, buf);
  489. if (buf != staticbuf) s_free(buf);
  490. return t;
  491. }
  492. /* Append to the sds string 's' a string obtained using printf-alike format
  493. * specifier.
  494. *
  495. * After the call, the modified sds string is no longer valid and all the
  496. * references must be substituted with the new pointer returned by the call.
  497. *
  498. * Example:
  499. *
  500. * s = sdsnew("Sum is: ");
  501. * s = sdscatprintf(s,"%d+%d = %d",a,b,a+b).
  502. *
  503. * Often you need to create a string from scratch with the printf-alike
  504. * format. When this is the need, just use sdsempty() as the target string:
  505. *
  506. * s = sdscatprintf(sdsempty(), "... your format ...", args);
  507. */
  508. sds sdscatprintf(sds s, const char *fmt, ...) {
  509. va_list ap;
  510. char *t;
  511. va_start(ap, fmt);
  512. t = sdscatvprintf(s,fmt,ap);
  513. va_end(ap);
  514. return t;
  515. }
  516. /* This function is similar to sdscatprintf, but much faster as it does
  517. * not rely on sprintf() family functions implemented by the libc that
  518. * are often very slow. Moreover directly handling the sds string as
  519. * new data is concatenated provides a performance improvement.
  520. *
  521. * However this function only handles an incompatible subset of printf-alike
  522. * format specifiers:
  523. *
  524. * %s - C String
  525. * %S - SDS string
  526. * %i - signed int
  527. * %I - 64 bit signed integer (long long, int64_t)
  528. * %u - unsigned int
  529. * %U - 64 bit unsigned integer (unsigned long long, uint64_t)
  530. * %% - Verbatim "%" character.
  531. */
  532. sds sdscatfmt(sds s, char const *fmt, ...) {
  533. const char *f = fmt;
  534. int i;
  535. va_list ap;
  536. va_start(ap,fmt);
  537. i = sdslen(s); /* Position of the next byte to write to dest str. */
  538. while(*f) {
  539. char next, *str;
  540. size_t l;
  541. long long num;
  542. unsigned long long unum;
  543. /* Make sure there is always space for at least 1 char. */
  544. if (sdsavail(s)==0) {
  545. s = sdsMakeRoomFor(s,1);
  546. }
  547. switch(*f) {
  548. case '%':
  549. next = *(f+1);
  550. f++;
  551. switch(next) {
  552. case 's':
  553. case 'S':
  554. str = va_arg(ap,char*);
  555. l = (next == 's') ? strlen(str) : sdslen(str);
  556. if (sdsavail(s) < l) {
  557. s = sdsMakeRoomFor(s,l);
  558. }
  559. memcpy(s+i,str,l);
  560. sdsinclen(s,l);
  561. i += l;
  562. break;
  563. case 'i':
  564. case 'I':
  565. if (next == 'i')
  566. num = va_arg(ap,int);
  567. else
  568. num = va_arg(ap,long long);
  569. {
  570. char buf[SDS_LLSTR_SIZE];
  571. l = sdsll2str(buf,num);
  572. if (sdsavail(s) < l) {
  573. s = sdsMakeRoomFor(s,l);
  574. }
  575. memcpy(s+i,buf,l);
  576. sdsinclen(s,l);
  577. i += l;
  578. }
  579. break;
  580. case 'u':
  581. case 'U':
  582. if (next == 'u')
  583. unum = va_arg(ap,unsigned int);
  584. else
  585. unum = va_arg(ap,unsigned long long);
  586. {
  587. char buf[SDS_LLSTR_SIZE];
  588. l = sdsull2str(buf,unum);
  589. if (sdsavail(s) < l) {
  590. s = sdsMakeRoomFor(s,l);
  591. }
  592. memcpy(s+i,buf,l);
  593. sdsinclen(s,l);
  594. i += l;
  595. }
  596. break;
  597. default: /* Handle %% and generally %<unknown>. */
  598. s[i++] = next;
  599. sdsinclen(s,1);
  600. break;
  601. }
  602. break;
  603. default:
  604. s[i++] = *f;
  605. sdsinclen(s,1);
  606. break;
  607. }
  608. f++;
  609. }
  610. va_end(ap);
  611. /* Add null-term */
  612. s[i] = '\0';
  613. return s;
  614. }
  615. /* Remove the part of the string from left and from right composed just of
  616. * contiguous characters found in 'cset', that is a null terminted C string.
  617. *
  618. * After the call, the modified sds string is no longer valid and all the
  619. * references must be substituted with the new pointer returned by the call.
  620. *
  621. * Example:
  622. *
  623. * s = sdsnew("AA...AA.a.aa.aHelloWorld :::");
  624. * s = sdstrim(s,"Aa. :");
  625. * printf("%s\n", s);
  626. *
  627. * Output will be just "Hello World".
  628. */
  629. sds sdstrim(sds s, const char *cset) {
  630. char *start, *end, *sp, *ep;
  631. size_t len;
  632. sp = start = s;
  633. ep = end = s+sdslen(s)-1;
  634. while(sp <= end && strchr(cset, *sp)) sp++;
  635. while(ep > sp && strchr(cset, *ep)) ep--;
  636. len = (sp > ep) ? 0 : ((ep-sp)+1);
  637. if (s != sp) memmove(s, sp, len);
  638. s[len] = '\0';
  639. sdssetlen(s,len);
  640. return s;
  641. }
  642. /* Turn the string into a smaller (or equal) string containing only the
  643. * substring specified by the 'start' and 'end' indexes.
  644. *
  645. * start and end can be negative, where -1 means the last character of the
  646. * string, -2 the penultimate character, and so forth.
  647. *
  648. * The interval is inclusive, so the start and end characters will be part
  649. * of the resulting string.
  650. *
  651. * The string is modified in-place.
  652. *
  653. * Example:
  654. *
  655. * s = sdsnew("Hello World");
  656. * sdsrange(s,1,-1); => "ello World"
  657. */
  658. void sdsrange(sds s, int start, int end) {
  659. size_t newlen, len = sdslen(s);
  660. if (len == 0) return;
  661. if (start < 0) {
  662. start = len+start;
  663. if (start < 0) start = 0;
  664. }
  665. if (end < 0) {
  666. end = len+end;
  667. if (end < 0) end = 0;
  668. }
  669. newlen = (start > end) ? 0 : (end-start)+1;
  670. if (newlen != 0) {
  671. if (start >= (signed)len) {
  672. newlen = 0;
  673. } else if (end >= (signed)len) {
  674. end = len-1;
  675. newlen = (start > end) ? 0 : (end-start)+1;
  676. }
  677. } else {
  678. start = 0;
  679. }
  680. if (start && newlen) memmove(s, s+start, newlen);
  681. s[newlen] = 0;
  682. sdssetlen(s,newlen);
  683. }
  684. /* Apply tolower() to every character of the sds string 's'. */
  685. void sdstolower(sds s) {
  686. int len = sdslen(s), j;
  687. for (j = 0; j < len; j++) s[j] = tolower(s[j]);
  688. }
  689. /* Apply toupper() to every character of the sds string 's'. */
  690. void sdstoupper(sds s) {
  691. int len = sdslen(s), j;
  692. for (j = 0; j < len; j++) s[j] = toupper(s[j]);
  693. }
  694. /* Compare two sds strings s1 and s2 with memcmp().
  695. *
  696. * Return value:
  697. *
  698. * positive if s1 > s2.
  699. * negative if s1 < s2.
  700. * 0 if s1 and s2 are exactly the same binary string.
  701. *
  702. * If two strings share exactly the same prefix, but one of the two has
  703. * additional characters, the longer string is considered to be greater than
  704. * the smaller one. */
  705. int sdscmp(const sds s1, const sds s2) {
  706. size_t l1, l2, minlen;
  707. int cmp;
  708. l1 = sdslen(s1);
  709. l2 = sdslen(s2);
  710. minlen = (l1 < l2) ? l1 : l2;
  711. cmp = memcmp(s1,s2,minlen);
  712. if (cmp == 0) return l1-l2;
  713. return cmp;
  714. }
  715. /* Split 's' with separator in 'sep'. An array
  716. * of sds strings is returned. *count will be set
  717. * by reference to the number of tokens returned.
  718. *
  719. * On out of memory, zero length string, zero length
  720. * separator, NULL is returned.
  721. *
  722. * Note that 'sep' is able to split a string using
  723. * a multi-character separator. For example
  724. * sdssplit("foo_-_bar","_-_"); will return two
  725. * elements "foo" and "bar".
  726. *
  727. * This version of the function is binary-safe but
  728. * requires length arguments. sdssplit() is just the
  729. * same function but for zero-terminated strings.
  730. */
  731. sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count) {
  732. int elements = 0, slots = 5, start = 0, j;
  733. sds *tokens;
  734. if (seplen < 1 || len < 0) return NULL;
  735. tokens = s_malloc(sizeof(sds)*slots);
  736. if (tokens == NULL) return NULL;
  737. if (len == 0) {
  738. *count = 0;
  739. return tokens;
  740. }
  741. for (j = 0; j < (len-(seplen-1)); j++) {
  742. /* make sure there is room for the next element and the final one */
  743. if (slots < elements+2) {
  744. sds *newtokens;
  745. slots *= 2;
  746. newtokens = s_realloc(tokens,sizeof(sds)*slots);
  747. if (newtokens == NULL) goto cleanup;
  748. tokens = newtokens;
  749. }
  750. /* search the separator */
  751. if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) {
  752. tokens[elements] = sdsnewlen(s+start,j-start);
  753. if (tokens[elements] == NULL) goto cleanup;
  754. elements++;
  755. start = j+seplen;
  756. j = j+seplen-1; /* skip the separator */
  757. }
  758. }
  759. /* Add the final element. We are sure there is room in the tokens array. */
  760. tokens[elements] = sdsnewlen(s+start,len-start);
  761. if (tokens[elements] == NULL) goto cleanup;
  762. elements++;
  763. *count = elements;
  764. return tokens;
  765. cleanup:
  766. {
  767. int i;
  768. for (i = 0; i < elements; i++) sdsfree(tokens[i]);
  769. s_free(tokens);
  770. *count = 0;
  771. return NULL;
  772. }
  773. }
  774. /* Free the result returned by sdssplitlen(), or do nothing if 'tokens' is NULL. */
  775. void sdsfreesplitres(sds *tokens, int count) {
  776. if (!tokens) return;
  777. while(count--)
  778. sdsfree(tokens[count]);
  779. s_free(tokens);
  780. }
  781. /* Append to the sds string "s" an escaped string representation where
  782. * all the non-printable characters (tested with isprint()) are turned into
  783. * escapes in the form "\n\r\a...." or "\x<hex-number>".
  784. *
  785. * After the call, the modified sds string is no longer valid and all the
  786. * references must be substituted with the new pointer returned by the call. */
  787. sds sdscatrepr(sds s, const char *p, size_t len) {
  788. s = sdscatlen(s,"\"",1);
  789. while(len--) {
  790. switch(*p) {
  791. case '\\':
  792. case '"':
  793. s = sdscatprintf(s,"\\%c",*p);
  794. break;
  795. case '\n': s = sdscatlen(s,"\\n",2); break;
  796. case '\r': s = sdscatlen(s,"\\r",2); break;
  797. case '\t': s = sdscatlen(s,"\\t",2); break;
  798. case '\a': s = sdscatlen(s,"\\a",2); break;
  799. case '\b': s = sdscatlen(s,"\\b",2); break;
  800. default:
  801. if (isprint(*p))
  802. s = sdscatprintf(s,"%c",*p);
  803. else
  804. s = sdscatprintf(s,"\\x%02x",(unsigned char)*p);
  805. break;
  806. }
  807. p++;
  808. }
  809. return sdscatlen(s,"\"",1);
  810. }
  811. /* Helper function for sdssplitargs() that returns non zero if 'c'
  812. * is a valid hex digit. */
  813. int is_hex_digit(char c) {
  814. return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
  815. (c >= 'A' && c <= 'F');
  816. }
  817. /* Helper function for sdssplitargs() that converts a hex digit into an
  818. * integer from 0 to 15 */
  819. int hex_digit_to_int(char c) {
  820. switch(c) {
  821. case '0': return 0;
  822. case '1': return 1;
  823. case '2': return 2;
  824. case '3': return 3;
  825. case '4': return 4;
  826. case '5': return 5;
  827. case '6': return 6;
  828. case '7': return 7;
  829. case '8': return 8;
  830. case '9': return 9;
  831. case 'a': case 'A': return 10;
  832. case 'b': case 'B': return 11;
  833. case 'c': case 'C': return 12;
  834. case 'd': case 'D': return 13;
  835. case 'e': case 'E': return 14;
  836. case 'f': case 'F': return 15;
  837. default: return 0;
  838. }
  839. }
  840. /* Split a line into arguments, where every argument can be in the
  841. * following programming-language REPL-alike form:
  842. *
  843. * foo bar "newline are supported\n" and "\xff\x00otherstuff"
  844. *
  845. * The number of arguments is stored into *argc, and an array
  846. * of sds is returned.
  847. *
  848. * The caller should free the resulting array of sds strings with
  849. * sdsfreesplitres().
  850. *
  851. * Note that sdscatrepr() is able to convert back a string into
  852. * a quoted string in the same format sdssplitargs() is able to parse.
  853. *
  854. * The function returns the allocated tokens on success, even when the
  855. * input string is empty, or NULL if the input contains unbalanced
  856. * quotes or closed quotes followed by non space characters
  857. * as in: "foo"bar or "foo'
  858. */
  859. sds *sdssplitargs(const char *line, int *argc) {
  860. const char *p = line;
  861. char *current = NULL;
  862. char **vector = NULL;
  863. *argc = 0;
  864. while(1) {
  865. /* skip blanks */
  866. while(*p && isspace(*p)) p++;
  867. if (*p) {
  868. /* get a token */
  869. int inq=0; /* set to 1 if we are in "quotes" */
  870. int insq=0; /* set to 1 if we are in 'single quotes' */
  871. int done=0;
  872. if (current == NULL) current = sdsempty();
  873. while(!done) {
  874. if (inq) {
  875. if (*p == '\\' && *(p+1) == 'x' &&
  876. is_hex_digit(*(p+2)) &&
  877. is_hex_digit(*(p+3)))
  878. {
  879. unsigned char byte;
  880. byte = (hex_digit_to_int(*(p+2))*16)+
  881. hex_digit_to_int(*(p+3));
  882. current = sdscatlen(current,(char*)&byte,1);
  883. p += 3;
  884. } else if (*p == '\\' && *(p+1)) {
  885. char c;
  886. p++;
  887. switch(*p) {
  888. case 'n': c = '\n'; break;
  889. case 'r': c = '\r'; break;
  890. case 't': c = '\t'; break;
  891. case 'b': c = '\b'; break;
  892. case 'a': c = '\a'; break;
  893. default: c = *p; break;
  894. }
  895. current = sdscatlen(current,&c,1);
  896. } else if (*p == '"') {
  897. /* closing quote must be followed by a space or
  898. * nothing at all. */
  899. if (*(p+1) && !isspace(*(p+1))) goto err;
  900. done=1;
  901. } else if (!*p) {
  902. /* unterminated quotes */
  903. goto err;
  904. } else {
  905. current = sdscatlen(current,p,1);
  906. }
  907. } else if (insq) {
  908. if (*p == '\\' && *(p+1) == '\'') {
  909. p++;
  910. current = sdscatlen(current,"'",1);
  911. } else if (*p == '\'') {
  912. /* closing quote must be followed by a space or
  913. * nothing at all. */
  914. if (*(p+1) && !isspace(*(p+1))) goto err;
  915. done=1;
  916. } else if (!*p) {
  917. /* unterminated quotes */
  918. goto err;
  919. } else {
  920. current = sdscatlen(current,p,1);
  921. }
  922. } else {
  923. switch(*p) {
  924. case ' ':
  925. case '\n':
  926. case '\r':
  927. case '\t':
  928. case '\0':
  929. done=1;
  930. break;
  931. case '"':
  932. inq=1;
  933. break;
  934. case '\'':
  935. insq=1;
  936. break;
  937. default:
  938. current = sdscatlen(current,p,1);
  939. break;
  940. }
  941. }
  942. if (*p) p++;
  943. }
  944. /* add the token to the vector */
  945. vector = s_realloc(vector,((*argc)+1)*sizeof(char*));
  946. vector[*argc] = current;
  947. (*argc)++;
  948. current = NULL;
  949. } else {
  950. /* Even on empty input string return something not NULL. */
  951. if (vector == NULL) vector = s_malloc(sizeof(void*));
  952. return vector;
  953. }
  954. }
  955. err:
  956. while((*argc)--)
  957. sdsfree(vector[*argc]);
  958. s_free(vector);
  959. if (current) sdsfree(current);
  960. *argc = 0;
  961. return NULL;
  962. }
  963. /* Modify the string substituting all the occurrences of the set of
  964. * characters specified in the 'from' string to the corresponding character
  965. * in the 'to' array.
  966. *
  967. * For instance: sdsmapchars(mystring, "ho", "01", 2)
  968. * will have the effect of turning the string "hello" into "0ell1".
  969. *
  970. * The function returns the sds string pointer, that is always the same
  971. * as the input pointer since no resize is needed. */
  972. sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) {
  973. size_t j, i, l = sdslen(s);
  974. for (j = 0; j < l; j++) {
  975. for (i = 0; i < setlen; i++) {
  976. if (s[j] == from[i]) {
  977. s[j] = to[i];
  978. break;
  979. }
  980. }
  981. }
  982. return s;
  983. }
  984. /* Join an array of C strings using the specified separator (also a C string).
  985. * Returns the result as an sds string. */
  986. sds sdsjoin(char **argv, int argc, char *sep) {
  987. sds join = sdsempty();
  988. int j;
  989. for (j = 0; j < argc; j++) {
  990. join = sdscat(join, argv[j]);
  991. if (j != argc-1) join = sdscat(join,sep);
  992. }
  993. return join;
  994. }
  995. /* Like sdsjoin, but joins an array of SDS strings. */
  996. sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen) {
  997. sds join = sdsempty();
  998. int j;
  999. for (j = 0; j < argc; j++) {
  1000. join = sdscatsds(join, argv[j]);
  1001. if (j != argc-1) join = sdscatlen(join,sep,seplen);
  1002. }
  1003. return join;
  1004. }
  1005. /* Wrappers to the allocators used by SDS. Note that SDS will actually
  1006. * just use the macros defined into sdsalloc.h in order to avoid to pay
  1007. * the overhead of function calls. Here we define these wrappers only for
  1008. * the programs SDS is linked to, if they want to touch the SDS internals
  1009. * even if they use a different allocator. */
  1010. void *sds_malloc(size_t size) { return s_malloc(size); }
  1011. void *sds_realloc(void *ptr, size_t size) { return s_realloc(ptr,size); }
  1012. void sds_free(void *ptr) { s_free(ptr); }
  1013. #if defined(SDS_TEST_MAIN)
  1014. #include <stdio.h>
  1015. #include "testhelp.h"
  1016. #include "limits.h"
  1017. #define UNUSED(x) (void)(x)
  1018. int sdsTest(void) {
  1019. {
  1020. sds x = sdsnew("foo"), y;
  1021. test_cond("Create a string and obtain the length",
  1022. sdslen(x) == 3 && memcmp(x,"foo\0",4) == 0)
  1023. sdsfree(x);
  1024. x = sdsnewlen("foo",2);
  1025. test_cond("Create a string with specified length",
  1026. sdslen(x) == 2 && memcmp(x,"fo\0",3) == 0)
  1027. x = sdscat(x,"bar");
  1028. test_cond("Strings concatenation",
  1029. sdslen(x) == 5 && memcmp(x,"fobar\0",6) == 0);
  1030. x = sdscpy(x,"a");
  1031. test_cond("sdscpy() against an originally longer string",
  1032. sdslen(x) == 1 && memcmp(x,"a\0",2) == 0)
  1033. x = sdscpy(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk");
  1034. test_cond("sdscpy() against an originally shorter string",
  1035. sdslen(x) == 33 &&
  1036. memcmp(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0)
  1037. sdsfree(x);
  1038. x = sdscatprintf(sdsempty(),"%d",123);
  1039. test_cond("sdscatprintf() seems working in the base case",
  1040. sdslen(x) == 3 && memcmp(x,"123\0",4) == 0)
  1041. sdsfree(x);
  1042. x = sdsnew("--");
  1043. x = sdscatfmt(x, "Hello %s World %I,%I--", "Hi!", LLONG_MIN,LLONG_MAX);
  1044. test_cond("sdscatfmt() seems working in the base case",
  1045. sdslen(x) == 60 &&
  1046. memcmp(x,"--Hello Hi! World -9223372036854775808,"
  1047. "9223372036854775807--",60) == 0)
  1048. printf("[%s]\n",x);
  1049. sdsfree(x);
  1050. x = sdsnew("--");
  1051. x = sdscatfmt(x, "%u,%U--", UINT_MAX, ULLONG_MAX);
  1052. test_cond("sdscatfmt() seems working with unsigned numbers",
  1053. sdslen(x) == 35 &&
  1054. memcmp(x,"--4294967295,18446744073709551615--",35) == 0)
  1055. sdsfree(x);
  1056. x = sdsnew(" x ");
  1057. sdstrim(x," x");
  1058. test_cond("sdstrim() works when all chars match",
  1059. sdslen(x) == 0)
  1060. sdsfree(x);
  1061. x = sdsnew(" x ");
  1062. sdstrim(x," ");
  1063. test_cond("sdstrim() works when a single char remains",
  1064. sdslen(x) == 1 && x[0] == 'x')
  1065. sdsfree(x);
  1066. x = sdsnew("xxciaoyyy");
  1067. sdstrim(x,"xy");
  1068. test_cond("sdstrim() correctly trims characters",
  1069. sdslen(x) == 4 && memcmp(x,"ciao\0",5) == 0)
  1070. y = sdsdup(x);
  1071. sdsrange(y,1,1);
  1072. test_cond("sdsrange(...,1,1)",
  1073. sdslen(y) == 1 && memcmp(y,"i\0",2) == 0)
  1074. sdsfree(y);
  1075. y = sdsdup(x);
  1076. sdsrange(y,1,-1);
  1077. test_cond("sdsrange(...,1,-1)",
  1078. sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)
  1079. sdsfree(y);
  1080. y = sdsdup(x);
  1081. sdsrange(y,-2,-1);
  1082. test_cond("sdsrange(...,-2,-1)",
  1083. sdslen(y) == 2 && memcmp(y,"ao\0",3) == 0)
  1084. sdsfree(y);
  1085. y = sdsdup(x);
  1086. sdsrange(y,2,1);
  1087. test_cond("sdsrange(...,2,1)",
  1088. sdslen(y) == 0 && memcmp(y,"\0",1) == 0)
  1089. sdsfree(y);
  1090. y = sdsdup(x);
  1091. sdsrange(y,1,100);
  1092. test_cond("sdsrange(...,1,100)",
  1093. sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)
  1094. sdsfree(y);
  1095. y = sdsdup(x);
  1096. sdsrange(y,100,100);
  1097. test_cond("sdsrange(...,100,100)",
  1098. sdslen(y) == 0 && memcmp(y,"\0",1) == 0)
  1099. sdsfree(y);
  1100. sdsfree(x);
  1101. x = sdsnew("foo");
  1102. y = sdsnew("foa");
  1103. test_cond("sdscmp(foo,foa)", sdscmp(x,y) > 0)
  1104. sdsfree(y);
  1105. sdsfree(x);
  1106. x = sdsnew("bar");
  1107. y = sdsnew("bar");
  1108. test_cond("sdscmp(bar,bar)", sdscmp(x,y) == 0)
  1109. sdsfree(y);
  1110. sdsfree(x);
  1111. x = sdsnew("aar");
  1112. y = sdsnew("bar");
  1113. test_cond("sdscmp(bar,bar)", sdscmp(x,y) < 0)
  1114. sdsfree(y);
  1115. sdsfree(x);
  1116. x = sdsnewlen("\a\n\0foo\r",7);
  1117. y = sdscatrepr(sdsempty(),x,sdslen(x));
  1118. test_cond("sdscatrepr(...data...)",
  1119. memcmp(y,"\"\\a\\n\\x00foo\\r\"",15) == 0)
  1120. {
  1121. unsigned int oldfree;
  1122. char *p;
  1123. int step = 10, j, i;
  1124. sdsfree(x);
  1125. sdsfree(y);
  1126. x = sdsnew("0");
  1127. test_cond("sdsnew() free/len buffers", sdslen(x) == 1 && sdsavail(x) == 0);
  1128. /* Run the test a few times in order to hit the first two
  1129. * SDS header types. */
  1130. for (i = 0; i < 10; i++) {
  1131. int oldlen = sdslen(x);
  1132. x = sdsMakeRoomFor(x,step);
  1133. int type = x[-1]&SDS_TYPE_MASK;
  1134. test_cond("sdsMakeRoomFor() len", sdslen(x) == oldlen);
  1135. if (type != SDS_TYPE_5) {
  1136. test_cond("sdsMakeRoomFor() free", sdsavail(x) >= step);
  1137. oldfree = sdsavail(x);
  1138. }
  1139. p = x+oldlen;
  1140. for (j = 0; j < step; j++) {
  1141. p[j] = 'A'+j;
  1142. }
  1143. sdsIncrLen(x,step);
  1144. }
  1145. test_cond("sdsMakeRoomFor() content",
  1146. memcmp("0ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ",x,101) == 0);
  1147. test_cond("sdsMakeRoomFor() final length",sdslen(x)==101);
  1148. sdsfree(x);
  1149. }
  1150. }
  1151. test_report()
  1152. return 0;
  1153. }
  1154. #endif
  1155. #ifdef SDS_TEST_MAIN
  1156. int main(void) {
  1157. return sdsTest();
  1158. }
  1159. #endif