31#ifndef ETL_TO_STRING_HELPER_INCLUDED
32#define ETL_TO_STRING_HELPER_INCLUDED
36#include "../platform.h"
37#include "../absolute.h"
38#include "../negative.h"
39#include "../basic_format_spec.h"
40#include "../type_traits.h"
41#include "../container.h"
42#include "../absolute.h"
43#include "../algorithm.h"
44#include "../iterator.h"
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>
99 typedef typename TIString::value_type type;
100 typedef typename TIString::iterator
iterator;
102 static const type t[] = {
't',
'r',
'u',
'e' };
103 static const type f[] = {
'f',
'a',
'l',
's',
'e' };
127 str.push_back(type(
'1'));
131 str.push_back(type(
'0'));
141 template <
typename T,
typename TIString>
148 typedef typename TIString::value_type type;
149 typedef typename TIString::iterator
iterator;
161 if ((format.
get_base() == 10U) && negative)
163 str.push_back(type(
'-'));
166 str.push_back(type(
'0'));
173 T remainder = etl::absolute(value % T(format.
get_base()));
174 str.push_back((remainder > 9) ? (format.
is_upper_case() ? type(
'A' + (remainder - 10)) : type(
'a' + (remainder - 10))) : type(
'0' + remainder));
175 value = value / T(format.
get_base());
179 if ((format.
get_base() == 10U) && negative)
181 str.push_back(type(
'-'));
190 str.push_back(format.
is_upper_case() ? type(
'B') : type(
'b'));
191 str.push_back(type(
'0'));
197 str.push_back(type(
'0'));
203 str.push_back(format.
is_upper_case() ? type(
'X') : type(
'x'));
204 str.push_back(type(
'0'));
216 etl::reverse(start, str.end());
225 template <
typename TIString>
230 typedef typename TIString::value_type type;
232 static const type n[] = {
'n',
'a',
'n' };
233 static const type i[] = {
'i',
'n',
'f' };
248 template <
typename TIString>
250 const uint32_t fractional,
256 typedef typename TIString::value_type type;
262 str.push_back(type(
'.'));
267#if ETL_USING_64BIT_TYPES
271 template <
typename TIString>
273 const uint64_t fractional,
279 typedef typename TIString::value_type type;
285 str.push_back(type(
'.'));
294 template <
typename T,
typename TIString>
300 typedef typename TIString::iterator
iterator;
301 typedef typename TIString::value_type type;
310 if (isnan(value) || isinf(value))
319#if ETL_NOT_USING_64BIT_TYPES
320 if (max_precision > 9)
332 uworkspace_t multiplier = 1U;
334 for (uint32_t i = 0U; i < fractional_format.
get_precision(); ++i)
340 T f_integral = floor(etl::absolute(value));
341 uworkspace_t integral =
static_cast<uworkspace_t
>(f_integral);
344 uworkspace_t fractional =
static_cast<uworkspace_t
>(round((etl::absolute(value) - f_integral) * multiplier));
347 if (fractional == multiplier)
362 template <
typename T,
typename TIString>
364 const uint32_t denominator_exponent,
367 const bool append =
false)
369 typedef typename TIString::iterator
iterator;
370 typedef typename TIString::value_type type;
371 typedef typename etl::make_unsigned<T>::type working_t;
381 working_t denominator = 1U;
383 for (uint32_t i = 0U; i < denominator_exponent; ++i)
389 working_t abs_value = etl::absolute_unsigned(value);
392 const uint32_t& original_decimal_digits = denominator_exponent;
395 const uint32_t displayed_decimal_digits = (format.
get_precision() > original_decimal_digits) ? original_decimal_digits : format.
get_precision();
403 fractional_format.
precision(displayed_decimal_digits).
width(displayed_decimal_digits).
fill(type(
'0')).right();
406 if (original_decimal_digits > displayed_decimal_digits)
409 uint32_t count = original_decimal_digits - fractional_format.
get_width();
412 uint32_t rounding = 5U;
419 abs_value += rounding;
423 working_t integral = abs_value / denominator;
424 working_t fractional = abs_value % denominator;
427 uint32_t count = original_decimal_digits - fractional_format.
get_width();
441 template <
typename TIString>
447 uintptr_t p =
reinterpret_cast<uintptr_t
>(value);
455 template <
typename TIString>
466 typename TIString::iterator start = str.end();
468 str.insert(str.end(), value.begin(), value.end());
476 template <
typename TSringView,
typename TIString>
487 typename TIString::iterator start = str.end();
489 str.insert(str.end(), value.begin(), value.end());
499 template <
typename TIString>
503 const bool append =
false)
513 template <
typename TIString>
517 const bool append =
false)
524#if ETL_USING_64BIT_TYPES
528 template <
typename T,
typename TIString>
544 template <
typename T,
typename TIString>
558 template <
typename T,
typename TIString>
574 template <
typename T,
typename TIString>
588 template <
typename T,
typename TIString>
595 etl::private_to_string::add_integral(type(value), str, format, append,
false);
603 template <
typename T,
typename TIString>
608 etl::private_to_string::add_integral_denominated(type(value), denominator_exponent, str, format, append,
false);
617 template <
typename T,
typename TIString>
Definition: limits.h:1164
add_pointer
Definition: type_traits_generator.h:898
conditional
Definition: type_traits_generator.h:1160
enable_if
Definition: type_traits_generator.h:1191
is_same
Definition: type_traits_generator.h:1041
bitset_ext
Definition: absolute.h:38
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_CONSTEXPR TContainer::iterator begin(TContainer &container)
Definition: iterator.h:931
ETL_CONSTEXPR TContainer::iterator end(TContainer &container)
Definition: iterator.h:961
Definition: type_traits.h:1376
iterator
Definition: iterator.h:399
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:295
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:456
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:363
void add_nan_inf(const bool not_a_number, const bool infinity, TIString &str)
Helper function for floating point nan and inf.
Definition: to_string_helper.h:226
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:249
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:142
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:477
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:442
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