xtd 0.2.0
Loading...
Searching...
No Matches
__format_stringer.hpp
Go to the documentation of this file.
1
3#pragma once
4
5#define __XTD_STD_INTERNAL__
7#undef __XTD_STD_INTERNAL__
10#include "../any.hpp"
11#include "../chrono.hpp"
12#include "../optional.hpp"
13#include "../types.hpp"
14#include <algorithm>
15#include <array>
16#include <cctype>
17#include <deque>
18#include <forward_list>
19#include <functional>
20#include <iomanip>
21#include <iostream>
22#include <locale>
23#include <list>
24#include <map>
25#include <set>
26#include <sstream>
27#include <stdexcept>
28#include <string>
29#include <system_error>
30#include <tuple>
31#include <type_traits>
32#include <typeindex>
33#include <unordered_map>
34#include <unordered_set>
35#include <utility>
36#include <valarray>
37#include <vector>
38
40namespace xtd {
41 class iformatable;
42 class object;
44 namespace globalization {
45 class culture_info;
46 }
47}
48
49template<typename char_t>
50[[nodiscard]] inline auto __codepoint_to_string(xtd::char32 codepoint) -> std::basic_string<char_t> {
51 std::basic_string<char_t> result;
52 if (codepoint < 0x80)
53 result.push_back(static_cast<char_t>(codepoint));
54 else if (codepoint < 0x800) {
55 result.push_back(static_cast<char_t>((codepoint >> 6) | 0xc0));
56 result.push_back(static_cast<char_t>((codepoint & 0x3f) | 0x80));
57 } else if (codepoint < 0x10000) {
58 result.push_back(static_cast<char_t>((codepoint >> 12) | 0xe0));
59 result.push_back(static_cast<char_t>(((codepoint >> 6) & 0x3f) | 0x80));
60 result.push_back(static_cast<char_t>((codepoint & 0x3f) | 0x80));
61 } else {
62 result.push_back(static_cast<char_t>((codepoint >> 18) | 0xf0));
63 result.push_back(static_cast<char_t>(((codepoint >> 12) & 0x3f) | 0x80));
64 result.push_back(static_cast<char_t>(((codepoint >> 6) & 0x3f) | 0x80));
65 result.push_back(static_cast<char_t>((codepoint & 0x3f) | 0x80));
66 }
67 return result;
68}
69
70template<typename char_t>
71[[nodiscard]] inline auto __to_string(char codepoint) -> std::basic_string<char_t> {
72 return __codepoint_to_string<char_t>(codepoint);
73}
74
75template<typename char_t>
76[[nodiscard]] inline auto __to_string(xtd::char32 codepoint) -> std::basic_string<char_t> {
77 return __codepoint_to_string<char_t>(codepoint);
78}
79
80template<typename char_t>
81[[nodiscard]] inline auto __to_string(xtd::char16 codepoint) -> std::basic_string<char_t> {
82 return __codepoint_to_string<char_t>(codepoint);
83}
84
85template<typename char_t>
86[[nodiscard]] inline auto __to_string(xtd::char8 codepoint) -> std::basic_string<char_t> {
87 return __codepoint_to_string<char_t>(codepoint);
88}
89
90template<typename char_t>
91[[nodiscard]] inline auto __to_string(xtd::wchar codepoint) -> std::basic_string<char_t> {
92 return __codepoint_to_string<char_t>(codepoint);
93}
94
95template<typename char_t>
96[[nodiscard]] inline auto __to_string(const std::basic_string<char_t>& str) -> std::basic_string<char_t> {
97 std::basic_string<char_t> result;
98 std::for_each(str.begin(), str.end(), [&](auto codepoint) {result += __to_string<char_t>(codepoint);});
99 return result;
100}
101
102template<typename char_t, typename arg_t>
103[[nodiscard]] inline auto __to_string(const std::basic_string<arg_t>& str) -> std::basic_string<char_t> {
104 std::basic_string<char_t> result;
105 for (auto codepoint : str)
106 result += __to_string<char_t>(codepoint);
107 return result;
108}
109
110template<typename char_t, typename arg_t>
111[[nodiscard]] inline auto __to_string(const arg_t* str) -> std::basic_string<char_t> {
112 std::basic_string<char_t> result;
113 for (auto codepoint : std::basic_string<arg_t>(str))
114 result += __to_string<char_t>(codepoint);
115 return result;
116}
117
118auto operator <<(std::ostream& os, const xtd::char8* str) -> std::ostream&;
119auto operator <<(std::ostream& os, const xtd::char16* str) -> std::ostream&;
120auto operator <<(std::ostream& os, const xtd::char32* str) -> std::ostream&;
121auto operator <<(std::ostream& os, const xtd::wchar* str) -> std::ostream&;
122
123template<typename enum_t>
124[[nodiscard]] auto __enum_to_string__(enum_t value) noexcept -> std::string;
125[[nodiscard]] auto __iformatable_to_string(const xtd::iformatable& value) noexcept -> std::string;
126[[nodiscard]] auto __object_to_string__(const xtd::object& value) noexcept -> std::string;
127
128template<typename char_t, typename type_t, typename bool_t>
129struct __enum_ostream__ {};
130
131template < class char_t, typename type_t >
132struct __enum_ostream__ < char_t, type_t, std::true_type > {
133 auto to_stream(std::basic_ostream<char_t>& os, const type_t& value) noexcept -> std::basic_ostream<char_t>& {
134 return os << __enum_to_string__(value);
135 }
136};
137
138template < class char_t, typename type_t, typename bool_t >
139struct __enum_or_polymorphic_ostream__ {};
140
141template < class char_t, typename type_t >
142struct __enum_or_polymorphic_ostream__ < char_t, type_t, std::true_type > {
143 auto to_stream(std::basic_ostream<char_t>& os, const type_t& value) noexcept -> std::basic_ostream <char_t>& {
144 if (dynamic_cast < const xtd::iformatable* > (&value)) return os << __iformatable_to_string(dynamic_cast<const xtd::iformatable&>(value));
145 if (dynamic_cast < const xtd::object* > (&value)) return os << __object_to_string__(dynamic_cast<const xtd::object&>(value));
146 return os << value;
147 }
148};
149
150template < class char_t, typename type_t >
151struct __enum_or_polymorphic_ostream__ < char_t, type_t, std::false_type > {
152 auto to_stream(std::basic_ostream<char_t>& os, const type_t& value) noexcept -> std::basic_ostream<char_t>& {
153 __enum_ostream__ < char, type_t, typename std::is_enum < type_t>::type > ().to_stream(os, value);
154 return os;
155 }
156};
157
158template < class value_t >
159[[nodiscard]] auto __format_stringer_to_std_string(const value_t& value) -> std::string {
160 std::basic_stringstream<char> ss;
161 //ss << value;
162 __enum_or_polymorphic_ostream__ < char, value_t, typename std::is_polymorphic < value_t>::type > ().to_stream(ss, value);
163 return ss.str();
164}
165
166[[nodiscard]] auto __format_stringer_to_std_string(const char& c) -> std::string;
167[[nodiscard]] auto __format_stringer_to_std_string(const xtd::char8& c) -> std::string;
168[[nodiscard]] auto __format_stringer_to_std_string(const xtd::char16& c) -> std::string;
169[[nodiscard]] auto __format_stringer_to_std_string(const xtd::char32& c) -> std::string;
170[[nodiscard]] auto __format_stringer_to_std_string(const xtd::wchar& c) -> std::string;
171[[nodiscard]] auto __format_stringer_to_std_string(const char* str) -> std::string;
172[[nodiscard]] auto __format_stringer_to_std_string(const xtd::char8* str) -> std::string;
173[[nodiscard]] auto __format_stringer_to_std_string(const xtd::char16* str) -> std::string;
174[[nodiscard]] auto __format_stringer_to_std_string(const xtd::char32* str) -> std::string;
175[[nodiscard]] auto __format_stringer_to_std_string(const xtd::wchar* str) -> std::string;
176[[nodiscard]] auto __format_stringer_to_std_string(const std::string& str) -> std::string;
177[[nodiscard]] auto __format_stringer_to_std_string(const xtd::string& str) -> std::string;
178[[nodiscard]] auto __format_stringer_to_std_string(const std::u8string& str) -> std::string;
179[[nodiscard]] auto __format_stringer_to_std_string(const std::u16string& str) -> std::string;
180[[nodiscard]] auto __format_stringer_to_std_string(const std::u32string& str) -> std::string;
181[[nodiscard]] auto __format_stringer_to_std_string(const std::wstring& str) -> std::string;
182
183template < class char_t, typename value_t >
184[[nodiscard]] inline auto __format_stringer(value_t value) -> std::basic_string<char_t> {
185 std::basic_stringstream<char_t> ss;
186 ss << __format_stringer_to_std_string(value).c_str(); // Using "c_str()" is not the best method, but it is the only possibility if "char_t" is of another type than "char".
187 return ss.str();
188}
189
190template < class char_t, typename value_t >
191[[nodiscard]] inline auto __format_stringer(const bool& value) -> std::basic_string<char_t> {
192 std::basic_stringstream<char_t> ss;
193 ss << std::boolalpha << value;
194 return ss.str();
195}
196
197template < class char_t, typename value_t, xtd::int32 len >
198[[nodiscard]] inline auto __format_stringer(const char*& value) -> std::basic_string<char_t> {
199 std::basic_stringstream<char_t> ss;
200 ss << __to_string < char_t > (value);
201 return ss.str();
202}
203
204template < class char_t, typename value_t, xtd::int32 len >
205[[nodiscard]] inline auto __format_stringer(const char* const& value) -> std::basic_string<char_t> {
206 std::basic_stringstream<char_t> ss;
207 ss << __to_string < char_t > (value);
208 return ss.str();
209}
210
211template < class char_t, typename value_t >
212[[nodiscard]] inline auto __format_stringer(const xtd::char8*& value) -> std::basic_string<char_t> {
213 auto s = std::u8string(value);
214 std::basic_stringstream<char_t> ss;
215 ss << std::basic_string<char_t> (s.begin(), s.end());
216 return ss.str();
217}
218
219template < class char_t, typename value_t >
220[[nodiscard]] inline auto __format_stringer(const xtd::char8* const& value) -> std::basic_string<char_t> {
221 auto s = std::u8string(value);
222 std::basic_stringstream<char_t> ss;
223 ss << std::basic_string<char_t> (s.begin(), s.end());
224 return ss.str();
225}
226
227template < class char_t, typename value_t >
228[[nodiscard]] inline auto __format_stringer(const xtd::char16*& value) -> std::basic_string<char_t> {
229 std::basic_stringstream<char_t> ss;
230 ss << __to_string < char_t > (value);
231 return ss.str();
232}
233
234template < class char_t, typename value_t >
235[[nodiscard]] inline auto __format_stringer(const xtd::char16* const& value) -> std::basic_string<char_t> {
236 std::basic_stringstream<char_t> ss;
237 ss << __to_string < char_t > (value);
238 return ss.str();
239}
240
241template < class char_t, typename value_t >
242[[nodiscard]] inline auto __format_stringer(const xtd::char32*& value) -> std::basic_string<char_t> {
243 std::basic_stringstream<char_t> ss;
244 ss << __to_string < char_t > (value);
245 return ss.str();
246}
247
248template < class char_t, typename value_t >
249[[nodiscard]] inline auto __format_stringer(const xtd::char32* const& value) -> std::basic_string<char_t> {
250 std::basic_stringstream<char_t> ss;
251 ss << __to_string < char_t > (value);
252 return ss.str();
253}
254
255template < class char_t, typename value_t >
256[[nodiscard]] inline auto __format_stringer(const xtd::wchar*& value) -> std::basic_string<char_t> {
257 std::basic_stringstream<char_t> ss;
258 ss << __to_string < char_t > (value);
259 return ss.str();
260}
261
262template < class char_t, typename value_t >
263[[nodiscard]] inline auto __format_stringer(const xtd::wchar* const& value) -> std::basic_string<char_t> {
264 std::basic_stringstream<char_t> ss;
265 ss << __to_string < char_t > (value);
266 return ss.str();
267}
268
269template <>
270[[nodiscard]] inline auto __format_stringer<char, std::u8string&>(std::u8string& value) -> std::string {
271 std::basic_stringstream < char > ss;
272 ss << std::string(value.begin(), value.end());
273 return ss.str();
274}
275
276template <>
277[[nodiscard]] inline auto __format_stringer<char, const std::u16string&>(const std::u16string& value) -> std::string {
278 std::basic_stringstream < char > ss;
279 ss << __to_string < char > (value);
280 return ss.str();
281}
282
283template <>
284[[nodiscard]] inline auto __format_stringer<char, const std::u32string&>(const std::u32string& value) -> std::string {
285 std::basic_stringstream < char > ss;
286 ss << __to_string < char > (value);
287 return ss.str();
288}
289
290template <>
291[[nodiscard]] inline auto __format_stringer<char, const std::wstring&>(const std::wstring& value) -> std::string {
292 std::basic_stringstream < char > ss;
293 ss << __to_string < char > (value);
294 return ss.str();
295}
296
297template <>
298[[nodiscard]] inline auto __format_stringer<char, bool&>(bool& value) -> std::string;
299
300template <>
301[[nodiscard]] inline auto __format_stringer<char, xtd::sbyte&>(xtd::sbyte& value) -> std::string;
302
303template <>
304[[nodiscard]] inline auto __format_stringer<char, const unsigned char&>(const unsigned char& value) -> std::string;
305
306template <>
307[[nodiscard]] inline auto __format_stringer<char, unsigned char&>(unsigned char& value) -> std::string;
308
309template <>
310[[nodiscard]] inline auto __format_stringer<char, short&>(short& value) -> std::string;
311
312template <>
313[[nodiscard]] inline auto __format_stringer<char, unsigned short&>(unsigned short& value) -> std::string;
314
315template <>
316[[nodiscard]] inline auto __format_stringer<char, int&>(int& value) -> std::string;
317
318template <>
319[[nodiscard]] inline auto __format_stringer<char, unsigned int&>(unsigned int& value) -> std::string;
320
321template <>
322[[nodiscard]] inline auto __format_stringer<char, long&>(long& value) -> std::string;
323
324template <>
325[[nodiscard]] inline auto __format_stringer<char, unsigned long&>(unsigned long& value) -> std::string;
326
327template <>
328[[nodiscard]] inline auto __format_stringer<char, long long&>(long long& value) -> std::string;
329
330template <>
331[[nodiscard]] inline auto __format_stringer<char, unsigned long long&>(unsigned long long& value) -> std::string;
332
333template <>
334[[nodiscard]] inline auto __format_stringer<char, float&>(float& value) -> std::string;
335
336template <>
337[[nodiscard]] inline auto __format_stringer<char, double&>(double& value) -> std::string;
338
339template <>
340[[nodiscard]] inline auto __format_stringer<char, long double&>(long double& value) -> std::string;
341
342template <>
343[[nodiscard]] inline auto __format_stringer<char, std::chrono::system_clock::time_point&>(std::chrono::system_clock::time_point& value) -> std::string;
344
345template <>
346[[nodiscard]] inline auto __format_stringer<char, std::tm&>(tm& value) -> std::string;
347
348template <>
349[[nodiscard]] inline auto __format_stringer<char, xtd::wchar&>(xtd::wchar& value) -> std::string;
350
351template <>
352[[nodiscard]] inline auto __format_stringer<char, xtd::char8&>(xtd::char8& value) -> std::string;
353
354template <>
355[[nodiscard]] inline auto __format_stringer<char, xtd::char16&>(xtd::char16& value) -> std::string;
356
357template <>
358[[nodiscard]] inline auto __format_stringer<char, xtd::char32&>(xtd::char32& value) -> std::string;
Contains xtd::any type and std::bad_any_cast exception.
Contains std::chrono::days, std::chrono::weeks, std::chrono::months and std::chrono::years duration t...
Represents text as a sequence of character units.
Definition basic_string.hpp:66
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition culture_info.hpp:43
Provides functionality to format the value of an object into a string representation.
Definition iformatable.hpp:42
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
Contains xtd::collections::generic::ienumerable <type_t> interface.
@ value
Represnets the constant operator precedence (42).
Definition operator_precedence.hpp:30
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
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
@ s
The S key.
Definition console_key.hpp:124
Contains classes that define culture-related information, including language, country/region,...
Definition culture_info.hpp:20
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Contains xtd::optional type.
Contains xtd fundamental types.