Unicode.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // Unicode.cpp //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // Provides utitlity functions to work with Unicode and other encodings. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "dxc/Support/Global.h"
  12. #include <specstrings.h>
  13. #include "dxc/Support/Unicode.h"
  14. #include <string>
  15. #include "dxc/Support/WinIncludes.h"
  16. namespace Unicode {
  17. _Success_(return != false)
  18. bool UTF16ToEncodedString(_In_z_ const wchar_t* text, DWORD cp, DWORD flags, _Inout_ std::string* pValue, _Out_opt_ bool* lossy) {
  19. BOOL usedDefaultChar;
  20. LPBOOL pUsedDefaultChar = (lossy == nullptr) ? nullptr : &usedDefaultChar;
  21. size_t cUTF16 = wcslen(text);
  22. if (lossy != nullptr) *lossy = false;
  23. // Handle zero-length as a special case; it's a special value to indicate errors in WideCharToMultiByte.
  24. if (cUTF16 == 0) {
  25. pValue->resize(0);
  26. DXASSERT(lossy == nullptr || *lossy == false, "otherwise earlier initialization in this function was updated");
  27. return true;
  28. }
  29. int cbUTF8 = ::WideCharToMultiByte(cp, flags, text, cUTF16, nullptr, 0, nullptr, pUsedDefaultChar);
  30. if (cbUTF8 == 0)
  31. return false;
  32. pValue->resize(cbUTF8);
  33. cbUTF8 = ::WideCharToMultiByte(cp, flags, text, cUTF16, &(*pValue)[0], pValue->size(), nullptr, pUsedDefaultChar);
  34. DXASSERT(cbUTF8 > 0, "otherwise contents have changed");
  35. DXASSERT((*pValue)[pValue->size()] == '\0', "otherwise string didn't null-terminate after resize() call");
  36. if (lossy != nullptr) *lossy = usedDefaultChar;
  37. return true;
  38. }
  39. _Use_decl_annotations_
  40. bool UTF8ToUTF16String(const char *pUTF8, std::wstring *pUTF16) {
  41. size_t cbUTF8 = (pUTF8 == nullptr) ? 0 : strlen(pUTF8);
  42. return UTF8ToUTF16String(pUTF8, cbUTF8, pUTF16);
  43. }
  44. _Use_decl_annotations_
  45. bool UTF8ToUTF16String(const char *pUTF8, size_t cbUTF8, std::wstring *pUTF16) {
  46. DXASSERT_NOMSG(pUTF16 != nullptr);
  47. // Handle zero-length as a special case; it's a special value to indicate
  48. // errors in MultiByteToWideChar.
  49. if (cbUTF8 == 0) {
  50. pUTF16->resize(0);
  51. return true;
  52. }
  53. int cUTF16 = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, pUTF8,
  54. cbUTF8, nullptr, 0);
  55. if (cUTF16 == 0)
  56. return false;
  57. pUTF16->resize(cUTF16);
  58. cUTF16 = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, pUTF8, cbUTF8,
  59. &(*pUTF16)[0], pUTF16->size());
  60. DXASSERT(cUTF16 > 0, "otherwise contents changed");
  61. DXASSERT((*pUTF16)[pUTF16->size()] == L'\0',
  62. "otherwise wstring didn't null-terminate after resize() call");
  63. return true;
  64. }
  65. std::wstring UTF8ToUTF16StringOrThrow(_In_z_ const char *pUTF8) {
  66. std::wstring result;
  67. if (!UTF8ToUTF16String(pUTF8, &result)) {
  68. throw hlsl::Exception(DXC_E_STRING_ENCODING_FAILED);
  69. }
  70. return result;
  71. }
  72. _Use_decl_annotations_
  73. bool UTF8ToConsoleString(_In_z_ const char* text, _Inout_ std::string* pValue, _Out_opt_ bool* lossy) {
  74. DXASSERT_NOMSG(text != nullptr);
  75. DXASSERT_NOMSG(pValue != nullptr);
  76. std::wstring text16;
  77. if (lossy != nullptr) *lossy = false;
  78. if (!UTF8ToUTF16String(text, &text16)) {
  79. return false;
  80. }
  81. return UTF16ToConsoleString(text16.c_str(), pValue, lossy);
  82. }
  83. _Use_decl_annotations_
  84. bool UTF16ToConsoleString(const wchar_t* text, std::string* pValue, bool* lossy) {
  85. DXASSERT_NOMSG(text != nullptr);
  86. DXASSERT_NOMSG(pValue != nullptr);
  87. UINT cp = GetConsoleOutputCP();
  88. return UTF16ToEncodedString(text, cp, 0, pValue, lossy);
  89. }
  90. _Use_decl_annotations_
  91. bool UTF16ToUTF8String(const wchar_t *pUTF16, std::string *pUTF8) {
  92. DXASSERT_NOMSG(pUTF16 != nullptr);
  93. DXASSERT_NOMSG(pUTF8 != nullptr);
  94. return UTF16ToEncodedString(pUTF16, CP_UTF8, 0, pUTF8, nullptr);
  95. }
  96. std::string UTF16ToUTF8StringOrThrow(_In_z_ const wchar_t *pUTF16) {
  97. std::string result;
  98. if (!UTF16ToUTF8String(pUTF16, &result)) {
  99. throw hlsl::Exception(DXC_E_STRING_ENCODING_FAILED);
  100. }
  101. return result;
  102. }
  103. _Use_decl_annotations_
  104. bool UTF8BufferToUTF16Buffer(const char *pUTF8, int cbUTF8, wchar_t **ppUTF16, size_t *pcUTF16) throw() {
  105. *ppUTF16 = nullptr;
  106. *pcUTF16 = 0;
  107. if (cbUTF8 == 0 || (cbUTF8 == -1 && *pUTF8 == '\0')) {
  108. *ppUTF16 = new (std::nothrow) wchar_t[1];
  109. if (*ppUTF16 == nullptr)
  110. return false;
  111. (*ppUTF16)[0] = L'\0';
  112. *pcUTF16 = 1;
  113. return true;
  114. }
  115. int c = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, pUTF8, cbUTF8, nullptr, 0);
  116. if (c == 0)
  117. return false;
  118. // add space for null-terminator if we're not accounting for it
  119. if (cbUTF8 != -1)
  120. c += 1;
  121. wchar_t *p = new (std::nothrow) wchar_t[c];
  122. if (p == nullptr)
  123. return false;
  124. int converted = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
  125. pUTF8, cbUTF8,
  126. p, c);
  127. (void)converted;
  128. DXASSERT(converted > 0, "otherwise contents have changed");
  129. p[c - 1] = L'\0';
  130. *ppUTF16 = p;
  131. *pcUTF16 = c;
  132. return true;
  133. }
  134. _Use_decl_annotations_
  135. bool UTF16BufferToUTF8Buffer(const wchar_t *pUTF16, int cUTF16, char **ppUTF8, size_t *pcUTF8) throw() {
  136. *ppUTF8 = nullptr;
  137. *pcUTF8 = 0;
  138. if (cUTF16 == 0 || (cUTF16 == -1 && *pUTF16 == '\0')) {
  139. *ppUTF8 = new (std::nothrow) char[1];
  140. if (*ppUTF8 == nullptr)
  141. return false;
  142. (*ppUTF8)[0] = '\0';
  143. *pcUTF8 = 1;
  144. return true;
  145. }
  146. int c1 = ::WideCharToMultiByte(CP_UTF8, // code page
  147. 0, // flags
  148. pUTF16, // string to convert
  149. cUTF16, // size, in chars, of string to convert
  150. nullptr, // output buffer
  151. 0, // size of output buffer
  152. nullptr, nullptr);
  153. if (c1 == 0)
  154. return false;
  155. // add space for null-terminator if we're not accounting for it
  156. if (cUTF16 != -1)
  157. c1 += 1;
  158. char *p = new (std::nothrow) char[c1];
  159. if (p == nullptr)
  160. return false;
  161. int converted = ::WideCharToMultiByte(CP_UTF8, 0,
  162. pUTF16, cUTF16,
  163. p, c1,
  164. nullptr, nullptr);
  165. (void)converted;
  166. DXASSERT(converted > 0, "otherwise contents have changed");
  167. p[c1 - 1] = '\0';
  168. *ppUTF8 = p;
  169. *pcUTF8 = c1;
  170. return true;
  171. }
  172. template<typename TChar>
  173. static
  174. bool IsStarMatchT(const TChar *pMask, size_t maskLen, const TChar *pName, size_t nameLen, TChar star) {
  175. if (maskLen == 0 && nameLen == 0) {
  176. return true;
  177. }
  178. if (maskLen == 0 || nameLen == 0) {
  179. return false;
  180. }
  181. if (pMask[maskLen - 1] == star) {
  182. // Prefix match.
  183. if (maskLen == 1) { // For just '*', everything is a match.
  184. return true;
  185. }
  186. --maskLen;
  187. if (maskLen > nameLen) { // Mask is longer than name, can't be a match.
  188. return false;
  189. }
  190. return 0 == memcmp(pMask, pName, sizeof(TChar) * maskLen);
  191. }
  192. else {
  193. // Exact match.
  194. if (nameLen != maskLen) {
  195. return false;
  196. }
  197. return 0 == memcmp(pMask, pName, sizeof(TChar) * nameLen);
  198. }
  199. }
  200. _Use_decl_annotations_
  201. bool IsStarMatchUTF8(const char *pMask, size_t maskLen, const char *pName, size_t nameLen) {
  202. return IsStarMatchT<char>(pMask, maskLen, pName, nameLen, '*');
  203. }
  204. _Use_decl_annotations_
  205. bool IsStarMatchUTF16(const wchar_t *pMask, size_t maskLen, const wchar_t *pName, size_t nameLen) {
  206. return IsStarMatchT<wchar_t>(pMask, maskLen, pName, nameLen, L'*');
  207. }
  208. } // namespace Unicode