2012-04-14 15:48:31 +00:00
|
|
|
#ifndef SPROUT_CSTDLIB_STR_TO_INT_HPP
|
|
|
|
#define SPROUT_CSTDLIB_STR_TO_INT_HPP
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdlib>
|
2012-05-09 01:15:19 +00:00
|
|
|
#include <cinttypes>
|
2012-04-14 15:48:31 +00:00
|
|
|
#include <limits>
|
2012-04-16 03:00:20 +00:00
|
|
|
#include <type_traits>
|
2012-04-14 15:48:31 +00:00
|
|
|
#include <sprout/config.hpp>
|
|
|
|
#include <sprout/iterator/operation.hpp>
|
2012-04-16 02:21:40 +00:00
|
|
|
#include <sprout/ctype/ascii.hpp>
|
2012-04-14 15:48:31 +00:00
|
|
|
#include <sprout/utility/enabler_if.hpp>
|
|
|
|
#include <sprout/detail/char_conversion.hpp>
|
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace detail {
|
|
|
|
// Copyright (c) 2011 osyo-manga : http://d.hatena.ne.jp/osyo-manga/
|
|
|
|
|
|
|
|
template<typename IntType, typename CStrIterator>
|
|
|
|
inline SPROUT_CONSTEXPR IntType str_to_int_impl_1(CStrIterator str, int base, IntType val, IntType x, bool negative) {
|
|
|
|
return x == static_cast<IntType>(-1)
|
|
|
|
? (negative ? -val : val)
|
|
|
|
: val > (std::numeric_limits<IntType>::max() - x - (negative ? 1 : 0)) / base
|
|
|
|
? (negative ? std::numeric_limits<IntType>::min() : std::numeric_limits<IntType>::max())
|
|
|
|
: 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
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename IntType, typename CStrIterator>
|
|
|
|
inline SPROUT_CONSTEXPR IntType str_to_int_impl(CStrIterator str, int base, bool negative) {
|
|
|
|
return *str == static_cast<typename std::iterator_traits<CStrIterator>::value_type>('0')
|
|
|
|
? *sprout::next(str) == static_cast<typename std::iterator_traits<CStrIterator>::value_type>('x')
|
|
|
|
|| *sprout::next(str) == static_cast<typename std::iterator_traits<CStrIterator>::value_type>('X')
|
|
|
|
? 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
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
2012-04-15 01:31:49 +00:00
|
|
|
template<typename IntType, typename CStrIterator, typename sprout::enabler_if<std::is_unsigned<IntType>::value>::type = sprout::enabler>
|
|
|
|
inline SPROUT_CONSTEXPR IntType str_to_int(CStrIterator str, int base) {
|
|
|
|
return sprout::ascii::isspace(*str)
|
|
|
|
? sprout::detail::str_to_int<IntType>(sprout::next(str), base)
|
|
|
|
: *str == static_cast<typename std::iterator_traits<CStrIterator>::value_type>('+')
|
|
|
|
? sprout::detail::str_to_int_impl<IntType>(sprout::next(str), base, false)
|
|
|
|
: sprout::detail::str_to_int_impl<IntType>(str, base, false)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename IntType, typename CStrIterator, typename sprout::enabler_if<std::is_signed<IntType>::value>::type = sprout::enabler>
|
2012-04-14 15:48:31 +00:00
|
|
|
inline SPROUT_CONSTEXPR IntType str_to_int(CStrIterator str, int base) {
|
|
|
|
return sprout::ascii::isspace(*str)
|
|
|
|
? sprout::detail::str_to_int<IntType>(sprout::next(str), base)
|
|
|
|
: *str == static_cast<typename std::iterator_traits<CStrIterator>::value_type>('-')
|
|
|
|
? sprout::detail::str_to_int_impl<IntType>(sprout::next(str), base, true)
|
|
|
|
: *str == static_cast<typename std::iterator_traits<CStrIterator>::value_type>('+')
|
|
|
|
? sprout::detail::str_to_int_impl<IntType>(sprout::next(str), base, false)
|
|
|
|
: sprout::detail::str_to_int_impl<IntType>(str, base, false)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename IntType, typename CStrIterator, typename CharPtr>
|
|
|
|
inline SPROUT_CONSTEXPR IntType str_to_int(CStrIterator str, CharPtr* endptr, int base) {
|
|
|
|
return !endptr ? sprout::detail::str_to_int<IntType>(str, base)
|
2012-04-16 04:32:47 +00:00
|
|
|
: std::is_signed<IntType>::value
|
|
|
|
? 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-04-14 15:48:31 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
//
|
|
|
|
// str_to_int
|
|
|
|
//
|
|
|
|
template<typename IntType, typename Char, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
|
|
|
|
inline SPROUT_CONSTEXPR IntType str_to_int(Char const* str, Char** endptr, int base = 10) {
|
|
|
|
return sprout::detail::str_to_int<IntType>(str, endptr, base);
|
|
|
|
}
|
|
|
|
template<typename IntType, typename Char, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
|
|
|
|
inline SPROUT_CONSTEXPR IntType str_to_int(Char const* str, std::nullptr_t endptr, int base = 10) {
|
|
|
|
return sprout::detail::str_to_int<IntType>(str, base);
|
|
|
|
}
|
|
|
|
template<typename IntType, typename Char, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
|
|
|
|
inline SPROUT_CONSTEXPR IntType str_to_int(Char const* str, int base = 10) {
|
|
|
|
return sprout::detail::str_to_int<IntType>(str, base);
|
|
|
|
}
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_CSTDLIB_STR_TO_INT_HPP
|