xtd 1.0.0
Loading...
Searching...
No Matches
to_string_.hpp
Go to the documentation of this file.
1
4#pragma once
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."
8#endif
10
11//#include "globalization/culture_info.hpp"
12#include "stream_insertable.hpp"
13#include "to_string.hpp"
14
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;
18
19template<typename value_t>
20[[nodiscard]] inline auto xtd::to_string(const value_t& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
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 else if constexpr(requires(const xtd::raw_type<value_t>& value, const xtd::string& _fmt) {{value.to_string(_fmt)} -> xtd::textual;}) return value.to_string(fmt);
24 else if constexpr(requires(const xtd::raw_type<value_t>& value) {{value.to_string()} -> xtd::textual;}) return value.to_string();
25 else if constexpr(xtd::iterable<value_t> && !std::is_same_v<value_t, xtd::string>) return __xtd_iterable_to_string(value, fmt, loc);
26 else if constexpr(xtd::stream_insertable<value_t>) {
27 auto ss = std::stringstream {};
28 ss << value;
29 return ss.str();
31}
32
33/*
34template<typename value_t>
35requires (requires (const xtd::raw_type<value_t>& value, const xtd::string& fmt) {{value.to_string(fmt)} -> xtd::textual;})
36[[nodiscard]] inline auto xtd::to_string(const value_t& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
37 return value.to_string(fmt);
38}
39
40template<typename value_t>
41requires (!requires (const xtd::raw_type<value_t>& value, const xtd::string& fmt) {{value.to_string(fmt)} -> xtd::textual;} && requires (const xtd::raw_type<value_t>& value) {{value.to_string()} -> xtd::textual;})
42[[nodiscard]] inline auto xtd::to_string(const value_t& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
43 return value.to_string();
44}
45*/
46
47template<>
48[[nodiscard]] inline auto xtd::to_string(const bool& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
49 return __boolean_formatter(fmt.chars(), value, loc);
50}
51
52template<>
53[[nodiscard]] inline auto xtd::to_string(const sbyte& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
54 return __numeric_formatter(fmt.chars(), value, loc);
55}
56
57template<>
58[[nodiscard]] inline auto xtd::to_string(const char& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
59 return __character_formatter(fmt.chars(), value, loc);
60}
61
62template<>
63[[nodiscard]] inline auto xtd::to_string(const unsigned char& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
64 return __numeric_formatter(fmt.chars(), value, loc);
65}
66
67template<>
68[[nodiscard]] inline auto xtd::to_string(const short& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
69 return __numeric_formatter(fmt.chars(), value, loc);
70}
71
72template<>
73[[nodiscard]] inline auto xtd::to_string(const unsigned short& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
74 return __numeric_formatter(fmt.chars(), value, loc);
75}
76
77template<>
78[[nodiscard]] inline auto xtd::to_string(const int& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
79 return __numeric_formatter(fmt.chars(), value, loc);
80}
81
82template<>
83[[nodiscard]] inline auto xtd::to_string(const unsigned int& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
84 return __numeric_formatter(fmt.chars(), value, loc);
85}
86
87template<>
88[[nodiscard]] inline auto xtd::to_string(const long& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
89 return __numeric_formatter(fmt.chars(), value, loc);
90}
91
92template<>
93[[nodiscard]] inline auto xtd::to_string(const unsigned long& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
94 return __numeric_formatter(fmt.chars(), value, loc);
95}
96
97template<>
98[[nodiscard]] inline auto xtd::to_string(const long long& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
99 return __numeric_formatter(fmt.chars(), value, loc);
100}
101
102template<>
103[[nodiscard]] inline auto xtd::to_string(const unsigned long long& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
104 return __numeric_formatter(fmt.chars(), value, loc);
105}
106
107template<>
108[[nodiscard]] inline auto xtd::to_string(const float& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
109 return __floating_point_formatter(fmt.chars(), value, loc);
110}
111
112template<>
113[[nodiscard]] inline auto xtd::to_string(const double& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
114 return __floating_point_formatter(fmt.chars(), value, loc);
115}
116
117template<>
118[[nodiscard]] inline auto xtd::to_string(const long double& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
119 return __floating_point_formatter(fmt.chars(), value, loc);
120}
121
122template<>
123[[nodiscard]] inline auto xtd::to_string(const std::chrono::system_clock::time_point& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
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);
125}
126
127template<>
128[[nodiscard]] inline auto xtd::to_string(const std::tm& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
129 return __date_time_formatter(fmt.chars(), value, 0, loc);
130}
131
132template<typename type_t, typename period_t>
133[[nodiscard]] inline auto xtd::to_string(const std::chrono::duration<type_t, period_t>& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
134 return __duration_formatter(fmt.chars(), value, loc);
135}
136
137template<>
138[[nodiscard]] inline auto xtd::to_string(const char8& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
139 return __character_formatter(fmt.chars(), value, loc);
140}
141
142template<>
143[[nodiscard]] inline auto xtd::to_string(const char16& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
144 return __character_formatter(fmt.chars(), value, loc);
145}
146
147template<>
148[[nodiscard]] inline auto xtd::to_string(const char32& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
149 return __character_formatter(fmt.chars(), value, loc);
150}
151
152template<>
153[[nodiscard]] inline auto xtd::to_string(const wchar& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
154 return __character_formatter(fmt.chars(), value, loc);
155}
156
157template<typename value_t>
158[[nodiscard]] inline auto xtd::to_string(const value_t& value, const xtd::string& fmt) -> xtd::string {
159 return to_string(value, fmt, std::locale());
160}
161
162[[nodiscard]] inline auto xtd::to_string(const char* value, const xtd::string& fmt) -> xtd::string {
163 return to_string(value, fmt, std::locale());
164}
165
166template<typename type_t>
167[[nodiscard]] inline auto xtd::to_string(const std::atomic<type_t>& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
168 return to_string(value.load(), fmt, loc);
169}
170
171template<>
172[[nodiscard]] inline auto xtd::to_string(const std::partial_ordering& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
173 if (value == std::partial_ordering::less) return "less";
174 if (value == std::partial_ordering::greater) return "greater";
175 return "equivalent";
176}
177
178template<>
179[[nodiscard]] inline auto xtd::to_string(const std::strong_ordering& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
180 if (value == std::strong_ordering::less) return "less";
181 if (value == std::strong_ordering::greater) return "greater";
182 return "equivalent";
183}
184
185template<>
186[[nodiscard]] inline auto xtd::to_string(const std::weak_ordering& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
187 if (value == std::weak_ordering::less) return "less";
188 if (value == std::weak_ordering::greater) return "greater";
189 return "equivalent";
190}
191
192template<typename value_t>
193[[nodiscard]] inline auto xtd::to_string(const value_t* value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
194 if (!value) return "(null)";
195 return __numeric_formatter(fmt.chars(), reinterpret_cast<intptr>(value), loc);
196}
197
198template<typename value_t>
199[[nodiscard]] inline auto xtd::to_string(value_t* const value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
200 if (!value) return "(null)";
201 return __numeric_formatter(fmt.chars(), reinterpret_cast<intptr>(value), loc);
202}
203
204template<typename type_t>
205[[nodiscard]] inline auto xtd::to_string(const std::shared_ptr<type_t>& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
206 if (!value) return "(null)";
207 return __numeric_formatter(fmt.chars(), reinterpret_cast<intptr>(value.get()), loc);
208}
209
210template<typename type_t>
211[[nodiscard]] inline auto xtd::to_string(const std::unique_ptr<type_t>& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
212 if (!value) return "(null)";
213 return __numeric_formatter(fmt.chars(), reinterpret_cast<intptr>(value.get()), loc);
214}
215
216template<>
217[[nodiscard]] inline auto xtd::to_string(const xtd::any& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
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)";
220}
221
222template<typename type_t>
223[[nodiscard]] inline auto xtd::to_string(const std::optional<type_t>& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
224 return !value.has_value() ? "(null)" : std::string {"("} + to_string(value.value(), fmt, loc).chars() + std::string {")"};
225}
226
227template<>
228[[nodiscard]] inline auto xtd::to_string(const std::nullopt_t& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
229 return "(null)";
230}
231
232template<>
233[[nodiscard]] inline auto xtd::to_string(const std::nullptr_t&, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
234 return "null";
235}
236
237template<typename type1_t, typename type2_t>
238[[nodiscard]] inline auto xtd::to_string(const std::pair<type1_t, type2_t>& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
239 return std::string {"("} + to_string(value.first, fmt, loc).chars() + std::string {", "} + to_string(value.second, fmt, loc).chars() + std::string {")"};
240}
241
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);
245}
246
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();
250}
251
252template<typename ...types_t>
253[[nodiscard]] inline auto xtd::to_string(const std::tuple<types_t ...>& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
254 return __xtd_tuple_stringer < std::tuple<types_t ...>, 0, sizeof...(types_t) - 1 >::to_string(std::string {"("}, value, fmt, loc) + ")";
255}
256
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);
262}
263
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) + "]";
267}
268
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;
272 oss.imbue(loc);
273 oss << "[";
274 auto first = true;
275 auto& mutable_values = const_cast<iterable_t&>(values);
276 for (const auto value : mutable_values) {
277 if (!first) oss << ", ";
278 first = false;
279 oss << xtd::to_string(value, fmt, loc);
280 }
281 oss << "]";
282 return oss.str();
283}
284
285template<typename char_t, class traits_t>
286[[nodiscard]] inline auto xtd::to_string(const std::basic_string_view<char_t, traits_t>& chars, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
287 auto str = xtd::string {};
288 for (const auto& c : chars)
289 str += xtd::to_string(c, fmt, loc);
290 return str;
291}
292
293template<typename type_t, xtd::usize size>
294[[nodiscard]] inline auto xtd::to_string(const std::array<type_t, size>& values, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
295 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
296}
297
298template<xtd::usize size>
299[[nodiscard]] inline auto xtd::to_string(const std::bitset<size>& values, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
300 auto result = xtd::string {"["};
301 for (auto index = xtd::usize {0}; index < values.length(); ++index)
302 result += (index ? ", " : "") + to_string(static_cast<bool>(values[index]), fmt, loc);
303 return result + "]";
304}
305
306template<typename type_t, typename allocator_t>
307[[nodiscard]] inline auto xtd::to_string(const std::deque<type_t, allocator_t>& values, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
308 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
309}
310
311template<typename type_t, typename allocator_t>
312[[nodiscard]] inline auto xtd::to_string(const std::forward_list<type_t, allocator_t>& values, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
313 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
314}
315
316template<typename type_t>
317[[nodiscard]] inline auto xtd::to_string(const std::initializer_list<type_t>& values, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
318 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
319}
320
321template<typename type_t, typename allocator_t>
322[[nodiscard]] inline auto xtd::to_string(const std::list<type_t, allocator_t>& values, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
323 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
324}
325
326template<typename type_t, typename container_t>
327[[nodiscard]] inline auto xtd::to_string(const std::queue<type_t, container_t>& values, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
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);
331}
332
333template<typename type_t, typename container_t>
334[[nodiscard]] inline auto xtd::to_string(const std::priority_queue<type_t, container_t>& values, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
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;
340 };
341 auto items = std_priority_queue {values};
342 return __xtd_sequence_container_to_string(items.begin(), items.end(), fmt, loc);
343}
344
345template<typename type_t, typename container_t>
346[[nodiscard]] inline auto xtd::to_string(const std::stack<type_t, container_t>& values, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
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);
350}
351
352template<typename type_t>
353[[nodiscard]] inline auto xtd::to_string(const std::valarray<type_t>& values, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
354 return __xtd_sequence_container_to_string(std::begin(values), std::end(values), fmt, loc);
355}
356
357template<typename type_t, typename allocator_t>
358[[nodiscard]] inline auto xtd::to_string(const std::vector<type_t, allocator_t>& values, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
359 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
360}
361
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) + "}";
365}
366
367template<typename key_t, typename value_t, typename compare_t, typename allocator_t>
368[[nodiscard]] inline auto xtd::to_string(const std::map<key_t, value_t, compare_t, allocator_t>& values, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
369 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
370}
371
372template<typename key_t, typename value_t, typename compare_t, typename allocator_t>
373[[nodiscard]] inline auto xtd::to_string(const std::multimap<key_t, value_t, compare_t, allocator_t>& values, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
374 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
375}
376
377template<typename key_t, typename compare_t, typename allocator_t>
378[[nodiscard]] inline auto xtd::to_string(const std::multiset<key_t, compare_t, allocator_t>& values, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
379 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
380}
381
382template<typename key_t, typename compare_t, typename allocator_t>
383[[nodiscard]] inline auto xtd::to_string(const std::set<key_t, compare_t, allocator_t>& values, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
384 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
385}
386
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);
390}
391
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);
395}
396
397template<typename key_t, typename compare_t, typename allocator_t>
398[[nodiscard]] inline auto xtd::to_string(const std::unordered_multiset<key_t, compare_t, allocator_t>& values, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
399 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
400}
401
402template<typename key_t, typename compare_t, typename allocator_t>
403[[nodiscard]] inline auto xtd::to_string(const std::unordered_set<key_t, compare_t, allocator_t>& values, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
404 return __xtd_associative_container_to_string(values.begin(), values.end(), fmt, loc);
405}
406
407template<typename ...args_t>
408[[nodiscard]] inline auto xtd::to_string(const std::variant<args_t...>& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
409 if (value.valueless_by_exception()) return "(valueless_by_exception)";
410 return std::visit([&](auto && t){return xtd::to_string(t, fmt, loc);}, value);
411}
412
413#if __cpp_lib_mdspan
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()> {};
417 auto format = [&](const auto& self, xtd::usize dimension) -> xtd::string {
418 if (dimension == values.rank()) return xtd::to_string(values[indices], fmt, loc);
419 auto result = xtd::string {"["};
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);
424 }
425 result += "]";
426 return result;
427 };
428 return format(format, 0);
429}
430#endif
431
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;
436 return to_string(value, values);
437}
438
439template<>
440inline auto xtd::to_string(const std::filesystem::path& value, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
441 return to_string(value.string(), fmt, loc);
442}
443
444template<typename type_t>
445[[nodiscard]] inline auto xtd::to_string(const xtd::collections::generic::ienumerable<type_t>& values, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
446 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
447}
448
449template<typename type_t>
450[[nodiscard]] inline auto xtd::to_string(const xtd::collections::generic::helpers::raw_array<type_t>& values, const xtd::string& fmt, const std::locale& loc) -> xtd::string {
451 return __xtd_sequence_container_to_string(values.begin(), values.end(), fmt, loc);
452}
453
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;
458 string_t result;
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);
465 }
466 }
467 if (!result.empty()) return result;
468 return to_string(static_cast<long long>(value), string_t {'G'}, std::locale());
469}
470
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);
476}
477
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);
481}
482
483template<>
484[[nodiscard]] inline auto __format_stringer<char, bool&>(bool& value) -> std::string {
485 return xtd::to_string(value, "G", std::locale());
486}
487
488template<>
489[[nodiscard]] inline auto __format_stringer<char, xtd::sbyte&>(xtd::sbyte& value) -> std::string {
490 return xtd::to_string(value, "G", std::locale());
491}
492
493template<>
494[[nodiscard]] inline auto __format_stringer<char, const unsigned char&>(const unsigned char& value) -> std::string {
495 return xtd::to_string(value, "G", std::locale());
496}
497
498template<>
499[[nodiscard]] inline auto __format_stringer<char, unsigned char&>(unsigned char& value) -> std::string {
500 return xtd::to_string(value, "G", std::locale());
501}
502
503template<>
504[[nodiscard]] inline auto __format_stringer<char, short&>(short& value) -> std::string {
505 return xtd::to_string(value, "G", std::locale());
506}
507
508template<>
509[[nodiscard]] inline auto __format_stringer<char, unsigned short&>(unsigned short& value) -> std::string {
510 return xtd::to_string(value, "G", std::locale());
511}
512
513template<>
514[[nodiscard]] inline auto __format_stringer<char, int&>(int& value) -> std::string {
515 return xtd::to_string(value, "G", std::locale());
516}
517
518template<>
519[[nodiscard]] inline auto __format_stringer<char, unsigned int&>(unsigned int& value) -> std::string {
520 return xtd::to_string(value, "G", std::locale());
521}
522
523template<>
524[[nodiscard]] inline auto __format_stringer<char, long&>(long& value) -> std::string {
525 return xtd::to_string(value, "G", std::locale());
526}
527
528template<>
529[[nodiscard]] inline auto __format_stringer<char, unsigned long&>(unsigned long& value) -> std::string {
530 return xtd::to_string(value, "G", std::locale());
531}
532
533template<>
534[[nodiscard]] inline auto __format_stringer<char, long long&>(long long& value) -> std::string {
535 return xtd::to_string(value, "G", std::locale());
536}
537
538template<>
539[[nodiscard]] inline auto __format_stringer<char, unsigned long long&>(unsigned long long& value) -> std::string {
540 return xtd::to_string(value, "G", std::locale());
541}
542
543template<>
544[[nodiscard]] inline auto __format_stringer<char, float&>(float& value) -> std::string {
545 return xtd::to_string(value, "G", std::locale());
546}
547
548template<>
549[[nodiscard]] inline auto __format_stringer<char, double&>(double& value) -> std::string {
550 return xtd::to_string(value, "G", std::locale());
551}
552
553template<>
554[[nodiscard]] inline auto __format_stringer<char, long double&>(long double& value) -> std::string {
555 return xtd::to_string(value, "G", std::locale());
556}
557
558template<>
559[[nodiscard]] inline auto __format_stringer<char, std::chrono::system_clock::time_point&>(std::chrono::system_clock::time_point& value) -> std::string {
560 return xtd::to_string(value, "G", std::locale());
561}
562
563template<>
564[[nodiscard]] inline auto __format_stringer<char, std::tm&>(tm& value) -> std::string {
565 return xtd::to_string(value, "G", std::locale());
566}
567
568template<>
569[[nodiscard]] inline auto __format_stringer<char, xtd::wchar&>(xtd::wchar& value) -> std::string {
570 return xtd::to_string(value, "G", std::locale());
571}
572
573template<>
574[[nodiscard]] inline auto __format_stringer<char, xtd::char8&>(xtd::char8& value) -> std::string {
575 return xtd::to_string(value, "G", std::locale());
576}
577
578template<>
579[[nodiscard]] inline auto __format_stringer<char, xtd::char16&>(xtd::char16& value) -> std::string {
580 return xtd::to_string(value, "G", std::locale());
581}
582
583template<>
584[[nodiscard]] inline auto __format_stringer<char, xtd::char32&>(xtd::char32& value) -> std::string {
585 return xtd::to_string(value, "G", std::locale());
586}
587
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());
591}
592
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>& {
596 //return os << value;
597 return os << xtd::to_string(value, std::basic_string < char_t > {}, std::locale {});
598 }
599};
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
auto format(const xtd::string &fmt, args_t &&... args) -> xtd::string
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition format.hpp:21
@ 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.