An optimized version of forward() with length == 1.

This commit is contained in:
Ferdinand Majerech 2014-08-05 20:52:05 +02:00
parent 34e6f55bd9
commit 92396b4cae

View file

@ -291,7 +291,7 @@ final class Reader
/// Move current position forward. /// Move current position forward.
/// ///
/// Params: length = Number of characters to move position forward. /// Params: length = Number of characters to move position forward.
void forward(size_t length = 1) @safe pure nothrow @nogc void forward(size_t length) @safe pure nothrow @nogc
{ {
mixin FastCharSearch!"\n\u0085\u2028\u2029"d search; mixin FastCharSearch!"\n\u0085\u2028\u2029"d search;
@ -345,6 +345,49 @@ final class Reader
lastDecodedCharOffset_ = 0; lastDecodedCharOffset_ = 0;
} }
/// Move current position forward by one character.
void forward() @trusted pure nothrow @nogc
{
++charIndex_;
lastDecodedBufferOffset_ = bufferOffset_;
lastDecodedCharOffset_ = 0;
// ASCII
if(upcomingASCII_ > 0)
{
--upcomingASCII_;
const c = buffer_[bufferOffset_++];
if(c == '\n' || (c == '\r' && buffer_[bufferOffset_] != '\n'))
{
++line_;
column_ = 0;
return;
}
++column_;
return;
}
// UTF-8
mixin FastCharSearch!"\n\u0085\u2028\u2029"d search;
assert(bufferOffset_ < buffer_.length,
"Attempted to decode past the end of YAML buffer");
assert(buffer_[bufferOffset_] >= 0x80,
"ASCII must be handled by preceding code");
const c = decodeValidUTF8NoGC(buffer_, bufferOffset_);
// New line. (can compare with '\n' without decoding since it's ASCII)
if(search.canFind(c) || (c == '\r' && buffer_[bufferOffset_] != '\n'))
{
++line_;
column_ = 0;
}
else if(c != '\uFEFF') { ++column_; }
checkASCII();
}
/// Used to build slices of read data in Reader; to avoid allocations. /// Used to build slices of read data in Reader; to avoid allocations.
SliceBuilder sliceBuilder; SliceBuilder sliceBuilder;