berns 4.0.0 → 4.1.0

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