From 7e7cede8b1209e71b151cc872e647a2ba3d7eade Mon Sep 17 00:00:00 2001 From: m-holger Date: Wed, 4 Jun 2025 13:50:42 +0100 Subject: [PATCH] Refactor `is::OffsetBuffer` to rename `cur_offset` to `pos` and consolidate redundant methods for improved code clarity. --- libqpdf/BufferInputSource.cc | 46 +++++++++++++++++++++++----------------------- libqpdf/qpdf/InputSource_private.hh | 29 +++++++---------------------- 2 files changed, 30 insertions(+), 45 deletions(-) diff --git a/libqpdf/BufferInputSource.cc b/libqpdf/BufferInputSource.cc index af2c6bc..71390cf 100644 --- a/libqpdf/BufferInputSource.cc +++ b/libqpdf/BufferInputSource.cc @@ -146,57 +146,57 @@ BufferInputSource::unreadCh(char ch) } qpdf_offset_t -is::OffsetBuffer::findAndSkipNextEOL_internal() +is::OffsetBuffer::findAndSkipNextEOL() { - if (cur_offset < 0) { + if (pos < 0) { throw std::logic_error("INTERNAL ERROR: is::OffsetBuffer offset < 0"); } auto end_pos = static_cast(view_.size()); - if (cur_offset >= end_pos) { + if (pos >= end_pos) { last_offset = end_pos + global_offset; - cur_offset = end_pos; - return end_pos; + pos = end_pos; + return end_pos + global_offset; } qpdf_offset_t result = 0; auto buffer = view_.begin(); auto end = view_.end(); - auto p = buffer + cur_offset; + auto p = buffer + pos; while (p < end && !(*p == '\r' || *p == '\n')) { ++p; } if (p < end) { result = p - buffer; - cur_offset = result + 1; + pos = result + 1; ++p; - while (cur_offset < end_pos && (*p == '\r' || *p == '\n')) { + while (pos < end_pos && (*p == '\r' || *p == '\n')) { ++p; - ++cur_offset; + ++pos; } } else { - cur_offset = end_pos; + pos = end_pos; result = end_pos; } - return result; + return result + global_offset; } void -is::OffsetBuffer::seek_internal(qpdf_offset_t offset, int whence) +is::OffsetBuffer::seek(qpdf_offset_t offset, int whence) { switch (whence) { case SEEK_SET: - cur_offset = offset; + pos = offset - global_offset; break; case SEEK_END: QIntC::range_check(static_cast(view_.size()), offset); - cur_offset = static_cast(view_.size()) + offset; + pos = static_cast(view_.size()) + offset; break; case SEEK_CUR: - QIntC::range_check(cur_offset, offset); - cur_offset += offset; + QIntC::range_check(pos, offset); + pos += offset; break; default: @@ -204,7 +204,7 @@ is::OffsetBuffer::seek_internal(qpdf_offset_t offset, int whence) break; } - if (cur_offset < 0) { + if (pos < 0) { throw std::runtime_error(description + ": seek before beginning of buffer"); } } @@ -212,18 +212,18 @@ is::OffsetBuffer::seek_internal(qpdf_offset_t offset, int whence) size_t is::OffsetBuffer::read(char* buffer, size_t length) { - if (cur_offset < 0) { + if (pos < 0) { throw std::logic_error("INTERNAL ERROR: is::OffsetBuffer offset < 0"); } auto end_pos = static_cast(view_.size()); - if (cur_offset >= end_pos) { + if (pos >= end_pos) { last_offset = end_pos + global_offset; return 0; } - last_offset = cur_offset + global_offset; - size_t len = std::min(QIntC::to_size(end_pos - cur_offset), length); - memcpy(buffer, view_.data() + cur_offset, len); - cur_offset += QIntC::to_offset(len); + last_offset = pos + global_offset; + size_t len = std::min(QIntC::to_size(end_pos - pos), length); + memcpy(buffer, view_.data() + pos, len); + pos += QIntC::to_offset(len); return len; } diff --git a/libqpdf/qpdf/InputSource_private.hh b/libqpdf/qpdf/InputSource_private.hh index 800530a..a52bcb3 100644 --- a/libqpdf/qpdf/InputSource_private.hh +++ b/libqpdf/qpdf/InputSource_private.hh @@ -30,11 +30,7 @@ namespace qpdf::is ~OffsetBuffer() final = default; - qpdf_offset_t - findAndSkipNextEOL() final - { - return findAndSkipNextEOL_internal() + global_offset; - } + qpdf_offset_t findAndSkipNextEOL() final; std::string const& getName() const final @@ -45,23 +41,15 @@ namespace qpdf::is qpdf_offset_t tell() final { - return cur_offset + global_offset; + return pos + global_offset; } - void - seek(qpdf_offset_t offset, int whence) final - { - if (whence == SEEK_SET) { - seek_internal(offset - global_offset, whence); - } else { - seek_internal(offset, whence); - } - } + void seek(qpdf_offset_t offset, int whence) final; void rewind() final { - seek_internal(0, SEEK_SET); + pos = 0; } size_t read(char* buffer, size_t length) final; @@ -69,17 +57,14 @@ namespace qpdf::is void unreadCh(char ch) final { - if (cur_offset > 0) { - --cur_offset; + if (pos > 0) { + --pos; } } private: - qpdf_offset_t findAndSkipNextEOL_internal(); - void seek_internal(qpdf_offset_t offset, int whence); - std::string description; - qpdf_offset_t cur_offset{0}; + qpdf_offset_t pos{0}; std::string_view view_; qpdf_offset_t global_offset; }; -- libgit2 0.21.4