aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Functions/URL/basename.cpp
blob: bc747e2595fec5dacf61f76b45d28e815e46963e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionStringToString.h>
#include <Functions/StringHelpers.h>
#include <base/find_symbols.h>

namespace DB
{

/** Extract substring after the last slash or backslash.
  * If there are no slashes, return the string unchanged.
  * It is used to extract filename from path.
  */
struct ExtractBasename
{
    static size_t getReserveLengthForElement() { return 16; } /// Just a guess.

    static void execute(Pos data, size_t size, Pos & res_data, size_t & res_size)
    {
        res_data = data;
        res_size = size;

        Pos pos = data;
        Pos end = pos + size;

        if ((pos = find_last_symbols_or_null<'/', '\\'>(pos, end)))
        {
            ++pos;
            res_data = pos;
            res_size = end - pos;
        }
    }
};

struct NameBasename { static constexpr auto name = "basename"; };
using FunctionBasename = FunctionStringToString<ExtractSubstringImpl<ExtractBasename>, NameBasename>;

REGISTER_FUNCTION(Basename)
{
    factory.registerFunction<FunctionBasename>();
}

}