From 709ab00e4404970760a4d6398b35e309639ec58e Mon Sep 17 00:00:00 2001 From: Ferdinand Majerech Date: Tue, 29 Jul 2014 02:58:04 +0200 Subject: [PATCH] A UTF-8 slice(). --- source/dyaml/reader.d | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/source/dyaml/reader.d b/source/dyaml/reader.d index 2a272e4..e8f09a4 100644 --- a/source/dyaml/reader.d +++ b/source/dyaml/reader.d @@ -220,6 +220,26 @@ final class Reader return buffer_[start .. end]; } + char[] slice8(const size_t end) @safe pure nothrow @nogc + { + // Fast path in case the caller has already peek()ed all the way to end. + if(end == lastDecodedCharOffset_) + { + return buffer8_[bufferOffset8_ .. lastDecodedBufferOffset_]; + } + + lastDecodedCharOffset_ = 0; + lastDecodedBufferOffset_ = bufferOffset8_; + + // 'Slow' path - decode everything up to end. + while(lastDecodedCharOffset_ < end && + lastDecodedBufferOffset_ < buffer8_.length) + { + decodeNext(); + } + + return buffer8_[bufferOffset8_ .. lastDecodedBufferOffset_]; + } /// Get the next character, moving buffer position beyond it. ///