From 173fc842c4eb1da6ca07a4ab6026c4c62bc8c09b Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 9 Sep 2018 18:07:11 -0400 Subject: basic compiler id hash working --- src/util.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'src/util.cpp') diff --git a/src/util.cpp b/src/util.cpp index cafd2c3d85..060d7f8fb5 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -43,3 +43,42 @@ uint32_t ptr_hash(const void *ptr) { bool ptr_eq(const void *a, const void *b) { return a == b; } + +// Ported from std/mem.zig. +bool SplitIterator_isSplitByte(SplitIterator *self, uint8_t byte) { + for (size_t i = 0; i < self->split_bytes.len; i += 1) { + if (byte == self->split_bytes.ptr[i]) { + return true; + } + } + return false; +} + +// Ported from std/mem.zig. +Optional> SplitIterator_next(SplitIterator *self) { + // move to beginning of token + while (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>::some(self->buffer.slice(start, end)); +} + +// Ported from std/mem.zig +SplitIterator memSplit(Slice buffer, Slice split_bytes) { + return SplitIterator{0, buffer, split_bytes}; +} -- cgit v1.2.3 From e3f0ba4984f932cd3a9a99504d540b4cd8393364 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 13 Sep 2018 11:24:57 -0400 Subject: alternate fix using the rest() function --- src/cache_hash.cpp | 16 +++------------- src/util.cpp | 10 ++++++++++ src/util.hpp | 3 ++- 3 files changed, 15 insertions(+), 14 deletions(-) (limited to 'src/util.cpp') diff --git a/src/cache_hash.cpp b/src/cache_hash.cpp index dcf15d16b9..5b1f55686a 100644 --- 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 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> 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 index 060d7f8fb5..192d74e766 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -78,6 +78,16 @@ Optional> SplitIterator_next(SplitIterator *self) { return Optional>::some(self->buffer.slice(start, end)); } +// Ported from std/mem.zig +Slice 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 buffer, Slice split_bytes) { return SplitIterator{0, buffer, split_bytes}; diff --git a/src/util.hpp b/src/util.hpp index f18c369fb5..fa5804efb5 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -263,7 +263,8 @@ struct SplitIterator { }; bool SplitIterator_isSplitByte(SplitIterator *self, uint8_t byte); -Optional> SplitIterator_next(SplitIterator *self); +Optional< Slice > SplitIterator_next(SplitIterator *self); +Slice SplitIterator_rest(SplitIterator *self); SplitIterator memSplit(Slice buffer, Slice split_bytes); #endif -- cgit v1.2.3