mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +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 <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
static std::locale locale("en_US.UTF-8");
|
||||||
|
|
||||||
std::string util::escape(std::string_view s, bool escapeUnicode) {
|
std::string util::escape(std::string_view s, bool escapeUnicode) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
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 util::lower_case(const std::wstring& str) {
|
||||||
std::wstring result = str;
|
std::wstring result = str;
|
||||||
static const std::locale loc("");
|
|
||||||
for (uint i = 0; i < result.length(); i++) {
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::wstring util::upper_case(const std::wstring& str) {
|
std::wstring util::upper_case(const std::wstring& str) {
|
||||||
std::wstring result = str;
|
std::wstring result = str;
|
||||||
static const std::locale loc("");
|
|
||||||
for (uint i = 0; i < result.length(); i++) {
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::wstring util::capitalized(const std::wstring& str) {
|
std::wstring util::capitalized(const std::wstring& str) {
|
||||||
if (str.empty()) return str;
|
if (str.empty()) return str;
|
||||||
static const std::locale loc("");
|
return std::wstring({static_cast<wchar_t>(std::toupper(str[0], locale))}) +
|
||||||
return std::wstring({static_cast<wchar_t>(std::toupper(str[0], loc))}) +
|
|
||||||
str.substr(1);
|
str.substr(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string util::capitalized(const std::string& str) {
|
std::string util::capitalized(const std::string& str) {
|
||||||
if (str.empty()) return str;
|
if (str.empty()) return str;
|
||||||
static const std::locale loc("");
|
return std::string({static_cast<char>(std::toupper(str[0], locale))}) +
|
||||||
return std::string({static_cast<char>(std::toupper(str[0], loc))}) +
|
|
||||||
str.substr(1);
|
str.substr(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::wstring util::pascal_case(const std::wstring& str) {
|
std::wstring util::pascal_case(const std::wstring& str) {
|
||||||
if (str.empty()) return str;
|
if (str.empty()) return str;
|
||||||
static const std::locale loc("");
|
|
||||||
std::wstring result = str;
|
std::wstring result = str;
|
||||||
bool upper = true;
|
bool upper = true;
|
||||||
for (uint i = 0; i < result.length(); i++) {
|
for (uint i = 0; i < result.length(); i++) {
|
||||||
|
|
@ -547,7 +544,7 @@ std::wstring util::pascal_case(const std::wstring& str) {
|
||||||
if (c <= ' ') {
|
if (c <= ' ') {
|
||||||
upper = true;
|
upper = true;
|
||||||
} else if (upper) {
|
} 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;
|
upper = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -101,3 +101,9 @@ TEST(stringutil, escape_cases) {
|
||||||
}
|
}
|
||||||
EXPECT_EQ(utf8str, restored);
|
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