ClickHouse/dbms/Functions/URL/basename.cpp

43 lines
1.1 KiB
C++
Raw Normal View History

2019-04-29 09:40:44 +00:00
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionStringToString.h>
#include <common/find_symbols.h>
2019-12-15 06:34:43 +00:00
#include "FunctionsURL.h"
2019-04-29 09:40:44 +00:00
namespace DB
{
2019-04-30 16:14:53 +00:00
/** Extract substring after the last slash or backslash.
2019-04-30 16:15:08 +00:00
* If there are no slashes, return the string unchanged.
2019-04-30 16:14:53 +00:00
* It is used to extract filename from path.
*/
2019-04-29 09:40:44 +00:00
struct ExtractBasename
{
2019-04-29 21:40:54 +00:00
static size_t getReserveLengthForElement() { return 16; } /// Just a guess.
2019-04-29 09:40:44 +00:00
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>();
}
}