1 #include <iostream> 2 #include <stdio.h> 3 4 #include "re2/re2.h" 5 6 using namespace re2; 7 8 extern "C" { 9 typedef void re2_regexp; 10 11 typedef struct re2_string { 12 const char *text; 13 int len; 14 } re2_string; 15 re2_regexp_new(re2_string pat)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 re2_regexp_free(re2_regexp * re)21 void re2_regexp_free(re2_regexp *re) { 22 delete reinterpret_cast<RE2*>(re); 23 } 24 re2_regexp_match(re2_regexp * re,re2_string text,int startpos,int endpos)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 re2_regexp_find(re2_regexp * re,re2_string text,int startpos,int endpos,int * match_start,int * match_end)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