re2.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #ifdef TEST_RE2
  7. #include "performance.hpp"
  8. #include <boost/scoped_ptr.hpp>
  9. #include <re2.h>
  10. using namespace re2;
  11. struct re2_regex : public abstract_regex
  12. {
  13. private:
  14. boost::scoped_ptr<RE2> pat;
  15. public:
  16. re2_regex() {}
  17. ~re2_regex(){}
  18. virtual bool set_expression(const char* pp, bool isperl)
  19. {
  20. if(!isperl)
  21. return false;
  22. std::string s("(?m)");
  23. s += pp;
  24. pat.reset(new RE2(s));
  25. return pat->ok();
  26. }
  27. virtual bool match_test(const char* text);
  28. virtual unsigned find_all(const char* text);
  29. virtual std::string name();
  30. struct initializer
  31. {
  32. initializer()
  33. {
  34. re2_regex::register_instance(boost::shared_ptr<abstract_regex>(new re2_regex));
  35. }
  36. void do_nothing()const {}
  37. };
  38. static const initializer init;
  39. };
  40. const re2_regex::initializer re2_regex::init;
  41. bool re2_regex::match_test(const char * text)
  42. {
  43. return RE2::FullMatch(text, *pat);
  44. }
  45. unsigned re2_regex::find_all(const char * text)
  46. {
  47. unsigned count = 0;
  48. StringPiece input(text);
  49. while(RE2::FindAndConsume(&input, *pat))
  50. {
  51. ++count;
  52. }
  53. return count;
  54. }
  55. std::string re2_regex::name()
  56. {
  57. init.do_nothing();
  58. return "RE2";
  59. }
  60. #endif