diff options
author | Jay Berkenbilt <ejb@ql.org> | 2022-05-21 13:41:09 +0200 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2022-05-21 14:43:21 +0200 |
commit | 47c093c48b7ac3eb97c33b8edfafdf89685cffc7 (patch) | |
tree | 94f9f9922dc5dffaaf2ac73e4f30b8832e9d6403 /libqpdf/QPDF.cc | |
parent | 9b2eb01e2505e301ce95d31f5387fea0de35eff0 (diff) | |
download | qpdf-47c093c48b7ac3eb97c33b8edfafdf89685cffc7.tar.zst |
Replace std::regex with validators for better performance
Diffstat (limited to 'libqpdf/QPDF.cc')
-rw-r--r-- | libqpdf/QPDF.cc | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/libqpdf/QPDF.cc b/libqpdf/QPDF.cc index c353bc4c..2b79e6e3 100644 --- a/libqpdf/QPDF.cc +++ b/libqpdf/QPDF.cc @@ -385,20 +385,8 @@ QPDF::numWarnings() const } bool -QPDF::findHeader() +QPDF::validatePDFVersion(char const*& p, std::string& version) { - qpdf_offset_t global_offset = this->m->file->tell(); - std::string line = this->m->file->readLine(1024); - char const* p = line.c_str(); - if (strncmp(p, "%PDF-", 5) != 0) { - throw std::logic_error("findHeader is not looking at %PDF-"); - } - p += 5; - std::string version; - // Note: The string returned by line.c_str() is always - // null-terminated. The code below never overruns the buffer - // because a null character always short-circuits further - // advancement. bool valid = QUtil::is_digit(*p); if (valid) { while (QUtil::is_digit(*p)) { @@ -413,6 +401,25 @@ QPDF::findHeader() valid = false; } } + return valid; +} + +bool +QPDF::findHeader() +{ + qpdf_offset_t global_offset = this->m->file->tell(); + std::string line = this->m->file->readLine(1024); + char const* p = line.c_str(); + if (strncmp(p, "%PDF-", 5) != 0) { + throw std::logic_error("findHeader is not looking at %PDF-"); + } + p += 5; + std::string version; + // Note: The string returned by line.c_str() is always + // null-terminated. The code below never overruns the buffer + // because a null character always short-circuits further + // advancement. + bool valid = validatePDFVersion(p, version); if (valid) { this->m->pdf_version = version; if (global_offset != 0) { |