| 1 | // Copyright (C) 1997-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // Under Section 7 of GPL version 3, you are granted additional |
| 15 | // permissions described in the GCC Runtime Library Exception, version |
| 16 | // 3.1, as published by the Free Software Foundation. |
| 17 | |
| 18 | // You should have received a copy of the GNU General Public License and |
| 19 | // a copy of the GCC Runtime Library Exception along with this program; |
| 20 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 21 | // <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | #if __cplusplus != 201103L |
| 24 | # error This file must be compiled as C++11 |
| 25 | #endif |
| 26 | |
| 27 | #define _GLIBCXX_USE_CXX11_ABI 1 |
| 28 | #include <clocale> |
| 29 | #include <cstring> |
| 30 | #include <cstdlib> |
| 31 | #include <locale> |
| 32 | |
| 33 | namespace std _GLIBCXX_VISIBILITY(default) |
| 34 | { |
| 35 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 36 | |
| 37 | using namespace __gnu_cxx; |
| 38 | |
| 39 | static inline bool |
| 40 | is_C_locale(const char* s) |
| 41 | { |
| 42 | switch (s[0]) |
| 43 | { |
| 44 | case 'C': |
| 45 | return s[1] == '\0'; |
| 46 | case 'P': |
| 47 | return !std::strcmp(s+1, "OSIX"); |
| 48 | default: |
| 49 | return false; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | locale::locale(const char* __s) : _M_impl(0) |
| 54 | { |
| 55 | if (__s) |
| 56 | { |
| 57 | _S_initialize(); |
| 58 | if (is_C_locale(__s)) |
| 59 | (_M_impl = _S_classic)->_M_add_reference(); |
| 60 | else if (*__s) |
| 61 | _M_impl = new _Impl(__s, 1); |
| 62 | else |
| 63 | { |
| 64 | // Get it from the environment. |
| 65 | char* __env = std::getenv("LC_ALL"); |
| 66 | // If LC_ALL is set we are done. |
| 67 | if (__env && *__env) |
| 68 | { |
| 69 | if (is_C_locale(__env)) |
| 70 | (_M_impl = _S_classic)->_M_add_reference(); |
| 71 | else |
| 72 | _M_impl = new _Impl(__env, 1); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | // LANG may set a default different from "C". |
| 77 | string __lang; |
| 78 | __env = std::getenv("LANG"); |
| 79 | if (!__env || !*__env || is_C_locale(__env)) |
| 80 | __lang = "C"; |
| 81 | else |
| 82 | __lang = __env; |
| 83 | |
| 84 | // Scan the categories looking for the first one |
| 85 | // different from LANG. |
| 86 | size_t __i = 0; |
| 87 | if (__lang == "C") |
| 88 | for (; __i < _S_categories_size; ++__i) |
| 89 | { |
| 90 | __env = std::getenv(_S_categories[__i]); |
| 91 | if (__env && *__env && !is_C_locale(__env)) |
| 92 | break; |
| 93 | } |
| 94 | else |
| 95 | for (; __i < _S_categories_size; ++__i) |
| 96 | { |
| 97 | __env = std::getenv(_S_categories[__i]); |
| 98 | if (__env && *__env && __lang != __env) |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | // If one is found, build the complete string of |
| 103 | // the form LC_CTYPE=xxx;LC_NUMERIC=yyy; and so on... |
| 104 | if (__i < _S_categories_size) |
| 105 | { |
| 106 | string __str; |
| 107 | __str.reserve(128); |
| 108 | for (size_t __j = 0; __j < __i; ++__j) |
| 109 | { |
| 110 | __str += _S_categories[__j]; |
| 111 | __str += '='; |
| 112 | __str += __lang; |
| 113 | __str += ';'; |
| 114 | } |
| 115 | __str += _S_categories[__i]; |
| 116 | __str += '='; |
| 117 | __str += __env; |
| 118 | __str += ';'; |
| 119 | ++__i; |
| 120 | for (; __i < _S_categories_size; ++__i) |
| 121 | { |
| 122 | __env = std::getenv(_S_categories[__i]); |
| 123 | __str += _S_categories[__i]; |
| 124 | if (!__env || !*__env) |
| 125 | { |
| 126 | __str += '='; |
| 127 | __str += __lang; |
| 128 | __str += ';'; |
| 129 | } |
| 130 | else if (is_C_locale(__env)) |
| 131 | __str += "=C;"; |
| 132 | else |
| 133 | { |
| 134 | __str += '='; |
| 135 | __str += __env; |
| 136 | __str += ';'; |
| 137 | } |
| 138 | } |
| 139 | __str.erase(__str.end() - 1); |
| 140 | _M_impl = new _Impl(__str.c_str(), 1); |
| 141 | } |
| 142 | // ... otherwise either an additional instance of |
| 143 | // the "C" locale or LANG. |
| 144 | else if (__lang == "C") |
| 145 | (_M_impl = _S_classic)->_M_add_reference(); |
| 146 | else |
| 147 | _M_impl = new _Impl(__lang.c_str(), 1); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | else |
| 152 | __throw_runtime_error(__N("locale::locale null not valid")); |
| 153 | } |
| 154 | |
| 155 | locale::locale(const locale& __base, const char* __s, category __cat) |
| 156 | : _M_impl(0) |
| 157 | { |
| 158 | // NB: There are complicated, yet more efficient ways to do |
| 159 | // this. Building up locales on a per-category way is tedious, so |
| 160 | // let's do it this way until people complain. |
| 161 | locale __add(__s); |
| 162 | _M_coalesce(__base, __add, __cat); |
| 163 | } |
| 164 | |
| 165 | locale::locale(const locale& __base, const locale& __add, category __cat) |
| 166 | : _M_impl(0) |
| 167 | { _M_coalesce(__base, __add, __cat); } |
| 168 | |
| 169 | void |
| 170 | locale::_M_coalesce(const locale& __base, const locale& __add, |
| 171 | category __cat) |
| 172 | { |
| 173 | __cat = _S_normalize_category(__cat); |
| 174 | _M_impl = new _Impl(*__base._M_impl, 1); |
| 175 | |
| 176 | __try |
| 177 | { _M_impl->_M_replace_categories(__add._M_impl, __cat); } |
| 178 | __catch(...) |
| 179 | { |
| 180 | _M_impl->_M_remove_reference(); |
| 181 | __throw_exception_again; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | const int num_facets = ( |
| 186 | _GLIBCXX_NUM_FACETS + _GLIBCXX_NUM_CXX11_FACETS |
| 187 | #ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT |
| 188 | + _GLIBCXX_NUM_LBDL_ALT128_FACETS |
| 189 | #endif |
| 190 | ) |
| 191 | #ifdef _GLIBCXX_USE_WCHAR_T |
| 192 | * 2 |
| 193 | #endif |
| 194 | + _GLIBCXX_NUM_UNICODE_FACETS; |
| 195 | |
| 196 | // Construct named _Impl. |
| 197 | locale::_Impl:: |
| 198 | _Impl(const char* __s, size_t __refs) |
| 199 | : _M_refcount(__refs), _M_facets(0), _M_facets_size(num_facets), |
| 200 | _M_caches(0), _M_names(0) |
| 201 | { |
| 202 | // Initialize the underlying locale model, which also checks to |
| 203 | // see if the given name is valid. |
| 204 | __c_locale __cloc; |
| 205 | locale::facet::_S_create_c_locale(__cloc, __s); |
| 206 | __c_locale __clocm = __cloc; |
| 207 | |
| 208 | __try |
| 209 | { |
| 210 | _M_facets = new const facet*[_M_facets_size](); |
| 211 | _M_caches = new const facet*[_M_facets_size](); |
| 212 | _M_names = new char*[_S_categories_size](); |
| 213 | |
| 214 | // Name the categories. |
| 215 | const char* __smon = __s; |
| 216 | const size_t __len = std::strlen(__s); |
| 217 | if (!std::memchr(__s, ';', __len)) |
| 218 | { |
| 219 | _M_names[0] = new char[__len + 1]; |
| 220 | std::memcpy(_M_names[0], __s, __len + 1); |
| 221 | } |
| 222 | else |
| 223 | { |
| 224 | const char* __end = __s; |
| 225 | bool __found_ctype = false; |
| 226 | bool __found_monetary = false; |
| 227 | size_t __ci = 0, __mi = 0; |
| 228 | for (size_t __i = 0; __i < _S_categories_size; ++__i) |
| 229 | { |
| 230 | const char* __beg = std::strchr(__end + 1, '=') + 1; |
| 231 | __end = std::strchr(__beg, ';'); |
| 232 | if (!__end) |
| 233 | __end = __s + __len; |
| 234 | _M_names[__i] = new char[__end - __beg + 1]; |
| 235 | std::memcpy(_M_names[__i], __beg, __end - __beg); |
| 236 | _M_names[__i][__end - __beg] = '\0'; |
| 237 | if (!__found_ctype |
| 238 | && *(__beg - 2) == 'E' && *(__beg - 3) == 'P') |
| 239 | { |
| 240 | __found_ctype = true; |
| 241 | __ci = __i; |
| 242 | } |
| 243 | else if (!__found_monetary && *(__beg - 2) == 'Y') |
| 244 | { |
| 245 | __found_monetary = true; |
| 246 | __mi = __i; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | if (std::strcmp(_M_names[__ci], _M_names[__mi])) |
| 251 | { |
| 252 | __smon = _M_names[__mi]; |
| 253 | __clocm = locale::facet::_S_lc_ctype_c_locale(__cloc, |
| 254 | __smon); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | // Construct all standard facets and add them to _M_facets. |
| 259 | _M_init_facet(new std::ctype<char>(__cloc, 0, false)); |
| 260 | _M_init_facet(new codecvt<char, char, mbstate_t>(__cloc)); |
| 261 | _M_init_facet(new numpunct<char>(__cloc)); |
| 262 | _M_init_facet(new num_get<char>); |
| 263 | _M_init_facet(new num_put<char>); |
| 264 | _M_init_facet(new std::collate<char>(__cloc)); |
| 265 | _M_init_facet(new moneypunct<char, false>(__cloc, 0)); |
| 266 | _M_init_facet(new moneypunct<char, true>(__cloc, 0)); |
| 267 | _M_init_facet(new money_get<char>); |
| 268 | _M_init_facet(new money_put<char>); |
| 269 | _M_init_facet(new __timepunct<char>(__cloc, __s)); |
| 270 | _M_init_facet(new time_get<char>); |
| 271 | _M_init_facet(new time_put<char>); |
| 272 | _M_init_facet(new std::messages<char>(__cloc, __s)); |
| 273 | |
| 274 | #ifdef _GLIBCXX_USE_WCHAR_T |
| 275 | _M_init_facet(new std::ctype<wchar_t>(__cloc)); |
| 276 | _M_init_facet(new codecvt<wchar_t, char, mbstate_t>(__cloc)); |
| 277 | _M_init_facet(new numpunct<wchar_t>(__cloc)); |
| 278 | _M_init_facet(new num_get<wchar_t>); |
| 279 | _M_init_facet(new num_put<wchar_t>); |
| 280 | _M_init_facet(new std::collate<wchar_t>(__cloc)); |
| 281 | _M_init_facet(new moneypunct<wchar_t, false>(__clocm, __smon)); |
| 282 | _M_init_facet(new moneypunct<wchar_t, true>(__clocm, __smon)); |
| 283 | _M_init_facet(new money_get<wchar_t>); |
| 284 | _M_init_facet(new money_put<wchar_t>); |
| 285 | _M_init_facet(new __timepunct<wchar_t>(__cloc, __s)); |
| 286 | _M_init_facet(new time_get<wchar_t>); |
| 287 | _M_init_facet(new time_put<wchar_t>); |
| 288 | _M_init_facet(new std::messages<wchar_t>(__cloc, __s)); |
| 289 | #endif |
| 290 | |
| 291 | #if _GLIBCXX_NUM_UNICODE_FACETS != 0 |
| 292 | _M_init_facet(new codecvt<char16_t, char, mbstate_t>); |
| 293 | _M_init_facet(new codecvt<char32_t, char, mbstate_t>); |
| 294 | |
| 295 | #ifdef _GLIBCXX_USE_CHAR8_T |
| 296 | _M_init_facet(new codecvt<char16_t, char8_t, mbstate_t>); |
| 297 | _M_init_facet(new codecvt<char32_t, char8_t, mbstate_t>); |
| 298 | #endif |
| 299 | |
| 300 | #endif |
| 301 | |
| 302 | #if _GLIBCXX_USE_DUAL_ABI |
| 303 | _M_init_extra(&__cloc, &__clocm, __s, __smon); |
| 304 | #endif |
| 305 | |
| 306 | #ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT |
| 307 | _M_init_extra_ldbl128(false); |
| 308 | #endif |
| 309 | |
| 310 | locale::facet::_S_destroy_c_locale(__cloc); |
| 311 | if (__clocm != __cloc) |
| 312 | locale::facet::_S_destroy_c_locale(__clocm); |
| 313 | } |
| 314 | __catch(...) |
| 315 | { |
| 316 | locale::facet::_S_destroy_c_locale(__cloc); |
| 317 | if (__clocm != __cloc) |
| 318 | locale::facet::_S_destroy_c_locale(__clocm); |
| 319 | this->~_Impl(); |
| 320 | __throw_exception_again; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | void |
| 325 | locale::_Impl:: |
| 326 | _M_replace_categories(const _Impl* __imp, category __cat) |
| 327 | { |
| 328 | category __mask = 1; |
| 329 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 330 | // 3676. Name of locale composed using std::locale::none |
| 331 | if (!_M_names[0] || (__cat != none && !__imp->_M_names[0])) |
| 332 | { |
| 333 | if (_M_names[0]) |
| 334 | { |
| 335 | delete [] _M_names[0]; |
| 336 | _M_names[0] = 0; // Unnamed. |
| 337 | } |
| 338 | |
| 339 | for (size_t __ix = 0; __ix < _S_categories_size; ++__ix, __mask <<= 1) |
| 340 | { |
| 341 | if (__mask & __cat) |
| 342 | // Need to replace entry in _M_facets with other locale's info. |
| 343 | _M_replace_category(__imp, _S_facet_categories[__ix]); |
| 344 | } |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | if (!_M_names[1]) |
| 349 | { |
| 350 | // A full set of _M_names must be prepared, all identical |
| 351 | // to _M_names[0] to begin with. Then, below, a few will |
| 352 | // be replaced by the corresponding __imp->_M_names. I.e., |
| 353 | // not a "simple" locale anymore (see locale::operator==). |
| 354 | const size_t __len = std::strlen(_M_names[0]) + 1; |
| 355 | for (size_t __i = 1; __i < _S_categories_size; ++__i) |
| 356 | { |
| 357 | _M_names[__i] = new char[__len]; |
| 358 | std::memcpy(_M_names[__i], _M_names[0], __len); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | for (size_t __ix = 0; __ix < _S_categories_size; ++__ix, __mask <<= 1) |
| 363 | { |
| 364 | if (__mask & __cat) |
| 365 | { |
| 366 | // Need to replace entry in _M_facets with other locale's info. |
| 367 | _M_replace_category(__imp, _S_facet_categories[__ix]); |
| 368 | |
| 369 | // FIXME: Hack for libstdc++/29217: the numerical encodings |
| 370 | // of the time and collate categories are swapped vs the |
| 371 | // order of the names in locale::_S_categories. We'd like to |
| 372 | // adjust the former (the latter is dictated by compatibility |
| 373 | // with glibc) but we can't for binary compatibility. |
| 374 | size_t __ix_name = __ix; |
| 375 | if (__ix == 2 || __ix == 3) |
| 376 | __ix_name = 5 - __ix; |
| 377 | |
| 378 | char* __src = __imp->_M_names[__ix_name] ? |
| 379 | __imp->_M_names[__ix_name] : __imp->_M_names[0]; |
| 380 | const size_t __len = std::strlen(__src) + 1; |
| 381 | char* __new = new char[__len]; |
| 382 | std::memcpy(__new, __src, __len); |
| 383 | delete [] _M_names[__ix_name]; |
| 384 | _M_names[__ix_name] = __new; |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | _GLIBCXX_END_NAMESPACE_VERSION |
| 391 | } // namespace |