From e1eac82943a788f90cf14a0dd0605d03df630a18 Mon Sep 17 00:00:00 2001 From: Gammasoft Date: Sun, 27 Oct 2024 21:41:57 +0100 Subject: [PATCH] Add to_string and to_wstring methods --- .../include/xtd/native/win32/strings.h | 63 ++++++++++++++++++- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/src/xtd.core.native.win32/include/xtd/native/win32/strings.h b/src/xtd.core.native.win32/include/xtd/native/win32/strings.h index 8586c28d7d0..acfd27694e6 100644 --- a/src/xtd.core.native.win32/include/xtd/native/win32/strings.h +++ b/src/xtd.core.native.win32/include/xtd/native/win32/strings.h @@ -124,13 +124,72 @@ namespace xtd::native { std::transform(result.begin(), result.end(), result.begin(), ::tolower); return result; } - + + static std::string to_string(const std::wstring& str) { return to_string(str.c_str()); } + static std::string to_string(const wchar_t* str) { + std::string out; + unsigned int codepoint = 0; + while (*str != 0) { + wchar_t character = static_cast(*str); + if (character >= 0xd800 && character <= 0xdbff) + codepoint = ((character - 0xd800) << 10) + 0x10000; + else { + if (character >= 0xdc00 && character <= 0xdfff) + codepoint |= character - 0xdc00; + else + codepoint = character; + + if (codepoint <= 0x7f) + out.append(1, static_cast(codepoint)); + else if (codepoint <= 0x7ff) { + out.append(1, static_cast(0xc0 | ((codepoint >> 6) & 0x1f))); + out.append(1, static_cast(0x80 | (codepoint & 0x3f))); + } else if (codepoint <= 0xffff) { + out.append(1, static_cast(0xe0 | ((codepoint >> 12) & 0x0f))); + out.append(1, static_cast(0x80 | ((codepoint >> 6) & 0x3f))); + out.append(1, static_cast(0x80 | (codepoint & 0x3f))); + } else { + out.append(1, static_cast(0xf0 | ((codepoint >> 18) & 0x07))); + out.append(1, static_cast(0x80 | ((codepoint >> 12) & 0x3f))); + out.append(1, static_cast(0x80 | ((codepoint >> 6) & 0x3f))); + out.append(1, static_cast(0x80 | (codepoint & 0x3f))); + } + codepoint = 0; + } + ++str; + } + return out; + } + static const std::string to_upper(const std::string& str) noexcept { auto result = str; std::transform(result.begin(), result.end(), result.begin(), ::toupper); return result; } - + + static std::wstring to_wstring(const std::string& str) { return to_wstring(str.c_str()); } + static std::wstring to_wstring(const char* str) { + std::wstring out; + char32_t codepoint = 0; + while (*str != 0) { + unsigned char ch = static_cast(*str); + if (ch <= 0x7f) codepoint = ch; + else if (ch <= 0xbf) codepoint = (codepoint << 6) | (ch & 0x3f); + else if (ch <= 0xdf) codepoint = ch & 0x1f; + else if (ch <= 0xef) codepoint = ch & 0x0f; + else codepoint = ch & 0x07; + ++str; + if (((*str & 0xc0) != 0x80) && (codepoint <= 0x10ffff)) { + if (codepoint > 0xffff) { + out.append(1, static_cast(0xd800 + (static_cast(codepoint) >> 10))); + out.append(1, static_cast(0xdc00 + (static_cast(codepoint) & 0x03ff))); + } else if (codepoint < 0xd800 || codepoint >= 0xe000) + out.append(1, static_cast(codepoint)); + } + } + return out; + } + static std::string trim_end(const std::string& str, const std::vector& trim_chars) noexcept { if (!str.size()) return str; auto result = str;