mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-05 15:21:43 +00:00
a2d78e674f
* split FunctionsStringSearch.h * wip * includes * format
64 lines
1.0 KiB
C++
64 lines
1.0 KiB
C++
#pragma once
|
|
|
|
#include <DB/Core/Types.h>
|
|
|
|
namespace DB
|
|
{
|
|
/// Переводит выражение LIKE в regexp re2. Например, abc%def -> ^abc.*def$
|
|
inline String likePatternToRegexp(const String & pattern)
|
|
{
|
|
String res;
|
|
res.reserve(pattern.size() * 2);
|
|
const char * pos = pattern.data();
|
|
const char * end = pos + pattern.size();
|
|
|
|
if (pos < end && *pos == '%')
|
|
++pos;
|
|
else
|
|
res = "^";
|
|
|
|
while (pos < end)
|
|
{
|
|
switch (*pos)
|
|
{
|
|
case '^': case '$': case '.': case '[': case '|': case '(': case ')': case '?': case '*': case '+': case '{':
|
|
res += '\\';
|
|
res += *pos;
|
|
break;
|
|
case '%':
|
|
if (pos + 1 != end)
|
|
res += ".*";
|
|
else
|
|
return res;
|
|
break;
|
|
case '_':
|
|
res += ".";
|
|
break;
|
|
case '\\':
|
|
++pos;
|
|
if (pos == end)
|
|
res += "\\\\";
|
|
else
|
|
{
|
|
if (*pos == '%' || *pos == '_')
|
|
res += *pos;
|
|
else
|
|
{
|
|
res += '\\';
|
|
res += *pos;
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
res += *pos;
|
|
break;
|
|
}
|
|
++pos;
|
|
}
|
|
|
|
res += '$';
|
|
return res;
|
|
}
|
|
|
|
}
|