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