ChakraICU.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // Different assertion code is used in ChakraFull that enforces that messages are char literals
  45. #ifdef _CHAKRACOREBUILD
  46. #define ICU_ERRORMESSAGE(e) u_errorName(e)
  47. #else
  48. #define ICU_ERRORMESSAGE(e) "Bad status returned from ICU"
  49. #endif
  50. #define ICU_FAILURE(e) (U_FAILURE(e) || e == U_STRING_NOT_TERMINATED_WARNING)
  51. #define ICU_BUFFER_FAILURE(e) (e == U_BUFFER_OVERFLOW_ERROR || e == U_STRING_NOT_TERMINATED_WARNING)
  52. namespace PlatformAgnostic
  53. {
  54. namespace ICUHelpers
  55. {
  56. template<typename TObject, void(__cdecl * CloseFunction)(TObject)>
  57. class ScopedICUObject
  58. {
  59. private:
  60. TObject object;
  61. public:
  62. explicit ScopedICUObject(TObject object) : object(object)
  63. {
  64. }
  65. ScopedICUObject(const ScopedICUObject &other) = delete;
  66. ScopedICUObject(ScopedICUObject &&other) = delete;
  67. ScopedICUObject &operator=(const ScopedICUObject &other) = delete;
  68. ScopedICUObject &operator=(ScopedICUObject &&other) = delete;
  69. ~ScopedICUObject()
  70. {
  71. if (object != nullptr)
  72. {
  73. CloseFunction(object);
  74. }
  75. }
  76. operator TObject()
  77. {
  78. return object;
  79. }
  80. };
  81. typedef ScopedICUObject<UEnumeration *, uenum_close> ScopedUEnumeration;
  82. typedef ScopedICUObject<UCollator *, ucol_close> ScopedUCollator;
  83. typedef ScopedICUObject<UDateFormat *, udat_close> ScopedUDateFormat;
  84. typedef ScopedICUObject<UNumberFormat *, unum_close> ScopedUNumberFormat;
  85. typedef ScopedICUObject<UNumberingSystem *, unumsys_close> ScopedUNumberingSystem;
  86. typedef ScopedICUObject<UDateTimePatternGenerator *, udatpg_close> ScopedUDateTimePatternGenerator;
  87. typedef ScopedICUObject<UFieldPositionIterator *, ufieldpositer_close> ScopedUFieldPositionIterator;
  88. inline int GetICUMajorVersion()
  89. {
  90. UVersionInfo version = { 0 };
  91. u_getVersion(version);
  92. return version[0];
  93. }
  94. }
  95. }
  96. #endif // ifdef HAS_ICU