Refactored func attribs in Reader.

This commit is contained in:
Ferdinand Majerech 2014-08-05 23:00:23 +02:00
parent ada8335504
commit 0268a1ea39

View file

@ -124,6 +124,7 @@ final class Reader
checkASCII(); checkASCII();
} }
pure nothrow @nogc:
/// Get character at specified index relative to current position. /// Get character at specified index relative to current position.
/// ///
/// Params: index = Index of the character to get relative to current position /// Params: index = Index of the character to get relative to current position
@ -134,7 +135,7 @@ final class Reader
/// ///
// XXX removed; search for 'risky' to find why. // XXX removed; search for 'risky' to find why.
// Throws: ReaderException if trying to read past the end of the buffer. // Throws: ReaderException if trying to read past the end of the buffer.
dchar peek(const size_t index) @safe pure nothrow @nogc dchar peek(const size_t index) @safe
{ {
if(index < upcomingASCII_) { return buffer_[bufferOffset_ + index]; } if(index < upcomingASCII_) { return buffer_[bufferOffset_ + index]; }
if(characterCount_ <= charIndex_ + index) if(characterCount_ <= charIndex_ + index)
@ -177,7 +178,7 @@ final class Reader
} }
/// Optimized version of peek() for the case where peek index is 0. /// Optimized version of peek() for the case where peek index is 0.
dchar peek() @safe pure nothrow @nogc dchar peek() @safe
{ {
if(upcomingASCII_ > 0) { return buffer_[bufferOffset_]; } if(upcomingASCII_ > 0) { return buffer_[bufferOffset_]; }
if(characterCount_ <= charIndex_) { return '\0'; } if(characterCount_ <= charIndex_) { return '\0'; }
@ -194,13 +195,13 @@ final class Reader
/// case, '\0' will be returned. /// case, '\0' will be returned.
/// ///
/// Returns: Byte at specified position or '\0' if outside of the buffer. /// Returns: Byte at specified position or '\0' if outside of the buffer.
char peekByte(const size_t index) @safe pure nothrow @nogc char peekByte(const size_t index) @safe
{ {
return characterCount_ > (charIndex_ + index) ? buffer_[bufferOffset_ + index] : '\0'; return characterCount_ > (charIndex_ + index) ? buffer_[bufferOffset_ + index] : '\0';
} }
/// Optimized version of peekByte() for the case where peek byte index is 0. /// Optimized version of peekByte() for the case where peek byte index is 0.
char peekByte() @safe pure nothrow @nogc char peekByte() @safe
{ {
return characterCount_ > charIndex_ ? buffer_[bufferOffset_] : '\0'; return characterCount_ > charIndex_ ? buffer_[bufferOffset_] : '\0';
} }
@ -217,7 +218,7 @@ final class Reader
/// slice will be shorter. /// slice will be shorter.
/// ///
/// Returns: Characters starting at current position or an empty slice if out of bounds. /// Returns: Characters starting at current position or an empty slice if out of bounds.
char[] prefix(const size_t length) @safe pure nothrow @nogc char[] prefix(const size_t length) @safe
{ {
return slice(length); return slice(length);
} }
@ -233,7 +234,7 @@ final class Reader
/// this. /// this.
/// ///
/// Returns: Bytes starting at current position. /// Returns: Bytes starting at current position.
char[] prefixBytes(const size_t length) @safe pure nothrow @nogc char[] prefixBytes(const size_t length) @safe
{ {
assert(length == 0 || bufferOffset_ + length < buffer_.length, assert(length == 0 || bufferOffset_ + length < buffer_.length,
"prefixBytes out of bounds"); "prefixBytes out of bounds");
@ -250,7 +251,7 @@ final class Reader
/// be shorter. /// be shorter.
/// ///
/// Returns: Slice into the internal buffer or an empty slice if out of bounds. /// Returns: Slice into the internal buffer or an empty slice if out of bounds.
char[] slice(const size_t end) @safe pure nothrow @nogc char[] slice(const size_t end) @safe
{ {
// Fast path in case the caller has already peek()ed all the way to end. // Fast path in case the caller has already peek()ed all the way to end.
if(end == lastDecodedCharOffset_) if(end == lastDecodedCharOffset_)
@ -278,7 +279,7 @@ final class Reader
/// ///
/// Throws: ReaderException if trying to read past the end of the buffer /// Throws: ReaderException if trying to read past the end of the buffer
/// or if invalid data is read. /// or if invalid data is read.
dchar get() @safe pure nothrow @nogc dchar get() @safe
{ {
const result = peek(); const result = peek();
forward(); forward();
@ -290,7 +291,7 @@ final class Reader
/// Params: length = Number or characters (code points, not bytes) to get. /// Params: length = Number or characters (code points, not bytes) to get.
/// ///
/// Returns: Characters starting at current position. /// Returns: Characters starting at current position.
char[] get(const size_t length) @safe pure nothrow @nogc char[] get(const size_t length) @safe
{ {
auto result = slice(length); auto result = slice(length);
forward(length); forward(length);
@ -300,7 +301,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) @safe pure nothrow @nogc void forward(size_t length) @safe
{ {
mixin FastCharSearch!"\n\u0085\u2028\u2029"d search; mixin FastCharSearch!"\n\u0085\u2028\u2029"d search;
@ -355,7 +356,7 @@ final class Reader
} }
/// Move current position forward by one character. /// Move current position forward by one character.
void forward() @trusted pure nothrow @nogc void forward() @trusted
{ {
++charIndex_; ++charIndex_;
lastDecodedBufferOffset_ = bufferOffset_; lastDecodedBufferOffset_ = bufferOffset_;
@ -400,24 +401,25 @@ final class Reader
/// 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;
@safe pure nothrow @nogc:
/// Get a string describing current buffer position, used for error messages. /// Get a string describing current buffer position, used for error messages.
Mark mark() @safe pure nothrow const @nogc { return Mark(line_, column_); } Mark mark() const { return Mark(line_, column_); }
/// Get current line number. /// Get current line number.
uint line() @safe pure nothrow const @nogc { return line_; } uint line() const { return line_; }
/// Get current column number. /// Get current column number.
uint column() @safe pure nothrow const @nogc { return column_; } uint column() const { return column_; }
/// Get index of the current character in the buffer. /// Get index of the current character in the buffer.
size_t charIndex() @safe pure nothrow const @nogc { return charIndex_; } size_t charIndex() const { return charIndex_; }
/// Get encoding of the input buffer. /// Get encoding of the input buffer.
Encoding encoding() @safe pure nothrow const @nogc { return encoding_; } Encoding encoding() const { return encoding_; }
private: private:
// Update upcomingASCII_ (should be called forward()ing over a UTF-8 sequence) // Update upcomingASCII_ (should be called forward()ing over a UTF-8 sequence)
void checkASCII() @safe pure nothrow @nogc void checkASCII()
{ {
upcomingASCII_ = countASCII(buffer_[bufferOffset_ .. $]); upcomingASCII_ = countASCII(buffer_[bufferOffset_ .. $]);
} }
@ -426,7 +428,7 @@ private:
// lastDecodedCharOffset_/lastDecodedBufferOffset_ and update them. // lastDecodedCharOffset_/lastDecodedBufferOffset_ and update them.
// //
// Does not advance the buffer position. Used in peek() and slice(). // Does not advance the buffer position. Used in peek() and slice().
dchar decodeNext() @safe pure nothrow @nogc dchar decodeNext()
{ {
assert(lastDecodedBufferOffset_ < buffer_.length, assert(lastDecodedBufferOffset_ < buffer_.length,
"Attempted to decode past the end of YAML buffer"); "Attempted to decode past the end of YAML buffer");
@ -451,6 +453,7 @@ private:
/// See begin() documentation. /// See begin() documentation.
struct SliceBuilder struct SliceBuilder
{ {
pure nothrow @nogc:
private: private:
// No copying by the user. // No copying by the user.
@disable this(this); @disable this(this);
@ -471,7 +474,7 @@ private:
// The number of elements currently in endStack_. // The number of elements currently in endStack_.
size_t endStackUsed_ = 0; size_t endStackUsed_ = 0;
@safe pure nothrow const @nogc invariant() @safe const invariant()
{ {
if(!inProgress) { return; } if(!inProgress) { return; }
assert(end_ <= reader_.bufferOffset_, "Slice ends after buffer position"); assert(end_ <= reader_.bufferOffset_, "Slice ends after buffer position");
@ -479,7 +482,7 @@ private:
} }
// Is a slice currently being built? // Is a slice currently being built?
bool inProgress() @safe pure nothrow const @nogc bool inProgress() @safe const
{ {
assert(start_ == size_t.max ? end_ == size_t.max : end_ != size_t.max, assert(start_ == size_t.max ? end_ == size_t.max : end_ != size_t.max,
"start_/end_ are not consistent"); "start_/end_ are not consistent");
@ -497,7 +500,7 @@ public:
/// forward() move the position. E.g. it is valid to extend a slice by write()-ing /// forward() move the position. E.g. it is valid to extend a slice by write()-ing
/// a string just returned by get() - but not one returned by prefix() unless the /// a string just returned by get() - but not one returned by prefix() unless the
/// position has changed since the prefix() call. /// position has changed since the prefix() call.
void begin() @system pure nothrow @nogc void begin() @system
{ {
assert(!inProgress, "Beginning a slice while another slice is being built"); assert(!inProgress, "Beginning a slice while another slice is being built");
assert(endStackUsed_ == 0, "Slice stack not empty at slice begin"); assert(endStackUsed_ == 0, "Slice stack not empty at slice begin");
@ -513,7 +516,7 @@ public:
/// ///
/// Returns a string; once a slice is finished it is definitive that its contents /// Returns a string; once a slice is finished it is definitive that its contents
/// will not be changed. /// will not be changed.
char[] finish() @system pure nothrow @nogc char[] finish() @system
{ {
assert(inProgress, "finish called without begin"); assert(inProgress, "finish called without begin");
assert(endStackUsed_ == 0, "Finishing a slice with running transactions."); assert(endStackUsed_ == 0, "Finishing a slice with running transactions.");
@ -531,7 +534,7 @@ public:
/// end of the slice being built, the slice is extended (trivial operation). /// end of the slice being built, the slice is extended (trivial operation).
/// ///
/// See_Also: begin /// See_Also: begin
void write(char[] str) @system pure nothrow @nogc void write(char[] str) @system
{ {
assert(inProgress, "write called without begin"); assert(inProgress, "write called without begin");
assert(end_ <= reader_.bufferOffset_, assert(end_ <= reader_.bufferOffset_,
@ -558,7 +561,7 @@ public:
/// Data can only be written up to the current position in the Reader buffer. /// Data can only be written up to the current position in the Reader buffer.
/// ///
/// See_Also: begin /// See_Also: begin
void write(dchar c) @system pure nothrow @nogc void write(dchar c) @system
{ {
assert(inProgress, "write called without begin"); assert(inProgress, "write called without begin");
if(c < 0x80) if(c < 0x80)
@ -585,7 +588,7 @@ public:
/// position = Position to insert the character at in code units, not code points. /// position = Position to insert the character at in code units, not code points.
/// Must be less than slice length(); a previously returned length() /// Must be less than slice length(); a previously returned length()
/// can be used. /// can be used.
void insert(const dchar c, const size_t position) @system pure nothrow @nogc void insert(const dchar c, const size_t position) @system
{ {
assert(inProgress, "insert called without begin"); assert(inProgress, "insert called without begin");
assert(start_ + position <= end_, "Trying to insert after the end of the slice"); assert(start_ + position <= end_, "Trying to insert after the end of the slice");
@ -609,7 +612,7 @@ public:
} }
/// Get the current length of the slice. /// Get the current length of the slice.
size_t length() @safe pure nothrow const @nogc size_t length() @safe const
{ {
return end_ - start_; return end_ - start_;
} }
@ -619,6 +622,7 @@ public:
/// Can be used to save and revert back to slice state. /// Can be used to save and revert back to slice state.
struct Transaction struct Transaction
{ {
@system pure nothrow @nogc:
private: private:
// The slice builder affected by the transaction. // The slice builder affected by the transaction.
SliceBuilder* builder_ = null; SliceBuilder* builder_ = null;
@ -635,7 +639,7 @@ public:
/// ended either by commit()-ing or reverting through the destructor. /// ended either by commit()-ing or reverting through the destructor.
/// ///
/// Saves the current state of a slice. /// Saves the current state of a slice.
this(ref SliceBuilder builder) @system pure nothrow @nogc this(ref SliceBuilder builder)
{ {
builder_ = &builder; builder_ = &builder;
stackLevel_ = builder_.endStackUsed_; stackLevel_ = builder_.endStackUsed_;
@ -649,7 +653,7 @@ public:
/// ///
/// Does nothing for a default-initialized transaction (the transaction has not /// Does nothing for a default-initialized transaction (the transaction has not
/// been started yet). /// been started yet).
void commit() @system pure nothrow @nogc void commit()
{ {
assert(!committed_, "Can't commit a transaction more than once"); assert(!committed_, "Can't commit a transaction more than once");
@ -663,7 +667,7 @@ public:
/// Destroy the transaction and revert it if it hasn't been committed yet. /// Destroy the transaction and revert it if it hasn't been committed yet.
/// ///
/// Does nothing for a default-initialized transaction. /// Does nothing for a default-initialized transaction.
~this() @system pure nothrow @nogc ~this()
{ {
if(builder_ is null || committed_) { return; } if(builder_ is null || committed_) { return; }
assert(builder_.endStackUsed_ == stackLevel_ + 1, assert(builder_.endStackUsed_ == stackLevel_ + 1,
@ -677,7 +681,7 @@ private:
// Push the current end of the slice so we can revert to it if needed. // Push the current end of the slice so we can revert to it if needed.
// //
// Used by Transaction. // Used by Transaction.
void push() @system pure nothrow @nogc void push() @system
{ {
assert(inProgress, "push called without begin"); assert(inProgress, "push called without begin");
assert(endStackUsed_ < endStack_.length, "Slice stack overflow"); assert(endStackUsed_ < endStack_.length, "Slice stack overflow");
@ -688,7 +692,7 @@ private:
// value, reverting changes since the old end was pushed. // value, reverting changes since the old end was pushed.
// //
// Used by Transaction. // Used by Transaction.
void pop() @system pure nothrow @nogc void pop() @system
{ {
assert(inProgress, "pop called without begin"); assert(inProgress, "pop called without begin");
assert(endStackUsed_ > 0, "Trying to pop an empty slice stack"); assert(endStackUsed_ > 0, "Trying to pop an empty slice stack");
@ -699,7 +703,7 @@ private:
// changes made since pushing the old end. // changes made since pushing the old end.
// //
// Used by Transaction. // Used by Transaction.
void apply() @system pure nothrow @nogc void apply() @system
{ {
assert(inProgress, "apply called without begin"); assert(inProgress, "apply called without begin");
assert(endStackUsed_ > 0, "Trying to apply an empty slice stack"); assert(endStackUsed_ > 0, "Trying to apply an empty slice stack");