Commit 959ae4b4da48801b09219a9258d0e2c6733b884d

Authored by m-holger
1 parent fa9df75b

Avoid unnecessary string copies in ContentNormalizer::handleToken

libqpdf/ContentNormalizer.cc
@@ -11,7 +11,6 @@ ContentNormalizer::ContentNormalizer() : @@ -11,7 +11,6 @@ ContentNormalizer::ContentNormalizer() :
11 void 11 void
12 ContentNormalizer::handleToken(QPDFTokenizer::Token const& token) 12 ContentNormalizer::handleToken(QPDFTokenizer::Token const& token)
13 { 13 {
14 - std::string value = token.getRawValue();  
15 QPDFTokenizer::token_type_e token_type = token.getType(); 14 QPDFTokenizer::token_type_e token_type = token.getType();
16 15
17 if (token_type == QPDFTokenizer::tt_bad) { 16 if (token_type == QPDFTokenizer::tt_bad) {
@@ -24,6 +23,7 @@ ContentNormalizer::handleToken(QPDFTokenizer::Token const& token) @@ -24,6 +23,7 @@ ContentNormalizer::handleToken(QPDFTokenizer::Token const& token)
24 switch (token_type) { 23 switch (token_type) {
25 case QPDFTokenizer::tt_space: 24 case QPDFTokenizer::tt_space:
26 { 25 {
  26 + std::string const& value = token.getRawValue();
27 size_t len = value.length(); 27 size_t len = value.length();
28 for (size_t i = 0; i < len; ++i) { 28 for (size_t i = 0; i < len; ++i) {
29 char ch = value.at(i); 29 char ch = value.at(i);
@@ -38,7 +38,7 @@ ContentNormalizer::handleToken(QPDFTokenizer::Token const&amp; token) @@ -38,7 +38,7 @@ ContentNormalizer::handleToken(QPDFTokenizer::Token const&amp; token)
38 } 38 }
39 } 39 }
40 } 40 }
41 - break; 41 + return;
42 42
43 case QPDFTokenizer::tt_string: 43 case QPDFTokenizer::tt_string:
44 // Replacing string and name tokens in this way normalizes their representation as this will 44 // Replacing string and name tokens in this way normalizes their representation as this will
@@ -52,12 +52,12 @@ ContentNormalizer::handleToken(QPDFTokenizer::Token const&amp; token) @@ -52,12 +52,12 @@ ContentNormalizer::handleToken(QPDFTokenizer::Token const&amp; token)
52 52
53 default: 53 default:
54 writeToken(token); 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 write("\n"); 61 write("\n");
62 } 62 }
63 } 63 }
libqpdf/QPDFObjectHandle.cc
@@ -148,7 +148,7 @@ QPDFObjectHandle::TokenFilter::write(std::string const&amp; str) @@ -148,7 +148,7 @@ QPDFObjectHandle::TokenFilter::write(std::string const&amp; str)
148 void 148 void
149 QPDFObjectHandle::TokenFilter::writeToken(QPDFTokenizer::Token const& token) 149 QPDFObjectHandle::TokenFilter::writeToken(QPDFTokenizer::Token const& token)
150 { 150 {
151 - std::string value = token.getRawValue(); 151 + std::string const& value = token.getRawValue();
152 write(value.c_str(), value.length()); 152 write(value.c_str(), value.length());
153 } 153 }
154 154