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-14 15:48:31 +00:00
|
|
|
#ifndef SPROUT_CSTDLIB_STR_TO_INT_HPP
|
|
|
|
#define SPROUT_CSTDLIB_STR_TO_INT_HPP
|
|
|
|
|
|
|
|
#include <cstdlib>
|
2012-04-16 03:00:20 +00:00
|
|
|
#include <type_traits>
|
2012-04-14 15:48:31 +00:00
|
|
|
#include <sprout/config.hpp>
|
2014-04-30 07:30:26 +00:00
|
|
|
#include <sprout/workaround/std/cstddef.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-14 15:48:31 +00:00
|
|
|
#include <sprout/iterator/operation.hpp>
|
2012-04-16 02:21:40 +00:00
|
|
|
#include <sprout/ctype/ascii.hpp>
|
2014-10-27 01:16:49 +00:00
|
|
|
#include <sprout/detail/char_literal.hpp>
|
2012-04-14 15:48:31 +00:00
|
|
|
#include <sprout/detail/char_conversion.hpp>
|
2014-04-30 07:30:26 +00:00
|
|
|
#if !defined(_MSC_VER)
|
|
|
|
# include <cinttypes>
|
|
|
|
#endif
|
2012-04-14 15:48:31 +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
|
|
|
str_to_int_impl_1(NullTerminatedIterator str, int base, IntType val, IntType x, bool negative) {
|
2012-12-17 14:10:23 +00:00
|
|
|
return x == static_cast<IntType>(-1) ? (negative ? -1 * val : val)
|
2013-08-06 15:15:09 +00:00
|
|
|
: val > (sprout::numeric_limits<IntType>::max() - x - (negative ? 1 : 0)) / base
|
|
|
|
? (negative ? sprout::numeric_limits<IntType>::min() : sprout::numeric_limits<IntType>::max())
|
2012-04-14 15:48:31 +00:00
|
|
|
: sprout::detail::str_to_int_impl_1<IntType>(
|
|
|
|
sprout::next(str),
|
|
|
|
base,
|
|
|
|
val * base + x,
|
|
|
|
sprout::detail::char_to_int<IntType>(*sprout::next(str), base),
|
|
|
|
negative
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
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
|
|
|
str_to_int_impl(NullTerminatedIterator str, int base, bool negative) {
|
2014-10-27 01:16:49 +00:00
|
|
|
typedef typename std::iterator_traits<NullTerminatedIterator>::value_type char_type;
|
|
|
|
return *str == SPROUT_CHAR_LITERAL('0', char_type)
|
|
|
|
? *sprout::next(str) == SPROUT_CHAR_LITERAL('x', char_type)
|
|
|
|
|| *sprout::next(str) == SPROUT_CHAR_LITERAL('X', char_type)
|
2012-04-14 15:48:31 +00:00
|
|
|
? sprout::detail::str_to_int_impl_1<IntType>(
|
|
|
|
sprout::next(str, 2),
|
|
|
|
base ? base : 16,
|
|
|
|
IntType(),
|
2012-04-16 06:33:29 +00:00
|
|
|
sprout::detail::char_to_int<IntType>(*sprout::next(str, 2), base ? base : 16),
|
2012-04-14 15:48:31 +00:00
|
|
|
negative
|
|
|
|
)
|
|
|
|
: sprout::detail::str_to_int_impl_1<IntType>(
|
|
|
|
sprout::next(str),
|
|
|
|
base ? base : 8,
|
|
|
|
IntType(),
|
2012-04-16 06:33:29 +00:00
|
|
|
sprout::detail::char_to_int<IntType>(*sprout::next(str), base ? base : 8),
|
2012-04-14 15:48:31 +00:00
|
|
|
negative
|
|
|
|
)
|
|
|
|
: sprout::detail::str_to_int_impl_1<IntType>(
|
|
|
|
str,
|
|
|
|
base ? base : 10,
|
|
|
|
IntType(),
|
2012-04-16 06:33:29 +00:00
|
|
|
sprout::detail::char_to_int<IntType>(*str, base ? base : 10),
|
2012-04-14 15:48:31 +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
|
|
|
str_to_int(NullTerminatedIterator str, int base) {
|
2014-10-27 01:16:49 +00:00
|
|
|
typedef typename std::iterator_traits<NullTerminatedIterator>::value_type char_type;
|
2012-04-15 01:31:49 +00:00
|
|
|
return sprout::ascii::isspace(*str)
|
2012-10-05 15:58:56 +00:00
|
|
|
? sprout::detail::str_to_int<IntType>(sprout::next(str), base)
|
2014-10-27 01:16:49 +00:00
|
|
|
: *str == SPROUT_CHAR_LITERAL('+', char_type)
|
2012-10-05 15:58:56 +00:00
|
|
|
? sprout::detail::str_to_int_impl<IntType>(sprout::next(str), base, false)
|
2012-04-15 01:31:49 +00:00
|
|
|
: sprout::detail::str_to_int_impl<IntType>(str, base, 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
|
|
|
str_to_int(NullTerminatedIterator str, int base) {
|
2014-10-27 01:16:49 +00:00
|
|
|
typedef typename std::iterator_traits<NullTerminatedIterator>::value_type char_type;
|
2012-04-14 15:48:31 +00:00
|
|
|
return sprout::ascii::isspace(*str)
|
2012-10-05 15:58:56 +00:00
|
|
|
? sprout::detail::str_to_int<IntType>(sprout::next(str), base)
|
2014-10-27 01:16:49 +00:00
|
|
|
: *str == SPROUT_CHAR_LITERAL('-', char_type)
|
2012-10-05 15:58:56 +00:00
|
|
|
? sprout::detail::str_to_int_impl<IntType>(sprout::next(str), base, true)
|
2014-10-27 01:16:49 +00:00
|
|
|
: *str == SPROUT_CHAR_LITERAL('+', char_type)
|
2012-10-05 15:58:56 +00:00
|
|
|
? sprout::detail::str_to_int_impl<IntType>(sprout::next(str), base, false)
|
2012-04-14 15:48:31 +00:00
|
|
|
: sprout::detail::str_to_int_impl<IntType>(str, base, false)
|
|
|
|
;
|
|
|
|
}
|
2014-07-10 08:08:33 +00:00
|
|
|
template<typename IntType, typename NullTerminatedIterator, typename CharPtr>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR IntType
|
2014-07-10 08:08:33 +00:00
|
|
|
str_to_int(NullTerminatedIterator str, CharPtr* endptr, int base) {
|
2012-12-17 11:10:17 +00:00
|
|
|
return !endptr ? sprout::detail::str_to_int<IntType>(str, base)
|
2014-03-01 01:35:24 +00:00
|
|
|
#if defined(_MSC_VER)
|
2015-03-31 11:05:32 +00:00
|
|
|
: sprout::is_signed<IntType>::value
|
2012-12-17 11:10:17 +00:00
|
|
|
? static_cast<IntType>(std::strtol(&*str, endptr, base))
|
|
|
|
: static_cast<IntType>(std::strtoul(&*str, endptr, base))
|
2014-03-01 01:35:24 +00:00
|
|
|
#elif defined(__MINGW32__)
|
2015-03-31 11:05:32 +00:00
|
|
|
: sprout::is_signed<IntType>::value
|
2012-04-16 04:32:47 +00:00
|
|
|
? sizeof(IntType) <= sizeof(long) ? static_cast<IntType>(std::strtol(&*str, endptr, base))
|
2014-03-01 01:35:24 +00:00
|
|
|
: sizeof(IntType) <= sizeof(long long) ? static_cast<IntType>(::strtoll(&*str, endptr, base))
|
|
|
|
: static_cast<IntType>(std::strtoimax(&*str, endptr, base))
|
|
|
|
: sizeof(IntType) <= sizeof(unsigned long) ? static_cast<IntType>(std::strtoul(&*str, endptr, base))
|
|
|
|
: sizeof(IntType) <= sizeof(unsigned long long) ? static_cast<IntType>(::strtoull(&*str, endptr, base))
|
|
|
|
: static_cast<IntType>(std::strtoumax(&*str, endptr, base))
|
2014-02-23 10:37:56 +00:00
|
|
|
#else
|
2015-03-31 11:05:32 +00:00
|
|
|
: sprout::is_signed<IntType>::value
|
2014-03-01 01:35:24 +00:00
|
|
|
? sizeof(IntType) <= sizeof(long) ? static_cast<IntType>(std::strtol(&*str, endptr, base))
|
2012-05-09 01:15:19 +00:00
|
|
|
: sizeof(IntType) <= sizeof(long long) ? static_cast<IntType>(std::strtoll(&*str, endptr, base))
|
|
|
|
: static_cast<IntType>(std::strtoimax(&*str, endptr, base))
|
2012-04-16 04:32:47 +00:00
|
|
|
: sizeof(IntType) <= sizeof(unsigned long) ? static_cast<IntType>(std::strtoul(&*str, endptr, base))
|
2012-05-09 01:15:19 +00:00
|
|
|
: sizeof(IntType) <= sizeof(unsigned long long) ? static_cast<IntType>(std::strtoull(&*str, endptr, base))
|
|
|
|
: static_cast<IntType>(std::strtoumax(&*str, endptr, base))
|
2012-12-17 11:10:17 +00:00
|
|
|
#endif
|
2014-03-01 01:35:24 +00:00
|
|
|
;
|
2012-04-14 15:48:31 +00:00
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
//
|
|
|
|
// str_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
|
|
|
str_to_int(Char const* str, Char** endptr, int base = 10) {
|
2012-04-14 15:48:31 +00:00
|
|
|
return sprout::detail::str_to_int<IntType>(str, endptr, base);
|
|
|
|
}
|
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
|
2013-07-22 13:00:09 +00:00
|
|
|
str_to_int(Char const* str, std::nullptr_t, int base = 10) {
|
2012-04-14 15:48:31 +00:00
|
|
|
return sprout::detail::str_to_int<IntType>(str, base);
|
|
|
|
}
|
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
|
|
|
str_to_int(Char const* str, int base = 10) {
|
2012-04-14 15:48:31 +00:00
|
|
|
return sprout::detail::str_to_int<IntType>(str, base);
|
|
|
|
}
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_CSTDLIB_STR_TO_INT_HPP
|