mirror of
https://github.com/vcmi/vcmi.git
synced 2025-12-01 23:12:49 +02:00
renamed Unicode -> TextOperations, to use for all text processing
This commit is contained in:
@@ -12,24 +12,25 @@
|
||||
VCMI_LIB_NAMESPACE_BEGIN
|
||||
|
||||
/// Namespace that provides utilites for unicode support (UTF-8)
|
||||
namespace Unicode
|
||||
namespace TextOperations
|
||||
{
|
||||
/// evaluates size of UTF-8 character
|
||||
size_t DLL_LINKAGE getCharacterSize(char firstByte);
|
||||
/// returns length (in bytes) of UTF-8 character starting from specified character
|
||||
size_t DLL_LINKAGE getUnicodeCharacterSize(char firstByte);
|
||||
|
||||
/// test if character is a valid UTF-8 symbol
|
||||
/// maxSize - maximum number of bytes this symbol may consist from ( = remainer of string)
|
||||
bool DLL_LINKAGE isValidCharacter(const char * character, size_t maxSize);
|
||||
bool DLL_LINKAGE isValidUnicodeCharacter(const char * character, size_t maxSize);
|
||||
|
||||
/// test if text contains ASCII-string (no need for unicode conversion)
|
||||
/// returns true if text contains valid ASCII-string
|
||||
/// Note that since UTF-8 extends ASCII, any ASCII string is also UTF-8 string
|
||||
bool DLL_LINKAGE isValidASCII(const std::string & text);
|
||||
bool DLL_LINKAGE isValidASCII(const char * data, size_t size);
|
||||
|
||||
/// test if text contains valid UTF-8 sequence
|
||||
bool DLL_LINKAGE isValidString(const std::string & text);
|
||||
bool DLL_LINKAGE isValidString(const char * data, size_t size);
|
||||
bool DLL_LINKAGE isValidUnicodeString(const std::string & text);
|
||||
bool DLL_LINKAGE isValidUnicodeString(const char * data, size_t size);
|
||||
|
||||
/// converts text to unicode from specified encoding or from one specified in settings
|
||||
/// converts text to UTF-8 from specified encoding or from one specified in settings
|
||||
std::string DLL_LINKAGE toUnicode(const std::string & text);
|
||||
std::string DLL_LINKAGE toUnicode(const std::string & text, const std::string & encoding);
|
||||
|
||||
@@ -38,8 +39,38 @@ namespace Unicode
|
||||
std::string DLL_LINKAGE fromUnicode(const std::string & text);
|
||||
std::string DLL_LINKAGE fromUnicode(const std::string & text, const std::string & encoding);
|
||||
|
||||
///delete (amount) UTF characters from right
|
||||
DLL_LINKAGE void trimRight(std::string & text, size_t amount = 1);
|
||||
///delete specified amount of UTF-8 characters from right
|
||||
DLL_LINKAGE void trimRightUnicode(std::string & text, size_t amount = 1);
|
||||
|
||||
/// 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>
|
||||
inline std::string formatMetric(Arithmetic number, int maxDigits);
|
||||
|
||||
/// replaces all symbols that normally need escaping with appropriate escape sequences
|
||||
std::string escapeString(std::string input);
|
||||
};
|
||||
|
||||
|
||||
|
||||
template<typename Arithmetic>
|
||||
inline std::string TextOperations::formatMetric(Arithmetic number, int maxDigits)
|
||||
{
|
||||
Arithmetic max = std::pow(10, maxDigits);
|
||||
if (std::abs(number) < max)
|
||||
return std::to_string(number);
|
||||
|
||||
std::string symbols = " kMGTPE";
|
||||
auto iter = symbols.begin();
|
||||
|
||||
while (std::abs(number) >= max)
|
||||
{
|
||||
number /= 1000;
|
||||
iter++;
|
||||
|
||||
assert(iter != symbols.end());//should be enough even for int64
|
||||
}
|
||||
return std::to_string(number) + *iter;
|
||||
}
|
||||
|
||||
VCMI_LIB_NAMESPACE_END
|
||||
|
||||
Reference in New Issue
Block a user