regex_grep.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. *
  3. * Copyright (c) 1998-2002
  4. * John Maddock
  5. *
  6. * Use, modification and distribution are subject to the
  7. * Boost Software License, Version 1.0. (See accompanying file
  8. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. */
  11. /*
  12. * LOCATION: see http://www.boost.org for most recent version.
  13. * FILE regex_grep.hpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Provides regex_grep implementation.
  16. */
  17. #ifndef BOOST_REGEX_V5_REGEX_GREP_HPP
  18. #define BOOST_REGEX_V5_REGEX_GREP_HPP
  19. #include <boost/regex/v5/basic_regex.hpp>
  20. #include <boost/regex/v5/match_flags.hpp>
  21. #include <boost/regex/v5/match_results.hpp>
  22. #include <boost/regex/v5/perl_matcher.hpp>
  23. namespace boost{
  24. //
  25. // regex_grep:
  26. // find all non-overlapping matches within the sequence first last:
  27. //
  28. BOOST_REGEX_MODULE_EXPORT template <class Predicate, class BidiIterator, class charT, class traits>
  29. inline unsigned int regex_grep(Predicate foo,
  30. BidiIterator first,
  31. BidiIterator last,
  32. const basic_regex<charT, traits>& e,
  33. match_flag_type flags = match_default)
  34. {
  35. if(e.flags() & regex_constants::failbit)
  36. return false;
  37. typedef typename match_results<BidiIterator>::allocator_type match_allocator_type;
  38. match_results<BidiIterator> m;
  39. BOOST_REGEX_DETAIL_NS::perl_matcher<BidiIterator, match_allocator_type, traits> matcher(first, last, m, e, flags, first);
  40. unsigned int count = 0;
  41. while(BOOST_REGEX_DETAIL_NS::factory_find(matcher))
  42. {
  43. ++count;
  44. if(0 == foo(m))
  45. return count; // caller doesn't want to go on
  46. if(m[0].second == last)
  47. return count; // we've reached the end, don't try and find an extra null match.
  48. if(m.length() == 0)
  49. {
  50. if(m[0].second == last)
  51. return count;
  52. // we found a NULL-match, now try to find
  53. // a non-NULL one at the same position:
  54. match_results<BidiIterator, match_allocator_type> m2(m);
  55. matcher.setf(match_not_null | match_continuous);
  56. if(BOOST_REGEX_DETAIL_NS::factory_find(matcher))
  57. {
  58. ++count;
  59. if(0 == foo(m))
  60. return count;
  61. }
  62. else
  63. {
  64. // reset match back to where it was:
  65. m = m2;
  66. }
  67. matcher.unsetf((match_not_null | match_continuous) & ~flags);
  68. }
  69. }
  70. return count;
  71. }
  72. //
  73. // regex_grep convenience interfaces:
  74. //
  75. BOOST_REGEX_MODULE_EXPORT template <class Predicate, class charT, class traits>
  76. inline unsigned int regex_grep(Predicate foo, const charT* str,
  77. const basic_regex<charT, traits>& e,
  78. match_flag_type flags = match_default)
  79. {
  80. return regex_grep(foo, str, str + traits::length(str), e, flags);
  81. }
  82. BOOST_REGEX_MODULE_EXPORT template <class Predicate, class ST, class SA, class charT, class traits>
  83. inline unsigned int regex_grep(Predicate foo, const std::basic_string<charT, ST, SA>& s,
  84. const basic_regex<charT, traits>& e,
  85. match_flag_type flags = match_default)
  86. {
  87. return regex_grep(foo, s.begin(), s.end(), e, flags);
  88. }
  89. } // namespace boost
  90. #endif // BOOST_REGEX_V5_REGEX_GREP_HPP