From 4783b223121d4f1133f0bb14dfd62833d7348a07 Mon Sep 17 00:00:00 2001 From: m-holger Date: Sat, 27 Jul 2024 18:06:12 +0100 Subject: [PATCH] In ContentNormalizer::handleToken refactor handling of space tokens Avoid writing each space char individually. --- libqpdf/ContentNormalizer.cc | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/libqpdf/ContentNormalizer.cc b/libqpdf/ContentNormalizer.cc index 2582e729..bca8ad66 100644 --- a/libqpdf/ContentNormalizer.cc +++ b/libqpdf/ContentNormalizer.cc @@ -25,18 +25,25 @@ ContentNormalizer::handleToken(QPDFTokenizer::Token const& token) case QPDFTokenizer::tt_space: { std::string const& value = token.getRawValue(); - size_t len = value.length(); - for (size_t i = 0; i < len; ++i) { - char ch = value.at(i); - if (ch == '\r') { - if ((i + 1 < len) && (value.at(i + 1) == '\n')) { - // ignore - } else { - write("\n"); - } - } else { - write(&ch, 1); + auto size = value.size(); + size_t pos = 0; + auto r_pos = value.find('\r'); + while (r_pos != std::string::npos) { + if (pos != r_pos) { + write(&value[pos], r_pos - pos); } + if (++r_pos >= size) { + write("\n"); + return; + } + if (value[r_pos] != '\n') { + write("\n"); + } + pos = r_pos; + r_pos = value.find('\r', pos); + } + if (pos < size) { + write(&value[pos], size - pos); } } return;