diff options
Diffstat (limited to 'src/scanner.c')
| -rw-r--r-- | src/scanner.c | 137 |
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 | |||
| 8 | void | ||
| 9 | scanner__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 | |||
| 17 | void | ||
| 18 | mmap_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 | |||
| 26 | void | ||
| 27 | scanner__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 | |||
| 34 | struct string_view | ||
| 35 | scanner__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 | |||
| 48 | void | ||
| 49 | scanner__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 | |||
| 57 | struct string_view | ||
| 58 | scanner__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 | |||
| 71 | void | ||
| 72 | scanner__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 | |||
| 81 | struct string_view | ||
| 82 | scanner__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 | |||
| 95 | uint64_t | ||
| 96 | scanner__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 | |||
| 113 | int64_t | ||
| 114 | scanner__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 | |||
| 130 | bool | ||
| 131 | scanner__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 | ||
