Skip to content

Commit

Permalink
Implemented contains with strstr.
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagokepe committed Apr 7, 2020
1 parent 93b5d6d commit c04436f
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/function/scalar/string/contains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <ctype.h>
#include <unordered_map>
#include <algorithm> // std::max
#include <cstring>

using namespace std;

Expand Down Expand Up @@ -51,6 +52,14 @@ struct ContainsOperator {
}
};

static bool contains_strstr(const string_t &str, const string_t &pattern);

struct ContainsSTRSTROperator {
template <class TA, class TB, class TR> static inline TR Operation(TA left, TB right) {
return contains_strstr(left, right);
}
};

static bool contains_instr(string_t haystack, string_t needle) {
// Getting information about the needle and the haystack
auto input_haystack = haystack.GetData();
Expand All @@ -73,6 +82,12 @@ static bool contains_instr(string_t haystack, string_t needle) {
return false;
}

static bool contains_strstr(const string_t &str, const string_t &pattern) {
auto str_data = str.GetData();
auto patt_data = pattern.GetData();
return (strstr(str_data , patt_data) != nullptr);
}

static bool contains_kmp(const string_t &str, const string_t &pattern, vector<uint32_t> &kmp_table) {
auto str_size = str.GetSize();
auto patt_size = pattern.GetSize();
Expand Down Expand Up @@ -261,12 +276,16 @@ void ContainsFun::RegisterFunction(BuiltinFunctions &set) {
SQLType::BOOLEAN, // return type
ScalarFunction::BinaryFunction<string_t, string_t, bool, ContainsOperator, true>));

set.AddFunction(ScalarFunction("contains_strstr", // name of the function
{SQLType::VARCHAR, SQLType::VARCHAR}, // argument list
SQLType::BOOLEAN, // return type
ScalarFunction::BinaryFunction<string_t, string_t, bool, ContainsSTRSTROperator, true>));

set.AddFunction(ScalarFunction("contains_kmp",
{SQLType::VARCHAR, SQLType::VARCHAR},
SQLType::BOOLEAN,
contains_kmp_function, false, contains_kmp_get_bind_function));


set.AddFunction(ScalarFunction("contains_bm",
{SQLType::VARCHAR, SQLType::VARCHAR},
SQLType::BOOLEAN,
Expand Down

0 comments on commit c04436f

Please sign in to comment.