1 | // RE2 compatibility layer with std::regex |
2 | |
3 | #pragma once |
4 | |
5 | #include "duckdb/common/winapi.hpp" |
6 | #include "duckdb/common/vector.hpp" |
7 | #include <string> |
8 | #include <stdexcept> |
9 | |
10 | namespace duckdb_re2 { |
11 | class RE2; |
12 | |
13 | enum class RegexOptions : uint8_t { NONE, CASE_INSENSITIVE }; |
14 | |
15 | class Regex { |
16 | public: |
17 | DUCKDB_API Regex(const std::string &pattern, RegexOptions options = RegexOptions::NONE); |
18 | Regex(const char *pattern, RegexOptions options = RegexOptions::NONE) : Regex(std::string(pattern)) { |
19 | } |
20 | const duckdb_re2::RE2 &GetRegex() const { |
21 | return *regex; |
22 | } |
23 | |
24 | private: |
25 | std::shared_ptr<duckdb_re2::RE2> regex; |
26 | }; |
27 | |
28 | struct GroupMatch { |
29 | std::string text; |
30 | uint32_t position; |
31 | |
32 | const std::string &str() const { |
33 | return text; |
34 | } |
35 | operator std::string() const { |
36 | return text; |
37 | } |
38 | }; |
39 | |
40 | struct Match { |
41 | duckdb::vector<GroupMatch> groups; |
42 | |
43 | GroupMatch &GetGroup(uint64_t index) { |
44 | if (index >= groups.size()) { |
45 | throw std::runtime_error("RE2: Match index is out of range" ); |
46 | } |
47 | return groups[index]; |
48 | } |
49 | |
50 | std::string str(uint64_t index) { |
51 | return GetGroup(index).text; |
52 | } |
53 | |
54 | uint64_t position(uint64_t index) { |
55 | return GetGroup(index).position; |
56 | } |
57 | |
58 | uint64_t length(uint64_t index) { |
59 | return GetGroup(index).text.size(); |
60 | } |
61 | |
62 | GroupMatch &operator[](uint64_t i) { |
63 | return GetGroup(index: i); |
64 | } |
65 | }; |
66 | |
67 | DUCKDB_API bool RegexSearch(const std::string &input, Match &match, const Regex ®ex); |
68 | DUCKDB_API bool RegexMatch(const std::string &input, Match &match, const Regex ®ex); |
69 | DUCKDB_API bool RegexMatch(const char *start, const char *end, Match &match, const Regex ®ex); |
70 | DUCKDB_API bool RegexMatch(const std::string &input, const Regex ®ex); |
71 | DUCKDB_API duckdb::vector<Match> RegexFindAll(const std::string &input, const Regex ®ex); |
72 | |
73 | } // namespace duckdb_re2 |
74 | |