1 | #include "duckdb/function/scalar/string_functions.hpp" |
2 | #include "duckdb/common/types/string_type.hpp" |
3 | |
4 | #include "duckdb/common/exception.hpp" |
5 | |
6 | namespace duckdb { |
7 | |
8 | static bool SuffixFunction(const string_t &str, const string_t &suffix); |
9 | |
10 | struct SuffixOperator { |
11 | template <class TA, class TB, class TR> |
12 | static inline TR Operation(TA left, TB right) { |
13 | return SuffixFunction(left, right); |
14 | } |
15 | }; |
16 | |
17 | static bool SuffixFunction(const string_t &str, const string_t &suffix) { |
18 | auto suffix_size = suffix.GetSize(); |
19 | auto str_size = str.GetSize(); |
20 | if (suffix_size > str_size) { |
21 | return false; |
22 | } |
23 | |
24 | auto suffix_data = suffix.GetData(); |
25 | auto str_data = str.GetData(); |
26 | int32_t suf_idx = suffix_size - 1; |
27 | idx_t str_idx = str_size - 1; |
28 | for (; suf_idx >= 0; --suf_idx, --str_idx) { |
29 | if (suffix_data[suf_idx] != str_data[str_idx]) { |
30 | return false; |
31 | } |
32 | } |
33 | return true; |
34 | } |
35 | |
36 | ScalarFunction SuffixFun::GetFunction() { |
37 | return ScalarFunction("suffix" , // name of the function |
38 | {LogicalType::VARCHAR, LogicalType::VARCHAR}, // argument list |
39 | LogicalType::BOOLEAN, // return type |
40 | ScalarFunction::BinaryFunction<string_t, string_t, bool, SuffixOperator>); |
41 | } |
42 | |
43 | void SuffixFun::RegisterFunction(BuiltinFunctions &set) { |
44 | set.AddFunction(function: GetFunction()); |
45 | } |
46 | |
47 | } // namespace duckdb |
48 | |