xtd 0.2.0
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<class 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<class type_t, class period_t>
275 inline std::string to_string(const std::chrono::duration<type_t, period_t>& value, const std::string& fmt, const std::locale& loc) {return __duration_formatter(fmt, value, loc);}
276
287 template<>
288 inline std::string to_string(const char8& value, const std::string& fmt, const std::locale& loc) {return __character_formatter(fmt, value, loc);}
289
300 template<>
301 inline std::string to_string(const char16& value, const std::string& fmt, const std::locale& loc) {return __character_formatter(fmt, value, loc);}
302
313 template<>
314 inline std::string to_string(const char32& value, const std::string& fmt, const std::locale& loc) {return __character_formatter(fmt, value, loc);}
315
326 template<>
327 inline std::string to_string(const wchar& value, const std::string& fmt, const std::locale& loc) {return __character_formatter(fmt, value, loc);}
328
330 std::string to_string(const char* value, const std::string& fmt, const std::locale& loc);
331 std::string to_string(const char8* value, const std::string& fmt, const std::locale& loc);
332 std::string to_string(const char16* value, const std::string& fmt, const std::locale& loc);
333 std::string to_string(const char32* value, const std::string& fmt, const std::locale& loc);
334 std::string to_string(const wchar* value, const std::string& fmt, const std::locale& loc);
335 std::string to_string(const std::string& value, const std::string& fmt, const std::locale& loc);
336 std::string to_string(const xtd::string& value, const std::string& fmt, const std::locale& loc);
337 std::string to_string(const std::u8string& value, const std::string& fmt, const std::locale& loc);
338 std::string to_string(const std::u16string& value, const std::string& fmt, const std::locale& loc);
339 std::string to_string(const std::u32string& value, const std::string& fmt, const std::locale& loc);
340 std::string to_string(const std::wstring& value, const std::string& fmt, const std::locale& loc);
342
352 template<class value_t>
353 inline std::string to_string(const value_t& value, const std::string& fmt) {return to_string(value, fmt, std::locale());}
354
356 inline std::string to_string(const char* value, const std::string& fmt) {return to_string(value, fmt, std::locale());}
357
358 template<>
359 inline std::string to_string(const std::partial_ordering& value, const std::string& fmt, const std::locale& loc) {
360 if (value == std::partial_ordering::less) return "less";
361 if (value == std::partial_ordering::greater) return "greater";
362 return "equivalent";
363 }
364
365 template<>
366 inline std::string to_string(const std::strong_ordering& value, const std::string& fmt, const std::locale& loc) {
367 if (value == std::strong_ordering::less) return "less";
368 if (value == std::strong_ordering::greater) return "greater";
369 return "equivalent";
370 }
371
372 template<>
373 inline std::string to_string(const std::weak_ordering& value, const std::string& fmt, const std::locale& loc) {
374 if (value == std::weak_ordering::less) return "less";
375 if (value == std::weak_ordering::greater) return "greater";
376 return "equivalent";
377 }
378
379 template<class value_t>
380 inline std::string to_string(const value_t* value, const std::string& fmt, const std::locale& loc) {
381 if (!value) return "(null)";
382 return __numeric_formatter(fmt, reinterpret_cast<intptr>(value), loc);
383 }
384
385 template<class value_t>
386 inline std::string to_string(value_t* const value, const std::string& fmt, const std::locale& loc) {
387 if (!value) return "(null)";
388 return __numeric_formatter(fmt, reinterpret_cast<intptr>(value), loc);
389 }
390
391 template<class type_t>
392 inline std::string to_string(const std::shared_ptr<type_t>& value, const std::string& fmt, const std::locale& loc) {
393 if (!value) return "(null)";
394 return __numeric_formatter(fmt, reinterpret_cast<intptr>(value.get()), loc);
395 }
396
397 template<class type_t>
398 inline std::string to_string(const std::unique_ptr<type_t>& value, const std::string& fmt, const std::locale& loc) {
399 if (!value) return "(null)";
400 return __numeric_formatter(fmt, reinterpret_cast<intptr>(value.get()), loc);
401 }
402
403 template<>
404 inline std::string to_string(const xtd::any& value, const std::string& fmt, const std::locale& loc) {
405 auto iterator = __any_stringer__.find(std::type_index(value.type()));
406 return iterator != __any_stringer__.cend() ? xtd::to_string(iterator->second(value), fmt, loc) : "(unregistered)";
407 }
408
409 template<class type_t>
410 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 {")"};}
411
412 template<>
413 inline std::string to_string(const std::nullopt_t& value, const std::string& fmt, const std::locale& loc) {return "(null)";}
414
415 template<>
416 inline std::string to_string(const std::nullptr_t&, const std::string& fmt, const std::locale& loc) {return "null";}
417
418 template<class type1_t, class type2_t>
419 inline std::string to_string(const std::pair<type1_t, type2_t>& value, const std::string& fmt, const std::locale& loc) {
420 return std::string {"("} + to_string(value.first, fmt, loc) + std::string {", "} + to_string(value.second, fmt, loc) + std::string {")"};
421 }
422
423 template<class type_t, unsigned n_t, unsigned last_t>
424 struct __xtd_tuple_stringer {
425 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);}
426 };
427
428 template<class type_t, unsigned n_t>
429 struct __xtd_tuple_stringer<type_t, n_t, n_t> {
430 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);}
431 };
432
433 template<class ...types_t>
434 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) + ")";}
435
436 template<class iterator_t>
437 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) {
438 if (iterator == end) return str;
439 auto new_str = str + (iterator == begin ? std::string {} : std::string {", "}) + xtd::to_string(*iterator, fmt, loc);
440 return __xtd_iterator_to_string(new_str, ++iterator, begin, end, fmt, loc);
441 }
442
443 template<class iterator_t>
444 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) + "]";}
445
446 template<class type_t, size_t size>
447 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);}
448
449 template<class type_t, class allocator_t>
450 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);}
451
452 template<class type_t, class allocator_t>
453 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);}
454
455 template<class type_t>
456 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);}
457
458 template<class type_t, class allocator_t>
459 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);}
460
461 template<class type_t>
462 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);}
463
464 template<class type_t, class allocator_t>
465 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);}
466
467 template<class iterator_t>
468 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) + "}";}
469
470 template<class key_t, class value_t, class compare_t, class allocator_t>
471 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);}
472
473 template<class key_t, class value_t, class compare_t, class allocator_t>
474 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);}
475
476 template<class key_t, class compare_t, class allocator_t>
477 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);}
478
479 template<class key_t, class compare_t, class allocator_t>
480 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);}
481
482 template<class key_t, class value_t, class compare_t, class allocator_t>
483 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);}
484
485 template<class key_t, class value_t, class compare_t, class allocator_t>
486 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);}
487
488 template<class key_t, class compare_t, class allocator_t>
489 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);}
490
491 template<class key_t, class compare_t, class allocator_t>
492 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);}
493
494 template<class type_t>
495 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);}
497
508 template<class value_t>
509 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>());}
510
521 template<>
522 inline std::wstring to_string(const bool& value, const std::wstring& fmt, const std::locale& loc) {return __boolean_formatter(fmt, value, loc);}
523
534 template<>
535 inline std::wstring to_string(const sbyte& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
536
547 template<>
548 inline std::wstring to_string(const char& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
549
560 template<>
561 inline std::wstring to_string(const unsigned char& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
562
573 template<>
574 inline std::wstring to_string(const short& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
575
586 template<>
587 inline std::wstring to_string(const unsigned short& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
588
599 template<>
600 inline std::wstring to_string(const int& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
601
612 template<>
613 inline std::wstring to_string(const unsigned int& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
614
625 template<>
626 inline std::wstring to_string(const long& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
627
638 template<>
639 inline std::wstring to_string(const unsigned long& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
640
651 template<>
652 inline std::wstring to_string(const long long& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
653
664 template<>
665 inline std::wstring to_string(const unsigned long long& value, const std::wstring& fmt, const std::locale& loc) {return __numeric_formatter(fmt, value, loc);}
666
677 template<>
678 inline std::wstring to_string(const float& value, const std::wstring& fmt, const std::locale& loc) {return __floating_point_formatter(fmt, value, loc);}
679
690 template<>
691 inline std::wstring to_string(const double& value, const std::wstring& fmt, const std::locale& loc) {return __floating_point_formatter(fmt, value, loc);}
692
703 template<>
704 inline std::wstring to_string(const long double& value, const std::wstring& fmt, const std::locale& loc) {return __floating_point_formatter(fmt, value, loc);}
705
716 template<>
717 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);}
718
729 template<>
730 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);}
731
742 template<class type_t, class period_t>
743 inline std::wstring to_string(const std::chrono::duration<type_t, period_t>& value, const std::wstring& fmt, const std::locale& loc) {return __duration_formatter(fmt, value, loc);}
744
746 template<class char_t, class value_t>
747 inline std::basic_string<char_t> __string_formatter(const std::basic_string<char_t>& fmt, value_t value, const std::locale& loc) {
748 return __format_stringer<char_t>(value);
749 }
751
762 template<>
763 inline std::wstring to_string(const std::wstring& value, const std::wstring& fmt, const std::locale& loc) {return __string_formatter(fmt, value, loc);}
764
775 template<>
776 inline std::wstring to_string(const char8& value, const std::wstring& fmt, const std::locale& loc) {return __character_formatter(fmt, value, loc);}
777
788 template<>
789 inline std::wstring to_string(const char16& value, const std::wstring& fmt, const std::locale& loc) {return __character_formatter(fmt, value, loc);}
790
801 template<>
802 inline std::wstring to_string(const char32& value, const std::wstring& fmt, const std::locale& loc) {return __character_formatter(fmt, value, loc);}
803
814 template<>
815 inline std::wstring to_string(const wchar& value, const std::wstring& fmt, const std::locale& loc) {return __character_formatter(fmt, value, loc);}
816
818 inline std::wstring to_string(const wchar* value, const std::wstring& fmt, const std::locale& loc) {return __string_formatter(fmt, value, loc);}
820
830 template<class value_t>
831 inline std::wstring to_string(const value_t& value, const std::wstring& fmt) {return to_string(value, fmt, std::locale());}
832
834 inline std::wstring to_string(const wchar* value, const std::wstring& fmt) {return to_string(value, fmt, std::locale());}
836}
837
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.
size_type find(const basic_string &str) const
Finds the first substring equal to the given character sequence. Search begins at 0,...
Definition basic_string.hpp:1144
virtual const_iterator end() const
Returns an iterator to the element following the last element of the enumarable.
Definition enumerable_iterators.hpp:170
virtual const_iterator begin() const
Returns an iterator to the first element of the enumarable.
Definition enumerable_iterators.hpp:155
Contains generic stream output methods.
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
int8_t sbyte
Represents a 8-bit signed integer.
Definition sbyte.hpp:23
wchar_t wchar
Represents a wide character.
Definition wchar.hpp:24
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
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
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
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
const_iterator end() const
Returns an iterator to the end.
Definition read_only_span.hpp:213
Contains xtd::register_any_stringer and xtd::unregister_any_stringer method.
Contains xtd fundamental types.