diff --git a/include/regex b/include/regex index 864cc0eba..30def6b2f 100644 --- a/include/regex +++ b/include/regex @@ -2244,7 +2244,6 @@ __bracket_expression<_CharT, _Traits>::__exec(__state& __s) const __exit: if (__found != __negate_) { - _CharT __ch = *__s.__current_; __s.__do_ = __state::__accept_and_consume; __s.__current_ += __consumed; __s.__node_ = this->first(); @@ -2478,16 +2477,16 @@ private: void __push_begin_marked_subexpression(); void __push_end_marked_subexpression(unsigned); - template + template bool - __search(_BidirectionalIterator __first, _BidirectionalIterator __last, - match_results<_BidirectionalIterator, _Allocator>& __m, + __search(const _CharT* __first, const _CharT* __last, + match_results& __m, regex_constants::match_flag_type __flags) const; - template + template bool - __match_at_start(_BidirectionalIterator __first, _BidirectionalIterator __last, - match_results<_BidirectionalIterator, _Allocator>& __m, + __match_at_start(const _CharT* __first, const _CharT* __last, + match_results& __m, vector& __lc, regex_constants::match_flag_type __flags) const; template @@ -2495,16 +2494,16 @@ private: __match_at_start_ecma(_BidirectionalIterator __first, _BidirectionalIterator __last, match_results<_BidirectionalIterator, _Allocator>& __m, regex_constants::match_flag_type __flags) const; - template + template bool __match_at_start_posix_nosubs(const _CharT* __first, const _CharT* __last, - match_results<_BidirectionalIterator, _Allocator>& __m, + match_results& __m, vector& __lc, regex_constants::match_flag_type __flags) const; - template + template bool - __match_at_start_posix_subs(_BidirectionalIterator __first, _BidirectionalIterator __last, - match_results<_BidirectionalIterator, _Allocator>& __m, + __match_at_start_posix_subs(const _CharT* __first, const _CharT* __last, + match_results& __m, vector& __lc, regex_constants::match_flag_type __flags) const; @@ -2514,6 +2513,44 @@ private: regex_search(_B, _B, match_results<_B, _A>&, const basic_regex<_C, _T>&, regex_constants::match_flag_type); + template + friend + bool + regex_search(const _C*, const _C*, match_results&, + const basic_regex<_C, _T>&, regex_constants::match_flag_type); + + template + friend + bool + regex_search(_B, _B, const basic_regex<_C, _T>&, + regex_constants::match_flag_type); + + template + friend + bool + regex_search(const _C*, const _C*, + const basic_regex<_C, _T>&, regex_constants::match_flag_type); + + template + friend + bool + regex_search(const _C*, match_results&, const basic_regex<_C, _T>&, + regex_constants::match_flag_type); + + template + friend + bool + regex_search(const basic_string<_C, _ST, _SA>& __s, + const basic_regex<_C, _T>& __e, + regex_constants::match_flag_type __flags); + + template + friend + bool + regex_search(const basic_string<_C, _ST, _SA>& __s, + match_results::const_iterator, _A>&, + const basic_regex<_C, _T>& __e, + regex_constants::match_flag_type __flags); }; template @@ -3975,6 +4012,29 @@ public: // swap: void swap(match_results& __m); + template + void __assign(_BidirectionalIterator __f, _BidirectionalIterator __l, + const match_results<_B, _A>& __m) + { + _B __mf = __m.prefix().first; + __matches_.resize(__m.size()); + for (size_type __i = 0; __i < __matches_.size(); ++__i) + { + __matches_[__i].first = next(__f, _STD::distance(__mf, __m[__i].first)); + __matches_[__i].second = next(__f, _STD::distance(__mf, __m[__i].second)); + __matches_[__i].matched = __m[__i].matched; + } + __unmatched_.first = __l; + __unmatched_.second = __l; + __unmatched_.matched = false; + __prefix_.first = next(__f, _STD::distance(__mf, __m.prefix().first)); + __prefix_.second = next(__f, _STD::distance(__mf, __m.prefix().second)); + __prefix_.matched = __m.prefix().matched; + __suffix_.first = next(__f, _STD::distance(__mf, __m.suffix().first)); + __suffix_.second = next(__f, _STD::distance(__mf, __m.suffix().second)); + __suffix_.matched = __m.suffix().matched; + } + private: void __init(unsigned __s, _BidirectionalIterator __f, _BidirectionalIterator __l); @@ -4047,18 +4107,17 @@ basic_regex<_CharT, _Traits>::__match_at_start_ecma( } template -template +template bool basic_regex<_CharT, _Traits>::__match_at_start_posix_nosubs( const _CharT* __first, const _CharT* __last, - match_results<_BidirectionalIterator, _Allocator>& __m, + match_results& __m, vector& __lc, regex_constants::match_flag_type __flags) const { - typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type; deque<__state> __states; - difference_type __highest_j = 0; - difference_type _N = _STD::distance(__first, __last); + ptrdiff_t __highest_j = 0; + ptrdiff_t _N = _STD::distance(__first, __last); __node* __st = __start_.get(); if (__st) { @@ -4126,22 +4185,21 @@ basic_regex<_CharT, _Traits>::__match_at_start_posix_nosubs( } template -template +template bool basic_regex<_CharT, _Traits>::__match_at_start_posix_subs( - _BidirectionalIterator __first, _BidirectionalIterator __last, - match_results<_BidirectionalIterator, _Allocator>& __m, + const _CharT* __first, const _CharT* __last, + match_results& __m, vector& __lc, regex_constants::match_flag_type __flags) const { - typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type; vector<__state> __states; - vector<_BidirectionalIterator> __current_stack; - vector > __saved_matches; + vector __current_stack; + vector > __saved_matches; __state __best_state; - difference_type __j = 0; - difference_type __highest_j = 0; - difference_type _N = _STD::distance(__first, __last); + ptrdiff_t __j = 0; + ptrdiff_t __highest_j = 0; + ptrdiff_t _N = _STD::distance(__first, __last); __node* __st = __start_.get(); if (__st) { @@ -4154,7 +4212,7 @@ basic_regex<_CharT, _Traits>::__match_at_start_posix_subs( __states.back().__loop_data_.resize(__loop_count()); __states.back().__node_ = __st; __states.back().__flags_ = __flags; - _BidirectionalIterator __current = __first; + const _CharT* __current = __first; bool __matched = false; do { @@ -4212,11 +4270,11 @@ basic_regex<_CharT, _Traits>::__match_at_start_posix_subs( } template -template +template bool basic_regex<_CharT, _Traits>::__match_at_start( - _BidirectionalIterator __first, _BidirectionalIterator __last, - match_results<_BidirectionalIterator, _Allocator>& __m, + const _CharT* __first, const _CharT* __last, + match_results& __m, vector& __lc, regex_constants::match_flag_type __flags) const { @@ -4228,11 +4286,11 @@ basic_regex<_CharT, _Traits>::__match_at_start( } template -template +template bool basic_regex<_CharT, _Traits>::__search( - _BidirectionalIterator __first, _BidirectionalIterator __last, - match_results<_BidirectionalIterator, _Allocator>& __m, + const _CharT* __first, const _CharT* __last, + match_results& __m, regex_constants::match_flag_type __flags) const { if (__left_anchor_) @@ -4274,6 +4332,21 @@ regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last, match_results<_BidirectionalIterator, _Allocator>& __m, const basic_regex<_CharT, _Traits>& __e, regex_constants::match_flag_type __flags = regex_constants::match_default) +{ + basic_string<_CharT> __s(__first, __last); + match_results __mc; + bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags); + __m.__assign(__first, __last, __mc); + return __r; +} + +template +inline _LIBCPP_INLINE_VISIBILITY +bool +regex_search(const _CharT* __first, const _CharT* __last, + match_results& __m, + const basic_regex<_CharT, _Traits>& __e, + regex_constants::match_flag_type __flags = regex_constants::match_default) { return __e.__search(__first, __last, __m, __flags); } @@ -4285,8 +4358,20 @@ regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last, const basic_regex<_CharT, _Traits>& __e, regex_constants::match_flag_type __flags = regex_constants::match_default) { - match_results<_BidirectionalIterator> __m; - return _STD::regex_search(__first, __last, __m, __e, __flags); + basic_string<_CharT> __s(__first, __last); + match_results __mc; + return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags); +} + +template +inline _LIBCPP_INLINE_VISIBILITY +bool +regex_search(const _CharT* __first, const _CharT* __last, + const basic_regex<_CharT, _Traits>& __e, + regex_constants::match_flag_type __flags = regex_constants::match_default) +{ + match_results __mc; + return __e.__search(__first, __last, __mc, __flags); } template @@ -4296,7 +4381,7 @@ regex_search(const _CharT* __str, match_results& __m, const basic_regex<_CharT, _Traits>& __e, regex_constants::match_flag_type __flags = regex_constants::match_default) { - return _STD::regex_search(__str, __str + _Traits::length(__str), __m, __e, __flags); + return __e.__search(__str, __str + _Traits::length(__str), __m, __flags); } template @@ -4305,7 +4390,8 @@ bool regex_search(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e, regex_constants::match_flag_type __flags = regex_constants::match_default) { - return _STD::regex_search(__str, __str + _Traits::length(__str), __e, __flags); + match_results __m; + return _STD::regex_search(__str, __m, __e, __flags); } template @@ -4315,7 +4401,8 @@ regex_search(const basic_string<_CharT, _ST, _SA>& __s, const basic_regex<_CharT, _Traits>& __e, regex_constants::match_flag_type __flags = regex_constants::match_default) { - return _STD::regex_search(__s.begin(), __s.end(), __e, __flags); + match_results __mc; + return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags); } template @@ -4326,7 +4413,10 @@ regex_search(const basic_string<_CharT, _ST, _SA>& __s, const basic_regex<_CharT, _Traits>& __e, regex_constants::match_flag_type __flags = regex_constants::match_default) { - return _STD::regex_search(__s.begin(), __s.end(), __m, __e, __flags); + match_results __mc; + bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags); + __m.__assign(__s.begin(), __s.end(), __mc); + return __r; } // regex_match diff --git a/test/re/re.alg/re.alg.search/basic.pass.cpp b/test/re/re.alg/re.alg.search/basic.pass.cpp index 7d10dfcd5..95791c891 100644 --- a/test/re/re.alg/re.alg.search/basic.pass.cpp +++ b/test/re/re.alg/re.alg.search/basic.pass.cpp @@ -19,6 +19,8 @@ #include #include +#include "../../iterators.h" + int main() { { @@ -741,4 +743,25 @@ int main() assert(m.position(0) == 1); assert(m.str(0) == "1a45ce"); } + { + const char r[] = "^[-+]\\{0,1\\}[0-9]\\{1,\\}[CF]$"; + std::ptrdiff_t sr = std::char_traits::length(r); + typedef forward_iterator FI; + typedef bidirectional_iterator BI; + std::regex regex(FI(r), FI(r+sr), std::regex_constants::basic); + std::match_results m; + const char s[] = "-40C"; + std::ptrdiff_t ss = std::char_traits::length(s); + assert(std::regex_search(BI(s), BI(s+ss), m, regex)); + assert(m.size() == 1); + assert(!m.prefix().matched); + assert(m.prefix().first == BI(s)); + assert(m.prefix().second == m[0].first); + assert(!m.suffix().matched); + assert(m.suffix().first == m[0].second); + assert(m.suffix().second == m[0].second); + assert(m.length(0) == 4); + assert(m.position(0) == 0); + assert(m.str(0) == s); + } }