comparison rust/hg-core/src/re2/rust_re2.cpp @ 44305:d8d4fa9a7f18

rust-re2: add wrapper for calling Re2 from Rust This assumes that Re2 is installed following Google's guide. I am not sure how we want to integrate it in the project, but I think a follow-up patch would be more appropriate for such work. As it stands, *not* having Re2 installed results in a compilation error, which is a problem as it breaks install compatibility. Hence, this is gated behind a non-default `with-re2` compilation feature. Differential Revision: https://phab.mercurial-scm.org/D7910
author Rapha?l Gom?s <rgomes@octobus.net>
date Thu, 16 Jan 2020 13:34:04 +0100
parents
children
comparison
equal deleted inserted replaced
44304:2fe89bec8011 44305:d8d4fa9a7f18
1 /*
2 rust_re2.cpp
3
4 C ABI export of Re2's C++ interface for Rust FFI.
5
6 Copyright 2020 Valentin Gatien-Baron
7
8 This software may be used and distributed according to the terms of the
9 GNU General Public License version 2 or any later version.
10 */
11
12 #include <re2/re2.h>
13 using namespace re2;
14
15 extern "C" {
16 RE2* rust_re2_create(const char* data, size_t len) {
17 RE2::Options o;
18 o.set_encoding(RE2::Options::Encoding::EncodingLatin1);
19 o.set_log_errors(false);
20 o.set_max_mem(50000000);
21
22 return new RE2(StringPiece(data, len), o);
23 }
24
25 void rust_re2_destroy(RE2* re) {
26 delete re;
27 }
28
29 bool rust_re2_ok(RE2* re) {
30 return re->ok();
31 }
32
33 void rust_re2_error(RE2* re, const char** outdata, size_t* outlen) {
34 const std::string& e = re->error();
35 *outdata = e.data();
36 *outlen = e.length();
37 }
38
39 bool rust_re2_match(RE2* re, char* data, size_t len, int ianchor) {
40 const StringPiece sp = StringPiece(data, len);
41
42 RE2::Anchor anchor =
43 ianchor == 0 ? RE2::Anchor::UNANCHORED :
44 (ianchor == 1 ? RE2::Anchor::ANCHOR_START :
45 RE2::Anchor::ANCHOR_BOTH);
46
47 return re->Match(sp, 0, len, anchor, NULL, 0);
48 }
49 }