1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/cstring/strrchr.hpp

96 lines
2.9 KiB
C++
Raw Normal View History

2012-04-01 13:15:09 +00:00
#ifndef SPROUT_CSTRING_STRRCHR_HPP
#define SPROUT_CSTRING_STRRCHR_HPP
2013-01-12 16:16:48 +00:00
#include <iterator>
#include <type_traits>
2012-04-01 13:15:09 +00:00
#include <sprout/config.hpp>
2013-01-12 16:16:48 +00:00
#include <sprout/iterator/operation.hpp>
2013-02-07 14:12:57 +00:00
#include <sprout/utility/pair/pair.hpp>
2013-01-12 16:16:48 +00:00
#include <sprout/type_traits/is_char_type.hpp>
2012-04-01 13:15:09 +00:00
namespace sprout {
2013-01-12 16:16:48 +00:00
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
2012-04-01 13:15:09 +00:00
// 7.21.5.5 strrchr <20>֐<EFBFBD>
2013-01-12 16:16:48 +00:00
//
// recursion depth:
// O(log N)
//
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR char const*
strrchr(char const* s, int c) {
2013-01-12 16:16:48 +00:00
return sprout::detail::strrchr(s, static_cast<char>(c));
2012-04-01 13:15:09 +00:00
}
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR char*
strrchr(char* s, int c) {
2013-01-12 16:16:48 +00:00
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);
2012-04-01 13:15:09 +00:00
}
} // namespace sprout
#endif // #ifndef SPROUT_CSTRING_STRRCHR_HPP