regex_grep.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. namespace boost{
  20. //
  21. // regex_grep:
  22. // find all non-overlapping matches within the sequence first last:
  23. //
  24. template <class Predicate, class BidiIterator, class charT, class traits>
  25. inline unsigned int regex_grep(Predicate foo,
  26. BidiIterator first,
  27. BidiIterator last,
  28. const basic_regex<charT, traits>& e,
  29. match_flag_type flags = match_default)
  30. {
  31. if(e.flags() & regex_constants::failbit)
  32. return false;
  33. typedef typename match_results<BidiIterator>::allocator_type match_allocator_type;
  34. match_results<BidiIterator> m;
  35. BOOST_REGEX_DETAIL_NS::perl_matcher<BidiIterator, match_allocator_type, traits> matcher(first, last, m, e, flags, first);
  36. unsigned int count = 0;
  37. while(matcher.find())
  38. {
  39. ++count;
  40. if(0 == foo(m))
  41. return count; // caller doesn't want to go on
  42. if(m[0].second == last)
  43. return count; // we've reached the end, don't try and find an extra null match.
  44. if(m.length() == 0)
  45. {
  46. if(m[0].second == last)
  47. return count;
  48. // we found a NULL-match, now try to find
  49. // a non-NULL one at the same position:
  50. match_results<BidiIterator, match_allocator_type> m2(m);
  51. matcher.setf(match_not_null | match_continuous);
  52. if(matcher.find())
  53. {
  54. ++count;
  55. if(0 == foo(m))
  56. return count;
  57. }
  58. else
  59. {
  60. // reset match back to where it was:
  61. m = m2;
  62. }
  63. matcher.unsetf((match_not_null | match_continuous) & ~flags);
  64. }
  65. }
  66. return count;
  67. }
  68. //
  69. // regex_grep convenience interfaces:
  70. //
  71. template <class Predicate, class charT, class traits>
  72. inline unsigned int regex_grep(Predicate foo, const charT* str,
  73. const basic_regex<charT, traits>& e,
  74. match_flag_type flags = match_default)
  75. {
  76. return regex_grep(foo, str, str + traits::length(str), e, flags);
  77. }
  78. template <class Predicate, class ST, class SA, class charT, class traits>
  79. inline unsigned int regex_grep(Predicate foo, const std::basic_string<charT, ST, SA>& s,
  80. const basic_regex<charT, traits>& e,
  81. match_flag_type flags = match_default)
  82. {
  83. return regex_grep(foo, s.begin(), s.end(), e, flags);
  84. }
  85. } // namespace boost
  86. #endif // BOOST_REGEX_V5_REGEX_GREP_HPP