Commit 0c9b6fe96407307857f0c6ff128401e39167fb6b

Authored by m-holger
1 parent 81cb7962

Refactor `is::OffsetBuffer` to use `std::string_view` instead of `Buffer` simplifying code.

libqpdf/BufferInputSource.cc
@@ -151,7 +151,7 @@ is::OffsetBuffer::findAndSkipNextEOL_internal() @@ -151,7 +151,7 @@ is::OffsetBuffer::findAndSkipNextEOL_internal()
151 if (cur_offset < 0) { 151 if (cur_offset < 0) {
152 throw std::logic_error("INTERNAL ERROR: is::OffsetBuffer offset < 0"); 152 throw std::logic_error("INTERNAL ERROR: is::OffsetBuffer offset < 0");
153 } 153 }
154 - qpdf_offset_t end_pos = max_offset; 154 + auto end_pos = static_cast<qpdf_offset_t>(view_.size());
155 if (cur_offset >= end_pos) { 155 if (cur_offset >= end_pos) {
156 last_offset = end_pos + global_offset; 156 last_offset = end_pos + global_offset;
157 cur_offset = end_pos; 157 cur_offset = end_pos;
@@ -159,9 +159,9 @@ is::OffsetBuffer::findAndSkipNextEOL_internal() @@ -159,9 +159,9 @@ is::OffsetBuffer::findAndSkipNextEOL_internal()
159 } 159 }
160 160
161 qpdf_offset_t result = 0; 161 qpdf_offset_t result = 0;
162 - unsigned char const* buffer = buf->getBuffer();  
163 - unsigned char const* end = buffer + end_pos;  
164 - unsigned char const* p = buffer + cur_offset; 162 + auto buffer = view_.begin();
  163 + auto end = view_.end();
  164 + auto p = buffer + cur_offset;
165 165
166 while (p < end && !(*p == '\r' || *p == '\n')) { 166 while (p < end && !(*p == '\r' || *p == '\n')) {
167 ++p; 167 ++p;
@@ -190,8 +190,8 @@ is::OffsetBuffer::seek_internal(qpdf_offset_t offset, int whence) @@ -190,8 +190,8 @@ is::OffsetBuffer::seek_internal(qpdf_offset_t offset, int whence)
190 break; 190 break;
191 191
192 case SEEK_END: 192 case SEEK_END:
193 - QIntC::range_check(max_offset, offset);  
194 - cur_offset = max_offset + offset; 193 + QIntC::range_check(static_cast<qpdf_offset_t>(view_.size()), offset);
  194 + cur_offset = static_cast<qpdf_offset_t>(view_.size()) + offset;
195 break; 195 break;
196 196
197 case SEEK_CUR: 197 case SEEK_CUR:
@@ -215,7 +215,7 @@ is::OffsetBuffer::read(char* buffer, size_t length) @@ -215,7 +215,7 @@ is::OffsetBuffer::read(char* buffer, size_t length)
215 if (cur_offset < 0) { 215 if (cur_offset < 0) {
216 throw std::logic_error("INTERNAL ERROR: is::OffsetBuffer offset < 0"); 216 throw std::logic_error("INTERNAL ERROR: is::OffsetBuffer offset < 0");
217 } 217 }
218 - qpdf_offset_t end_pos = max_offset; 218 + auto end_pos = static_cast<qpdf_offset_t>(view_.size());
219 if (cur_offset >= end_pos) { 219 if (cur_offset >= end_pos) {
220 last_offset = end_pos + global_offset; 220 last_offset = end_pos + global_offset;
221 return 0; 221 return 0;
@@ -223,7 +223,7 @@ is::OffsetBuffer::read(char* buffer, size_t length) @@ -223,7 +223,7 @@ is::OffsetBuffer::read(char* buffer, size_t length)
223 223
224 last_offset = cur_offset + global_offset; 224 last_offset = cur_offset + global_offset;
225 size_t len = std::min(QIntC::to_size(end_pos - cur_offset), length); 225 size_t len = std::min(QIntC::to_size(end_pos - cur_offset), length);
226 - memcpy(buffer, buf->getBuffer() + cur_offset, len); 226 + memcpy(buffer, view_.data() + cur_offset, len);
227 cur_offset += QIntC::to_offset(len); 227 cur_offset += QIntC::to_offset(len);
228 return len; 228 return len;
229 } 229 }
libqpdf/qpdf/InputSource_private.hh
@@ -15,8 +15,11 @@ namespace qpdf::is @@ -15,8 +15,11 @@ namespace qpdf::is
15 public: 15 public:
16 OffsetBuffer(std::string const& description, Buffer* buf, qpdf_offset_t global_offset) : 16 OffsetBuffer(std::string const& description, Buffer* buf, qpdf_offset_t global_offset) :
17 description(description), 17 description(description),
18 - buf(buf),  
19 - max_offset(buf ? static_cast<qpdf_offset_t>(buf->getSize()) : 0), 18 + view_(
  19 + buf && buf->getSize()
  20 + ? std::string_view(
  21 + reinterpret_cast<const char*>(buf->getBuffer()), buf->getSize())
  22 + : std::string_view()),
20 global_offset(global_offset) 23 global_offset(global_offset)
21 { 24 {
22 if (global_offset < 0) { 25 if (global_offset < 0) {
@@ -76,9 +79,8 @@ namespace qpdf::is @@ -76,9 +79,8 @@ namespace qpdf::is
76 void seek_internal(qpdf_offset_t offset, int whence); 79 void seek_internal(qpdf_offset_t offset, int whence);
77 80
78 std::string description; 81 std::string description;
79 - Buffer* buf;  
80 qpdf_offset_t cur_offset{0}; 82 qpdf_offset_t cur_offset{0};
81 - qpdf_offset_t max_offset; 83 + std::string_view view_;
82 qpdf_offset_t global_offset; 84 qpdf_offset_t global_offset;
83 }; 85 };
84 86