Commit cf3264b27bc8123dd9310043d28df23fab703c08

Authored by m-holger
1 parent d494aaac

Refactor QPDFWriter::writeObjectStreamOffsets

Rather than converting each (sequential) object id to a string,
generate a string for the first id and than increment the digits in the
string.
libqpdf/QPDFWriter.cc
... ... @@ -19,6 +19,7 @@
19 19 #include <qpdf/QTC.hh>
20 20 #include <qpdf/QUtil.hh>
21 21 #include <qpdf/RC4.hh>
  22 +#include <qpdf/Util.hh>
22 23  
23 24 #include <algorithm>
24 25 #include <cstdlib>
... ... @@ -1624,14 +1625,19 @@ QPDFWriter::unparseObject(
1624 1625 void
1625 1626 QPDFWriter::writeObjectStreamOffsets(std::vector<qpdf_offset_t>& offsets, int first_obj)
1626 1627 {
1627   - for (size_t i = 0; i < offsets.size(); ++i) {
1628   - if (i != 0) {
  1628 + qpdf_assert_debug(first_obj > 0);
  1629 + bool is_first = true;
  1630 + auto id = std::to_string(first_obj) + ' ';
  1631 + for (auto& offset: offsets) {
  1632 + if (is_first) {
  1633 + is_first = false;
  1634 + } else {
1629 1635 writeStringQDF("\n");
1630 1636 writeStringNoQDF(" ");
1631 1637 }
1632   - writeString(std::to_string(i + QIntC::to_size(first_obj)));
1633   - writeString(" ");
1634   - writeString(std::to_string(offsets.at(i)));
  1638 + writeString(id);
  1639 + util::increment(id, 1);
  1640 + writeString(std::to_string(offset));
1635 1641 }
1636 1642 writeString("\n");
1637 1643 }
... ...
libqpdf/qpdf/Util.hh
... ... @@ -44,6 +44,21 @@ namespace qpdf::util
44 44 return {'#', hexchars[static_cast<unsigned char>(c) >> 4], hexchars[c & 0x0f]};
45 45 }
46 46  
  47 + // Numerically increment a digit string. Ignore the last 'tail' characters.
  48 + inline void
  49 + increment(std::string& s, int tail = 0)
  50 + {
  51 + auto end = s.rend();
  52 + for (auto it = s.rbegin() + tail; it != end; ++it) {
  53 + ++*it;
  54 + if (*it != ':') {
  55 + return;
  56 + }
  57 + *it = '0';
  58 + }
  59 + s.insert(0, 1, '1');
  60 + }
  61 +
47 62 } // namespace qpdf::util
48 63  
49 64 #endif // UTIL_HH
... ...