/*============================================================================= Copyright (c) 2011-2013 Bolero MURAKAMI 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) =============================================================================*/ #ifndef SPROUT_UTILITY_STRING_REF_STRING_TO_INT_HPP #define SPROUT_UTILITY_STRING_REF_STRING_TO_INT_HPP #include #include #include #include #include #include #include namespace sprout { namespace detail { template inline IntType string_to_int_dynamic(sprout::basic_string_ref const& str, std::size_t* idx, int base = 10) { Elem* endptr = nullptr; IntType result = sprout::detail::str_to_int(str.c_str(), &endptr, base); *idx = endptr - str.c_str(); return result; } } // // string_to_int // template< typename IntType, typename Elem, typename Traits, typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR IntType string_to_int(sprout::basic_string_ref const& str, std::size_t* idx, int base = 10) { return !idx ? sprout::detail::str_to_int(str.begin(), base) : sprout::detail::string_to_int_dynamic(str, idx, base) ; } template< typename IntType, typename Elem, typename Traits, typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR IntType string_to_int(sprout::basic_string_ref const& str, int base = 10) { return sprout::detail::str_to_int(str.begin(), base); } // // stoi // template inline SPROUT_CONSTEXPR int stoi(sprout::basic_string_ref const& str, std::size_t* idx, int base = 10) { return sprout::string_to_int(str, idx, base); } template inline SPROUT_CONSTEXPR int stoi(sprout::basic_string_ref const& str, int base = 10) { return sprout::string_to_int(str, base); } // // stol // template inline SPROUT_CONSTEXPR long stol(sprout::basic_string_ref const& str, std::size_t* idx, int base = 10) { return sprout::string_to_int(str, idx, base); } template inline SPROUT_CONSTEXPR long stol(sprout::basic_string_ref const& str, int base = 10) { return sprout::string_to_int(str, base); } // // stoul // template inline SPROUT_CONSTEXPR unsigned long stoul(sprout::basic_string_ref const& str, std::size_t* idx, int base = 10) { return sprout::string_to_int(str, idx, base); } template inline SPROUT_CONSTEXPR unsigned long stoul(sprout::basic_string_ref const& str, int base = 10) { return sprout::string_to_int(str, base); } // // stoll // template inline SPROUT_CONSTEXPR long long stoll(sprout::basic_string_ref const& str, std::size_t* idx, int base = 10) { return sprout::string_to_int(str, idx, base); } template inline SPROUT_CONSTEXPR long long stoll(sprout::basic_string_ref const& str, int base = 10) { return sprout::string_to_int(str, base); } // // stoull // template inline SPROUT_CONSTEXPR unsigned long long stoull(sprout::basic_string_ref const& str, std::size_t* idx, int base = 10) { return sprout::string_to_int(str, idx, base); } template inline SPROUT_CONSTEXPR unsigned long long stoull(sprout::basic_string_ref const& str, int base = 10) { return sprout::string_to_int(str, base); } // // stoimax // template inline SPROUT_CONSTEXPR std::intmax_t stoimax(sprout::basic_string_ref const& str, std::size_t* idx, int base = 10) { return sprout::string_to_int(str, idx, base); } template inline SPROUT_CONSTEXPR std::intmax_t stoimax(sprout::basic_string_ref const& str, int base = 10) { return sprout::string_to_int(str, base); } // // stoumax // template inline SPROUT_CONSTEXPR std::uintmax_t stoumax(sprout::basic_string_ref const& str, std::size_t* idx, int base = 10) { return sprout::string_to_int(str, idx, base); } template inline SPROUT_CONSTEXPR std::uintmax_t stoumax(sprout::basic_string_ref const& str, int base = 10) { return sprout::string_to_int(str, base); } // // from_string // template< typename IntType, typename Elem, typename Traits, typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR IntType from_string(sprout::basic_string_ref const& str, std::size_t* idx) { return sprout::string_to_int(str, idx, 0); } template< typename IntType, typename Elem, typename Traits, typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR IntType from_string(sprout::basic_string_ref const& str) { return sprout::string_to_int(str, 0); } } // namespace sprout #endif // #ifndef SPROUT_UTILITY_STRING_REF_STRING_TO_INT_HPP