Quellcode durchsuchen

count unicode chars to extra function

Michael vor 2 Jahren
Ursprung
Commit
37e2292720
3 geänderte Dateien mit 10 neuen und 2 gelöschten Zeilen
  1. 1 2
      client/CPlayerInterface.cpp
  2. 6 0
      lib/TextOperations.cpp
  3. 3 0
      lib/TextOperations.h

+ 1 - 2
client/CPlayerInterface.cpp

@@ -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("\\/:?\"<>| ");

+ 6 - 0
lib/TextOperations.cpp

@@ -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, "\\", "\\\\");

+ 3 - 0
lib/TextOperations.h

@@ -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>