2018-11-30 19:37:31 +00:00
|
|
|
#include <Common/Exception.h>
|
2019-08-23 18:40:42 +00:00
|
|
|
#include <Common/PODArray.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/OptimizedRegularExpression.h>
|
2015-10-05 01:11:12 +00:00
|
|
|
|
|
|
|
#define MIN_LENGTH_FOR_STRSTR 3
|
2020-05-07 01:29:31 +00:00
|
|
|
#define MAX_SUBPATTERNS 1024
|
2015-10-05 01:11:12 +00:00
|
|
|
|
2017-05-10 04:00:19 +00:00
|
|
|
|
2018-11-30 19:37:31 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CANNOT_COMPILE_REGEXP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-10 04:00:19 +00:00
|
|
|
template <bool thread_safe>
|
|
|
|
void OptimizedRegularExpressionImpl<thread_safe>::analyze(
|
2017-05-10 02:45:21 +00:00
|
|
|
const std::string & regexp,
|
|
|
|
std::string & required_substring,
|
|
|
|
bool & is_trivial,
|
|
|
|
bool & required_substring_is_prefix)
|
2015-10-05 01:11:12 +00:00
|
|
|
{
|
2017-05-10 02:45:21 +00:00
|
|
|
/** The expression is trivial if all the metacharacters in it are escaped.
|
|
|
|
* The non-alternative string is
|
|
|
|
* a string outside parentheses,
|
|
|
|
* in which all metacharacters are escaped,
|
|
|
|
* and also if there are no '|' outside the brackets,
|
2017-05-10 04:00:19 +00:00
|
|
|
* and also avoid substrings of the form `http://` or `www` and some other
|
|
|
|
* (this is the hack for typical use case in Yandex.Metrica).
|
2017-05-10 02:45:21 +00:00
|
|
|
*/
|
|
|
|
const char * begin = regexp.data();
|
|
|
|
const char * pos = begin;
|
|
|
|
const char * end = regexp.data() + regexp.size();
|
|
|
|
int depth = 0;
|
|
|
|
is_trivial = true;
|
|
|
|
required_substring_is_prefix = false;
|
|
|
|
required_substring.clear();
|
|
|
|
bool has_alternative_on_depth_0 = false;
|
2020-06-14 00:43:42 +00:00
|
|
|
bool has_case_insensitive_flag = false;
|
2017-05-10 02:45:21 +00:00
|
|
|
|
|
|
|
/// Substring with a position.
|
2017-05-10 04:00:19 +00:00
|
|
|
using Substring = std::pair<std::string, size_t>;
|
|
|
|
using Substrings = std::vector<Substring>;
|
2017-05-10 02:45:21 +00:00
|
|
|
|
|
|
|
Substrings trivial_substrings(1);
|
|
|
|
Substring * last_substring = &trivial_substrings.back();
|
|
|
|
|
|
|
|
bool in_curly_braces = false;
|
|
|
|
bool in_square_braces = false;
|
|
|
|
|
|
|
|
while (pos != end)
|
|
|
|
{
|
|
|
|
switch (*pos)
|
|
|
|
{
|
|
|
|
case '\0':
|
|
|
|
pos = end;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\\':
|
|
|
|
{
|
|
|
|
++pos;
|
|
|
|
if (pos == end)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (*pos)
|
|
|
|
{
|
2020-06-14 00:43:42 +00:00
|
|
|
case '|':
|
|
|
|
case '(':
|
|
|
|
case ')':
|
|
|
|
case '^':
|
|
|
|
case '$':
|
|
|
|
case '.':
|
|
|
|
case '[':
|
|
|
|
case '?':
|
|
|
|
case '*':
|
|
|
|
case '+':
|
|
|
|
case '{':
|
2017-05-10 02:45:21 +00:00
|
|
|
if (depth == 0 && !in_curly_braces && !in_square_braces)
|
|
|
|
{
|
|
|
|
if (last_substring->first.empty())
|
|
|
|
last_substring->second = pos - begin;
|
|
|
|
last_substring->first.push_back(*pos);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/// all other escape sequences are not supported
|
|
|
|
is_trivial = false;
|
|
|
|
if (!last_substring->first.empty())
|
|
|
|
{
|
|
|
|
trivial_substrings.resize(trivial_substrings.size() + 1);
|
|
|
|
last_substring = &trivial_substrings.back();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++pos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case '|':
|
|
|
|
if (depth == 0)
|
|
|
|
has_alternative_on_depth_0 = true;
|
|
|
|
is_trivial = false;
|
|
|
|
if (!in_square_braces && !last_substring->first.empty())
|
|
|
|
{
|
|
|
|
trivial_substrings.resize(trivial_substrings.size() + 1);
|
|
|
|
last_substring = &trivial_substrings.back();
|
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '(':
|
|
|
|
if (!in_square_braces)
|
|
|
|
{
|
|
|
|
++depth;
|
|
|
|
is_trivial = false;
|
|
|
|
if (!last_substring->first.empty())
|
|
|
|
{
|
|
|
|
trivial_substrings.resize(trivial_substrings.size() + 1);
|
|
|
|
last_substring = &trivial_substrings.back();
|
|
|
|
}
|
2020-06-14 00:43:42 +00:00
|
|
|
|
|
|
|
/// Check for case-insensitive flag.
|
|
|
|
if (pos + 1 < end && pos[1] == '?')
|
|
|
|
{
|
|
|
|
for (size_t offset = 2; pos + offset < end; ++offset)
|
|
|
|
{
|
|
|
|
if (pos[offset] == '-' /// it means flag negation
|
|
|
|
/// various possible flags, actually only imsU are supported by re2
|
|
|
|
|| (pos[offset] >= 'a' && pos[offset] <= 'z')
|
|
|
|
|| (pos[offset] >= 'A' && pos[offset] <= 'Z'))
|
|
|
|
{
|
|
|
|
if (pos[offset] == 'i')
|
|
|
|
{
|
|
|
|
/// Actually it can be negated case-insensitive flag. But we don't care.
|
|
|
|
has_case_insensitive_flag = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-05-10 02:45:21 +00:00
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '[':
|
|
|
|
in_square_braces = true;
|
|
|
|
++depth;
|
|
|
|
is_trivial = false;
|
|
|
|
if (!last_substring->first.empty())
|
|
|
|
{
|
|
|
|
trivial_substrings.resize(trivial_substrings.size() + 1);
|
|
|
|
last_substring = &trivial_substrings.back();
|
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ']':
|
|
|
|
if (!in_square_braces)
|
|
|
|
goto ordinary;
|
|
|
|
|
|
|
|
in_square_braces = false;
|
|
|
|
--depth;
|
|
|
|
is_trivial = false;
|
|
|
|
if (!last_substring->first.empty())
|
|
|
|
{
|
|
|
|
trivial_substrings.resize(trivial_substrings.size() + 1);
|
|
|
|
last_substring = &trivial_substrings.back();
|
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ')':
|
|
|
|
if (!in_square_braces)
|
|
|
|
{
|
|
|
|
--depth;
|
|
|
|
is_trivial = false;
|
|
|
|
if (!last_substring->first.empty())
|
|
|
|
{
|
|
|
|
trivial_substrings.resize(trivial_substrings.size() + 1);
|
|
|
|
last_substring = &trivial_substrings.back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '^': case '$': case '.': case '+':
|
|
|
|
is_trivial = false;
|
|
|
|
if (!last_substring->first.empty() && !in_square_braces)
|
|
|
|
{
|
|
|
|
trivial_substrings.resize(trivial_substrings.size() + 1);
|
|
|
|
last_substring = &trivial_substrings.back();
|
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
break;
|
|
|
|
|
2019-01-22 19:56:53 +00:00
|
|
|
/// Quantifiers that allow a zero number of occurrences.
|
2017-05-10 02:45:21 +00:00
|
|
|
case '{':
|
|
|
|
in_curly_braces = true;
|
2017-12-02 03:22:51 +00:00
|
|
|
[[fallthrough]];
|
|
|
|
case '?':
|
|
|
|
[[fallthrough]];
|
|
|
|
case '*':
|
2017-05-10 02:45:21 +00:00
|
|
|
is_trivial = false;
|
|
|
|
if (!last_substring->first.empty() && !in_square_braces)
|
|
|
|
{
|
|
|
|
last_substring->first.resize(last_substring->first.size() - 1);
|
|
|
|
trivial_substrings.resize(trivial_substrings.size() + 1);
|
|
|
|
last_substring = &trivial_substrings.back();
|
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '}':
|
|
|
|
if (!in_curly_braces)
|
|
|
|
goto ordinary;
|
|
|
|
|
|
|
|
in_curly_braces = false;
|
|
|
|
++pos;
|
|
|
|
break;
|
|
|
|
|
|
|
|
ordinary: /// Normal, not escaped symbol.
|
2017-12-02 03:22:51 +00:00
|
|
|
[[fallthrough]];
|
2017-05-10 02:45:21 +00:00
|
|
|
default:
|
|
|
|
if (depth == 0 && !in_curly_braces && !in_square_braces)
|
|
|
|
{
|
|
|
|
if (last_substring->first.empty())
|
|
|
|
last_substring->second = pos - begin;
|
|
|
|
last_substring->first.push_back(*pos);
|
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last_substring && last_substring->first.empty())
|
|
|
|
trivial_substrings.pop_back();
|
|
|
|
|
|
|
|
if (!is_trivial)
|
|
|
|
{
|
2020-06-14 00:43:42 +00:00
|
|
|
if (!has_alternative_on_depth_0 && !has_case_insensitive_flag)
|
2017-05-10 02:45:21 +00:00
|
|
|
{
|
2019-05-05 12:26:20 +00:00
|
|
|
/// We choose the non-alternative substring of the maximum length for first search.
|
2019-05-05 09:32:26 +00:00
|
|
|
|
|
|
|
/// Tuning for typical usage domain
|
|
|
|
auto tuning_strings_condition = [](const std::string & str)
|
|
|
|
{
|
|
|
|
return str != "://" && str != "http://" && str != "www" && str != "Windows ";
|
|
|
|
};
|
2017-05-10 02:45:21 +00:00
|
|
|
size_t max_length = 0;
|
|
|
|
Substrings::const_iterator candidate_it = trivial_substrings.begin();
|
|
|
|
for (Substrings::const_iterator it = trivial_substrings.begin(); it != trivial_substrings.end(); ++it)
|
|
|
|
{
|
2019-05-05 12:26:20 +00:00
|
|
|
if (it->first.size() > max_length && tuning_strings_condition(it->first))
|
2017-05-10 02:45:21 +00:00
|
|
|
{
|
|
|
|
max_length = it->first.size();
|
|
|
|
candidate_it = it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max_length >= MIN_LENGTH_FOR_STRSTR)
|
|
|
|
{
|
|
|
|
required_substring = candidate_it->first;
|
|
|
|
required_substring_is_prefix = candidate_it->second == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 18:39:10 +00:00
|
|
|
else if (!trivial_substrings.empty())
|
2017-05-10 02:45:21 +00:00
|
|
|
{
|
|
|
|
required_substring = trivial_substrings.front().first;
|
|
|
|
required_substring_is_prefix = trivial_substrings.front().second == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* std::cerr
|
|
|
|
<< "regexp: " << regexp
|
|
|
|
<< ", is_trivial: " << is_trivial
|
|
|
|
<< ", required_substring: " << required_substring
|
|
|
|
<< ", required_substring_is_prefix: " << required_substring_is_prefix
|
|
|
|
<< std::endl;*/
|
2015-10-05 01:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-10 04:00:19 +00:00
|
|
|
template <bool thread_safe>
|
|
|
|
OptimizedRegularExpressionImpl<thread_safe>::OptimizedRegularExpressionImpl(const std::string & regexp_, int options)
|
2015-10-05 01:11:12 +00:00
|
|
|
{
|
2017-05-10 02:45:21 +00:00
|
|
|
analyze(regexp_, required_substring, is_trivial, required_substring_is_prefix);
|
|
|
|
|
2017-05-10 04:00:19 +00:00
|
|
|
/// Just three following options are supported
|
2017-05-10 02:45:21 +00:00
|
|
|
if (options & (~(RE_CASELESS | RE_NO_CAPTURE | RE_DOT_NL)))
|
2018-11-30 19:37:31 +00:00
|
|
|
throw DB::Exception("OptimizedRegularExpression: Unsupported option.", DB::ErrorCodes::CANNOT_COMPILE_REGEXP);
|
2017-05-10 02:45:21 +00:00
|
|
|
|
2018-11-30 19:37:31 +00:00
|
|
|
is_case_insensitive = options & RE_CASELESS;
|
|
|
|
bool is_no_capture = options & RE_NO_CAPTURE;
|
|
|
|
bool is_dot_nl = options & RE_DOT_NL;
|
2017-05-10 02:45:21 +00:00
|
|
|
|
|
|
|
number_of_subpatterns = 0;
|
|
|
|
if (!is_trivial)
|
|
|
|
{
|
|
|
|
/// Compile the re2 regular expression.
|
2018-06-03 16:51:31 +00:00
|
|
|
typename RegexType::Options regexp_options;
|
2017-05-10 02:45:21 +00:00
|
|
|
|
2018-11-30 19:37:31 +00:00
|
|
|
/// Never write error messages to stderr. It's ignorant to do it from library code.
|
|
|
|
regexp_options.set_log_errors(false);
|
|
|
|
|
2017-05-10 02:45:21 +00:00
|
|
|
if (is_case_insensitive)
|
2018-06-03 16:51:31 +00:00
|
|
|
regexp_options.set_case_sensitive(false);
|
2017-05-10 02:45:21 +00:00
|
|
|
|
|
|
|
if (is_dot_nl)
|
2018-06-03 16:51:31 +00:00
|
|
|
regexp_options.set_dot_nl(true);
|
2017-05-10 02:45:21 +00:00
|
|
|
|
2018-06-03 16:51:31 +00:00
|
|
|
re2 = std::make_unique<RegexType>(regexp_, regexp_options);
|
2017-05-10 02:45:21 +00:00
|
|
|
if (!re2->ok())
|
2020-03-25 11:34:33 +00:00
|
|
|
{
|
|
|
|
throw DB::Exception("OptimizedRegularExpression: cannot compile re2: "
|
|
|
|
+ regexp_ + ", error: " + re2->error()
|
|
|
|
+ ". Look at https://github.com/google/re2/wiki/Syntax "
|
|
|
|
"for reference. Please note that if you specify regex as an SQL "
|
|
|
|
"string literal, the slashes have to be additionally escaped. "
|
|
|
|
"For example, to match an opening brace, write '\\(' -- "
|
|
|
|
"the first slash is for SQL and the second one is for regex",
|
|
|
|
DB::ErrorCodes::CANNOT_COMPILE_REGEXP);
|
|
|
|
}
|
2020-03-25 13:51:50 +00:00
|
|
|
|
2017-05-10 02:45:21 +00:00
|
|
|
if (!is_no_capture)
|
|
|
|
{
|
|
|
|
number_of_subpatterns = re2->NumberOfCapturingGroups();
|
|
|
|
if (number_of_subpatterns > MAX_SUBPATTERNS)
|
2018-11-30 19:37:31 +00:00
|
|
|
throw DB::Exception("OptimizedRegularExpression: too many subpatterns in regexp: " + regexp_, DB::ErrorCodes::CANNOT_COMPILE_REGEXP);
|
2017-05-10 02:45:21 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-17 18:53:59 +00:00
|
|
|
|
|
|
|
if (!required_substring.empty())
|
|
|
|
{
|
|
|
|
if (is_case_insensitive)
|
|
|
|
case_insensitive_substring_searcher.emplace(required_substring.data(), required_substring.size());
|
|
|
|
else
|
|
|
|
case_sensitive_substring_searcher.emplace(required_substring.data(), required_substring.size());
|
|
|
|
}
|
2015-10-05 01:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-10 04:00:19 +00:00
|
|
|
template <bool thread_safe>
|
|
|
|
bool OptimizedRegularExpressionImpl<thread_safe>::match(const char * subject, size_t subject_size) const
|
2015-10-05 01:11:12 +00:00
|
|
|
{
|
2020-02-20 19:38:18 +00:00
|
|
|
const UInt8 * haystack = reinterpret_cast<const UInt8 *>(subject);
|
|
|
|
const UInt8 * haystack_end = haystack + subject_size;
|
|
|
|
|
2017-05-10 02:45:21 +00:00
|
|
|
if (is_trivial)
|
|
|
|
{
|
2020-02-21 15:55:36 +00:00
|
|
|
if (required_substring.empty())
|
|
|
|
return true;
|
|
|
|
|
2017-05-10 02:45:21 +00:00
|
|
|
if (is_case_insensitive)
|
2020-02-20 19:38:18 +00:00
|
|
|
return haystack_end != case_insensitive_substring_searcher->search(haystack, subject_size);
|
2017-05-10 02:45:21 +00:00
|
|
|
else
|
2020-02-20 19:38:18 +00:00
|
|
|
return haystack_end != case_sensitive_substring_searcher->search(haystack, subject_size);
|
2017-05-10 02:45:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!required_substring.empty())
|
|
|
|
{
|
|
|
|
if (is_case_insensitive)
|
2020-02-20 19:38:18 +00:00
|
|
|
{
|
|
|
|
if (haystack_end == case_insensitive_substring_searcher->search(haystack, subject_size))
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-10 02:45:21 +00:00
|
|
|
else
|
2020-02-20 19:38:18 +00:00
|
|
|
{
|
|
|
|
if (haystack_end == case_sensitive_substring_searcher->search(haystack, subject_size))
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-10 02:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return re2->Match(StringPieceType(subject, subject_size), 0, subject_size, RegexType::UNANCHORED, nullptr, 0);
|
|
|
|
}
|
2015-10-05 01:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-10 04:00:19 +00:00
|
|
|
template <bool thread_safe>
|
|
|
|
bool OptimizedRegularExpressionImpl<thread_safe>::match(const char * subject, size_t subject_size, Match & match) const
|
2015-10-05 01:11:12 +00:00
|
|
|
{
|
2020-02-20 19:38:18 +00:00
|
|
|
const UInt8 * haystack = reinterpret_cast<const UInt8 *>(subject);
|
|
|
|
const UInt8 * haystack_end = haystack + subject_size;
|
|
|
|
|
2017-05-10 02:45:21 +00:00
|
|
|
if (is_trivial)
|
|
|
|
{
|
2020-02-21 15:55:36 +00:00
|
|
|
if (required_substring.empty())
|
|
|
|
return true;
|
|
|
|
|
2020-02-20 19:38:18 +00:00
|
|
|
const UInt8 * pos;
|
2017-05-10 02:45:21 +00:00
|
|
|
if (is_case_insensitive)
|
2020-02-20 19:38:18 +00:00
|
|
|
pos = case_insensitive_substring_searcher->search(haystack, subject_size);
|
2017-05-10 02:45:21 +00:00
|
|
|
else
|
2020-02-20 19:38:18 +00:00
|
|
|
pos = case_sensitive_substring_searcher->search(haystack, subject_size);
|
2017-05-10 02:45:21 +00:00
|
|
|
|
2020-02-20 19:38:18 +00:00
|
|
|
if (haystack_end == pos)
|
|
|
|
return false;
|
2017-05-10 02:45:21 +00:00
|
|
|
else
|
|
|
|
{
|
2020-02-20 19:38:18 +00:00
|
|
|
match.offset = pos - haystack;
|
2017-05-10 02:45:21 +00:00
|
|
|
match.length = required_substring.size();
|
2020-03-08 21:04:10 +00:00
|
|
|
return true;
|
2017-05-10 02:45:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!required_substring.empty())
|
|
|
|
{
|
2020-02-20 19:38:18 +00:00
|
|
|
const UInt8 * pos;
|
2017-05-10 02:45:21 +00:00
|
|
|
if (is_case_insensitive)
|
2020-02-20 19:38:18 +00:00
|
|
|
pos = case_insensitive_substring_searcher->search(haystack, subject_size);
|
2017-05-10 02:45:21 +00:00
|
|
|
else
|
2020-02-20 19:38:18 +00:00
|
|
|
pos = case_sensitive_substring_searcher->search(haystack, subject_size);
|
2017-05-10 02:45:21 +00:00
|
|
|
|
2020-02-20 19:38:18 +00:00
|
|
|
if (haystack_end == pos)
|
|
|
|
return false;
|
2017-05-10 02:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StringPieceType piece;
|
|
|
|
|
|
|
|
if (!RegexType::PartialMatch(StringPieceType(subject, subject_size), *re2, &piece))
|
2020-02-20 19:38:18 +00:00
|
|
|
return false;
|
2017-05-10 02:45:21 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
match.offset = piece.data() - subject;
|
|
|
|
match.length = piece.length();
|
2020-02-20 19:38:18 +00:00
|
|
|
return true;
|
2017-05-10 02:45:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-05 01:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-10 04:00:19 +00:00
|
|
|
template <bool thread_safe>
|
|
|
|
unsigned OptimizedRegularExpressionImpl<thread_safe>::match(const char * subject, size_t subject_size, MatchVec & matches, unsigned limit) const
|
2015-10-05 01:11:12 +00:00
|
|
|
{
|
2020-02-20 19:38:18 +00:00
|
|
|
const UInt8 * haystack = reinterpret_cast<const UInt8 *>(subject);
|
|
|
|
const UInt8 * haystack_end = haystack + subject_size;
|
|
|
|
|
2017-05-10 02:45:21 +00:00
|
|
|
matches.clear();
|
|
|
|
|
|
|
|
if (limit == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (limit > number_of_subpatterns + 1)
|
|
|
|
limit = number_of_subpatterns + 1;
|
|
|
|
|
|
|
|
if (is_trivial)
|
|
|
|
{
|
2020-02-21 15:55:36 +00:00
|
|
|
if (required_substring.empty())
|
2020-02-24 18:49:38 +00:00
|
|
|
{
|
2020-02-24 18:52:03 +00:00
|
|
|
matches.emplace_back(Match{0, 0});
|
2020-02-21 15:55:36 +00:00
|
|
|
return 1;
|
2020-02-24 18:49:38 +00:00
|
|
|
}
|
2020-02-21 15:55:36 +00:00
|
|
|
|
2020-02-20 19:38:18 +00:00
|
|
|
const UInt8 * pos;
|
2017-05-10 02:45:21 +00:00
|
|
|
if (is_case_insensitive)
|
2020-02-20 19:38:18 +00:00
|
|
|
pos = case_insensitive_substring_searcher->search(haystack, subject_size);
|
2017-05-10 02:45:21 +00:00
|
|
|
else
|
2020-02-20 19:38:18 +00:00
|
|
|
pos = case_sensitive_substring_searcher->search(haystack, subject_size);
|
2017-05-10 02:45:21 +00:00
|
|
|
|
2020-02-20 19:38:18 +00:00
|
|
|
if (haystack_end == pos)
|
2017-05-10 02:45:21 +00:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Match match;
|
2020-02-20 19:38:18 +00:00
|
|
|
match.offset = pos - haystack;
|
2017-05-10 02:45:21 +00:00
|
|
|
match.length = required_substring.size();
|
|
|
|
matches.push_back(match);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!required_substring.empty())
|
|
|
|
{
|
2020-02-20 19:38:18 +00:00
|
|
|
const UInt8 * pos;
|
2017-05-10 02:45:21 +00:00
|
|
|
if (is_case_insensitive)
|
2020-02-20 19:38:18 +00:00
|
|
|
pos = case_insensitive_substring_searcher->search(haystack, subject_size);
|
2017-05-10 02:45:21 +00:00
|
|
|
else
|
2020-02-20 19:38:18 +00:00
|
|
|
pos = case_sensitive_substring_searcher->search(haystack, subject_size);
|
2017-05-10 02:45:21 +00:00
|
|
|
|
2020-02-20 19:38:18 +00:00
|
|
|
if (haystack_end == pos)
|
2017-05-10 02:45:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-07 01:29:31 +00:00
|
|
|
DB::PODArrayWithStackMemory<StringPieceType, 128> pieces(limit);
|
2017-05-10 02:45:21 +00:00
|
|
|
|
2019-08-23 18:40:42 +00:00
|
|
|
if (!re2->Match(StringPieceType(subject, subject_size), 0, subject_size, RegexType::UNANCHORED, pieces.data(), pieces.size()))
|
2017-05-10 02:45:21 +00:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
matches.resize(limit);
|
|
|
|
for (size_t i = 0; i < limit; ++i)
|
|
|
|
{
|
|
|
|
if (pieces[i] != nullptr)
|
|
|
|
{
|
|
|
|
matches[i].offset = pieces[i].data() - subject;
|
|
|
|
matches[i].length = pieces[i].length();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
matches[i].offset = std::string::npos;
|
|
|
|
matches[i].length = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return limit;
|
|
|
|
}
|
|
|
|
}
|
2015-10-05 01:11:12 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 19:37:31 +00:00
|
|
|
template class OptimizedRegularExpressionImpl<true>;
|
|
|
|
template class OptimizedRegularExpressionImpl<false>;
|