Debian Codegraph @

texk/web2c/mfluadir/otfcc/include/dep/sds.h

raw
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, Redis Labs, Inc
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Redis nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef __SDS_H
33 #define __SDS_H
34
35 #define SDS_MAX_PREALLOC (1024 * 1024)
36
37 #include <sys/types.h>
38 #include <stdarg.h>
39 #include <stdint.h>
40
41 typedef char *sds;
42
43 /* Note: sdshdr5 is never used, we just access the flags byte directly.
44 * However is here to document the layout of type 5 SDS strings. */
45 #ifdef _MSC_VER
46 #define __attribute__(x)
47 #pragma pack(push,1)
48 #endif
49 struct __attribute__((__packed__)) sdshdr5 {
50 unsigned char flags; /* 3 lsb of type, and 5 msb of string length */
51 char buf[];
52 };
53 struct __attribute__((__packed__)) sdshdr8 {
54 uint8_t len; /* used */
55 uint8_t alloc; /* excluding the header and null terminator */
56 unsigned char flags; /* 3 lsb of type, 5 unused bits */
57 char buf[];
58 };
59 struct __attribute__((__packed__)) sdshdr16 {
60 uint16_t len; /* used */
61 uint16_t alloc; /* excluding the header and null terminator */
62 unsigned char flags; /* 3 lsb of type, 5 unused bits */
63 char buf[];
64 };
65 struct __attribute__((__packed__)) sdshdr32 {
66 uint32_t len; /* used */
67 uint32_t alloc; /* excluding the header and null terminator */
68 unsigned char flags; /* 3 lsb of type, 5 unused bits */
69 char buf[];
70 };
71 struct __attribute__((__packed__)) sdshdr64 {
72 uint64_t len; /* used */
73 uint64_t alloc; /* excluding the header and null terminator */
74 unsigned char flags; /* 3 lsb of type, 5 unused bits */
75 char buf[];
76 };
77 #ifdef _MSC_VER
78 #pragma pack(pop)
79 #endif
80 #define SDS_TYPE_5 0
81 #define SDS_TYPE_8 1
82 #define SDS_TYPE_16 2
83 #define SDS_TYPE_32 3
84 #define SDS_TYPE_64 4
85 #define SDS_TYPE_MASK 7
86 #define SDS_TYPE_BITS 3
87 #define SDS_HDR_VAR(T, s) struct sdshdr##T *sh = (void *)((s) - (sizeof(struct sdshdr##T)));
88 #define SDS_HDR(T, s) ((struct sdshdr##T *)((s) - (sizeof(struct sdshdr##T))))
89 #define SDS_TYPE_5_LEN(f) ((f) >> SDS_TYPE_BITS)
90
91 static inline size_t sdslen(const sds s) {
92 unsigned char flags = s[-1];
93 switch (flags & SDS_TYPE_MASK) {
94 case SDS_TYPE_5:
95 return SDS_TYPE_5_LEN(flags);
96 case SDS_TYPE_8:
97 return SDS_HDR(8, s)->len;
98 case SDS_TYPE_16:
99 return SDS_HDR(16, s)->len;
100 case SDS_TYPE_32:
101 return SDS_HDR(32, s)->len;
102 case SDS_TYPE_64:
103 return SDS_HDR(64, s)->len;
104 }
105 return 0;
106 }
107
108 static inline size_t sdsavail(const sds s) {
109 unsigned char flags = s[-1];
110 switch (flags & SDS_TYPE_MASK) {
111 case SDS_TYPE_5: {
112 return 0;
113 }
114 case SDS_TYPE_8: {
115 SDS_HDR_VAR(8, s);
116 return sh->alloc - sh->len;
117 }
118 case SDS_TYPE_16: {
119 SDS_HDR_VAR(16, s);
120 return sh->alloc - sh->len;
121 }
122 case SDS_TYPE_32: {
123 SDS_HDR_VAR(32, s);
124 return sh->alloc - sh->len;
125 }
126 case SDS_TYPE_64: {
127 SDS_HDR_VAR(64, s);
128 return sh->alloc - sh->len;
129 }
130 }
131 return 0;
132 }
133
134 static inline void sdssetlen(sds s, size_t newlen) {
135 unsigned char flags = s[-1];
136 switch (flags & SDS_TYPE_MASK) {
137 case SDS_TYPE_5: {
138 unsigned char *fp = ((unsigned char *)s) - 1;
139 *fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS);
140 } break;
141 case SDS_TYPE_8:
142 SDS_HDR(8, s)->len = (uint8_t)newlen;
143 break;
144 case SDS_TYPE_16:
145 SDS_HDR(16, s)->len = (uint16_t)newlen;
146 break;
147 case SDS_TYPE_32:
148 SDS_HDR(32, s)->len = (uint32_t)newlen;
149 break;
150 case SDS_TYPE_64:
151 SDS_HDR(64, s)->len = newlen;
152 break;
153 }
154 }
155
156 static inline void sdsinclen(sds s, size_t inc) {
157 unsigned char flags = s[-1];
158 switch (flags & SDS_TYPE_MASK) {
159 case SDS_TYPE_5: {
160 unsigned char *fp = ((unsigned char *)s) - 1;
161 unsigned char newlen = SDS_TYPE_5_LEN(flags) + inc;
162 *fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS);
163 } break;
164 case SDS_TYPE_8:
165 SDS_HDR(8, s)->len += (uint8_t)inc;
166 break;
167 case SDS_TYPE_16:
168 SDS_HDR(16, s)->len += (uint16_t)inc;
169 break;
170 case SDS_TYPE_32:
171 SDS_HDR(32, s)->len += (uint32_t)inc;
172 break;
173 case SDS_TYPE_64:
174 SDS_HDR(64, s)->len += inc;
175 break;
176 }
177 }
178
179 /* sdsalloc() = sdsavail() + sdslen() */
180 static inline size_t sdsalloc(const sds s) {
181 unsigned char flags = s[-1];
182 switch (flags & SDS_TYPE_MASK) {
183 case SDS_TYPE_5:
184 return SDS_TYPE_5_LEN(flags);
185 case SDS_TYPE_8:
186 return SDS_HDR(8, s)->alloc;
187 case SDS_TYPE_16:
188 return SDS_HDR(16, s)->alloc;
189 case SDS_TYPE_32:
190 return SDS_HDR(32, s)->alloc;
191 case SDS_TYPE_64:
192 return SDS_HDR(64, s)->alloc;
193 }
194 return 0;
195 }
196
197 static inline void sdssetalloc(sds s, size_t newlen) {
198 unsigned char flags = s[-1];
199 switch (flags & SDS_TYPE_MASK) {
200 case SDS_TYPE_5:
201 /* Nothing to do, this type has no total allocation info. */
202 break;
203 case SDS_TYPE_8:
204 SDS_HDR(8, s)->alloc = (uint8_t)newlen;
205 break;
206 case SDS_TYPE_16:
207 SDS_HDR(16, s)->alloc = (uint16_t)newlen;
208 break;
209 case SDS_TYPE_32:
210 SDS_HDR(32, s)->alloc = (uint32_t)newlen;
211 break;
212 case SDS_TYPE_64:
213 SDS_HDR(64, s)->alloc = newlen;
214 break;
215 }
216 }
217
218 sds sdsnewlen(const void *init, size_t initlen);
219 sds sdsnew(const char *init);
220 sds sdsempty(void);
221 sds sdsdup(const sds s);
222 void sdsfree(sds s);
223 sds sdsgrowzero(sds s, size_t len);
224 sds sdscatlen(sds s, const void *t, size_t len);
225 sds sdscat(sds s, const char *t);
226 sds sdscatsds(sds s, const sds t);
227 sds sdscpylen(sds s, const char *t, size_t len);
228 sds sdscpy(sds s, const char *t);
229
230 sds sdscatvprintf(sds s, const char *fmt, va_list ap);
231 #ifdef __GNUC__
232 sds sdscatprintf(sds s, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
233 #else
234 sds sdscatprintf(sds s, const char *fmt, ...);
235 #endif
236
237 sds sdscatfmt(sds s, char const *fmt, ...);
238 sds sdstrim(sds s, const char *cset);
239 void sdsrange(sds s, int start, int end);
240 void sdsupdatelen(sds s);
241 void sdsclear(sds s);
242 int sdscmp(const sds s1, const sds s2);
243 sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count);
244 void sdsfreesplitres(sds *tokens, int count);
245 void sdstolower(sds s);
246 void sdstoupper(sds s);
247 sds sdsfromlonglong(long long value);
248 sds sdscatrepr(sds s, const char *p, size_t len);
249 sds *sdssplitargs(const char *line, int *argc);
250 sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen);
251 sds sdsjoin(char **argv, int argc, char *sep);
252 sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen);
253
254 /* Low level functions exposed to the user API */
255 sds sdsMakeRoomFor(sds s, size_t addlen);
256 void sdsIncrLen(sds s, int incr);
257 sds sdsRemoveFreeSpace(sds s);
258 size_t sdsAllocSize(sds s);
259 void *sdsAllocPtr(sds s);
260
261 #ifdef REDIS_TEST
262 int sdsTest(int argc, char *argv[]);
263 #endif
264
265 #endif