xtd 0.2.0
basic_string_.hpp
Go to the documentation of this file.
1
4#pragma once
6#if !defined(__XTD_BASIC_STRING_INTERNAL__)
7#error "Do not include this file: Internal use only. Include <xtd/basic_string> or <xtd/basic_string.hpp> instead."
8#endif
9
10#include "array.hpp"
11//#include "formatter.hpp"
12
14template<class char_t, class traits_t, class allocator_t>
15inline xtd::basic_string<char_t, traits_t, allocator_t>::basic_string(const allocator_type& allocator) noexcept : chars_(allocator) {}
16
17template<class char_t, class traits_t, class allocator_t>
19 return index_of_any(values, 0, size());
20}
21
22template<class char_t, class traits_t, class allocator_t>
24 return index_of_any(values, start_index, size() - start_index);
25}
26
27template<class char_t, class traits_t, class allocator_t>
30 auto index = xtd::size {0};
31 for (const auto& item : *this) {
32 if (index++ < start_index) continue;
33 if (index - 1 > start_index + count) break;
34 if (std::find(values.begin(), values.end(), item) != values.end()) return index - 1;
35 }
36 return npos;
37}
38
39template<class char_t, class traits_t, class allocator_t>
40inline xtd::size xtd::basic_string<char_t, traits_t, allocator_t>::index_of_any(const std::initializer_list<value_type>& values) const noexcept {
41 return index_of_any(xtd::array<value_type>(values));
42}
43
44template<class char_t, class traits_t, class allocator_t>
45inline xtd::size xtd::basic_string<char_t, traits_t, allocator_t>::index_of_any(const std::initializer_list<value_type>& values, xtd::size start_index) const {
46 return index_of_any(xtd::array<value_type>(values), start_index);
47}
48
49template<class char_t, class traits_t, class allocator_t>
50inline xtd::size xtd::basic_string<char_t, traits_t, allocator_t>::index_of_any(const std::initializer_list<value_type>& values, xtd::size start_index, xtd::size count) const {
51 return index_of_any(xtd::array<value_type>(values), start_index, count);
52}
53
54template<class char_t, class traits_t, class allocator_t>
56 return last_index_of_any(values, 0, size());
57}
58
59template<class char_t, class traits_t, class allocator_t>
61 return last_index_of_any(values, start_index, size() - start_index);
62}
63
64template<class char_t, class traits_t, class allocator_t>
67 auto index = size() - 1;
68 for (auto iterator = crbegin(); iterator != crend(); ++iterator) {
69 if (index-- > start_index + count) continue;
70 if (index + 1 < start_index) break;
71 if (std::find(values.begin(), values.end(), *iterator) != values.end()) return index + 1;
72 }
73 return npos;
74}
75
76template<class char_t, class traits_t, class allocator_t>
77inline xtd::size xtd::basic_string<char_t, traits_t, allocator_t>::last_index_of_any(const std::initializer_list<value_type>& values) const noexcept {
78 return last_index_of_any(xtd::array<value_type>(values));
79}
80
81template<class char_t, class traits_t, class allocator_t>
82inline xtd::size xtd::basic_string<char_t, traits_t, allocator_t>::last_index_of_any(const std::initializer_list<value_type>& values, xtd::size start_index) const {
83 return last_index_of_any(xtd::array<value_type>(values), start_index);
84}
85
86template<class char_t, class traits_t, class allocator_t>
87inline xtd::size xtd::basic_string<char_t, traits_t, allocator_t>::last_index_of_any(const std::initializer_list<value_type>& values, xtd::size start_index, xtd::size count) const {
88 return last_index_of_any(xtd::array<value_type>(values), start_index, count);
89}
90
91template<class char_t, class traits_t, class allocator_t>
93 return split(default_split_separators, std::numeric_limits<xtd::size>::max(), xtd::string_split_options::none);
94}
95
96template<class char_t, class traits_t, class allocator_t>
98 return split(xtd::array<value_type> {separator}, std::numeric_limits<xtd::size>::max(), xtd::string_split_options::none);
99}
100
101template<class char_t, class traits_t, class allocator_t>
103 return split(xtd::array<value_type> {separator}, std::numeric_limits<xtd::size>::max(), options);
104}
105
106template<class char_t, class traits_t, class allocator_t>
109}
110
111template<class char_t, class traits_t, class allocator_t>
113 return split(xtd::array<value_type> {separator}, count, options);
114}
115
116template<class char_t, class traits_t, class allocator_t>
118 return split(separators, std::numeric_limits<xtd::size>::max(), xtd::string_split_options::none);
119}
120
121template<class char_t, class traits_t, class allocator_t>
123 return split(separators, std::numeric_limits<xtd::size>::max(), options);
124}
125
126template<class char_t, class traits_t, class allocator_t>
128 return split(separators, count, xtd::string_split_options::none);
129}
130
131template<class char_t, class traits_t, class allocator_t>
133 if (count == 0) return {};
134 if (count == 1) return {*this};
135
136 auto result = std::vector<basic_string> {};
137 auto sub_string = basic_string {};
138 auto split_char_separators = separators.size() == 0 ? default_split_separators : separators;
139 for (auto it = begin(); it != end(); ++it) {
140 auto is_separator = std::find(split_char_separators.begin(), split_char_separators.end(), *it) != split_char_separators.end();
141 if (!is_separator) sub_string.chars_.append(basic_string(1, *it));
142 auto is_last = (it + 1 == end());
143 auto should_add = is_last || is_separator;
144 auto keep_empty = sub_string.length() > 0 || (options != xtd::string_split_options::remove_empty_entries);
145
146 if (should_add && keep_empty) {
147 if (result.size() == count - 1) {
148 result.push_back(sub_string + basic_string(c_str(), it - begin() + (is_separator ? 0 : 1), length() - (it - begin()) + (is_separator ? 0 : 1)));
149 return result;
150 }
151 result.push_back(sub_string);
152 sub_string.chars_.clear();
153 }
154 }
155
156 if (size() > 0 && std::find(split_char_separators.begin(), split_char_separators.end(), at(size() - 1)) != split_char_separators.end() && options != xtd::string_split_options::remove_empty_entries) result.push_back(basic_string {});
157
158 return result;
159}
160
161template<class char_t, class traits_t, class allocator_t>
163 return to_array(0, size());
164}
165
166template<class char_t, class traits_t, class allocator_t>
168 return to_array(start_index, size() - start_index);
169}
170
171template<class char_t, class traits_t, class allocator_t>
173 if (start_index >= size()) return {};
174 if (start_index + length >= size()) return {begin() + start_index, end()};
175 return {begin() + start_index, begin() + start_index + length};
176}
177
178template<class char_t, class traits_t, class allocator_t>
180 return to_array(0, size());
181}
182
183template<class char_t, class traits_t, class allocator_t>
185 return to_array(start_index, length);
186}
187
188template<class char_t, class traits_t, class allocator_t>
190 auto words = split({' '});
191 for (auto& word : words)
192 if (word.size() && word != word.to_upper()) word = static_cast<value_type>(toupper(word[0])) + word.substring(1).to_lower();
193 return basic_string::join(" ", words);
194}
195
196template<class char_t, class traits_t, class allocator_t>
198 return trim(xtd::array<value_type> {trim_char});}
199
200template<class char_t, class traits_t, class allocator_t>
202 return trim_start(trim_chars).trim_end(trim_chars);
203}
204
205template<class char_t, class traits_t, class allocator_t>
207 return trim_end(xtd::array<value_type> {trim_char});
208}
209
210template<class char_t, class traits_t, class allocator_t>
212 if (!size()) return *this;
213 auto result = chars_;
214 while (std::find(trim_chars.begin(), trim_chars.end(), result[result.size() - 1]) != trim_chars.end())
215 result.erase(result.size() - 1, 1);
216 return result;
217}
218
219template<class char_t, class traits_t, class allocator_t>
221
222template<class char_t, class traits_t, class allocator_t>
224 if (!size()) return *this;
225 auto result = chars_;
226 while (std::find(trim_chars.begin(), trim_chars.end(), result[0]) != trim_chars.end())
227 result.erase(0, 1);
228 return result;
229}
230
231template<class char_t, class traits_t, class allocator_t>
233 auto result = basic_string::empty_string;
234 std::for_each(values.begin(), values.end(), [&](const auto & item) {result += item;});
235 return result;
236}
237
238template<class char_t, class traits_t, class allocator_t>
240 auto result = basic_string::empty_string;
241 std::for_each(values.begin(), values.end(), [&](const auto & item) {result += item;});
242 return result;
243}
244
245template<class char_t, class traits_t, class allocator_t>
246template<class other_char_t>
248 auto result = basic_string::empty_string;
249 std::for_each(values.begin(), values.end(), [&](const auto & item) {result += item;});
250 return result;
251}
252
253
254template<class char_t, class traits_t, class allocator_t>
255template<class object_t>
257 basic_string result;
258 for (const auto& arg : args)
259 result += format("{}", arg);
260 return result;
261}
262
263template<class char_t, class traits_t, class allocator_t>
264template<class ...args_t>
266 auto result = basic_string<char> {};
267 auto index = xtd::size {0};
268 auto formats = std::vector<__format_information<char>> {};
269 auto begin_format_iterator = fmt.end();
270 auto end_format_iterator = fmt.end();
271 for (auto iterator = fmt.begin(); iterator != fmt.end(); ++iterator) {
272 if (*iterator == '{') {
273 if (++iterator == fmt.end())
275 if (*iterator == '{')
276 result += *iterator;
277 else {
278 begin_format_iterator = iterator;
279 while (iterator != fmt.end() && *iterator != '}') ++iterator;
280 if (iterator == fmt.end())
282 end_format_iterator = iterator;
283 __format_information<char> fi;
284 fi.location = result.size();
285 auto format_str = std::basic_string<char> {begin_format_iterator, end_format_iterator};
286 if (format_str.size() == 0)
287 fi.index = index++;
288 else {
289 xtd::size index_alignment_separator = basic_string(format_str).index_of(',');
290 xtd::size index_format_separator = basic_string(format_str).index_of(u':');
291
292 if (index_alignment_separator != std::basic_string<char>::npos && index_format_separator != std::basic_string<char>::npos && index_alignment_separator > index_format_separator)
293 index_alignment_separator = std::basic_string<char>::npos;
294
295 if (index_alignment_separator != basic_string<char_t>::npos)
296 fi.alignment = format_str.substr(index_alignment_separator + 1, index_format_separator != std::basic_string<char>::npos ? index_format_separator - index_alignment_separator - 1 : std::basic_string<char>::npos);
297
298 if (index_format_separator != basic_string<char>::npos)
299 fi.format = format_str.substr(index_format_separator + 1);
300
301 if (index_alignment_separator == 0 || index_format_separator == 0)
302 fi.index = index++;
303 else {
304 auto index_str = std::basic_string<char> {};
305 if (index_alignment_separator != basic_string<char>::npos)
306 index_str = format_str.substr(0, index_alignment_separator);
307 else if (index_format_separator != basic_string<char>::npos)
308 index_str = format_str.substr(0, index_format_separator);
309 else
310 index_str = std::move(format_str);
311 try {
312 for (auto c : index_str)
314 fi.index = std::stoi(index_str);
315 } catch (...) {
317 }
318 }
319 }
320 formats.push_back(fi);
321 }
322 } else if (*iterator == '}') {
323 if (++iterator == fmt.end())
325 if (*iterator != '}')
327 result += *iterator;
328 } else
329 result += *iterator;
330 }
331
332 __basic_string_extract_format_arg(result, formats, std::forward<args_t>(args)...);
333 return result.c_str();
334}
335
336template<class char_t, class traits_t, class allocator_t>
337template<class value_t>
338inline xtd::basic_string<char_t, traits_t, allocator_t> xtd::basic_string<char_t, traits_t, allocator_t>::join(const basic_string& separator, const std::initializer_list<value_t>& values) noexcept {
339 return join(separator, xtd::array<value_t>(values));
340}
341
342template<class char_t, class traits_t, class allocator_t>
343template<class value_t>
344inline xtd::basic_string<char_t, traits_t, allocator_t> xtd::basic_string<char_t, traits_t, allocator_t>::join(const basic_string& separator, const std::initializer_list<value_t>& values, xtd::size index) {
345 return join(separator, xtd::array<value_t>(values), index);
346}
347
348template<class char_t, class traits_t, class allocator_t>
349template<class value_t>
350inline xtd::basic_string<char_t, traits_t, allocator_t> xtd::basic_string<char_t, traits_t, allocator_t>::join(const basic_string& separator, const std::initializer_list<value_t>& values, xtd::size index, xtd::size count) {
351 return join(separator, xtd::array<value_t>(values), index, count);
352}
353
354template<class char_t, class traits_t, class allocator_t>
356
357template<class char_t, class traits_t, class allocator_t>
358inline const xtd::array<typename xtd::basic_string<char_t, traits_t, allocator_t>::value_type> xtd::basic_string<char_t, traits_t, allocator_t>::default_split_separators = {9, 10, 11, 12, 13, 32};
359
360template<class char_t, class traits_t, class allocator_t>
361inline const xtd::array<typename xtd::basic_string<char_t, traits_t, allocator_t>::value_type> xtd::basic_string<char_t, traits_t, allocator_t>::default_trim_chars = {9, 10, 11, 12, 13, 32};
362
363template<class arg_t>
364void __basic_string_extract_format_arg(std::basic_string<char>& fmt, xtd::size& index, std::vector<__format_information<char>>& formats, arg_t&& arg) {
365//void __basic_string_extract_format_arg(xtd::basic_string<char>& fmt, xtd::size& index, std::vector<__format_information<char>>& formats, arg_t&& arg) {
366 auto offset = xtd::size {0};
367 for (auto& format : formats) {
368 format.location += offset;
369 if (format.index == index) {
370 //xtd::basic_string<char> arg_str = xtd::formatter<arg_t> {}(arg, format.format, std::locale());
371 xtd::basic_string<char> arg_str = xtd::to_string(arg, format.format);
372
373 if (!format.alignment.empty()) {
374 xtd::int32 alignment = 0;
375 try {
376 alignment = std::stoi(format.alignment);
377 } catch (...) {
379 }
380 if (alignment > 0) arg_str = arg_str.pad_left(alignment);
381 else if (alignment < 0) arg_str = arg_str.pad_right(-alignment);
382 }
383 fmt.insert(format.location, arg_str);
384 offset += arg_str.size();
385 }
386 }
387 ++index;
388}
389
390template<class ...args_t>
391void __basic_string_extract_format_arg(xtd::basic_string<char>& fmt, std::vector<__format_information<char>>& formats, args_t&&... args) {
392 auto index = xtd::size {0};
393 (__basic_string_extract_format_arg(const_cast<std::basic_string<char>&>(fmt.chars()), index, formats, args), ...);
394 //(__basic_string_extract_format_arg(fmt, index, formats, args), ...);
395 unused_(index); // workaround to mute gcc warning: unused-but-set-variable
396}
397
398template<class target_t, class source_t>
399inline std::basic_string<target_t> __xtd_convert_to_string(std::basic_string<source_t>&& str) noexcept {
400 auto out = std::basic_string<target_t> {};
401 auto codepoint = 0u;
402 for (const auto& character : str) {
403 if (character >= 0xd800 && character <= 0xdbff)
404 codepoint = ((character - 0xd800) << 10) + 0x10000;
405 else {
406 if (character >= 0xdc00 && character <= 0xdfff) codepoint |= character - 0xdc00;
407 else codepoint = character;
408
409 if (codepoint <= 0x7f)
410 out.append(1, static_cast<target_t>(codepoint));
411 else if (codepoint <= 0x7ff) {
412 out.append(1, static_cast<target_t>(0xc0 | ((codepoint >> 6) & 0x1f)));
413 out.append(1, static_cast<target_t>(0x80 | (codepoint & 0x3f)));
414 } else if (codepoint <= 0xffff) {
415 out.append(1, static_cast<target_t>(0xe0 | ((codepoint >> 12) & 0x0f)));
416 out.append(1, static_cast<target_t>(0x80 | ((codepoint >> 6) & 0x3f)));
417 out.append(1, static_cast<target_t>(0x80 | (codepoint & 0x3f)));
418 } else {
419 out.append(1, static_cast<target_t>(0xf0 | ((codepoint >> 18) & 0x07)));
420 out.append(1, static_cast<target_t>(0x80 | ((codepoint >> 12) & 0x3f)));
421 out.append(1, static_cast<target_t>(0x80 | ((codepoint >> 6) & 0x3f)));
422 out.append(1, static_cast<target_t>(0x80 | (codepoint & 0x3f)));
423 }
424 codepoint = 0;
425 }
426 }
427 return out;
428}
429
430template<>
431inline std::basic_string<xtd::char16> __xtd_convert_to_string<xtd::char16, char>(std::basic_string<char>&& str) noexcept {
432 auto out = std::basic_string<xtd::char16> {};
433 auto codepoint = 0u;
434 auto str_ptr = str.data();
435 while (*str_ptr != 0) {
436 auto ch = static_cast<unsigned char>(*str_ptr);
437 if (ch <= 0x7f) codepoint = ch;
438 else if (ch <= 0xbf) codepoint = (codepoint << 6) | (ch & 0x3f);
439 else if (ch <= 0xdf) codepoint = ch & 0x1f;
440 else if (ch <= 0xef) codepoint = ch & 0x0f;
441 else codepoint = ch & 0x07;
442 ++str_ptr;
443 if (((*str_ptr & 0xc0) != 0x80) && (codepoint <= 0x10ffff)) {
444 if (codepoint > 0xffff) {
445 out.append(1, static_cast<xtd::char16>(0xd800 + (static_cast<xtd::char16>(codepoint) >> 10)));
446 out.append(1, static_cast<xtd::char16>(0xdc00 + (static_cast<xtd::char16>(codepoint) & 0x03ff)));
447 } else if (codepoint < 0xd800 || codepoint >= 0xe000)
448 out.append(1, static_cast<xtd::char16>(codepoint));
449 }
450 }
451 return out;
452}
453
454template<>
455inline std::basic_string<xtd::char16> __xtd_convert_to_string<xtd::char16, xtd::char8>(std::basic_string<xtd::char8>&& str) noexcept {
456 auto out = std::basic_string<xtd::char16> {};
457 auto codepoint = 0u;
458 auto str_ptr = str.data();
459 while (*str_ptr != 0) {
460 auto ch = static_cast<unsigned char>(*str_ptr);
461 if (ch <= 0x7f) codepoint = ch;
462 else if (ch <= 0xbf) codepoint = (codepoint << 6) | (ch & 0x3f);
463 else if (ch <= 0xdf) codepoint = ch & 0x1f;
464 else if (ch <= 0xef) codepoint = ch & 0x0f;
465 else codepoint = ch & 0x07;
466 ++str_ptr;
467 if (((*str_ptr & 0xc0) != 0x80) && (codepoint <= 0x10ffff)) {
468 if (codepoint > 0xffff) {
469 out.append(1, static_cast<xtd::char16>(0xd800 + (static_cast<xtd::char16>(codepoint) >> 10)));
470 out.append(1, static_cast<xtd::char16>(0xdc00 + (static_cast<xtd::char16>(codepoint) & 0x03ff)));
471 } else if (codepoint < 0xd800 || codepoint >= 0xe000)
472 out.append(1, static_cast<xtd::char16>(codepoint));
473 }
474 }
475 return out;
476}
477
478template<>
479inline std::basic_string<xtd::wchar> __xtd_convert_to_string<xtd::wchar, char>(std::basic_string<char>&& str) noexcept {
480 auto out = std::basic_string<xtd::wchar> {};
481 auto codepoint = 0u;
482 auto str_ptr = str.data();
483 while (*str_ptr != 0) {
484 auto ch = static_cast<unsigned char>(*str_ptr);
485 if (ch <= 0x7f) codepoint = ch;
486 else if (ch <= 0xbf) codepoint = (codepoint << 6) | (ch & 0x3f);
487 else if (ch <= 0xdf) codepoint = ch & 0x1f;
488 else if (ch <= 0xef) codepoint = ch & 0x0f;
489 else codepoint = ch & 0x07;
490 ++str_ptr;
491 if (((*str_ptr & 0xc0) != 0x80) && (codepoint <= 0x10ffff)) {
492 if (sizeof(xtd::wchar) > 2)
493 out.append(1, static_cast<xtd::wchar>(codepoint));
494 else if (codepoint > 0xffff) {
495 out.append(1, static_cast<xtd::wchar>(0xd800 + (static_cast<xtd::wchar>(codepoint) >> 10)));
496 out.append(1, static_cast<xtd::wchar>(0xdc00 + (static_cast<xtd::wchar>(codepoint) & 0x03ff)));
497 } else if (codepoint < 0xd800 || codepoint >= 0xe000)
498 out.append(1, static_cast<xtd::wchar>(codepoint));
499 }
500 }
501 return out;
502}
503
504template<>
505inline std::basic_string<xtd::wchar> __xtd_convert_to_string<xtd::wchar, xtd::char8>(std::basic_string<xtd::char8>&& str) noexcept {
506 auto out = std::basic_string<xtd::wchar> {};
507 auto codepoint = 0u;
508 auto str_ptr = str.data();
509 while (*str_ptr != 0) {
510 auto ch = static_cast<unsigned char>(*str_ptr);
511 if (ch <= 0x7f) codepoint = ch;
512 else if (ch <= 0xbf) codepoint = (codepoint << 6) | (ch & 0x3f);
513 else if (ch <= 0xdf) codepoint = ch & 0x1f;
514 else if (ch <= 0xef) codepoint = ch & 0x0f;
515 else codepoint = ch & 0x07;
516 ++str_ptr;
517 if (((*str_ptr & 0xc0) != 0x80) && (codepoint <= 0x10ffff)) {
518 if (sizeof(xtd::wchar) > 2)
519 out.append(1, static_cast<xtd::wchar>(codepoint));
520 else if (codepoint > 0xffff) {
521 out.append(1, static_cast<xtd::wchar>(0xd800 + (static_cast<xtd::wchar>(codepoint) >> 10)));
522 out.append(1, static_cast<xtd::wchar>(0xdc00 + (static_cast<xtd::wchar>(codepoint) & 0x03ff)));
523 } else if (codepoint < 0xd800 || codepoint >= 0xe000)
524 out.append(1, static_cast<xtd::wchar>(codepoint));
525 }
526 }
527 return out;
528}
529
530template<>
531inline std::basic_string<xtd::char32> __xtd_convert_to_string<xtd::char32, char>(std::basic_string<char>&& str) noexcept {
532 auto out = std::basic_string<xtd::char32> {};
533 auto codepoint = 0u;
534 auto str_ptr = str.data();
535 while (*str_ptr != 0) {
536 auto ch = static_cast<unsigned char>(*str_ptr);
537 if (ch <= 0x7f) codepoint = ch;
538 else if (ch <= 0xbf) codepoint = (codepoint << 6) | (ch & 0x3f);
539 else if (ch <= 0xdf) codepoint = ch & 0x1f;
540 else if (ch <= 0xef) codepoint = ch & 0x0f;
541 else codepoint = ch & 0x07;
542 ++str_ptr;
543 if (((*str_ptr & 0xc0) != 0x80) && (codepoint <= 0x10ffff))
544 out.append(1, static_cast<xtd::char32>(codepoint));
545 }
546 return out;
547}
548
549template<>
550inline std::basic_string<xtd::char32> __xtd_convert_to_string<xtd::char32, xtd::char8>(std::basic_string<xtd::char8>&& str) noexcept {
551 auto out = std::basic_string<xtd::char32> {};
552 auto codepoint = 0u;
553 auto str_ptr = str.data();
554 while (*str_ptr != 0) {
555 auto ch = static_cast<unsigned char>(*str_ptr);
556 if (ch <= 0x7f) codepoint = ch;
557 else if (ch <= 0xbf) codepoint = (codepoint << 6) | (ch & 0x3f);
558 else if (ch <= 0xdf) codepoint = ch & 0x1f;
559 else if (ch <= 0xef) codepoint = ch & 0x0f;
560 else codepoint = ch & 0x07;
561 ++str_ptr;
562 if (((*str_ptr & 0xc0) != 0x80) && (codepoint <= 0x10ffff))
563 out.append(1, static_cast<xtd::char32>(codepoint));
564 }
565 return out;
566}
567
568template<>
569inline std::basic_string<char> __xtd_convert_to_string<char, char>(std::basic_string<char>&& str) noexcept {
570 return std::move(str);
571}
572
573template<>
574inline std::basic_string<xtd::char16> __xtd_convert_to_string<xtd::char16, xtd::char16>(std::basic_string<xtd::char16>&& str) noexcept {
575 return std::move(str);
576}
577
578template<>
579inline std::basic_string<xtd::char32> __xtd_convert_to_string<xtd::char32, xtd::char32>(std::basic_string<xtd::char32>&& str) noexcept {
580 return std::move(str);
581}
582
583template<>
584inline std::basic_string<xtd::char8> __xtd_convert_to_string<xtd::char8, xtd::char8>(std::basic_string<xtd::char8>&& str) noexcept {
585 return std::move(str);
586}
587
588template<>
589inline std::basic_string<xtd::wchar> __xtd_convert_to_string<xtd::wchar, xtd::wchar>(std::basic_string<xtd::wchar>&& str) noexcept {
590 return std::move(str);
591}
592
593template<>
594inline std::basic_string<xtd::char8> __xtd_convert_to_string<xtd::char8, char>(std::basic_string<char>&& str) noexcept {
595 return std::basic_string<xtd::char8> {reinterpret_cast<const xtd::char8*>(str.c_str())};
596}
597
598template<>
599inline std::basic_string<char> __xtd_convert_to_string<char, xtd::char8>(std::basic_string<xtd::char8>&& str) noexcept {
600 return std::basic_string<char> {reinterpret_cast<const char*>(str.c_str())};
601}
602
603template<>
604inline std::basic_string<xtd::char16> __xtd_convert_to_string<xtd::char16, xtd::char32>(std::basic_string<xtd::char32>&& str) noexcept {
605 return __xtd_convert_to_string<xtd::char16>(__xtd_convert_to_string<char>(std::move(str)));
606}
607
608template<>
609inline std::basic_string<xtd::char16> __xtd_convert_to_string<xtd::char16, xtd::wchar>(std::basic_string<xtd::wchar>&& str) noexcept {
610 return __xtd_convert_to_string<xtd::char16>(__xtd_convert_to_string<char>(std::move(str)));
611}
612
613template<>
614inline std::basic_string<xtd::char32> __xtd_convert_to_string<xtd::char32, xtd::char16>(std::basic_string<xtd::char16>&& str) noexcept {
615 return __xtd_convert_to_string<xtd::char32>(__xtd_convert_to_string<char>(std::move(str)));
616}
617
618template<>
619inline std::basic_string<xtd::char32> __xtd_convert_to_string<xtd::char32, xtd::wchar>(std::basic_string<xtd::wchar>&& str) noexcept {
620 return __xtd_convert_to_string<xtd::char32>(__xtd_convert_to_string<char>(std::move(str)));
621}
622
623template<>
624inline std::basic_string<xtd::wchar> __xtd_convert_to_string<xtd::wchar, xtd::char16>(std::basic_string<xtd::char16>&& str) noexcept {
625 return __xtd_convert_to_string<xtd::wchar>(__xtd_convert_to_string<char>(std::move(str)));}
626
627template<>
628inline std::basic_string<xtd::wchar> __xtd_convert_to_string<xtd::wchar, xtd::char32>(std::basic_string<xtd::char32>&& str) noexcept {
629 return __xtd_convert_to_string<xtd::wchar>(__xtd_convert_to_string<char>(std::move(str)));
630}
631
632template<class target_t, class source_t>
633inline std::basic_string<target_t> __xtd_convert_to_string(const std::basic_string<source_t>& str) noexcept {
634 auto out = std::basic_string<target_t> {};
635 auto codepoint = 0u;
636 for (const auto& character : str) {
637 if (character >= 0xd800 && character <= 0xdbff)
638 codepoint = ((character - 0xd800) << 10) + 0x10000;
639 else {
640 if (character >= 0xdc00 && character <= 0xdfff) codepoint |= character - 0xdc00;
641 else codepoint = character;
642
643 if (codepoint <= 0x7f)
644 out.append(1, static_cast<target_t>(codepoint));
645 else if (codepoint <= 0x7ff) {
646 out.append(1, static_cast<target_t>(0xc0 | ((codepoint >> 6) & 0x1f)));
647 out.append(1, static_cast<target_t>(0x80 | (codepoint & 0x3f)));
648 } else if (codepoint <= 0xffff) {
649 out.append(1, static_cast<target_t>(0xe0 | ((codepoint >> 12) & 0x0f)));
650 out.append(1, static_cast<target_t>(0x80 | ((codepoint >> 6) & 0x3f)));
651 out.append(1, static_cast<target_t>(0x80 | (codepoint & 0x3f)));
652 } else {
653 out.append(1, static_cast<target_t>(0xf0 | ((codepoint >> 18) & 0x07)));
654 out.append(1, static_cast<target_t>(0x80 | ((codepoint >> 12) & 0x3f)));
655 out.append(1, static_cast<target_t>(0x80 | ((codepoint >> 6) & 0x3f)));
656 out.append(1, static_cast<target_t>(0x80 | (codepoint & 0x3f)));
657 }
658 codepoint = 0;
659 }
660 }
661 return out;
662}
663
664template<>
665inline std::basic_string<xtd::char16> __xtd_convert_to_string<xtd::char16, char>(const std::basic_string<char>& str) noexcept {
666 auto out = std::basic_string<xtd::char16> {};
667 auto codepoint = 0u;
668 auto str_ptr = str.data();
669 while (*str_ptr != 0) {
670 auto ch = static_cast<unsigned char>(*str_ptr);
671 if (ch <= 0x7f) codepoint = ch;
672 else if (ch <= 0xbf) codepoint = (codepoint << 6) | (ch & 0x3f);
673 else if (ch <= 0xdf) codepoint = ch & 0x1f;
674 else if (ch <= 0xef) codepoint = ch & 0x0f;
675 else codepoint = ch & 0x07;
676 ++str_ptr;
677 if (((*str_ptr & 0xc0) != 0x80) && (codepoint <= 0x10ffff)) {
678 if (codepoint > 0xffff) {
679 out.append(1, static_cast<xtd::char16>(0xd800 + (static_cast<xtd::char16>(codepoint) >> 10)));
680 out.append(1, static_cast<xtd::char16>(0xdc00 + (static_cast<xtd::char16>(codepoint) & 0x03ff)));
681 } else if (codepoint < 0xd800 || codepoint >= 0xe000)
682 out.append(1, static_cast<xtd::char16>(codepoint));
683 }
684 }
685 return out;
686}
687
688template<>
689inline std::basic_string<xtd::char16> __xtd_convert_to_string<xtd::char16, xtd::char8>(const std::basic_string<xtd::char8>& str) noexcept {
690 auto out = std::basic_string<xtd::char16> {};
691 auto codepoint = 0u;
692 auto str_ptr = str.data();
693 while (*str_ptr != 0) {
694 auto ch = static_cast<unsigned char>(*str_ptr);
695 if (ch <= 0x7f) codepoint = ch;
696 else if (ch <= 0xbf) codepoint = (codepoint << 6) | (ch & 0x3f);
697 else if (ch <= 0xdf) codepoint = ch & 0x1f;
698 else if (ch <= 0xef) codepoint = ch & 0x0f;
699 else codepoint = ch & 0x07;
700 ++str_ptr;
701 if (((*str_ptr & 0xc0) != 0x80) && (codepoint <= 0x10ffff)) {
702 if (codepoint > 0xffff) {
703 out.append(1, static_cast<xtd::char16>(0xd800 + (static_cast<xtd::char16>(codepoint) >> 10)));
704 out.append(1, static_cast<xtd::char16>(0xdc00 + (static_cast<xtd::char16>(codepoint) & 0x03ff)));
705 } else if (codepoint < 0xd800 || codepoint >= 0xe000)
706 out.append(1, static_cast<xtd::char16>(codepoint));
707 }
708 }
709 return out;
710}
711
712template<>
713inline std::basic_string<xtd::wchar> __xtd_convert_to_string<xtd::wchar, char>(const std::basic_string<char>& str) noexcept {
714 auto out = std::basic_string<xtd::wchar> {};
715 auto codepoint = 0u;
716 auto str_ptr = str.data();
717 while (*str_ptr != 0) {
718 auto ch = static_cast<unsigned char>(*str_ptr);
719 if (ch <= 0x7f) codepoint = ch;
720 else if (ch <= 0xbf) codepoint = (codepoint << 6) | (ch & 0x3f);
721 else if (ch <= 0xdf) codepoint = ch & 0x1f;
722 else if (ch <= 0xef) codepoint = ch & 0x0f;
723 else codepoint = ch & 0x07;
724 ++str_ptr;
725 if (((*str_ptr & 0xc0) != 0x80) && (codepoint <= 0x10ffff)) {
726 if (sizeof(xtd::wchar) > 2)
727 out.append(1, static_cast<xtd::wchar>(codepoint));
728 else if (codepoint > 0xffff) {
729 out.append(1, static_cast<xtd::wchar>(0xd800 + (static_cast<xtd::wchar>(codepoint) >> 10)));
730 out.append(1, static_cast<xtd::wchar>(0xdc00 + (static_cast<xtd::wchar>(codepoint) & 0x03ff)));
731 } else if (codepoint < 0xd800 || codepoint >= 0xe000)
732 out.append(1, static_cast<xtd::wchar>(codepoint));
733 }
734 }
735 return out;
736}
737
738template<>
739inline std::basic_string<xtd::wchar> __xtd_convert_to_string<xtd::wchar, xtd::char8>(const std::basic_string<xtd::char8>& str) noexcept {
740 auto out = std::basic_string<xtd::wchar> {};
741 auto codepoint = 0u;
742 auto str_ptr = str.data();
743 while (*str_ptr != 0) {
744 auto ch = static_cast<unsigned char>(*str_ptr);
745 if (ch <= 0x7f) codepoint = ch;
746 else if (ch <= 0xbf) codepoint = (codepoint << 6) | (ch & 0x3f);
747 else if (ch <= 0xdf) codepoint = ch & 0x1f;
748 else if (ch <= 0xef) codepoint = ch & 0x0f;
749 else codepoint = ch & 0x07;
750 ++str_ptr;
751 if (((*str_ptr & 0xc0) != 0x80) && (codepoint <= 0x10ffff)) {
752 if (sizeof(xtd::wchar) > 2)
753 out.append(1, static_cast<xtd::wchar>(codepoint));
754 else if (codepoint > 0xffff) {
755 out.append(1, static_cast<xtd::wchar>(0xd800 + (static_cast<xtd::wchar>(codepoint) >> 10)));
756 out.append(1, static_cast<xtd::wchar>(0xdc00 + (static_cast<xtd::wchar>(codepoint) & 0x03ff)));
757 } else if (codepoint < 0xd800 || codepoint >= 0xe000)
758 out.append(1, static_cast<xtd::wchar>(codepoint));
759 }
760 }
761 return out;
762}
763
764template<>
765inline std::basic_string<xtd::char32> __xtd_convert_to_string<xtd::char32, char>(const std::basic_string<char>& str) noexcept {
766 auto out = std::basic_string<xtd::char32> {};
767 auto codepoint = 0u;
768 auto str_ptr = str.data();
769 while (*str_ptr != 0) {
770 auto ch = static_cast<unsigned char>(*str_ptr);
771 if (ch <= 0x7f) codepoint = ch;
772 else if (ch <= 0xbf) codepoint = (codepoint << 6) | (ch & 0x3f);
773 else if (ch <= 0xdf) codepoint = ch & 0x1f;
774 else if (ch <= 0xef) codepoint = ch & 0x0f;
775 else codepoint = ch & 0x07;
776 ++str_ptr;
777 if (((*str_ptr & 0xc0) != 0x80) && (codepoint <= 0x10ffff))
778 out.append(1, static_cast<xtd::char32>(codepoint));
779 }
780 return out;
781}
782
783template<>
784inline std::basic_string<xtd::char32> __xtd_convert_to_string<xtd::char32, xtd::char8>(const std::basic_string<xtd::char8>& str) noexcept {
785 auto out = std::basic_string<xtd::char32> {};
786 auto codepoint = 0u;
787 auto str_ptr = str.data();
788 while (*str_ptr != 0) {
789 auto ch = static_cast<unsigned char>(*str_ptr);
790 if (ch <= 0x7f) codepoint = ch;
791 else if (ch <= 0xbf) codepoint = (codepoint << 6) | (ch & 0x3f);
792 else if (ch <= 0xdf) codepoint = ch & 0x1f;
793 else if (ch <= 0xef) codepoint = ch & 0x0f;
794 else codepoint = ch & 0x07;
795 ++str_ptr;
796 if (((*str_ptr & 0xc0) != 0x80) && (codepoint <= 0x10ffff))
797 out.append(1, static_cast<xtd::char32>(codepoint));
798 }
799 return out;
800}
801
802template<>
803inline std::basic_string<char> __xtd_convert_to_string<char, char>(const std::basic_string<char>& str) noexcept {
804 return str;
805}
806
807template<>
808inline std::basic_string<xtd::char16> __xtd_convert_to_string<xtd::char16, xtd::char16>(const std::basic_string<xtd::char16>& str) noexcept {
809 return str;
810}
811
812template<>
813inline std::basic_string<xtd::char32> __xtd_convert_to_string<xtd::char32, xtd::char32>(const std::basic_string<xtd::char32>& str) noexcept {
814 return str;
815}
816
817template<>
818inline std::basic_string<xtd::char8> __xtd_convert_to_string<xtd::char8, xtd::char8>(const std::basic_string<xtd::char8>& str) noexcept {
819 return str;
820}
821
822template<>
823inline std::basic_string<xtd::wchar> __xtd_convert_to_string<xtd::wchar, xtd::wchar>(const std::basic_string<xtd::wchar>& str) noexcept {
824 return str;
825}
826
827template<>
828inline std::basic_string<xtd::char8> __xtd_convert_to_string<xtd::char8, char>(const std::basic_string<char>& str) noexcept {
829 return reinterpret_cast<const xtd::char8*>(str.c_str());
830}
831
832template<>
833inline std::basic_string<char> __xtd_convert_to_string<char, xtd::char8>(const std::basic_string<xtd::char8>& str) noexcept {
834 return reinterpret_cast<const char*>(str.c_str());
835}
836
837template<>
838inline std::basic_string<xtd::char16> __xtd_convert_to_string<xtd::char16, xtd::char32>(const std::basic_string<xtd::char32>& str) noexcept {
839 return __xtd_convert_to_string<xtd::char16>(__xtd_convert_to_string<char>(str));
840}
841
842template<>
843inline std::basic_string<xtd::char16> __xtd_convert_to_string<xtd::char16, xtd::wchar>(const std::basic_string<xtd::wchar>& str) noexcept {
844 return __xtd_convert_to_string<xtd::char16>(__xtd_convert_to_string<char>(str));
845}
846
847template<>
848inline std::basic_string<xtd::char32> __xtd_convert_to_string<xtd::char32, xtd::char16>(const std::basic_string<xtd::char16>& str) noexcept {
849 return __xtd_convert_to_string<xtd::char32>(__xtd_convert_to_string<char>(str));
850}
851
852template<>
853inline std::basic_string<xtd::char32> __xtd_convert_to_string<xtd::char32, xtd::wchar>(const std::basic_string<xtd::wchar>& str) noexcept {
854 return __xtd_convert_to_string<xtd::char32>(__xtd_convert_to_string<char>(str));
855}
856
857template<>
858inline std::basic_string<xtd::wchar> __xtd_convert_to_string<xtd::wchar, xtd::char16>(const std::basic_string<xtd::char16>& str) noexcept {
859 return __xtd_convert_to_string<xtd::wchar>(__xtd_convert_to_string<char>(str));
860}
861
862template<>
863inline std::basic_string<xtd::wchar> __xtd_convert_to_string<xtd::wchar, xtd::char32>(const std::basic_string<xtd::char32>& str) noexcept {
864 return __xtd_convert_to_string<xtd::wchar>(__xtd_convert_to_string<char>(str));
865}
Contains xtd::array class.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:61
const_iterator end() const noexcept override
Returns an iterator to the element following the last element of the enumerable.
Definition basic_array.hpp:178
const_iterator begin() const noexcept override
Returns an iterator to the first element of the enumerable.
Definition basic_array.hpp:134
Represents text as a sequence of character units.
Definition basic_string.hpp:71
basic_string pad_left(xtd::size total_width) const noexcept
Right-aligns the characters in this basic_string, padding with spaces on the left for a specified tot...
Definition basic_string.hpp:1543
xtd::size index_of_any(const xtd::array< value_type > &values) const noexcept
Reports the index of the first occurrence in this instance of any character in a specified array of c...
xtd::size last_index_of_any(const xtd::array< value_type > &values) const noexcept
Reports the index of the last occurrence in this instance of any character in a specified array of ch...
basic_string pad_right(xtd::size total_width) const noexcept
Left-aligns the characters in this basic_string, padding with spaces on the right for a specified tot...
Definition basic_string.hpp:1557
auto concat(const ienumerable< char_t > &second) const noexcept
Definition enumerable.hpp:185
static void throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current())
Throws an exption with specified exception case.
xtd::string format(const xtd::string &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition format.hpp:20
@ format
The format is not valid.
Definition exception_case.hpp:49
@ index_out_of_range
The index is out of range.
Definition exception_case.hpp:59
@ format_opened_bracket_without_end_bracket
The format contains open backet_without_end_bracket.
Definition exception_case.hpp:53
@ format_closing_bracket_without_open_bracket
The format contains close backet_without_open_bracket.
Definition exception_case.hpp:51
#define unused_
It may be used to suppress the "unused variable" or "unused local typedefs" compiler warnings when th...
Definition unused.hpp:30
@ character
Specifies that the text is trimmed to the nearest character.
Definition string_trimming.hpp:21
@ word
Specifies that text is trimmed to the nearest word.
Definition string_trimming.hpp:23
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
char8_t char8
Represents a 8-bit unicode character.
Definition char8.hpp:26
wchar_t wchar
Represents a wide character.
Definition wchar.hpp:24
int32_t int32
Represents a 32-bit signed integer.
Definition int32.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
string_split_options
Specifies whether applicable xtd::string::split method overloads include or omit empty substrings fro...
Definition string_split_options.hpp:14
@ none
The return value includes array elements that contain an empty string.
Definition string_split_options.hpp:16
@ remove_empty_entries
The return value does not include array elements that contain an empty string.
Definition string_split_options.hpp:18
@ u
The U key.
Definition console_key.hpp:128
@ separator
The Separator key.
Definition console_key.hpp:172
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
const_reverse_iterator crbegin() const
Returns a reverse iterator to the beginning.
Definition read_only_span.hpp:194
const_reference at(size_type pos) const
Gets the specified element with bounds checking.
Definition read_only_span.hpp:255
constexpr size_type length() const noexcept
Returns the length of the current read_only_span.
Definition read_only_span.hpp:229
const_reverse_iterator crend() const
Returns a reverse iterator to the end.
Definition read_only_span.hpp:197
xtd::array< std::remove_cv_t< type_t > > to_array() const noexcept
Copies the contents of this read_only_span into a new array.
Definition read_only_span.hpp:368