diff options
Diffstat (limited to 'src/util.cpp')
| -rw-r--r-- | src/util.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/util.cpp b/src/util.cpp index 9a6a382993..65b1fe3082 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -86,6 +86,32 @@ Optional<Slice<uint8_t>> SplitIterator_next(SplitIterator *self) { return Optional<Slice<uint8_t>>::some(self->buffer.slice(start, end)); } +// Ported from std/mem.zig. +// This one won't collapse multiple separators into one, so you could use it, for example, +// to parse Comma Separated Value format. +Optional<Slice<uint8_t>> SplitIterator_next_separate(SplitIterator *self) { + // move to beginning of token + if (self->index < self->buffer.len && + SplitIterator_isSplitByte(self, self->buffer.ptr[self->index])) + { + self->index += 1; + } + size_t start = self->index; + if (start == self->buffer.len) { + return {}; + } + + // move to end of token + while (self->index < self->buffer.len && + !SplitIterator_isSplitByte(self, self->buffer.ptr[self->index])) + { + self->index += 1; + } + size_t end = self->index; + + return Optional<Slice<uint8_t>>::some(self->buffer.slice(start, end)); +} + // Ported from std/mem.zig Slice<uint8_t> SplitIterator_rest(SplitIterator *self) { // move to beginning of token |
