From 47ec95c7ffcacfb9602f8e0f17d4528ce0b5dd68 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Thu, 19 Apr 2012 12:29:48 +0900 Subject: [PATCH] add detail/int.hpp --- sprout/detail/int.hpp | 52 +++++++++++++++++++++++++++++++++ sprout/string/int_to_string.hpp | 38 +++--------------------- 2 files changed, 56 insertions(+), 34 deletions(-) create mode 100644 sprout/detail/int.hpp diff --git a/sprout/detail/int.hpp b/sprout/detail/int.hpp new file mode 100644 index 00000000..de257fe5 --- /dev/null +++ b/sprout/detail/int.hpp @@ -0,0 +1,52 @@ +#ifndef SPROUT_DETAIL_INT_HPP +#define SPROUT_DETAIL_INT_HPP + +#include +#include +#include + +namespace sprout { + namespace detail { + // + // int_pow + // + template::value>::type = sprout::enabler> + inline SPROUT_CONSTEXPR IntType + int_pow(int exponent) { + return exponent ? Base * sprout::detail::int_pow(exponent - 1) + : 1 + ; + } + + // + // int_digits + // + template + inline SPROUT_CONSTEXPR int + int_digits_impl(IntType val) { + return val ? 1 + sprout::detail::int_digits_impl(val / Base) + : 0 + ; + } + template::value>::type = sprout::enabler> + inline SPROUT_CONSTEXPR int + int_digits(IntType val) { + return val ? 1 + sprout::detail::int_digits_impl(val / Base) + : 1 + ; + } + + // + // int_digit_at + // + template::value>::type = sprout::enabler> + inline SPROUT_CONSTEXPR int + int_digit_at(IntType val, int digits) { + return val < 0 ? -((val / sprout::detail::int_pow(digits)) % Base) + : (val / sprout::detail::int_pow(digits)) % Base + ; + } + } // namespace detail +} // namespace sprout + +#endif // #ifndef SPROUT_DETAIL_INT_HPP diff --git a/sprout/string/int_to_string.hpp b/sprout/string/int_to_string.hpp index c9edbb67..e2e56e91 100644 --- a/sprout/string/int_to_string.hpp +++ b/sprout/string/int_to_string.hpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace sprout { // @@ -23,37 +24,6 @@ namespace sprout { {}; namespace detail { - template - inline SPROUT_CONSTEXPR IntType - int_pow(int exponent) { - return exponent ? Base * sprout::detail::int_pow(exponent - 1) - : 1 - ; - } - - template - inline SPROUT_CONSTEXPR int - int_digits_impl(IntType val) { - return val ? 1 + sprout::detail::int_digits_impl(val / Base) - : 0 - ; - } - template - inline SPROUT_CONSTEXPR int - int_digits(IntType val) { - return val ? 1 + sprout::detail::int_digits_impl(val / Base) - : 1 - ; - } - - template - inline SPROUT_CONSTEXPR int - int_digit_of(IntType val, int digits) { - return val < 0 ? -((val / sprout::detail::int_pow(digits)) % Base) - : (val / sprout::detail::int_pow(digits)) % Base - ; - } - template< typename Elem, int Base, @@ -66,7 +36,7 @@ namespace sprout { return val < 0 ? sprout::basic_string::value>{ { static_cast('-'), - (Indexes < digits ? sprout::detail::int_to_char(sprout::detail::int_digit_of(val, digits - 1 - Indexes)) + (Indexes < digits ? sprout::detail::int_to_char(sprout::detail::int_digit_at(val, digits - 1 - Indexes)) : Elem() )... }, @@ -74,7 +44,7 @@ namespace sprout { } : sprout::basic_string::value>{ { - (Indexes < digits ? sprout::detail::int_to_char(sprout::detail::int_digit_of(val, digits - 1 - Indexes)) + (Indexes < digits ? sprout::detail::int_to_char(sprout::detail::int_digit_at(val, digits - 1 - Indexes)) : Elem() )... }, @@ -93,7 +63,7 @@ namespace sprout { int_to_string(IntType val, int digits, sprout::index_tuple) { return sprout::basic_string::value>{ { - (Indexes < digits ? sprout::detail::int_to_char(sprout::detail::int_digit_of(val, digits - 1 - Indexes)) + (Indexes < digits ? sprout::detail::int_to_char(sprout::detail::int_digit_at(val, digits - 1 - Indexes)) : Elem() )... },