2013-08-08 09:54:33 +00:00
|
|
|
|
/*=============================================================================
|
|
|
|
|
Copyright (c) 2011-2013 Bolero MURAKAMI
|
|
|
|
|
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_STRLEN_HPP
|
|
|
|
|
#define SPROUT_CSTRING_STRLEN_HPP
|
|
|
|
|
|
|
|
|
|
#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-03 08:01:50 +00:00
|
|
|
|
#include <sprout/iterator/operation.hpp>
|
2013-01-12 16:16:48 +00:00
|
|
|
|
#include <sprout/iterator/ptr_index_iterator.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>
|
2013-01-11 19:08:44 +00:00
|
|
|
|
#include <sprout/algorithm/find.hpp>
|
2012-04-01 13:15:09 +00:00
|
|
|
|
|
|
|
|
|
namespace sprout {
|
2013-01-12 16:16:48 +00:00
|
|
|
|
namespace detail {
|
|
|
|
|
template<typename InputIterator>
|
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<InputIterator, std::size_t>
|
|
|
|
|
strlen_impl_1(
|
|
|
|
|
sprout::pair<InputIterator, std::size_t> const& current,
|
|
|
|
|
typename std::iterator_traits<InputIterator>::difference_type n
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
typedef sprout::pair<InputIterator, std::size_t> type;
|
|
|
|
|
return !*current.first ? current
|
|
|
|
|
: n == 1 ? type(sprout::next(current.first), current.second + 1)
|
|
|
|
|
: sprout::detail::strlen_impl_1(
|
|
|
|
|
sprout::detail::strlen_impl_1(
|
|
|
|
|
current,
|
|
|
|
|
n / 2
|
|
|
|
|
),
|
|
|
|
|
n - n / 2
|
|
|
|
|
)
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
template<typename InputIterator>
|
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<InputIterator, std::size_t>
|
|
|
|
|
strlen_impl(
|
|
|
|
|
sprout::pair<InputIterator, std::size_t> const& current,
|
|
|
|
|
typename std::iterator_traits<InputIterator>::difference_type n
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
return !*current.first ? current
|
|
|
|
|
: sprout::detail::strlen_impl(
|
|
|
|
|
sprout::detail::strlen_impl_1(
|
|
|
|
|
current,
|
|
|
|
|
n
|
|
|
|
|
),
|
|
|
|
|
n * 2
|
|
|
|
|
)
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
template<typename InputIterator>
|
|
|
|
|
inline SPROUT_CONSTEXPR std::size_t
|
|
|
|
|
strlen(InputIterator first) {
|
|
|
|
|
typedef sprout::pair<InputIterator, std::size_t> type;
|
|
|
|
|
return sprout::detail::strlen_impl(type(first, 0), 1).second;
|
|
|
|
|
}
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
2013-03-22 05:24:19 +00:00
|
|
|
|
// 7.21.6.3 strlen <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 std::size_t
|
|
|
|
|
strlen(char const* s) {
|
2013-01-12 16:16:48 +00:00
|
|
|
|
return sprout::detail::strlen(s);
|
2012-04-01 13:15:09 +00:00
|
|
|
|
}
|
2012-12-21 16:02:49 +00:00
|
|
|
|
inline SPROUT_CONSTEXPR std::size_t
|
|
|
|
|
strlen(char const* s, std::size_t n) {
|
2013-01-03 08:01:50 +00:00
|
|
|
|
return sprout::distance(
|
2013-02-26 08:03:30 +00:00
|
|
|
|
sprout::ptr_index(s),
|
|
|
|
|
sprout::find(sprout::ptr_index(s), sprout::ptr_index(s, n), '\0')
|
2012-12-21 16:02:49 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
2013-01-12 16:16:48 +00:00
|
|
|
|
|
|
|
|
|
template<typename Elem>
|
|
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
|
sprout::is_char_type<Elem>::value,
|
|
|
|
|
std::size_t
|
|
|
|
|
>::type
|
|
|
|
|
strlen(Elem* s) {
|
|
|
|
|
return sprout::detail::strlen(s);
|
|
|
|
|
}
|
|
|
|
|
template<typename Elem>
|
|
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
|
sprout::is_char_type<Elem>::value,
|
|
|
|
|
std::size_t
|
|
|
|
|
>::type
|
|
|
|
|
strlen(Elem* s, std::size_t n) {
|
|
|
|
|
typedef typename std::decay<Elem>::type type;
|
|
|
|
|
return sprout::distance(
|
2013-02-26 08:03:30 +00:00
|
|
|
|
sprout::ptr_index(s),
|
|
|
|
|
sprout::find(sprout::ptr_index(s), sprout::ptr_index(s, n), type())
|
2013-01-12 16:16:48 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
2012-04-01 13:15:09 +00:00
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_CSTRING_STRLEN_HPP
|