Coverage Report

Created: 2026-05-14 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/duckdb/third_party/hyperloglog/sds.cpp
Line
Count
Source
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.hpp"
40
41
namespace duckdb_hll {
42
43
0
static bool CharacterIsSpace(char c) {
44
0
       return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r';
45
0
}
46
47
0
static inline int sdsHdrSize(char type) {
48
0
    switch(type&SDS_TYPE_MASK) {
49
0
        case SDS_TYPE_5:
50
0
            return sizeof(struct sdshdr5);
51
0
        case SDS_TYPE_8:
52
0
            return sizeof(struct sdshdr8);
53
0
        case SDS_TYPE_16:
54
0
            return sizeof(struct sdshdr16);
55
0
        case SDS_TYPE_32:
56
0
            return sizeof(struct sdshdr32);
57
0
        case SDS_TYPE_64:
58
0
            return sizeof(struct sdshdr64);
59
0
    }
60
0
    return 0;
61
0
}
62
63
0
static inline char sdsReqType(size_t string_size) {
64
0
    if (string_size < 1<<5)
65
0
        return SDS_TYPE_5;
66
0
    if (string_size < 1<<8)
67
0
        return SDS_TYPE_8;
68
0
    if (string_size < 1<<16)
69
0
        return SDS_TYPE_16;
70
0
#if (LONG_MAX == LLONG_MAX)
71
0
    if (string_size < 1ll<<32)
72
0
        return SDS_TYPE_32;
73
0
    return SDS_TYPE_64;
74
#else
75
    return SDS_TYPE_32;
76
#endif
77
0
}
78
79
/* Create a new sds string with the content specified by the 'init' pointer
80
 * and 'initlen'.
81
 * If NULL is used for 'init' the string is initialized with zero bytes.
82
 * If SDS_NOINIT is used, the buffer is left uninitialized;
83
 *
84
 * The string is always null-termined (all the sds strings are, always) so
85
 * even if you create an sds string with:
86
 *
87
 * mystring = sdsnewlen("abc",3);
88
 *
89
 * You can print the string with printf() as there is an implicit \0 at the
90
 * end of the string. However the string is binary safe and can contain
91
 * \0 characters in the middle, as the length is stored in the sds header. */
92
0
sds sdsnewlen(const void *init, size_t initlen) {
93
0
    void *sh;
94
0
    sds s;
95
0
    char type = sdsReqType(initlen);
96
    /* Empty strings are usually created in order to append. Use type 8
97
     * since type 5 is not good at this. */
98
0
    if (type == SDS_TYPE_5 && initlen == 0) type = SDS_TYPE_8;
99
0
    int hdrlen = sdsHdrSize(type);
100
0
    unsigned char *fp; /* flags pointer. */
101
102
0
    sh = malloc(hdrlen+initlen+1);
103
0
    if (!init)
104
0
        memset(sh, 0, hdrlen+initlen+1);
105
0
    if (sh == NULL) return NULL;
106
0
    s = (char*)sh+hdrlen;
107
0
    fp = ((unsigned char*)s)-1;
108
0
    switch(type) {
109
0
        case SDS_TYPE_5: {
110
0
            *fp = type | (initlen << SDS_TYPE_BITS);
111
0
            break;
112
0
        }
113
0
        case SDS_TYPE_8: {
114
0
            SDS_HDR_VAR(8,s);
115
0
            sh->len = initlen;
116
0
            sh->alloc = initlen;
117
0
            *fp = type;
118
0
            break;
119
0
        }
120
0
        case SDS_TYPE_16: {
121
0
            SDS_HDR_VAR(16,s);
122
0
            sh->len = initlen;
123
0
            sh->alloc = initlen;
124
0
            *fp = type;
125
0
            break;
126
0
        }
127
0
        case SDS_TYPE_32: {
128
0
            SDS_HDR_VAR(32,s);
129
0
            sh->len = initlen;
130
0
            sh->alloc = initlen;
131
0
            *fp = type;
132
0
            break;
133
0
        }
134
0
        case SDS_TYPE_64: {
135
0
            SDS_HDR_VAR(64,s);
136
0
            sh->len = initlen;
137
0
            sh->alloc = initlen;
138
0
            *fp = type;
139
0
            break;
140
0
        }
141
0
    }
142
0
    if (initlen && init)
143
0
        memcpy(s, init, initlen);
144
0
    s[initlen] = '\0';
145
0
    return s;
146
0
}
147
148
/* Create an empty (zero length) sds string. Even in this case the string
149
 * always has an implicit null term. */
150
0
sds sdsempty(void) {
151
0
    return sdsnewlen("",0);
152
0
}
153
154
/* Create a new sds string starting from a null terminated C string. */
155
0
sds sdsnew(const char *init) {
156
0
    size_t initlen = (init == NULL) ? 0 : strlen(init);
157
0
    return sdsnewlen(init, initlen);
158
0
}
159
160
/* Duplicate an sds string. */
161
0
sds sdsdup(const sds s) {
162
0
    return sdsnewlen(s, sdslen(s));
163
0
}
164
165
/* Free an sds string. No operation is performed if 's' is NULL. */
166
0
void sdsfree(sds s) {
167
0
    if (s == NULL) return;
168
0
    free((char*)s-sdsHdrSize(s[-1]));
169
0
}
170
171
/* Set the sds string length to the length as obtained with strlen(), so
172
 * considering as content only up to the first null term character.
173
 *
174
 * This function is useful when the sds string is hacked manually in some
175
 * way, like in the following example:
176
 *
177
 * s = sdsnew("foobar");
178
 * s[2] = '\0';
179
 * sdsupdatelen(s);
180
 * printf("%d\n", sdslen(s));
181
 *
182
 * The output will be "2", but if we comment out the call to sdsupdatelen()
183
 * the output will be "6" as the string was modified but the logical length
184
 * remains 6 bytes. */
185
0
void sdsupdatelen(sds s) {
186
0
    size_t reallen = strlen(s);
187
0
    sdssetlen(s, reallen);
188
0
}
189
190
/* Modify an sds string in-place to make it empty (zero length).
191
 * However all the existing buffer is not discarded but set as free space
192
 * so that next append operations will not require allocations up to the
193
 * number of bytes previously available. */
194
0
void sdsclear(sds s) {
195
0
    sdssetlen(s, 0);
196
0
    s[0] = '\0';
197
0
}
198
199
/* Enlarge the free space at the end of the sds string so that the caller
200
 * is sure that after calling this function can overwrite up to addlen
201
 * bytes after the end of the string, plus one more byte for nul term.
202
 *
203
 * Note: this does not change the *length* of the sds string as returned
204
 * by sdslen(), but only the free buffer space we have. */
205
0
sds sdsMakeRoomFor(sds s, size_t addlen) {
206
0
    void *sh, *newsh;
207
0
    size_t avail = sdsavail(s);
208
0
    size_t len, newlen, reqlen;
209
0
    char type, oldtype = s[-1] & SDS_TYPE_MASK;
210
0
    int hdrlen;
211
212
    /* Return ASAP if there is enough space left. */
213
0
    if (avail >= addlen) return s;
214
215
0
    len = sdslen(s);
216
0
    sh = (char*)s-sdsHdrSize(oldtype);
217
0
    reqlen = newlen = (len+addlen);
218
0
    assert(newlen > len);   /* Catch size_t overflow */
219
0
    if (newlen < SDS_MAX_PREALLOC)
220
0
        newlen *= 2;
221
0
    else
222
0
        newlen += SDS_MAX_PREALLOC;
223
224
0
    type = sdsReqType(newlen);
225
226
    /* Don't use type 5: the user is appending to the string and type 5 is
227
     * not able to remember empty space, so sdsMakeRoomFor() must be called
228
     * at every appending operation. */
229
0
    if (type == SDS_TYPE_5) type = SDS_TYPE_8;
230
231
0
    hdrlen = sdsHdrSize(type);
232
0
    assert(hdrlen + newlen + 1 > reqlen);  /* Catch size_t overflow */
233
0
    (void)reqlen;
234
0
    if (oldtype==type) {
235
0
        newsh = realloc(sh, hdrlen+newlen+1);
236
0
        if (newsh == NULL) return NULL;
237
0
        s = (char*)newsh+hdrlen;
238
0
    } else {
239
        /* Since the header size changes, need to move the string forward,
240
         * and can't use realloc */
241
0
        newsh = malloc(hdrlen+newlen+1);
242
0
        if (newsh == NULL) return NULL;
243
0
        memcpy((char*)newsh+hdrlen, s, len+1);
244
0
        free(sh);
245
0
        s = (char*)newsh+hdrlen;
246
0
        s[-1] = type;
247
0
        sdssetlen(s, len);
248
0
    }
249
0
    sdssetalloc(s, newlen);
250
0
    return s;
251
0
}
252
253
/* Reallocate the sds string so that it has no free space at the end. The
254
 * contained string remains not altered, but next concatenation operations
255
 * will require a reallocation.
256
 *
257
 * After the call, the passed sds string is no longer valid and all the
258
 * references must be substituted with the new pointer returned by the call. */
259
0
sds sdsRemoveFreeSpace(sds s) {
260
0
    void *sh, *newsh;
261
0
    char type, oldtype = s[-1] & SDS_TYPE_MASK;
262
0
    int hdrlen, oldhdrlen = sdsHdrSize(oldtype);
263
0
    size_t len = sdslen(s);
264
0
    sh = (char*)s-oldhdrlen;
265
266
    /* Check what would be the minimum SDS header that is just good enough to
267
     * fit this string. */
268
0
    type = sdsReqType(len);
269
0
    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
0
    if (oldtype==type || type > SDS_TYPE_8) {
276
0
        newsh = realloc(sh, oldhdrlen+len+1);
277
0
        if (newsh == NULL) return NULL;
278
0
        s = (char*)newsh+oldhdrlen;
279
0
    } else {
280
0
        newsh = malloc(hdrlen+len+1);
281
0
        if (newsh == NULL) return NULL;
282
0
        memcpy((char*)newsh+hdrlen, s, len+1);
283
0
        free(sh);
284
0
        s = (char*)newsh+hdrlen;
285
0
        s[-1] = type;
286
0
        sdssetlen(s, len);
287
0
    }
288
0
    sdssetalloc(s, len);
289
0
    return s;
290
0
}
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
0
size_t sdsAllocSize(sds s) {
300
0
    size_t alloc = sdsalloc(s);
301
0
    return sdsHdrSize(s[-1])+alloc+1;
302
0
}
303
304
/* Return the pointer of the actual SDS allocation (normally SDS strings
305
 * are referenced by the start of the string buffer). */
306
0
void *sdsAllocPtr(sds s) {
307
0
    return (void*) (s-sdsHdrSize(s[-1]));
308
0
}
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
0
void sdsIncrLen(sds s, ssize_t incr) {
334
0
    unsigned char flags = s[-1];
335
0
    size_t len;
336
0
    switch(flags&SDS_TYPE_MASK) {
337
0
        case SDS_TYPE_5: {
338
0
            unsigned char *fp = ((unsigned char*)s)-1;
339
0
            unsigned char oldlen = SDS_TYPE_5_LEN(flags);
340
0
            assert((incr > 0 && oldlen+incr < 32) || (incr < 0 && oldlen >= (unsigned int)(-incr)));
341
0
            *fp = SDS_TYPE_5 | ((oldlen+incr) << SDS_TYPE_BITS);
342
0
            len = oldlen+incr;
343
0
            break;
344
0
        }
345
0
        case SDS_TYPE_8: {
346
0
            SDS_HDR_VAR(8,s);
347
0
            assert((incr >= 0 && sh->alloc-sh->len >= incr) || (incr < 0 && sh->len >= (unsigned int)(-incr)));
348
0
            len = (sh->len += incr);
349
0
            break;
350
0
        }
351
0
        case SDS_TYPE_16: {
352
0
            SDS_HDR_VAR(16,s);
353
0
            assert((incr >= 0 && sh->alloc-sh->len >= incr) || (incr < 0 && sh->len >= (unsigned int)(-incr)));
354
0
            len = (sh->len += incr);
355
0
            break;
356
0
        }
357
0
        case SDS_TYPE_32: {
358
0
            SDS_HDR_VAR(32,s);
359
0
            assert((incr >= 0 && sh->alloc-sh->len >= (unsigned int)incr) || (incr < 0 && sh->len >= (unsigned int)(-incr)));
360
0
            len = (sh->len += incr);
361
0
            break;
362
0
        }
363
0
        case SDS_TYPE_64: {
364
0
            SDS_HDR_VAR(64,s);
365
0
            assert((incr >= 0 && sh->alloc-sh->len >= (uint64_t)incr) || (incr < 0 && sh->len >= (uint64_t)(-incr)));
366
0
            len = (sh->len += incr);
367
0
            break;
368
0
        }
369
0
        default: len = 0; /* Just to avoid compilation warnings. */
370
0
    }
371
0
    s[len] = '\0';
372
0
}
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
0
sds sdsgrowzero(sds s, size_t len) {
380
0
    size_t curlen = sdslen(s);
381
382
0
    if (len <= curlen) return s;
383
0
    s = sdsMakeRoomFor(s,len-curlen);
384
0
    if (s == NULL) return NULL;
385
386
    /* Make sure added region doesn't contain garbage */
387
0
    memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */
388
0
    sdssetlen(s, len);
389
0
    return s;
390
0
}
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
0
sds sdscatlen(sds s, const void *t, size_t len) {
398
0
    size_t curlen = sdslen(s);
399
400
0
    s = sdsMakeRoomFor(s,len);
401
0
    if (s == NULL) return NULL;
402
0
    memcpy(s+curlen, t, len);
403
0
    sdssetlen(s, curlen+len);
404
0
    s[curlen+len] = '\0';
405
0
    return s;
406
0
}
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
0
sds sdscat(sds s, const char *t) {
413
0
    return sdscatlen(s, t, strlen(t));
414
0
}
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
0
sds sdscatsds(sds s, const sds t) {
421
0
    return sdscatlen(s, t, sdslen(t));
422
0
}
423
424
/* Destructively modify the sds string 's' to hold the specified binary
425
 * safe string pointed by 't' of length 'len' bytes. */
426
0
sds sdscpylen(sds s, const char *t, size_t len) {
427
0
    if (sdsalloc(s) < len) {
428
0
        s = sdsMakeRoomFor(s,len-sdslen(s));
429
0
        if (s == NULL) return NULL;
430
0
    }
431
0
    memcpy(s, t, len);
432
0
    s[len] = '\0';
433
0
    sdssetlen(s, len);
434
0
    return s;
435
0
}
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
0
sds sdscpy(sds s, const char *t) {
440
0
    return sdscpylen(s, t, strlen(t));
441
0
}
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
0
int sdsll2str(char *s, long long value) {
451
0
    char *p, aux;
452
0
    unsigned long long v;
453
0
    size_t l;
454
455
    /* Generate the string representation, this method produces
456
     * an reversed string. */
457
0
    v = (value < 0) ? -value : value;
458
0
    p = s;
459
0
    do {
460
0
        *p++ = '0'+(v%10);
461
0
        v /= 10;
462
0
    } while(v);
463
0
    if (value < 0) *p++ = '-';
464
465
    /* Compute length and add null term. */
466
0
    l = p-s;
467
0
    *p = '\0';
468
469
    /* Reverse the string. */
470
0
    p--;
471
0
    while(s < p) {
472
0
        aux = *s;
473
0
        *s = *p;
474
0
        *p = aux;
475
0
        s++;
476
0
        p--;
477
0
    }
478
0
    return l;
479
0
}
480
481
/* Identical sdsll2str(), but for unsigned long long type. */
482
0
int sdsull2str(char *s, unsigned long long v) {
483
0
    char *p, aux;
484
0
    size_t l;
485
486
    /* Generate the string representation, this method produces
487
     * an reversed string. */
488
0
    p = s;
489
0
    do {
490
0
        *p++ = '0'+(v%10);
491
0
        v /= 10;
492
0
    } while(v);
493
494
    /* Compute length and add null term. */
495
0
    l = p-s;
496
0
    *p = '\0';
497
498
    /* Reverse the string. */
499
0
    p--;
500
0
    while(s < p) {
501
0
        aux = *s;
502
0
        *s = *p;
503
0
        *p = aux;
504
0
        s++;
505
0
        p--;
506
0
    }
507
0
    return l;
508
0
}
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
0
sds sdsfromlonglong(long long value) {
515
0
    char buf[SDS_LLSTR_SIZE];
516
0
    int len = sdsll2str(buf,value);
517
518
0
    return sdsnewlen(buf,len);
519
0
}
520
521
/* Like sdscatprintf() but gets va_list instead of being variadic. */
522
0
sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
523
0
    va_list cpy;
524
0
    char staticbuf[1024], *buf = staticbuf, *t;
525
0
    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
0
    if (buflen > sizeof(staticbuf)) {
530
0
        buf = (char*) malloc(buflen);
531
0
        if (buf == NULL) return NULL;
532
0
    } else {
533
0
        buflen = sizeof(staticbuf);
534
0
    }
535
536
    /* Try with buffers two times bigger every time we fail to
537
     * fit the string in the current buffer size. */
538
0
    while(1) {
539
0
        buf[buflen-2] = '\0';
540
0
        va_copy(cpy,ap);
541
0
        vsnprintf(buf, buflen, fmt, cpy);
542
0
        va_end(cpy);
543
0
        if (buf[buflen-2] != '\0') {
544
0
            if (buf != staticbuf) free(buf);
545
0
            buflen *= 2;
546
0
            buf = (char*) malloc(buflen);
547
0
            if (buf == NULL) return NULL;
548
0
            continue;
549
0
        }
550
0
        break;
551
0
    }
552
553
    /* Finally concat the obtained string to the SDS string and return it. */
554
0
    t = sdscat(s, buf);
555
0
    if (buf != staticbuf) free(buf);
556
0
    return t;
557
0
}
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
0
sds sdscatprintf(sds s, const char *fmt, ...) {
576
0
    va_list ap;
577
0
    char *t;
578
0
    va_start(ap, fmt);
579
0
    t = sdscatvprintf(s,fmt,ap);
580
0
    va_end(ap);
581
0
    return t;
582
0
}
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
0
sds sdscatfmt(sds s, char const *fmt, ...) {
601
0
    size_t initlen = sdslen(s);
602
0
    const char *f = fmt;
603
0
    long i;
604
0
    va_list ap;
605
606
0
    va_start(ap,fmt);
607
0
    f = fmt;    /* Next format specifier byte to process. */
608
0
    i = initlen; /* Position of the next byte to write to dest str. */
609
0
    while(*f) {
610
0
        char next, *str;
611
0
        size_t l;
612
0
        long long num;
613
0
        unsigned long long unum;
614
615
        /* Make sure there is always space for at least 1 char. */
616
0
        if (sdsavail(s)==0) {
617
0
            s = sdsMakeRoomFor(s,1);
618
0
        }
619
620
0
        switch(*f) {
621
0
        case '%':
622
0
            next = *(f+1);
623
0
            f++;
624
0
            switch(next) {
625
0
            case 's':
626
0
            case 'S':
627
0
                str = va_arg(ap,char*);
628
0
                l = (next == 's') ? strlen(str) : sdslen(str);
629
0
                if (sdsavail(s) < l) {
630
0
                    s = sdsMakeRoomFor(s,l);
631
0
                }
632
0
                memcpy(s+i,str,l);
633
0
                sdsinclen(s,l);
634
0
                i += l;
635
0
                break;
636
0
            case 'i':
637
0
            case 'I':
638
0
                if (next == 'i')
639
0
                    num = va_arg(ap,int);
640
0
                else
641
0
                    num = va_arg(ap,long long);
642
0
                {
643
0
                    char buf[SDS_LLSTR_SIZE];
644
0
                    l = sdsll2str(buf,num);
645
0
                    if (sdsavail(s) < l) {
646
0
                        s = sdsMakeRoomFor(s,l);
647
0
                    }
648
0
                    memcpy(s+i,buf,l);
649
0
                    sdsinclen(s,l);
650
0
                    i += l;
651
0
                }
652
0
                break;
653
0
            case 'u':
654
0
            case 'U':
655
0
                if (next == 'u')
656
0
                    unum = va_arg(ap,unsigned int);
657
0
                else
658
0
                    unum = va_arg(ap,unsigned long long);
659
0
                {
660
0
                    char buf[SDS_LLSTR_SIZE];
661
0
                    l = sdsull2str(buf,unum);
662
0
                    if (sdsavail(s) < l) {
663
0
                        s = sdsMakeRoomFor(s,l);
664
0
                    }
665
0
                    memcpy(s+i,buf,l);
666
0
                    sdsinclen(s,l);
667
0
                    i += l;
668
0
                }
669
0
                break;
670
0
            default: /* Handle %% and generally %<unknown>. */
671
0
                s[i++] = next;
672
0
                sdsinclen(s,1);
673
0
                break;
674
0
            }
675
0
            break;
676
0
        default:
677
0
            s[i++] = *f;
678
0
            sdsinclen(s,1);
679
0
            break;
680
0
        }
681
0
        f++;
682
0
    }
683
0
    va_end(ap);
684
685
    /* Add null-term */
686
0
    s[i] = '\0';
687
0
    return s;
688
0
}
689
690
/* Remove the part of the string from left and from right composed just of
691
 * contiguous characters found in 'cset', that is a null terminted C string.
692
 *
693
 * After the call, the modified sds string is no longer valid and all the
694
 * references must be substituted with the new pointer returned by the call.
695
 *
696
 * Example:
697
 *
698
 * s = sdsnew("AA...AA.a.aa.aHelloWorld     :::");
699
 * s = sdstrim(s,"Aa. :");
700
 * printf("%s\n", s);
701
 *
702
 * Output will be just "Hello World".
703
 */
704
0
sds sdstrim(sds s, const char *cset) {
705
0
    char *start, *end, *sp, *ep;
706
0
    size_t len;
707
708
0
    sp = start = s;
709
0
    ep = end = s+sdslen(s)-1;
710
0
    while(sp <= end && strchr(cset, *sp)) sp++;
711
0
    while(ep > sp && strchr(cset, *ep)) ep--;
712
0
    len = (sp > ep) ? 0 : ((ep-sp)+1);
713
0
    if (s != sp) memmove(s, sp, len);
714
0
    s[len] = '\0';
715
0
    sdssetlen(s,len);
716
0
    return s;
717
0
}
718
719
/* Turn the string into a smaller (or equal) string containing only the
720
 * substring specified by the 'start' and 'end' indexes.
721
 *
722
 * start and end can be negative, where -1 means the last character of the
723
 * string, -2 the penultimate character, and so forth.
724
 *
725
 * The interval is inclusive, so the start and end characters will be part
726
 * of the resulting string.
727
 *
728
 * The string is modified in-place.
729
 *
730
 * Example:
731
 *
732
 * s = sdsnew("Hello World");
733
 * sdsrange(s,1,-1); => "ello World"
734
 */
735
0
void sdsrange(sds s, ssize_t start, ssize_t end) {
736
0
    size_t newlen, len = sdslen(s);
737
738
0
    if (len == 0) return;
739
0
    if (start < 0) {
740
0
        start = len+start;
741
0
        if (start < 0) start = 0;
742
0
    }
743
0
    if (end < 0) {
744
0
        end = len+end;
745
0
        if (end < 0) end = 0;
746
0
    }
747
0
    newlen = (start > end) ? 0 : (end-start)+1;
748
0
    if (newlen != 0) {
749
0
        if (start >= (ssize_t)len) {
750
0
            newlen = 0;
751
0
        } else if (end >= (ssize_t)len) {
752
0
            end = len-1;
753
0
            newlen = (start > end) ? 0 : (end-start)+1;
754
0
        }
755
0
    } else {
756
0
        start = 0;
757
0
    }
758
0
    if (start && newlen) memmove(s, s+start, newlen);
759
0
    s[newlen] = 0;
760
0
    sdssetlen(s,newlen);
761
0
}
762
763
/* Apply tolower() to every character of the sds string 's'. */
764
0
void sdstolower(sds s) {
765
0
    size_t len = sdslen(s), j;
766
767
0
    for (j = 0; j < len; j++) s[j] = tolower(s[j]);
768
0
}
769
770
/* Apply toupper() to every character of the sds string 's'. */
771
0
void sdstoupper(sds s) {
772
0
    size_t len = sdslen(s), j;
773
774
0
    for (j = 0; j < len; j++) s[j] = toupper(s[j]);
775
0
}
776
777
/* Compare two sds strings s1 and s2 with memcmp().
778
 *
779
 * Return value:
780
 *
781
 *     positive if s1 > s2.
782
 *     negative if s1 < s2.
783
 *     0 if s1 and s2 are exactly the same binary string.
784
 *
785
 * If two strings share exactly the same prefix, but one of the two has
786
 * additional characters, the longer string is considered to be greater than
787
 * the smaller one. */
788
0
int sdscmp(const sds s1, const sds s2) {
789
0
    size_t l1, l2, minlen;
790
0
    int cmp;
791
792
0
    l1 = sdslen(s1);
793
0
    l2 = sdslen(s2);
794
0
    minlen = (l1 < l2) ? l1 : l2;
795
0
    cmp = memcmp(s1,s2,minlen);
796
0
    if (cmp == 0) return l1>l2? 1: (l1<l2? -1: 0);
797
0
    return cmp;
798
0
}
799
800
/* Split 's' with separator in 'sep'. An array
801
 * of sds strings is returned. *count will be set
802
 * by reference to the number of tokens returned.
803
 *
804
 * On out of memory, zero length string, zero length
805
 * separator, NULL is returned.
806
 *
807
 * Note that 'sep' is able to split a string using
808
 * a multi-character separator. For example
809
 * sdssplit("foo_-_bar","_-_"); will return two
810
 * elements "foo" and "bar".
811
 *
812
 * This version of the function is binary-safe but
813
 * requires length arguments. sdssplit() is just the
814
 * same function but for zero-terminated strings.
815
 */
816
0
sds *sdssplitlen(const char *s, ssize_t len, const char *sep, int seplen, int *count) {
817
0
    int elements = 0, slots = 5;
818
0
    long start = 0, j;
819
0
    sds *tokens;
820
821
0
    if (seplen < 1 || len < 0) return NULL;
822
823
0
    tokens = (sds*) malloc(sizeof(sds)*slots);
824
0
    if (tokens == NULL) return NULL;
825
826
0
    if (len == 0) {
827
0
        *count = 0;
828
0
        return tokens;
829
0
    }
830
0
    for (j = 0; j < (len-(seplen-1)); j++) {
831
        /* make sure there is room for the next element and the final one */
832
0
        if (slots < elements+2) {
833
0
            sds *newtokens;
834
835
0
            slots *= 2;
836
0
            newtokens = (sds*) realloc(tokens,sizeof(sds)*slots);
837
0
            if (newtokens == NULL) goto cleanup;
838
0
            tokens = newtokens;
839
0
        }
840
        /* search the separator */
841
0
        if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) {
842
0
            tokens[elements] = sdsnewlen(s+start,j-start);
843
0
            if (tokens[elements] == NULL) goto cleanup;
844
0
            elements++;
845
0
            start = j+seplen;
846
0
            j = j+seplen-1; /* skip the separator */
847
0
        }
848
0
    }
849
    /* Add the final element. We are sure there is room in the tokens array. */
850
0
    tokens[elements] = sdsnewlen(s+start,len-start);
851
0
    if (tokens[elements] == NULL) goto cleanup;
852
0
    elements++;
853
0
    *count = elements;
854
0
    return tokens;
855
856
0
cleanup:
857
0
    {
858
0
        int i;
859
0
        for (i = 0; i < elements; i++) sdsfree(tokens[i]);
860
0
        free(tokens);
861
0
        *count = 0;
862
0
        return NULL;
863
0
    }
864
0
}
865
866
/* Free the result returned by sdssplitlen(), or do nothing if 'tokens' is NULL. */
867
0
void sdsfreesplitres(sds *tokens, int count) {
868
0
    if (!tokens) return;
869
0
    while(count--)
870
0
        sdsfree(tokens[count]);
871
0
    free(tokens);
872
0
}
873
874
/* Append to the sds string "s" an escaped string representation where
875
 * all the non-printable characters (tested with isprint()) are turned into
876
 * escapes in the form "\n\r\a...." or "\x<hex-number>".
877
 *
878
 * After the call, the modified sds string is no longer valid and all the
879
 * references must be substituted with the new pointer returned by the call. */
880
0
sds sdscatrepr(sds s, const char *p, size_t len) {
881
0
    s = sdscatlen(s,"\"",1);
882
0
    while(len--) {
883
0
        switch(*p) {
884
0
        case '\\':
885
0
        case '"':
886
0
            s = sdscatprintf(s,"\\%c",*p);
887
0
            break;
888
0
        case '\n': s = sdscatlen(s,"\\n",2); break;
889
0
        case '\r': s = sdscatlen(s,"\\r",2); break;
890
0
        case '\t': s = sdscatlen(s,"\\t",2); break;
891
0
        case '\a': s = sdscatlen(s,"\\a",2); break;
892
0
        case '\b': s = sdscatlen(s,"\\b",2); break;
893
0
        default:
894
0
            if (isprint(*p))
895
0
                s = sdscatprintf(s,"%c",*p);
896
0
            else
897
0
                s = sdscatprintf(s,"\\x%02x",(unsigned char)*p);
898
0
            break;
899
0
        }
900
0
        p++;
901
0
    }
902
0
    return sdscatlen(s,"\"",1);
903
0
}
904
905
/* Helper function for sdssplitargs() that returns non zero if 'c'
906
 * is a valid hex digit. */
907
0
int is_hex_digit(char c) {
908
0
    return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
909
0
           (c >= 'A' && c <= 'F');
910
0
}
911
912
/* Helper function for sdssplitargs() that converts a hex digit into an
913
 * integer from 0 to 15 */
914
0
int hex_digit_to_int(char c) {
915
0
    switch(c) {
916
0
    case '0': return 0;
917
0
    case '1': return 1;
918
0
    case '2': return 2;
919
0
    case '3': return 3;
920
0
    case '4': return 4;
921
0
    case '5': return 5;
922
0
    case '6': return 6;
923
0
    case '7': return 7;
924
0
    case '8': return 8;
925
0
    case '9': return 9;
926
0
    case 'a': case 'A': return 10;
927
0
    case 'b': case 'B': return 11;
928
0
    case 'c': case 'C': return 12;
929
0
    case 'd': case 'D': return 13;
930
0
    case 'e': case 'E': return 14;
931
0
    case 'f': case 'F': return 15;
932
0
    default: return 0;
933
0
    }
934
0
}
935
936
/* Split a line into arguments, where every argument can be in the
937
 * following programming-language REPL-alike form:
938
 *
939
 * foo bar "newline are supported\n" and "\xff\x00otherstuff"
940
 *
941
 * The number of arguments is stored into *argc, and an array
942
 * of sds is returned.
943
 *
944
 * The caller should free the resulting array of sds strings with
945
 * sdsfreesplitres().
946
 *
947
 * Note that sdscatrepr() is able to convert back a string into
948
 * a quoted string in the same format sdssplitargs() is able to parse.
949
 *
950
 * The function returns the allocated tokens on success, even when the
951
 * input string is empty, or NULL if the input contains unbalanced
952
 * quotes or closed quotes followed by non space characters
953
 * as in: "foo"bar or "foo'
954
 */
955
0
sds *sdssplitargs(const char *line, int *argc) {
956
0
    const char *p = line;
957
0
    char *current = NULL;
958
0
    char **vector = NULL;
959
960
0
    *argc = 0;
961
0
    while(1) {
962
        /* skip blanks */
963
0
        while(*p && CharacterIsSpace(*p)) p++;
964
0
        if (*p) {
965
            /* get a token */
966
0
            int inq=0;  /* set to 1 if we are in "quotes" */
967
0
            int insq=0; /* set to 1 if we are in 'single quotes' */
968
0
            int done=0;
969
970
0
            if (current == NULL) current = sdsempty();
971
0
            while(!done) {
972
0
                if (inq) {
973
0
                    if (*p == '\\' && *(p+1) == 'x' &&
974
0
                                             is_hex_digit(*(p+2)) &&
975
0
                                             is_hex_digit(*(p+3)))
976
0
                    {
977
0
                        unsigned char byte;
978
979
0
                        byte = (hex_digit_to_int(*(p+2))*16)+
980
0
                                hex_digit_to_int(*(p+3));
981
0
                        current = sdscatlen(current,(char*)&byte,1);
982
0
                        p += 3;
983
0
                    } else if (*p == '\\' && *(p+1)) {
984
0
                        char c;
985
986
0
                        p++;
987
0
                        switch(*p) {
988
0
                        case 'n': c = '\n'; break;
989
0
                        case 'r': c = '\r'; break;
990
0
                        case 't': c = '\t'; break;
991
0
                        case 'b': c = '\b'; break;
992
0
                        case 'a': c = '\a'; break;
993
0
                        default: c = *p; break;
994
0
                        }
995
0
                        current = sdscatlen(current,&c,1);
996
0
                    } else if (*p == '"') {
997
                        /* closing quote must be followed by a space or
998
                         * nothing at all. */
999
0
                        if (*(p+1) && !CharacterIsSpace(*(p+1))) goto err;
1000
0
                        done=1;
1001
0
                    } else if (!*p) {
1002
                        /* unterminated quotes */
1003
0
                        goto err;
1004
0
                    } else {
1005
0
                        current = sdscatlen(current,p,1);
1006
0
                    }
1007
0
                } else if (insq) {
1008
0
                    if (*p == '\\' && *(p+1) == '\'') {
1009
0
                        p++;
1010
0
                        current = sdscatlen(current,"'",1);
1011
0
                    } else if (*p == '\'') {
1012
                        /* closing quote must be followed by a space or
1013
                         * nothing at all. */
1014
0
                        if (*(p+1) && !CharacterIsSpace(*(p+1))) goto err;
1015
0
                        done=1;
1016
0
                    } else if (!*p) {
1017
                        /* unterminated quotes */
1018
0
                        goto err;
1019
0
                    } else {
1020
0
                        current = sdscatlen(current,p,1);
1021
0
                    }
1022
0
                } else {
1023
0
                    switch(*p) {
1024
0
                    case ' ':
1025
0
                    case '\n':
1026
0
                    case '\r':
1027
0
                    case '\t':
1028
0
                    case '\0':
1029
0
                        done=1;
1030
0
                        break;
1031
0
                    case '"':
1032
0
                        inq=1;
1033
0
                        break;
1034
0
                    case '\'':
1035
0
                        insq=1;
1036
0
                        break;
1037
0
                    default:
1038
0
                        current = sdscatlen(current,p,1);
1039
0
                        break;
1040
0
                    }
1041
0
                }
1042
0
                if (*p) p++;
1043
0
            }
1044
            /* add the token to the vector */
1045
0
            vector = (char**) realloc(vector,((*argc)+1)*sizeof(char*));
1046
0
            vector[*argc] = current;
1047
0
            (*argc)++;
1048
0
            current = NULL;
1049
0
        } else {
1050
            /* Even on empty input string return something not NULL. */
1051
0
            if (vector == NULL) vector = (char**) malloc(sizeof(void*));
1052
0
            return vector;
1053
0
        }
1054
0
    }
1055
1056
0
err:
1057
0
    while((*argc)--)
1058
0
        sdsfree(vector[*argc]);
1059
0
    free(vector);
1060
0
    if (current) sdsfree(current);
1061
0
    *argc = 0;
1062
0
    return NULL;
1063
0
}
1064
1065
/* Modify the string substituting all the occurrences of the set of
1066
 * characters specified in the 'from' string to the corresponding character
1067
 * in the 'to' array.
1068
 *
1069
 * For instance: sdsmapchars(mystring, "ho", "01", 2)
1070
 * will have the effect of turning the string "hello" into "0ell1".
1071
 *
1072
 * The function returns the sds string pointer, that is always the same
1073
 * as the input pointer since no resize is needed. */
1074
0
sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) {
1075
0
    size_t j, i, l = sdslen(s);
1076
1077
0
    for (j = 0; j < l; j++) {
1078
0
        for (i = 0; i < setlen; i++) {
1079
0
            if (s[j] == from[i]) {
1080
0
                s[j] = to[i];
1081
0
                break;
1082
0
            }
1083
0
        }
1084
0
    }
1085
0
    return s;
1086
0
}
1087
1088
/* Join an array of C strings using the specified separator (also a C string).
1089
 * Returns the result as an sds string. */
1090
0
sds sdsjoin(char **argv, int argc, char *sep) {
1091
0
    sds join = sdsempty();
1092
0
    int j;
1093
1094
0
    for (j = 0; j < argc; j++) {
1095
0
        join = sdscat(join, argv[j]);
1096
0
        if (j != argc-1) join = sdscat(join,sep);
1097
0
    }
1098
0
    return join;
1099
0
}
1100
1101
/* Like sdsjoin, but joins an array of SDS strings. */
1102
0
sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen) {
1103
0
    sds join = sdsempty();
1104
0
    int j;
1105
1106
0
    for (j = 0; j < argc; j++) {
1107
0
        join = sdscatsds(join, argv[j]);
1108
0
        if (j != argc-1) join = sdscatlen(join,sep,seplen);
1109
0
    }
1110
0
    return join;
1111
0
}
1112
1113
/* Wrappers to the allocators used by SDS. Note that SDS will actually
1114
 * just use the macros defined into sdsalloc.h in order to avoid to pay
1115
 * the overhead of function calls. Here we define these wrappers only for
1116
 * the programs SDS is linked to, if they want to touch the SDS internals
1117
 * even if they use a different allocator. */
1118
0
void *sdmalloc(size_t size) { return malloc(size); }
1119
0
void *sdrealloc(void *ptr, size_t size) { return realloc(ptr,size); }
1120
0
void sdfree(void *ptr) { free(ptr); }
1121
1122
}