瀏覽代碼

Fast-path for percentEncodeMini(string&)

Nils Maier 12 年之前
父節點
當前提交
75e61ee3d0
共有 1 個文件被更改,包括 17 次插入8 次删除
  1. 17 8
      src/util.cc

+ 17 - 8
src/util.cc

@@ -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;
-      ++i) {
-    // 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 == '>') {
+  for (const auto& c: src) {
+    if(!inPercentEncodeMini(c)) {
       result += fmt("%%%02X", c);
       result += fmt("%%%02X", c);
     } else {
     } else {
       result += c;
       result += c;