mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-12-23 21:25:49 +00:00
add from_string, stol, stoul, stod, and more...
This commit is contained in:
parent
686d1bf67a
commit
c901c3058a
9 changed files with 554 additions and 270 deletions
|
@ -231,7 +231,9 @@ namespace sprout {
|
|||
template<typename FloatType, typename CStrIterator, typename CharPtr>
|
||||
inline SPROUT_CONSTEXPR FloatType str_to_float(CStrIterator str, CharPtr* endptr) {
|
||||
return !endptr ? sprout::detail::str_to_float<FloatType>(str)
|
||||
: std::strtod(str, endptr)
|
||||
: std::is_same<typename std::remove_cv<FloatType>::type, float>::value ? std::strtof(&*str, endptr)
|
||||
: std::is_same<typename std::remove_cv<FloatType>::type, double>::value ? std::strtod(&*str, endptr)
|
||||
: std::strtold(&*str, endptr)
|
||||
;
|
||||
}
|
||||
} // namespace detail
|
||||
|
|
|
@ -81,7 +81,11 @@ namespace sprout {
|
|||
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)
|
||||
: std::strtol(str, endptr, base)
|
||||
: std::is_signed<IntType>::value
|
||||
? sizeof(IntType) <= sizeof(long) ? static_cast<IntType>(std::strtol(&*str, endptr, base))
|
||||
: static_cast<IntType>(std::strtoll(&*str, endptr, base))
|
||||
: sizeof(IntType) <= sizeof(unsigned long) ? static_cast<IntType>(std::strtoul(&*str, endptr, base))
|
||||
: static_cast<IntType>(std::strtoull(&*str, endptr, base))
|
||||
;
|
||||
}
|
||||
} // namespace detail
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
#include <sprout/string/tuple.hpp>
|
||||
#include <sprout/string/container.hpp>
|
||||
#include <sprout/string/make_string.hpp>
|
||||
#include <sprout/string/to_string.hpp>
|
||||
#include <sprout/string/conversion.hpp>
|
||||
#include <sprout/string/type_traits.hpp>
|
||||
#include <sprout/string/alias.hpp>
|
||||
|
||||
|
|
8
sprout/string/conversion.hpp
Normal file
8
sprout/string/conversion.hpp
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef SPROUT_STRING_CONVERSION_HPP
|
||||
#define SPROUT_STRING_CONVERSION_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <sprout/string/to_string.hpp>
|
||||
#include <sprout/string/from_strong.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_STRING_CONVERSION_HPP
|
8
sprout/string/from_strong.hpp
Normal file
8
sprout/string/from_strong.hpp
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef SPROUT_STRING_FROM_STRING_HPP
|
||||
#define SPROUT_STRING_FROM_STRING_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <sprout/string/string_to_int.hpp>
|
||||
#include <sprout/string/string_to_float.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_STRING_FROM_STRING_HPP
|
274
sprout/string/int_to_string.hpp
Normal file
274
sprout/string/int_to_string.hpp
Normal file
|
@ -0,0 +1,274 @@
|
|||
#ifndef SPROUT_STRING_INT_TO_STRING_HPP
|
||||
#define SPROUT_STRING_INT_TO_STRING_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/string/string.hpp>
|
||||
#include <sprout/string/make_string.hpp>
|
||||
#include <sprout/integer/integer_digits.hpp>
|
||||
#include <sprout/utility/enabler_if.hpp>
|
||||
#include <sprout/detail/char_conversion.hpp>
|
||||
|
||||
namespace sprout {
|
||||
|
||||
namespace detail {
|
||||
template<
|
||||
typename Elem,
|
||||
std::size_t Base,
|
||||
typename IntType,
|
||||
typename... Args,
|
||||
typename sprout::enabler_if<(sizeof...(Args) == sprout::integer_digits<IntType, Base>::value - 1)>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::integer_digits<IntType, Base>::value>
|
||||
int_to_string_impl_1(IntType val, bool negative, Args... args) {
|
||||
return negative ? sprout::make_string_as<Elem>(static_cast<Elem>('-'), args...)
|
||||
: sprout::make_string_as<Elem>(args...)
|
||||
;
|
||||
}
|
||||
template<
|
||||
typename Elem,
|
||||
std::size_t Base,
|
||||
typename IntType,
|
||||
typename... Args,
|
||||
typename sprout::enabler_if<(sizeof...(Args) < sprout::integer_digits<IntType, Base>::value - 1)>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::integer_digits<IntType, Base>::value>
|
||||
int_to_string_impl_1(IntType val, bool negative, Args... args) {
|
||||
return val == 0
|
||||
? (negative ? sprout::make_string_as<Elem>(static_cast<Elem>('-'), args...)
|
||||
: sprout::make_string_as<Elem>(args...)
|
||||
)
|
||||
: sprout::detail::int_to_string_impl_1<Elem, Base>(
|
||||
val / Base,
|
||||
negative,
|
||||
sprout::detail::int_to_char<Elem>(val % Base, Base),
|
||||
args...
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename Elem, std::size_t Base, typename IntType>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::integer_digits<IntType, Base>::value>
|
||||
int_to_string_impl(IntType val) {
|
||||
return val == 0 ? sprout::make_string_as<Elem>(static_cast<Elem>('0'))
|
||||
: val < 0 ? sprout::detail::int_to_string_impl_1<Elem, Base>(-val, true)
|
||||
: sprout::detail::int_to_string_impl_1<Elem, Base>(val, false)
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename Elem,
|
||||
std::size_t Base,
|
||||
typename UIntType,
|
||||
typename... Args,
|
||||
typename sprout::enabler_if<(sizeof...(Args) == sprout::integer_digits<UIntType, Base>::value)>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::integer_digits<UIntType, Base>::value>
|
||||
uint_to_string_impl_1(UIntType val, Args... args) {
|
||||
return sprout::make_string_as<Elem>(args...);
|
||||
}
|
||||
template<
|
||||
typename Elem,
|
||||
std::size_t Base,
|
||||
typename UIntType,
|
||||
typename... Args,
|
||||
typename sprout::enabler_if<(sizeof...(Args) < sprout::integer_digits<UIntType, Base>::value)>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::integer_digits<UIntType, Base>::value>
|
||||
uint_to_string_impl_1(UIntType val, Args... args) {
|
||||
return val == 0
|
||||
? sprout::make_string_as<Elem>(args...)
|
||||
: sprout::detail::uint_to_string_impl_1<Elem, Base>(
|
||||
val / Base,
|
||||
sprout::detail::int_to_char<Elem>(val % Base, Base),
|
||||
args...
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename Elem, std::size_t Base, typename UIntType>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::integer_digits<UIntType, Base>::value>
|
||||
uint_to_string_impl(UIntType val) {
|
||||
return val == 0 ? sprout::make_string_as<Elem>(static_cast<Elem>('0'))
|
||||
: sprout::detail::uint_to_string_impl_1<Elem, Base>(val)
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename Elem,
|
||||
std::size_t Base,
|
||||
typename IntType,
|
||||
typename sprout::enabler_if<std::is_signed<IntType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::integer_digits<IntType, Base>::value>
|
||||
int_to_string(IntType val) {
|
||||
return sprout::detail::int_to_string_impl<Elem, Base>(val);
|
||||
}
|
||||
template<
|
||||
typename Elem,
|
||||
std::size_t Base,
|
||||
typename IntType,
|
||||
typename sprout::enabler_if<std::is_unsigned<IntType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::integer_digits<IntType, Base>::value>
|
||||
int_to_string(IntType val) {
|
||||
return sprout::detail::uint_to_string_impl<Elem, Base>(val);
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
//
|
||||
// int_to_string
|
||||
//
|
||||
template<
|
||||
typename Elem,
|
||||
std::size_t Base,
|
||||
typename IntType,
|
||||
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::integer_digits<IntType, Base>::value>
|
||||
int_to_string(IntType val) {
|
||||
return sprout::detail::int_to_string<Elem, Base>(val);
|
||||
}
|
||||
|
||||
//
|
||||
// to_string_of
|
||||
//
|
||||
template<typename Elem, typename IntType, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::integer_digits<IntType, 10>::value>
|
||||
to_string_of(IntType val) {
|
||||
return sprout::int_to_string<Elem, 10>(val);
|
||||
}
|
||||
|
||||
//
|
||||
// to_string
|
||||
//
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char, sprout::integer_digits<int>::value>
|
||||
to_string(int val) {
|
||||
return sprout::to_string_of<char>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char, sprout::integer_digits<unsigned>::value>
|
||||
to_string(unsigned val) {
|
||||
return sprout::to_string_of<char>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char, sprout::integer_digits<long>::value>
|
||||
to_string(long val) {
|
||||
return sprout::to_string_of<char>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char, sprout::integer_digits<unsigned long>::value>
|
||||
to_string(unsigned long val) {
|
||||
return sprout::to_string_of<char>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char, sprout::integer_digits<long long>::value>
|
||||
to_string(long long val) {
|
||||
return sprout::to_string_of<char>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char, sprout::integer_digits<unsigned long long>::value>
|
||||
to_string(unsigned long long val) {
|
||||
return sprout::to_string_of<char>(val);
|
||||
}
|
||||
template<typename IntType, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char, sprout::integer_digits<IntType>::value>
|
||||
to_string(IntType val) {
|
||||
return sprout::to_string_of<char>(val);
|
||||
}
|
||||
|
||||
//
|
||||
// to_wstring
|
||||
//
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<wchar_t, sprout::integer_digits<int>::value>
|
||||
to_wstring(int val) {
|
||||
return sprout::to_string_of<wchar_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<wchar_t, sprout::integer_digits<unsigned>::value>
|
||||
to_wstring(unsigned val) {
|
||||
return sprout::to_string_of<wchar_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<wchar_t, sprout::integer_digits<long>::value>
|
||||
to_wstring(long val) {
|
||||
return sprout::to_string_of<wchar_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<wchar_t, sprout::integer_digits<unsigned long>::value>
|
||||
to_wstring(unsigned long val) {
|
||||
return sprout::to_string_of<wchar_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<wchar_t, sprout::integer_digits<long long>::value>
|
||||
to_wstring(long long val) {
|
||||
return sprout::to_string_of<wchar_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<wchar_t, sprout::integer_digits<unsigned long long>::value>
|
||||
to_wstring(unsigned long long val) {
|
||||
return sprout::to_string_of<wchar_t>(val);
|
||||
}
|
||||
template<typename IntType, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<wchar_t, sprout::integer_digits<IntType>::value>
|
||||
to_wstring(IntType val) {
|
||||
return sprout::to_string_of<wchar_t>(val);
|
||||
}
|
||||
|
||||
//
|
||||
// to_u16string
|
||||
//
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char16_t, sprout::integer_digits<int>::value>
|
||||
to_u16string(int val) {
|
||||
return sprout::to_string_of<char16_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char16_t, sprout::integer_digits<unsigned>::value>
|
||||
to_u16string(unsigned val) {
|
||||
return sprout::to_string_of<char16_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char16_t, sprout::integer_digits<long>::value>
|
||||
to_u16string(long val) {
|
||||
return sprout::to_string_of<char16_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char16_t, sprout::integer_digits<unsigned long>::value>
|
||||
to_u16string(unsigned long val) {
|
||||
return sprout::to_string_of<char16_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char16_t, sprout::integer_digits<long long>::value>
|
||||
to_u16string(long long val) {
|
||||
return sprout::to_string_of<char16_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char16_t, sprout::integer_digits<unsigned long long>::value>
|
||||
to_u16string(unsigned long long val) {
|
||||
return sprout::to_string_of<char16_t>(val);
|
||||
}
|
||||
template<typename IntType, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char16_t, sprout::integer_digits<IntType>::value>
|
||||
to_u16string(IntType val) {
|
||||
return sprout::to_string_of<char16_t>(val);
|
||||
}
|
||||
|
||||
//
|
||||
// to_u32string
|
||||
//
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char32_t, sprout::integer_digits<int>::value>
|
||||
to_u32string(int val) {
|
||||
return sprout::to_string_of<char32_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char32_t, sprout::integer_digits<unsigned>::value>
|
||||
to_u32string(unsigned val) {
|
||||
return sprout::to_string_of<char32_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char32_t, sprout::integer_digits<long>::value>
|
||||
to_u32string(long val) {
|
||||
return sprout::to_string_of<char32_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char32_t, sprout::integer_digits<unsigned long>::value>
|
||||
to_u32string(unsigned long val) {
|
||||
return sprout::to_string_of<char32_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char32_t, sprout::integer_digits<long long>::value>
|
||||
to_u32string(long long val) {
|
||||
return sprout::to_string_of<char32_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char32_t, sprout::integer_digits<unsigned long long>::value>
|
||||
to_u32string(unsigned long long val) {
|
||||
return sprout::to_string_of<char32_t>(val);
|
||||
}
|
||||
template<typename IntType, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char32_t, sprout::integer_digits<IntType>::value>
|
||||
to_u32string(IntType val) {
|
||||
return sprout::to_string_of<char32_t>(val);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_STRING_INT_TO_STRING_HPP
|
115
sprout/string/string_to_float.hpp
Normal file
115
sprout/string/string_to_float.hpp
Normal file
|
@ -0,0 +1,115 @@
|
|||
#ifndef SPROUT_STRING_STRING_TO_FLOAT_HPP
|
||||
#define SPROUT_STRING_STRING_TO_FLOAT_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/string/string.hpp>
|
||||
#include <sprout/cstdlib/str_to_float.hpp>
|
||||
#include <sprout/utility/enabler_if.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
template<
|
||||
typename FloatType,
|
||||
typename Elem,
|
||||
std::size_t N,
|
||||
typename Traits
|
||||
>
|
||||
inline FloatType string_to_float_dynamic(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx) {
|
||||
Elem* endptr = nullptr;
|
||||
FloatType result = sprout::detail::str_to_float<FloatType>(str.c_str(), &endptr);
|
||||
*idx = endptr - str.c_str();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// string_to_float
|
||||
//
|
||||
template<
|
||||
typename FloatType,
|
||||
typename Elem,
|
||||
std::size_t N,
|
||||
typename Traits,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR FloatType string_to_float(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx) {
|
||||
return !idx ? sprout::detail::str_to_float<FloatType>(str.begin())
|
||||
: sprout::detail::string_to_float_dynamic<FloatType>(str, idx)
|
||||
;
|
||||
}
|
||||
template<
|
||||
typename FloatType,
|
||||
typename Elem,
|
||||
std::size_t N,
|
||||
typename Traits,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR FloatType string_to_float(sprout::basic_string<Elem, N, Traits> const& str) {
|
||||
return sprout::detail::str_to_float<FloatType>(str.begin());
|
||||
}
|
||||
|
||||
//
|
||||
// stof
|
||||
//
|
||||
template<typename Elem, std::size_t N, typename Traits>
|
||||
inline SPROUT_CONSTEXPR float stof(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx) {
|
||||
return sprout::string_to_float<float>(str, idx);
|
||||
}
|
||||
template<typename Elem, std::size_t N, typename Traits>
|
||||
inline SPROUT_CONSTEXPR float stof(sprout::basic_string<Elem, N, Traits> const& str) {
|
||||
return sprout::string_to_float<float>(str);
|
||||
}
|
||||
|
||||
//
|
||||
// stod
|
||||
//
|
||||
template<typename Elem, std::size_t N, typename Traits>
|
||||
inline SPROUT_CONSTEXPR double stod(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx) {
|
||||
return sprout::string_to_float<double>(str, idx);
|
||||
}
|
||||
template<typename Elem, std::size_t N, typename Traits>
|
||||
inline SPROUT_CONSTEXPR double stod(sprout::basic_string<Elem, N, Traits> const& str) {
|
||||
return sprout::string_to_float<double>(str);
|
||||
}
|
||||
|
||||
//
|
||||
// stold
|
||||
//
|
||||
template<typename Elem, std::size_t N, typename Traits>
|
||||
inline SPROUT_CONSTEXPR long double stold(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx) {
|
||||
return sprout::string_to_float<long double>(str, idx);
|
||||
}
|
||||
template<typename Elem, std::size_t N, typename Traits>
|
||||
inline SPROUT_CONSTEXPR long double stold(sprout::basic_string<Elem, N, Traits> const& str) {
|
||||
return sprout::string_to_float<long double>(str);
|
||||
}
|
||||
|
||||
//
|
||||
// from_string
|
||||
//
|
||||
template<
|
||||
typename FloatType,
|
||||
typename Elem,
|
||||
std::size_t N,
|
||||
typename Traits,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR FloatType from_string(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx) {
|
||||
return sprout::string_to_float<FloatType>(str, idx);
|
||||
}
|
||||
template<
|
||||
typename FloatType,
|
||||
typename Elem,
|
||||
std::size_t N,
|
||||
typename Traits,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR FloatType from_string(sprout::basic_string<Elem, N, Traits> const& str) {
|
||||
return sprout::string_to_float<FloatType>(str);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_STRING_STRING_TO_FLOAT_HPP
|
139
sprout/string/string_to_int.hpp
Normal file
139
sprout/string/string_to_int.hpp
Normal file
|
@ -0,0 +1,139 @@
|
|||
#ifndef SPROUT_STRING_STRING_TO_INT_HPP
|
||||
#define SPROUT_STRING_STRING_TO_INT_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/string/string.hpp>
|
||||
#include <sprout/cstdlib/str_to_int.hpp>
|
||||
#include <sprout/utility/enabler_if.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
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) {
|
||||
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<
|
||||
typename IntType,
|
||||
typename Elem,
|
||||
std::size_t N,
|
||||
typename Traits,
|
||||
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR IntType string_to_int(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx, int base = 10) {
|
||||
return !idx ? sprout::detail::str_to_int<IntType>(str.begin(), base)
|
||||
: sprout::detail::string_to_int_dynamic<IntType>(str, idx, base)
|
||||
;
|
||||
}
|
||||
template<
|
||||
typename IntType,
|
||||
typename Elem,
|
||||
std::size_t N,
|
||||
typename Traits,
|
||||
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR IntType string_to_int(sprout::basic_string<Elem, N, Traits> const& str, int base = 10) {
|
||||
return sprout::detail::str_to_int<IntType>(str.begin(), base);
|
||||
}
|
||||
|
||||
//
|
||||
// stoi
|
||||
//
|
||||
template<typename Elem, std::size_t N, typename Traits>
|
||||
inline SPROUT_CONSTEXPR int stoi(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx, int base = 10) {
|
||||
return sprout::string_to_int<int>(str, idx, base);
|
||||
}
|
||||
template<typename Elem, std::size_t N, typename Traits>
|
||||
inline SPROUT_CONSTEXPR int stoi(sprout::basic_string<Elem, N, Traits> const& str, int base = 10) {
|
||||
return sprout::string_to_int<int>(str, base);
|
||||
}
|
||||
|
||||
//
|
||||
// stol
|
||||
//
|
||||
template<typename Elem, std::size_t N, typename Traits>
|
||||
inline SPROUT_CONSTEXPR long stol(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx, int base = 10) {
|
||||
return sprout::string_to_int<long>(str, idx, base);
|
||||
}
|
||||
template<typename Elem, std::size_t N, typename Traits>
|
||||
inline SPROUT_CONSTEXPR long stol(sprout::basic_string<Elem, N, Traits> const& str, int base = 10) {
|
||||
return sprout::string_to_int<long>(str, base);
|
||||
}
|
||||
|
||||
//
|
||||
// stoul
|
||||
//
|
||||
template<typename Elem, std::size_t N, typename Traits>
|
||||
inline SPROUT_CONSTEXPR unsigned long stoul(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx, int base = 10) {
|
||||
return sprout::string_to_int<unsigned long>(str, idx, base);
|
||||
}
|
||||
template<typename Elem, std::size_t N, typename Traits>
|
||||
inline SPROUT_CONSTEXPR unsigned long stoul(sprout::basic_string<Elem, N, Traits> const& str, int base = 10) {
|
||||
return sprout::string_to_int<unsigned long>(str, base);
|
||||
}
|
||||
|
||||
//
|
||||
// stoll
|
||||
//
|
||||
template<typename Elem, std::size_t N, typename Traits>
|
||||
inline SPROUT_CONSTEXPR long long stoll(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx, int base = 10) {
|
||||
return sprout::string_to_int<long long>(str, idx, base);
|
||||
}
|
||||
template<typename Elem, std::size_t N, typename Traits>
|
||||
inline SPROUT_CONSTEXPR long long stoll(sprout::basic_string<Elem, N, Traits> const& str, int base = 10) {
|
||||
return sprout::string_to_int<long long>(str, base);
|
||||
}
|
||||
|
||||
//
|
||||
// stoull
|
||||
//
|
||||
template<typename Elem, std::size_t N, typename Traits>
|
||||
inline SPROUT_CONSTEXPR unsigned long long stoull(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx, int base = 10) {
|
||||
return sprout::string_to_int<unsigned long long>(str, idx, base);
|
||||
}
|
||||
template<typename Elem, std::size_t N, typename Traits>
|
||||
inline SPROUT_CONSTEXPR unsigned long long stoull(sprout::basic_string<Elem, N, Traits> const& str, int base = 10) {
|
||||
return sprout::string_to_int<unsigned long long>(str, base);
|
||||
}
|
||||
|
||||
//
|
||||
// from_string
|
||||
//
|
||||
template<
|
||||
typename IntType,
|
||||
typename Elem,
|
||||
std::size_t N,
|
||||
typename Traits,
|
||||
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR IntType from_string(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx) {
|
||||
return sprout::string_to_int<IntType>(str, idx);
|
||||
}
|
||||
template<
|
||||
typename IntType,
|
||||
typename Elem,
|
||||
std::size_t N,
|
||||
typename Traits,
|
||||
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR IntType from_string(sprout::basic_string<Elem, N, Traits> const& str) {
|
||||
return sprout::string_to_int<IntType>(str);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_STRING_STRING_TO_INT_HPP
|
|
@ -2,272 +2,6 @@
|
|||
#define SPROUT_STRING_TO_STRING_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/string/string.hpp>
|
||||
#include <sprout/string/make_string.hpp>
|
||||
#include <sprout/integer/integer_digits.hpp>
|
||||
#include <sprout/utility/enabler_if.hpp>
|
||||
#include <sprout/detail/char_conversion.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
template<
|
||||
typename Elem,
|
||||
std::size_t Base,
|
||||
typename IntType,
|
||||
typename... Args,
|
||||
typename sprout::enabler_if<(sizeof...(Args) == sprout::integer_digits<IntType, Base>::value - 1)>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::integer_digits<IntType, Base>::value>
|
||||
int_to_string_impl_1(IntType val, bool negative, Args... args) {
|
||||
return negative ? sprout::make_string_as<Elem>(static_cast<Elem>('-'), args...)
|
||||
: sprout::make_string_as<Elem>(args...)
|
||||
;
|
||||
}
|
||||
template<
|
||||
typename Elem,
|
||||
std::size_t Base,
|
||||
typename IntType,
|
||||
typename... Args,
|
||||
typename sprout::enabler_if<(sizeof...(Args) < sprout::integer_digits<IntType, Base>::value - 1)>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::integer_digits<IntType, Base>::value>
|
||||
int_to_string_impl_1(IntType val, bool negative, Args... args) {
|
||||
return val == 0
|
||||
? (negative ? sprout::make_string_as<Elem>(static_cast<Elem>('-'), args...)
|
||||
: sprout::make_string_as<Elem>(args...)
|
||||
)
|
||||
: sprout::detail::int_to_string_impl_1<Elem, Base>(
|
||||
val / Base,
|
||||
negative,
|
||||
sprout::detail::int_to_char<Elem>(val % Base, Base),
|
||||
args...
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename Elem, std::size_t Base, typename IntType>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::integer_digits<IntType, Base>::value>
|
||||
int_to_string_impl(IntType val) {
|
||||
return val == 0 ? sprout::make_string_as<Elem>(static_cast<Elem>('0'))
|
||||
: val < 0 ? sprout::detail::int_to_string_impl_1<Elem, Base>(-val, true)
|
||||
: sprout::detail::int_to_string_impl_1<Elem, Base>(val, false)
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename Elem,
|
||||
std::size_t Base,
|
||||
typename UIntType,
|
||||
typename... Args,
|
||||
typename sprout::enabler_if<(sizeof...(Args) == sprout::integer_digits<UIntType, Base>::value)>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::integer_digits<UIntType, Base>::value>
|
||||
uint_to_string_impl_1(UIntType val, Args... args) {
|
||||
return sprout::make_string_as<Elem>(args...);
|
||||
}
|
||||
template<
|
||||
typename Elem,
|
||||
std::size_t Base,
|
||||
typename UIntType,
|
||||
typename... Args,
|
||||
typename sprout::enabler_if<(sizeof...(Args) < sprout::integer_digits<UIntType, Base>::value)>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::integer_digits<UIntType, Base>::value>
|
||||
uint_to_string_impl_1(UIntType val, Args... args) {
|
||||
return val == 0
|
||||
? sprout::make_string_as<Elem>(args...)
|
||||
: sprout::detail::uint_to_string_impl_1<Elem, Base>(
|
||||
val / Base,
|
||||
sprout::detail::int_to_char<Elem>(val % Base, Base),
|
||||
args...
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename Elem, std::size_t Base, typename UIntType>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::integer_digits<UIntType, Base>::value>
|
||||
uint_to_string_impl(UIntType val) {
|
||||
return val == 0 ? sprout::make_string_as<Elem>(static_cast<Elem>('0'))
|
||||
: sprout::detail::uint_to_string_impl_1<Elem, Base>(val)
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename Elem,
|
||||
std::size_t Base,
|
||||
typename IntType,
|
||||
typename sprout::enabler_if<std::is_signed<IntType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::integer_digits<IntType, Base>::value>
|
||||
int_to_string(IntType val) {
|
||||
return sprout::detail::int_to_string_impl<Elem, Base>(val);
|
||||
}
|
||||
template<
|
||||
typename Elem,
|
||||
std::size_t Base,
|
||||
typename IntType,
|
||||
typename sprout::enabler_if<std::is_unsigned<IntType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::integer_digits<IntType, Base>::value>
|
||||
int_to_string(IntType val) {
|
||||
return sprout::detail::uint_to_string_impl<Elem, Base>(val);
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
//
|
||||
// int_to_string
|
||||
//
|
||||
template<
|
||||
typename Elem,
|
||||
std::size_t Base,
|
||||
typename IntType,
|
||||
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::integer_digits<IntType, Base>::value>
|
||||
int_to_string(IntType val) {
|
||||
return sprout::detail::int_to_string<Elem, Base>(val);
|
||||
}
|
||||
|
||||
//
|
||||
// to_string_of
|
||||
//
|
||||
template<typename Elem, typename IntType, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::integer_digits<IntType, 10>::value>
|
||||
to_string_of(IntType val) {
|
||||
return sprout::int_to_string<Elem, 10>(val);
|
||||
}
|
||||
|
||||
//
|
||||
// to_string
|
||||
//
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char, sprout::integer_digits<int>::value>
|
||||
to_string(int val) {
|
||||
return sprout::to_string_of<char>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char, sprout::integer_digits<unsigned>::value>
|
||||
to_string(unsigned val) {
|
||||
return sprout::to_string_of<char>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char, sprout::integer_digits<long>::value>
|
||||
to_string(long val) {
|
||||
return sprout::to_string_of<char>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char, sprout::integer_digits<unsigned long>::value>
|
||||
to_string(unsigned long val) {
|
||||
return sprout::to_string_of<char>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char, sprout::integer_digits<long long>::value>
|
||||
to_string(long long val) {
|
||||
return sprout::to_string_of<char>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char, sprout::integer_digits<unsigned long long>::value>
|
||||
to_string(unsigned long long val) {
|
||||
return sprout::to_string_of<char>(val);
|
||||
}
|
||||
template<typename IntType, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char, sprout::integer_digits<IntType>::value>
|
||||
to_string(IntType val) {
|
||||
return sprout::to_string_of<char>(val);
|
||||
}
|
||||
|
||||
//
|
||||
// to_wstring
|
||||
//
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<wchar_t, sprout::integer_digits<int>::value>
|
||||
to_wstring(int val) {
|
||||
return sprout::to_string_of<wchar_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<wchar_t, sprout::integer_digits<unsigned>::value>
|
||||
to_wstring(unsigned val) {
|
||||
return sprout::to_string_of<wchar_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<wchar_t, sprout::integer_digits<long>::value>
|
||||
to_wstring(long val) {
|
||||
return sprout::to_string_of<wchar_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<wchar_t, sprout::integer_digits<unsigned long>::value>
|
||||
to_wstring(unsigned long val) {
|
||||
return sprout::to_string_of<wchar_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<wchar_t, sprout::integer_digits<long long>::value>
|
||||
to_wstring(long long val) {
|
||||
return sprout::to_string_of<wchar_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<wchar_t, sprout::integer_digits<unsigned long long>::value>
|
||||
to_wstring(unsigned long long val) {
|
||||
return sprout::to_string_of<wchar_t>(val);
|
||||
}
|
||||
template<typename IntType, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<wchar_t, sprout::integer_digits<IntType>::value>
|
||||
to_wstring(IntType val) {
|
||||
return sprout::to_string_of<wchar_t>(val);
|
||||
}
|
||||
|
||||
//
|
||||
// to_u16string
|
||||
//
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char16_t, sprout::integer_digits<int>::value>
|
||||
to_u16string(int val) {
|
||||
return sprout::to_string_of<char16_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char16_t, sprout::integer_digits<unsigned>::value>
|
||||
to_u16string(unsigned val) {
|
||||
return sprout::to_string_of<char16_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char16_t, sprout::integer_digits<long>::value>
|
||||
to_u16string(long val) {
|
||||
return sprout::to_string_of<char16_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char16_t, sprout::integer_digits<unsigned long>::value>
|
||||
to_u16string(unsigned long val) {
|
||||
return sprout::to_string_of<char16_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char16_t, sprout::integer_digits<long long>::value>
|
||||
to_u16string(long long val) {
|
||||
return sprout::to_string_of<char16_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char16_t, sprout::integer_digits<unsigned long long>::value>
|
||||
to_u16string(unsigned long long val) {
|
||||
return sprout::to_string_of<char16_t>(val);
|
||||
}
|
||||
template<typename IntType, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char16_t, sprout::integer_digits<IntType>::value>
|
||||
to_u16string(IntType val) {
|
||||
return sprout::to_string_of<char16_t>(val);
|
||||
}
|
||||
|
||||
//
|
||||
// to_u32string
|
||||
//
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char32_t, sprout::integer_digits<int>::value>
|
||||
to_u32string(int val) {
|
||||
return sprout::to_string_of<char32_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char32_t, sprout::integer_digits<unsigned>::value>
|
||||
to_u32string(unsigned val) {
|
||||
return sprout::to_string_of<char32_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char32_t, sprout::integer_digits<long>::value>
|
||||
to_u32string(long val) {
|
||||
return sprout::to_string_of<char32_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char32_t, sprout::integer_digits<unsigned long>::value>
|
||||
to_u32string(unsigned long val) {
|
||||
return sprout::to_string_of<char32_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char32_t, sprout::integer_digits<long long>::value>
|
||||
to_u32string(long long val) {
|
||||
return sprout::to_string_of<char32_t>(val);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char32_t, sprout::integer_digits<unsigned long long>::value>
|
||||
to_u32string(unsigned long long val) {
|
||||
return sprout::to_string_of<char32_t>(val);
|
||||
}
|
||||
template<typename IntType, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<char32_t, sprout::integer_digits<IntType>::value>
|
||||
to_u32string(IntType val) {
|
||||
return sprout::to_string_of<char32_t>(val);
|
||||
}
|
||||
} // namespace sprout
|
||||
#include <sprout/string/int_to_string.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_STRING_TO_STRING_HPP
|
||||
|
|
Loading…
Reference in a new issue