1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-11 13:15:38 +02:00

count unicode chars to extra function

This commit is contained in:
Michael 2023-08-24 18:34:00 +02:00 committed by GitHub
parent e6f1677d3a
commit 37e2292720
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 2 deletions

View File

@ -214,8 +214,7 @@ void CPlayerInterface::performAutosave()
if(prefix.empty())
{
std::string name = cb->getMapHeader()->name;
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv;
int txtlen = conv.from_bytes(name).size();
int txtlen = TextOperations::getUnicodeCharactersCount(name);
TextOperations::trimRightUnicode(name, std::max(0, txtlen - 15));
std::string forbiddenChars("\\/:?\"<>| ");

View File

@ -193,6 +193,12 @@ void TextOperations::trimRightUnicode(std::string & text, const size_t amount)
}
}
size_t getUnicodeCharactersCount(const std::string & text)
{
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv;
return conv.from_bytes(text).size();
}
std::string TextOperations::escapeString(std::string input)
{
boost::replace_all(input, "\\", "\\\\");

View File

@ -46,6 +46,9 @@ namespace TextOperations
///delete specified amount of UTF-8 characters from right
DLL_LINKAGE void trimRightUnicode(std::string & text, size_t amount = 1);
/// give back amount of unicode characters
size_t DLL_LINKAGE getUnicodeCharactersCount(const std::string & text);
/// converts number into string using metric system prefixes, e.g. 'k' or 'M' to keep resulting strings within specified size
/// Note that resulting string may have more symbols than digits: minus sign and prefix symbol
template<typename Arithmetic>