diff options
author | vng <viktor.govako@gmail.com> | 2014-08-07 16:21:03 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 02:24:09 +0300 |
commit | efdf60f5435d49b703ee2754d0e21a97bdf80d82 (patch) | |
tree | 8b200eec6a66a8acb904cebceed0b8bdad020e72 /coding | |
parent | f32f150211fc3187c1c724f799a1fb583bdab6ce (diff) |
Faster url encoding/decoding.
Diffstat (limited to 'coding')
-rw-r--r-- | coding/hex.cpp | 8 | ||||
-rw-r--r-- | coding/hex.hpp | 11 | ||||
-rw-r--r-- | 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 <boost/type_traits/is_integral.hpp> -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>(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; } |