Sprout/sprout/cstring/strchr.hpp

100 lines
3 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_STRCHR_HPP
#define SPROUT_CSTRING_STRCHR_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>
#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 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
2013-01-12 16:16:48 +00:00
),
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
)
{
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
2012-04-01 13:15:09 +00:00
2016-04-05 09:21:13 +00:00
// 7.21.5.2 strchr function
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*
strchr(char const* s, int c) {
2013-01-12 16:16:48 +00:00
return sprout::detail::str_find_check(
sprout::detail::strchr(s, static_cast<char>(c)),
static_cast<char>(c)
);
2012-04-01 13:15:09 +00:00
}
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR char*
strchr(char* s, int c) {
2013-01-12 16:16:48 +00:00
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
);
2012-04-01 13:15:09 +00:00
}
} // namespace sprout
#endif // #ifndef SPROUT_CSTRING_STRCHR_HPP