to issue/#31092_add_encodeURLComponent_function

This commit is contained in:
zzsmdfj 2022-02-17 11:32:47 +08:00
parent 4dcb411f4f
commit 6b78da6f02

View File

@ -17,7 +17,7 @@ static size_t encodeURL(const char * src, size_t src_size, char * dst, bool spac
for (size_t i = 0; i < src_size - 1; i++)
{
if ((src[i] >= '0' && src[i] <= '9') || (src[i] >= 'a' && src[i] <= 'z') || (src[i] >= 'A' && src[i] <= 'Z')
|| src[i] == '-' || src[i] == '_' || src[i] == '.' || src[i] == '!' || src[i] == '~' || (src[i] >= '\'' && src[i] <= '*'))
|| src[i] == '-' || src[i] == '_' || src[i] == '.' || src[i] == '~')
{
*dst_pos++ = src[i];
}
@ -106,14 +106,23 @@ static size_t decodeURL(const char * src, size_t src_size, char * dst, bool plus
return dst_pos - dst;
}
enum URLCodeStrategy
{
encode,
decode
};
/// Percent decode of URL data.
template <bool plus_as_space, bool encode>
template <bool plus_space_swap, URLCodeStrategy code_strategy>
struct CodeURLComponentImpl
{
static void vector(const ColumnString::Chars & data, const ColumnString::Offsets & offsets,
ColumnString::Chars & res_data, ColumnString::Offsets & res_offsets)
{
if (code_strategy == encode)
//the destination(res_data) string is at most three times the length of the source string
res_data.resize(data.size() * 3);
else
res_data.resize(data.size());
size_t size = offsets.size();
res_offsets.resize(size);
@ -126,10 +135,10 @@ struct CodeURLComponentImpl
const char * src_data = reinterpret_cast<const char *>(&data[prev_offset]);
size_t src_size = offsets[i] - prev_offset;
size_t dst_size;
if (encode)
dst_size = encodeURL(src_data, src_size, reinterpret_cast<char *>(res_data.data() + res_offset), plus_as_space);
if (code_strategy == encode)
dst_size = encodeURL(src_data, src_size, reinterpret_cast<char *>(res_data.data() + res_offset), plus_space_swap);
else
dst_size = decodeURL(src_data, src_size, reinterpret_cast<char *>(res_data.data() + res_offset), plus_as_space);
dst_size = decodeURL(src_data, src_size, reinterpret_cast<char *>(res_data.data() + res_offset), plus_space_swap);
res_offset += dst_size;
res_offsets[i] = res_offset;
prev_offset = offsets[i];
@ -149,10 +158,10 @@ struct NameDecodeURLComponent { static constexpr auto name = "decodeURLComponent
struct NameEncodeURLComponent { static constexpr auto name = "encodeURLComponent"; };
struct NameDecodeURLFormComponent { static constexpr auto name = "decodeURLFormComponent"; };
struct NameEncodeURLFormComponent { static constexpr auto name = "encodeURLFormComponent"; };
using FunctionDecodeURLComponent = FunctionStringToString<CodeURLComponentImpl<false, false>, NameDecodeURLComponent>;
using FunctionEncodeURLComponent = FunctionStringToString<CodeURLComponentImpl<false, true>, NameEncodeURLComponent>;
using FunctionDecodeURLFormComponent = FunctionStringToString<CodeURLComponentImpl<true, false>, NameDecodeURLFormComponent>;
using FunctionEncodeURLFormComponent = FunctionStringToString<CodeURLComponentImpl<true, true>, NameEncodeURLFormComponent>;
using FunctionDecodeURLComponent = FunctionStringToString<CodeURLComponentImpl<false, decode>, NameDecodeURLComponent>;
using FunctionEncodeURLComponent = FunctionStringToString<CodeURLComponentImpl<false, encode>, NameEncodeURLComponent>;
using FunctionDecodeURLFormComponent = FunctionStringToString<CodeURLComponentImpl<true, decode>, NameDecodeURLFormComponent>;
using FunctionEncodeURLFormComponent = FunctionStringToString<CodeURLComponentImpl<true, encode>, NameEncodeURLFormComponent>;
void registerFunctionEncodeAndDecodeURLComponent(FunctionFactory & factory)
{