Debian Codegraph @

lib/util/tokenize.h

raw
1 /// \file
2 /// \brief String tokenization
3 /// \ingroup cgraph_utils
4 ///
5 /// This is essentially equivalent to `strtok` but with two main improvements:
6 ///
7 /// 1. The input string is not modified. This means, if you have a `const`
8 /// string, you do not need to `strdup` it in order to tokenize it. This
9 /// (combined with other properties like no opaque struct pointers) enables
10 /// you to tokenize a string with no heap allocation.
11 ///
12 /// 2. No global state. All the state for tokenization is contained in the
13 /// `tok_t` struct.
14 ///
15 /// The above two properties are intended to make string tokenization scalable
16 /// (no locks, no thread-shared state) and transparent to the compiler (a good
17 /// optimizing compiler implements all the string.h functions we use as
18 /// built-ins and, if `separators` is a compile-time literal, can typically
19 /// flatten everything into a tight loop with no function calls).
20 ///
21 /// Sample usage:
22 ///
23 /// const char my_input[] = "foo; bar:/baz";
24 /// for (tok_t t = tok(my_input, ";:/"); !tok_end(&t); tok_next(&t)) {
25 /// strview_t s = tok_get(&t);
26 /// printf("%.*s\n", (int)s.size, s.data);
27 /// }
28 /// // prints “foo”, “ bar”, “baz”
29
30 #include <assert.h>
31 #include <stddef.h>
32 #include <string.h>
33 #include <util/strview.h>
34
35 /// state for an in-progress string tokenization
36 typedef struct {
37 const char *start; ///< start of the string being scanned
38 const char *separators; ///< characters to treat as token separators
39 strview_t next; ///< next token to yield
40 } tok_t;
41
42 /// begin tokenization of a new string
43 static inline tok_t tok(const char *input, const char *separators) {
44
45 assert(input != NULL);
46 assert(separators != NULL);
47 assert(strcmp(separators, "") != 0 &&
48 "at least one separator must be provided");
49
50 #ifndef NDEBUG
51 for (const char *s1 = separators; *s1 != '\0'; ++s1) {
52 for (const char *s2 = s1 + 1; *s2 != '\0'; ++s2) {
53 assert(*s1 != *s2 && "duplicate separator characters");
54 }
55 }
56 #endif
57
58 tok_t t = {.start = input, .separators = separators};
59
60 // find the end of the first token
61 size_t size = strcspn(input, separators);
62 t.next = (strview_t){.data = input, .size = size};
63
64 return t;
65 }
66
67 /// is this tokenizer exhausted?
68 static inline bool tok_end(const tok_t *t) {
69
70 assert(t != NULL);
71
72 return t->next.data == NULL;
73 }
74
75 /// get the current token
76 static inline strview_t tok_get(const tok_t *t) {
77
78 assert(t != NULL);
79 assert(t->next.data != NULL && "extracting from an exhausted tokenizer");
80
81 return t->next;
82 }
83
84 /// advance to the next token in the string being scanned
85 static inline void tok_next(tok_t *t) {
86
87 assert(t != NULL);
88 assert(t->start != NULL);
89 assert(t->separators != NULL);
90 assert(t->next.data != NULL && "advancing an exhausted tokenizer");
91
92 // resume from where the previous token ended
93 const char *start = t->next.data + t->next.size;
94
95 // if we are at the end of the string, we are done
96 if (start == t->start + strlen(t->start)) {
97 t->next = (strview_t){0};
98 return;
99 }
100
101 // skip last separator characters
102 start += strspn(start, t->separators);
103
104 // find the end of the next token
105 size_t size = strcspn(start, t->separators);
106
107 t->next = (strview_t){.data = start, .size = size};
108 }