57-locale.patch 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. diff --git a/src/locale.cpp b/src/locale.cpp
  2. index 7fdd5be..1b937dc 100644
  3. --- a/src/locale.cpp
  4. +++ b/src/locale.cpp
  5. @@ -38,8 +38,8 @@
  6. # include <langinfo.h>
  7. #endif
  8. -#include "include/atomic_support.h"
  9. #include "include/sso_allocator.h"
  10. +#include <mutex>
  11. // On Linux, wint_t and wchar_t have different signed-ness, and this causes
  12. // lots of noise in the build log, but no bugs that I know of.
  13. @@ -593,8 +593,17 @@ void locale::facet::__on_zero_shared() noexcept { delete this; }
  14. constinit int32_t locale::id::__next_id = 0;
  15. long locale::id::__get() {
  16. - call_once(__flag_, [&] { __id_ = __libcpp_atomic_add(&__next_id, 1); });
  17. - return __id_ - 1;
  18. + int32_t result = __id_.load(std::memory_order_acquire);
  19. + if (result == 0) {
  20. + static std::mutex m;
  21. + std::lock_guard<std::mutex> guard(m);
  22. + result = __id_.load(std::memory_order_acquire);
  23. + if (result == 0) {
  24. + result = ++__next_id;
  25. + __id_.store(result, std::memory_order_release);
  26. + }
  27. + }
  28. + return result - 1;
  29. }
  30. // template <> class collate_byname<char>
  31. @@ -895,6 +904,11 @@ extern "C" const int** __ctype_tolower_loc();
  32. extern "C" const int** __ctype_toupper_loc();
  33. #endif
  34. +#if defined(__ANDROID__)
  35. +// See src/support/android/android_locale.cpp
  36. +extern "C" const unsigned short* const _ctype_android;
  37. +#endif
  38. +
  39. #ifdef _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE
  40. const ctype<char>::mask* ctype<char>::classic_table() noexcept {
  41. // clang-format off
  42. @@ -990,6 +1004,8 @@ const ctype<char>::mask* ctype<char>::classic_table() noexcept {
  43. # elif defined(_NEWLIB_VERSION)
  44. // Newlib has a 257-entry table in ctype_.c, where (char)0 starts at [1].
  45. return _ctype_ + 1;
  46. +# elif defined(__ANDROID__)
  47. + return _ctype_android;
  48. # elif defined(_AIX)
  49. return (const unsigned int*)__lc_ctype_ptr->obj->mask;
  50. # elif defined(__MVS__)
  51. @@ -3644,15 +3660,25 @@ __codecvt_utf16<char32_t, true>::result __codecvt_utf16<char32_t, true>::do_out(
  52. extern_type* to,
  53. extern_type* to_end,
  54. extern_type*& to_nxt) const {
  55. +#if defined(_LIBCPP_SHORT_WCHAR)
  56. + const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm);
  57. + const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end);
  58. + const uint16_t* _frm_nxt = _frm;
  59. +#else
  60. const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
  61. const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
  62. const uint32_t* _frm_nxt = _frm;
  63. - uint8_t* _to = reinterpret_cast<uint8_t*>(to);
  64. - uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
  65. - uint8_t* _to_nxt = _to;
  66. - result r = ucs4_to_utf16le(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, __maxcode_, __mode_);
  67. - frm_nxt = frm + (_frm_nxt - _frm);
  68. - to_nxt = to + (_to_nxt - _to);
  69. +#endif
  70. + uint8_t* _to = reinterpret_cast<uint8_t*>(to);
  71. + uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
  72. + uint8_t* _to_nxt = _to;
  73. +#if defined(_LIBCPP_SHORT_WCHAR)
  74. + result r = ucs2_to_utf16le(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, __maxcode_, __mode_);
  75. +#else
  76. + result r = ucs4_to_utf16le(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, __maxcode_, __mode_);
  77. +#endif
  78. + frm_nxt = frm + (_frm_nxt - _frm);
  79. + to_nxt = to + (_to_nxt - _to);
  80. return r;
  81. }
  82. @@ -3712,7 +3738,7 @@ __codecvt_utf8_utf16<wchar_t>::result __codecvt_utf8_utf16<wchar_t>::do_out(
  83. extern_type* to,
  84. extern_type* to_end,
  85. extern_type*& to_nxt) const {
  86. -# if defined(_LIBCPP_SHORT_WCHAR)
  87. +# ifdef _WIN32
  88. const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm);
  89. const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end);
  90. const uint16_t* _frm_nxt = _frm;
  91. @@ -3741,7 +3767,7 @@ __codecvt_utf8_utf16<wchar_t>::result __codecvt_utf8_utf16<wchar_t>::do_in(
  92. const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
  93. const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
  94. const uint8_t* _frm_nxt = _frm;
  95. -# if defined(_LIBCPP_SHORT_WCHAR)
  96. +# ifdef _WIN32
  97. uint16_t* _to = reinterpret_cast<uint16_t*>(to);
  98. uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end);
  99. uint16_t* _to_nxt = _to;
  100. @@ -3880,12 +3906,18 @@ __codecvt_utf8_utf16<char32_t>::result __codecvt_utf8_utf16<char32_t>::do_in(
  101. const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
  102. const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
  103. const uint8_t* _frm_nxt = _frm;
  104. - uint32_t* _to = reinterpret_cast<uint32_t*>(to);
  105. - uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
  106. - uint32_t* _to_nxt = _to;
  107. - result r = utf8_to_utf16(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, __maxcode_, __mode_);
  108. - frm_nxt = frm + (_frm_nxt - _frm);
  109. - to_nxt = to + (_to_nxt - _to);
  110. +#if defined(_LIBCPP_SHORT_WCHAR)
  111. + uint16_t* _to = reinterpret_cast<uint16_t*>(to);
  112. + uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end);
  113. + uint16_t* _to_nxt = _to;
  114. +#else
  115. + uint32_t* _to = reinterpret_cast<uint32_t*>(to);
  116. + uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
  117. + uint32_t* _to_nxt = _to;
  118. +#endif
  119. + result r = utf8_to_utf16(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, __maxcode_, __mode_);
  120. + frm_nxt = frm + (_frm_nxt - _frm);
  121. + to_nxt = to + (_to_nxt - _to);
  122. return r;
  123. }
  124. @@ -5123,7 +5155,7 @@ static void __init_pat(
  125. // We insert the space into the symbol instead of
  126. // setting pat.field[2]=space so that when
  127. // showbase is not set, the space goes away too.
  128. - __curr_symbol_.insert(0, 1, space_char);
  129. + __curr_symbol_.insert((size_t)0, 1, space_char);
  130. }
  131. return;
  132. default:
  133. @@ -5145,7 +5177,7 @@ static void __init_pat(
  134. // We insert the space into the symbol instead of
  135. // setting pat.field[2]=space so that when
  136. // showbase is not set, the space goes away too.
  137. - __curr_symbol_.insert(0, 1, space_char);
  138. + __curr_symbol_.insert((size_t)0, 1, space_char);
  139. }
  140. return;
  141. case 2: // Space between sign and currency or value.
  142. @@ -5174,7 +5206,7 @@ static void __init_pat(
  143. // We insert the space into the symbol instead of
  144. // setting pat.field[1]=space so that when
  145. // showbase is not set, the space goes away too.
  146. - __curr_symbol_.insert(0, 1, space_char);
  147. + __curr_symbol_.insert((size_t)0, 1, space_char);
  148. }
  149. pat.field[1] = none;
  150. pat.field[2] = symbol;
  151. @@ -5216,7 +5248,7 @@ static void __init_pat(
  152. // We insert the space into the symbol instead of
  153. // setting pat.field[2]=space so that when
  154. // showbase is not set, the space goes away too.
  155. - __curr_symbol_.insert(0, 1, space_char);
  156. + __curr_symbol_.insert((size_t)0, 1, space_char);
  157. }
  158. return;
  159. default:
  160. @@ -5238,7 +5270,7 @@ static void __init_pat(
  161. // We insert the space into the symbol instead of
  162. // setting pat.field[1]=space so that when
  163. // showbase is not set, the space goes away too.
  164. - __curr_symbol_.insert(0, 1, space_char);
  165. + __curr_symbol_.insert((size_t)0, 1, space_char);
  166. }
  167. return;
  168. case 2: // Space between sign and currency or value.
  169. @@ -5277,7 +5309,7 @@ static void __init_pat(
  170. // We insert the space into the symbol instead of
  171. // setting pat.field[2]=space so that when
  172. // showbase is not set, the space goes away too.
  173. - __curr_symbol_.insert(0, 1, space_char);
  174. + __curr_symbol_.insert((size_t)0, 1, space_char);
  175. }
  176. return;
  177. default: