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<xtd::iterable range_t>
17[[nodiscard]]
auto __xtd_iterable_to_string(
const range_t& values,
const xtd::string& fmt,
const std::locale& loc) -> std::string;
19template<
typename 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);
25 else if constexpr(
xtd::iterable<value_t> && !std::is_same_v<value_t, xtd::string>)
return __xtd_iterable_to_string(value, fmt, loc);
27 auto ss = std::stringstream {};
49 return __boolean_formatter(fmt.chars(), value, loc);
54 return __numeric_formatter(fmt.chars(), value, loc);
59 return __character_formatter(fmt.chars(), value, loc);
64 return __numeric_formatter(fmt.chars(), value, loc);
69 return __numeric_formatter(fmt.chars(), value, loc);
74 return __numeric_formatter(fmt.chars(), value, loc);
79 return __numeric_formatter(fmt.chars(), value, loc);
84 return __numeric_formatter(fmt.chars(), value, loc);
89 return __numeric_formatter(fmt.chars(), value, loc);
94 return __numeric_formatter(fmt.chars(), value, loc);
99 return __numeric_formatter(fmt.chars(), value, loc);
104 return __numeric_formatter(fmt.chars(), value, loc);
109 return __floating_point_formatter(fmt.chars(), value, loc);
114 return __floating_point_formatter(fmt.chars(), value, loc);
119 return __floating_point_formatter(fmt.chars(), value, loc);
124 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);
129 return __date_time_formatter(fmt.chars(), value, 0, loc);
132template<
typename type_t,
typename period_t>
134 return __duration_formatter(fmt.chars(), value, loc);
139 return __character_formatter(fmt.chars(), value, loc);
144 return __character_formatter(fmt.chars(), value, loc);
149 return __character_formatter(fmt.chars(), value, loc);
154 return __character_formatter(fmt.chars(), value, loc);
157template<
typename value_t>
159 return to_string(value, fmt, std::locale());
163 return to_string(value, fmt, std::locale());
166template<
typename type_t>
173 if (value == std::partial_ordering::less)
return "less";
174 if (value == std::partial_ordering::greater)
return "greater";
180 if (value == std::strong_ordering::less)
return "less";
181 if (value == std::strong_ordering::greater)
return "greater";
187 if (value == std::weak_ordering::less)
return "less";
188 if (value == std::weak_ordering::greater)
return "greater";
192template<
typename value_t>
194 if (!value)
return "(null)";
195 return __numeric_formatter(fmt.chars(),
reinterpret_cast<intptr>(value), loc);
198template<
typename value_t>
200 if (!value)
return "(null)";
201 return __numeric_formatter(fmt.chars(),
reinterpret_cast<intptr>(value), loc);
204template<
typename type_t>
206 if (!value)
return "(null)";
207 return __numeric_formatter(fmt.chars(),
reinterpret_cast<intptr>(
value.get()), loc);
210template<
typename type_t>
212 if (!value)
return "(null)";
213 return __numeric_formatter(fmt.chars(),
reinterpret_cast<intptr>(
value.get()), loc);
218 auto iterator = __any_stringer__.find(std::type_index(
value.type()));
219 return iterator != __any_stringer__.cend() ?
xtd::to_string(iterator->second(value), fmt, loc) :
"(unregistered)";
222template<
typename type_t>
224 return !
value.has_value() ?
"(null)" : std::string {
"("} +
to_string(
value.value(), fmt, loc).chars() + std::string {
")"};
237template<
typename type1_t,
typename type2_t>
239 return std::string {
"("} +
to_string(
value.first, fmt, loc).chars() + std::string {
", "} +
to_string(
value.second, fmt, loc).chars() + std::string {
")"};
242template<
typename type_t,
unsigned n_t,
unsigned last_t>
243[[nodiscard]]
inline auto 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) ->
xtd::string {
244 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);
247template<
typename type_t,
unsigned n_t>
248[[nodiscard]]
inline auto 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) ->
xtd::string {
249 return str +
xtd::to_string(std::get<n_t>(value), fmt, loc).chars();
252template<
typename ...types_t>
254 return __xtd_tuple_stringer < std::tuple<types_t ...>, 0,
sizeof...(types_t) - 1 >::to_string(std::string {
"("},
value, fmt, loc) +
")";
257template<
typename iterator_t>
258[[nodiscard]]
inline auto __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) -> std::string {
259 if (iterator == end)
return str;
260 auto new_str = str + (iterator ==
begin ? std::string {} : std::string {
", "}) +
xtd::to_string(*iterator, fmt, loc).chars();
261 return __xtd_iterator_to_string(new_str, ++iterator, begin, end, fmt, loc);
264template<
typename iterator_t>
265[[nodiscard]]
inline auto __xtd_sequence_container_to_string(
const iterator_t& begin,
const iterator_t& end,
const xtd::string& fmt,
const std::locale& loc) -> std::string {
266 return __xtd_iterator_to_string(
"[", begin, begin, end, fmt, loc) +
"]";
269template<xtd::iterable iterable_t>
270[[nodiscard]]
inline auto __xtd_iterable_to_string(
const iterable_t& values,
const xtd::string& fmt,
const std::locale& loc) -> std::string {
271 std::ostringstream oss;
275 auto& mutable_values =
const_cast<iterable_t&
>(values);
276 for (
const auto value : mutable_values) {
277 if (!first) oss <<
", ";
285template<
typename char_t,
class traits_t>
288 for (
const auto& c : chars)
293template<
typename type_t, xtd::usize size>
295 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
298template<xtd::usize size>
301 for (
auto index =
xtd::usize {0}; index < values.length(); ++index)
302 result += (index ?
", " :
"") +
to_string(
static_cast<bool>(values[index]), fmt, loc);
306template<
typename type_t,
typename allocator_t>
308 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
311template<
typename type_t,
typename allocator_t>
313 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
316template<
typename type_t>
318 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
321template<
typename type_t,
typename allocator_t>
323 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
326template<
typename type_t,
typename container_t>
328 struct accessor :
public std::queue<type_t> {
static auto get() {
return &accessor::c;}};
329 const auto& underlying_items = values.*accessor::get();
330 return __xtd_sequence_container_to_string(underlying_items.begin(), underlying_items.end(), fmt, loc);
333template<
typename type_t,
typename container_t>
335 struct std_priority_queue :
public std::queue<type_t> {
336 std_priority_queue(
const std::priority_queue<type_t>& queue) :
ptr {reinterpret_cast<const std_priority_queue*>(&
queue)} {}
337 auto begin()
const {
return ptr->c.begin();}
338 auto end()
const {
return ptr->c.end();}
339 const std_priority_queue*
ptr;
341 auto items = std_priority_queue {values};
342 return __xtd_sequence_container_to_string(items.begin(), items.end(), fmt, loc);
345template<
typename type_t,
typename container_t>
347 struct accessor :
public std::stack<type_t> {
static auto get() {
return &accessor::c;}};
348 const auto& underlying_items = values.*accessor::get();
349 return __xtd_sequence_container_to_string(underlying_items.rbegin(), underlying_items.rend(), fmt, loc);
352template<
typename type_t>
354 return __xtd_sequence_container_to_string(std::begin(values), std::end(values), fmt, loc);
357template<
typename type_t,
typename allocator_t>
359 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
362template<
typename iterator_t>
363[[nodiscard]]
inline auto __xtd_associative_container_to_string(
const iterator_t& begin,
const iterator_t& end,
const xtd::string& fmt,
const std::locale& loc) -> std::string {
364 return __xtd_iterator_to_string(
"{", begin, begin, end, fmt, loc) +
"}";
367template<
typename key_t,
typename value_t,
typename compare_t,
typename allocator_t>
369 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
372template<
typename key_t,
typename value_t,
typename compare_t,
typename allocator_t>
374 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
377template<
typename key_t,
typename compare_t,
typename allocator_t>
379 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
382template<
typename key_t,
typename compare_t,
typename allocator_t>
384 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
387template<
typename key_t,
typename value_t,
typename compare_t,
typename allocator_t>
388[[nodiscard]]
inline auto xtd::to_string(
const std::unordered_map<key_t, value_t, compare_t, allocator_t>& values,
const xtd::string& fmt,
const std::locale& loc) ->
xtd::string {
389 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
392template<
typename key_t,
typename value_t,
typename compare_t,
typename allocator_t>
393[[nodiscard]]
inline auto xtd::to_string(
const std::unordered_multimap<key_t, value_t, compare_t, allocator_t>& values,
const xtd::string& fmt,
const std::locale& loc) ->
xtd::string {
394 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
397template<
typename key_t,
typename compare_t,
typename allocator_t>
399 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
402template<
typename key_t,
typename compare_t,
typename allocator_t>
404 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
407template<
typename ...args_t>
409 if (
value.valueless_by_exception())
return "(valueless_by_exception)";
414template<
typename type_t,
typename extents_t,
typename layout_policy_t,
typename accessor_policy_t>
415[[nodiscard]]
inline auto xtd::to_string(
const std::mdspan<type_t, extents_t, layout_policy_t, accessor_policy_t>& values,
const xtd::string& fmt,
const std::locale& loc) ->
xtd::string {
416 auto indices = std::array<
xtd::usize, values.rank()> {};
418 if (dimension == values.rank())
return xtd::to_string(values[indices], fmt, loc);
420 for (
xtd::usize i = 0;
i < values.extent(dimension); ++
i) {
421 if (i != 0) result +=
", ";
422 indices[dimension] =
i;
423 result += self(self, dimension + 1);
432template<
typename type_t>
433[[nodiscard]]
inline auto xtd::to_string(type_t value,
const std::initializer_list<std::pair<type_t, xtd::string>>& il) ->
xtd::string {
434 std::map<type_t, xtd::string, std::greater<type_t>> values;
435 for (
const auto& item : il) values[item.first] = item.second;
444template<
typename type_t>
446 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
449template<
typename type_t>
451 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
454template<
typename type_t,
typename string_t>
455[[nodiscard]]
inline auto xtd::to_string(type_t value,
const std::map<type_t, string_t, std::greater<type_t>>& values) -> string_t {
456 auto it = values.find(value);
457 if (it != values.end())
return it->second;
459 long long rest =
static_cast<long long>(
value);
460 for (
const auto& item : values) {
461 if (
static_cast<long long>(item.first) != 0 && (rest &
static_cast<long long>(item.first)) ==
static_cast<long long>(item.first)) {
462 if (!result.empty()) result = string_t {
',',
' '} + result;
463 result = item.second + result;
464 rest -=
static_cast<long long>(item.first);
467 if (!result.empty())
return result;
468 return to_string(
static_cast<long long>(value), string_t {
'G'}, std::locale());
471template<
typename type_t,
typename string_t>
472[[nodiscard]]
inline auto xtd::to_string(type_t value,
const std::map<type_t, string_t>& values) -> string_t {
473 std::map<type_t, string_t, std::greater<type_t>> descending_values;
474 for (
const auto& item : values) descending_values[item.first] = item.second;
475 return to_string(value, descending_values);
478template<
typename char_t,
typename value_t>
479[[nodiscard]]
inline auto __string_formatter(
const std::basic_string<char_t>& fmt, value_t value,
const std::locale& loc) -> std::basic_string<char_t> {
480 return __format_stringer<char_t>(value);
484[[nodiscard]]
inline auto __format_stringer<char, bool&>(
bool& value) -> std::string {
489[[nodiscard]]
inline auto __format_stringer<char, xtd::sbyte&>(
xtd::sbyte& value) -> std::string {
494[[nodiscard]]
inline auto __format_stringer<char, const unsigned char&>(
const unsigned char& value) -> std::string {
499[[nodiscard]]
inline auto __format_stringer<char, unsigned char&>(
unsigned char& value) -> std::string {
504[[nodiscard]]
inline auto __format_stringer<char, short&>(
short& value) -> std::string {
509[[nodiscard]]
inline auto __format_stringer<char, unsigned short&>(
unsigned short& value) -> std::string {
514[[nodiscard]]
inline auto __format_stringer<char, int&>(
int& value) -> std::string {
519[[nodiscard]]
inline auto __format_stringer<char, unsigned int&>(
unsigned int& value) -> std::string {
524[[nodiscard]]
inline auto __format_stringer<char, long&>(
long& value) -> std::string {
529[[nodiscard]]
inline auto __format_stringer<char, unsigned long&>(
unsigned long& value) -> std::string {
534[[nodiscard]]
inline auto __format_stringer<char, long long&>(
long long& value) -> std::string {
539[[nodiscard]]
inline auto __format_stringer<char, unsigned long long&>(
unsigned long long& value) -> std::string {
544[[nodiscard]]
inline auto __format_stringer<char, float&>(
float& value) -> std::string {
549[[nodiscard]]
inline auto __format_stringer<char, double&>(
double& value) -> std::string {
554[[nodiscard]]
inline auto __format_stringer<char, long double&>(
long double& value) -> std::string {
559[[nodiscard]]
inline auto __format_stringer<char, std::chrono::system_clock::time_point&>(std::chrono::system_clock::time_point& value) -> std::string {
564[[nodiscard]]
inline auto __format_stringer<char, std::tm&>(tm& value) -> std::string {
569[[nodiscard]]
inline auto __format_stringer<char, xtd::wchar&>(
xtd::wchar& value) -> std::string {
574[[nodiscard]]
inline auto __format_stringer<char, xtd::char8&>(
xtd::char8& value) -> std::string {
579[[nodiscard]]
inline auto __format_stringer<char, xtd::char16&>(
xtd::char16& value) -> std::string {
584[[nodiscard]]
inline auto __format_stringer<char, xtd::char32&>(
xtd::char32& value) -> std::string {
588template<
typename char_t,
typename type_t,
typename period_t = std::ratio<1>>
589auto operator <<(std::basic_ostream<char_t>& os,
const std::chrono::duration<type_t, period_t>& value) -> std::basic_ostream<char_t>& {
590 return os <<
xtd::to_string(value, std::basic_string<char_t> {
'G'}, std::locale());
593template <
class char_t,
typename type_t >
594struct __enum_ostream__<char_t, type_t, std::false_type> {
595 auto to_stream(std::basic_ostream < char_t >& os,
const type_t& value)
noexcept -> std::basic_ostream<char_t>& {
597 return os <<
xtd::to_string(value, std::basic_string < char_t > {}, std::locale {});
Internal vector-like container used as a storage backend for xtd collections.
Definition raw_array.hpp:38
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.hpp:46
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
Definition iterable.hpp:15
Definition stream_insertable.hpp:13
Definition textual.hpp:16
generic::queue< xtd::any_object > queue
Represents a first-in, first-out collection of objects.
Definition queue.hpp:27
@ value
Represnets the constant operator precedence (42).
Definition operator_precedence.hpp:30
@ 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
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
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
std::remove_cvref_t< value_t > raw_type
Represents a raw type alias equivalent to std::remove_cvref_t<value_t>.
Definition raw_type.hpp:25
@ begin
Specifies the beginning of a stream.
Definition seek_origin.hpp:18
@ end
The END key.
Definition console_key.hpp:42
@ i
The I key.
Definition console_key.hpp:104
auto first() const -> read_only_span< type_t, count >
Obtains a subspan consisting of the first count elements of the sequence.
Definition read_only_span.hpp:245
auto to_string() const noexcept -> xtd::string override
Returns the string representation of this xtd::read_only_span <type_t> object.
Definition read_only_span.hpp:354
Contains xtd::stream_insertable concept.
Contains xtd::to_string methods.