forked from duckdb/duckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
add regexp_escape function
Showing
6 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ add_library_unity( | |
md5.cpp | ||
pad.cpp | ||
printf.cpp | ||
regexp_escape.cpp | ||
repeat.cpp | ||
replace.cpp | ||
reverse.cpp | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include "duckdb/core_functions/scalar/string_functions.hpp" | ||
#include "duckdb/common/string_util.hpp" | ||
|
||
namespace duckdb { | ||
|
||
static string_t escape(const string_t &str, vector<char> &escaped_pattern) { | ||
auto input_str = str.GetData(); | ||
auto size_str = str.GetSize(); | ||
|
||
escaped_pattern.clear(); // reuse the buffer | ||
// note: reserving double the size to account for escaping ('\\') as an average case | ||
// to have half of the characters in the input string are special | ||
escaped_pattern.reserve(2 * size_str); | ||
string special_chars = "()[]{}?*+-|^$\\.&~#"; | ||
for (idx_t i = 0; i < size_str; ++i) { | ||
char ch = input_str[i]; | ||
if (special_chars.find(ch) != std::string::npos) { | ||
escaped_pattern.push_back('\\'); // escape the special character | ||
} | ||
escaped_pattern.push_back(ch); | ||
} | ||
return string_t(escaped_pattern.data(), escaped_pattern.size()); | ||
} | ||
|
||
static void RegexpEscapeFunction(DataChunk &args, ExpressionState &state, Vector &result) { | ||
vector<char> escaped_pattern; | ||
auto input_str = args.GetValue(0, 0); | ||
result.Reference(escape(input_str.ToString(), escaped_pattern)); | ||
} | ||
|
||
ScalarFunction RegexpEscapeFun::GetFunction() { | ||
return ScalarFunction({LogicalType::VARCHAR}, LogicalType::VARCHAR, RegexpEscapeFunction); | ||
} | ||
|
||
} // namespace duckdb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# name: test/sql/function/string/regex_escape.test | ||
# description: regex escape test | ||
# group: [string] | ||
|
||
statement ok | ||
PRAGMA enable_verification | ||
|
||
# test the example | ||
query T | ||
SELECT regexp_escape('https://duckdb.org'); | ||
---- | ||
https://duckdb\.org | ||
|
||
# no special chars in the input | ||
query T | ||
SELECT regexp_escape('abc'); | ||
---- | ||
abc | ||
|
||
# metacharacters | ||
query T | ||
SELECT regexp_escape('a.b'); | ||
---- | ||
a\.b | ||
|
||
query T | ||
SELECT regexp_escape('()[]{}*+?|^$'); | ||
---- | ||
\(\)\[\]\{\}\*\+\?\|\^\$ | ||
|
||
query T | ||
SELECT regexp_escape('a.b[c]*'); | ||
---- | ||
a\.b\[c\]\* | ||
|
||
# empty string | ||
query T | ||
SELECT regexp_escape('""'); | ||
---- | ||
"" | ||
|
||
# whitespaces | ||
query T | ||
SELECT regexp_escape('a b c'); | ||
---- | ||
a b c | ||
|
||
query T | ||
SELECT regexp_escape('line1\nline2'); | ||
---- | ||
line1\\nline2 | ||
|
||
# case sensitive | ||
query T | ||
SELECT regexp_escape('CaseSensitive'); | ||
---- | ||
CaseSensitive | ||
|
||
# unicode character | ||
query T | ||
SELECT regexp_escape('@'); | ||
---- | ||
@ | ||
|
||
# backslashes | ||
query T | ||
SELECT regexp_escape('path\to\wonderland'); | ||
---- | ||
path\\to\\wonderland | ||
|
||
# escape all special characters | ||
query T | ||
SELECT regexp_escape('$()*+.?[\]^{|}-'); | ||
---- | ||
\$\(\)\*\+\.\?\[\\\]\^\{\|\}\- |