forked from organicmaps/organicmaps
Faster url encoding/decoding.
This commit is contained in:
parent
f32f150211
commit
efdf60f543
3 changed files with 30 additions and 18 deletions
|
@ -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')
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue