mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2025-07-08 14:34:09 +00:00
fix recursion depth: cstring algorithm
This commit is contained in:
parent
f26032dce8
commit
b51b14efa9
25 changed files with 792 additions and 142 deletions
|
@ -1,25 +1,93 @@
|
|||
#ifndef SPROUT_CSTRING_STRPBRK_HPP
|
||||
#define SPROUT_CSTRING_STRPBRK_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <iterator>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include <sprout/utility/pair.hpp>
|
||||
#include <sprout/type_traits/is_char_type.hpp>
|
||||
#include <sprout/cstring/strchr.hpp>
|
||||
#include <sprout/detail/str.hpp>
|
||||
|
||||
namespace sprout {
|
||||
// Copyright (C) 2011 RiSK (sscrisk)
|
||||
namespace detail {
|
||||
template<typename InputIterator1, typename ForwardIterator2>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, bool>
|
||||
strpbrk_impl_1(
|
||||
sprout::pair<InputIterator1, bool> current,
|
||||
ForwardIterator2 first2,
|
||||
typename std::iterator_traits<InputIterator1>::difference_type n
|
||||
)
|
||||
{
|
||||
typedef sprout::pair<InputIterator1, bool> type;
|
||||
return current.second || !*current.first ? current
|
||||
: n == 1 ? sprout::strchr(first2, *current.first)
|
||||
? type(current.first, true)
|
||||
: type(sprout::next(current.first), false)
|
||||
: sprout::detail::strpbrk_impl_1(
|
||||
sprout::detail::strpbrk_impl_1(
|
||||
current,
|
||||
first2, n / 2
|
||||
),
|
||||
first2, n - n / 2
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename InputIterator1, typename ForwardIterator2>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, bool>
|
||||
strpbrk_impl(
|
||||
sprout::pair<InputIterator1, bool> current,
|
||||
ForwardIterator2 first2,
|
||||
typename std::iterator_traits<InputIterator1>::difference_type n
|
||||
)
|
||||
{
|
||||
typedef sprout::pair<InputIterator1, bool> type;
|
||||
return current.second || !*current.first ? current
|
||||
: sprout::detail::strpbrk_impl(
|
||||
sprout::detail::strpbrk_impl_1(
|
||||
current,
|
||||
first2, n
|
||||
),
|
||||
first2, n * 2
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename InputIterator1, typename ForwardIterator2>
|
||||
inline SPROUT_CONSTEXPR InputIterator1
|
||||
strpbrk(InputIterator1 first1, ForwardIterator2 first2) {
|
||||
typedef sprout::pair<InputIterator1, bool> type;
|
||||
return sprout::detail::strpbrk_impl(type(first1, false), first2, 1).first;
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
// 7.21.5.4 strpbrk ŠÖ<C5A0>”
|
||||
//
|
||||
// recursion depth:
|
||||
// O(log(N1+N2))
|
||||
//
|
||||
inline SPROUT_CONSTEXPR char const*
|
||||
strpbrk(char const* s1, char const* s2) {
|
||||
return !*s1 ? nullptr
|
||||
: sprout::strchr(s2, *s1) ? s1
|
||||
: sprout::strpbrk(s1 + 1, s2)
|
||||
;
|
||||
return sprout::detail::str_find_check(
|
||||
sprout::detail::strpbrk(s1, s2)
|
||||
);
|
||||
}
|
||||
|
||||
inline SPROUT_CONSTEXPR char*
|
||||
strpbrk(char* s1, char const* s2) {
|
||||
return const_cast<char*>(sprout::strpbrk(const_cast<char const*>(s1), s2));
|
||||
return sprout::detail::str_find_check(
|
||||
sprout::detail::strpbrk(s1, s2)
|
||||
);
|
||||
}
|
||||
|
||||
template<typename Elem>
|
||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||
sprout::is_char_type<Elem>::value,
|
||||
Elem*
|
||||
>::type
|
||||
strpbrk(Elem* s1, typename std::remove_const<Elem>::type const* s2) {
|
||||
return sprout::detail::str_find_check(
|
||||
sprout::detail::strpbrk(s1, s2)
|
||||
);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue