aboutsummaryrefslogtreecommitdiff
path: root/src/scanner.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/scanner.c')
-rw-r--r--src/scanner.c137
1 files changed, 137 insertions, 0 deletions
diff --git a/src/scanner.c b/src/scanner.c
new file mode 100644
index 0000000..8326eee
--- /dev/null
+++ b/src/scanner.c
@@ -0,0 +1,137 @@
1// scanner.c
2#include <string.h>
3#include <stdlib.h>
4
5#include "utils.h"
6#include "scanner.h"
7
8void
9scanner__init (struct scanner *self, struct string_view sv)
10{
11 ASSERT (sv.begin <= sv.end);
12
13 self->cursor = sv.begin;
14 self->end = sv.end;
15}
16
17void
18mmap_wholescanner__skip_until (struct scanner *self, char c)
19{
20 ASSERT (self->cursor <= self->end);
21
22 char *p = memchr (self->cursor, c, self->end - self->cursor);
23 self->cursor = p ? p : self->end;
24}
25
26void
27scanner__skip_over (struct scanner *self, char c)
28{
29 scanner__skip_until (self, c);
30 if (!scanner__reached_end (self))
31 self->cursor += 1;
32}
33
34struct string_view
35scanner__get_until (struct scanner *self, char c)
36{
37 ASSERT (self->cursor <= self->end);
38
39 struct string_view sv;
40
41 sv.begin = self->cursor;
42 scanner__skip_until (self, c);
43 sv.end = self->cursor;
44
45 return sv;
46}
47
48void
49scanner__skip_until_one_of (struct scanner *self, char *str)
50{
51 ASSERT (self->cursor <= self->end);
52
53 while (self->cursor != self->end && strchr (str, *self->cursor) == NULL)
54 self->cursor += 1;
55}
56
57struct string_view
58scanner__get_until_one_of (struct scanner *self, char *str)
59{
60 ASSERT (self->cursor <= self->end);
61
62 struct string_view sv;
63
64 sv.begin = self->cursor;
65 scanner__skip_until_one_of (self, str);
66 sv.end = self->cursor;
67
68 return sv;
69}
70
71void
72scanner__skip_n (struct scanner *self, size_t n)
73{
74 ASSERT (self->cursor <= self->end);
75
76 self->cursor += n;
77 if (self->cursor > self->end)
78 self->cursor = self->end;
79}
80
81struct string_view
82scanner__get_n (struct scanner *self, size_t n)
83{
84 ASSERT (self->cursor <= self->end);
85
86 struct string_view sv;
87
88 sv.begin = self->cursor;
89 scanner__skip_n (self, n);
90 sv.end = self->cursor;
91
92 return sv;
93}
94
95uint64_t
96scanner__get_uint64 (struct scanner *self)
97{
98 ASSERT (self->cursor <= self->end);
99
100 uint64_t val = 0;
101 while ((unsigned) *self->cursor - '0' < 10) {
102 val *= 10;
103 val += *self->cursor - '0';
104 self->cursor += 1;
105 ASSERT (self->cursor < self->end);
106 }
107
108 ASSERT (self->cursor <= self->end);
109
110 return val;
111}
112
113int64_t
114scanner__get_int64 (struct scanner *self)
115{
116 ASSERT (self->cursor <= self->end);
117
118 int64_t negative = 1;
119 if (*self->cursor == '-') {
120 negative = -1;
121 self->cursor += 1;
122 }
123 int64_t val = scanner__get_uint64 (self);
124
125 ASSERT (self->cursor <= self->end);
126
127 return negative*val;
128}
129
130bool
131scanner__reached_end (struct scanner *self)
132{
133 ASSERT (self->cursor <= self->end);
134
135 return self->cursor == self->end;
136}
137// scanner.c ends here