2
1
mirror of https://github.com/qpdf/qpdf.git synced 2025-02-05 05:18:25 +00:00

Refactor InputSource::readLine

This commit is contained in:
m-holger 2024-11-02 12:12:39 +00:00
parent c502abbc98
commit fa2d331215

View File

@ -5,6 +5,8 @@
#include <cstring> #include <cstring>
#include <stdexcept> #include <stdexcept>
using namespace std::literals;
void void
InputSource::setLastOffset(qpdf_offset_t offset) InputSource::setLastOffset(qpdf_offset_t offset)
{ {
@ -25,19 +27,20 @@ InputSource::readLine(size_t max_line_length)
// After this is called, the file will be positioned after a line terminator or at the end of // After this is called, the file will be positioned after a line terminator or at the end of
// the file, and last_offset will point to position the file had when this method was called. // the file, and last_offset will point to position the file had when this method was called.
qpdf_offset_t offset = this->tell(); auto result = read(max_line_length);
auto bp = std::make_unique<char[]>(max_line_length + 1); auto eol = result.find_first_of("\n\r"sv);
char* buf = bp.get(); if (eol != std::string::npos) {
memset(buf, '\0', max_line_length + 1); auto next_line = result.find_first_not_of("\n\r"sv, eol);
this->read(buf, max_line_length); result.resize(eol);
this->seek(offset, SEEK_SET); if (eol != std::string::npos) {
qpdf_offset_t eol = this->findAndSkipNextEOL(); seek(last_offset + static_cast<qpdf_offset_t>(next_line), SEEK_SET);
this->last_offset = offset; return result;
size_t line_length = QIntC::to_size(eol - offset); }
if (line_length < max_line_length) {
buf[line_length] = '\0';
} }
return {buf}; // We did not necessarily find the end of the trailing newline sequence.
seek(last_offset, SEEK_SET);
findAndSkipNextEOL();
return result;
} }
bool bool