1 | #include "duckdb/function/scalar/string_functions.hpp" |
2 | |
3 | #include "utf8proc_wrapper.hpp" |
4 | |
5 | namespace duckdb { |
6 | |
7 | struct NFCNormalizeOperator { |
8 | template <class INPUT_TYPE, class RESULT_TYPE> |
9 | static RESULT_TYPE Operation(INPUT_TYPE input, Vector &result) { |
10 | auto input_data = input.GetData(); |
11 | auto input_length = input.GetSize(); |
12 | if (StripAccentsFun::IsAscii(input: input_data, n: input_length)) { |
13 | return input; |
14 | } |
15 | auto normalized_str = Utf8Proc::Normalize(s: input_data, len: input_length); |
16 | D_ASSERT(normalized_str); |
17 | auto result_str = StringVector::AddString(result, normalized_str); |
18 | free(normalized_str); |
19 | return result_str; |
20 | } |
21 | }; |
22 | |
23 | static void NFCNormalizeFunction(DataChunk &args, ExpressionState &state, Vector &result) { |
24 | D_ASSERT(args.ColumnCount() == 1); |
25 | |
26 | UnaryExecutor::ExecuteString<string_t, string_t, NFCNormalizeOperator>(input&: args.data[0], result, count: args.size()); |
27 | StringVector::AddHeapReference(vector&: result, other&: args.data[0]); |
28 | } |
29 | |
30 | ScalarFunction NFCNormalizeFun::GetFunction() { |
31 | return ScalarFunction("nfc_normalize" , {LogicalType::VARCHAR}, LogicalType::VARCHAR, NFCNormalizeFunction); |
32 | } |
33 | |
34 | void NFCNormalizeFun::RegisterFunction(BuiltinFunctions &set) { |
35 | set.AddFunction(function: NFCNormalizeFun::GetFunction()); |
36 | } |
37 | |
38 | } // namespace duckdb |
39 | |