Skip to content

Commit

Permalink
Untag dispatch regex (#2484)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephan T. Lavavej <stl@nuwen.net>
Co-authored-by: Casey Carter <Casey@Carter.net>
  • Loading branch information
3 people authored Mar 19, 2022
1 parent c221b34 commit b973fd0
Showing 1 changed file with 56 additions and 67 deletions.
123 changes: 56 additions & 67 deletions stl/inc/regex
Original file line number Diff line number Diff line change
Expand Up @@ -497,44 +497,6 @@ private:
regex_constants::error_type _Err;
};

template <class _Traits, class _FwdIt1, class _FwdIt2>
int _Iter_compare3(_FwdIt1 _First1, _FwdIt1 _Last1, _FwdIt2 _First2, _FwdIt2 _Last2, true_type) {
// _Iter_compare for memory buffer ranges
return _Traits_compare<_Traits>(
_First1, static_cast<size_t>(_Last1 - _First1), _First2, static_cast<size_t>(_Last2 - _First2));
}

template <class _Traits, class _FwdIt1, class _FwdIt2>
int _Iter_compare3(_FwdIt1 _First1, _FwdIt1 _Last1, _FwdIt2 _First2, _FwdIt2 _Last2, false_type) {
// _Iter_compare for general ranges
const pair<_FwdIt1, _FwdIt2> _Cmp = _STD mismatch(_First1, _Last1, _First2, _Last2, _Char_traits_eq<_Traits>{});

if (_Cmp.first == _Last1) {
if (_Cmp.second == _Last2) {
return 0;
} else {
return -1;
}
}

if (_Cmp.second == _Last2) {
return 1;
}

if (_Traits::lt(*_Cmp.first, *_Cmp.second)) {
return -1;
} else {
return 1;
}
}

template <class _Traits, class _FwdIt1, class _FwdIt2>
int _Iter_compare2(_FwdIt1 _First1, _FwdIt1 _Last1, _FwdIt2 _First2, _FwdIt2 _Last2) {
// decide which _Iter_compare implementation to use
return _Iter_compare3<_Traits>(
_First1, _Last1, _First2, _Last2, bool_constant<conjunction_v<is_pointer<_FwdIt1>, is_pointer<_FwdIt2>>>{});
}

template <class _Traits, class _FwdIt1, class _FwdIt2>
int _Iter_compare(_FwdIt1 _First1, _FwdIt1 _Last1, _FwdIt2 _First2, _FwdIt2 _Last2) {
// compare two iterator ranges:
Expand All @@ -547,8 +509,36 @@ int _Iter_compare(_FwdIt1 _First1, _FwdIt1 _Last1, _FwdIt2 _First2, _FwdIt2 _Las

_Adl_verify_range(_First1, _Last1);
_Adl_verify_range(_First2, _Last2);
return _Iter_compare2<_Traits>(
_Get_unwrapped(_First1), _Get_unwrapped(_Last1), _Get_unwrapped(_First2), _Get_unwrapped(_Last2));

auto _UFirst1 = _Get_unwrapped(_First1);
auto _ULast1 = _Get_unwrapped(_Last1);
auto _UFirst2 = _Get_unwrapped(_First2);
auto _ULast2 = _Get_unwrapped(_Last2);

if constexpr (is_pointer_v<decltype(_UFirst1)> && is_pointer_v<decltype(_UFirst2)>) {
return _Traits_compare<_Traits>(
_UFirst1, static_cast<size_t>(_ULast1 - _UFirst1), _UFirst2, static_cast<size_t>(_ULast2 - _UFirst2));
} else {
const auto _Cmp = _STD mismatch(_UFirst1, _ULast1, _UFirst2, _ULast2, _Char_traits_eq<_Traits>{});

if (_Cmp.first == _ULast1) {
if (_Cmp.second == _ULast2) {
return 0;
} else {
return -1;
}
}

if (_Cmp.second == _ULast2) {
return 1;
}

if (_Traits::lt(*_Cmp.first, *_Cmp.second)) {
return -1;
} else {
return 1;
}
}
}

inline bool _Is_word(unsigned char _UCh) {
Expand Down Expand Up @@ -1804,13 +1794,13 @@ public:
basic_regex() : _Rep(nullptr) {} // construct empty object

explicit basic_regex(_In_z_ const _Elem* _Ptr, flag_type _Flags = regex_constants::ECMAScript) : _Rep(nullptr) {
_Reset(_Ptr, _Ptr + _RxTraits::length(_Ptr), _Flags, random_access_iterator_tag{});
_Reset(_Ptr, _Ptr + _RxTraits::length(_Ptr), _Flags);
}

basic_regex(_In_reads_(_Count) const _Elem* _Ptr, size_t _Count, flag_type _Flags = regex_constants::ECMAScript)
: _Rep(nullptr) {
if (_Ptr) {
_Reset(_Ptr, _Ptr + _Count, _Flags, random_access_iterator_tag{});
_Reset(_Ptr, _Ptr + _Count, _Flags);
return;
}

Expand All @@ -1821,19 +1811,19 @@ public:
explicit basic_regex(
const basic_string<_Elem, _STtraits, _STalloc>& _Str, flag_type _Flags = regex_constants::ECMAScript)
: _Rep(nullptr) {
_Reset(_Str.data(), _Str.data() + static_cast<ptrdiff_t>(_Str.size()), _Flags, random_access_iterator_tag{});
_Reset(_Str.data(), _Str.data() + static_cast<ptrdiff_t>(_Str.size()), _Flags);
}

template <class _InIt>
basic_regex(_InIt _First, _InIt _Last, flag_type _Flags) : _Rep(nullptr) {
_Adl_verify_range(_First, _Last);
_Reset(_Get_unwrapped(_First), _Get_unwrapped(_Last), _Flags, _Iter_cat_t<_InIt>{});
_Reset(_Get_unwrapped(_First), _Get_unwrapped(_Last), _Flags);
}

template <class _InIt>
basic_regex(_InIt _First, _InIt _Last) : _Rep(nullptr) {
_Adl_verify_range(_First, _Last);
_Reset(_Get_unwrapped(_First), _Get_unwrapped(_Last), regex_constants::ECMAScript, _Iter_cat_t<_InIt>{});
_Reset(_Get_unwrapped(_First), _Get_unwrapped(_Last), regex_constants::ECMAScript);
}

basic_regex(const basic_regex& _Right)
Expand All @@ -1847,7 +1837,7 @@ public:
}

basic_regex(initializer_list<_Elem> _Ilist, flag_type _Flags = regex_constants::ECMAScript) : _Rep(nullptr) {
_Reset(_Ilist.begin(), _Ilist.end(), _Flags, random_access_iterator_tag{});
_Reset(_Ilist.begin(), _Ilist.end(), _Flags);
}

basic_regex& operator=(initializer_list<_Elem> _Ilist) { // replace with regular expression in initializer_list
Expand All @@ -1856,7 +1846,7 @@ public:

basic_regex& assign(initializer_list<_Elem> _Ilist, flag_type _Flags = regex_constants::ECMAScript) {
// replace with regular expression in initializer_list
_Reset(_Ilist.begin(), _Ilist.end(), _Flags, random_access_iterator_tag{});
_Reset(_Ilist.begin(), _Ilist.end(), _Flags);
return *this;
}

Expand Down Expand Up @@ -1896,14 +1886,13 @@ public:
}

basic_regex& operator=(_In_z_ const _Elem* _Ptr) {
_Reset(_Ptr, _Ptr + _RxTraits::length(_Ptr), ECMAScript, random_access_iterator_tag{});
_Reset(_Ptr, _Ptr + _RxTraits::length(_Ptr), ECMAScript);
return *this;
}

template <class _STtraits, class _STalloc>
basic_regex& operator=(const basic_string<_Elem, _STtraits, _STalloc>& _Str) {
_Reset(
_Str.data(), _Str.data() + static_cast<ptrdiff_t>(_Str.size()), ECMAScript, random_access_iterator_tag{});
_Reset(_Str.data(), _Str.data() + static_cast<ptrdiff_t>(_Str.size()), ECMAScript);
return *this;
}

Expand All @@ -1930,21 +1919,21 @@ public:

basic_regex& assign(
_In_reads_(_Count) const _Elem* _Ptr, size_t _Count, flag_type _Flags = regex_constants::ECMAScript) {
_Reset(_Ptr, _Ptr + _Count, _Flags, random_access_iterator_tag{});
_Reset(_Ptr, _Ptr + _Count, _Flags);
return *this;
}

template <class _STtraits, class _STalloc>
basic_regex& assign(
const basic_string<_Elem, _STtraits, _STalloc>& _Str, flag_type _Flags = regex_constants::ECMAScript) {
_Reset(_Str.data(), _Str.data() + static_cast<ptrdiff_t>(_Str.size()), _Flags, random_access_iterator_tag{});
_Reset(_Str.data(), _Str.data() + static_cast<ptrdiff_t>(_Str.size()), _Flags);
return *this;
}

template <class _InIt>
basic_regex& assign(_InIt _First, _InIt _Last, flag_type _Flags = regex_constants::ECMAScript) {
_Adl_verify_range(_First, _Last);
_Reset(_Get_unwrapped(_First), _Get_unwrapped(_Last), _Flags, _Iter_cat_t<_InIt>{});
_Reset(_Get_unwrapped(_First), _Get_unwrapped(_Last), _Flags);
return *this;
}

Expand Down Expand Up @@ -1998,23 +1987,23 @@ private:
}

template <class _InIt>
void _Reset(_InIt _First, _InIt _Last, flag_type _Flags, input_iterator_tag) {
// build regular expression from input iterators
basic_string<_Iter_value_t<_InIt>> _Str(_First, _Last);

_Reset(_Str.data(), _Str.data() + static_cast<ptrdiff_t>(_Str.size()), _Flags, forward_iterator_tag{});
}

template <class _FwdIt>
void _Reset(_FwdIt _First, _FwdIt _Last, flag_type _Flags, forward_iterator_tag) {
// build regular expression from forward iterators
void _Reset(_InIt _First, _InIt _Last, flag_type _Flags) {
// build regular expression from iterator range
if constexpr (_Is_fwd_iter_v<_InIt>) {
#if _ENHANCED_REGEX_VISUALIZER
_Visualization.assign(_First, _Last);
_Visualization.assign(_First, _Last);
#endif // _ENHANCED_REGEX_VISUALIZER

_Parser<_FwdIt, _Elem, _RxTraits> _Prs(_Traits, _First, _Last, _Flags);
_Root_node* _Rx = _Prs._Compile();
_Reset(_Rx);
_Parser<_InIt, _Elem, _RxTraits> _Prs(_Traits, _First, _Last, _Flags);
_Root_node* _Rx = _Prs._Compile();
_Reset(_Rx);
} else {
static_assert(_Is_input_iter_v<_InIt>, "Iterators must be at least input iterators");

basic_string<_Iter_value_t<_InIt>> _Str(_First, _Last);

_Reset(_Str.data(), _Str.data() + static_cast<ptrdiff_t>(_Str.size()), _Flags);
}
}

void _Reset(_Root_node* _Rx) { // build regular expression holding root node _Rx
Expand Down

0 comments on commit b973fd0

Please sign in to comment.