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

83 lines
2.8 KiB
C++
Raw Normal View History

2012-04-01 13:15:09 +00:00
#ifndef SPROUT_CSTRING_STRSPN_HPP
2012-12-21 16:02:49 +00:00
#define SPROUT_CSTRING_STRSPN_HPP
2012-04-01 13:15:09 +00:00
#include <cstddef>
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>
#include <sprout/tuple/tuple.hpp>
#include <sprout/type_traits/is_char_type.hpp>
2012-04-01 13:15:09 +00:00
#include <sprout/cstring/strchr.hpp>
namespace sprout {
2013-01-12 16:16:48 +00:00
namespace detail {
template<typename InputIterator1, typename ForwardIterator2>
inline SPROUT_CONSTEXPR sprout::tuples::tuple<InputIterator1, std::size_t, bool>
strspn_impl_1(
sprout::tuples::tuple<InputIterator1, std::size_t, bool> const& current,
ForwardIterator2 first2, typename std::iterator_traits<InputIterator1>::difference_type n
)
{
typedef sprout::tuples::tuple<InputIterator1, std::size_t, bool> type;
return sprout::tuples::get<2>(current) || !*sprout::tuples::get<0>(current) ? current
: n == 1 ? *sprout::detail::strchr(first2, *sprout::tuples::get<0>(current))
? type(sprout::next(sprout::tuples::get<0>(current)), sprout::tuples::get<1>(current) + 1, false)
: type(sprout::tuples::get<0>(current), sprout::tuples::get<1>(current), true)
: sprout::detail::strspn_impl_1(
sprout::detail::strspn_impl_1(
current,
first2, n / 2
),
first2, n - n / 2
)
;
}
template<typename InputIterator1, typename ForwardIterator2>
inline SPROUT_CONSTEXPR sprout::tuples::tuple<InputIterator1, std::size_t, bool>
strspn_impl(
sprout::tuples::tuple<InputIterator1, std::size_t, bool> const& current,
ForwardIterator2 first2, typename std::iterator_traits<InputIterator1>::difference_type n
)
{
typedef sprout::tuples::tuple<InputIterator1, std::size_t, bool> type;
return sprout::tuples::get<2>(current) || !*sprout::tuples::get<0>(current) ? current
: sprout::detail::strspn_impl(
sprout::detail::strspn_impl_1(
current,
first2, n
),
first2, n * 2
)
;
}
template<typename InputIterator1, typename ForwardIterator2>
inline SPROUT_CONSTEXPR std::size_t
strspn(InputIterator1 first1, ForwardIterator2 first2) {
typedef sprout::tuples::tuple<InputIterator1, std::size_t, bool> type;
return sprout::tuples::get<1>(sprout::detail::strspn_impl(type(first1, 0, false), first2, 1));
}
} // namespace detail
2012-04-01 13:15:09 +00:00
// 7.21.5.6 strspn <20>֐<EFBFBD>
2013-01-12 16:16:48 +00:00
//
// recursion depth:
// O(log(N1+N2))
//
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR std::size_t
strspn(char const* s1, char const* s2) {
2013-01-12 16:16:48 +00:00
return sprout::detail::strspn(s1, s2);
}
template<typename Elem>
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::is_char_type<Elem>::value,
std::size_t
>::type
strspn(Elem* s1, Elem* s2) {
return sprout::detail::strspn(s1, s2);
2012-04-01 13:15:09 +00:00
}
} // namespace sprout
2012-12-21 16:02:49 +00:00
#endif // #ifndef SPROUT_CSTRING_STRSPN_HPP