• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 ///////////////////////////////////////////////////////////////
2 //  Copyright 2015 John Maddock. Distributed under the Boost
3 //  Software License, Version 1.0. (See accompanying file
4 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
5 //
6 
7 #ifdef TEST_RE2
8 
9 #include "performance.hpp"
10 #include <boost/scoped_ptr.hpp>
11 #include <re2.h>
12 
13 using namespace re2;
14 
15 struct re2_regex : public abstract_regex
16 {
17 private:
18    boost::scoped_ptr<RE2> pat;
19 public:
re2_regexre2_regex20    re2_regex() {}
~re2_regexre2_regex21    ~re2_regex(){}
set_expressionre2_regex22    virtual bool set_expression(const char* pp, bool isperl)
23    {
24       if(!isperl)
25          return false;
26       std::string s("(?m)");
27       s += pp;
28       pat.reset(new RE2(s));
29       return pat->ok();
30    }
31    virtual bool match_test(const char* text);
32    virtual unsigned find_all(const char* text);
33    virtual std::string name();
34 
35    struct initializer
36    {
initializerre2_regex::initializer37       initializer()
38       {
39          re2_regex::register_instance(boost::shared_ptr<abstract_regex>(new re2_regex));
40       }
do_nothingre2_regex::initializer41       void do_nothing()const {}
42    };
43    static const initializer init;
44 };
45 
46 const re2_regex::initializer re2_regex::init;
47 
48 
match_test(const char * text)49 bool re2_regex::match_test(const char * text)
50 {
51    return RE2::FullMatch(text, *pat);
52 }
53 
find_all(const char * text)54 unsigned re2_regex::find_all(const char * text)
55 {
56    unsigned count = 0;
57    StringPiece input(text);
58    while(RE2::FindAndConsume(&input, *pat))
59    {
60       ++count;
61    }
62    return count;
63 }
64 
name()65 std::string re2_regex::name()
66 {
67    init.do_nothing();
68    return "RE2";
69 }
70 
71 #endif
72