xpressive.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #include <boost/config.hpp>
  7. #include "performance.hpp"
  8. #include <boost/xpressive/xpressive.hpp>
  9. using namespace boost::xpressive;
  10. struct xpressive_regex : public abstract_regex
  11. {
  12. private:
  13. cregex e;
  14. cmatch what;
  15. public:
  16. virtual bool set_expression(const char* pe, bool isperl)
  17. {
  18. if(!isperl)
  19. return false;
  20. try
  21. {
  22. e = cregex::compile(pe, regex_constants::ECMAScript);
  23. }
  24. catch(const std::exception&)
  25. {
  26. return false;
  27. }
  28. return true;
  29. }
  30. virtual bool match_test(const char* text);
  31. virtual unsigned find_all(const char* text);
  32. virtual std::string name();
  33. struct initializer
  34. {
  35. initializer()
  36. {
  37. xpressive_regex::register_instance(boost::shared_ptr<abstract_regex>(new xpressive_regex));
  38. }
  39. void do_nothing()const {}
  40. };
  41. static const initializer init;
  42. };
  43. const xpressive_regex::initializer xpressive_regex::init;
  44. bool xpressive_regex::match_test(const char * text)
  45. {
  46. return regex_match(text, what, e);
  47. }
  48. unsigned xpressive_regex::find_all(const char * text)
  49. {
  50. cregex_token_iterator i(text, text + std::strlen(text), e), j;
  51. unsigned count = 0;
  52. while(i != j)
  53. {
  54. ++i;
  55. ++count;
  56. }
  57. return count;
  58. }
  59. std::string xpressive_regex::name()
  60. {
  61. init.do_nothing();
  62. return "boost::xpressive::cregex";
  63. }