Unicode.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 UTF8BufferToUTF16ComHeap(const char *pUTF8, wchar_t **ppUTF16) throw() {
  105. *ppUTF16 = nullptr;
  106. int c = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, pUTF8, -1,
  107. nullptr, 0);
  108. if (c == 0)
  109. return false;
  110. CComHeapPtr<wchar_t> p;
  111. if (!p.Allocate(c))
  112. return false;
  113. DXVERIFY_NOMSG(0 < ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, pUTF8,
  114. -1, p.m_pData, c));
  115. *ppUTF16 = p.Detach();
  116. return true;
  117. }
  118. _Use_decl_annotations_
  119. bool UTF8BufferToUTF16Buffer(const char *pUTF8, int cbUTF8, wchar_t **ppUTF16, size_t *pcUTF16) throw() {
  120. *ppUTF16 = nullptr;
  121. *pcUTF16 = 0;
  122. if (cbUTF8 == 0 || (cbUTF8 == -1 && *pUTF8 == '\0')) {
  123. *ppUTF16 = new (std::nothrow) wchar_t[1];
  124. if (*ppUTF16 == nullptr)
  125. return false;
  126. (*ppUTF16)[0] = L'\0';
  127. *pcUTF16 = 1;
  128. return true;
  129. }
  130. int c = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, pUTF8, cbUTF8, nullptr, 0);
  131. if (c == 0)
  132. return false;
  133. // add space for null-terminator if we're not accounting for it
  134. if (cbUTF8 != -1)
  135. c += 1;
  136. wchar_t *p = new (std::nothrow) wchar_t[c];
  137. if (p == nullptr)
  138. return false;
  139. int converted = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
  140. pUTF8, cbUTF8,
  141. p, c);
  142. (void)converted;
  143. DXASSERT(converted > 0, "otherwise contents have changed");
  144. p[c - 1] = L'\0';
  145. *ppUTF16 = p;
  146. *pcUTF16 = c;
  147. return true;
  148. }
  149. _Use_decl_annotations_
  150. bool UTF16BufferToUTF8Buffer(const wchar_t *pUTF16, int cUTF16, char **ppUTF8, size_t *pcUTF8) throw() {
  151. *ppUTF8 = nullptr;
  152. *pcUTF8 = 0;
  153. if (cUTF16 == 0 || (cUTF16 == -1 && *pUTF16 == '\0')) {
  154. *ppUTF8 = new (std::nothrow) char[1];
  155. if (*ppUTF8 == nullptr)
  156. return false;
  157. (*ppUTF8)[0] = '\0';
  158. *pcUTF8 = 1;
  159. return true;
  160. }
  161. int c1 = ::WideCharToMultiByte(CP_UTF8, // code page
  162. 0, // flags
  163. pUTF16, // string to convert
  164. cUTF16, // size, in chars, of string to convert
  165. nullptr, // output buffer
  166. 0, // size of output buffer
  167. nullptr, nullptr);
  168. if (c1 == 0)
  169. return false;
  170. // add space for null-terminator if we're not accounting for it
  171. if (cUTF16 != -1)
  172. c1 += 1;
  173. char *p = new (std::nothrow) char[c1];
  174. if (p == nullptr)
  175. return false;
  176. int converted = ::WideCharToMultiByte(CP_UTF8, 0,
  177. pUTF16, cUTF16,
  178. p, c1,
  179. nullptr, nullptr);
  180. (void)converted;
  181. DXASSERT(converted > 0, "otherwise contents have changed");
  182. p[c1 - 1] = '\0';
  183. *ppUTF8 = p;
  184. *pcUTF8 = c1;
  185. return true;
  186. }
  187. template<typename TChar>
  188. static
  189. bool IsStarMatchT(const TChar *pMask, size_t maskLen, const TChar *pName, size_t nameLen, TChar star) {
  190. if (maskLen == 0 && nameLen == 0) {
  191. return true;
  192. }
  193. if (maskLen == 0 || nameLen == 0) {
  194. return false;
  195. }
  196. if (pMask[maskLen - 1] == star) {
  197. // Prefix match.
  198. if (maskLen == 1) { // For just '*', everything is a match.
  199. return true;
  200. }
  201. --maskLen;
  202. if (maskLen > nameLen) { // Mask is longer than name, can't be a match.
  203. return false;
  204. }
  205. return 0 == memcmp(pMask, pName, sizeof(TChar) * maskLen);
  206. }
  207. else {
  208. // Exact match.
  209. if (nameLen != maskLen) {
  210. return false;
  211. }
  212. return 0 == memcmp(pMask, pName, sizeof(TChar) * nameLen);
  213. }
  214. }
  215. _Use_decl_annotations_
  216. bool IsStarMatchUTF8(const char *pMask, size_t maskLen, const char *pName, size_t nameLen) {
  217. return IsStarMatchT<char>(pMask, maskLen, pName, nameLen, '*');
  218. }
  219. _Use_decl_annotations_
  220. bool IsStarMatchUTF16(const wchar_t *pMask, size_t maskLen, const wchar_t *pName, size_t nameLen) {
  221. return IsStarMatchT<wchar_t>(pMask, maskLen, pName, nameLen, L'*');
  222. }
  223. } // namespace Unicode