zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit e3f0ba4984f932cd3a9a99504d540b4cd8393364 (tree)
parent 6d0a122816fabec6e7ef212c743fa22d7f38433e
Author: Andrew Kelley <superjoe30@gmail.com>
Date:   Thu, 13 Sep 2018 11:24:57 -0400

alternate fix using the rest() function

Diffstat:
Msrc/cache_hash.cpp | 16+++-------------
Msrc/util.cpp | 10++++++++++
Msrc/util.hpp | 3++-
3 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/src/cache_hash.cpp b/src/cache_hash.cpp @@ -337,22 +337,12 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) { return ErrorInvalidFormat; } - // We should be at the last item, - // so switch from iterating on spaces ' ' to newlines '\n' - // First, check to make sure we have the runway to do so: - if (it.index == it.buffer.len) { + Slice<uint8_t> file_path = SplitIterator_rest(&it); + if (file_path.len == 0) { os_file_close(ch->manifest_file); return ErrorInvalidFormat; } - it.index++; - // Too close for missiles, I’m switching to guns - it.split_bytes = str("\n"); - Optional<Slice<uint8_t>> opt_file_path = SplitIterator_next(&it); - if (!opt_file_path.is_some) { - os_file_close(ch->manifest_file); - return ErrorInvalidFormat; - } - Buf *this_path = buf_create_from_slice(opt_file_path.value); + Buf *this_path = buf_create_from_slice(file_path); if (chf->path != nullptr && !buf_eql_buf(this_path, chf->path)) { os_file_close(ch->manifest_file); return ErrorInvalidFormat; diff --git a/src/util.cpp b/src/util.cpp @@ -79,6 +79,16 @@ Optional<Slice<uint8_t>> SplitIterator_next(SplitIterator *self) { } // Ported from std/mem.zig +Slice<uint8_t> SplitIterator_rest(SplitIterator *self) { + // move to beginning of token + size_t index = self->index; + while (index < self->buffer.len && SplitIterator_isSplitByte(self, self->buffer.ptr[index])) { + index += 1; + } + return self->buffer.sliceFrom(index); +} + +// Ported from std/mem.zig SplitIterator memSplit(Slice<uint8_t> buffer, Slice<uint8_t> split_bytes) { return SplitIterator{0, buffer, split_bytes}; } diff --git a/src/util.hpp b/src/util.hpp @@ -263,7 +263,8 @@ struct SplitIterator { }; bool SplitIterator_isSplitByte(SplitIterator *self, uint8_t byte); -Optional<Slice<uint8_t>> SplitIterator_next(SplitIterator *self); +Optional< Slice<uint8_t> > SplitIterator_next(SplitIterator *self); +Slice<uint8_t> SplitIterator_rest(SplitIterator *self); SplitIterator memSplit(Slice<uint8_t> buffer, Slice<uint8_t> split_bytes); #endif