#ifndef SPROUT_CSTDLIB_STR_TO_INT_HPP #define SPROUT_CSTDLIB_STR_TO_INT_HPP #include #include #include #include #include #include #include #include namespace sprout { namespace detail { // Copyright (c) 2011 osyo-manga : http://d.hatena.ne.jp/osyo-manga/ template inline SPROUT_CONSTEXPR IntType str_to_int_impl_1(CStrIterator str, int base, IntType val, IntType x, bool negative) { return x == static_cast(-1) ? (negative ? -val : val) : val > (std::numeric_limits::max() - x - (negative ? 1 : 0)) / base ? (negative ? std::numeric_limits::min() : std::numeric_limits::max()) : sprout::detail::str_to_int_impl_1( sprout::next(str), base, val * base + x, sprout::detail::char_to_int(*sprout::next(str), base), negative ) ; } template inline SPROUT_CONSTEXPR IntType str_to_int_impl(CStrIterator str, int base, bool negative) { return *str == static_cast::value_type>('0') ? *sprout::next(str) == static_cast::value_type>('x') || *sprout::next(str) == static_cast::value_type>('X') ? sprout::detail::str_to_int_impl_1( sprout::next(str, 2), base ? base : 16, IntType(), sprout::detail::char_to_int(*sprout::next(str, 2), base), negative ) : sprout::detail::str_to_int_impl_1( sprout::next(str), base ? base : 8, IntType(), sprout::detail::char_to_int(*sprout::next(str), base), negative ) : sprout::detail::str_to_int_impl_1( str, base ? base : 10, IntType(), sprout::detail::char_to_int(*str, base), negative ) ; } template::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(sprout::next(str), base) : *str == static_cast::value_type>('+') ? sprout::detail::str_to_int_impl(sprout::next(str), base, false) : sprout::detail::str_to_int_impl(str, base, false) ; } template::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(sprout::next(str), base) : *str == static_cast::value_type>('-') ? sprout::detail::str_to_int_impl(sprout::next(str), base, true) : *str == static_cast::value_type>('+') ? sprout::detail::str_to_int_impl(sprout::next(str), base, false) : sprout::detail::str_to_int_impl(str, base, false) ; } template inline SPROUT_CONSTEXPR IntType str_to_int(CStrIterator str, CharPtr* endptr, int base) { return !endptr ? sprout::detail::str_to_int(str, base) : std::strtol(str, endptr, base) ; } } // namespace detail // // str_to_int // template::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(str, endptr, base); } template::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(str, base); } template::value>::type = sprout::enabler> inline SPROUT_CONSTEXPR IntType str_to_int(Char const* str, int base = 10) { return sprout::detail::str_to_int(str, base); } } // namespace sprout #endif // #ifndef SPROUT_CSTDLIB_STR_TO_INT_HPP