ChakraICU.h 4.2 KB

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