2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 10:58:58 +00:00

In ContentNormalizer::handleToken refactor handling of space tokens

Avoid writing each space char individually.
This commit is contained in:
m-holger 2024-07-27 18:06:12 +01:00
parent ffe462e67e
commit 4783b22312

View File

@ -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;