Unicode.h 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // Unicode.h //
  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. #pragma once
  12. #include <string>
  13. #ifdef _WIN32
  14. #include <specstrings.h>
  15. #else
  16. // MultiByteToWideChar which is a Windows-specific method.
  17. // This is a very simplistic implementation for non-Windows platforms. This
  18. // implementation completely ignores CodePage and dwFlags.
  19. int MultiByteToWideChar(uint32_t CodePage, uint32_t dwFlags,
  20. const char *lpMultiByteStr, int cbMultiByte,
  21. wchar_t *lpWideCharStr, int cchWideChar);
  22. // WideCharToMultiByte is a Windows-specific method.
  23. // This is a very simplistic implementation for non-Windows platforms. This
  24. // implementation completely ignores CodePage and dwFlags.
  25. int WideCharToMultiByte(uint32_t CodePage, uint32_t dwFlags,
  26. const wchar_t *lpWideCharStr, int cchWideChar,
  27. char *lpMultiByteStr, int cbMultiByte,
  28. const char *lpDefaultChar = nullptr,
  29. bool *lpUsedDefaultChar = nullptr);
  30. #endif // _WIN32
  31. namespace Unicode
  32. {
  33. // Based on http://msdn.microsoft.com/en-us/library/windows/desktop/dd374101(v=vs.85).aspx.
  34. enum class Encoding { ASCII = 0, UTF8, UTF8_BOM, UTF16_LE, UTF16_BE, UTF32_LE, UTF32_BE };
  35. // An acp_char is a character encoded in the current Windows ANSI code page.
  36. typedef char acp_char;
  37. // A ccp_char is a character encoded in the console code page.
  38. typedef char ccp_char;
  39. _Success_(return != false)
  40. bool UTF8ToConsoleString(_In_opt_count_(textLen) const char* text, _In_ size_t textLen, _Inout_ std::string* pValue, _Out_opt_ bool* lossy);
  41. _Success_(return != false)
  42. bool UTF8ToConsoleString(_In_z_ const char* text, _Inout_ std::string* pValue, _Out_opt_ bool* lossy);
  43. _Success_(return != false)
  44. bool UTF16ToConsoleString(_In_opt_count_(textLen) const wchar_t* text, _In_ size_t textLen, _Inout_ std::string* pValue, _Out_opt_ bool* lossy);
  45. _Success_(return != false)
  46. bool UTF16ToConsoleString(_In_z_ const wchar_t* text, _Inout_ std::string* pValue, _Out_opt_ bool* lossy);
  47. _Success_(return != false)
  48. bool UTF8ToUTF16String(_In_opt_z_ const char *pUTF8, _Inout_ std::wstring *pUTF16);
  49. _Success_(return != false)
  50. bool UTF8ToUTF16String(_In_opt_count_(cbUTF8) const char *pUTF8, size_t cbUTF8, _Inout_ std::wstring *pUTF16);
  51. std::wstring UTF8ToUTF16StringOrThrow(_In_z_ const char *pUTF8);
  52. _Success_(return != false)
  53. bool UTF16ToUTF8String(_In_z_ const wchar_t *pUTF16, size_t cUTF16, _Inout_ std::string *pUTF8);
  54. bool UTF16ToUTF8String(_In_z_ const wchar_t *pUTF16, _Inout_ std::string *pUTF8);
  55. std::string UTF16ToUTF8StringOrThrow(_In_z_ const wchar_t *pUTF16);
  56. bool IsStarMatchUTF8(_In_reads_opt_(maskLen) const char *pMask, size_t maskLen,
  57. _In_reads_opt_(nameLen) const char *pName, size_t nameLen);
  58. bool IsStarMatchUTF16(_In_reads_opt_(maskLen) const wchar_t *pMask, size_t maskLen,
  59. _In_reads_opt_(nameLen) const wchar_t *pName, size_t nameLen);
  60. _Success_(return != false)
  61. bool UTF8BufferToUTF16ComHeap(_In_z_ const char *pUTF8,
  62. _Outptr_result_z_ wchar_t **ppUTF16) throw();
  63. _Success_(return != false)
  64. bool UTF8BufferToUTF16Buffer(
  65. _In_NLS_string_(cbUTF8) const char *pUTF8,
  66. int cbUTF8,
  67. _Outptr_result_buffer_(*pcchUTF16) wchar_t **ppUTF16,
  68. size_t *pcchUTF16) throw();
  69. _Success_(return != false)
  70. bool UTF16BufferToUTF8Buffer(
  71. _In_NLS_string_(cchUTF16) const wchar_t *pUTF16,
  72. int cchUTF16,
  73. _Outptr_result_buffer_(*pcbUTF8) char **ppUTF8,
  74. size_t *pcbUTF8) throw();
  75. } // namespace Unicode