6#if !defined(__XTD_TO_STRING_INTERNAL__)
7#error "Do not include this file: Internal use only. Include <xtd/to_string> or <xtd/to_string.hpp> instead."
16template<
class range_t>
17std::string __xtd_range_to_string(
const range_t& values,
const xtd::string& fmt,
const std::locale& loc);
19template<
class value_t>
21 if constexpr(std::is_polymorphic_v<value_t>)
return __to_string_polymorphic(value, fmt, loc);
22 else if constexpr(std::is_enum_v<value_t>)
return __enum_formatter<char>(fmt, value, loc);
23 #if defined(__xtd__cpp_lib_ranges)
24 else if constexpr(std::ranges::range<value_t> && !std::is_same_v<value_t, xtd::string>)
return __xtd_range_to_string(value, fmt, loc);
27 auto ss = std::stringstream {};
35 return __boolean_formatter(fmt.
chars(), value, loc);
40 return __numeric_formatter(fmt.
chars(), value, loc);
45 return __character_formatter(fmt.
chars(), value, loc);
50 return __numeric_formatter(fmt.
chars(), value, loc);
55 return __numeric_formatter(fmt.
chars(), value, loc);
60 return __numeric_formatter(fmt.
chars(), value, loc);
65 return __numeric_formatter(fmt.
chars(), value, loc);
70 return __numeric_formatter(fmt.
chars(), value, loc);
75 return __numeric_formatter(fmt.
chars(), value, loc);
80 return __numeric_formatter(fmt.
chars(), value, loc);
85 return __numeric_formatter(fmt.
chars(), value, loc);
90 return __numeric_formatter(fmt.
chars(), value, loc);
95 return __floating_point_formatter(fmt.
chars(), value, loc);
100 return __floating_point_formatter(fmt.
chars(), value, loc);
105 return __floating_point_formatter(fmt.
chars(), value, loc);
110 return __date_time_formatter(fmt.
chars(), std::chrono::system_clock::to_time_t(value), std::chrono::duration_cast<std::chrono::nanoseconds>(value.time_since_epoch()).count() % 1000000000, loc);
115 return __date_time_formatter(fmt.
chars(), value, 0, loc);
118template<
class type_t,
class period_t>
120 return __duration_formatter(fmt.
chars(), value, loc);
125 return __character_formatter(fmt.
chars(), value, loc);
130 return __character_formatter(fmt.
chars(), value, loc);
135 return __character_formatter(fmt.
chars(), value, loc);
140 return __character_formatter(fmt.
chars(), value, loc);
143template<
class value_t>
145 return to_string(value, fmt, std::locale());
149 return to_string(value, fmt, std::locale());
154 if (value == std::partial_ordering::less)
return "less";
155 if (value == std::partial_ordering::greater)
return "greater";
161 if (value == std::strong_ordering::less)
return "less";
162 if (value == std::strong_ordering::greater)
return "greater";
168 if (value == std::weak_ordering::less)
return "less";
169 if (value == std::weak_ordering::greater)
return "greater";
173template<
class value_t>
175 if (!value)
return "(null)";
176 return __numeric_formatter(fmt.
chars(),
reinterpret_cast<intptr>(value), loc);
179template<
class value_t>
181 if (!value)
return "(null)";
182 return __numeric_formatter(fmt.
chars(),
reinterpret_cast<intptr>(value), loc);
185template<
class type_t>
187 if (!value)
return "(null)";
188 return __numeric_formatter(fmt.
chars(),
reinterpret_cast<intptr>(value.get()), loc);
191template<
class type_t>
193 if (!value)
return "(null)";
194 return __numeric_formatter(fmt.
chars(),
reinterpret_cast<intptr>(value.get()), loc);
199 auto iterator = __any_stringer__.find(std::type_index(value.type()));
200 return iterator != __any_stringer__.
cend() ?
xtd::to_string(iterator->second(value), fmt, loc) :
"(unregistered)";
203template<
class type_t>
205 return !value.has_value() ?
"(null)" : std::string {
"("} +
to_string(value.value(), fmt, loc).
chars() + std::string {
")"};
218template<
class type1_t,
class type2_t>
220 return std::string {
"("} +
to_string(value.first, fmt, loc).
chars() + std::string {
", "} +
to_string(value.second, fmt, loc).
chars() + std::string {
")"};
223template<
class type_t,
unsigned n_t,
unsigned last_t>
224inline xtd::string xtd::__xtd_tuple_stringer<type_t, n_t, last_t>::to_string(
const std::string& str,
const type_t& value,
const xtd::string& fmt,
const std::locale& loc) {
225 return __xtd_tuple_stringer < type_t, n_t + 1, last_t >::to_string(str +
xtd::to_string(std::get<n_t>(value), fmt, loc).
chars() +
", ", value, fmt, loc);
228template<
class type_t,
unsigned n_t>
229inline xtd::string xtd::__xtd_tuple_stringer<type_t, n_t, n_t>::to_string(
const std::string& str,
const type_t& value,
const xtd::string& fmt,
const std::locale& loc) {
233template<
class ...types_t>
235 return __xtd_tuple_stringer < std::tuple<types_t ...>, 0,
sizeof...(types_t) - 1 >::to_string(std::string {
"("}, value, fmt, loc) +
")";
238template<
class iterator_t>
239inline std::string __xtd_iterator_to_string(
const std::string& str, iterator_t iterator,
const iterator_t& begin,
const iterator_t& end,
const xtd::string& fmt,
const std::locale& loc) {
240 if (iterator == end)
return str;
241 auto new_str = str + (iterator ==
begin ? std::string {} : std::string {
", "}) +
xtd::to_string(*iterator, fmt, loc).chars();
242 return __xtd_iterator_to_string(new_str, ++iterator, begin, end, fmt, loc);
245template<
class iterator_t>
246inline std::string __xtd_sequence_container_to_string(
const iterator_t& begin,
const iterator_t& end,
const xtd::string& fmt,
const std::locale& loc) {
247 return __xtd_iterator_to_string(
"[", begin, begin, end, fmt, loc) +
"]";
250template<
class range_t>
251inline std::string __xtd_range_to_string(
const range_t& values,
const xtd::string& fmt,
const std::locale& loc) {
252 std::ostringstream oss;
256 for (
auto&& v : values) {
257 if (!first) oss <<
", ";
265template<
class type_t,
size_t size>
267 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
273 for (
auto index =
size_t {0}; index < values.length(); ++index)
274 result += (index ?
", " :
"") +
to_string(
static_cast<bool>(values[index]), fmt, loc);
278template<
class type_t,
class allocator_t>
280 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
283template<
class type_t,
class allocator_t>
285 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
288template<
class type_t>
290 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
293template<
class type_t,
class allocator_t>
295 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
298template<
class type_t,
class container_t>
300 struct std_queue :
public std::queue<type_t> {
301 std_queue(
const std::queue<type_t>& queue) :
ptr {reinterpret_cast<const std_queue*>(&
queue)} {}
302 auto begin()
const {
return ptr->c.begin();}
303 auto end()
const {
return ptr->c.end();}
304 const std_queue*
ptr;
306 auto items = std_queue {values};
307 return __xtd_sequence_container_to_string(items.begin(), items.end(), fmt, loc);
310template<
class type_t,
class container_t>
312 struct std_priority_queue :
public std::queue<type_t> {
313 std_priority_queue(
const std::priority_queue<type_t>& queue) :
ptr {reinterpret_cast<const std_priority_queue*>(&
queue)} {}
314 auto begin()
const {
return ptr->c.begin();}
315 auto end()
const {
return ptr->c.end();}
316 const std_priority_queue*
ptr;
318 auto items = std_priority_queue {values};
319 return __xtd_sequence_container_to_string(items.begin(), items.end(), fmt, loc);
322template<
class type_t,
class container_t>
324 struct std_stack :
public std::stack<type_t> {
325 std_stack(
const std::stack<type_t>& queue) :
ptr {reinterpret_cast<const std_stack*>(&
queue)} {}
326 auto begin()
const {
return ptr->c.begin();}
327 auto end()
const {
return ptr->c.end();}
328 const std_stack*
ptr;
330 auto items = std_stack {values};
331 return __xtd_sequence_container_to_string(items.begin(), items.end(), fmt, loc);
334template<
class type_t>
336 return __xtd_sequence_container_to_string(std::begin(values), std::end(values), fmt, loc);
339template<
class type_t,
class allocator_t>
341 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
344template<
class iterator_t>
345inline std::string __xtd_associative_container_to_string(
const iterator_t& begin,
const iterator_t& end,
const xtd::string& fmt,
const std::locale& loc) {
346 return __xtd_iterator_to_string(
"{", begin, begin, end, fmt, loc) +
"}";
349template<
class key_t,
class value_t,
class compare_t,
class allocator_t>
351 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
354template<
class key_t,
class value_t,
class compare_t,
class allocator_t>
356 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
359template<
class key_t,
class compare_t,
class allocator_t>
361 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
364template<
class key_t,
class compare_t,
class allocator_t>
366 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
369template<
class key_t,
class value_t,
class compare_t,
class allocator_t>
371 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
374template<
class key_t,
class value_t,
class compare_t,
class allocator_t>
376 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
379template<
class key_t,
class compare_t,
class allocator_t>
381 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
384template<
class key_t,
class compare_t,
class allocator_t>
386 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
389template<
class type_t>
391 std::map<type_t, xtd::string, std::greater<type_t>> values;
392 for (
auto item : il) values[item.first] = item.second;
396#if defined(__xtd__cpp_lib_ranges)
397template <std::ranges::range range_t>
399 return __xtd_range_to_string(values, fmt, loc);
403template<
class type_t>
405 return __xtd_sequence_container_to_string(values.
begin(), values.
end(), fmt, loc);
408template<
class type_t>
410 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
413template<
class type_t,
class string_t>
414inline string_t
xtd::to_string(type_t value,
const std::map<type_t, string_t, std::greater<type_t>>& values) {
415 auto it = values.find(value);
416 if (it != values.end())
return it->second;
418 long long rest =
static_cast<long long>(value);
419 for (
auto item : values) {
420 if (
static_cast<long long>(item.first) != 0 && (rest &
static_cast<long long>(item.first)) ==
static_cast<long long>(item.first)) {
421 if (!result.empty()) result = string_t {
',',
' '} + result;
422 result = item.second + result;
423 rest -=
static_cast<long long>(item.first);
426 if (!result.empty())
return result;
427 return to_string(
static_cast<long long>(value), string_t {
'G'}, std::locale());
430template<
class type_t,
class string_t>
431inline string_t
xtd::to_string(type_t value,
const std::map<type_t, string_t>& values) {
432 std::map<type_t, string_t, std::greater<type_t>> descending_values;
433 for (
auto item : values) descending_values[item.first] = item.second;
434 return to_string(value, descending_values);
437template<
class char_t,
class value_t>
438inline std::basic_string<char_t> __string_formatter(
const std::basic_string<char_t>& fmt, value_t value,
const std::locale& loc) {
439 return __format_stringer<char_t>(value);
443inline std::string __format_stringer < char, bool& > (
bool& value) {
448inline std::string __format_stringer < char, xtd::sbyte& > (
xtd::sbyte& value) {
453inline std::string __format_stringer < char, const unsigned char& > (
const unsigned char& value) {
458inline std::string __format_stringer < char, unsigned char& > (
unsigned char& value) {
463inline std::string __format_stringer < char, short& > (
short& value) {
468inline std::string __format_stringer < char, unsigned short& > (
unsigned short& value) {
473inline std::string __format_stringer < char, int& > (
int& value) {
478inline std::string __format_stringer < char, unsigned int& > (
unsigned int& value) {
483inline std::string __format_stringer < char, long& > (
long& value) {
488inline std::string __format_stringer < char, unsigned long& > (
unsigned long& value) {
493inline std::string __format_stringer < char, long long& > (
long long& value) {
498inline std::string __format_stringer < char, unsigned long long& > (
unsigned long long& value) {
503inline std::string __format_stringer < char, float& > (
float& value) {
508inline std::string __format_stringer < char, double& > (
double& value) {
513inline std::string __format_stringer < char, long double& > (
long double& value) {
518inline std::string __format_stringer < char, std::chrono::system_clock::time_point& > (std::chrono::system_clock::time_point& value) {
523inline std::string __format_stringer < char, std::tm& > (tm& value) {
528inline std::string __format_stringer < char, xtd::wchar& > (
xtd::wchar& value) {
533inline std::string __format_stringer < char, xtd::char8& > (
xtd::char8& value) {
538inline std::string __format_stringer < char, xtd::char16& > (
xtd::char16& value) {
543inline std::string __format_stringer < char, xtd::char32& > (
xtd::char32& value) {
547template<
class char_t,
class type_t,
class period_t = std::ratio<1>>
548std::basic_ostream<char_t>& operator <<(std::basic_ostream<char_t>& os,
const std::chrono::duration<type_t, period_t>& value) {
549 return os <<
xtd::to_string(value, std::basic_string<char_t> {
'G'}, std::locale());
552template <
class char_t,
class type_t >
553struct __enum_ostream__ < char_t, type_t, std::false_type > {
554 std::basic_ostream < char_t >& to_stream(std::basic_ostream < char_t >& os,
const type_t& value)
noexcept {
556 return os <<
xtd::to_string(value, std::basic_string < char_t > {}, std::locale {});
const base_type & chars() const noexcept
Returns a reference to the underlying base type.
Definition basic_string.hpp:354
virtual auto begin() const -> const_iterator
Returns an iterator to the first element of the enumerable.
Definition enumerable_iterators.hpp:140
virtual auto end() const -> const_iterator
Returns an iterator to the element following the last element of the enumerable.
Definition enumerable_iterators.hpp:155
virtual auto cend() const -> const_iterator
Returns an iterator to the element following the last element of the enumerable.
Definition enumerable_iterators.hpp:151
Internal vector-like container used as a storage backend for xtd collections.
Definition raw_array.hpp:29
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.hpp:40
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
Concept stream_insertable.
Definition stream_insertable.hpp:26
generic::queue< xtd::any_object > queue
Represents a first-in, first-out collection of objects.
Definition queue.hpp:27
@ format
The format is not valid.
Definition exception_case.hpp:51
xtd::basic_string< char > string
Represents text as a sequence of UTF-8 code units.
Definition __string_definitions.hpp:43
char8_t char8
Represents a 8-bit unicode character.
Definition char8.hpp:26
std::int8_t sbyte
Represents a 8-bit signed integer.
Definition sbyte.hpp:23
wchar_t wchar
Represents a wide character.
Definition wchar.hpp:24
std::intmax_t intptr
Represent a pointer or a handle.
Definition intptr.hpp:23
char16_t char16
Represents a 16-bit unicode character.
Definition char16.hpp:26
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.hpp:27
char32_t char32
Represents a 32-bit unicode character.
Definition char32.hpp:23
std::any any
Represents the any alias on std::any.
Definition any.hpp:24
const_iterator begin() const
Returns an iterator to the beginning.
Definition read_only_span.hpp:183
string to_string() const noexcept override
Returns the string representation of this xtd::read_only_span <type_t> object.
Definition read_only_span.hpp:375
read_only_span< type_t, count > first() const
Obtains a subspan consisting of the first count elements of the sequence.
Definition read_only_span.hpp:282
const_iterator end() const
Returns an iterator to the end.
Definition read_only_span.hpp:213
Contains xtd::stream_insertable concept.
Contains xtd::to_string methods.