std.cpp 1.5 KB

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