aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Functions/firstLine.cpp
blob: 20b47361d58a1b49a32d0a0b6cb5f6cd9d0915a6 (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
{

struct FirstLine
{
    static size_t getReserveLengthForElement() { return 16; }

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

        const Pos end = data + size;
        const Pos pos = find_first_symbols<'\r', '\n'>(data, end);
        res_size = pos - data;
    }
};

struct NameFirstLine
{
    static constexpr auto name = "firstLine";
};

using FunctionFirstLine = FunctionStringToString<ExtractSubstringImpl<FirstLine>, NameFirstLine>;

REGISTER_FUNCTION(FirstLine)
{
    factory.registerFunction<FunctionFirstLine>(FunctionDocumentation{
        .description = "Returns first line of a multi-line string.",
        .syntax = "firstLine(string)",
        .arguments = {{.name = "string", .description = "The string to process."}},
        .returned_value = {"The first line of the string or the whole string if there is no line separators."},
        .examples = {
            {.name = "Return first line", .query = "firstLine('Hello\\nWorld')", .result = "'Hello'"},
            {.name = "Return whole string", .query = "firstLine('Hello World')", .result = "'Hello World'"},
        }});
}
}