mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 10:31:50 +00:00
fix stringutil locale-related functions
This commit is contained in:
parent
5f2c0f69ce
commit
30177b9d2a
2 changed files with 13 additions and 10 deletions
|
|
@ -7,6 +7,8 @@
|
|||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
static std::locale locale("en_US.UTF-8");
|
||||
|
||||
std::string util::escape(std::string_view s, bool escapeUnicode) {
|
||||
std::stringstream ss;
|
||||
ss << '"';
|
||||
|
|
@ -507,39 +509,34 @@ double util::parse_double(const std::string& str, size_t offset, size_t len) {
|
|||
|
||||
std::wstring util::lower_case(const std::wstring& str) {
|
||||
std::wstring result = str;
|
||||
static const std::locale loc("");
|
||||
for (uint i = 0; i < result.length(); i++) {
|
||||
result[i] = static_cast<wchar_t>(std::tolower(str[i], loc));
|
||||
result[i] = static_cast<wchar_t>(std::tolower(str[i], locale));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::wstring util::upper_case(const std::wstring& str) {
|
||||
std::wstring result = str;
|
||||
static const std::locale loc("");
|
||||
for (uint i = 0; i < result.length(); i++) {
|
||||
result[i] = static_cast<wchar_t>(std::toupper(str[i], loc));
|
||||
result[i] = static_cast<wchar_t>(std::toupper(str[i], locale));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::wstring util::capitalized(const std::wstring& str) {
|
||||
if (str.empty()) return str;
|
||||
static const std::locale loc("");
|
||||
return std::wstring({static_cast<wchar_t>(std::toupper(str[0], loc))}) +
|
||||
return std::wstring({static_cast<wchar_t>(std::toupper(str[0], locale))}) +
|
||||
str.substr(1);
|
||||
}
|
||||
|
||||
std::string util::capitalized(const std::string& str) {
|
||||
if (str.empty()) return str;
|
||||
static const std::locale loc("");
|
||||
return std::string({static_cast<char>(std::toupper(str[0], loc))}) +
|
||||
return std::string({static_cast<char>(std::toupper(str[0], locale))}) +
|
||||
str.substr(1);
|
||||
}
|
||||
|
||||
std::wstring util::pascal_case(const std::wstring& str) {
|
||||
if (str.empty()) return str;
|
||||
static const std::locale loc("");
|
||||
std::wstring result = str;
|
||||
bool upper = true;
|
||||
for (uint i = 0; i < result.length(); i++) {
|
||||
|
|
@ -547,7 +544,7 @@ std::wstring util::pascal_case(const std::wstring& str) {
|
|||
if (c <= ' ') {
|
||||
upper = true;
|
||||
} else if (upper) {
|
||||
result[i] = static_cast<wchar_t>(std::toupper(str[i], loc));
|
||||
result[i] = static_cast<wchar_t>(std::toupper(str[i], locale));
|
||||
upper = false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -101,3 +101,9 @@ TEST(stringutil, escape_cases) {
|
|||
}
|
||||
EXPECT_EQ(utf8str, restored);
|
||||
}
|
||||
|
||||
TEST(stringutil, cases) {
|
||||
EXPECT_EQ(util::upper_case(L"тест"), std::wstring(L"ТЕСТ"));
|
||||
EXPECT_EQ(util::lower_case(L"ТЕСТ"), std::wstring(L"тест"));
|
||||
EXPECT_EQ(util::pascal_case(L"тестовая строка"), std::wstring(L"Тестовая Строка"));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue