summaryrefslogtreecommitdiffstats
path: root/sds.h
blob: d52de5689bd243bc295e42ac4348a407765976de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* SDS (Simple Dynamic Strings), A C dynamic strings library.
 *
 * Copyright (c) 2006-2014, Salvatore Sanfilippo <antirez at gmail dot com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of Redis nor the names of its contributors may be used
 *     to endorse or promote products derived from this software without
 *     specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef __MICROBBS__SDS_H
#define __MICROBBS__SDS_H

#define SDS_MAX_PREALLOC (1024*1024)

#include <sys/types.h>
#include <stdarg.h>

typedef char *sds;

struct sdshdr {
    int len;
    int free;
    char buf[];
};

static inline size_t sds_len(const sds s) {
    struct sdshdr *sh = (void*)(s-sizeof *sh);
    return sh->len;
}

static inline size_t sds_avail(const sds s) {
    struct sdshdr *sh = (void*)(s-sizeof *sh);
    return sh->free;
}

sds sds_newlen(const void *init, size_t initlen);
sds sds_new(const char *init);
sds sds_empty(void);
size_t sds_len(const sds s);
sds sds_dup(const sds s);
void sds_free(sds s);
size_t sds_avail(const sds s);
sds sds_growzero(sds s, size_t len);
sds sds_catlen(sds s, const void *t, size_t len);
sds sds_cat(sds s, const char *t);
sds sds_catsds(sds s, const sds t);
sds sds_cpylen(sds s, const char *t, size_t len);
sds sds_cpy(sds s, const char *t);

sds sds_catvprintf(sds s, const char *fmt, va_list ap);
#ifdef __GNUC__
sds sds_catprintf(sds s, const char *fmt, ...)
    __attribute__((format(printf, 2, 3)));
#else
sds sds_catprintf(sds s, const char *fmt, ...);
#endif

void sds_trim(sds s, const char *cset);
void sds_range(sds s, int start, int end);
void sds_updatelen(sds s);
void sds_clear(sds s);
int sds_cmp(const sds s1, const sds s2);
sds *sds_splitlen(const char *s, int len, const char *sep, int seplen, int *count);
void sds_freesplitres(sds *tokens, int count);
void sds_tolower(sds s);
void sds_toupper(sds s);
sds sds_fromlonglong(long long value);
sds sds_catrepr(sds s, const char *p, size_t len);
sds *sds_splitargs(const char *line, int *argc);
sds sds_mapchars(sds s, const char *from, const char *to, size_t setlen);
sds sds_join(char **argv, int argc, char *sep, size_t seplen);
sds sds_joinsds(sds *argv, int argc, const char *sep, size_t seplen);

/* Low level functions exposed to the user API */
sds sds_make_room_for(sds s, size_t addlen);
void sds_incr_len(sds s, int incr);
sds sds_remove_free_space(sds s);
size_t sds_alloc_size(sds s);

#endif