ChakraICU.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. #include "CommonDefines.h"
  7. #ifdef HAS_ICU
  8. #ifdef WINDOWS10_ICU
  9. // if WINDOWS10_ICU is defined, pretend like we are building for recent Redstone,
  10. // even if that isn't necessarily true
  11. #pragma push_macro("NTDDI_VERSION")
  12. #undef NTDDI_VERSION
  13. #define NTDDI_VERSION NTDDI_WIN10_RS5
  14. #include <icu.h>
  15. #pragma pop_macro("NTDDI_VERSION")
  16. #else // ifdef WINDOWS10_ICU
  17. // Normalize ICU_VERSION for non-Kit ICU
  18. #ifndef ICU_VERSION
  19. #include "unicode/uvernum.h"
  20. #define ICU_VERSION U_ICU_VERSION_MAJOR_NUM
  21. #endif
  22. // Make non-Windows Kit ICU look and act like Windows Kit ICU for better compat
  23. #define U_SHOW_CPLUSPLUS_API 0
  24. // ICU 55 (Ubuntu 16.04 system default) has uloc_toUnicodeLocale* marked as draft, which is required for Intl
  25. #if ICU_VERSION > 56
  26. #define U_DEFAULT_SHOW_DRAFT 0
  27. #define U_HIDE_DRAFT_API 1
  28. #endif
  29. #define U_HIDE_DEPRECATED_API 1
  30. #define U_HIDE_OBSOLETE_API 1
  31. #define U_HIDE_INTERNAL_API 1
  32. #include "unicode/ucal.h"
  33. #include "unicode/uclean.h"
  34. #include "unicode/ucol.h"
  35. #include "unicode/udat.h"
  36. #include "unicode/udatpg.h"
  37. #include "unicode/uloc.h"
  38. #include "unicode/ulocdata.h"
  39. #include "unicode/unumsys.h"
  40. #include "unicode/ustring.h"
  41. #include "unicode/unorm2.h"
  42. #include "unicode/upluralrules.h"
  43. #endif // ifdef WINDOWS10_ICU
  44. // Use PAL wrappers for Linux arm64 to fix wchar_t/char16_t mismatches.
  45. // Cannot go before system unicode headers - here is the earliest
  46. // possible point to override these.
  47. #if defined(_ARM64_) && defined(__linux__)
  48. #define wcschr PAL_wcschr
  49. #define wcscmp PAL_wcscmp
  50. #define wcslen PAL_wcslen
  51. #define wcsncmp PAL_wcsncmp
  52. #define wcsrchr PAL_wcsrchr
  53. #define wcsstr PAL_wcsstr
  54. #define wmemcmp PAL_wmemcmp
  55. #define wprintf PAL_wprintf
  56. #endif
  57. // Different assertion code is used in ChakraFull that enforces that messages are char literals
  58. #ifdef _CHAKRACOREBUILD
  59. #define ICU_ERRORMESSAGE(e) u_errorName(e)
  60. #else
  61. #define ICU_ERRORMESSAGE(e) "Bad status returned from ICU"
  62. #endif
  63. #define ICU_FAILURE(e) (U_FAILURE(e) || e == U_STRING_NOT_TERMINATED_WARNING)
  64. #define ICU_BUFFER_FAILURE(e) (e == U_BUFFER_OVERFLOW_ERROR || e == U_STRING_NOT_TERMINATED_WARNING)
  65. namespace PlatformAgnostic
  66. {
  67. namespace ICUHelpers
  68. {
  69. template<typename TObject, void(__cdecl * CloseFunction)(TObject)>
  70. class ScopedICUObject
  71. {
  72. private:
  73. TObject object;
  74. public:
  75. explicit ScopedICUObject(TObject object) : object(object)
  76. {
  77. }
  78. ScopedICUObject(const ScopedICUObject &other) = delete;
  79. ScopedICUObject(ScopedICUObject &&other) = delete;
  80. ScopedICUObject &operator=(const ScopedICUObject &other) = delete;
  81. ScopedICUObject &operator=(ScopedICUObject &&other) = delete;
  82. ~ScopedICUObject()
  83. {
  84. if (object != nullptr)
  85. {
  86. CloseFunction(object);
  87. }
  88. }
  89. operator TObject()
  90. {
  91. return object;
  92. }
  93. };
  94. typedef ScopedICUObject<UEnumeration *, uenum_close> ScopedUEnumeration;
  95. typedef ScopedICUObject<UCollator *, ucol_close> ScopedUCollator;
  96. typedef ScopedICUObject<UDateFormat *, udat_close> ScopedUDateFormat;
  97. typedef ScopedICUObject<UNumberFormat *, unum_close> ScopedUNumberFormat;
  98. typedef ScopedICUObject<UNumberingSystem *, unumsys_close> ScopedUNumberingSystem;
  99. typedef ScopedICUObject<UDateTimePatternGenerator *, udatpg_close> ScopedUDateTimePatternGenerator;
  100. typedef ScopedICUObject<UFieldPositionIterator *, ufieldpositer_close> ScopedUFieldPositionIterator;
  101. inline int GetICUMajorVersion()
  102. {
  103. UVersionInfo version = { 0 };
  104. u_getVersion(version);
  105. return version[0];
  106. }
  107. }
  108. }
  109. #endif // ifdef HAS_ICU