2
0

Unicode.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #pragma once
  2. #include "Defines.h"
  3. #include <cstdint>
  4. #include <string>
  5. namespace gameplay
  6. {
  7. /**
  8. * Collection of unicode conversion utilities.
  9. *
  10. * Typically only used when using directly Windown API and other external libraries using unicode.
  11. *
  12. * Note:
  13. * Use the 'Path' class instead when converting windows file system paths instead of this class.
  14. *
  15. * @see Path::convert_utf8_to_windows_path
  16. * @see Path::convert_windows_to_utf8_path
  17. */
  18. class GP_API Unicode
  19. {
  20. public:
  21. /**
  22. * Failure string returned from 'convert_utf32_to_utf8' code point function,
  23. * usually because of invalid input code point
  24. *
  25. * @see convert_utf32_to_utf8
  26. */
  27. static inline const std::string UTF32_TO_UTF8_FAILURE = "[?]";
  28. /**
  29. * Convert a utf-32 codepoint to utf-8 encoded string
  30. *
  31. * @param codepoint The code point to convert, in utf-32 encoding
  32. * @return The utf-8 encoded version of codepoint.
  33. */
  34. static std::string convert_utf32_to_utf8(uint32_t codepoint);
  35. #if GP_PLATFORM_WINDOWS
  36. /**
  37. * Failure string returned from 'convert_wide_to_utf8' functions when conversion
  38. * cannot be performed, usually because of invalid input string.
  39. *
  40. * @see WIDE_TO_UTF8_FAILURE
  41. */
  42. static inline const std::string WIDE_TO_UTF8_FAILURE = "[failure-converting-to-utf8]";
  43. /**
  44. * Converts a Windows wide string to utf-8 string
  45. *
  46. * @param wide Input string to convert, in Windows utf-16 encoding.
  47. * @return The utf-8 encoded version of wide or WIDE_TO_UTF8_FAILURE message if conversion cannot be performed.
  48. */
  49. static std::string convert_wide_to_utf8(const wchar_t* wide);
  50. /**
  51. * Converts a Windows wide string to utf-8 string
  52. *
  53. * @param wide Input string to convert, in Windows utf-16 encoding.
  54. * @return The utf-8 encoded version of wide or UNICODE_TO_UTF8_FAILURE message if conversion cannot be performed.
  55. */
  56. static std::string convert_wide_to_utf8(const std::wstring& wide);
  57. /**
  58. * Failure string returned from 'convert_utf8_to_wide' functions when conversion
  59. * cannot be performed, usually because of invalid input string.
  60. *
  61. * @see convert_utf8_to_wide
  62. */
  63. static inline const std::wstring UTF8_TO_WIDE_FAILURE = L"[failure-converting-to-wide]";
  64. /**
  65. * Converts a utf-8 encoded string to Windows wide character array
  66. *
  67. * @param utf8 Input string to convert, in UTF-8 encoding.
  68. * @return Wide string version of utf8 or UTF8_TO_WIDE_FAILURE message if conversion cannot be performed.
  69. */
  70. static std::wstring convert_utf8_to_wide(const char* utf8);
  71. /**
  72. * Converts a utf-8 encoded string to Windows wide character array
  73. * Do not use this function for Windows file path conversion!
  74. *
  75. * @param utf8 Input string to convert, in UTF-8 encoding.
  76. * @return Wide string version of utf8 or UTF8_TO_WIDE_FAILURE message if conversion cannot be performed.
  77. */
  78. static std::wstring convert_utf8_to_wide(const std::string& utf8);
  79. /**
  80. * Performs a case-sensitive comparison of wide strings in Unicode (Windows) using system default locale.
  81. *
  82. * @param str1 First input string.
  83. * @param str2 Second input string.
  84. * @return < 0 if string1 less than str2, 0 if str1 identical to str2, > 0 if str1 greater than str2,
  85. * INT_MAX on error.
  86. */
  87. static int compare_wide_strings_case_insensitive(const wchar_t* str1, const wchar_t* str2);
  88. /**
  89. * Performs a case-insensitive comparison of wide strings in Unicode (Windows) using system default locale.
  90. *
  91. * @param str1 The first input string.
  92. * @param str2 The second input string.
  93. * @return < 0 if string1 less than str2, 0 if str1 identical to str2, > 0 if str1 greater than str2,
  94. * INT_MAX on error.
  95. */
  96. static int compare_wide_strings_case_insensitive(const std::wstring& str1, const std::wstring& str2);
  97. /**
  98. * Converts wide string in Unicode (Windows) to uppercase using system default locale.
  99. *
  100. * @param str Input string.
  101. * @return The uppercased string.
  102. */
  103. static std::wstring convert_wide_string_to_uppercase(const std::wstring& str);
  104. /**
  105. * Converts wide string in Unicode (Windows) to uppercase using system default locale, in-place version.
  106. *
  107. * @param str The string to be pass in and converted.
  108. */
  109. static void convert_wide_string_to_uppercase_in_place(std::wstring& str);
  110. /**
  111. * Converts wide string in Unicode (Windows) to lowercase using system default locale.
  112. *
  113. * @param str The input string.
  114. * @return The converted lowercased string.
  115. */
  116. static std::wstring convert_wide_string_to_lowercase(const std::wstring& str);
  117. /**
  118. * Converts wide string in Unicode (Windows) to lowercase using system default locale, in-place version.
  119. *
  120. * @param string The input and output string.
  121. */
  122. static void convert_wide_string_to_cowercase_in_place(std::wstring& string);
  123. #endif
  124. };
  125. }