Debian Codegraph @

debian/missing-sources/htslib/kstring.h

raw symbols SCIP
1 /* The MIT License
2
3 Copyright (C) 2011 by Attractive Chaos <[email protected]>
4 Copyright (C) 2013-2014, 2016, 2018-2019 Genome Research Ltd.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25 */
26
27 #ifndef KSTRING_H
28 #define KSTRING_H
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <limits.h>
36 #include <sys/types.h>
37
38 #include "hts_defs.h"
39
40 #ifndef kroundup32
41 #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
42 #endif
43
44 #ifndef kroundup_size_t
45 #define kroundup_size_t(x) (--(x), \
46 (x)|=(x)>>(sizeof(size_t)/8), /* 0 or 1 */ \
47 (x)|=(x)>>(sizeof(size_t)/4), /* 1 or 2 */ \
48 (x)|=(x)>>(sizeof(size_t)/2), /* 2 or 4 */ \
49 (x)|=(x)>>(sizeof(size_t)), /* 4 or 8 */ \
50 (x)|=(x)>>(sizeof(size_t)*2), /* 8 or 16 */ \
51 (x)|=(x)>>(sizeof(size_t)*4), /* 16 or 32 */ \
52 ++(x))
53 #endif
54
55 #if defined __GNUC__ && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4))
56 #ifdef __MINGW_PRINTF_FORMAT
57 #define KS_ATTR_PRINTF(fmt, arg) __attribute__((__format__ (__MINGW_PRINTF_FORMAT, fmt, arg)))
58 #else
59 #define KS_ATTR_PRINTF(fmt, arg) __attribute__((__format__ (__printf__, fmt, arg)))
60 #endif // __MINGW_PRINTF_FORMAT
61 #else
62 #define KS_ATTR_PRINTF(fmt, arg)
63 #endif
64
65 #ifndef HAVE___BUILTIN_CLZ
66 #if defined __GNUC__ && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
67 #define HAVE___BUILTIN_CLZ 1
68 #endif
69 #endif
70
71 /* kstring_t is a simple non-opaque type whose fields are likely to be
72 * used directly by user code (but see also ks_str() and ks_len() below).
73 * A kstring_t object is initialised by either of
74 * kstring_t str = KS_INITIALIZE;
75 * kstring_t str; ...; ks_initialize(&str);
76 * and either ownership of the underlying buffer should be given away before
77 * the object disappears (see ks_release() below) or the kstring_t should be
78 * destroyed with ks_free(&str) or free(str.s) */
79 #ifndef KSTRING_T
80 #define KSTRING_T kstring_t
81 typedef struct kstring_t {
82 size_t l, m;
83 char *s;
84 } kstring_t;
85 #endif
86
87 typedef struct {
88 uint64_t tab[4];
89 int sep, finished;
90 const char *p; // end of the current token
91 } ks_tokaux_t;
92
93 #ifdef __cplusplus
94 extern "C" {
95 #endif
96
97 HTSLIB_EXPORT
98 int kvsprintf(kstring_t *s, const char *fmt, va_list ap) KS_ATTR_PRINTF(2,0);
99
100 HTSLIB_EXPORT
101 int ksprintf(kstring_t *s, const char *fmt, ...) KS_ATTR_PRINTF(2,3);
102
103 HTSLIB_EXPORT
104 int kputd(double d, kstring_t *s); // custom %g only handler
105
106 HTSLIB_EXPORT
107 int ksplit_core(char *s, int delimiter, int *_max, int **_offsets);
108
109 HTSLIB_EXPORT
110 char *kstrstr(const char *str, const char *pat, int **_prep);
111
112 HTSLIB_EXPORT
113 char *kstrnstr(const char *str, const char *pat, int n, int **_prep);
114
115 HTSLIB_EXPORT
116 void *kmemmem(const void *_str, int n, const void *_pat, int m, int **_prep);
117
118 /* kstrtok() is similar to strtok_r() except that str is not
119 * modified and both str and sep can be NULL. For efficiency, it is
120 * actually recommended to set both to NULL in the subsequent calls
121 * if sep is not changed. */
122 HTSLIB_EXPORT
123 char *kstrtok(const char *str, const char *sep, ks_tokaux_t *aux);
124
125 /* kgetline() uses the supplied fgets()-like function to read a "\n"-
126 * or "\r\n"-terminated line from fp. The line read is appended to the
127 * kstring without its terminator and 0 is returned; EOF is returned at
128 * EOF or on error (determined by querying fp, as per fgets()). */
129 typedef char *kgets_func(char *, int, void *);
130 HTSLIB_EXPORT
131 int kgetline(kstring_t *s, kgets_func *fgets, void *fp);
132
133 // This matches the signature of hgetln(), apart from the last pointer
134 typedef ssize_t kgets_func2(char *, size_t, void *);
135 HTSLIB_EXPORT
136 int kgetline2(kstring_t *s, kgets_func2 *fgets, void *fp);
137
138 #ifdef __cplusplus
139 }
140 #endif
141
142 /// kstring initializer for structure assignment
143 #define KS_INITIALIZE { 0, 0, NULL }
144
145 /// kstring initializer for pointers
146 /**
147 @note Not to be used if the buffer has been allocated. Use ks_release()
148 or ks_clear() instead.
149 */
150
151 static inline void ks_initialize(kstring_t *s)
152 {
153 s->l = s->m = 0;
154 s->s = NULL;
155 }
156
157 /// Resize a kstring to a given capacity
158 static inline int ks_resize(kstring_t *s, size_t size)
159 {
160 if (s->m < size) {
161 char *tmp;
162 kroundup_size_t(size);
163 tmp = (char*)realloc(s->s, size);
164 if (!tmp && size)
165 return -1;
166 s->s = tmp;
167 s->m = size;
168 }
169 return 0;
170 }
171
172 /// Increase kstring capacity by a given number of bytes
173 static inline int ks_expand(kstring_t *s, size_t expansion)
174 {
175 size_t new_size = s->l + expansion;
176
177 if (new_size < s->l) // Overflow check
178 return -1;
179 return ks_resize(s, new_size);
180 }
181
182 /// Returns the kstring buffer
183 static inline char *ks_str(kstring_t *s)
184 {
185 return s->s;
186 }
187
188 /// Returns the kstring buffer, or an empty string if l == 0
189 /**
190 * Unlike ks_str(), this function will never return NULL. If the kstring is
191 * empty it will return a read-only empty string. As the returned value
192 * may be read-only, the caller should not attempt to modify it.
193 */
194 static inline const char *ks_c_str(kstring_t *s)
195 {
196 return s->l && s->s ? s->s : "";
197 }
198
199 static inline size_t ks_len(kstring_t *s)
200 {
201 return s->l;
202 }
203
204 /// Reset kstring length to zero
205 /**
206 @return The kstring itself
207
208 Example use: kputsn(string, len, ks_clear(s))
209 */
210 static inline kstring_t *ks_clear(kstring_t *s)
211 {
212 s->l = 0;
213 return s;
214 }
215
216 // Give ownership of the underlying buffer away to something else (making
217 // that something else responsible for freeing it), leaving the kstring_t
218 // empty and ready to be used again, or ready to go out of scope without
219 // needing free(str.s) to prevent a memory leak.
220 static inline char *ks_release(kstring_t *s)
221 {
222 char *ss = s->s;
223 s->l = s->m = 0;
224 s->s = NULL;
225 return ss;
226 }
227
228 /// Safely free the underlying buffer in a kstring.
229 static inline void ks_free(kstring_t *s)
230 {
231 if (s) {
232 free(s->s);
233 ks_initialize(s);
234 }
235 }
236
237 static inline int kputsn(const char *p, size_t l, kstring_t *s)
238 {
239 size_t new_sz = s->l + l + 2;
240 if (new_sz <= s->l || ks_resize(s, new_sz) < 0)
241 return EOF;
242 memcpy(s->s + s->l, p, l);
243 s->l += l;
244 s->s[s->l] = 0;
245 return l;
246 }
247
248 static inline int kputs(const char *p, kstring_t *s)
249 {
250 return kputsn(p, strlen(p), s);
251 }
252
253 static inline int kputc(int c, kstring_t *s)
254 {
255 if (ks_resize(s, s->l + 2) < 0)
256 return EOF;
257 s->s[s->l++] = c;
258 s->s[s->l] = 0;
259 return (unsigned char)c;
260 }
261
262 static inline int kputc_(int c, kstring_t *s)
263 {
264 if (ks_resize(s, s->l + 1) < 0)
265 return EOF;
266 s->s[s->l++] = c;
267 return 1;
268 }
269
270 static inline int kputsn_(const void *p, size_t l, kstring_t *s)
271 {
272 size_t new_sz = s->l + l;
273 if (new_sz < s->l || ks_resize(s, new_sz ? new_sz : 1) < 0)
274 return EOF;
275 memcpy(s->s + s->l, p, l);
276 s->l += l;
277 return l;
278 }
279
280 static inline int kputuw(unsigned x, kstring_t *s)
281 {
282 #if HAVE___BUILTIN_CLZ && UINT_MAX == 4294967295U
283 static const unsigned int kputuw_num_digits[32] = {
284 10, 10, 10, 9, 9, 9, 8, 8,
285 8, 7, 7, 7, 7, 6, 6, 6,
286 5, 5, 5, 4, 4, 4, 4, 3,
287 3, 3, 2, 2, 2, 1, 1, 1
288 };
289 static const unsigned int kputuw_thresholds[32] = {
290 0, 0, 1000000000U, 0, 0, 100000000U, 0, 0,
291 10000000, 0, 0, 0, 1000000, 0, 0, 100000,
292 0, 0, 10000, 0, 0, 0, 1000, 0,
293 0, 100, 0, 0, 10, 0, 0, 0
294 };
295 #else
296 uint64_t m;
297 #endif
298 static const char kputuw_dig2r[] =
299 "00010203040506070809"
300 "10111213141516171819"
301 "20212223242526272829"
302 "30313233343536373839"
303 "40414243444546474849"
304 "50515253545556575859"
305 "60616263646566676869"
306 "70717273747576777879"
307 "80818283848586878889"
308 "90919293949596979899";
309 unsigned int l, j;
310 char *cp;
311
312 // Trivial case - also prevents __builtin_clz(0), which is undefined
313 if (x < 10) {
314 if (ks_resize(s, s->l + 2) < 0)
315 return EOF;
316 s->s[s->l++] = '0'+x;
317 s->s[s->l] = 0;
318 return 0;
319 }
320
321 // Find out how many digits are to be printed.
322 #if HAVE___BUILTIN_CLZ && UINT_MAX == 4294967295U
323 /*
324 * Table method - should be quick if clz can be done in hardware.
325 * Find the most significant bit of the value to print and look
326 * up in a table to find out how many decimal digits are needed.
327 * This number needs to be adjusted by 1 for cases where the decimal
328 * length could vary for a given number of bits (for example,
329 * a four bit number could be between 8 and 15).
330 */
331
332 l = __builtin_clz(x);
333 l = kputuw_num_digits[l] - (x < kputuw_thresholds[l]);
334 #else
335 // Fallback for when clz is not available
336 m = 1;
337 l = 0;
338 do {
339 l++;
340 m *= 10;
341 } while (x >= m);
342 #endif
343
344 if (ks_resize(s, s->l + l + 2) < 0)
345 return EOF;
346
347 // Add digits two at a time
348 j = l;
349 cp = s->s + s->l;
350 while (x >= 10) {
351 const char *d = &kputuw_dig2r[2*(x%100)];
352 x /= 100;
353 memcpy(&cp[j-=2], d, 2);
354 }
355
356 // Last one (if necessary). We know that x < 10 by now.
357 if (j == 1)
358 cp[0] = x + '0';
359
360 s->l += l;
361 s->s[s->l] = 0;
362 return 0;
363 }
364
365 static inline int kputw(int c, kstring_t *s)
366 {
367 unsigned int x = c;
368 if (c < 0) {
369 x = -x;
370 if (ks_resize(s, s->l + 3) < 0)
371 return EOF;
372 s->s[s->l++] = '-';
373 }
374
375 return kputuw(x, s);
376 }
377
378 static inline int kputll(long long c, kstring_t *s)
379 {
380 char buf[32];
381 int i, l = 0;
382 unsigned long long x = c;
383 if (c < 0) x = -x;
384 do { buf[l++] = x%10 + '0'; x /= 10; } while (x > 0);
385 if (c < 0) buf[l++] = '-';
386 if (ks_resize(s, s->l + l + 2) < 0)
387 return EOF;
388 for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i];
389 s->s[s->l] = 0;
390 return 0;
391 }
392
393 static inline int kputl(long c, kstring_t *s) {
394 return kputll(c, s);
395 }
396
397 /*
398 * Returns 's' split by delimiter, with *n being the number of components;
399 * NULL on failue.
400 */
401 static inline int *ksplit(kstring_t *s, int delimiter, int *n)
402 {
403 int max = 0, *offsets = 0;
404 *n = ksplit_core(s->s, delimiter, &max, &offsets);
405 return offsets;
406 }
407
408 #endif