sds.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  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 (!init)
  88. memset(sh, 0, hdrlen+initlen+1);
  89. if (sh == NULL) return NULL;
  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. size_t initlen = sdslen(s);
  534. const char *f = fmt;
  535. int i;
  536. va_list ap;
  537. va_start(ap,fmt);
  538. f = fmt; /* Next format specifier byte to process. */
  539. i = initlen; /* Position of the next byte to write to dest str. */
  540. while(*f) {
  541. char next, *str;
  542. size_t l;
  543. long long num;
  544. unsigned long long unum;
  545. /* Make sure there is always space for at least 1 char. */
  546. if (sdsavail(s)==0) {
  547. s = sdsMakeRoomFor(s,1);
  548. }
  549. switch(*f) {
  550. case '%':
  551. next = *(f+1);
  552. f++;
  553. switch(next) {
  554. case 's':
  555. case 'S':
  556. str = va_arg(ap,char*);
  557. l = (next == 's') ? strlen(str) : sdslen(str);
  558. if (sdsavail(s) < l) {
  559. s = sdsMakeRoomFor(s,l);
  560. }
  561. memcpy(s+i,str,l);
  562. sdsinclen(s,l);
  563. i += l;
  564. break;
  565. case 'i':
  566. case 'I':
  567. if (next == 'i')
  568. num = va_arg(ap,int);
  569. else
  570. num = va_arg(ap,long long);
  571. {
  572. char buf[SDS_LLSTR_SIZE];
  573. l = sdsll2str(buf,num);
  574. if (sdsavail(s) < l) {
  575. s = sdsMakeRoomFor(s,l);
  576. }
  577. memcpy(s+i,buf,l);
  578. sdsinclen(s,l);
  579. i += l;
  580. }
  581. break;
  582. case 'u':
  583. case 'U':
  584. if (next == 'u')
  585. unum = va_arg(ap,unsigned int);
  586. else
  587. unum = va_arg(ap,unsigned long long);
  588. {
  589. char buf[SDS_LLSTR_SIZE];
  590. l = sdsull2str(buf,unum);
  591. if (sdsavail(s) < l) {
  592. s = sdsMakeRoomFor(s,l);
  593. }
  594. memcpy(s+i,buf,l);
  595. sdsinclen(s,l);
  596. i += l;
  597. }
  598. break;
  599. default: /* Handle %% and generally %<unknown>. */
  600. s[i++] = next;
  601. sdsinclen(s,1);
  602. break;
  603. }
  604. break;
  605. default:
  606. s[i++] = *f;
  607. sdsinclen(s,1);
  608. break;
  609. }
  610. f++;
  611. }
  612. va_end(ap);
  613. /* Add null-term */
  614. s[i] = '\0';
  615. return s;
  616. }
  617. /* Remove the part of the string from left and from right composed just of
  618. * contiguous characters found in 'cset', that is a null terminted C string.
  619. *
  620. * After the call, the modified sds string is no longer valid and all the
  621. * references must be substituted with the new pointer returned by the call.
  622. *
  623. * Example:
  624. *
  625. * s = sdsnew("AA...AA.a.aa.aHelloWorld :::");
  626. * s = sdstrim(s,"Aa. :");
  627. * printf("%s\n", s);
  628. *
  629. * Output will be just "Hello World".
  630. */
  631. sds sdstrim(sds s, const char *cset) {
  632. char *start, *end, *sp, *ep;
  633. size_t len;
  634. sp = start = s;
  635. ep = end = s+sdslen(s)-1;
  636. while(sp <= end && strchr(cset, *sp)) sp++;
  637. while(ep > sp && strchr(cset, *ep)) ep--;
  638. len = (sp > ep) ? 0 : ((ep-sp)+1);
  639. if (s != sp) memmove(s, sp, len);
  640. s[len] = '\0';
  641. sdssetlen(s,len);
  642. return s;
  643. }
  644. /* Turn the string into a smaller (or equal) string containing only the
  645. * substring specified by the 'start' and 'end' indexes.
  646. *
  647. * start and end can be negative, where -1 means the last character of the
  648. * string, -2 the penultimate character, and so forth.
  649. *
  650. * The interval is inclusive, so the start and end characters will be part
  651. * of the resulting string.
  652. *
  653. * The string is modified in-place.
  654. *
  655. * Example:
  656. *
  657. * s = sdsnew("Hello World");
  658. * sdsrange(s,1,-1); => "ello World"
  659. */
  660. void sdsrange(sds s, int start, int end) {
  661. size_t newlen, len = sdslen(s);
  662. if (len == 0) return;
  663. if (start < 0) {
  664. start = len+start;
  665. if (start < 0) start = 0;
  666. }
  667. if (end < 0) {
  668. end = len+end;
  669. if (end < 0) end = 0;
  670. }
  671. newlen = (start > end) ? 0 : (end-start)+1;
  672. if (newlen != 0) {
  673. if (start >= (signed)len) {
  674. newlen = 0;
  675. } else if (end >= (signed)len) {
  676. end = len-1;
  677. newlen = (start > end) ? 0 : (end-start)+1;
  678. }
  679. } else {
  680. start = 0;
  681. }
  682. if (start && newlen) memmove(s, s+start, newlen);
  683. s[newlen] = 0;
  684. sdssetlen(s,newlen);
  685. }
  686. /* Apply tolower() to every character of the sds string 's'. */
  687. void sdstolower(sds s) {
  688. int len = sdslen(s), j;
  689. for (j = 0; j < len; j++) s[j] = tolower(s[j]);
  690. }
  691. /* Apply toupper() to every character of the sds string 's'. */
  692. void sdstoupper(sds s) {
  693. int len = sdslen(s), j;
  694. for (j = 0; j < len; j++) s[j] = toupper(s[j]);
  695. }
  696. /* Compare two sds strings s1 and s2 with memcmp().
  697. *
  698. * Return value:
  699. *
  700. * positive if s1 > s2.
  701. * negative if s1 < s2.
  702. * 0 if s1 and s2 are exactly the same binary string.
  703. *
  704. * If two strings share exactly the same prefix, but one of the two has
  705. * additional characters, the longer string is considered to be greater than
  706. * the smaller one. */
  707. int sdscmp(const sds s1, const sds s2) {
  708. size_t l1, l2, minlen;
  709. int cmp;
  710. l1 = sdslen(s1);
  711. l2 = sdslen(s2);
  712. minlen = (l1 < l2) ? l1 : l2;
  713. cmp = memcmp(s1,s2,minlen);
  714. if (cmp == 0) return l1-l2;
  715. return cmp;
  716. }
  717. /* Split 's' with separator in 'sep'. An array
  718. * of sds strings is returned. *count will be set
  719. * by reference to the number of tokens returned.
  720. *
  721. * On out of memory, zero length string, zero length
  722. * separator, NULL is returned.
  723. *
  724. * Note that 'sep' is able to split a string using
  725. * a multi-character separator. For example
  726. * sdssplit("foo_-_bar","_-_"); will return two
  727. * elements "foo" and "bar".
  728. *
  729. * This version of the function is binary-safe but
  730. * requires length arguments. sdssplit() is just the
  731. * same function but for zero-terminated strings.
  732. */
  733. sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count) {
  734. int elements = 0, slots = 5, start = 0, j;
  735. sds *tokens;
  736. if (seplen < 1 || len < 0) return NULL;
  737. tokens = s_malloc(sizeof(sds)*slots);
  738. if (tokens == NULL) return NULL;
  739. if (len == 0) {
  740. *count = 0;
  741. return tokens;
  742. }
  743. for (j = 0; j < (len-(seplen-1)); j++) {
  744. /* make sure there is room for the next element and the final one */
  745. if (slots < elements+2) {
  746. sds *newtokens;
  747. slots *= 2;
  748. newtokens = s_realloc(tokens,sizeof(sds)*slots);
  749. if (newtokens == NULL) goto cleanup;
  750. tokens = newtokens;
  751. }
  752. /* search the separator */
  753. if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) {
  754. tokens[elements] = sdsnewlen(s+start,j-start);
  755. if (tokens[elements] == NULL) goto cleanup;
  756. elements++;
  757. start = j+seplen;
  758. j = j+seplen-1; /* skip the separator */
  759. }
  760. }
  761. /* Add the final element. We are sure there is room in the tokens array. */
  762. tokens[elements] = sdsnewlen(s+start,len-start);
  763. if (tokens[elements] == NULL) goto cleanup;
  764. elements++;
  765. *count = elements;
  766. return tokens;
  767. cleanup:
  768. {
  769. int i;
  770. for (i = 0; i < elements; i++) sdsfree(tokens[i]);
  771. s_free(tokens);
  772. *count = 0;
  773. return NULL;
  774. }
  775. }
  776. /* Free the result returned by sdssplitlen(), or do nothing if 'tokens' is NULL. */
  777. void sdsfreesplitres(sds *tokens, int count) {
  778. if (!tokens) return;
  779. while(count--)
  780. sdsfree(tokens[count]);
  781. s_free(tokens);
  782. }
  783. /* Append to the sds string "s" an escaped string representation where
  784. * all the non-printable characters (tested with isprint()) are turned into
  785. * escapes in the form "\n\r\a...." or "\x<hex-number>".
  786. *
  787. * After the call, the modified sds string is no longer valid and all the
  788. * references must be substituted with the new pointer returned by the call. */
  789. sds sdscatrepr(sds s, const char *p, size_t len) {
  790. s = sdscatlen(s,"\"",1);
  791. while(len--) {
  792. switch(*p) {
  793. case '\\':
  794. case '"':
  795. s = sdscatprintf(s,"\\%c",*p);
  796. break;
  797. case '\n': s = sdscatlen(s,"\\n",2); break;
  798. case '\r': s = sdscatlen(s,"\\r",2); break;
  799. case '\t': s = sdscatlen(s,"\\t",2); break;
  800. case '\a': s = sdscatlen(s,"\\a",2); break;
  801. case '\b': s = sdscatlen(s,"\\b",2); break;
  802. default:
  803. if (isprint(*p))
  804. s = sdscatprintf(s,"%c",*p);
  805. else
  806. s = sdscatprintf(s,"\\x%02x",(unsigned char)*p);
  807. break;
  808. }
  809. p++;
  810. }
  811. return sdscatlen(s,"\"",1);
  812. }
  813. /* Helper function for sdssplitargs() that returns non zero if 'c'
  814. * is a valid hex digit. */
  815. int is_hex_digit(char c) {
  816. return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
  817. (c >= 'A' && c <= 'F');
  818. }
  819. /* Helper function for sdssplitargs() that converts a hex digit into an
  820. * integer from 0 to 15 */
  821. int hex_digit_to_int(char c) {
  822. switch(c) {
  823. case '0': return 0;
  824. case '1': return 1;
  825. case '2': return 2;
  826. case '3': return 3;
  827. case '4': return 4;
  828. case '5': return 5;
  829. case '6': return 6;
  830. case '7': return 7;
  831. case '8': return 8;
  832. case '9': return 9;
  833. case 'a': case 'A': return 10;
  834. case 'b': case 'B': return 11;
  835. case 'c': case 'C': return 12;
  836. case 'd': case 'D': return 13;
  837. case 'e': case 'E': return 14;
  838. case 'f': case 'F': return 15;
  839. default: return 0;
  840. }
  841. }
  842. /* Split a line into arguments, where every argument can be in the
  843. * following programming-language REPL-alike form:
  844. *
  845. * foo bar "newline are supported\n" and "\xff\x00otherstuff"
  846. *
  847. * The number of arguments is stored into *argc, and an array
  848. * of sds is returned.
  849. *
  850. * The caller should free the resulting array of sds strings with
  851. * sdsfreesplitres().
  852. *
  853. * Note that sdscatrepr() is able to convert back a string into
  854. * a quoted string in the same format sdssplitargs() is able to parse.
  855. *
  856. * The function returns the allocated tokens on success, even when the
  857. * input string is empty, or NULL if the input contains unbalanced
  858. * quotes or closed quotes followed by non space characters
  859. * as in: "foo"bar or "foo'
  860. */
  861. sds *sdssplitargs(const char *line, int *argc) {
  862. const char *p = line;
  863. char *current = NULL;
  864. char **vector = NULL;
  865. *argc = 0;
  866. while(1) {
  867. /* skip blanks */
  868. while(*p && isspace(*p)) p++;
  869. if (*p) {
  870. /* get a token */
  871. int inq=0; /* set to 1 if we are in "quotes" */
  872. int insq=0; /* set to 1 if we are in 'single quotes' */
  873. int done=0;
  874. if (current == NULL) current = sdsempty();
  875. while(!done) {
  876. if (inq) {
  877. if (*p == '\\' && *(p+1) == 'x' &&
  878. is_hex_digit(*(p+2)) &&
  879. is_hex_digit(*(p+3)))
  880. {
  881. unsigned char byte;
  882. byte = (hex_digit_to_int(*(p+2))*16)+
  883. hex_digit_to_int(*(p+3));
  884. current = sdscatlen(current,(char*)&byte,1);
  885. p += 3;
  886. } else if (*p == '\\' && *(p+1)) {
  887. char c;
  888. p++;
  889. switch(*p) {
  890. case 'n': c = '\n'; break;
  891. case 'r': c = '\r'; break;
  892. case 't': c = '\t'; break;
  893. case 'b': c = '\b'; break;
  894. case 'a': c = '\a'; break;
  895. default: c = *p; break;
  896. }
  897. current = sdscatlen(current,&c,1);
  898. } else if (*p == '"') {
  899. /* closing quote must be followed by a space or
  900. * nothing at all. */
  901. if (*(p+1) && !isspace(*(p+1))) goto err;
  902. done=1;
  903. } else if (!*p) {
  904. /* unterminated quotes */
  905. goto err;
  906. } else {
  907. current = sdscatlen(current,p,1);
  908. }
  909. } else if (insq) {
  910. if (*p == '\\' && *(p+1) == '\'') {
  911. p++;
  912. current = sdscatlen(current,"'",1);
  913. } else if (*p == '\'') {
  914. /* closing quote must be followed by a space or
  915. * nothing at all. */
  916. if (*(p+1) && !isspace(*(p+1))) goto err;
  917. done=1;
  918. } else if (!*p) {
  919. /* unterminated quotes */
  920. goto err;
  921. } else {
  922. current = sdscatlen(current,p,1);
  923. }
  924. } else {
  925. switch(*p) {
  926. case ' ':
  927. case '\n':
  928. case '\r':
  929. case '\t':
  930. case '\0':
  931. done=1;
  932. break;
  933. case '"':
  934. inq=1;
  935. break;
  936. case '\'':
  937. insq=1;
  938. break;
  939. default:
  940. current = sdscatlen(current,p,1);
  941. break;
  942. }
  943. }
  944. if (*p) p++;
  945. }
  946. /* add the token to the vector */
  947. vector = s_realloc(vector,((*argc)+1)*sizeof(char*));
  948. vector[*argc] = current;
  949. (*argc)++;
  950. current = NULL;
  951. } else {
  952. /* Even on empty input string return something not NULL. */
  953. if (vector == NULL) vector = s_malloc(sizeof(void*));
  954. return vector;
  955. }
  956. }
  957. err:
  958. while((*argc)--)
  959. sdsfree(vector[*argc]);
  960. s_free(vector);
  961. if (current) sdsfree(current);
  962. *argc = 0;
  963. return NULL;
  964. }
  965. /* Modify the string substituting all the occurrences of the set of
  966. * characters specified in the 'from' string to the corresponding character
  967. * in the 'to' array.
  968. *
  969. * For instance: sdsmapchars(mystring, "ho", "01", 2)
  970. * will have the effect of turning the string "hello" into "0ell1".
  971. *
  972. * The function returns the sds string pointer, that is always the same
  973. * as the input pointer since no resize is needed. */
  974. sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) {
  975. size_t j, i, l = sdslen(s);
  976. for (j = 0; j < l; j++) {
  977. for (i = 0; i < setlen; i++) {
  978. if (s[j] == from[i]) {
  979. s[j] = to[i];
  980. break;
  981. }
  982. }
  983. }
  984. return s;
  985. }
  986. /* Join an array of C strings using the specified separator (also a C string).
  987. * Returns the result as an sds string. */
  988. sds sdsjoin(char **argv, int argc, char *sep) {
  989. sds join = sdsempty();
  990. int j;
  991. for (j = 0; j < argc; j++) {
  992. join = sdscat(join, argv[j]);
  993. if (j != argc-1) join = sdscat(join,sep);
  994. }
  995. return join;
  996. }
  997. /* Like sdsjoin, but joins an array of SDS strings. */
  998. sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen) {
  999. sds join = sdsempty();
  1000. int j;
  1001. for (j = 0; j < argc; j++) {
  1002. join = sdscatsds(join, argv[j]);
  1003. if (j != argc-1) join = sdscatlen(join,sep,seplen);
  1004. }
  1005. return join;
  1006. }
  1007. /* Wrappers to the allocators used by SDS. Note that SDS will actually
  1008. * just use the macros defined into sdsalloc.h in order to avoid to pay
  1009. * the overhead of function calls. Here we define these wrappers only for
  1010. * the programs SDS is linked to, if they want to touch the SDS internals
  1011. * even if they use a different allocator. */
  1012. void *sds_malloc(size_t size) { return s_malloc(size); }
  1013. void *sds_realloc(void *ptr, size_t size) { return s_realloc(ptr,size); }
  1014. void sds_free(void *ptr) { s_free(ptr); }
  1015. #if defined(SDS_TEST_MAIN)
  1016. #include <stdio.h>
  1017. #include "testhelp.h"
  1018. #include "limits.h"
  1019. #define UNUSED(x) (void)(x)
  1020. int sdsTest(void) {
  1021. {
  1022. sds x = sdsnew("foo"), y;
  1023. test_cond("Create a string and obtain the length",
  1024. sdslen(x) == 3 && memcmp(x,"foo\0",4) == 0)
  1025. sdsfree(x);
  1026. x = sdsnewlen("foo",2);
  1027. test_cond("Create a string with specified length",
  1028. sdslen(x) == 2 && memcmp(x,"fo\0",3) == 0)
  1029. x = sdscat(x,"bar");
  1030. test_cond("Strings concatenation",
  1031. sdslen(x) == 5 && memcmp(x,"fobar\0",6) == 0);
  1032. x = sdscpy(x,"a");
  1033. test_cond("sdscpy() against an originally longer string",
  1034. sdslen(x) == 1 && memcmp(x,"a\0",2) == 0)
  1035. x = sdscpy(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk");
  1036. test_cond("sdscpy() against an originally shorter string",
  1037. sdslen(x) == 33 &&
  1038. memcmp(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0)
  1039. sdsfree(x);
  1040. x = sdscatprintf(sdsempty(),"%d",123);
  1041. test_cond("sdscatprintf() seems working in the base case",
  1042. sdslen(x) == 3 && memcmp(x,"123\0",4) == 0)
  1043. sdsfree(x);
  1044. x = sdsnew("--");
  1045. x = sdscatfmt(x, "Hello %s World %I,%I--", "Hi!", LLONG_MIN,LLONG_MAX);
  1046. test_cond("sdscatfmt() seems working in the base case",
  1047. sdslen(x) == 60 &&
  1048. memcmp(x,"--Hello Hi! World -9223372036854775808,"
  1049. "9223372036854775807--",60) == 0)
  1050. printf("[%s]\n",x);
  1051. sdsfree(x);
  1052. x = sdsnew("--");
  1053. x = sdscatfmt(x, "%u,%U--", UINT_MAX, ULLONG_MAX);
  1054. test_cond("sdscatfmt() seems working with unsigned numbers",
  1055. sdslen(x) == 35 &&
  1056. memcmp(x,"--4294967295,18446744073709551615--",35) == 0)
  1057. sdsfree(x);
  1058. x = sdsnew(" x ");
  1059. sdstrim(x," x");
  1060. test_cond("sdstrim() works when all chars match",
  1061. sdslen(x) == 0)
  1062. sdsfree(x);
  1063. x = sdsnew(" x ");
  1064. sdstrim(x," ");
  1065. test_cond("sdstrim() works when a single char remains",
  1066. sdslen(x) == 1 && x[0] == 'x')
  1067. sdsfree(x);
  1068. x = sdsnew("xxciaoyyy");
  1069. sdstrim(x,"xy");
  1070. test_cond("sdstrim() correctly trims characters",
  1071. sdslen(x) == 4 && memcmp(x,"ciao\0",5) == 0)
  1072. y = sdsdup(x);
  1073. sdsrange(y,1,1);
  1074. test_cond("sdsrange(...,1,1)",
  1075. sdslen(y) == 1 && memcmp(y,"i\0",2) == 0)
  1076. sdsfree(y);
  1077. y = sdsdup(x);
  1078. sdsrange(y,1,-1);
  1079. test_cond("sdsrange(...,1,-1)",
  1080. sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)
  1081. sdsfree(y);
  1082. y = sdsdup(x);
  1083. sdsrange(y,-2,-1);
  1084. test_cond("sdsrange(...,-2,-1)",
  1085. sdslen(y) == 2 && memcmp(y,"ao\0",3) == 0)
  1086. sdsfree(y);
  1087. y = sdsdup(x);
  1088. sdsrange(y,2,1);
  1089. test_cond("sdsrange(...,2,1)",
  1090. sdslen(y) == 0 && memcmp(y,"\0",1) == 0)
  1091. sdsfree(y);
  1092. y = sdsdup(x);
  1093. sdsrange(y,1,100);
  1094. test_cond("sdsrange(...,1,100)",
  1095. sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)
  1096. sdsfree(y);
  1097. y = sdsdup(x);
  1098. sdsrange(y,100,100);
  1099. test_cond("sdsrange(...,100,100)",
  1100. sdslen(y) == 0 && memcmp(y,"\0",1) == 0)
  1101. sdsfree(y);
  1102. sdsfree(x);
  1103. x = sdsnew("foo");
  1104. y = sdsnew("foa");
  1105. test_cond("sdscmp(foo,foa)", sdscmp(x,y) > 0)
  1106. sdsfree(y);
  1107. sdsfree(x);
  1108. x = sdsnew("bar");
  1109. y = sdsnew("bar");
  1110. test_cond("sdscmp(bar,bar)", sdscmp(x,y) == 0)
  1111. sdsfree(y);
  1112. sdsfree(x);
  1113. x = sdsnew("aar");
  1114. y = sdsnew("bar");
  1115. test_cond("sdscmp(bar,bar)", sdscmp(x,y) < 0)
  1116. sdsfree(y);
  1117. sdsfree(x);
  1118. x = sdsnewlen("\a\n\0foo\r",7);
  1119. y = sdscatrepr(sdsempty(),x,sdslen(x));
  1120. test_cond("sdscatrepr(...data...)",
  1121. memcmp(y,"\"\\a\\n\\x00foo\\r\"",15) == 0)
  1122. {
  1123. unsigned int oldfree;
  1124. char *p;
  1125. int step = 10, j, i;
  1126. sdsfree(x);
  1127. sdsfree(y);
  1128. x = sdsnew("0");
  1129. test_cond("sdsnew() free/len buffers", sdslen(x) == 1 && sdsavail(x) == 0);
  1130. /* Run the test a few times in order to hit the first two
  1131. * SDS header types. */
  1132. for (i = 0; i < 10; i++) {
  1133. int oldlen = sdslen(x);
  1134. x = sdsMakeRoomFor(x,step);
  1135. int type = x[-1]&SDS_TYPE_MASK;
  1136. test_cond("sdsMakeRoomFor() len", sdslen(x) == oldlen);
  1137. if (type != SDS_TYPE_5) {
  1138. test_cond("sdsMakeRoomFor() free", sdsavail(x) >= step);
  1139. oldfree = sdsavail(x);
  1140. }
  1141. p = x+oldlen;
  1142. for (j = 0; j < step; j++) {
  1143. p[j] = 'A'+j;
  1144. }
  1145. sdsIncrLen(x,step);
  1146. }
  1147. test_cond("sdsMakeRoomFor() content",
  1148. memcmp("0ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ",x,101) == 0);
  1149. test_cond("sdsMakeRoomFor() final length",sdslen(x)==101);
  1150. sdsfree(x);
  1151. }
  1152. }
  1153. test_report()
  1154. return 0;
  1155. }
  1156. #endif
  1157. #ifdef SDS_TEST_MAIN
  1158. int main(void) {
  1159. return sdsTest();
  1160. }
  1161. #endif