Commit 388719d42ef290d025fb10c9e03e40b98d2b728c

Authored by m-holger
1 parent 6df06185

Add `Buffer::view()` method and update unit tests

Implement `Buffer::view()` to provide a read-only view of the buffer content. Update unit tests to validate `view()` behavior alongside `move()`. Include necessary `#include <string_view>` for functionality.
include/qpdf/Buffer.hh
... ... @@ -25,6 +25,7 @@
25 25 #include <cstddef>
26 26 #include <memory>
27 27 #include <string>
  28 +#include <string_view>
28 29  
29 30 class Buffer
30 31 {
... ... @@ -71,6 +72,10 @@ class Buffer
71 72 QPDF_DLL
72 73 std::string move();
73 74  
  75 + // Return a string_view to the data.
  76 + QPDF_DLL
  77 + std::string_view view() const;
  78 +
74 79 private:
75 80 class Members;
76 81  
... ...
libqpdf/Buffer.cc
... ... @@ -108,3 +108,12 @@ Buffer::move()
108 108 }
109 109 return {m->buf, m->size};
110 110 }
  111 +
  112 +std::string_view
  113 +Buffer::view() const
  114 +{
  115 + if (!m->buf) {
  116 + return {};
  117 + }
  118 + return {m->buf, m->size};
  119 +}
... ...
libtests/buffer.cc
... ... @@ -35,11 +35,13 @@ main()
35 35 assert(bc2p[0] == 'R');
36 36 assert(bc2p[1] == 'W');
37 37  
38   - // Test move method
  38 + // Test move and view method
  39 + assert(bc1.view() == "RW");
39 40 auto s1 = bc1.move();
40 41 assert(bc1.getBuffer() == nullptr);
41 42 assert(bc1.getSize() == 0);
42 43 assert(s1 == "RW");
  44 + assert(bc1.view().empty());
43 45  
44 46 // Test Buffer(std:string&&)
45 47 Buffer bc3("QW");
... ...