TextOperations.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 utilities for unicode support (UTF-8)
  13. namespace TextOperations
  14. {
  15. /// returns 32-bit UTF codepoint for UTF-8 character symbol
  16. uint32_t DLL_LINKAGE getUnicodeCodepoint(const char *data, size_t maxSize);
  17. /// returns 32-bit UTF codepoint for character symbol in selected single-byte encoding
  18. uint32_t DLL_LINKAGE getUnicodeCodepoint(char data, const std::string & encoding );
  19. /// returns length (in bytes) of UTF-8 character starting from specified character
  20. size_t DLL_LINKAGE getUnicodeCharacterSize(char firstByte);
  21. /// test if character is a valid UTF-8 symbol
  22. /// maxSize - maximum number of bytes this symbol may consist from ( = remainder of string)
  23. bool DLL_LINKAGE isValidUnicodeCharacter(const char * character, size_t maxSize);
  24. /// returns true if text contains valid ASCII-string
  25. /// Note that since UTF-8 extends ASCII, any ASCII string is also UTF-8 string
  26. bool DLL_LINKAGE isValidASCII(const std::string & text);
  27. bool DLL_LINKAGE isValidASCII(const char * data, size_t size);
  28. /// test if text contains valid UTF-8 sequence
  29. bool DLL_LINKAGE isValidUnicodeString(const std::string & text);
  30. bool DLL_LINKAGE isValidUnicodeString(const char * data, size_t size);
  31. /// converts text to UTF-8 from specified encoding or from one specified in settings
  32. std::string DLL_LINKAGE toUnicode(const std::string & text, const std::string & encoding);
  33. /// converts text from unicode to specified encoding or to one specified in settings
  34. /// NOTE: usage of these functions should be avoided if possible
  35. std::string DLL_LINKAGE fromUnicode(const std::string & text, const std::string & encoding);
  36. ///delete specified amount of UTF-8 characters from right
  37. DLL_LINKAGE void trimRightUnicode(std::string & text, size_t amount = 1);
  38. /// give back amount of unicode characters
  39. size_t DLL_LINKAGE getUnicodeCharactersCount(const std::string & text);
  40. /// converts number into string using metric system prefixes, e.g. 'k' or 'M' to keep resulting strings within specified size
  41. /// Note that resulting string may have more symbols than digits: minus sign and prefix symbol
  42. template<typename Arithmetic>
  43. inline std::string formatMetric(Arithmetic number, int maxDigits);
  44. /// replaces all symbols that normally need escaping with appropriate escape sequences
  45. std::string escapeString(std::string input);
  46. /// get formatted DateTime depending on the language selected
  47. DLL_LINKAGE std::string getFormattedDateTimeLocal(std::time_t dt);
  48. /// get formatted current DateTime depending on the language selected
  49. /// timeOffset - optional parameter to modify current time by specified time in seconds
  50. DLL_LINKAGE std::string getCurrentFormattedDateTimeLocal(std::chrono::seconds timeOffset = {});
  51. /// get formatted time (without date)
  52. DLL_LINKAGE std::string getFormattedTimeLocal(std::time_t dt);
  53. /// get formatted time (without date)
  54. /// timeOffset - optional parameter to modify current time by specified time in seconds
  55. DLL_LINKAGE std::string getCurrentFormattedTimeLocal(std::chrono::seconds timeOffset = {});
  56. };
  57. template<typename Arithmetic>
  58. inline std::string TextOperations::formatMetric(Arithmetic number, int maxDigits)
  59. {
  60. Arithmetic max = std::pow(10, maxDigits);
  61. if (std::abs(number) < max)
  62. return std::to_string(number);
  63. std::string symbols = " kMGTPE";
  64. auto iter = symbols.begin();
  65. while (std::abs(number) >= max)
  66. {
  67. number /= 1000;
  68. iter++;
  69. assert(iter != symbols.end());//should be enough even for int64
  70. }
  71. return std::to_string(number) + *iter;
  72. }
  73. VCMI_LIB_NAMESPACE_END