regex_grep.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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_V4_REGEX_GREP_HPP
  18. #define BOOST_REGEX_V4_REGEX_GREP_HPP
  19. namespace boost{
  20. #ifdef BOOST_MSVC
  21. #pragma warning(push)
  22. #pragma warning(disable: 4103)
  23. #endif
  24. #ifdef BOOST_HAS_ABI_HEADERS
  25. # include BOOST_ABI_PREFIX
  26. #endif
  27. #ifdef BOOST_MSVC
  28. #pragma warning(pop)
  29. #endif
  30. //
  31. // regex_grep:
  32. // find all non-overlapping matches within the sequence first last:
  33. //
  34. template <class Predicate, class BidiIterator, class charT, class traits>
  35. inline unsigned int regex_grep(Predicate foo,
  36. BidiIterator first,
  37. BidiIterator last,
  38. const basic_regex<charT, traits>& e,
  39. match_flag_type flags = match_default)
  40. {
  41. if(e.flags() & regex_constants::failbit)
  42. return false;
  43. typedef typename match_results<BidiIterator>::allocator_type match_allocator_type;
  44. match_results<BidiIterator> m;
  45. BOOST_REGEX_DETAIL_NS::perl_matcher<BidiIterator, match_allocator_type, traits> matcher(first, last, m, e, flags, first);
  46. unsigned int count = 0;
  47. while(matcher.find())
  48. {
  49. ++count;
  50. if(0 == foo(m))
  51. return count; // caller doesn't want to go on
  52. if(m[0].second == last)
  53. return count; // we've reached the end, don't try and find an extra null match.
  54. if(m.length() == 0)
  55. {
  56. if(m[0].second == last)
  57. return count;
  58. // we found a NULL-match, now try to find
  59. // a non-NULL one at the same position:
  60. match_results<BidiIterator, match_allocator_type> m2(m);
  61. matcher.setf(match_not_null | match_continuous);
  62. if(matcher.find())
  63. {
  64. ++count;
  65. if(0 == foo(m))
  66. return count;
  67. }
  68. else
  69. {
  70. // reset match back to where it was:
  71. m = m2;
  72. }
  73. matcher.unsetf((match_not_null | match_continuous) & ~flags);
  74. }
  75. }
  76. return count;
  77. }
  78. //
  79. // regex_grep convenience interfaces:
  80. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  81. //
  82. // this isn't really a partial specialisation, but template function
  83. // overloading - if the compiler doesn't support partial specialisation
  84. // then it really won't support this either:
  85. template <class Predicate, class charT, class traits>
  86. inline unsigned int regex_grep(Predicate foo, const charT* str,
  87. const basic_regex<charT, traits>& e,
  88. match_flag_type flags = match_default)
  89. {
  90. return regex_grep(foo, str, str + traits::length(str), e, flags);
  91. }
  92. template <class Predicate, class ST, class SA, class charT, class traits>
  93. inline unsigned int regex_grep(Predicate foo, const std::basic_string<charT, ST, SA>& s,
  94. const basic_regex<charT, traits>& e,
  95. match_flag_type flags = match_default)
  96. {
  97. return regex_grep(foo, s.begin(), s.end(), e, flags);
  98. }
  99. #else // partial specialisation
  100. inline unsigned int regex_grep(bool (*foo)(const cmatch&), const char* str,
  101. const regex& e,
  102. match_flag_type flags = match_default)
  103. {
  104. return regex_grep(foo, str, str + regex::traits_type::length(str), e, flags);
  105. }
  106. #ifndef BOOST_NO_WREGEX
  107. inline unsigned int regex_grep(bool (*foo)(const wcmatch&), const wchar_t* str,
  108. const wregex& e,
  109. match_flag_type flags = match_default)
  110. {
  111. return regex_grep(foo, str, str + wregex::traits_type::length(str), e, flags);
  112. }
  113. #endif
  114. inline unsigned int regex_grep(bool (*foo)(const match_results<std::string::const_iterator>&), const std::string& s,
  115. const regex& e,
  116. match_flag_type flags = match_default)
  117. {
  118. return regex_grep(foo, s.begin(), s.end(), e, flags);
  119. }
  120. #if !defined(BOOST_NO_WREGEX)
  121. inline unsigned int regex_grep(bool (*foo)(const match_results<std::basic_string<wchar_t>::const_iterator>&),
  122. const std::basic_string<wchar_t>& s,
  123. const wregex& e,
  124. match_flag_type flags = match_default)
  125. {
  126. return regex_grep(foo, s.begin(), s.end(), e, flags);
  127. }
  128. #endif
  129. #endif
  130. #ifdef BOOST_MSVC
  131. #pragma warning(push)
  132. #pragma warning(disable: 4103)
  133. #endif
  134. #ifdef BOOST_HAS_ABI_HEADERS
  135. # include BOOST_ABI_SUFFIX
  136. #endif
  137. #ifdef BOOST_MSVC
  138. #pragma warning(pop)
  139. #endif
  140. } // namespace boost
  141. #endif // BOOST_REGEX_V4_REGEX_GREP_HPP