Chris@16: /* Chris@16: * Chris@16: * Copyright (c) 1998-2002 Chris@16: * John Maddock Chris@16: * Chris@16: * Use, modification and distribution are subject to the Chris@16: * Boost Software License, Version 1.0. (See accompanying file Chris@16: * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Chris@16: */ Chris@16: Chris@16: /* Chris@16: * LOCATION: see http://www.boost.org for most recent version. Chris@16: * FILE regex_grep.hpp Chris@16: * VERSION see Chris@16: * DESCRIPTION: Provides regex_grep implementation. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_REGEX_V4_REGEX_GREP_HPP Chris@16: #define BOOST_REGEX_V4_REGEX_GREP_HPP Chris@16: Chris@16: Chris@16: namespace boost{ Chris@16: Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(push) Chris@16: #pragma warning(disable: 4103) Chris@16: #endif Chris@16: #ifdef BOOST_HAS_ABI_HEADERS Chris@16: # include BOOST_ABI_PREFIX Chris@16: #endif Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: // Chris@16: // regex_grep: Chris@16: // find all non-overlapping matches within the sequence first last: Chris@16: // Chris@16: template Chris@16: inline unsigned int regex_grep(Predicate foo, Chris@16: BidiIterator first, Chris@16: BidiIterator last, Chris@16: const basic_regex& e, Chris@16: match_flag_type flags = match_default) Chris@16: { Chris@16: if(e.flags() & regex_constants::failbit) Chris@16: return false; Chris@16: Chris@16: typedef typename match_results::allocator_type match_allocator_type; Chris@16: Chris@16: match_results m; Chris@16: re_detail::perl_matcher matcher(first, last, m, e, flags, first); Chris@16: unsigned int count = 0; Chris@16: while(matcher.find()) Chris@16: { Chris@16: ++count; Chris@16: if(0 == foo(m)) Chris@16: return count; // caller doesn't want to go on Chris@16: if(m[0].second == last) Chris@16: return count; // we've reached the end, don't try and find an extra null match. Chris@16: if(m.length() == 0) Chris@16: { Chris@16: if(m[0].second == last) Chris@16: return count; Chris@16: // we found a NULL-match, now try to find Chris@16: // a non-NULL one at the same position: Chris@16: match_results m2(m); Chris@16: matcher.setf(match_not_null | match_continuous); Chris@16: if(matcher.find()) Chris@16: { Chris@16: ++count; Chris@16: if(0 == foo(m)) Chris@16: return count; Chris@16: } Chris@16: else Chris@16: { Chris@16: // reset match back to where it was: Chris@16: m = m2; Chris@16: } Chris@16: matcher.unsetf((match_not_null | match_continuous) & ~flags); Chris@16: } Chris@16: } Chris@16: return count; Chris@16: } Chris@16: Chris@16: // Chris@16: // regex_grep convenience interfaces: Chris@16: #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING Chris@16: // Chris@16: // this isn't really a partial specialisation, but template function Chris@16: // overloading - if the compiler doesn't support partial specialisation Chris@16: // then it really won't support this either: Chris@16: template Chris@16: inline unsigned int regex_grep(Predicate foo, const charT* str, Chris@16: const basic_regex& e, Chris@16: match_flag_type flags = match_default) Chris@16: { Chris@16: return regex_grep(foo, str, str + traits::length(str), e, flags); Chris@16: } Chris@16: Chris@16: template Chris@16: inline unsigned int regex_grep(Predicate foo, const std::basic_string& s, Chris@16: const basic_regex& e, Chris@16: match_flag_type flags = match_default) Chris@16: { Chris@16: return regex_grep(foo, s.begin(), s.end(), e, flags); Chris@16: } Chris@16: #else // partial specialisation Chris@16: inline unsigned int regex_grep(bool (*foo)(const cmatch&), const char* str, Chris@16: const regex& e, Chris@16: match_flag_type flags = match_default) Chris@16: { Chris@16: return regex_grep(foo, str, str + regex::traits_type::length(str), e, flags); Chris@16: } Chris@16: #ifndef BOOST_NO_WREGEX Chris@16: inline unsigned int regex_grep(bool (*foo)(const wcmatch&), const wchar_t* str, Chris@16: const wregex& e, Chris@16: match_flag_type flags = match_default) Chris@16: { Chris@16: return regex_grep(foo, str, str + wregex::traits_type::length(str), e, flags); Chris@16: } Chris@16: #endif Chris@16: inline unsigned int regex_grep(bool (*foo)(const match_results&), const std::string& s, Chris@16: const regex& e, Chris@16: match_flag_type flags = match_default) Chris@16: { Chris@16: return regex_grep(foo, s.begin(), s.end(), e, flags); Chris@16: } Chris@16: #if !defined(BOOST_NO_WREGEX) Chris@16: inline unsigned int regex_grep(bool (*foo)(const match_results::const_iterator>&), Chris@16: const std::basic_string& s, Chris@16: const wregex& e, Chris@16: match_flag_type flags = match_default) Chris@16: { Chris@16: return regex_grep(foo, s.begin(), s.end(), e, flags); Chris@16: } Chris@16: #endif Chris@16: #endif Chris@16: Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(push) Chris@16: #pragma warning(disable: 4103) Chris@16: #endif Chris@16: #ifdef BOOST_HAS_ABI_HEADERS Chris@16: # include BOOST_ABI_SUFFIX Chris@16: #endif Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_REGEX_V4_REGEX_GREP_HPP Chris@16: