posix.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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_POSIX
  7. #include "performance.hpp"
  8. #include <boost/lexical_cast.hpp>
  9. #include <regex.h>
  10. struct posix_regex : public abstract_regex
  11. {
  12. private:
  13. regex_t pe, pe2;
  14. bool init;
  15. public:
  16. posix_regex() : init(false) {}
  17. ~posix_regex()
  18. {
  19. if(init)
  20. {
  21. regfree(&pe);
  22. regfree(&pe2);
  23. }
  24. }
  25. virtual bool set_expression(const char* pat, bool isperl)
  26. {
  27. if(isperl)
  28. return false;
  29. if(init)
  30. {
  31. regfree(&pe);
  32. regfree(&pe2);
  33. }
  34. else
  35. init = true;
  36. int r = regcomp(&pe, pat, REG_EXTENDED);
  37. std::string s(pat);
  38. if(s.size() && (s[0] != '^'))
  39. s.insert(0, 1, '^');
  40. if(s.size() && (*s.rbegin() != '$'))
  41. s.append("$");
  42. r |= regcomp(&pe2, s.c_str(), REG_EXTENDED);
  43. return r ? false : true;
  44. }
  45. virtual bool match_test(const char* text);
  46. virtual unsigned find_all(const char* text);
  47. virtual std::string name();
  48. struct initializer
  49. {
  50. initializer()
  51. {
  52. posix_regex::register_instance(boost::shared_ptr<abstract_regex>(new posix_regex));
  53. }
  54. void do_nothing()const {}
  55. };
  56. static const initializer init2;
  57. };
  58. const posix_regex::initializer posix_regex::init2;
  59. bool posix_regex::match_test(const char * text)
  60. {
  61. regmatch_t m[30];
  62. int r = regexec(&pe2, text, 30, m, 0);
  63. return r == 0;
  64. }
  65. unsigned posix_regex::find_all(const char * text)
  66. {
  67. unsigned count = 0;
  68. regmatch_t m[30];
  69. int flags = 0;
  70. while(regexec(&pe, text, 30, m, flags) == 0)
  71. {
  72. ++count;
  73. text += m[0].rm_eo;
  74. if(m[0].rm_eo - m[0].rm_so)
  75. flags = *(text - 1) == '\n' ? 0 : REG_NOTBOL;
  76. else
  77. flags = 0;
  78. }
  79. return 0;
  80. }
  81. std::string posix_regex::name()
  82. {
  83. init2.do_nothing();
  84. return "POSIX";
  85. }
  86. #endif