diff --git a/sprout/cstdlib/str_to_int.hpp b/sprout/cstdlib/str_to_int.hpp index 79fdd5a3..e057f2e5 100644 --- a/sprout/cstdlib/str_to_int.hpp +++ b/sprout/cstdlib/str_to_int.hpp @@ -39,21 +39,21 @@ namespace sprout { sprout::next(str, 2), base ? base : 16, IntType(), - sprout::detail::char_to_int(*sprout::next(str, 2), base), + sprout::detail::char_to_int(*sprout::next(str, 2), base ? base : 16), negative ) : sprout::detail::str_to_int_impl_1( sprout::next(str), base ? base : 8, IntType(), - sprout::detail::char_to_int(*sprout::next(str), base), + sprout::detail::char_to_int(*sprout::next(str), base ? base : 8), negative ) : sprout::detail::str_to_int_impl_1( str, base ? base : 10, IntType(), - sprout::detail::char_to_int(*str, base), + sprout::detail::char_to_int(*str, base ? base : 10), negative ) ; diff --git a/sprout/detail/char_conversion.hpp b/sprout/detail/char_conversion.hpp index b831139c..fe1bcce4 100644 --- a/sprout/detail/char_conversion.hpp +++ b/sprout/detail/char_conversion.hpp @@ -9,9 +9,9 @@ namespace sprout { namespace detail { template - inline SPROUT_CONSTEXPR Elem int_to_char(IntType val, std::size_t base){ + inline SPROUT_CONSTEXPR Elem int_to_char(IntType val, int base){ return val >= 0 && val < 10 ? static_cast('0') + val - : val >= 10 && val < base ? static_cast('a') + (val - 10) + : val >= 10 && val < static_cast(base) ? static_cast('a') + (val - 10) : throw std::invalid_argument("value out of bounds") ; } @@ -23,10 +23,10 @@ namespace sprout { } template - inline SPROUT_CONSTEXPR IntType char_to_int(Elem c, std::size_t base){ - return sprout::ascii::isdigit(c) && static_cast(c - static_cast('0')) < base ? c - static_cast('0') - : sprout::ascii::islower(c) && static_cast(c - static_cast('a') + 10) < base ? c - static_cast('a') + 10 - : sprout::ascii::isupper(c) && static_cast(c - static_cast('A') + 10) < base ? c - static_cast('A') + 10 + inline SPROUT_CONSTEXPR IntType char_to_int(Elem c, int base){ + return sprout::ascii::isdigit(c) && c - static_cast('0') < base ? c - static_cast('0') + : sprout::ascii::islower(c) && c - static_cast('a') + 10 < base ? c - static_cast('a') + 10 + : sprout::ascii::isupper(c) && c - static_cast('A') + 10 < base ? c - static_cast('A') + 10 : static_cast(-1) ; } diff --git a/sprout/string/conversion.hpp b/sprout/string/conversion.hpp index 0090dd35..ad207887 100644 --- a/sprout/string/conversion.hpp +++ b/sprout/string/conversion.hpp @@ -3,6 +3,6 @@ #include #include -#include +#include #endif // #ifndef SPROUT_STRING_CONVERSION_HPP diff --git a/sprout/string/from_strong.hpp b/sprout/string/from_string.hpp similarity index 100% rename from sprout/string/from_strong.hpp rename to sprout/string/from_string.hpp diff --git a/sprout/string/int_to_string.hpp b/sprout/string/int_to_string.hpp index 957129b1..38212cf7 100644 --- a/sprout/string/int_to_string.hpp +++ b/sprout/string/int_to_string.hpp @@ -1,7 +1,6 @@ #ifndef SPROUT_STRING_INT_TO_STRING_HPP #define SPROUT_STRING_INT_TO_STRING_HPP -#include #include #include #include @@ -11,16 +10,26 @@ #include namespace sprout { + // + // printed_integer_digits + // + template + struct printed_integer_digits + : public std::integral_constant< + std::size_t, + sprout::integer_digits::value + (std::is_signed::value ? 1 : 0) + > + {}; namespace detail { template< typename Elem, - std::size_t Base, + int Base, typename IntType, typename... Args, - typename sprout::enabler_if<(sizeof...(Args) == sprout::integer_digits::value - 1)>::type = sprout::enabler + typename sprout::enabler_if<(sizeof...(Args) == sprout::printed_integer_digits::value - 1)>::type = sprout::enabler > - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> int_to_string_impl_1(IntType val, bool negative, Args... args) { return negative ? sprout::make_string_as(static_cast('-'), args...) : sprout::make_string_as(args...) @@ -28,12 +37,12 @@ namespace sprout { } template< typename Elem, - std::size_t Base, + int Base, typename IntType, typename... Args, - typename sprout::enabler_if<(sizeof...(Args) < sprout::integer_digits::value - 1)>::type = sprout::enabler + typename sprout::enabler_if<(sizeof...(Args) < sprout::printed_integer_digits::value - 1)>::type = sprout::enabler > - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> int_to_string_impl_1(IntType val, bool negative, Args... args) { return val == 0 ? (negative ? sprout::make_string_as(static_cast('-'), args...) @@ -42,39 +51,38 @@ namespace sprout { : sprout::detail::int_to_string_impl_1( val / Base, negative, - sprout::detail::int_to_char(val % Base, Base), + sprout::detail::int_to_char(negative ? -(val % Base) : val % Base, Base), args... ) ; } - template - inline SPROUT_CONSTEXPR sprout::basic_string::value> + template + inline SPROUT_CONSTEXPR sprout::basic_string::value> int_to_string_impl(IntType val) { return val == 0 ? sprout::make_string_as(static_cast('0')) - : val < 0 ? sprout::detail::int_to_string_impl_1(-val, true) - : sprout::detail::int_to_string_impl_1(val, false) + : sprout::detail::int_to_string_impl_1(val, val < 0) ; } template< typename Elem, - std::size_t Base, + int Base, typename UIntType, typename... Args, - typename sprout::enabler_if<(sizeof...(Args) == sprout::integer_digits::value)>::type = sprout::enabler + typename sprout::enabler_if<(sizeof...(Args) == sprout::printed_integer_digits::value)>::type = sprout::enabler > - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> uint_to_string_impl_1(UIntType val, Args... args) { return sprout::make_string_as(args...); } template< typename Elem, - std::size_t Base, + int Base, typename UIntType, typename... Args, - typename sprout::enabler_if<(sizeof...(Args) < sprout::integer_digits::value)>::type = sprout::enabler + typename sprout::enabler_if<(sizeof...(Args) < sprout::printed_integer_digits::value)>::type = sprout::enabler > - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> uint_to_string_impl_1(UIntType val, Args... args) { return val == 0 ? sprout::make_string_as(args...) @@ -85,8 +93,8 @@ namespace sprout { ) ; } - template - inline SPROUT_CONSTEXPR sprout::basic_string::value> + template + inline SPROUT_CONSTEXPR sprout::basic_string::value> uint_to_string_impl(UIntType val) { return val == 0 ? sprout::make_string_as(static_cast('0')) : sprout::detail::uint_to_string_impl_1(val) @@ -95,21 +103,21 @@ namespace sprout { template< typename Elem, - std::size_t Base, + int Base, typename IntType, typename sprout::enabler_if::value>::type = sprout::enabler > - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> int_to_string(IntType val) { return sprout::detail::int_to_string_impl(val); } template< typename Elem, - std::size_t Base, + int Base, typename IntType, typename sprout::enabler_if::value>::type = sprout::enabler > - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> int_to_string(IntType val) { return sprout::detail::uint_to_string_impl(val); } @@ -120,11 +128,11 @@ namespace sprout { // template< typename Elem, - std::size_t Base, + int Base, typename IntType, typename sprout::enabler_if::value>::type = sprout::enabler > - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> int_to_string(IntType val) { return sprout::detail::int_to_string(val); } @@ -133,7 +141,7 @@ namespace sprout { // to_string_of // template::value>::type = sprout::enabler> - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_string_of(IntType val) { return sprout::int_to_string(val); } @@ -141,32 +149,32 @@ namespace sprout { // // to_string // - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_string(int val) { return sprout::to_string_of(val); } - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_string(unsigned val) { return sprout::to_string_of(val); } - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_string(long val) { return sprout::to_string_of(val); } - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_string(unsigned long val) { return sprout::to_string_of(val); } - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_string(long long val) { return sprout::to_string_of(val); } - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_string(unsigned long long val) { return sprout::to_string_of(val); } template::value>::type = sprout::enabler> - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_string(IntType val) { return sprout::to_string_of(val); } @@ -174,32 +182,32 @@ namespace sprout { // // to_wstring // - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_wstring(int val) { return sprout::to_string_of(val); } - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_wstring(unsigned val) { return sprout::to_string_of(val); } - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_wstring(long val) { return sprout::to_string_of(val); } - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_wstring(unsigned long val) { return sprout::to_string_of(val); } - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_wstring(long long val) { return sprout::to_string_of(val); } - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_wstring(unsigned long long val) { return sprout::to_string_of(val); } template::value>::type = sprout::enabler> - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_wstring(IntType val) { return sprout::to_string_of(val); } @@ -207,32 +215,32 @@ namespace sprout { // // to_u16string // - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_u16string(int val) { return sprout::to_string_of(val); } - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_u16string(unsigned val) { return sprout::to_string_of(val); } - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_u16string(long val) { return sprout::to_string_of(val); } - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_u16string(unsigned long val) { return sprout::to_string_of(val); } - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_u16string(long long val) { return sprout::to_string_of(val); } - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_u16string(unsigned long long val) { return sprout::to_string_of(val); } template::value>::type = sprout::enabler> - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_u16string(IntType val) { return sprout::to_string_of(val); } @@ -240,32 +248,32 @@ namespace sprout { // // to_u32string // - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_u32string(int val) { return sprout::to_string_of(val); } - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_u32string(unsigned val) { return sprout::to_string_of(val); } - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_u32string(long val) { return sprout::to_string_of(val); } - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_u32string(unsigned long val) { return sprout::to_string_of(val); } - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_u32string(long long val) { return sprout::to_string_of(val); } - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_u32string(unsigned long long val) { return sprout::to_string_of(val); } template::value>::type = sprout::enabler> - inline SPROUT_CONSTEXPR sprout::basic_string::value> + inline SPROUT_CONSTEXPR sprout::basic_string::value> to_u32string(IntType val) { return sprout::to_string_of(val); } diff --git a/sprout/string/string_to_int.hpp b/sprout/string/string_to_int.hpp index d190d9d8..05463a65 100644 --- a/sprout/string/string_to_int.hpp +++ b/sprout/string/string_to_int.hpp @@ -122,7 +122,7 @@ namespace sprout { typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR IntType from_string(sprout::basic_string const& str, std::size_t* idx) { - return sprout::string_to_int(str, idx); + return sprout::string_to_int(str, idx, 0); } template< typename IntType, @@ -132,7 +132,7 @@ namespace sprout { typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR IntType from_string(sprout::basic_string const& str) { - return sprout::string_to_int(str); + return sprout::string_to_int(str, 0); } } // namespace sprout