Commit c436f0357b473d0e4a58a3f895190887f7e3626d

Authored by m-holger
1 parent 31073ea3

Refactor `QPDFObjectHandle` and related classes to replace `Buffer` usage with `…

…std::string_view`, improving performance and code clarity.
include/qpdf/QPDF.hh
... ... @@ -95,7 +95,8 @@ class QPDF
95 95  
96 96 // Parse a PDF file loaded into a memory buffer. This works exactly like processFile except
97 97 // that the PDF file is in memory instead of on disk. The description appears in any warning or
98   - // error message in place of the file name.
  98 + // error message in place of the file name. The buffer is owned by the caller and must remain
  99 + // valid for the lifetime of the QPDF object.
99 100 QPDF_DLL
100 101 void processMemoryFile(
101 102 char const* description, char const* buf, size_t length, char const* password = nullptr);
... ...
include/qpdf/QPDFObjectHandle.hh
... ... @@ -1351,7 +1351,7 @@ class QPDFObjectHandle: public qpdf::BaseHandle
1351 1351 void setParsedOffset(qpdf_offset_t offset);
1352 1352 void parseContentStream_internal(std::string const& description, ParserCallbacks* callbacks);
1353 1353 static void parseContentStream_data(
1354   - std::shared_ptr<Buffer>,
  1354 + std::string_view stream_data,
1355 1355 std::string const& description,
1356 1356 ParserCallbacks* callbacks,
1357 1357 QPDF* context);
... ...
libqpdf/JSON.cc
... ... @@ -2,7 +2,7 @@
2 2  
3 3 #include <qpdf/JSON_writer.hh>
4 4  
5   -#include <qpdf/BufferInputSource.hh>
  5 +#include <qpdf/InputSource_private.hh>
6 6 #include <qpdf/Pl_Base64.hh>
7 7 #include <qpdf/Pl_Concatenate.hh>
8 8 #include <qpdf/Pl_String.hh>
... ... @@ -1348,7 +1348,7 @@ JSON::parse(InputSource&amp; is, Reactor* reactor)
1348 1348 JSON
1349 1349 JSON::parse(std::string const& s)
1350 1350 {
1351   - BufferInputSource bis("json input", s);
  1351 + is::OffsetBuffer bis("json input", s);
1352 1352 JSONParser jp(bis, nullptr);
1353 1353 return jp.parse();
1354 1354 }
... ...
libqpdf/Pl_QPDFTokenizer.cc
1 1 #include <qpdf/Pl_QPDFTokenizer.hh>
2 2  
3   -#include <qpdf/BufferInputSource.hh>
  3 +#include <qpdf/InputSource_private.hh>
  4 +#include <qpdf/Pipeline_private.hh>
4 5 #include <qpdf/QTC.hh>
  6 +
5 7 #include <stdexcept>
6 8  
  9 +using namespace qpdf;
  10 +
7 11 class Pl_QPDFTokenizer::Members
8 12 {
9 13 public:
... ... @@ -13,7 +17,8 @@ class Pl_QPDFTokenizer::Members
13 17  
14 18 QPDFObjectHandle::TokenFilter* filter{nullptr};
15 19 QPDFTokenizer tokenizer;
16   - Pl_Buffer buf{"tokenizer buffer"};
  20 + std::string buffer;
  21 + pl::String buf{"pl_tokenizer", nullptr, buffer};
17 22 };
18 23  
19 24 Pl_QPDFTokenizer::Pl_QPDFTokenizer(
... ... @@ -39,8 +44,7 @@ Pl_QPDFTokenizer::write(unsigned char const* data, size_t len)
39 44 void
40 45 Pl_QPDFTokenizer::finish()
41 46 {
42   - m->buf.finish();
43   - auto input = BufferInputSource("tokenizer data", m->buf.getBuffer(), true);
  47 + auto input = is::OffsetBuffer("tokenizer data", m->buffer);
44 48 std::string empty;
45 49 while (true) {
46 50 auto token = m->tokenizer.readToken(input, empty, true);
... ...
libqpdf/QPDF.cc
... ... @@ -11,7 +11,6 @@
11 11 #include <sstream>
12 12 #include <vector>
13 13  
14   -#include <qpdf/BufferInputSource.hh>
15 14 #include <qpdf/FileInputSource.hh>
16 15 #include <qpdf/InputSource_private.hh>
17 16 #include <qpdf/OffsetInputSource.hh>
... ... @@ -259,12 +258,8 @@ void
259 258 QPDF::processMemoryFile(
260 259 char const* description, char const* buf, size_t length, char const* password)
261 260 {
262   - processInputSource(
263   - std::shared_ptr<InputSource>(
264   - // line-break
265   - new BufferInputSource(
266   - description, new Buffer(QUtil::unsigned_char_pointer(buf), length), true)),
267   - password);
  261 + auto is = std::make_shared<is::OffsetBuffer>(description, std::string_view{buf, length});
  262 + processInputSource(is, password);
268 263 }
269 264  
270 265 void
... ...
libqpdf/QPDFObjectHandle.cc
... ... @@ -2,7 +2,6 @@
2 2  
3 3 #include <qpdf/QPDFObjectHandle_private.hh>
4 4  
5   -#include <qpdf/BufferInputSource.hh>
6 5 #include <qpdf/JSON_writer.hh>
7 6 #include <qpdf/Pipeline_private.hh>
8 7 #include <qpdf/Pl_Buffer.hh>
... ... @@ -1452,10 +1451,7 @@ QPDFObjectHandle
1452 1451 QPDFObjectHandle::parse(
1453 1452 QPDF* context, std::string const& object_str, std::string const& object_description)
1454 1453 {
1455   - // BufferInputSource does not modify the input, but Buffer either requires a string& or copies
1456   - // the string.
1457   - Buffer buf(const_cast<std::string&>(object_str));
1458   - auto input = BufferInputSource("parsed object", &buf);
  1454 + auto input = is::OffsetBuffer("parsed object", object_str);
1459 1455 auto result = QPDFParser::parse(input, object_description, context);
1460 1456 size_t offset = QIntC::to_size(input.tell());
1461 1457 while (offset < object_str.length()) {
... ... @@ -1549,11 +1545,11 @@ void
1549 1545 QPDFObjectHandle::parseContentStream_internal(
1550 1546 std::string const& description, ParserCallbacks* callbacks)
1551 1547 {
1552   - Pl_Buffer buf("concatenated stream data buffer");
  1548 + std::string stream_data;
  1549 + pl::String buf(stream_data);
1553 1550 std::string all_description;
1554 1551 pipeContentStreams(&buf, description, all_description);
1555   - auto stream_data = buf.getBufferSharedPointer();
1556   - callbacks->contentSize(stream_data->getSize());
  1552 + callbacks->contentSize(stream_data.size());
1557 1553 try {
1558 1554 parseContentStream_data(stream_data, all_description, callbacks, getOwningQPDF());
1559 1555 } catch (TerminateParsing&) {
... ... @@ -1564,13 +1560,13 @@ QPDFObjectHandle::parseContentStream_internal(
1564 1560  
1565 1561 void
1566 1562 QPDFObjectHandle::parseContentStream_data(
1567   - std::shared_ptr<Buffer> stream_data,
  1563 + std::string_view stream_data,
1568 1564 std::string const& description,
1569 1565 ParserCallbacks* callbacks,
1570 1566 QPDF* context)
1571 1567 {
1572   - size_t stream_length = stream_data->getSize();
1573   - auto input = BufferInputSource(description, stream_data.get());
  1568 + size_t stream_length = stream_data.size();
  1569 + auto input = is::OffsetBuffer(description, stream_data);
1574 1570 Tokenizer tokenizer;
1575 1571 tokenizer.allowEOF();
1576 1572 auto sp_description = QPDFParser::make_description(description, "content");
... ...
libqpdf/QPDFParser.cc
1 1 #include <qpdf/QPDFParser.hh>
2 2  
3   -#include <qpdf/BufferInputSource.hh>
4 3 #include <qpdf/QPDF.hh>
5 4 #include <qpdf/QPDFObjGen.hh>
6 5 #include <qpdf/QPDFObjectHandle.hh>
... ...
libqpdf/QPDF_objects.cc
... ... @@ -12,7 +12,6 @@
12 12 #include <vector>
13 13  
14 14 #include <qpdf/BufferInputSource.hh>
15   -#include <qpdf/FileInputSource.hh>
16 15 #include <qpdf/InputSource_private.hh>
17 16 #include <qpdf/OffsetInputSource.hh>
18 17 #include <qpdf/Pipeline.hh>
... ...