From efdf60f5435d49b703ee2754d0e21a97bdf80d82 Mon Sep 17 00:00:00 2001 From: vng Date: Thu, 7 Aug 2014 15:21:03 +0300 Subject: [PATCH] Faster url encoding/decoding. --- coding/hex.cpp | 8 ++++---- coding/hex.hpp | 11 +++++++---- coding/url_encode.hpp | 29 +++++++++++++++++++---------- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/coding/hex.cpp b/coding/hex.cpp index 0eab19f4b6..66ddffd678 100644 --- a/coding/hex.cpp +++ b/coding/hex.cpp @@ -1,8 +1,10 @@ -#include "../base/SRC_FIRST.hpp" #include "hex.hpp" +#include "../base/assert.hpp" -namespace impl { + +namespace impl +{ static const char kToHexTable[] = "0123456789ABCDEF"; void ToHexRaw(void const * src, size_t size, void * dst) @@ -19,8 +21,6 @@ namespace impl { } } -// static const char kFromHexTable[] = "0123456789ABCDEF"; - uint8_t HexDigitToRaw(uint8_t const digit) { if (digit >= '0' && digit <= '9') diff --git a/coding/hex.hpp b/coding/hex.hpp index 1fe22e29ca..251079cfa1 100644 --- a/coding/hex.hpp +++ b/coding/hex.hpp @@ -1,13 +1,14 @@ #pragma once #include "../base/base.hpp" -#include "../base/assert.hpp" #include "../std/string.hpp" #include -namespace impl { + +namespace impl +{ void ToHexRaw(void const * src, size_t size, void * dst); void FromHexRaw(void const * src, size_t size, void * dst); } @@ -71,14 +72,16 @@ inline string NumToHex(char c) } /// @} -inline string FromHex(void const * ptr, size_t size) { +inline string FromHex(void const * ptr, size_t size) +{ string result; result.resize(size / 2); ::impl::FromHexRaw(ptr, size, &result[0]); return result; } -inline string FromHex(string const & src) { +inline string FromHex(string const & src) +{ return FromHex(src.c_str(), src.size()); } diff --git a/coding/url_encode.hpp b/coding/url_encode.hpp index 4cadcae7fd..cba1ee497e 100644 --- a/coding/url_encode.hpp +++ b/coding/url_encode.hpp @@ -4,28 +4,36 @@ #include "../std/string.hpp" + inline string UrlEncode(string const & rawUrl) { - string result(rawUrl); - for (size_t i = 0; i < result.size(); ++i) + size_t const count = rawUrl.size(); + string result; + result.reserve(count); + + for (size_t i = 0; i < count; ++i) { - char const c = result[i]; - if (c < '-' || c == '/' || (c > '9' && c < 'A') || (c > 'Z' && c < '_') - || c == '`' || (c > 'z' && c < '~') || c > '~') + char const c = rawUrl[i]; + if (c < '-' || c == '/' || (c > '9' && c < 'A') || (c > 'Z' && c < '_') || + c == '`' || (c > 'z' && c < '~') || c > '~') { - string hexStr("%"); - hexStr += NumToHex(c); - result.replace(i, 1, hexStr); - i += 2; + result += '%'; + result += NumToHex(c); } + else + result += rawUrl[i]; } + return result; } inline string UrlDecode(string const & encodedUrl) { + size_t const count = encodedUrl.size(); string result; - for (size_t i = 0; i < encodedUrl.size(); ++i) + result.reserve(count); + + for (size_t i = 0; i < count; ++i) { if (encodedUrl[i] == '%') { @@ -35,5 +43,6 @@ inline string UrlDecode(string const & encodedUrl) else result += encodedUrl[i]; } + return result; }