Sprout/sprout/cstring/strstr.hpp

162 lines
5.9 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2016-02-25 09:48:28 +00:00
Copyright (c) 2011-2016 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
https://github.com/bolero-MURAKAMI/Sprout
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
2012-04-01 13:15:09 +00:00
#ifndef SPROUT_CSTRING_STRSTR_HPP
#define SPROUT_CSTRING_STRSTR_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-02-07 15:49:47 +00:00
#include <sprout/tuple/tuple/tuple.hpp>
#include <sprout/tuple/tuple/get.hpp>
2013-01-12 16:16:48 +00:00
#include <sprout/type_traits/is_char_type.hpp>
#include <sprout/detail/str.hpp>
2012-04-01 13:15:09 +00:00
namespace sprout {
2013-01-12 16:16:48 +00:00
namespace detail {
template<typename ForwardIterator1, typename ForwardIterator2>
inline SPROUT_CONSTEXPR sprout::tuples::tuple<ForwardIterator1, ForwardIterator2, bool>
strstr_one_impl_1(
sprout::tuples::tuple<ForwardIterator1, ForwardIterator2, bool> const& current,
ForwardIterator1 first1_, typename std::iterator_traits<ForwardIterator1>::difference_type n
)
{
typedef sprout::tuples::tuple<ForwardIterator1, ForwardIterator2, bool> type;
return sprout::tuples::get<2>(current) ? current
: !*sprout::tuples::get<1>(current) ? type(first1_, sprout::tuples::get<1>(current), true)
: !*sprout::tuples::get<0>(current) ? type(sprout::tuples::get<0>(current), sprout::tuples::get<1>(current), true)
: n == 1 ? !(*sprout::tuples::get<0>(current) == *sprout::tuples::get<1>(current))
? type(sprout::next(sprout::tuples::get<0>(current)), sprout::tuples::get<1>(current), true)
: type(sprout::next(sprout::tuples::get<0>(current)), sprout::next(sprout::tuples::get<1>(current)), false)
: sprout::detail::strstr_one_impl_1(
sprout::detail::strstr_one_impl_1(
current,
first1_, n / 2
),
first1_, n - n / 2
)
;
}
template<typename ForwardIterator1, typename ForwardIterator2>
inline SPROUT_CONSTEXPR sprout::tuples::tuple<ForwardIterator1, ForwardIterator2, bool>
strstr_one_impl(
sprout::tuples::tuple<ForwardIterator1, ForwardIterator2, bool> const& current,
ForwardIterator1 first1_, typename std::iterator_traits<ForwardIterator1>::difference_type n
)
{
typedef sprout::tuples::tuple<ForwardIterator1, ForwardIterator2, bool> type;
return sprout::tuples::get<2>(current) ? current
: !*sprout::tuples::get<1>(current) ? type(first1_, sprout::tuples::get<1>(current), true)
: !*sprout::tuples::get<0>(current) ? type(sprout::tuples::get<0>(current), sprout::tuples::get<1>(current), true)
: sprout::detail::strstr_one_impl(
sprout::detail::strstr_one_impl_1(
current,
first1_, n
),
first1_, n * 2
)
;
}
template<typename ForwardIterator1, typename ForwardIterator2>
inline SPROUT_CONSTEXPR ForwardIterator1
strstr_one(ForwardIterator1 first1, ForwardIterator2 first2) {
typedef sprout::tuples::tuple<ForwardIterator1, ForwardIterator2, bool> type;
return sprout::tuples::get<0>(sprout::detail::strstr_one_impl(type(first1, first2, false), first1, 1));
}
template<typename ForwardIterator>
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, bool>
strstr_impl_fork(sprout::pair<ForwardIterator, bool> const& current, ForwardIterator searched) {
typedef sprout::pair<ForwardIterator, bool> type;
return searched == current.first || !*searched ? type(searched, true)
: type(sprout::next(current.first), false)
;
}
template<typename ForwardIterator1, typename ForwardIterator2>
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator1, bool>
strstr_impl_1(
sprout::pair<ForwardIterator1, bool> const& current,
ForwardIterator2 first2,
typename std::iterator_traits<ForwardIterator1>::difference_type n
)
{
return current.second || !*current.first ? current
: n == 1 ? sprout::detail::strstr_impl_fork(
current,
sprout::detail::strstr_one(current.first, first2)
)
: sprout::detail::strstr_impl_1(
sprout::detail::strstr_impl_1(
current,
first2, n / 2
),
first2, n - n / 2
)
;
}
template<typename ForwardIterator1, typename ForwardIterator2>
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator1, bool>
strstr_impl(
sprout::pair<ForwardIterator1, bool> const& current,
ForwardIterator2 first2,
typename std::iterator_traits<ForwardIterator1>::difference_type n
)
{
return current.second || !*current.first ? current
: sprout::detail::strstr_impl(
sprout::detail::strstr_impl_1(
current,
first2, n
),
first2, n * 2
)
;
}
template<typename ForwardIterator1, typename ForwardIterator2>
inline SPROUT_CONSTEXPR ForwardIterator1
strstr(ForwardIterator1 first1, ForwardIterator2 first2) {
typedef sprout::pair<ForwardIterator1, bool> type;
return !*first2 ? first1
: sprout::detail::strstr_impl(type(first1, false), first2, 1).first
;
}
} // namespace detail
2012-04-01 13:15:09 +00:00
2016-04-05 09:21:13 +00:00
// 7.21.5.7 strstr function
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 char const*
strstr(char const* s1, char const* s2) {
2013-01-12 16:16:48 +00:00
return sprout::detail::str_find_check(
sprout::detail::strstr(s1, s2)
);
2012-04-01 13:15:09 +00:00
}
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR char*
strstr(char* s1, char const* s2) {
2013-01-12 16:16:48 +00:00
return sprout::detail::str_find_check(
sprout::detail::strstr(s1, s2)
);
}
template<typename Elem>
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::is_char_type<Elem>::value,
Elem*
>::type
strstr(Elem* s1, typename std::remove_const<Elem>::type const* s2) {
return sprout::detail::str_find_check(
sprout::detail::strstr(s1, s2)
);
2012-04-01 13:15:09 +00:00
}
} // namespace sprout
#endif // #ifndef SPROUT_CSTRING_STRSTR_HPP