fix recursion depth: cstring algorithm

This commit is contained in:
bolero-MURAKAMI 2013-01-13 01:16:48 +09:00
parent f26032dce8
commit b51b14efa9
25 changed files with 792 additions and 142 deletions

View file

@ -1,24 +1,94 @@
#ifndef SPROUT_CSTRING_STRRCHR_HPP
#define SPROUT_CSTRING_STRRCHR_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>
namespace sprout {
// Copyright (C) 2011 RiSK (sscrisk)
namespace detail {
template<typename ForwardIterator, typename T>
inline SPROUT_CONSTEXPR ForwardIterator
strrchr_impl_check(sprout::pair<ForwardIterator, ForwardIterator> const& found, T const& value) {
return *found.second == value ? found.second
: !value ? found.first
: ForwardIterator()
;
}
template<typename ForwardIterator, typename T>
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator>
strrchr_impl_1(
sprout::pair<ForwardIterator, ForwardIterator> const& current,
T const& value,
typename std::iterator_traits<ForwardIterator>::difference_type n
)
{
typedef sprout::pair<ForwardIterator, ForwardIterator> type;
return !*current.first ? current
: n == 1 ? *current.first == value ? type(sprout::next(current.first), current.first) : type(sprout::next(current.first), current.second)
: sprout::detail::strrchr_impl_1(
sprout::detail::strrchr_impl_1(
current,
value, n / 2
),
value, n - n / 2
)
;
}
template<typename ForwardIterator, typename T>
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator>
strrchr_impl(
sprout::pair<ForwardIterator, ForwardIterator> const& current,
T const& value,
typename std::iterator_traits<ForwardIterator>::difference_type n
)
{
typedef sprout::pair<ForwardIterator, ForwardIterator> type;
return !*current.first ? current
: sprout::detail::strrchr_impl(
sprout::detail::strrchr_impl_1(
current,
value, n
),
value, n * 2
)
;
}
template<typename ForwardIterator, typename T>
inline SPROUT_CONSTEXPR ForwardIterator
strrchr(ForwardIterator first, T const& value) {
typedef sprout::pair<ForwardIterator, ForwardIterator> type;
return sprout::detail::strrchr_impl_check(
sprout::detail::strrchr_impl(type(first, first), value, 1),
value
);
}
} // namespace detail
// 7.21.5.5 strrchr ŠÖ<C5A0>
//
// recursion depth:
// O(log N)
//
inline SPROUT_CONSTEXPR char const*
strrchr(char const* s, int c) {
return *s == static_cast<char>(c) && (!*s || !sprout::strrchr(s + 1, c))? s
: !*s ? nullptr
: sprout::strrchr(s + 1, c)
;
return sprout::detail::strrchr(s, static_cast<char>(c));
}
inline SPROUT_CONSTEXPR char*
strrchr(char* s, int c) {
return const_cast<char*>(sprout::strrchr(const_cast<char const*>(s), c));
return sprout::detail::strrchr(s, static_cast<char>(c));
}
template<typename Elem>
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::is_char_type<Elem>::value,
Elem*
>::type
strrchr(Elem* s, typename std::decay<Elem>::type c) {
return sprout::detail::strrchr(s, c);
}
} // namespace sprout