From 4c5ecbe39bf17ebd1c663c863cef5094f53caf86 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Sat, 24 Jan 2015 03:07:00 +0100 Subject: Added seek function to CharReader, improved getOffset --- src/core/common/CharReader.cpp | 21 ++++++++++++++++++--- src/core/common/CharReader.hpp | 22 ++++++++++++++++------ 2 files changed, 34 insertions(+), 9 deletions(-) (limited to 'src/core/common') diff --git a/src/core/common/CharReader.cpp b/src/core/common/CharReader.cpp index 6fd3d45..07482ff 100644 --- a/src/core/common/CharReader.cpp +++ b/src/core/common/CharReader.cpp @@ -502,16 +502,31 @@ size_t CharReader::readRaw(char *buf, size_t size) return res; } +size_t CharReader::seek(size_t requestedOffset) +{ + // Fetch the current offset + const ssize_t currentOffs = getOffset(); + const ssize_t relativeOffs = requestedOffset - currentOffs; + + // Perform the actual seeking, move the peek cursor to the read cursor + const ssize_t reachedOffs = currentOffs + buffer->moveCursor(readCursor, relativeOffs); + buffer->copyCursor(readCursor, peekCursor); + coherent = true; + + // Clamp to values larger or equal to zero + return reachedOffs < 0 ? 0 : reachedOffs; +} + bool CharReader::atEnd() const { return buffer->atEnd(readCursor); } -SourceOffset CharReader::getOffset() const +size_t CharReader::getOffset() const { return buffer->offset(readCursor) + offs; } -SourcePosition CharReader::getPosition() const +size_t CharReader::getPeekOffset() const { - return getOffset(); + return buffer->offset(readCursor) + offs; } SourceLocation CharReader::getLocation() const diff --git a/src/core/common/CharReader.hpp b/src/core/common/CharReader.hpp index 5a4d906..cbd7b74 100644 --- a/src/core/common/CharReader.hpp +++ b/src/core/common/CharReader.hpp @@ -297,8 +297,7 @@ public: * @param relativeOffs is a positive or negative integer number specifying * the number of bytes the cursor should be moved forward (positive numbers) * or backwards (negative numbers). - * @return the actual number of bytes the cursor was moved. This number is - * smaller than the relativeOffs given in the constructor if the + * @return the actual number of bytes the cursor was moved. */ ssize_t moveCursor(CursorId cursor, ssize_t relativeOffs); @@ -522,6 +521,17 @@ public: */ size_t readRaw(char *buf, size_t size); + /** + * Moves read and peek cursor to the requested offset. Returns the offset + * that was actually reached. + * + * @param requestedOffset is the requested offset. This offset may no longer + * be reachable by the CharReader. + * @return the actually reached offset. The operation was successful, if + * the requested and reached offset are equal. + */ + size_t seek(size_t requestedOffset); + /** * Returns true if there are no more characters as the stream was closed. * @@ -534,14 +544,14 @@ public: * * @return the offset of the read cursor in bytes. */ - SourceOffset getOffset() const; + size_t getOffset() const; /** - * Returns the offset of the read cursor in bytes. + * Returns the offset of the peek cursor in bytes. * - * @return the offset of the read cursor in bytes. + * @return the offset of the peek cursor in bytes. */ - SourcePosition getPosition() const; + size_t getPeekOffset() const; /** * Returns a SourceLocation object describing the exact position (including -- cgit v1.2.3