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

113 lines
3.3 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2014-01-08 07:48:12 +00:00
Copyright (c) 2011-2014 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_MEMCHR_HPP
#define SPROUT_CSTRING_MEMCHR_HPP
#include <cstddef>
2013-06-26 15:36:17 +00:00
#include <type_traits>
#include <utility>
2013-06-26 15:38:14 +00:00
#include <sprout/config.hpp>
2013-06-26 15:36:17 +00:00
#include <sprout/type_traits/enabler_if.hpp>
2013-06-26 15:38:14 +00:00
#include <sprout/iterator/ptr_index_iterator.hpp>
#include <sprout/algorithm/find.hpp>
2013-06-26 15:36:17 +00:00
2012-04-01 13:15:09 +00:00
namespace sprout {
namespace detail {
2012-12-21 16:02:49 +00:00
inline SPROUT_CONSTEXPR unsigned char const*
memchr_impl(unsigned char const* found, unsigned char const* last) {
return found == last ? nullptr
: found
;
}
inline SPROUT_CONSTEXPR unsigned char*
memchr_impl(unsigned char* found, unsigned char* last) {
return found == last ? nullptr
: found
;
2012-04-01 13:15:09 +00:00
}
2013-06-26 15:36:17 +00:00
template<typename T>
struct memchr_result {
private:
static void const* check(void const*);
static void* check(void*);
static void check(...);
public:
typedef decltype(check(std::declval<T>())) type;
};
2012-04-01 13:15:09 +00:00
} // namespace detail
2013-03-22 05:24:19 +00:00
// 7.21.5.1 memchr <20>֐<EFBFBD>
2013-01-13 08:41:45 +00:00
//
// recursion depth:
// O(log N)
//
2013-06-26 15:36:17 +00:00
#if 0
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR void const*
2012-12-21 16:02:49 +00:00
memchr(void const* s, int c, std::size_t n) {
return sprout::detail::memchr_impl(
2013-02-26 08:03:30 +00:00
sprout::ptr_unindex(
2013-01-11 19:08:44 +00:00
sprout::find(
2013-02-26 08:03:30 +00:00
sprout::ptr_index(static_cast<unsigned char const*>(s)), sprout::ptr_index(static_cast<unsigned char const*>(s), n),
2012-12-21 16:02:49 +00:00
static_cast<unsigned char>(c)
)
),
static_cast<unsigned char const*>(s) + n
);
2012-04-01 13:15:09 +00:00
}
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR void*
2012-12-21 16:02:49 +00:00
memchr(void* s, int c, std::size_t n) {
return sprout::detail::memchr_impl(
2013-02-26 08:03:30 +00:00
sprout::ptr_unindex(
2013-01-11 19:08:44 +00:00
sprout::find(
2013-02-26 08:03:30 +00:00
sprout::ptr_index(static_cast<unsigned char*>(s)), sprout::ptr_index(static_cast<unsigned char*>(s), n),
2012-12-21 16:02:49 +00:00
static_cast<unsigned char>(c)
)
),
static_cast<unsigned char*>(s) + n
2012-04-01 13:15:09 +00:00
);
}
2013-06-26 15:36:17 +00:00
#endif
template<
typename T,
typename sprout::enabler_if<std::is_same<typename sprout::detail::memchr_result<T>::type, void const*>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR void const*
memchr(T s, int c, std::size_t n) {
return sprout::detail::memchr_impl(
sprout::ptr_unindex(
sprout::find(
sprout::ptr_index(static_cast<unsigned char const*>(s)), sprout::ptr_index(static_cast<unsigned char const*>(s), n),
static_cast<unsigned char>(c)
)
),
static_cast<unsigned char const*>(s) + n
);
}
template<
typename T,
typename sprout::enabler_if<std::is_same<typename sprout::detail::memchr_result<T>::type, void*>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR void*
memchr(T s, int c, std::size_t n) {
return sprout::detail::memchr_impl(
sprout::ptr_unindex(
sprout::find(
sprout::ptr_index(static_cast<unsigned char*>(s)), sprout::ptr_index(static_cast<unsigned char*>(s), n),
static_cast<unsigned char>(c)
)
),
static_cast<unsigned char*>(s) + n
);
}
2012-04-01 13:15:09 +00:00
} // namespace sprout
#endif // #ifndef SPROUT_CSTRING_MEMCHR_HPP