1#include <Functions/FunctionFactory.h>
2#include <Functions/FunctionStringToString.h>
3#include "protocol.h"
4#include <common/find_symbols.h>
5
6
7namespace DB
8{
9
10/// With dot at the end.
11struct ExtractWWW
12{
13 static void execute(Pos data, size_t size, Pos & res_data, size_t & res_size)
14 {
15 res_data = data;
16 res_size = 0;
17
18 Pos pos = data;
19 Pos end = pos + size;
20
21 if (end != (pos = find_first_symbols<'/'>(pos, end)))
22 {
23 if (pos != data)
24 {
25 Pos tmp;
26 size_t protocol_length;
27 ExtractProtocol::execute(data, size, tmp, protocol_length);
28
29 if (pos != data + protocol_length + 1)
30 return;
31 }
32
33 if (end - pos < 2 || *(pos) != '/' || *(pos + 1) != '/')
34 return;
35
36 const char *start_of_host = (pos += 2);
37 for (; pos < end; ++pos)
38 {
39 if (*pos == '@')
40 start_of_host = pos + 1;
41 else if (*pos == ':' || *pos == '/' || *pos == '?' || *pos == '#')
42 break;
43 }
44
45 if (start_of_host + 4 < end && !strncmp(start_of_host, "www.", 4))
46 {
47 res_data = start_of_host;
48 res_size = 4;
49 }
50 }
51 }
52};
53
54struct NameCutWWW { static constexpr auto name = "cutWWW"; };
55using FunctionCutWWW = FunctionStringToString<CutSubstringImpl<ExtractWWW>, NameCutWWW>;
56
57void registerFunctionCutWWW(FunctionFactory & factory)
58{
59 factory.registerFunction<FunctionCutWWW>();
60}
61
62}
63