2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2019-01-07 08:47:17 +00:00
|
|
|
Copyright (c) 2011-2019 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-16 04:32:47 +00:00
|
|
|
#ifndef SPROUT_STRING_STRING_TO_INT_HPP
|
|
|
|
#define SPROUT_STRING_STRING_TO_INT_HPP
|
|
|
|
|
2012-04-17 14:18:51 +00:00
|
|
|
#include <cstdint>
|
2012-04-16 04:32:47 +00:00
|
|
|
#include <string>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
2014-04-30 07:30:26 +00:00
|
|
|
#include <sprout/workaround/std/cstddef.hpp>
|
2012-04-16 04:32:47 +00:00
|
|
|
#include <sprout/string/string.hpp>
|
|
|
|
#include <sprout/cstdlib/str_to_int.hpp>
|
2012-05-26 15:43:38 +00:00
|
|
|
#include <sprout/type_traits/enabler_if.hpp>
|
2012-04-16 04:32:47 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace detail {
|
2012-10-05 15:58:56 +00:00
|
|
|
template<typename IntType, typename Elem, std::size_t N, typename Traits>
|
|
|
|
inline IntType
|
|
|
|
string_to_int_dynamic(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx, int base = 10) {
|
2012-04-16 04:32:47 +00:00
|
|
|
Elem* endptr = nullptr;
|
|
|
|
IntType result = sprout::detail::str_to_int<IntType>(str.c_str(), &endptr, base);
|
|
|
|
*idx = endptr - str.c_str();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// string_to_int
|
|
|
|
//
|
|
|
|
template<
|
2012-10-05 15:58:56 +00:00
|
|
|
typename IntType, typename Elem, std::size_t N, typename Traits,
|
2012-04-16 04:32:47 +00:00
|
|
|
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
|
|
|
>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR IntType
|
|
|
|
string_to_int(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx, int base = 10) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return !idx ? sprout::detail::str_to_int<IntType>(str.begin(), base)
|
|
|
|
: sprout::detail::string_to_int_dynamic<IntType>(str, idx, base)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<
|
2012-10-05 15:58:56 +00:00
|
|
|
typename IntType, typename Elem, std::size_t N, typename Traits,
|
2012-04-16 04:32:47 +00:00
|
|
|
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
|
|
|
>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR IntType
|
|
|
|
string_to_int(sprout::basic_string<Elem, N, Traits> const& str, int base = 10) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return sprout::detail::str_to_int<IntType>(str.begin(), base);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// stoi
|
|
|
|
//
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR int
|
|
|
|
stoi(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx, int base = 10) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return sprout::string_to_int<int>(str, idx, base);
|
|
|
|
}
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR int
|
|
|
|
stoi(sprout::basic_string<Elem, N, Traits> const& str, int base = 10) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return sprout::string_to_int<int>(str, base);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// stol
|
|
|
|
//
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR long
|
|
|
|
stol(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx, int base = 10) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return sprout::string_to_int<long>(str, idx, base);
|
|
|
|
}
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR long
|
|
|
|
stol(sprout::basic_string<Elem, N, Traits> const& str, int base = 10) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return sprout::string_to_int<long>(str, base);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// stoul
|
|
|
|
//
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR unsigned long
|
|
|
|
stoul(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx, int base = 10) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return sprout::string_to_int<unsigned long>(str, idx, base);
|
|
|
|
}
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR unsigned long
|
|
|
|
stoul(sprout::basic_string<Elem, N, Traits> const& str, int base = 10) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return sprout::string_to_int<unsigned long>(str, base);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// stoll
|
|
|
|
//
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR long long
|
|
|
|
stoll(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx, int base = 10) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return sprout::string_to_int<long long>(str, idx, base);
|
|
|
|
}
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR long long
|
|
|
|
stoll(sprout::basic_string<Elem, N, Traits> const& str, int base = 10) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return sprout::string_to_int<long long>(str, base);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// stoull
|
|
|
|
//
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR unsigned long long
|
|
|
|
stoull(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx, int base = 10) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return sprout::string_to_int<unsigned long long>(str, idx, base);
|
|
|
|
}
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR unsigned long long
|
|
|
|
stoull(sprout::basic_string<Elem, N, Traits> const& str, int base = 10) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return sprout::string_to_int<unsigned long long>(str, base);
|
|
|
|
}
|
|
|
|
|
2012-04-17 14:18:51 +00:00
|
|
|
//
|
|
|
|
// stoimax
|
|
|
|
//
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR std::intmax_t
|
|
|
|
stoimax(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx, int base = 10) {
|
2012-04-17 14:18:51 +00:00
|
|
|
return sprout::string_to_int<std::intmax_t>(str, idx, base);
|
|
|
|
}
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR std::intmax_t
|
|
|
|
stoimax(sprout::basic_string<Elem, N, Traits> const& str, int base = 10) {
|
2012-04-17 14:18:51 +00:00
|
|
|
return sprout::string_to_int<std::intmax_t>(str, base);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// stoumax
|
|
|
|
//
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR std::uintmax_t
|
|
|
|
stoumax(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx, int base = 10) {
|
2012-04-17 14:18:51 +00:00
|
|
|
return sprout::string_to_int<std::uintmax_t>(str, idx, base);
|
|
|
|
}
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR std::uintmax_t
|
|
|
|
stoumax(sprout::basic_string<Elem, N, Traits> const& str, int base = 10) {
|
2012-04-17 14:18:51 +00:00
|
|
|
return sprout::string_to_int<std::uintmax_t>(str, base);
|
|
|
|
}
|
|
|
|
|
2012-04-16 04:32:47 +00:00
|
|
|
//
|
|
|
|
// from_string
|
|
|
|
//
|
|
|
|
template<
|
2012-10-05 15:58:56 +00:00
|
|
|
typename IntType, typename Elem, std::size_t N, typename Traits,
|
2012-04-16 04:32:47 +00:00
|
|
|
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
|
|
|
>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR IntType
|
|
|
|
from_string(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx) {
|
2012-04-16 06:33:29 +00:00
|
|
|
return sprout::string_to_int<IntType>(str, idx, 0);
|
2012-04-16 04:32:47 +00:00
|
|
|
}
|
|
|
|
template<
|
2012-10-05 15:58:56 +00:00
|
|
|
typename IntType, typename Elem, std::size_t N, typename Traits,
|
2012-04-16 04:32:47 +00:00
|
|
|
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
|
|
|
>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR IntType
|
|
|
|
from_string(sprout::basic_string<Elem, N, Traits> const& str) {
|
2012-04-16 06:33:29 +00:00
|
|
|
return sprout::string_to_int<IntType>(str, 0);
|
2012-04-16 04:32:47 +00:00
|
|
|
}
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_STRING_STRING_TO_INT_HPP
|