1 | #include <Functions/FunctionFactory.h> |
2 | #include <Functions/FunctionsStringSearch.h> |
3 | #include <common/find_symbols.h> |
4 | |
5 | namespace DB |
6 | { |
7 | |
8 | struct CutURLParameterImpl |
9 | { |
10 | static void vector(const ColumnString::Chars & data, |
11 | const ColumnString::Offsets & offsets, |
12 | std::string pattern, |
13 | ColumnString::Chars & res_data, ColumnString::Offsets & res_offsets) |
14 | { |
15 | res_data.reserve(data.size()); |
16 | res_offsets.resize(offsets.size()); |
17 | |
18 | pattern += '='; |
19 | const char * param_str = pattern.c_str(); |
20 | size_t param_len = pattern.size(); |
21 | |
22 | size_t prev_offset = 0; |
23 | size_t res_offset = 0; |
24 | |
25 | for (size_t i = 0; i < offsets.size(); ++i) |
26 | { |
27 | size_t cur_offset = offsets[i]; |
28 | |
29 | const char * url_begin = reinterpret_cast<const char *>(&data[prev_offset]); |
30 | const char * url_end = reinterpret_cast<const char *>(&data[cur_offset]) - 1; |
31 | const char * begin_pos = url_begin; |
32 | const char * end_pos = begin_pos; |
33 | |
34 | do |
35 | { |
36 | const char * query_string_begin = find_first_symbols<'?', '#'>(url_begin, url_end); |
37 | if (query_string_begin + 1 >= url_end) |
38 | break; |
39 | |
40 | const char * pos = static_cast<const char *>(memmem(query_string_begin + 1, url_end - query_string_begin - 1, param_str, param_len)); |
41 | if (pos == nullptr) |
42 | break; |
43 | |
44 | if (pos[-1] != '?' && pos[-1] != '#' && pos[-1] != '&') |
45 | { |
46 | pos = nullptr; |
47 | break; |
48 | } |
49 | |
50 | begin_pos = pos; |
51 | end_pos = begin_pos + param_len; |
52 | |
53 | /// Skip the value. |
54 | while (*end_pos && *end_pos != '&' && *end_pos != '#') |
55 | ++end_pos; |
56 | |
57 | /// Capture '&' before or after the parameter. |
58 | if (*end_pos == '&') |
59 | ++end_pos; |
60 | else if (begin_pos[-1] == '&') |
61 | --begin_pos; |
62 | } while (false); |
63 | |
64 | size_t cut_length = (url_end - url_begin) - (end_pos - begin_pos); |
65 | res_data.resize(res_offset + cut_length + 1); |
66 | memcpySmallAllowReadWriteOverflow15(&res_data[res_offset], url_begin, begin_pos - url_begin); |
67 | memcpySmallAllowReadWriteOverflow15(&res_data[res_offset] + (begin_pos - url_begin), end_pos, url_end - end_pos); |
68 | res_offset += cut_length + 1; |
69 | res_data[res_offset - 1] = 0; |
70 | res_offsets[i] = res_offset; |
71 | |
72 | prev_offset = cur_offset; |
73 | } |
74 | } |
75 | }; |
76 | |
77 | struct NameCutURLParameter { static constexpr auto name = "cutURLParameter" ; }; |
78 | using FunctionCutURLParameter = FunctionsStringSearchToString<CutURLParameterImpl, NameCutURLParameter>; |
79 | |
80 | void registerFunctionCutURLParameter(FunctionFactory & factory) |
81 | { |
82 | factory.registerFunction<FunctionCutURLParameter>(); |
83 | } |
84 | |
85 | } |
86 | |