2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2016-02-25 09:48:28 +00:00
|
|
|
Copyright (c) 2011-2016 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_INT_TO_STRING_HPP
|
|
|
|
#define SPROUT_STRING_INT_TO_STRING_HPP
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
2014-04-30 07:30:26 +00:00
|
|
|
#include <sprout/workaround/std/cstddef.hpp>
|
2013-04-06 04:06:51 +00:00
|
|
|
#include <sprout/index_tuple/metafunction.hpp>
|
2012-04-16 04:32:47 +00:00
|
|
|
#include <sprout/string/string.hpp>
|
|
|
|
#include <sprout/integer/integer_digits.hpp>
|
2012-05-26 15:43:38 +00:00
|
|
|
#include <sprout/type_traits/enabler_if.hpp>
|
2015-04-04 16:32:49 +00:00
|
|
|
#include <sprout/type_traits/integral_constant.hpp>
|
|
|
|
#include <sprout/type_traits/is_signed.hpp>
|
|
|
|
#include <sprout/type_traits/is_unsigned.hpp>
|
2014-10-27 01:16:49 +00:00
|
|
|
#include <sprout/detail/char_literal.hpp>
|
2012-04-16 04:32:47 +00:00
|
|
|
#include <sprout/detail/char_conversion.hpp>
|
2012-06-23 23:22:12 +00:00
|
|
|
#include <sprout/detail/math/int.hpp>
|
2012-04-16 04:32:47 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
2012-04-16 06:33:29 +00:00
|
|
|
//
|
|
|
|
// printed_integer_digits
|
|
|
|
//
|
|
|
|
template<typename IntType, std::size_t Base = 10>
|
|
|
|
struct printed_integer_digits
|
2013-11-22 12:11:08 +00:00
|
|
|
: public sprout::integral_constant<
|
2012-04-16 06:33:29 +00:00
|
|
|
std::size_t,
|
2015-03-31 11:05:32 +00:00
|
|
|
sprout::integer_digits<IntType, Base>::value + (sprout::is_signed<IntType>::value ? 1 : 0)
|
2012-04-16 06:33:29 +00:00
|
|
|
>
|
|
|
|
{};
|
2012-04-16 04:32:47 +00:00
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
template<
|
2012-10-05 15:58:56 +00:00
|
|
|
typename Elem, int Base, typename IntType,
|
2012-04-16 17:23:41 +00:00
|
|
|
sprout::index_t... Indexes,
|
2015-03-31 11:05:32 +00:00
|
|
|
typename sprout::enabler_if<sprout::is_signed<IntType>::value>::type = sprout::enabler
|
2012-04-16 04:32:47 +00:00
|
|
|
>
|
2012-04-16 06:33:29 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_integer_digits<IntType, Base>::value>
|
2012-04-16 17:23:41 +00:00
|
|
|
int_to_string(IntType val, int digits, sprout::index_tuple<Indexes...>) {
|
2013-08-09 10:04:37 +00:00
|
|
|
typedef sprout::detail::string_construct_access<Elem, sprout::printed_integer_digits<IntType, Base>::value> access_type;
|
|
|
|
return val < 0 ? access_type::raw_construct(
|
|
|
|
static_cast<std::size_t>(digits + 1),
|
2014-10-27 01:16:49 +00:00
|
|
|
SPROUT_CHAR_LITERAL('-', Elem),
|
2013-08-09 10:04:37 +00:00
|
|
|
(Indexes < digits ? sprout::detail::int_to_char<Elem>(sprout::detail::int_digit_at<Base>(val, digits - 1 - Indexes))
|
|
|
|
: Elem()
|
|
|
|
)...
|
|
|
|
)
|
|
|
|
: access_type::raw_construct(
|
|
|
|
static_cast<std::size_t>(digits),
|
|
|
|
(Indexes < digits ? sprout::detail::int_to_char<Elem>(sprout::detail::int_digit_at<Base>(val, digits - 1 - Indexes))
|
|
|
|
: Elem()
|
|
|
|
)...
|
|
|
|
)
|
2012-04-16 17:23:41 +00:00
|
|
|
;
|
2012-04-16 04:32:47 +00:00
|
|
|
}
|
|
|
|
template<
|
2012-10-05 15:58:56 +00:00
|
|
|
typename Elem, int Base, typename IntType,
|
2012-04-16 17:23:41 +00:00
|
|
|
sprout::index_t... Indexes,
|
2015-03-31 11:05:32 +00:00
|
|
|
typename sprout::enabler_if<sprout::is_unsigned<IntType>::value>::type = sprout::enabler
|
2012-04-16 04:32:47 +00:00
|
|
|
>
|
2012-04-16 06:33:29 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_integer_digits<IntType, Base>::value>
|
2012-04-16 17:23:41 +00:00
|
|
|
int_to_string(IntType val, int digits, sprout::index_tuple<Indexes...>) {
|
2013-08-09 10:04:37 +00:00
|
|
|
typedef sprout::detail::string_construct_access<Elem, sprout::printed_integer_digits<IntType, Base>::value> access_type;
|
|
|
|
return access_type::raw_construct(
|
|
|
|
static_cast<std::size_t>(digits),
|
|
|
|
(Indexes < digits ? sprout::detail::int_to_char<Elem>(sprout::detail::int_digit_at<Base>(val, digits - 1 - Indexes))
|
|
|
|
: Elem()
|
|
|
|
)...
|
|
|
|
);
|
2012-04-16 04:32:47 +00:00
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
//
|
|
|
|
// int_to_string
|
|
|
|
//
|
|
|
|
template<
|
2012-10-05 15:58:56 +00:00
|
|
|
typename Elem, int Base = 10, typename IntType,
|
2012-04-16 04:32:47 +00:00
|
|
|
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
|
|
|
>
|
2012-04-16 06:33:29 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_integer_digits<IntType, Base>::value>
|
2012-04-16 04:32:47 +00:00
|
|
|
int_to_string(IntType val) {
|
2012-04-16 17:23:41 +00:00
|
|
|
return sprout::detail::int_to_string<Elem, Base>(
|
|
|
|
val,
|
|
|
|
sprout::detail::int_digits<Base>(val),
|
2013-03-31 06:14:10 +00:00
|
|
|
sprout::make_index_tuple<sprout::integer_digits<IntType, Base>::value>::make()
|
2012-04-16 17:23:41 +00:00
|
|
|
);
|
2012-04-16 04:32:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// to_string_of
|
|
|
|
//
|
2012-10-05 15:58:56 +00:00
|
|
|
template<
|
|
|
|
typename Elem, typename IntType,
|
|
|
|
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
|
|
|
>
|
2012-04-16 06:33:29 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_integer_digits<IntType, 10>::value>
|
2012-04-16 04:32:47 +00:00
|
|
|
to_string_of(IntType val) {
|
|
|
|
return sprout::int_to_string<Elem, 10>(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// to_string
|
|
|
|
//
|
|
|
|
template<typename IntType, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
|
2012-04-16 06:33:29 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::basic_string<char, sprout::printed_integer_digits<IntType>::value>
|
2012-04-16 04:32:47 +00:00
|
|
|
to_string(IntType val) {
|
|
|
|
return sprout::to_string_of<char>(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// to_wstring
|
|
|
|
//
|
|
|
|
template<typename IntType, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
|
2012-04-16 06:33:29 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::basic_string<wchar_t, sprout::printed_integer_digits<IntType>::value>
|
2012-04-16 04:32:47 +00:00
|
|
|
to_wstring(IntType val) {
|
|
|
|
return sprout::to_string_of<wchar_t>(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// to_u16string
|
|
|
|
//
|
|
|
|
template<typename IntType, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
|
2012-04-16 06:33:29 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::basic_string<char16_t, sprout::printed_integer_digits<IntType>::value>
|
2012-04-16 04:32:47 +00:00
|
|
|
to_u16string(IntType val) {
|
|
|
|
return sprout::to_string_of<char16_t>(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// to_u32string
|
|
|
|
//
|
|
|
|
template<typename IntType, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
|
2012-04-16 06:33:29 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::basic_string<char32_t, sprout::printed_integer_digits<IntType>::value>
|
2012-04-16 04:32:47 +00:00
|
|
|
to_u32string(IntType val) {
|
|
|
|
return sprout::to_string_of<char32_t>(val);
|
|
|
|
}
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_STRING_INT_TO_STRING_HPP
|