2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2015-01-10 10:13:57 +00:00
|
|
|
Copyright (c) 2011-2015 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-15 01:31:49 +00:00
|
|
|
#ifndef SPROUT_CSTDLIB_DECIMAL_TO_INT_HPP
|
|
|
|
#define SPROUT_CSTDLIB_DECIMAL_TO_INT_HPP
|
|
|
|
|
|
|
|
#include <iterator>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
2013-08-06 15:15:09 +00:00
|
|
|
#include <sprout/limits.hpp>
|
2015-03-31 11:05:32 +00:00
|
|
|
#include <sprout/type_traits/std_type_traits.hpp>
|
2012-04-15 01:31:49 +00:00
|
|
|
#include <sprout/iterator/operation.hpp>
|
|
|
|
#include <sprout/ctype/ascii.hpp>
|
2014-10-27 01:16:49 +00:00
|
|
|
#include <sprout/detail/char_literal.hpp>
|
|
|
|
#include <sprout/detail/char_type_of_consecutive.hpp>
|
2012-04-15 01:31:49 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace detail {
|
|
|
|
// Copyright (c) 2011 osyo-manga : http://d.hatena.ne.jp/osyo-manga/
|
|
|
|
|
2014-07-10 08:08:33 +00:00
|
|
|
template<typename IntType, typename NullTerminatedIterator>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR IntType
|
2014-07-10 08:08:33 +00:00
|
|
|
ascii_to_int_impl(NullTerminatedIterator str, IntType val, bool negative) {
|
2014-10-27 01:16:49 +00:00
|
|
|
typedef typename std::iterator_traits<NullTerminatedIterator>::value_type value_type;
|
|
|
|
SPROUT_STATIC_ASSERT(sprout::detail::is_char_type_of_consecutive_digits<value_type>::value);
|
2012-04-15 01:31:49 +00:00
|
|
|
return !sprout::ascii::isdigit(*str)
|
|
|
|
? negative ? -val : val
|
2014-10-27 01:16:49 +00:00
|
|
|
: val > (sprout::numeric_limits<IntType>::max() - (*str - SPROUT_CHAR_LITERAL('0', value_type)) - (negative ? 1 : 0)) / 10
|
2013-08-06 15:15:09 +00:00
|
|
|
? (negative ? sprout::numeric_limits<IntType>::min() : sprout::numeric_limits<IntType>::max())
|
2012-04-15 01:31:49 +00:00
|
|
|
: sprout::detail::ascii_to_int_impl<IntType>(
|
|
|
|
sprout::next(str),
|
2014-10-27 01:16:49 +00:00
|
|
|
val * 10 + (*str - SPROUT_CHAR_LITERAL('0', value_type)),
|
2012-04-15 01:31:49 +00:00
|
|
|
negative
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
2014-07-10 08:08:33 +00:00
|
|
|
template<typename IntType, typename NullTerminatedIterator>
|
2012-12-17 11:10:17 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
2015-03-31 11:05:32 +00:00
|
|
|
sprout::is_unsigned<IntType>::value,
|
2012-12-17 11:10:17 +00:00
|
|
|
IntType
|
|
|
|
>::type
|
2014-07-10 08:08:33 +00:00
|
|
|
ascii_to_int(NullTerminatedIterator str) {
|
2014-10-27 01:16:49 +00:00
|
|
|
typedef typename std::iterator_traits<NullTerminatedIterator>::value_type value_type;
|
2012-04-15 01:31:49 +00:00
|
|
|
return sprout::ascii::isspace(*str)
|
|
|
|
? sprout::detail::ascii_to_int<IntType>(sprout::next(str))
|
2014-10-27 01:16:49 +00:00
|
|
|
: *str == SPROUT_CHAR_LITERAL('+', value_type)
|
2012-04-15 01:31:49 +00:00
|
|
|
? sprout::detail::ascii_to_int_impl<IntType>(sprout::next(str), IntType(), false)
|
|
|
|
: sprout::detail::ascii_to_int_impl<IntType>(str, IntType(), false)
|
|
|
|
;
|
|
|
|
}
|
2014-07-10 08:08:33 +00:00
|
|
|
template<typename IntType, typename NullTerminatedIterator>
|
2012-12-17 11:10:17 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
2015-03-31 11:05:32 +00:00
|
|
|
sprout::is_signed<IntType>::value,
|
2012-12-17 11:10:17 +00:00
|
|
|
IntType
|
|
|
|
>::type
|
2014-07-10 08:08:33 +00:00
|
|
|
ascii_to_int(NullTerminatedIterator str) {
|
2014-10-27 01:16:49 +00:00
|
|
|
typedef typename std::iterator_traits<NullTerminatedIterator>::value_type value_type;
|
2012-04-15 01:31:49 +00:00
|
|
|
return sprout::ascii::isspace(*str)
|
|
|
|
? sprout::detail::ascii_to_int<IntType>(sprout::next(str))
|
2014-10-27 01:16:49 +00:00
|
|
|
: *str == SPROUT_CHAR_LITERAL('-', value_type)
|
2012-04-15 01:31:49 +00:00
|
|
|
? sprout::detail::ascii_to_int_impl<IntType>(sprout::next(str), IntType(), true)
|
2014-10-27 01:16:49 +00:00
|
|
|
: *str == SPROUT_CHAR_LITERAL('+', value_type)
|
2012-04-15 01:31:49 +00:00
|
|
|
? sprout::detail::ascii_to_int_impl<IntType>(sprout::next(str), IntType(), false)
|
|
|
|
: sprout::detail::ascii_to_int_impl<IntType>(str, IntType(), false)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
//
|
|
|
|
// ascii_to_int
|
|
|
|
//
|
2012-12-17 11:10:17 +00:00
|
|
|
template<typename IntType, typename Char>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
std::is_integral<IntType>::value,
|
|
|
|
IntType
|
|
|
|
>::type
|
2012-10-05 15:58:56 +00:00
|
|
|
ascii_to_int(Char const* str) {
|
2012-04-15 01:31:49 +00:00
|
|
|
return sprout::detail::ascii_to_int<IntType>(str);
|
|
|
|
}
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_CSTDLIB_DECIMAL_TO_INT_HPP
|