From 799080b0f9ae55aa675b4c935a702054e11cade4 Mon Sep 17 00:00:00 2001 From: m-holger Date: Sun, 20 Jul 2025 17:20:50 +0100 Subject: [PATCH] In FUTURE, refactor `BufferInputSource` to use is::OffsetBuffer. --- include/qpdf/BufferInputSource.hh | 8 ++++++++ libqpdf/BufferInputSource.cc | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/include/qpdf/BufferInputSource.hh b/include/qpdf/BufferInputSource.hh index 33adc5a..90cdcdc 100644 --- a/include/qpdf/BufferInputSource.hh +++ b/include/qpdf/BufferInputSource.hh @@ -23,6 +23,8 @@ #include #include +#include + class QPDF_DLL_CLASS BufferInputSource: public InputSource { public: @@ -52,11 +54,17 @@ class QPDF_DLL_CLASS BufferInputSource: public InputSource void unreadCh(char ch) override; private: +#ifndef QPDF_FUTURE bool own_memory; std::string description; Buffer* buf; qpdf_offset_t cur_offset; qpdf_offset_t max_offset; +#else + class Members; + + std::unique_ptr m; +#endif }; #endif // QPDF_BUFFERINPUTSOURCE_HH diff --git a/libqpdf/BufferInputSource.cc b/libqpdf/BufferInputSource.cc index 71390cf..1ea3a4d 100644 --- a/libqpdf/BufferInputSource.cc +++ b/libqpdf/BufferInputSource.cc @@ -10,6 +10,8 @@ using namespace qpdf; +#ifndef QPDF_FUTURE + BufferInputSource::BufferInputSource(std::string const& description, Buffer* buf, bool own_memory) : own_memory(own_memory), description(description), @@ -145,6 +147,86 @@ BufferInputSource::unreadCh(char ch) } } +#else + +class BufferInputSource::Members +{ + public: + Members(std::string const& description, Buffer* buf, bool own_memory) : + buf(own_memory ? buf : nullptr), + is(description, + buf && buf->getSize() > 0 + ? std::string_view(reinterpret_cast(buf->getBuffer()), buf->getSize()) + : std::string_view()) + { + } + + Members(std::string const& description, std::string const& str) : + content(str), + is(description, content) + { + } + + ~Members() = default; + + std::unique_ptr buf{nullptr}; + std::string content; + is::OffsetBuffer is; +}; + +BufferInputSource::BufferInputSource(std::string const& description, Buffer* buf, bool own_memory) : + m(std::make_unique(description, buf, own_memory)) +{ +} + +BufferInputSource::BufferInputSource(std::string const& description, std::string const& contents) : + m(std::make_unique(description, contents)) +{ +} +BufferInputSource::~BufferInputSource() = default; + +qpdf_offset_t +BufferInputSource::findAndSkipNextEOL() +{ + auto result = m->is.findAndSkipNextEOL(); + last_offset = m->is.getLastOffset(); + return result; +} +std::string const& +BufferInputSource::getName() const +{ + return m->is.getName(); +} +qpdf_offset_t +BufferInputSource::tell() +{ + return m->is.tell(); +} +void +BufferInputSource::seek(qpdf_offset_t offset, int whence) +{ + m->is.seek(offset, whence); +} +void +BufferInputSource::rewind() +{ + m->is.rewind(); +} +size_t +BufferInputSource::read(char* buffer, size_t length) +{ + auto result = m->is.read(buffer, length); + last_offset = m->is.getLastOffset(); + return result; +} +void +BufferInputSource::unreadCh(char ch) +{ + m->is.unreadCh(ch); +} + +#endif // QPDF_FUTURE + qpdf_offset_t is::OffsetBuffer::findAndSkipNextEOL() { @@ -161,7 +243,7 @@ is::OffsetBuffer::findAndSkipNextEOL() qpdf_offset_t result = 0; auto buffer = view_.begin(); auto end = view_.end(); - auto p = buffer + pos; + auto p = buffer + static_cast(pos); while (p < end && !(*p == '\r' || *p == '\n')) { ++p; -- libgit2 0.21.4