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