Fast-path for percentEncodeMini(string&)

pull/122/head
Nils Maier 2013-08-24 22:25:29 +02:00
parent b2da75ca33
commit 75e61ee3d0
1 changed files with 17 additions and 8 deletions

View File

@ -347,10 +347,21 @@ bool isCRLF(const char c)
} }
namespace { namespace {
inline static
bool isUtf8Tail(unsigned char ch) bool isUtf8Tail(unsigned char ch)
{ {
return in(ch, 0x80u, 0xbfu); return in(ch, 0x80u, 0xbfu);
} }
inline static
bool inPercentEncodeMini(const unsigned char c)
{
return c > 0x20 && c < 0x7fu &&
// Chromium escapes following characters. Firefox4 escapes more.
c != '"' && c != '<' && c != '>';
}
} // namespace } // namespace
bool isUtf8(const std::string& str) bool isUtf8(const std::string& str)
@ -443,15 +454,13 @@ std::string percentEncode(const std::string& target)
std::string percentEncodeMini(const std::string& src) std::string percentEncodeMini(const std::string& src)
{ {
if (std::find_if_not(src.begin(), src.end(), inPercentEncodeMini) ==
src.end()) {
return src;
}
std::string result; std::string result;
for(std::string::const_iterator i = src.begin(), eoi = src.end(); i != eoi; for (const auto& c: src) {
++i) { if(!inPercentEncodeMini(c)) {
// Non-Printable ASCII and non-ASCII chars + some ASCII chars.
unsigned char c = *i;
if(in(c, 0x00u, 0x20u) || c >= 0x7fu ||
// Chromium escapes following characters. Firefox4 escapes
// more.
c == '"' || c == '<' || c == '>') {
result += fmt("%%%02X", c); result += fmt("%%%02X", c);
} else { } else {
result += c; result += c;