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