Commit 82c178b035042c5ef11cc6e96d635112856548a3
1 parent
388719d4
Add `Buffer::data()`, `empty()`, and `size()` methods with unit tests
Introduce new methods: `data()` to access buffer content, `empty()` to check buffer state, and `size()` to retrieve buffer size. Update unit tests to validate the behavior of these methods alongside existing functionality.
Showing
4 changed files
with
73 additions
and
4 deletions
include/qpdf/Buffer.hh
| ... | ... | @@ -76,6 +76,22 @@ class Buffer |
| 76 | 76 | QPDF_DLL |
| 77 | 77 | std::string_view view() const; |
| 78 | 78 | |
| 79 | + // Return a pointer to the data. NB: Unlike getBuffer, this method returns a valid pointer even | |
| 80 | + // if the Buffer is empty. | |
| 81 | + QPDF_DLL | |
| 82 | + char const* data() const; | |
| 83 | + | |
| 84 | + // Return a pointer to the data. NB: Unlike getBuffer, this method returns a valid pointer even | |
| 85 | + // if the Buffer is empty. | |
| 86 | + QPDF_DLL | |
| 87 | + char* data(); | |
| 88 | + | |
| 89 | + QPDF_DLL | |
| 90 | + bool empty() const; | |
| 91 | + | |
| 92 | + QPDF_DLL | |
| 93 | + size_t size() const; | |
| 94 | + | |
| 79 | 95 | private: |
| 80 | 96 | class Members; |
| 81 | 97 | ... | ... |
libqpdf/Buffer.cc
| ... | ... | @@ -117,3 +117,27 @@ Buffer::view() const |
| 117 | 117 | } |
| 118 | 118 | return {m->buf, m->size}; |
| 119 | 119 | } |
| 120 | + | |
| 121 | +char const* | |
| 122 | +Buffer::data() const | |
| 123 | +{ | |
| 124 | + return m->buf ? m->buf : m->str.data(); | |
| 125 | +} | |
| 126 | + | |
| 127 | +char* | |
| 128 | +Buffer::data() | |
| 129 | +{ | |
| 130 | + return m->buf ? m->buf : m->str.data(); | |
| 131 | +} | |
| 132 | + | |
| 133 | +bool | |
| 134 | +Buffer::empty() const | |
| 135 | +{ | |
| 136 | + return m->size == 0; | |
| 137 | +} | |
| 138 | + | |
| 139 | +size_t | |
| 140 | +Buffer::size() const | |
| 141 | +{ | |
| 142 | + return m->size; | |
| 143 | +} | ... | ... |
libtests/buffer.cc
| ... | ... | @@ -35,13 +35,36 @@ main() |
| 35 | 35 | assert(bc2p[0] == 'R'); |
| 36 | 36 | assert(bc2p[1] == 'W'); |
| 37 | 37 | |
| 38 | - // Test move and view method | |
| 38 | + // Test move, view, data, empty and size methods | |
| 39 | 39 | assert(bc1.view() == "RW"); |
| 40 | + assert(std::string_view(bc1.data(), bc1.getSize()) == "RW"); | |
| 41 | + *bc1.data() = 'Z'; | |
| 42 | + assert(!bc1.empty()); | |
| 43 | + assert(bc1.size() == 2); | |
| 40 | 44 | auto s1 = bc1.move(); |
| 41 | - assert(bc1.getBuffer() == nullptr); | |
| 45 | + assert(bc1.empty()); | |
| 46 | + assert(bc1.size() == 0); | |
| 47 | + assert(!bc1.getBuffer()); | |
| 42 | 48 | assert(bc1.getSize() == 0); |
| 43 | - assert(s1 == "RW"); | |
| 49 | + assert(s1 == "ZW"); | |
| 44 | 50 | assert(bc1.view().empty()); |
| 51 | + assert(bc1.data()); | |
| 52 | + assert(Buffer(s1).move() == "ZW"); | |
| 53 | + assert(s1 == "ZW"); | |
| 54 | + | |
| 55 | + // Test const methods | |
| 56 | + const Buffer cb(s1); | |
| 57 | + assert(*cb.data() == 'Z'); | |
| 58 | + assert(*(cb.getBuffer() + 1) == 'W'); | |
| 59 | + | |
| 60 | + // Test constructors | |
| 61 | + assert(Buffer().empty()); | |
| 62 | + assert(Buffer().copy().empty()); | |
| 63 | + assert(!Buffer().getBuffer()); | |
| 64 | + assert(Buffer().data()); | |
| 65 | + assert(Buffer().move().empty()); | |
| 66 | + assert(Buffer(s1).size() == 2); | |
| 67 | + assert(*Buffer(s1).data() == 'Z'); | |
| 45 | 68 | |
| 46 | 69 | // Test Buffer(std:string&&) |
| 47 | 70 | Buffer bc3("QW"); | ... | ... |
manual/release-notes.rst
| ... | ... | @@ -38,9 +38,15 @@ more detail. |
| 38 | 38 | overhead of repeatedly validating the underlying document structure |
| 39 | 39 | and/or building internal caches. If the underlying document structure |
| 40 | 40 | is directly modified (without the use of DocumentHelpers), the |
| 41 | - ``validate`` methods revalidates the structure and resynchronizes any | |
| 41 | + ``validate`` methods revalidate the structure and resynchronize any | |
| 42 | 42 | internal caches. |
| 43 | 43 | |
| 44 | + - Add new ``Buffer`` methods ``move``, ``view``, ``data``, ``size`` and | |
| 45 | + ``empty``. The new methods present the ``Buffer`` as a ``char`` (rather | |
| 46 | + than ``unsigned char``) container and facilitate the efficient moving | |
| 47 | + of its content into a `std::string``. | |
| 48 | + | |
| 49 | + | |
| 44 | 50 | - CLI Enhancements |
| 45 | 51 | |
| 46 | 52 | - Disallow option :qpdf:ref:`--deterministic-id` to be used together | ... | ... |