1#include <Functions/FunctionFactory.h>
2#include <Functions/FunctionStringToString.h>
3#include <common/find_symbols.h>
4#include "FunctionsURL.h"
5
6namespace DB
7{
8
9/** Extract substring after the last slash or backslash.
10 * If there are no slashes, return the string unchanged.
11 * It is used to extract filename from path.
12 */
13struct ExtractBasename
14{
15 static size_t getReserveLengthForElement() { return 16; } /// Just a guess.
16
17 static void execute(Pos data, size_t size, Pos & res_data, size_t & res_size)
18 {
19 res_data = data;
20 res_size = size;
21
22 Pos pos = data;
23 Pos end = pos + size;
24
25 if ((pos = find_last_symbols_or_null<'/', '\\'>(pos, end)))
26 {
27 ++pos;
28 res_data = pos;
29 res_size = end - pos;
30 }
31 }
32};
33
34struct NameBasename { static constexpr auto name = "basename"; };
35using FunctionBasename = FunctionStringToString<ExtractSubstringImpl<ExtractBasename>, NameBasename>;
36
37void registerFunctionBasename(FunctionFactory & factory)
38{
39 factory.registerFunction<FunctionBasename>();
40}
41
42}
43