1 // Copyright 2019 The RE2 Authors. All Rights Reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 #include <memory>
6 #include <string>
7 #include <tuple>
8 #include <utility>
9 #include <vector>
10
11 #include <pybind11/pybind11.h>
12 #include <pybind11/stl.h>
13 #include "absl/strings/string_view.h"
14 #include "re2/filtered_re2.h"
15 #include "re2/re2.h"
16 #include "re2/set.h"
17
18 #ifdef _WIN32
19 #include <basetsd.h>
20 #define ssize_t SSIZE_T
21 #endif
22
23 namespace re2_python {
24
25 // This is conventional.
26 namespace py = pybind11;
27
28 // In terms of the pybind11 API, a py::buffer is merely a py::object that
29 // supports the buffer interface/protocol and you must explicitly request
30 // a py::buffer_info in order to access the actual bytes. Under the hood,
31 // the py::buffer_info manages a reference count to the py::buffer, so it
32 // must be constructed and subsequently destructed while holding the GIL.
FromBytes(const py::buffer_info & bytes)33 static inline absl::string_view FromBytes(const py::buffer_info& bytes) {
34 char* data = reinterpret_cast<char*>(bytes.ptr);
35 ssize_t size = bytes.size;
36 return absl::string_view(data, size);
37 }
38
OneCharLen(const char * ptr)39 static inline int OneCharLen(const char* ptr) {
40 return "\1\1\1\1\1\1\1\1\1\1\1\1\2\2\3\4"[(*ptr & 0xFF) >> 4];
41 }
42
43 // Helper function for when Python encodes str to bytes and then needs to
44 // convert str offsets to bytes offsets. Assumes that text is valid UTF-8.
CharLenToBytes(py::buffer buffer,ssize_t pos,ssize_t len)45 ssize_t CharLenToBytes(py::buffer buffer, ssize_t pos, ssize_t len) {
46 auto bytes = buffer.request();
47 auto text = FromBytes(bytes);
48 auto ptr = text.data() + pos;
49 auto end = text.data() + text.size();
50 while (ptr < end && len > 0) {
51 ptr += OneCharLen(ptr);
52 --len;
53 }
54 return ptr - (text.data() + pos);
55 }
56
57 // Helper function for when Python decodes bytes to str and then needs to
58 // convert bytes offsets to str offsets. Assumes that text is valid UTF-8.
BytesToCharLen(py::buffer buffer,ssize_t pos,ssize_t endpos)59 ssize_t BytesToCharLen(py::buffer buffer, ssize_t pos, ssize_t endpos) {
60 auto bytes = buffer.request();
61 auto text = FromBytes(bytes);
62 auto ptr = text.data() + pos;
63 auto end = text.data() + endpos;
64 ssize_t len = 0;
65 while (ptr < end) {
66 ptr += OneCharLen(ptr);
67 ++len;
68 }
69 return len;
70 }
71
RE2InitShim(py::buffer buffer,const RE2::Options & options)72 std::unique_ptr<RE2> RE2InitShim(py::buffer buffer,
73 const RE2::Options& options) {
74 auto bytes = buffer.request();
75 auto pattern = FromBytes(bytes);
76 return std::make_unique<RE2>(pattern, options);
77 }
78
RE2ErrorShim(const RE2 & self)79 py::bytes RE2ErrorShim(const RE2& self) {
80 // Return std::string as bytes. That is, without decoding to str.
81 return self.error();
82 }
83
RE2NamedCapturingGroupsShim(const RE2 & self)84 std::vector<std::pair<py::bytes, int>> RE2NamedCapturingGroupsShim(
85 const RE2& self) {
86 const int num_groups = self.NumberOfCapturingGroups();
87 std::vector<std::pair<py::bytes, int>> groups;
88 groups.reserve(num_groups);
89 for (const auto& it : self.NamedCapturingGroups()) {
90 groups.emplace_back(it.first, it.second);
91 }
92 return groups;
93 }
94
RE2ProgramFanoutShim(const RE2 & self)95 std::vector<int> RE2ProgramFanoutShim(const RE2& self) {
96 std::vector<int> histogram;
97 self.ProgramFanout(&histogram);
98 return histogram;
99 }
100
RE2ReverseProgramFanoutShim(const RE2 & self)101 std::vector<int> RE2ReverseProgramFanoutShim(const RE2& self) {
102 std::vector<int> histogram;
103 self.ReverseProgramFanout(&histogram);
104 return histogram;
105 }
106
RE2PossibleMatchRangeShim(const RE2 & self,int maxlen)107 std::tuple<bool, py::bytes, py::bytes> RE2PossibleMatchRangeShim(
108 const RE2& self, int maxlen) {
109 std::string min, max;
110 // Return std::string as bytes. That is, without decoding to str.
111 return {self.PossibleMatchRange(&min, &max, maxlen), min, max};
112 }
113
RE2MatchShim(const RE2 & self,RE2::Anchor anchor,py::buffer buffer,ssize_t pos,ssize_t endpos)114 std::vector<std::pair<ssize_t, ssize_t>> RE2MatchShim(const RE2& self,
115 RE2::Anchor anchor,
116 py::buffer buffer,
117 ssize_t pos,
118 ssize_t endpos) {
119 auto bytes = buffer.request();
120 auto text = FromBytes(bytes);
121 const int num_groups = self.NumberOfCapturingGroups() + 1; // need $0
122 std::vector<absl::string_view> groups;
123 groups.resize(num_groups);
124 py::gil_scoped_release release_gil;
125 if (!self.Match(text, pos, endpos, anchor, groups.data(), groups.size())) {
126 // Ensure that groups are null before converting to spans!
127 for (auto& it : groups) {
128 it = absl::string_view();
129 }
130 }
131 std::vector<std::pair<ssize_t, ssize_t>> spans;
132 spans.reserve(num_groups);
133 for (const auto& it : groups) {
134 if (it.data() == NULL) {
135 spans.emplace_back(-1, -1);
136 } else {
137 spans.emplace_back(it.data() - text.data(),
138 it.data() - text.data() + it.size());
139 }
140 }
141 return spans;
142 }
143
RE2QuoteMetaShim(py::buffer buffer)144 py::bytes RE2QuoteMetaShim(py::buffer buffer) {
145 auto bytes = buffer.request();
146 auto pattern = FromBytes(bytes);
147 // Return std::string as bytes. That is, without decoding to str.
148 return RE2::QuoteMeta(pattern);
149 }
150
151 class Set {
152 public:
Set(RE2::Anchor anchor,const RE2::Options & options)153 Set(RE2::Anchor anchor, const RE2::Options& options)
154 : set_(options, anchor) {}
155
156 ~Set() = default;
157
158 // Not copyable or movable.
159 Set(const Set&) = delete;
160 Set& operator=(const Set&) = delete;
161
Add(py::buffer buffer)162 int Add(py::buffer buffer) {
163 auto bytes = buffer.request();
164 auto pattern = FromBytes(bytes);
165 int index = set_.Add(pattern, /*error=*/NULL); // -1 on error
166 return index;
167 }
168
Compile()169 bool Compile() {
170 // Compiling can fail.
171 return set_.Compile();
172 }
173
Match(py::buffer buffer) const174 std::vector<int> Match(py::buffer buffer) const {
175 auto bytes = buffer.request();
176 auto text = FromBytes(bytes);
177 std::vector<int> matches;
178 py::gil_scoped_release release_gil;
179 set_.Match(text, &matches);
180 return matches;
181 }
182
183 private:
184 RE2::Set set_;
185 };
186
187 class Filter {
188 public:
189 Filter() = default;
190 ~Filter() = default;
191
192 // Not copyable or movable.
193 Filter(const Filter&) = delete;
194 Filter& operator=(const Filter&) = delete;
195
Add(py::buffer buffer,const RE2::Options & options)196 int Add(py::buffer buffer, const RE2::Options& options) {
197 auto bytes = buffer.request();
198 auto pattern = FromBytes(bytes);
199 int index = -1; // not clobbered on error
200 filter_.Add(pattern, options, &index);
201 return index;
202 }
203
Compile()204 bool Compile() {
205 std::vector<std::string> atoms;
206 filter_.Compile(&atoms);
207 RE2::Options options;
208 options.set_literal(true);
209 options.set_case_sensitive(false);
210 set_ = std::make_unique<RE2::Set>(options, RE2::UNANCHORED);
211 for (int i = 0; i < static_cast<int>(atoms.size()); ++i) {
212 if (set_->Add(atoms[i], /*error=*/NULL) != i) {
213 // Should never happen: the atom is a literal!
214 py::pybind11_fail("set_->Add() failed");
215 }
216 }
217 // Compiling can fail.
218 return set_->Compile();
219 }
220
Match(py::buffer buffer,bool potential) const221 std::vector<int> Match(py::buffer buffer, bool potential) const {
222 auto bytes = buffer.request();
223 auto text = FromBytes(bytes);
224 std::vector<int> atoms;
225 py::gil_scoped_release release_gil;
226 set_->Match(text, &atoms);
227 std::vector<int> matches;
228 if (potential) {
229 filter_.AllPotentials(atoms, &matches);
230 } else {
231 filter_.AllMatches(text, atoms, &matches);
232 }
233 return matches;
234 }
235
GetRE2(int index) const236 const RE2& GetRE2(int index) const {
237 return filter_.GetRE2(index);
238 }
239
240 private:
241 re2::FilteredRE2 filter_;
242 std::unique_ptr<RE2::Set> set_;
243 };
244
PYBIND11_MODULE(_re2,module)245 PYBIND11_MODULE(_re2, module) {
246 module.def("CharLenToBytes", &CharLenToBytes);
247 module.def("BytesToCharLen", &BytesToCharLen);
248
249 // CLASSES
250 // class RE2
251 // enum Anchor
252 // class Options
253 // enum Encoding
254 // class Set
255 // class Filter
256 py::class_<RE2> re2(module, "RE2");
257 py::enum_<RE2::Anchor> anchor(re2, "Anchor");
258 py::class_<RE2::Options> options(re2, "Options");
259 py::enum_<RE2::Options::Encoding> encoding(options, "Encoding");
260 py::class_<Set> set(module, "Set");
261 py::class_<Filter> filter(module, "Filter");
262
263 anchor.value("UNANCHORED", RE2::Anchor::UNANCHORED);
264 anchor.value("ANCHOR_START", RE2::Anchor::ANCHOR_START);
265 anchor.value("ANCHOR_BOTH", RE2::Anchor::ANCHOR_BOTH);
266
267 encoding.value("UTF8", RE2::Options::Encoding::EncodingUTF8);
268 encoding.value("LATIN1", RE2::Options::Encoding::EncodingLatin1);
269
270 options.def(py::init<>())
271 .def_property("max_mem", //
272 &RE2::Options::max_mem, //
273 &RE2::Options::set_max_mem) //
274 .def_property("encoding", //
275 &RE2::Options::encoding, //
276 &RE2::Options::set_encoding) //
277 .def_property("posix_syntax", //
278 &RE2::Options::posix_syntax, //
279 &RE2::Options::set_posix_syntax) //
280 .def_property("longest_match", //
281 &RE2::Options::longest_match, //
282 &RE2::Options::set_longest_match) //
283 .def_property("log_errors", //
284 &RE2::Options::log_errors, //
285 &RE2::Options::set_log_errors) //
286 .def_property("literal", //
287 &RE2::Options::literal, //
288 &RE2::Options::set_literal) //
289 .def_property("never_nl", //
290 &RE2::Options::never_nl, //
291 &RE2::Options::set_never_nl) //
292 .def_property("dot_nl", //
293 &RE2::Options::dot_nl, //
294 &RE2::Options::set_dot_nl) //
295 .def_property("never_capture", //
296 &RE2::Options::never_capture, //
297 &RE2::Options::set_never_capture) //
298 .def_property("case_sensitive", //
299 &RE2::Options::case_sensitive, //
300 &RE2::Options::set_case_sensitive) //
301 .def_property("perl_classes", //
302 &RE2::Options::perl_classes, //
303 &RE2::Options::set_perl_classes) //
304 .def_property("word_boundary", //
305 &RE2::Options::word_boundary, //
306 &RE2::Options::set_word_boundary) //
307 .def_property("one_line", //
308 &RE2::Options::one_line, //
309 &RE2::Options::set_one_line); //
310
311 re2.def(py::init(&RE2InitShim))
312 .def("ok", &RE2::ok)
313 .def("error", &RE2ErrorShim)
314 .def("options", &RE2::options)
315 .def("NumberOfCapturingGroups", &RE2::NumberOfCapturingGroups)
316 .def("NamedCapturingGroups", &RE2NamedCapturingGroupsShim)
317 .def("ProgramSize", &RE2::ProgramSize)
318 .def("ReverseProgramSize", &RE2::ReverseProgramSize)
319 .def("ProgramFanout", &RE2ProgramFanoutShim)
320 .def("ReverseProgramFanout", &RE2ReverseProgramFanoutShim)
321 .def("PossibleMatchRange", &RE2PossibleMatchRangeShim)
322 .def("Match", &RE2MatchShim)
323 .def_static("QuoteMeta", &RE2QuoteMetaShim);
324
325 set.def(py::init<RE2::Anchor, const RE2::Options&>())
326 .def("Add", &Set::Add)
327 .def("Compile", &Set::Compile)
328 .def("Match", &Set::Match);
329
330 filter.def(py::init<>())
331 .def("Add", &Filter::Add)
332 .def("Compile", &Filter::Compile)
333 .def("Match", &Filter::Match)
334 .def("GetRE2", &Filter::GetRE2,
335 py::return_value_policy::reference_internal);
336 }
337
338 } // namespace re2_python
339