31#ifndef ETL_TO_STRING_HELPER_INCLUDED
32#define ETL_TO_STRING_HELPER_INCLUDED
49#if ETL_USING_STL && ETL_USING_CPP11
55 namespace private_to_string
57#if ETL_NOT_USING_64BIT_TYPES
58 typedef int32_t workspace_t;
59 typedef uint32_t uworkspace_t;
61 typedef int64_t workspace_t;
62 typedef uint64_t uworkspace_t;
68 template <
typename TIString>
71 uint32_t length =
static_cast<uint32_t
>(etl::distance(position, str.end()));
75 uint32_t fill_length = format.
get_width() - length;
80 str.insert(str.end(), fill_length, format.
get_fill());
85 str.insert(position, fill_length, format.
get_fill());
93 template <
typename TIString>
96 typedef typename TIString::value_type
type;
97 typedef typename TIString::iterator
iterator;
99 static const type t[] = {
't',
'r',
'u',
'e'};
100 static const type f[] = {
'f',
'a',
'l',
's',
'e'};
113 str.insert(str.end(), ETL_OR_STD11::begin(t), ETL_OR_STD11::end(t));
117 str.insert(str.end(), ETL_OR_STD11::begin(f), ETL_OR_STD11::end(f));
124 str.push_back(
type(
'1'));
128 str.push_back(
type(
'0'));
138 template <
typename T,
typename TIString>
141 typedef typename TIString::value_type
type;
142 typedef typename TIString::iterator
iterator;
155 if ((format.
get_base() == 10U) && negative)
157 str.push_back(
type(
'-'));
160 str.push_back(
type(
'0'));
167 T remainder = etl::absolute(value % T(format.
get_base()));
168 str.push_back((remainder > 9) ? (format.
is_upper_case() ?
type(
'A' + (remainder - 10)) :
type(
'a' + (remainder - 10)))
169 :
type(
'0' + remainder));
170 value = value / T(format.
get_base());
174 if ((format.
get_base() == 10U) && negative)
176 str.push_back(
type(
'-'));
186 str.push_back(
type(
'0'));
192 str.push_back(
type(
'0'));
199 str.push_back(
type(
'0'));
211 etl::reverse(start, str.end());
220 template <
typename TIString>
221 void add_nan_inf(
const bool not_a_number,
const bool infinity,
const bool is_negative, TIString& str,
224 typedef typename TIString::value_type
type;
226 static const type nan_lower[] = {
'n',
'a',
'n'};
227 static const type nan_upper[] = {
'N',
'A',
'N'};
228 static const type inf_lower[] = {
'i',
'n',
'f'};
229 static const type inf_upper[] = {
'I',
'N',
'F'};
235 str.insert(str.end(), ETL_OR_STD11::begin(nan_upper), ETL_OR_STD11::end(nan_upper));
239 str.insert(str.end(), ETL_OR_STD11::begin(nan_lower), ETL_OR_STD11::end(nan_lower));
246 str.push_back(
type(
'-'));
251 str.insert(str.end(), ETL_OR_STD11::begin(inf_upper), ETL_OR_STD11::end(inf_upper));
255 str.insert(str.end(), ETL_OR_STD11::begin(inf_lower), ETL_OR_STD11::end(inf_lower));
263 template <
typename TIString>
268 typedef typename TIString::value_type
type;
274 str.push_back(
type(
'.'));
279#if ETL_USING_64BIT_TYPES
283 template <
typename TIString>
288 typedef typename TIString::value_type
type;
294 str.push_back(
type(
'.'));
303 template <
typename T,
typename TIString>
306 typedef typename TIString::value_type
type;
309 const uint32_t precision = (requested_precision > max_precision) ? max_precision : requested_precision;
317 T abs_value = etl::absolute(value);
320 int32_t exponent = 0;
321 T scaled = abs_value;
326 while (scaled >= T(10))
332 else if (scaled > T(0))
335 while (scaled < T(1))
343 uworkspace_t multiplier = 1U;
344 for (uint32_t i = 0U; i < precision; ++i)
350 T f_integral =
::floor(scaled);
351 uworkspace_t integral =
static_cast<uworkspace_t
>(f_integral);
354 uworkspace_t fractional =
static_cast<uworkspace_t
>(
::round((scaled - f_integral) * multiplier));
357 if (fractional == multiplier)
370 etl::is_negative(value));
374 str.push_back((exponent < 0) ?
type(
'-') :
type(
'+'));
376 uworkspace_t abs_exponent =
static_cast<uworkspace_t
>(etl::absolute(exponent));
387 template <
typename T,
typename TIString>
390 typedef typename TIString::value_type
type;
399 uworkspace_t multiplier = 1U;
401 for (uint32_t i = 0U; i < fractional_format.
get_precision(); ++i)
407 T f_integral =
::floor(etl::absolute(value));
408 uworkspace_t integral =
static_cast<uworkspace_t
>(f_integral);
411 uworkspace_t fractional =
static_cast<uworkspace_t
>(
::round((etl::absolute(value) - f_integral) * multiplier));
414 if (fractional == multiplier)
427 template <
typename T,
typename TIString>
430 typedef typename TIString::iterator
iterator;
439 if (isnan(value) || isinf(value))
448#if ETL_NOT_USING_64BIT_TYPES
449 if (max_precision > 9)
457 if (requires_scientific_form)
474 template <
typename T,
typename TIString>
476 const bool append =
false)
478 typedef typename TIString::iterator
iterator;
479 typedef typename TIString::value_type
type;
480 typedef typename etl::make_unsigned<T>::type working_t;
490 working_t denominator = 1U;
492 for (uint32_t i = 0U; i < denominator_exponent; ++i)
498 working_t abs_value = etl::absolute_unsigned(value);
501 const uint32_t& original_decimal_digits = denominator_exponent;
504 const uint32_t displayed_decimal_digits = (format.
get_precision() > original_decimal_digits) ? original_decimal_digits : format.
get_precision();
515 if (original_decimal_digits > displayed_decimal_digits)
518 uint32_t count = original_decimal_digits - fractional_format.
get_width();
521 uint32_t rounding = 5U;
528 abs_value += rounding;
532 working_t integral = abs_value / denominator;
533 working_t fractional = abs_value % denominator;
536 uint32_t count = original_decimal_digits - fractional_format.
get_width();
550 template <
typename TIString>
553 uintptr_t p =
reinterpret_cast<uintptr_t
>(value);
561 template <
typename TIString>
569 typename TIString::iterator start = str.end();
571 str.insert(str.end(), value.begin(), value.end());
579 template <
typename TSringView,
typename TIString>
587 typename TIString::iterator start = str.end();
589 str.insert(str.end(), value.begin(), value.end());
599 template <
typename TIString>
610 template <
typename TIString>
618#if ETL_USING_64BIT_TYPES
622 template <
typename T,
typename TIString>
623 typename etl::enable_if<etl::is_integral<T>::value && !etl::is_same<T, bool>::value && !etl::is_one_of<T, int64_t, uint64_t>::value,
627 typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>
::type type;
637 template <
typename T,
typename TIString>
638 typename etl::enable_if<etl::is_integral<T>::value && !etl::is_same<T, bool>::value && etl::is_one_of<T, int64_t, uint64_t>::value,
650 template <
typename T,
typename TIString>
651 typename etl::enable_if<etl::is_integral<T>::value && !etl::is_same<T, bool>::value && !etl::is_one_of<T, int64_t, uint64_t>::value,
654 const bool append =
false)
656 typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>
::type type;
666 template <
typename T,
typename TIString>
667 typename etl::enable_if<etl::is_integral<T>::value && !etl::is_same<T, bool>::value && etl::is_one_of<T, int64_t, uint64_t>::value,
670 const bool append =
false)
680 template <
typename T,
typename TIString>
681 typename etl::enable_if<etl::is_integral<T>::value && !etl::is_same<T, bool>::value>::value,
684 typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>
::type type;
694 template <
typename T,
typename TIString>
695 typename etl::enable_if<etl::is_integral<T>::value && !etl::is_same<T, bool>::value>::value,
697 const bool append =
false)
708 template <
typename T,
typename TIString>
709 typename etl::enable_if<etl::is_floating_point<T>::value,
const TIString&>::type
ETL_CONSTEXPR14 etl::enable_if< etl::is_specialization< TToDuration, etl::chrono::duration >::value, TToDuration >::type floor(const etl::chrono::duration< TRep, TPeriod > &d) ETL_NOEXCEPT
Rounds down a duration to the nearest lower precision.
Definition duration.h:630
etl::enable_if<!etl::is_same< T, etl::istring >::value &&!etl::is_same< T, etl::string_view >::value, constetl::istring & >::type to_string(const T value, etl::istring &str, bool append=false)
Definition to_string.h:50
ETL_CONSTEXPR14 etl::enable_if< etl::is_specialization< TToDuration, etl::chrono::duration >::value, TToDuration >::type round(const etl::chrono::duration< TRep, TPeriod > &d) ETL_NOEXCEPT
Definition duration.h:665
iterator
Definition iterator.h:482
void add_alignment(TIString &str, typename TIString::iterator position, const etl::basic_format_spec< TIString > &format)
Helper function for left/right alignment.
Definition to_string_helper.h:69
void add_floating_point(const T value, TIString &str, const etl::basic_format_spec< TIString > &format, const bool append)
Helper function for floating point.
Definition to_string_helper.h:428
void add_string(const TIString &value, TIString &str, const etl::basic_format_spec< TIString > &format, const bool append)
Helper function for strings.
Definition to_string_helper.h:562
void add_integral_denominated(const T value, const uint32_t denominator_exponent, TIString &str, const etl::basic_format_spec< TIString > &format, const bool append=false)
Helper function for denominated integers.
Definition to_string_helper.h:475
void add_floating_point_non_scientific(const T value, TIString &str, const etl::basic_format_spec< TIString > &format, const uint32_t max_precision)
Helper function for floating point in non-scientific format.
Definition to_string_helper.h:388
void add_integral_and_fractional(const uint32_t integral, const uint32_t fractional, TIString &str, const etl::basic_format_spec< TIString > &integral_format, const etl::basic_format_spec< TIString > &fractional_format, const bool negative)
Helper function for floating point integral and fractional.
Definition to_string_helper.h:264
const TIString & to_string(const bool value, TIString &str, const etl::basic_format_spec< TIString > &format, const bool append=false)
For booleans.
Definition to_string_helper.h:600
void add_integral(T value, TIString &str, const etl::basic_format_spec< TIString > &format, bool append, const bool negative)
Helper function for integrals.
Definition to_string_helper.h:139
void add_nan_inf(const bool not_a_number, const bool infinity, const bool is_negative, TIString &str, const etl::basic_format_spec< TIString > &format)
Helper function for floating point nan and inf.
Definition to_string_helper.h:221
void add_string_view(const TSringView &value, TIString &str, const etl::basic_format_spec< TIString > &format, const bool append)
Helper function for string views.
Definition to_string_helper.h:580
void add_pointer(const volatile void *value, TIString &str, const etl::basic_format_spec< TIString > &format, const bool append)
Helper function for pointers.
Definition to_string_helper.h:551
void add_boolean(const bool value, TIString &str, const etl::basic_format_spec< TIString > &format, const bool append)
Helper function for booleans.
Definition to_string_helper.h:94
void add_floating_point_scientific(const T value, TIString &str, const etl::basic_format_spec< TIString > &format, const uint32_t max_precision)
Helper function for floating point in scientific format.
Definition to_string_helper.h:304