klib
[Ported from github.com/attractivechaos/klib] added block arena
Brought by drakestapleton
klib / kstring.h
| 1 | /* The MIT License |
| 2 | |
| 3 | Copyright (c) by Attractive Chaos <attractor@live.co.uk> |
| 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 | #ifndef KSTRING_T |
| 55 | #define KSTRING_T kstring_t |
| 56 | typedef struct __kstring_t { |
| 57 | size_t l, m; |
| 58 | char *s; |
| 59 | } kstring_t; |
| 60 | #endif |
| 61 | |
| 62 | typedef struct { |
| 63 | uint64_t tab[4]; |
| 64 | int sep, finished; |
| 65 | const char *p; // end of the current token |
| 66 | } ks_tokaux_t; |
| 67 | |
| 68 | #ifdef __cplusplus |
| 69 | extern "C" { |
| 70 | #endif |
| 71 | |
| 72 | int kvsprintf(kstring_t *s, const char *fmt, va_list ap) KS_ATTR_PRINTF(2,0); |
| 73 | int ksprintf(kstring_t *s, const char *fmt, ...) KS_ATTR_PRINTF(2,3); |
| 74 | int ksplit_core(char *s, int delimiter, int *_max, int **_offsets); |
| 75 | char *kstrstr(const char *str, const char *pat, int **_prep); |
| 76 | char *kstrnstr(const char *str, const char *pat, int n, int **_prep); |
| 77 | void *kmemmem(const void *_str, int n, const void *_pat, int m, int **_prep); |
| 78 | |
| 79 | /* kstrtok() is similar to strtok_r() except that str is not |
| 80 | * modified and both str and sep can be NULL. For efficiency, it is |
| 81 | * actually recommended to set both to NULL in the subsequent calls |
| 82 | * if sep is not changed. */ |
| 83 | char *kstrtok(const char *str, const char *sep, ks_tokaux_t *aux); |
| 84 | |
| 85 | /* kgetline() uses the supplied fgets()-like function to read a "\n"- |
| 86 | * or "\r\n"-terminated line from fp. The line read is appended to the |
| 87 | * kstring without its terminator and 0 is returned; EOF is returned at |
| 88 | * EOF or on error (determined by querying fp, as per fgets()). */ |
| 89 | typedef char *kgets_func(char *, int, void *); |
| 90 | int kgetline(kstring_t *s, kgets_func *fgets, void *fp); |
| 91 | |
| 92 | #ifdef __cplusplus |
| 93 | } |
| 94 | #endif |
| 95 | |
| 96 | static inline int ks_resize(kstring_t *s, size_t size) |
| 97 | { |
| 98 | if (s->m < size) { |
| 99 | char *tmp; |
| 100 | s->m = size; |
| 101 | kroundup32(s->m); |
| 102 | if ((tmp = (char*)realloc(s->s, s->m))) |
| 103 | s->s = tmp; |
| 104 | else |
| 105 | return -1; |
| 106 | } |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static inline char *ks_str(kstring_t *s) |
| 111 | { |
| 112 | return s->s; |
| 113 | } |
| 114 | |
| 115 | static inline size_t ks_len(kstring_t *s) |
| 116 | { |
| 117 | return s->l; |
| 118 | } |
| 119 | |
| 120 | // Give ownership of the underlying buffer away to something else (making |
| 121 | // that something else responsible for freeing it), leaving the kstring_t |
| 122 | // empty and ready to be used again, or ready to go out of scope without |
| 123 | // needing free(str.s) to prevent a memory leak. |
| 124 | static inline char *ks_release(kstring_t *s) |
| 125 | { |
| 126 | char *ss = s->s; |
| 127 | s->l = s->m = 0; |
| 128 | s->s = NULL; |
| 129 | return ss; |
| 130 | } |
| 131 | |
| 132 | static inline int kputsn(const char *p, int l, kstring_t *s) |
| 133 | { |
| 134 | if (s->l + l + 1 >= s->m) { |
| 135 | char *tmp; |
| 136 | s->m = s->l + l + 2; |
| 137 | kroundup32(s->m); |
| 138 | if ((tmp = (char*)realloc(s->s, s->m))) |
| 139 | s->s = tmp; |
| 140 | else |
| 141 | return EOF; |
| 142 | } |
| 143 | memcpy(s->s + s->l, p, l); |
| 144 | s->l += l; |
| 145 | s->s[s->l] = 0; |
| 146 | return l; |
| 147 | } |
| 148 | |
| 149 | static inline int kputs(const char *p, kstring_t *s) |
| 150 | { |
| 151 | return kputsn(p, strlen(p), s); |
| 152 | } |
| 153 | |
| 154 | static inline int kputc(int c, kstring_t *s) |
| 155 | { |
| 156 | if (s->l + 1 >= s->m) { |
| 157 | char *tmp; |
| 158 | s->m = s->l + 2; |
| 159 | kroundup32(s->m); |
| 160 | if ((tmp = (char*)realloc(s->s, s->m))) |
| 161 | s->s = tmp; |
| 162 | else |
| 163 | return EOF; |
| 164 | } |
| 165 | s->s[s->l++] = c; |
| 166 | s->s[s->l] = 0; |
| 167 | return c; |
| 168 | } |
| 169 | |
| 170 | static inline int kputc_(int c, kstring_t *s) |
| 171 | { |
| 172 | if (s->l + 1 > s->m) { |
| 173 | char *tmp; |
| 174 | s->m = s->l + 1; |
| 175 | kroundup32(s->m); |
| 176 | if ((tmp = (char*)realloc(s->s, s->m))) |
| 177 | s->s = tmp; |
| 178 | else |
| 179 | return EOF; |
| 180 | } |
| 181 | s->s[s->l++] = c; |
| 182 | return 1; |
| 183 | } |
| 184 | |
| 185 | static inline int kputsn_(const void *p, int l, kstring_t *s) |
| 186 | { |
| 187 | if (s->l + l > s->m) { |
| 188 | char *tmp; |
| 189 | s->m = s->l + l; |
| 190 | kroundup32(s->m); |
| 191 | if ((tmp = (char*)realloc(s->s, s->m))) |
| 192 | s->s = tmp; |
| 193 | else |
| 194 | return EOF; |
| 195 | } |
| 196 | memcpy(s->s + s->l, p, l); |
| 197 | s->l += l; |
| 198 | return l; |
| 199 | } |
| 200 | |
| 201 | static inline int kputw(int c, kstring_t *s) |
| 202 | { |
| 203 | char buf[16]; |
| 204 | int i, l = 0; |
| 205 | unsigned int x = c; |
| 206 | if (c < 0) x = -x; |
| 207 | do { buf[l++] = x%10 + '0'; x /= 10; } while (x > 0); |
| 208 | if (c < 0) buf[l++] = '-'; |
| 209 | if (s->l + l + 1 >= s->m) { |
| 210 | char *tmp; |
| 211 | s->m = s->l + l + 2; |
| 212 | kroundup32(s->m); |
| 213 | if ((tmp = (char*)realloc(s->s, s->m))) |
| 214 | s->s = tmp; |
| 215 | else |
| 216 | return EOF; |
| 217 | } |
| 218 | for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i]; |
| 219 | s->s[s->l] = 0; |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static inline int kputuw(unsigned c, kstring_t *s) |
| 224 | { |
| 225 | char buf[16]; |
| 226 | int l, i; |
| 227 | unsigned x; |
| 228 | if (c == 0) return kputc('0', s); |
| 229 | for (l = 0, x = c; x > 0; x /= 10) buf[l++] = x%10 + '0'; |
| 230 | if (s->l + l + 1 >= s->m) { |
| 231 | char *tmp; |
| 232 | s->m = s->l + l + 2; |
| 233 | kroundup32(s->m); |
| 234 | if ((tmp = (char*)realloc(s->s, s->m))) |
| 235 | s->s = tmp; |
| 236 | else |
| 237 | return EOF; |
| 238 | } |
| 239 | for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i]; |
| 240 | s->s[s->l] = 0; |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static inline int kputl(long c, kstring_t *s) |
| 245 | { |
| 246 | char buf[32]; |
| 247 | int i, l = 0; |
| 248 | unsigned long x = c; |
| 249 | if (c < 0) x = -x; |
| 250 | do { buf[l++] = x%10 + '0'; x /= 10; } while (x > 0); |
| 251 | if (c < 0) buf[l++] = '-'; |
| 252 | if (s->l + l + 1 >= s->m) { |
| 253 | char *tmp; |
| 254 | s->m = s->l + l + 2; |
| 255 | kroundup32(s->m); |
| 256 | if ((tmp = (char*)realloc(s->s, s->m))) |
| 257 | s->s = tmp; |
| 258 | else |
| 259 | return EOF; |
| 260 | } |
| 261 | for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i]; |
| 262 | s->s[s->l] = 0; |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * Returns 's' split by delimiter, with *n being the number of components; |
| 268 | * NULL on failue. |
| 269 | */ |
| 270 | static inline int *ksplit(kstring_t *s, int delimiter, int *n) |
| 271 | { |
| 272 | int max = 0, *offsets = 0; |
| 273 | *n = ksplit_core(s->s, delimiter, &max, &offsets); |
| 274 | return offsets; |
| 275 | } |
| 276 | |
| 277 | #endif |