xref: /third_party/rust/crates/regex/bench/src/ffi/re2.cpp (revision c67d6573)
1#include <iostream>
2#include <stdio.h>
3
4#include "re2/re2.h"
5
6using namespace re2;
7
8extern "C" {
9    typedef void re2_regexp;
10
11    typedef struct re2_string {
12        const char *text;
13        int len;
14    } re2_string;
15
16    re2_regexp* re2_regexp_new(re2_string pat) {
17        re2::StringPiece re2_pat(pat.text, pat.len);
18        return reinterpret_cast<re2_regexp*>(new RE2(re2_pat));
19    }
20
21    void re2_regexp_free(re2_regexp *re) {
22        delete reinterpret_cast<RE2*>(re);
23    }
24
25    bool re2_regexp_match(re2_regexp *re, re2_string text,
26                          int startpos, int endpos) {
27        RE2 *cpp_re = reinterpret_cast<RE2*>(re);
28        re2::StringPiece cpp_text(text.text, text.len);
29
30        return cpp_re->Match(cpp_text, startpos, endpos, RE2::UNANCHORED,
31                             NULL, 0);
32    }
33
34    bool re2_regexp_find(re2_regexp *re, re2_string text,
35                         int startpos, int endpos,
36                         int *match_start, int *match_end) {
37        RE2 *cpp_re = reinterpret_cast<RE2*>(re);
38        re2::StringPiece cpp_text(text.text, text.len);
39        re2::StringPiece result;
40        bool matched;
41
42        matched = cpp_re->Match(cpp_text, startpos, endpos, RE2::UNANCHORED,
43                                &result, 1);
44        if (matched) {
45            *match_start = result.data() - cpp_text.data();
46            *match_end = *match_start + result.length();
47        }
48        return matched;
49    }
50}
51