История изменений
Исправление fsb4000, (текущая версия) :
Для белых людей. Нечто вроде std::transform(s.begin(), s.end(), s.begin(), ::tolower); придумали явно для BLM и прочих погромщиков.
Я понял, что ты не переходил по моей ссылке.
Кстати,
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
это UB
И об этом написано в cppreference, у Страуструпа и по моему даже в стандарте есть прям точь в точь такой пример.
Like all other functions from
<cctype>
, the behavior of std::tolower is undefined if the argument’s value is neither representable as unsigned char nor equal to EOF. To use these functions safely with plain chars (or signed chars), the argument should first be converted to unsigned char
std::string str_tolower(std::string s) {
std::transform(s.begin(), s.end(), s.begin(),
// static_cast<int(*)(int)>(std::tolower) // wrong
// [](int c){ return std::tolower(c); } // wrong
// [](char c){ return std::tolower(c); } // wrong
[](unsigned char c){ return std::tolower(c); } // correct
);
return s;
}
А по моей первой ссылке был такой пример:
#include <locale>
#include <iostream>
void try_lower(const std::ctype<wchar_t>& f, wchar_t c)
{
wchar_t up = f.tolower(c);
if (up != c) {
std::wcout << "Lower case form of \'" << c << "' is " << up << '\n';
} else {
std::wcout << '\'' << c << "' has no lower case form\n";
}
}
int main()
{
std::locale::global(std::locale("en_US.utf8"));
std::wcout.imbue(std::locale());
std::wcout << "In US English UTF-8 locale:\n";
auto& f = std::use_facet<std::ctype<wchar_t>>(std::locale());
try_lower(f, L'Σ');
try_lower(f, L'Ɛ');
try_lower(f, L'A');
std::wstring str = L"HELLo, wORLD!";
std::wcout << "Lowercase form of the string '" << str << "' is ";
f.tolower(&str[0], &str[0] + str.size());
std::wcout << "'" << str << "'\n";
}
Исходная версия fsb4000, :
Для белых людей. Нечто вроде std::transform(s.begin(), s.end(), s.begin(), ::tolower); придумали явно для BLM и прочих погромщиков.
Я понял, что ты не переходил по моей ссылке.
Кстати,
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
это UB
И об этом написано в cppreference, у Страуструпа и по моему даже в стандарте есть прям точь в точь такой пример.
Like all other functions from , the behavior of std::tolower is undefined if the argument’s value is neither representable as unsigned char nor equal to EOF. To use these functions safely with plain chars (or signed chars), the argument should first be converted to unsigned char
std::string str_tolower(std::string s) {
std::transform(s.begin(), s.end(), s.begin(),
// static_cast<int(*)(int)>(std::tolower) // wrong
// [](int c){ return std::tolower(c); } // wrong
// [](char c){ return std::tolower(c); } // wrong
[](unsigned char c){ return std::tolower(c); } // correct
);
return s;
}
А по моей первой ссылке был такой пример:
#include <locale>
#include <iostream>
void try_lower(const std::ctype<wchar_t>& f, wchar_t c)
{
wchar_t up = f.tolower(c);
if (up != c) {
std::wcout << "Lower case form of \'" << c << "' is " << up << '\n';
} else {
std::wcout << '\'' << c << "' has no lower case form\n";
}
}
int main()
{
std::locale::global(std::locale("en_US.utf8"));
std::wcout.imbue(std::locale());
std::wcout << "In US English UTF-8 locale:\n";
auto& f = std::use_facet<std::ctype<wchar_t>>(std::locale());
try_lower(f, L'Σ');
try_lower(f, L'Ɛ');
try_lower(f, L'A');
std::wstring str = L"HELLo, wORLD!";
std::wcout << "Lowercase form of the string '" << str << "' is ";
f.tolower(&str[0], &str[0] + str.size());
std::wcout << "'" << str << "'\n";
}