diff --git a/include/qpdf/Buffer.hh b/include/qpdf/Buffer.hh index 06e4706..d667c1a 100644 --- a/include/qpdf/Buffer.hh +++ b/include/qpdf/Buffer.hh @@ -76,6 +76,22 @@ class Buffer QPDF_DLL std::string_view view() const; + // Return a pointer to the data. NB: Unlike getBuffer, this method returns a valid pointer even + // if the Buffer is empty. + QPDF_DLL + char const* data() const; + + // Return a pointer to the data. NB: Unlike getBuffer, this method returns a valid pointer even + // if the Buffer is empty. + QPDF_DLL + char* data(); + + QPDF_DLL + bool empty() const; + + QPDF_DLL + size_t size() const; + private: class Members; diff --git a/libqpdf/Buffer.cc b/libqpdf/Buffer.cc index 14d6b8e..5971b25 100644 --- a/libqpdf/Buffer.cc +++ b/libqpdf/Buffer.cc @@ -117,3 +117,27 @@ Buffer::view() const } return {m->buf, m->size}; } + +char const* +Buffer::data() const +{ + return m->buf ? m->buf : m->str.data(); +} + +char* +Buffer::data() +{ + return m->buf ? m->buf : m->str.data(); +} + +bool +Buffer::empty() const +{ + return m->size == 0; +} + +size_t +Buffer::size() const +{ + return m->size; +} diff --git a/libtests/buffer.cc b/libtests/buffer.cc index 29e7428..f092255 100644 --- a/libtests/buffer.cc +++ b/libtests/buffer.cc @@ -35,13 +35,36 @@ main() assert(bc2p[0] == 'R'); assert(bc2p[1] == 'W'); - // Test move and view method + // Test move, view, data, empty and size methods assert(bc1.view() == "RW"); + assert(std::string_view(bc1.data(), bc1.getSize()) == "RW"); + *bc1.data() = 'Z'; + assert(!bc1.empty()); + assert(bc1.size() == 2); auto s1 = bc1.move(); - assert(bc1.getBuffer() == nullptr); + assert(bc1.empty()); + assert(bc1.size() == 0); + assert(!bc1.getBuffer()); assert(bc1.getSize() == 0); - assert(s1 == "RW"); + assert(s1 == "ZW"); assert(bc1.view().empty()); + assert(bc1.data()); + assert(Buffer(s1).move() == "ZW"); + assert(s1 == "ZW"); + + // Test const methods + const Buffer cb(s1); + assert(*cb.data() == 'Z'); + assert(*(cb.getBuffer() + 1) == 'W'); + + // Test constructors + assert(Buffer().empty()); + assert(Buffer().copy().empty()); + assert(!Buffer().getBuffer()); + assert(Buffer().data()); + assert(Buffer().move().empty()); + assert(Buffer(s1).size() == 2); + assert(*Buffer(s1).data() == 'Z'); // Test Buffer(std:string&&) Buffer bc3("QW"); diff --git a/manual/release-notes.rst b/manual/release-notes.rst index baff92c..a68d42f 100644 --- a/manual/release-notes.rst +++ b/manual/release-notes.rst @@ -38,9 +38,15 @@ more detail. overhead of repeatedly validating the underlying document structure and/or building internal caches. If the underlying document structure is directly modified (without the use of DocumentHelpers), the - ``validate`` methods revalidates the structure and resynchronizes any + ``validate`` methods revalidate the structure and resynchronize any internal caches. + - Add new ``Buffer`` methods ``move``, ``view``, ``data``, ``size`` and + ``empty``. The new methods present the ``Buffer`` as a ``char`` (rather + than ``unsigned char``) container and facilitate the efficient moving + of its content into a `std::string``. + + - CLI Enhancements - Disallow option :qpdf:ref:`--deterministic-id` to be used together