Skip to content

Commit

Permalink
Add to_string and to_wstring methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Oct 27, 2024
1 parent c15e191 commit e1eac82
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions src/xtd.core.native.win32/include/xtd/native/win32/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<wchar_t>(*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<char>(codepoint));
else if (codepoint <= 0x7ff) {
out.append(1, static_cast<char>(0xc0 | ((codepoint >> 6) & 0x1f)));
out.append(1, static_cast<char>(0x80 | (codepoint & 0x3f)));
} else if (codepoint <= 0xffff) {
out.append(1, static_cast<char>(0xe0 | ((codepoint >> 12) & 0x0f)));
out.append(1, static_cast<char>(0x80 | ((codepoint >> 6) & 0x3f)));
out.append(1, static_cast<char>(0x80 | (codepoint & 0x3f)));
} else {
out.append(1, static_cast<char>(0xf0 | ((codepoint >> 18) & 0x07)));
out.append(1, static_cast<char>(0x80 | ((codepoint >> 12) & 0x3f)));
out.append(1, static_cast<char>(0x80 | ((codepoint >> 6) & 0x3f)));
out.append(1, static_cast<char>(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<unsigned char>(*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<wchar_t>(0xd800 + (static_cast<char16_t>(codepoint) >> 10)));
out.append(1, static_cast<wchar_t>(0xdc00 + (static_cast<char16_t>(codepoint) & 0x03ff)));
} else if (codepoint < 0xd800 || codepoint >= 0xe000)
out.append(1, static_cast<wchar_t>(codepoint));
}
}
return out;
}

static std::string trim_end(const std::string& str, const std::vector<char>& trim_chars) noexcept {
if (!str.size()) return str;
auto result = str;
Expand Down

0 comments on commit e1eac82

Please sign in to comment.