TextOperations.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * TextOperations.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. VCMI_LIB_NAMESPACE_BEGIN
  12. /// Namespace that provides utilites for unicode support (UTF-8)
  13. namespace TextOperations
  14. {
  15. /// returns length (in bytes) of UTF-8 character starting from specified character
  16. size_t DLL_LINKAGE getUnicodeCharacterSize(char firstByte);
  17. /// test if character is a valid UTF-8 symbol
  18. /// maxSize - maximum number of bytes this symbol may consist from ( = remainer of string)
  19. bool DLL_LINKAGE isValidUnicodeCharacter(const char * character, size_t maxSize);
  20. /// returns true if text contains valid ASCII-string
  21. /// Note that since UTF-8 extends ASCII, any ASCII string is also UTF-8 string
  22. bool DLL_LINKAGE isValidASCII(const std::string & text);
  23. bool DLL_LINKAGE isValidASCII(const char * data, size_t size);
  24. /// test if text contains valid UTF-8 sequence
  25. bool DLL_LINKAGE isValidUnicodeString(const std::string & text);
  26. bool DLL_LINKAGE isValidUnicodeString(const char * data, size_t size);
  27. /// converts text to UTF-8 from specified encoding or from one specified in settings
  28. std::string DLL_LINKAGE toUnicode(const std::string & text);
  29. std::string DLL_LINKAGE toUnicode(const std::string & text, const std::string & encoding);
  30. /// converts text from unicode to specified encoding or to one specified in settings
  31. /// NOTE: usage of these functions should be avoided if possible
  32. std::string DLL_LINKAGE fromUnicode(const std::string & text);
  33. std::string DLL_LINKAGE fromUnicode(const std::string & text, const std::string & encoding);
  34. ///delete specified amount of UTF-8 characters from right
  35. DLL_LINKAGE void trimRightUnicode(std::string & text, size_t amount = 1);
  36. /// converts number into string using metric system prefixes, e.g. 'k' or 'M' to keep resulting strings within specified size
  37. /// Note that resulting string may have more symbols than digits: minus sign and prefix symbol
  38. template<typename Arithmetic>
  39. inline std::string formatMetric(Arithmetic number, int maxDigits);
  40. /// replaces all symbols that normally need escaping with appropriate escape sequences
  41. std::string escapeString(std::string input);
  42. };
  43. template<typename Arithmetic>
  44. inline std::string TextOperations::formatMetric(Arithmetic number, int maxDigits)
  45. {
  46. Arithmetic max = std::pow(10, maxDigits);
  47. if (std::abs(number) < max)
  48. return std::to_string(number);
  49. std::string symbols = " kMGTPE";
  50. auto iter = symbols.begin();
  51. while (std::abs(number) >= max)
  52. {
  53. number /= 1000;
  54. iter++;
  55. assert(iter != symbols.end());//should be enough even for int64
  56. }
  57. return std::to_string(number) + *iter;
  58. }
  59. VCMI_LIB_NAMESPACE_END