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