Commit 959ae4b4da48801b09219a9258d0e2c6733b884d
1 parent
fa9df75b
Avoid unnecessary string copies in ContentNormalizer::handleToken
Showing
2 changed files
with
7 additions
and
7 deletions
libqpdf/ContentNormalizer.cc
| ... | ... | @@ -11,7 +11,6 @@ ContentNormalizer::ContentNormalizer() : |
| 11 | 11 | void |
| 12 | 12 | ContentNormalizer::handleToken(QPDFTokenizer::Token const& token) |
| 13 | 13 | { |
| 14 | - std::string value = token.getRawValue(); | |
| 15 | 14 | QPDFTokenizer::token_type_e token_type = token.getType(); |
| 16 | 15 | |
| 17 | 16 | if (token_type == QPDFTokenizer::tt_bad) { |
| ... | ... | @@ -24,6 +23,7 @@ ContentNormalizer::handleToken(QPDFTokenizer::Token const& token) |
| 24 | 23 | switch (token_type) { |
| 25 | 24 | case QPDFTokenizer::tt_space: |
| 26 | 25 | { |
| 26 | + std::string const& value = token.getRawValue(); | |
| 27 | 27 | size_t len = value.length(); |
| 28 | 28 | for (size_t i = 0; i < len; ++i) { |
| 29 | 29 | char ch = value.at(i); |
| ... | ... | @@ -38,7 +38,7 @@ ContentNormalizer::handleToken(QPDFTokenizer::Token const& token) |
| 38 | 38 | } |
| 39 | 39 | } |
| 40 | 40 | } |
| 41 | - break; | |
| 41 | + return; | |
| 42 | 42 | |
| 43 | 43 | case QPDFTokenizer::tt_string: |
| 44 | 44 | // Replacing string and name tokens in this way normalizes their representation as this will |
| ... | ... | @@ -52,12 +52,12 @@ ContentNormalizer::handleToken(QPDFTokenizer::Token const& token) |
| 52 | 52 | |
| 53 | 53 | default: |
| 54 | 54 | writeToken(token); |
| 55 | - break; | |
| 55 | + return; | |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | - value = token.getRawValue(); | |
| 59 | - if (((token_type == QPDFTokenizer::tt_string) || (token_type == QPDFTokenizer::tt_name)) && | |
| 60 | - ((value.find('\r') != std::string::npos) || (value.find('\n') != std::string::npos))) { | |
| 58 | + // tt_string or tt_name | |
| 59 | + std::string const& value = token.getRawValue(); | |
| 60 | + if (value.find('\r') != std::string::npos || value.find('\n') != std::string::npos) { | |
| 61 | 61 | write("\n"); |
| 62 | 62 | } |
| 63 | 63 | } | ... | ... |
libqpdf/QPDFObjectHandle.cc
| ... | ... | @@ -148,7 +148,7 @@ QPDFObjectHandle::TokenFilter::write(std::string const& str) |
| 148 | 148 | void |
| 149 | 149 | QPDFObjectHandle::TokenFilter::writeToken(QPDFTokenizer::Token const& token) |
| 150 | 150 | { |
| 151 | - std::string value = token.getRawValue(); | |
| 151 | + std::string const& value = token.getRawValue(); | |
| 152 | 152 | write(value.c_str(), value.length()); |
| 153 | 153 | } |
| 154 | 154 | ... | ... |