Add a basename function

This commit is contained in:
Guillaume Tassery 2019-04-29 16:40:44 +07:00
parent 97c532c0f0
commit 95141804fb
2 changed files with 40 additions and 0 deletions

View File

@ -0,0 +1,38 @@
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionStringToString.h>
#include <Functions/FunctionsURL.h>
#include <common/find_symbols.h>
namespace DB
{
struct ExtractBasename
{
static size_t getReserveLengthForElement() { return 25; }
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>;
void registerFunctionBasename(FunctionFactory & factory)
{
factory.registerFunction<FunctionBasename>();
}
}

View File

@ -42,6 +42,7 @@ void registerFunctionLowCardinalityKeys(FunctionFactory &);
void registerFunctionsIn(FunctionFactory &);
void registerFunctionJoinGet(FunctionFactory &);
void registerFunctionFilesystem(FunctionFactory &);
void registerFunctionBasename(FunctionFactory &);
void registerFunctionsMiscellaneous(FunctionFactory & factory)
{
@ -84,6 +85,7 @@ void registerFunctionsMiscellaneous(FunctionFactory & factory)
registerFunctionsIn(factory);
registerFunctionJoinGet(factory);
registerFunctionFilesystem(factory);
registerFunctionBasename(factory);
}
}