Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
tokenize.h
Go to the documentation of this file.
1
29
30#include <assert.h>
31#include <stddef.h>
32#include <string.h>
33#include <util/strview.h>
34
36typedef struct {
37 const char *start;
38 const char *separators;
40} tok_t;
41
43static 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
68static inline bool tok_end(const tok_t *t) {
69
70 assert(t != NULL);
71
72 return t->next.data == NULL;
73}
74
76static 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
85static 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}
node NULL
Definition grammar.y:163
NEATOPROCS_API void s1(graph_t *, node_t *)
Definition stuff.c:671
a non-owning string reference
Definition strview.h:20
const char * data
start of the pointed to string
Definition strview.h:21
size_t size
extent of the string in bytes
Definition strview.h:22
state for an in-progress string tokenization
Definition tokenize.h:36
strview_t next
next token to yield
Definition tokenize.h:39
const char * separators
characters to treat as token separators
Definition tokenize.h:38
const char * start
start of the string being scanned
Definition tokenize.h:37
Non-owning string references.
static strview_t tok_get(const tok_t *t)
get the current token
Definition tokenize.h:76
static tok_t tok(const char *input, const char *separators)
begin tokenization of a new string
Definition tokenize.h:43
static bool tok_end(const tok_t *t)
is this tokenizer exhausted?
Definition tokenize.h:68
static void tok_next(tok_t *t)
advance to the next token in the string being scanned
Definition tokenize.h:85