Debian Codegraph @

src/kstring.h

raw
1 /* The MIT License
2
3 Copyright (c) by Attractive Chaos <[email protected]>
4
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 "Software"), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
12
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 */
25
26 #ifndef KSTRING_H
27 #define KSTRING_H
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <stdint.h>
33 #include <stdio.h>
34
35 #ifndef kroundup32
36 #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
37 #endif
38
39 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
40 #define KS_ATTR_PRINTF(fmt, arg) __attribute__((__format__ (__printf__, fmt, arg)))
41 #else
42 #define KS_ATTR_PRINTF(fmt, arg)
43 #endif
44
45
46 /* kstring_t is a simple non-opaque type whose fields are likely to be
47 * used directly by user code (but see also ks_str() and ks_len() below).
48 * A kstring_t object is initialised by either of
49 * kstring_t str = { 0, 0, NULL };
50 * kstring_t str; ...; str.l = str.m = 0; str.s = NULL;
51 * and either ownership of the underlying buffer should be given away before
52 * the object disappears (see ks_release() below) or the kstring_t should be
53 * destroyed with free(str.s); */
54 #define kvsprintf abpoa_kvsprintf
55 #define ksprintf abpoa_ksprintf
56 #define ksplit_core abpoa_ksplit_core
57 #define kstrstr abpoa_kstrstr
58 #define kstrnstr abpoa_kstrnstr
59 #define kmemmem abpoa_kmemmem
60 #define kstrtok abpoa_kstrtok
61 #define kgetline abpoa_kgetline
62
63 #ifndef KSTRING_T
64 #define KSTRING_T kstring_t
65 typedef struct __kstring_t {
66 size_t l, m;
67 char *s;
68 } kstring_t;
69 #endif
70
71 typedef struct {
72 uint64_t tab[4];
73 int sep, finished;
74 const char *p; // end of the current token
75 } ks_tokaux_t;
76
77 #ifdef __cplusplus
78 extern "C" {
79 #endif
80
81 int kvsprintf(kstring_t *s, const char *fmt, va_list ap) KS_ATTR_PRINTF(2,0);
82 int ksprintf(kstring_t *s, const char *fmt, ...) KS_ATTR_PRINTF(2,3);
83 int ksplit_core(char *s, int delimiter, int *_max, int **_offsets);
84 char *kstrstr(const char *str, const char *pat, int **_prep);
85 char *kstrnstr(const char *str, const char *pat, int n, int **_prep);
86 void *kmemmem(const void *_str, int n, const void *_pat, int m, int **_prep);
87
88 /* kstrtok() is similar to strtok_r() except that str is not
89 * modified and both str and sep can be NULL. For efficiency, it is
90 * actually recommended to set both to NULL in the subsequent calls
91 * if sep is not changed. */
92 char *kstrtok(const char *str, const char *sep, ks_tokaux_t *aux);
93
94 /* kgetline() uses the supplied fgets()-like function to read a "\n"-
95 * or "\r\n"-terminated line from fp. The line read is appended to the
96 * kstring without its terminator and 0 is returned; EOF is returned at
97 * EOF or on error (determined by querying fp, as per fgets()). */
98 typedef char *kgets_func(char *, int, void *);
99 int kgetline(kstring_t *s, kgets_func *fgets, void *fp);
100
101 #ifdef __cplusplus
102 }
103 #endif
104
105 static inline int ks_resize(kstring_t *s, size_t size)
106 {
107 if (s->m < size) {
108 char *tmp;
109 s->m = size;
110 kroundup32(s->m);
111 if ((tmp = (char*)realloc(s->s, s->m)))
112 s->s = tmp;
113 else
114 return -1;
115 }
116 return 0;
117 }
118
119 static inline char *ks_str(kstring_t *s)
120 {
121 return s->s;
122 }
123
124 static inline size_t ks_len(kstring_t *s)
125 {
126 return s->l;
127 }
128
129 // Give ownership of the underlying buffer away to something else (making
130 // that something else responsible for freeing it), leaving the kstring_t
131 // empty and ready to be used again, or ready to go out of scope without
132 // needing free(str.s) to prevent a memory leak.
133 static inline char *ks_release(kstring_t *s)
134 {
135 char *ss = s->s;
136 s->l = s->m = 0;
137 s->s = NULL;
138 return ss;
139 }
140
141 static inline int kputsn(const char *p, int l, kstring_t *s)
142 {
143 if (s->l + l + 1 >= s->m) {
144 char *tmp;
145 s->m = s->l + l + 2;
146 kroundup32(s->m);
147 if ((tmp = (char*)realloc(s->s, s->m)))
148 s->s = tmp;
149 else
150 return EOF;
151 }
152 memcpy(s->s + s->l, p, l);
153 s->l += l;
154 s->s[s->l] = 0;
155 return l;
156 }
157
158 static inline int kputs(const char *p, kstring_t *s)
159 {
160 return kputsn(p, strlen(p), s);
161 }
162
163 static inline int kputc(int c, kstring_t *s)
164 {
165 if (s->l + 1 >= s->m) {
166 char *tmp;
167 s->m = s->l + 2;
168 kroundup32(s->m);
169 if ((tmp = (char*)realloc(s->s, s->m)))
170 s->s = tmp;
171 else
172 return EOF;
173 }
174 s->s[s->l++] = c;
175 s->s[s->l] = 0;
176 return c;
177 }
178
179 static inline int kputc_(int c, kstring_t *s)
180 {
181 if (s->l + 1 > s->m) {
182 char *tmp;
183 s->m = s->l + 1;
184 kroundup32(s->m);
185 if ((tmp = (char*)realloc(s->s, s->m)))
186 s->s = tmp;
187 else
188 return EOF;
189 }
190 s->s[s->l++] = c;
191 return 1;
192 }
193
194 static inline int kputsn_(const void *p, int l, kstring_t *s)
195 {
196 if (s->l + l > s->m) {
197 char *tmp;
198 s->m = s->l + l;
199 kroundup32(s->m);
200 if ((tmp = (char*)realloc(s->s, s->m)))
201 s->s = tmp;
202 else
203 return EOF;
204 }
205 memcpy(s->s + s->l, p, l);
206 s->l += l;
207 return l;
208 }
209
210 static inline int kputw(int c, kstring_t *s)
211 {
212 char buf[16];
213 int i, l = 0;
214 unsigned int x = c;
215 if (c < 0) x = -x;
216 do { buf[l++] = x%10 + '0'; x /= 10; } while (x > 0);
217 if (c < 0) buf[l++] = '-';
218 if (s->l + l + 1 >= s->m) {
219 char *tmp;
220 s->m = s->l + l + 2;
221 kroundup32(s->m);
222 if ((tmp = (char*)realloc(s->s, s->m)))
223 s->s = tmp;
224 else
225 return EOF;
226 }
227 for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i];
228 s->s[s->l] = 0;
229 return 0;
230 }
231
232 static inline int kputuw(unsigned c, kstring_t *s)
233 {
234 char buf[16];
235 int l, i;
236 unsigned x;
237 if (c == 0) return kputc('0', s);
238 for (l = 0, x = c; x > 0; x /= 10) buf[l++] = x%10 + '0';
239 if (s->l + l + 1 >= s->m) {
240 char *tmp;
241 s->m = s->l + l + 2;
242 kroundup32(s->m);
243 if ((tmp = (char*)realloc(s->s, s->m)))
244 s->s = tmp;
245 else
246 return EOF;
247 }
248 for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i];
249 s->s[s->l] = 0;
250 return 0;
251 }
252
253 static inline int kputl(long c, kstring_t *s)
254 {
255 char buf[32];
256 int i, l = 0;
257 unsigned long x = c;
258 if (c < 0) x = -x;
259 do { buf[l++] = x%10 + '0'; x /= 10; } while (x > 0);
260 if (c < 0) buf[l++] = '-';
261 if (s->l + l + 1 >= s->m) {
262 char *tmp;
263 s->m = s->l + l + 2;
264 kroundup32(s->m);
265 if ((tmp = (char*)realloc(s->s, s->m)))
266 s->s = tmp;
267 else
268 return EOF;
269 }
270 for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i];
271 s->s[s->l] = 0;
272 return 0;
273 }
274
275 /*
276 * Returns 's' split by delimiter, with *n being the number of components;
277 * NULL on failue.
278 */
279 static inline int *ksplit(kstring_t *s, int delimiter, int *n)
280 {
281 int max = 0, *offsets = 0;
282 *n = ksplit_core(s->s, delimiter, &max, &offsets);
283 return offsets;
284 }
285
286 #endif