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 28, 2024
1 parent e1eac82 commit 4e31a00
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 3 deletions.
61 changes: 60 additions & 1 deletion src/xtd.core.native.linux/include/xtd/native/linux/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,71 @@ namespace xtd::native::linux {
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
59 changes: 59 additions & 0 deletions src/xtd.core.native.macos/include/xtd/native/macos/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,71 @@ namespace xtd::native::macos {
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
61 changes: 60 additions & 1 deletion src/xtd.core.native.posix/include/xtd/native/posix/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,71 @@ namespace xtd::native::posix {
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
61 changes: 60 additions & 1 deletion src/xtd.core.native.unix/include/xtd/native/unix/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,71 @@ namespace xtd::native::unix {
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 4e31a00

Please sign in to comment.