xtd 0.2.0
Loading...
Searching...
No Matches
to_string.hpp
Go to the documentation of this file.
1
4#pragma once
6#define __XTD_CORE_INTERNAL__
18#undef __XTD_CORE_INTERNAL__
19#define __XTD_STD_INTERNAL__
21#undef __XTD_STD_INTERNAL__
25#include "types.hpp"
26#include <string>
27
29namespace xtd {
40 template<typename value_t>
41 inline std::string to_string(const value_t& value, const std::string& fmt, const std::locale& loc) {return __to_string_polymorphic(value, fmt, loc, std::is_polymorphic<value_t>());}
42
53 template<>
54 inline std::string to_string(const bool& value, const std::string& fmt, const std::locale& loc) {return __boolean_formatter(fmt, value, loc);}
55
66 template<>
67 inline std::string to_string(const sbyte& value, const std::string& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
68
79 template<>
80 inline std::string to_string(const char& value, const std::string& fmt, const std::locale& loc) {return __character_formatter(fmt, value, loc);}
81
92 template<>
93 inline std::string to_string(const unsigned char& value, const std::string& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
94
105 template<>
106 inline std::string to_string(const short& value, const std::string& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
107
118 template<>
119 inline std::string to_string(const unsigned short& value, const std::string& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
120
131 template<>
132 inline std::string to_string(const int& value, const std::string& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
133
144 template<>
145 inline std::string to_string(const unsigned int& value, const std::string& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
146
157 template<>
158 inline std::string to_string(const long& value, const std::string& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
159
170 template<>
171 inline std::string to_string(const unsigned long& value, const std::string& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
172
183 template<>
184 inline std::string to_string(const long long& value, const std::string& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
185
196 template<>
197 inline std::string to_string(const unsigned long long& value, const std::string& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
198
209 template<>
210 inline std::string to_string(const float& value, const std::string& fmt, const std::locale& loc) {return __floating_point_formatter(fmt, value, loc);}
211
222 template<>
223 inline std::string to_string(const double& value, const std::string& fmt, const std::locale& loc) {return __floating_point_formatter(fmt, value, loc);}
224
235 template<>
236 inline std::string to_string(const long double& value, const std::string& fmt, const std::locale& loc) {return __floating_point_formatter(fmt, value, loc);}
237
248 template<>
249 inline std::string to_string(const std::chrono::system_clock::time_point& value, const std::string& fmt, const std::locale& loc) {return __date_time_formatter(fmt, std::chrono::system_clock::to_time_t(value), std::chrono::duration_cast<std::chrono::nanoseconds>(value.time_since_epoch()).count() % 1000000000, loc);}
250
261 template<>
262 inline std::string to_string(const std::tm& value, const std::string& fmt, const std::locale& loc) {return __date_time_formatter(fmt, value, 0, loc);}
263
274 template<typename type_t, typename Period>
275 inline std::string to_string(const std::chrono::duration<type_t, Period>& value, const std::string& fmt, const std::locale& loc) {return __duration_formatter(fmt, value, loc);}
276
277#if defined(__xtd__cpp_lib_char8_t)
288 template<>
289 inline std::string to_string(const char8& value, const std::string& fmt, const std::locale& loc) {return __character_formatter(fmt, value, loc);}
290#endif
291
302 template<>
303 inline std::string to_string(const char16& value, const std::string& fmt, const std::locale& loc) {return __character_formatter(fmt, value, loc);}
304
315 template<>
316 inline std::string to_string(const char32& value, const std::string& fmt, const std::locale& loc) {return __character_formatter(fmt, value, loc);}
317
328 template<>
329 inline std::string to_string(const wchar& value, const std::string& fmt, const std::locale& loc) {return __character_formatter(fmt, value, loc);}
330
332 std::string to_string(const char* value, const std::string& fmt, const std::locale& loc);
333#if defined(__xtd__cpp_lib_char8_t)
334 std::string to_string(const char8* value, const std::string& fmt, const std::locale& loc);
335#endif
336 std::string to_string(const char16* value, const std::string& fmt, const std::locale& loc);
337 std::string to_string(const char32* value, const std::string& fmt, const std::locale& loc);
338 std::string to_string(const wchar* value, const std::string& fmt, const std::locale& loc);
339 std::string to_string(const std::string& value, const std::string& fmt, const std::locale& loc);
340 std::string to_string(const xtd::string& value, const std::string& fmt, const std::locale& loc);
341#if defined(__xtd__cpp_lib_char8_t)
342 std::string to_string(const std::u8string& value, const std::string& fmt, const std::locale& loc);
343#endif
344 std::string to_string(const std::u16string& value, const std::string& fmt, const std::locale& loc);
345 std::string to_string(const std::u32string& value, const std::string& fmt, const std::locale& loc);
346 std::string to_string(const std::wstring& value, const std::string& fmt, const std::locale& loc);
348
358 template<typename value_t>
359 inline std::string to_string(const value_t& value, const std::string& fmt) {return to_string(value, fmt, std::locale());}
360
362 inline std::string to_string(const char* value, const std::string& fmt) {return to_string(value, fmt, std::locale());}
363
364#if defined(__xtd__cpp_lib_three_way_comparison)
365 template<>
366 inline std::string to_string(const std::partial_ordering& value, const std::string& fmt, const std::locale& loc) {
367 if (value == std::partial_ordering::less) return "less";
368 if (value == std::partial_ordering::greater) return "greater";
369 return "equivalent";
370 }
371
372 template<>
373 inline std::string to_string(const std::strong_ordering& value, const std::string& fmt, const std::locale& loc) {
374 if (value == std::strong_ordering::less) return "less";
375 if (value == std::strong_ordering::greater) return "greater";
376 return "equivalent";
377 }
378
379 template<>
380 inline std::string to_string(const std::weak_ordering& value, const std::string& fmt, const std::locale& loc) {
381 if (value == std::weak_ordering::less) return "less";
382 if (value == std::weak_ordering::greater) return "greater";
383 return "equivalent";
384 }
385#endif
386
387 template<typename value_t>
388 inline std::string to_string(const value_t* value, const std::string& fmt, const std::locale& loc) {
389 if (!value) return "(null)";
390 return __numeric_formatter(fmt, reinterpret_cast<intptr>(value), loc);
391 }
392
393 template<typename value_t>
394 inline std::string to_string(value_t* const value, const std::string& fmt, const std::locale& loc) {
395 if (!value) return "(null)";
396 return __numeric_formatter(fmt, reinterpret_cast<intptr>(value), loc);
397 }
398
399 template<typename type_t>
400 inline std::string to_string(const std::shared_ptr<type_t>& value, const std::string& fmt, const std::locale& loc) {
401 if (!value) return "(null)";
402 return __numeric_formatter(fmt, reinterpret_cast<intptr>(value.get()), loc);
403 }
404
405 template<typename type_t>
406 inline std::string to_string(const std::unique_ptr<type_t>& value, const std::string& fmt, const std::locale& loc) {
407 if (!value) return "(null)";
408 return __numeric_formatter(fmt, reinterpret_cast<intptr>(value.get()), loc);
409 }
410
411 template<>
412 inline std::string to_string(const std::any& value, const std::string& fmt, const std::locale& loc) {
413 auto iterator = __any_stringer__.find(std::type_index(value.type()));
414 return iterator != __any_stringer__.cend() ? xtd::to_string(iterator->second(value), fmt, loc) : "(unregistered)";
415 }
416
417 template<typename type_t>
418 inline std::string to_string(const std::optional<type_t>& value, const std::string& fmt, const std::locale& loc) {return !value.has_value() ? "(null)" : std::string {"("} + to_string(value.value(), fmt, loc) + std::string {")"};}
419
420 template<>
421 inline std::string to_string(const std::nullopt_t& value, const std::string& fmt, const std::locale& loc) {return "(null)";}
422
423 template<>
424 inline std::string to_string(const std::nullptr_t&, const std::string& fmt, const std::locale& loc) {return "null";}
425
426 template<typename type1_t, typename type2_t>
427 inline std::string to_string(const std::pair<type1_t, type2_t>& value, const std::string& fmt, const std::locale& loc) {
428 return std::string {"("} + to_string(value.first, fmt, loc) + std::string {", "} + to_string(value.second, fmt, loc) + std::string {")"};
429 }
430
431 template<typename type_t, unsigned n_t, unsigned last_t>
432 struct __xtd_tuple_stringer {
433 static std::string to_string(const std::string& str, const type_t& value, const std::string& fmt, const std::locale& loc) {return __xtd_tuple_stringer<type_t, n_t + 1, last_t >::to_string(str + xtd::to_string(std::get<n_t>(value), fmt, loc) + ", ", value, fmt, loc);}
434 };
435
436 template<typename type_t, unsigned n_t>
437 struct __xtd_tuple_stringer<type_t, n_t, n_t> {
438 static std::string to_string(const std::string& str, const type_t& value, const std::string& fmt, const std::locale& loc) {return str + xtd::to_string(std::get<n_t>(value), fmt, loc);}
439 };
440
441 template<typename ... types_t>
442 inline std::string to_string(const std::tuple<types_t ...>& value, const std::string& fmt, const std::locale& loc) {return __xtd_tuple_stringer<std::tuple<types_t ...>, 0, sizeof...(types_t) - 1 >::to_string(std::string {"("}, value, fmt, loc) + ")";}
443
444 template<typename iterator_t>
445 inline std::string __xtd_iterator_to_string(const std::string& str, iterator_t iterator, const iterator_t& begin, const iterator_t& end, const std::string& fmt, const std::locale& loc) {
446 if (iterator == end) return str;
447 auto new_str = str + (iterator == begin ? std::string {} : std::string {", "}) + xtd::to_string(*iterator, fmt, loc);
448 return __xtd_iterator_to_string(new_str, ++iterator, begin, end, fmt, loc);
449 }
450
451 template<typename iterator_t>
452 inline std::string __xtd_sequence_container_to_string(const iterator_t& begin, const iterator_t& end, const std::string& fmt, const std::locale& loc) {return __xtd_iterator_to_string("[", begin, begin, end, fmt, loc) + "]";}
453
454 template<typename type_t, size_t size>
455 inline std::string to_string(const std::array<type_t, size>& values, const std::string& fmt, const std::locale& loc) {return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);}
456
457 template<typename type_t, typename allocator_t>
458 inline std::string to_string(const std::deque<type_t, allocator_t>& values, const std::string& fmt, const std::locale& loc) {return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);}
459
460 template<typename type_t, typename allocator_t>
461 inline std::string to_string(const std::forward_list<type_t, allocator_t>& values, const std::string& fmt, const std::locale& loc) {return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);}
462
463 template<typename type_t>
464 inline std::string to_string(const std::initializer_list<type_t>& values, const std::string& fmt, const std::locale& loc) {return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);}
465
466 template<typename type_t, typename allocator_t>
467 inline std::string to_string(const std::list<type_t, allocator_t>& values, const std::string& fmt, const std::locale& loc) {return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);}
468
469 template<typename type_t>
470 inline std::string to_string(const std::valarray<type_t>& values, const std::string& fmt, const std::locale& loc) {return __xtd_sequence_container_to_string(std::begin(values), std::end(values), fmt, loc);}
471
472 template<typename type_t, typename allocator_t>
473 inline std::string to_string(const std::vector<type_t, allocator_t>& values, const std::string& fmt, const std::locale& loc) {return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);}
474
475 template<typename iterator_t>
476 inline std::string __xtd_associative_container_to_string(const iterator_t& begin, const iterator_t& end, const std::string& fmt, const std::locale& loc) {return __xtd_iterator_to_string("{", begin, begin, end, fmt, loc) + "}";}
477
478 template<typename key_t, typename value_t, typename compare_t, typename allocator_t>
479 inline std::string to_string(const std::map<key_t, value_t, compare_t, allocator_t>& values, const std::string& fmt, const std::locale& loc) {return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);}
480
481 template<typename key_t, typename value_t, typename compare_t, typename allocator_t>
482 inline std::string to_string(const std::multimap<key_t, value_t, compare_t, allocator_t>& values, const std::string& fmt, const std::locale& loc) {return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);}
483
484 template<typename key_t, typename compare_t, typename allocator_t>
485 inline std::string to_string(const std::multiset<key_t, compare_t, allocator_t>& values, const std::string& fmt, const std::locale& loc) {return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);}
486
487 template<typename key_t, typename compare_t, typename allocator_t>
488 inline std::string to_string(const std::set<key_t, compare_t, allocator_t>& values, const std::string& fmt, const std::locale& loc) {return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);}
489
490 template<typename key_t, typename value_t, typename compare_t, typename allocator_t>
491 inline std::string to_string(const std::unordered_map<key_t, value_t, compare_t, allocator_t>& values, const std::string& fmt, const std::locale& loc) {return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);}
492
493 template<typename key_t, typename value_t, typename compare_t, typename allocator_t>
494 inline std::string to_string(const std::unordered_multimap<key_t, value_t, compare_t, allocator_t>& values, const std::string& fmt, const std::locale& loc) {return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);}
495
496 template<typename key_t, typename compare_t, typename allocator_t>
497 inline std::string to_string(const std::unordered_multiset<key_t, compare_t, allocator_t>& values, const std::string& fmt, const std::locale& loc) {return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);}
498
499 template<typename key_t, typename compare_t, typename allocator_t>
500 inline std::string to_string(const std::unordered_set<key_t, compare_t, allocator_t>& values, const std::string& fmt, const std::locale& loc) {return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);}
501
502 template<typename type_t>
503 inline std::string to_string(const xtd::collections::generic::ienumerable<type_t>& values, const std::string& fmt, const std::locale& loc) {return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);}
505
516 template<typename value_t>
517 inline std::wstring to_string(const value_t& value, const std::wstring& fmt, const std::locale& loc) {return __to_string_polymorphic(value, fmt, loc, std::is_polymorphic<value_t>());}
518
529 template<>
530 inline std::wstring to_string(const bool& value, const std::wstring& fmt, const std::locale& loc) {return __boolean_formatter(fmt, value, loc);}
531
542 template<>
543 inline std::wstring to_string(const sbyte& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
544
555 template<>
556 inline std::wstring to_string(const char& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
557
568 template<>
569 inline std::wstring to_string(const unsigned char& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
570
581 template<>
582 inline std::wstring to_string(const short& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
583
594 template<>
595 inline std::wstring to_string(const unsigned short& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
596
607 template<>
608 inline std::wstring to_string(const int& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
609
620 template<>
621 inline std::wstring to_string(const unsigned int& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
622
633 template<>
634 inline std::wstring to_string(const long& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
635
646 template<>
647 inline std::wstring to_string(const unsigned long& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
648
659 template<>
660 inline std::wstring to_string(const long long& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
661
672 template<>
673 inline std::wstring to_string(const unsigned long long& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
674
685 template<>
686 inline std::wstring to_string(const float& value, const std::wstring& fmt, const std::locale& loc) {return __floating_point_formatter(fmt, value, loc);}
687
698 template<>
699 inline std::wstring to_string(const double& value, const std::wstring& fmt, const std::locale& loc) {return __floating_point_formatter(fmt, value, loc);}
700
711 template<>
712 inline std::wstring to_string(const long double& value, const std::wstring& fmt, const std::locale& loc) {return __floating_point_formatter(fmt, value, loc);}
713
724 template<>
725 inline std::wstring to_string(const std::chrono::system_clock::time_point& value, const std::wstring& fmt, const std::locale& loc) {return __date_time_formatter(fmt, std::chrono::system_clock::to_time_t(value), std::chrono::duration_cast<std::chrono::milliseconds>(value.time_since_epoch()).count() % 1000000000, loc);}
726
737 template<>
738 inline std::wstring to_string(const std::tm& value, const std::wstring& fmt, const std::locale& loc) {return __date_time_formatter(fmt, value, 0, loc);}
739
750 template<typename type_t, typename Period>
751 inline std::wstring to_string(const std::chrono::duration<type_t, Period>& value, const std::wstring& fmt, const std::locale& loc) {return __duration_formatter(fmt, value, loc);}
752
754 template<typename char_t, typename value_t>
755 inline std::basic_string<char_t> __string_formatter(const std::basic_string<char_t>& fmt, value_t value, const std::locale& loc) {
756 return __format_stringer<char_t>(value);
757 }
759
770 template<>
771 inline std::wstring to_string(const std::wstring& value, const std::wstring& fmt, const std::locale& loc) {return __string_formatter(fmt, value, loc);}
772
773#if defined(__xtd__cpp_lib_char8_t)
784 template<>
785 inline std::wstring to_string(const char8& value, const std::wstring& fmt, const std::locale& loc) {return __character_formatter(fmt, value, loc);}
786#endif
787
798 template<>
799 inline std::wstring to_string(const char16& value, const std::wstring& fmt, const std::locale& loc) {return __character_formatter(fmt, value, loc);}
800
811 template<>
812 inline std::wstring to_string(const char32& value, const std::wstring& fmt, const std::locale& loc) {return __character_formatter(fmt, value, loc);}
813
824 template<>
825 inline std::wstring to_string(const wchar& value, const std::wstring& fmt, const std::locale& loc) {return __character_formatter(fmt, value, loc);}
826
828 inline std::wstring to_string(const wchar* value, const std::wstring& fmt, const std::locale& loc) {return __string_formatter(fmt, value, loc);}
830
840 template<typename value_t>
841 inline std::wstring to_string(const value_t& value, const std::wstring& fmt) {return to_string(value, fmt, std::locale());}
842
844 inline std::wstring to_string(const wchar* value, const std::wstring& fmt) {return to_string(value, fmt, std::locale());}
846}
847
Contains __binary_formatter method.
Contains __boolean_formatter method.
Contains __character_formatter method.
Contains __currency_formatter method.
Contains __date_time_formatter method.
Contains __duration_formatter method.
Contains __floating_point_formatter method.
Contains __format_stringer methods.
Contains __iformatable_formatter method.
Contains __natural_formatter method.
Contains __numeric_formatter method.
Contains __xtd_std_version definitions.
Represents text as a sequence of character units.
Definition basic_string.hpp:79
virtual const_iterator begin() const
Returns an iterator to the first element of the enumarable.
Definition enumerable_iterators.hpp:200
virtual const_iterator end() const
Returns an iterator to the element following the last element of the enumarable.
Definition enumerable_iterators.hpp:215
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.hpp:36
Contains generic stream output methods.
char8_t char8
Represents a 8-bit unicode character.
Definition char8.hpp:27
wchar_t wchar
Represents a wide character.
Definition wchar.hpp:24
int8_t sbyte
Represents a 8-bit signed integer.
Definition sbyte.hpp:23
char16_t char16
Represents a 16-bit unicode character.
Definition char16.hpp:26
char32_t char32
Represents a 32-bit unicode character.
Definition char32.hpp:26
intmax_t intptr
Represent a pointer or a handle.
Definition intptr.hpp:23
std::string to_string(const value_t &value, const std::string &fmt, const std::locale &loc)
Convert a specified value into a string with specified format and locale.
Definition to_string.hpp:41
@ end
The END key.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
Contains xtd::register_any_stringer and xtd::unregister_any_stringer method.
Contains xtd fundamental types.