mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2025-07-04 14:14: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,24 +1,92 @@
|
|||
#ifndef SPROUT_CSTRING_STRCHR_HPP
|
||||
#define SPROUT_CSTRING_STRCHR_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/detail/str.hpp>
|
||||
|
||||
namespace sprout {
|
||||
// Copyright (C) 2011 RiSK (sscrisk)
|
||||
namespace detail {
|
||||
template<typename InputIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<InputIterator, bool>
|
||||
strchr_impl_1(
|
||||
sprout::pair<InputIterator, bool> const& current,
|
||||
T const& value, typename std::iterator_traits<InputIterator>::difference_type n
|
||||
)
|
||||
{
|
||||
typedef sprout::pair<InputIterator, bool> type;
|
||||
return current.second || !*current.first ? current
|
||||
: n == 1 ? *current.first == value ? type(current.first, true) : type(sprout::next(current.first), false)
|
||||
: sprout::detail::strchr_impl_1(
|
||||
sprout::detail::strchr_impl_1(
|
||||
current,
|
||||
value, n / 2
|
||||
),
|
||||
value, n - n / 2
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename InputIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<InputIterator, bool>
|
||||
strchr_impl(
|
||||
sprout::pair<InputIterator, bool> const& current,
|
||||
T const& value, typename std::iterator_traits<InputIterator>::difference_type n
|
||||
)
|
||||
{
|
||||
typedef sprout::pair<InputIterator, bool> type;
|
||||
return current.second || !*current.first ? current
|
||||
: sprout::detail::strchr_impl(
|
||||
sprout::detail::strchr_impl_1(
|
||||
current,
|
||||
value, n
|
||||
),
|
||||
value, n * 2
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename InputIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR InputIterator
|
||||
strchr(InputIterator first, T const& value) {
|
||||
typedef sprout::pair<InputIterator, bool> type;
|
||||
return sprout::detail::strchr_impl(type(first, false), value, 1).first;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// 7.21.5.2 strchr ŠÖ<C5A0>”
|
||||
//
|
||||
// recursion depth:
|
||||
// O(log N)
|
||||
//
|
||||
inline SPROUT_CONSTEXPR char const*
|
||||
strchr(char const* s, int c) {
|
||||
return *s == static_cast<char>(c) ? s
|
||||
: !*s ? nullptr
|
||||
: sprout::strchr(s + 1, c)
|
||||
;
|
||||
return sprout::detail::str_find_check(
|
||||
sprout::detail::strchr(s, static_cast<char>(c)),
|
||||
static_cast<char>(c)
|
||||
);
|
||||
}
|
||||
|
||||
inline SPROUT_CONSTEXPR char*
|
||||
strchr(char* s, int c) {
|
||||
return const_cast<char*>(sprout::strchr(const_cast<char const*>(s), c));
|
||||
return sprout::detail::str_find_check(
|
||||
sprout::detail::strchr(s, static_cast<char>(c)),
|
||||
static_cast<char>(c)
|
||||
);
|
||||
}
|
||||
|
||||
template<typename Elem>
|
||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||
sprout::is_char_type<Elem>::value,
|
||||
Elem*
|
||||
>::type
|
||||
strchr(Elem* s, typename std::decay<Elem>::type c) {
|
||||
return sprout::detail::str_find_check(
|
||||
sprout::detail::strchr(s, c),
|
||||
c
|
||||
);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue