| 1 | // Locale support -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 1997-2024 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // Under Section 7 of GPL version 3, you are granted additional |
| 17 | // permissions described in the GCC Runtime Library Exception, version |
| 18 | // 3.1, as published by the Free Software Foundation. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License and |
| 21 | // a copy of the GCC Runtime Library Exception along with this program; |
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 | // <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | /** @file bits/locale_classes.h |
| 26 | * This is an internal header file, included by other library headers. |
| 27 | * Do not attempt to use it directly. @headername{locale} |
| 28 | */ |
| 29 | |
| 30 | // |
| 31 | // ISO C++ 14882: 22.1 Locales |
| 32 | // |
| 33 | |
| 34 | #ifndef _LOCALE_CLASSES_H |
| 35 | #define _LOCALE_CLASSES_H 1 |
| 36 | |
| 37 | #pragma GCC system_header |
| 38 | |
| 39 | #include <bits/localefwd.h> |
| 40 | #include <string> |
| 41 | #include <ext/atomicity.h> |
| 42 | |
| 43 | #ifdef __glibcxx_text_encoding |
| 44 | #include <text_encoding> |
| 45 | #endif |
| 46 | |
| 47 | namespace std _GLIBCXX_VISIBILITY(default) |
| 48 | { |
| 49 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 50 | |
| 51 | // 22.1.1 Class locale |
| 52 | /** |
| 53 | * @brief Container class for localization functionality. |
| 54 | * @ingroup locales |
| 55 | * |
| 56 | * The locale class is first a class wrapper for C library locales. It is |
| 57 | * also an extensible container for user-defined localization. A locale is |
| 58 | * a collection of facets that implement various localization features such |
| 59 | * as money, time, and number printing. |
| 60 | * |
| 61 | * Constructing C++ locales does not change the C library locale. |
| 62 | * |
| 63 | * This library supports efficient construction and copying of locales |
| 64 | * through a reference counting implementation of the locale class. |
| 65 | */ |
| 66 | class locale |
| 67 | { |
| 68 | public: |
| 69 | // Types: |
| 70 | /// Definition of locale::category. |
| 71 | typedef int category; |
| 72 | |
| 73 | // Forward decls and friends: |
| 74 | class facet; |
| 75 | class id; |
| 76 | class _Impl; |
| 77 | |
| 78 | friend class facet; |
| 79 | friend class _Impl; |
| 80 | |
| 81 | template<typename _Facet> |
| 82 | friend bool |
| 83 | has_facet(const locale&) throw(); |
| 84 | |
| 85 | template<typename _Facet> |
| 86 | friend const _Facet& |
| 87 | use_facet(const locale&); |
| 88 | |
| 89 | template<typename _Facet> |
| 90 | friend const _Facet* |
| 91 | __try_use_facet(const locale&) _GLIBCXX_NOTHROW; |
| 92 | |
| 93 | template<typename _Cache> |
| 94 | friend struct __use_cache; |
| 95 | |
| 96 | ///@{ |
| 97 | /** |
| 98 | * @brief Category values. |
| 99 | * |
| 100 | * The standard category values are none, ctype, numeric, collate, time, |
| 101 | * monetary, and messages. They form a bitmask that supports union and |
| 102 | * intersection. The category all is the union of these values. |
| 103 | * |
| 104 | * NB: Order must match _S_facet_categories definition in locale.cc |
| 105 | */ |
| 106 | static const category none = 0; |
| 107 | static const category ctype = 1L << 0; |
| 108 | static const category numeric = 1L << 1; |
| 109 | static const category collate = 1L << 2; |
| 110 | static const category time = 1L << 3; |
| 111 | static const category monetary = 1L << 4; |
| 112 | static const category messages = 1L << 5; |
| 113 | static const category all = (ctype | numeric | collate | |
| 114 | time | monetary | messages); |
| 115 | ///@} |
| 116 | |
| 117 | // Construct/copy/destroy: |
| 118 | |
| 119 | /** |
| 120 | * @brief Default constructor. |
| 121 | * |
| 122 | * Constructs a copy of the global locale. If no locale has been |
| 123 | * explicitly set, this is the C locale. |
| 124 | */ |
| 125 | locale() throw(); |
| 126 | |
| 127 | /** |
| 128 | * @brief Copy constructor. |
| 129 | * |
| 130 | * Constructs a copy of @a other. |
| 131 | * |
| 132 | * @param __other The locale to copy. |
| 133 | */ |
| 134 | locale(const locale& __other) throw(); |
| 135 | |
| 136 | /** |
| 137 | * @brief Named locale constructor. |
| 138 | * |
| 139 | * Constructs a copy of the named C library locale. |
| 140 | * |
| 141 | * @param __s Name of the locale to construct. |
| 142 | * @throw std::runtime_error if __s is null or an undefined locale. |
| 143 | */ |
| 144 | explicit |
| 145 | locale(const char* __s); |
| 146 | |
| 147 | /** |
| 148 | * @brief Construct locale with facets from another locale. |
| 149 | * |
| 150 | * Constructs a copy of the locale @a base. The facets specified by @a |
| 151 | * cat are replaced with those from the locale named by @a s. If base is |
| 152 | * named, this locale instance will also be named. |
| 153 | * |
| 154 | * @param __base The locale to copy. |
| 155 | * @param __s Name of the locale to use facets from. |
| 156 | * @param __cat Set of categories defining the facets to use from __s. |
| 157 | * @throw std::runtime_error if __s is null or an undefined locale. |
| 158 | */ |
| 159 | locale(const locale& __base, const char* __s, category __cat); |
| 160 | |
| 161 | #if __cplusplus >= 201103L |
| 162 | /** |
| 163 | * @brief Named locale constructor. |
| 164 | * |
| 165 | * Constructs a copy of the named C library locale. |
| 166 | * |
| 167 | * @param __s Name of the locale to construct. |
| 168 | * @throw std::runtime_error if __s is an undefined locale. |
| 169 | */ |
| 170 | explicit |
| 171 | locale(const std::string& __s) : locale(__s.c_str()) { } |
| 172 | |
| 173 | /** |
| 174 | * @brief Construct locale with facets from another locale. |
| 175 | * |
| 176 | * Constructs a copy of the locale @a base. The facets specified by @a |
| 177 | * cat are replaced with those from the locale named by @a s. If base is |
| 178 | * named, this locale instance will also be named. |
| 179 | * |
| 180 | * @param __base The locale to copy. |
| 181 | * @param __s Name of the locale to use facets from. |
| 182 | * @param __cat Set of categories defining the facets to use from __s. |
| 183 | * @throw std::runtime_error if __s is an undefined locale. |
| 184 | */ |
| 185 | locale(const locale& __base, const std::string& __s, category __cat) |
| 186 | : locale(__base, __s.c_str(), __cat) { } |
| 187 | #endif |
| 188 | |
| 189 | /** |
| 190 | * @brief Construct locale with facets from another locale. |
| 191 | * |
| 192 | * Constructs a copy of the locale @a base. The facets specified by @a |
| 193 | * cat are replaced with those from the locale @a add. If @a base and @a |
| 194 | * add are named, this locale instance will also be named. |
| 195 | * |
| 196 | * @param __base The locale to copy. |
| 197 | * @param __add The locale to use facets from. |
| 198 | * @param __cat Set of categories defining the facets to use from add. |
| 199 | */ |
| 200 | locale(const locale& __base, const locale& __add, category __cat); |
| 201 | |
| 202 | /** |
| 203 | * @brief Construct locale with another facet. |
| 204 | * |
| 205 | * Constructs a copy of the locale @a __other. The facet @a __f |
| 206 | * is added to @a __other, replacing an existing facet of type |
| 207 | * Facet if there is one. If @a __f is null, this locale is a |
| 208 | * copy of @a __other. |
| 209 | * |
| 210 | * @param __other The locale to copy. |
| 211 | * @param __f The facet to add in. |
| 212 | */ |
| 213 | template<typename _Facet> |
| 214 | locale(const locale& __other, _Facet* __f); |
| 215 | |
| 216 | /// Locale destructor. |
| 217 | ~locale() throw(); |
| 218 | |
| 219 | /** |
| 220 | * @brief Assignment operator. |
| 221 | * |
| 222 | * Set this locale to be a copy of @a other. |
| 223 | * |
| 224 | * @param __other The locale to copy. |
| 225 | * @return A reference to this locale. |
| 226 | */ |
| 227 | const locale& |
| 228 | operator=(const locale& __other) throw(); |
| 229 | |
| 230 | /** |
| 231 | * @brief Construct locale with another facet. |
| 232 | * |
| 233 | * Constructs and returns a new copy of this locale. Adds or replaces an |
| 234 | * existing facet of type Facet from the locale @a other into the new |
| 235 | * locale. |
| 236 | * |
| 237 | * @tparam _Facet The facet type to copy from other |
| 238 | * @param __other The locale to copy from. |
| 239 | * @return Newly constructed locale. |
| 240 | * @throw std::runtime_error if __other has no facet of type _Facet. |
| 241 | */ |
| 242 | template<typename _Facet> |
| 243 | _GLIBCXX_NODISCARD |
| 244 | locale |
| 245 | combine(const locale& __other) const; |
| 246 | |
| 247 | // Locale operations: |
| 248 | /** |
| 249 | * @brief Return locale name. |
| 250 | * @return Locale name or "*" if unnamed. |
| 251 | */ |
| 252 | _GLIBCXX_NODISCARD _GLIBCXX_DEFAULT_ABI_TAG |
| 253 | string |
| 254 | name() const; |
| 255 | |
| 256 | #ifdef __glibcxx_text_encoding |
| 257 | # if __CHAR_BIT__ == 8 |
| 258 | text_encoding |
| 259 | encoding() const; |
| 260 | # else |
| 261 | text_encoding |
| 262 | encoding() const = delete; |
| 263 | # endif |
| 264 | #endif |
| 265 | |
| 266 | /** |
| 267 | * @brief Locale equality. |
| 268 | * |
| 269 | * @param __other The locale to compare against. |
| 270 | * @return True if other and this refer to the same locale instance, are |
| 271 | * copies, or have the same name. False otherwise. |
| 272 | */ |
| 273 | _GLIBCXX_NODISCARD |
| 274 | bool |
| 275 | operator==(const locale& __other) const throw(); |
| 276 | |
| 277 | #if __cpp_impl_three_way_comparison < 201907L |
| 278 | /** |
| 279 | * @brief Locale inequality. |
| 280 | * |
| 281 | * @param __other The locale to compare against. |
| 282 | * @return ! (*this == __other) |
| 283 | */ |
| 284 | _GLIBCXX_NODISCARD |
| 285 | bool |
| 286 | operator!=(const locale& __other) const throw() |
| 287 | { return !(this->operator==(__other)); } |
| 288 | #endif |
| 289 | |
| 290 | /** |
| 291 | * @brief Compare two strings according to collate. |
| 292 | * |
| 293 | * Template operator to compare two strings using the compare function of |
| 294 | * the collate facet in this locale. One use is to provide the locale to |
| 295 | * the sort function. For example, a vector v of strings could be sorted |
| 296 | * according to locale loc by doing: |
| 297 | * @code |
| 298 | * std::sort(v.begin(), v.end(), loc); |
| 299 | * @endcode |
| 300 | * |
| 301 | * @param __s1 First string to compare. |
| 302 | * @param __s2 Second string to compare. |
| 303 | * @return True if collate<_Char> facet compares __s1 < __s2, else false. |
| 304 | */ |
| 305 | template<typename _Char, typename _Traits, typename _Alloc> |
| 306 | _GLIBCXX_NODISCARD |
| 307 | bool |
| 308 | operator()(const basic_string<_Char, _Traits, _Alloc>& __s1, |
| 309 | const basic_string<_Char, _Traits, _Alloc>& __s2) const; |
| 310 | |
| 311 | // Global locale objects: |
| 312 | /** |
| 313 | * @brief Set global locale |
| 314 | * |
| 315 | * This function sets the global locale to the argument and returns a |
| 316 | * copy of the previous global locale. If the argument has a name, it |
| 317 | * will also call std::setlocale(LC_ALL, loc.name()). |
| 318 | * |
| 319 | * @param __loc The new locale to make global. |
| 320 | * @return Copy of the old global locale. |
| 321 | */ |
| 322 | static locale |
| 323 | global(const locale& __loc); |
| 324 | |
| 325 | /** |
| 326 | * @brief Return reference to the C locale. |
| 327 | */ |
| 328 | _GLIBCXX_NODISCARD |
| 329 | static const locale& |
| 330 | classic(); |
| 331 | |
| 332 | private: |
| 333 | // The (shared) implementation |
| 334 | _Impl* _M_impl; |
| 335 | |
| 336 | // The "C" reference locale |
| 337 | static _Impl* _S_classic; |
| 338 | |
| 339 | // Current global locale |
| 340 | static _Impl* _S_global; |
| 341 | |
| 342 | // Names of underlying locale categories. |
| 343 | // NB: locale::global() has to know how to modify all the |
| 344 | // underlying categories, not just the ones required by the C++ |
| 345 | // standard. |
| 346 | static const char* const* const _S_categories; |
| 347 | |
| 348 | // Number of standard categories. For C++, these categories are |
| 349 | // collate, ctype, monetary, numeric, time, and messages. These |
| 350 | // directly correspond to ISO C99 macros LC_COLLATE, LC_CTYPE, |
| 351 | // LC_MONETARY, LC_NUMERIC, and LC_TIME. In addition, POSIX (IEEE |
| 352 | // 1003.1-2001) specifies LC_MESSAGES. |
| 353 | // In addition to the standard categories, the underlying |
| 354 | // operating system is allowed to define extra LC_* |
| 355 | // macros. For GNU systems, the following are also valid: |
| 356 | // LC_PAPER, LC_NAME, LC_ADDRESS, LC_TELEPHONE, LC_MEASUREMENT, |
| 357 | // and LC_IDENTIFICATION. |
| 358 | enum { _S_categories_size = 6 + _GLIBCXX_NUM_CATEGORIES }; |
| 359 | |
| 360 | #ifdef __GTHREADS |
| 361 | static __gthread_once_t _S_once; |
| 362 | #endif |
| 363 | |
| 364 | explicit |
| 365 | locale(_Impl*) throw(); |
| 366 | |
| 367 | static void |
| 368 | _S_initialize(); |
| 369 | |
| 370 | static void |
| 371 | _S_initialize_once() throw(); |
| 372 | |
| 373 | static category |
| 374 | _S_normalize_category(category); |
| 375 | |
| 376 | void |
| 377 | _M_coalesce(const locale& __base, const locale& __add, category __cat); |
| 378 | |
| 379 | #if _GLIBCXX_USE_CXX11_ABI |
| 380 | static const id* const _S_twinned_facets[]; |
| 381 | #endif |
| 382 | }; |
| 383 | |
| 384 | |
| 385 | // 22.1.1.1.2 Class locale::facet |
| 386 | /** |
| 387 | * @brief Localization functionality base class. |
| 388 | * @ingroup locales |
| 389 | * |
| 390 | * The facet class is the base class for a localization feature, such as |
| 391 | * money, time, and number printing. It provides common support for facets |
| 392 | * and reference management. |
| 393 | * |
| 394 | * Facets may not be copied or assigned. |
| 395 | */ |
| 396 | class locale::facet |
| 397 | { |
| 398 | private: |
| 399 | friend class locale; |
| 400 | friend class locale::_Impl; |
| 401 | |
| 402 | mutable _Atomic_word _M_refcount; |
| 403 | |
| 404 | // Contains data from the underlying "C" library for the classic locale. |
| 405 | static __c_locale _S_c_locale; |
| 406 | |
| 407 | // String literal for the name of the classic locale. |
| 408 | static const char _S_c_name[2]; |
| 409 | |
| 410 | #ifdef __GTHREADS |
| 411 | static __gthread_once_t _S_once; |
| 412 | #endif |
| 413 | |
| 414 | static void |
| 415 | _S_initialize_once(); |
| 416 | |
| 417 | protected: |
| 418 | /** |
| 419 | * @brief Facet constructor. |
| 420 | * |
| 421 | * This is the constructor provided by the standard. If refs is 0, the |
| 422 | * facet is destroyed when the last referencing locale is destroyed. |
| 423 | * Otherwise the facet will never be destroyed. |
| 424 | * |
| 425 | * @param __refs The initial value for reference count. |
| 426 | */ |
| 427 | explicit |
| 428 | facet(size_t __refs = 0) throw() : _M_refcount(__refs ? 1 : 0) |
| 429 | { } |
| 430 | |
| 431 | /// Facet destructor. |
| 432 | virtual |
| 433 | ~facet(); |
| 434 | |
| 435 | static void |
| 436 | _S_create_c_locale(__c_locale& __cloc, const char* __s, |
| 437 | __c_locale __old = 0); |
| 438 | |
| 439 | static __c_locale |
| 440 | _S_clone_c_locale(__c_locale& __cloc) throw(); |
| 441 | |
| 442 | static void |
| 443 | _S_destroy_c_locale(__c_locale& __cloc); |
| 444 | |
| 445 | static __c_locale |
| 446 | _S_lc_ctype_c_locale(__c_locale __cloc, const char* __s); |
| 447 | |
| 448 | // Returns data from the underlying "C" library data for the |
| 449 | // classic locale. |
| 450 | static __c_locale |
| 451 | _S_get_c_locale(); |
| 452 | |
| 453 | _GLIBCXX_CONST static const char* |
| 454 | _S_get_c_name() throw(); |
| 455 | |
| 456 | #if __cplusplus < 201103L |
| 457 | private: |
| 458 | facet(const facet&); // Not defined. |
| 459 | |
| 460 | facet& |
| 461 | operator=(const facet&); // Not defined. |
| 462 | #else |
| 463 | facet(const facet&) = delete; |
| 464 | |
| 465 | facet& |
| 466 | operator=(const facet&) = delete; |
| 467 | #endif |
| 468 | |
| 469 | private: |
| 470 | void |
| 471 | _M_add_reference() const throw() |
| 472 | { __gnu_cxx::__atomic_add_dispatch(mem: &_M_refcount, val: 1); } |
| 473 | |
| 474 | void |
| 475 | _M_remove_reference() const throw() |
| 476 | { |
| 477 | // Be race-detector-friendly. For more info see bits/c++config. |
| 478 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_refcount); |
| 479 | if (__gnu_cxx::__exchange_and_add_dispatch(mem: &_M_refcount, val: -1) == 1) |
| 480 | { |
| 481 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_refcount); |
| 482 | __try |
| 483 | { delete this; } |
| 484 | __catch(...) |
| 485 | { } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | const facet* _M_sso_shim(const id*) const; |
| 490 | const facet* _M_cow_shim(const id*) const; |
| 491 | |
| 492 | protected: |
| 493 | class __shim; // For internal use only. |
| 494 | }; |
| 495 | |
| 496 | |
| 497 | // 22.1.1.1.3 Class locale::id |
| 498 | /** |
| 499 | * @brief Facet ID class. |
| 500 | * @ingroup locales |
| 501 | * |
| 502 | * The ID class provides facets with an index used to identify them. |
| 503 | * Every facet class must define a public static member locale::id, or be |
| 504 | * derived from a facet that provides this member, otherwise the facet |
| 505 | * cannot be used in a locale. The locale::id ensures that each class |
| 506 | * type gets a unique identifier. |
| 507 | */ |
| 508 | class locale::id |
| 509 | { |
| 510 | private: |
| 511 | friend class locale; |
| 512 | friend class locale::_Impl; |
| 513 | |
| 514 | template<typename _Facet> |
| 515 | friend const _Facet& |
| 516 | use_facet(const locale&); |
| 517 | |
| 518 | template<typename _Facet> |
| 519 | friend bool |
| 520 | has_facet(const locale&) throw(); |
| 521 | |
| 522 | template<typename _Facet> |
| 523 | friend const _Facet* |
| 524 | __try_use_facet(const locale&) _GLIBCXX_NOTHROW; |
| 525 | |
| 526 | // NB: There is no accessor for _M_index because it may be used |
| 527 | // before the constructor is run; the effect of calling a member |
| 528 | // function (even an inline) would be undefined. |
| 529 | mutable size_t _M_index; |
| 530 | |
| 531 | // Last id number assigned. |
| 532 | static _Atomic_word _S_refcount; |
| 533 | |
| 534 | void |
| 535 | operator=(const id&); // Not defined. |
| 536 | |
| 537 | id(const id&); // Not defined. |
| 538 | |
| 539 | public: |
| 540 | // NB: This class is always a static data member, and thus can be |
| 541 | // counted on to be zero-initialized. |
| 542 | /// Constructor. |
| 543 | id() { } |
| 544 | |
| 545 | size_t |
| 546 | _M_id() const throw(); |
| 547 | }; |
| 548 | |
| 549 | |
| 550 | // Implementation object for locale. |
| 551 | class locale::_Impl |
| 552 | { |
| 553 | public: |
| 554 | // Friends. |
| 555 | friend class locale; |
| 556 | friend class locale::facet; |
| 557 | |
| 558 | template<typename _Facet> |
| 559 | friend bool |
| 560 | has_facet(const locale&) throw(); |
| 561 | |
| 562 | template<typename _Facet> |
| 563 | friend const _Facet& |
| 564 | use_facet(const locale&); |
| 565 | |
| 566 | template<typename _Facet> |
| 567 | friend const _Facet* |
| 568 | __try_use_facet(const locale&) _GLIBCXX_NOTHROW; |
| 569 | |
| 570 | template<typename _Cache> |
| 571 | friend struct __use_cache; |
| 572 | |
| 573 | private: |
| 574 | // Data Members. |
| 575 | _Atomic_word _M_refcount; |
| 576 | const facet** _M_facets; |
| 577 | size_t _M_facets_size; |
| 578 | const facet** _M_caches; |
| 579 | char** _M_names; |
| 580 | static const locale::id* const _S_id_ctype[]; |
| 581 | static const locale::id* const _S_id_numeric[]; |
| 582 | static const locale::id* const _S_id_collate[]; |
| 583 | static const locale::id* const _S_id_time[]; |
| 584 | static const locale::id* const _S_id_monetary[]; |
| 585 | static const locale::id* const _S_id_messages[]; |
| 586 | static const locale::id* const* const _S_facet_categories[]; |
| 587 | |
| 588 | void |
| 589 | _M_add_reference() throw() |
| 590 | { __gnu_cxx::__atomic_add_dispatch(mem: &_M_refcount, val: 1); } |
| 591 | |
| 592 | void |
| 593 | _M_remove_reference() throw() |
| 594 | { |
| 595 | // Be race-detector-friendly. For more info see bits/c++config. |
| 596 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_refcount); |
| 597 | if (__gnu_cxx::__exchange_and_add_dispatch(mem: &_M_refcount, val: -1) == 1) |
| 598 | { |
| 599 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_refcount); |
| 600 | __try |
| 601 | { delete this; } |
| 602 | __catch(...) |
| 603 | { } |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | _Impl(const _Impl&, size_t); |
| 608 | _Impl(const char*, size_t); |
| 609 | _Impl(size_t) throw(); |
| 610 | |
| 611 | ~_Impl() throw(); |
| 612 | |
| 613 | _Impl(const _Impl&); // Not defined. |
| 614 | |
| 615 | void |
| 616 | operator=(const _Impl&); // Not defined. |
| 617 | |
| 618 | bool |
| 619 | _M_check_same_name() |
| 620 | { |
| 621 | bool __ret = true; |
| 622 | if (_M_names[1]) |
| 623 | // We must actually compare all the _M_names: can be all equal! |
| 624 | for (size_t __i = 0; __ret && __i < _S_categories_size - 1; ++__i) |
| 625 | __ret = __builtin_strcmp(_M_names[__i], _M_names[__i + 1]) == 0; |
| 626 | return __ret; |
| 627 | } |
| 628 | |
| 629 | void |
| 630 | _M_replace_categories(const _Impl*, category); |
| 631 | |
| 632 | void |
| 633 | _M_replace_category(const _Impl*, const locale::id* const*); |
| 634 | |
| 635 | void |
| 636 | _M_replace_facet(const _Impl*, const locale::id*); |
| 637 | |
| 638 | void |
| 639 | _M_install_facet(const locale::id*, const facet*); |
| 640 | |
| 641 | template<typename _Facet> |
| 642 | void |
| 643 | _M_init_facet(_Facet* __facet) |
| 644 | { _M_install_facet(&_Facet::id, __facet); } |
| 645 | |
| 646 | template<typename _Facet> |
| 647 | void |
| 648 | _M_init_facet_unchecked(_Facet* __facet) |
| 649 | { |
| 650 | __facet->_M_add_reference(); |
| 651 | _M_facets[_Facet::id._M_id()] = __facet; |
| 652 | } |
| 653 | |
| 654 | void |
| 655 | _M_install_cache(const facet*, size_t); |
| 656 | |
| 657 | void (facet**); |
| 658 | void (void*, void*, const char*, const char*); |
| 659 | |
| 660 | #ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT |
| 661 | void _M_init_extra_ldbl128(bool); |
| 662 | #endif |
| 663 | }; |
| 664 | |
| 665 | |
| 666 | /** |
| 667 | * @brief Facet for localized string comparison. |
| 668 | * |
| 669 | * This facet encapsulates the code to compare strings in a localized |
| 670 | * manner. |
| 671 | * |
| 672 | * The collate template uses protected virtual functions to provide |
| 673 | * the actual results. The public accessors forward the call to |
| 674 | * the virtual functions. These virtual functions are hooks for |
| 675 | * developers to implement the behavior they require from the |
| 676 | * collate facet. |
| 677 | */ |
| 678 | template<typename _CharT> |
| 679 | class _GLIBCXX_NAMESPACE_CXX11 collate : public locale::facet |
| 680 | { |
| 681 | public: |
| 682 | // Types: |
| 683 | ///@{ |
| 684 | /// Public typedefs |
| 685 | typedef _CharT char_type; |
| 686 | typedef basic_string<_CharT> string_type; |
| 687 | ///@} |
| 688 | |
| 689 | protected: |
| 690 | // Underlying "C" library locale information saved from |
| 691 | // initialization, needed by collate_byname as well. |
| 692 | __c_locale _M_c_locale_collate; |
| 693 | |
| 694 | public: |
| 695 | /// Numpunct facet id. |
| 696 | static locale::id id; |
| 697 | |
| 698 | /** |
| 699 | * @brief Constructor performs initialization. |
| 700 | * |
| 701 | * This is the constructor provided by the standard. |
| 702 | * |
| 703 | * @param __refs Passed to the base facet class. |
| 704 | */ |
| 705 | explicit |
| 706 | collate(size_t __refs = 0) |
| 707 | : facet(__refs), _M_c_locale_collate(_S_get_c_locale()) |
| 708 | { } |
| 709 | |
| 710 | /** |
| 711 | * @brief Internal constructor. Not for general use. |
| 712 | * |
| 713 | * This is a constructor for use by the library itself to set up new |
| 714 | * locales. |
| 715 | * |
| 716 | * @param __cloc The C locale. |
| 717 | * @param __refs Passed to the base facet class. |
| 718 | */ |
| 719 | explicit |
| 720 | collate(__c_locale __cloc, size_t __refs = 0) |
| 721 | : facet(__refs), _M_c_locale_collate(_S_clone_c_locale(__cloc)) |
| 722 | { } |
| 723 | |
| 724 | /** |
| 725 | * @brief Compare two strings. |
| 726 | * |
| 727 | * This function compares two strings and returns the result by calling |
| 728 | * collate::do_compare(). |
| 729 | * |
| 730 | * @param __lo1 Start of string 1. |
| 731 | * @param __hi1 End of string 1. |
| 732 | * @param __lo2 Start of string 2. |
| 733 | * @param __hi2 End of string 2. |
| 734 | * @return 1 if string1 > string2, -1 if string1 < string2, else 0. |
| 735 | */ |
| 736 | int |
| 737 | compare(const _CharT* __lo1, const _CharT* __hi1, |
| 738 | const _CharT* __lo2, const _CharT* __hi2) const |
| 739 | { return this->do_compare(__lo1, __hi1, __lo2, __hi2); } |
| 740 | |
| 741 | /** |
| 742 | * @brief Transform string to comparable form. |
| 743 | * |
| 744 | * This function is a wrapper for strxfrm functionality. It takes the |
| 745 | * input string and returns a modified string that can be directly |
| 746 | * compared to other transformed strings. In the C locale, this |
| 747 | * function just returns a copy of the input string. In some other |
| 748 | * locales, it may replace two chars with one, change a char for |
| 749 | * another, etc. It does so by returning collate::do_transform(). |
| 750 | * |
| 751 | * @param __lo Start of string. |
| 752 | * @param __hi End of string. |
| 753 | * @return Transformed string_type. |
| 754 | */ |
| 755 | string_type |
| 756 | transform(const _CharT* __lo, const _CharT* __hi) const |
| 757 | { return this->do_transform(__lo, __hi); } |
| 758 | |
| 759 | /** |
| 760 | * @brief Return hash of a string. |
| 761 | * |
| 762 | * This function computes and returns a hash on the input string. It |
| 763 | * does so by returning collate::do_hash(). |
| 764 | * |
| 765 | * @param __lo Start of string. |
| 766 | * @param __hi End of string. |
| 767 | * @return Hash value. |
| 768 | */ |
| 769 | long |
| 770 | hash(const _CharT* __lo, const _CharT* __hi) const |
| 771 | { return this->do_hash(__lo, __hi); } |
| 772 | |
| 773 | // Used to abstract out _CharT bits in virtual member functions, below. |
| 774 | int |
| 775 | _M_compare(const _CharT*, const _CharT*) const throw(); |
| 776 | |
| 777 | size_t |
| 778 | _M_transform(_CharT*, const _CharT*, size_t) const throw(); |
| 779 | |
| 780 | protected: |
| 781 | /// Destructor. |
| 782 | virtual |
| 783 | ~collate() |
| 784 | { _S_destroy_c_locale(cloc&: _M_c_locale_collate); } |
| 785 | |
| 786 | /** |
| 787 | * @brief Compare two strings. |
| 788 | * |
| 789 | * This function is a hook for derived classes to change the value |
| 790 | * returned. @see compare(). |
| 791 | * |
| 792 | * @param __lo1 Start of string 1. |
| 793 | * @param __hi1 End of string 1. |
| 794 | * @param __lo2 Start of string 2. |
| 795 | * @param __hi2 End of string 2. |
| 796 | * @return 1 if string1 > string2, -1 if string1 < string2, else 0. |
| 797 | */ |
| 798 | virtual int |
| 799 | do_compare(const _CharT* __lo1, const _CharT* __hi1, |
| 800 | const _CharT* __lo2, const _CharT* __hi2) const; |
| 801 | |
| 802 | /** |
| 803 | * @brief Transform string to comparable form. |
| 804 | * |
| 805 | * This function is a hook for derived classes to change the value |
| 806 | * returned. |
| 807 | * |
| 808 | * @param __lo Start. |
| 809 | * @param __hi End. |
| 810 | * @return transformed string. |
| 811 | */ |
| 812 | virtual string_type |
| 813 | do_transform(const _CharT* __lo, const _CharT* __hi) const; |
| 814 | |
| 815 | /** |
| 816 | * @brief Return hash of a string. |
| 817 | * |
| 818 | * This function computes and returns a hash on the input string. This |
| 819 | * function is a hook for derived classes to change the value returned. |
| 820 | * |
| 821 | * @param __lo Start of string. |
| 822 | * @param __hi End of string. |
| 823 | * @return Hash value. |
| 824 | */ |
| 825 | virtual long |
| 826 | do_hash(const _CharT* __lo, const _CharT* __hi) const; |
| 827 | }; |
| 828 | |
| 829 | template<typename _CharT> |
| 830 | locale::id collate<_CharT>::id; |
| 831 | |
| 832 | // Specializations. |
| 833 | template<> |
| 834 | int |
| 835 | collate<char>::_M_compare(const char*, const char*) const throw(); |
| 836 | |
| 837 | template<> |
| 838 | size_t |
| 839 | collate<char>::_M_transform(char*, const char*, size_t) const throw(); |
| 840 | |
| 841 | #ifdef _GLIBCXX_USE_WCHAR_T |
| 842 | template<> |
| 843 | int |
| 844 | collate<wchar_t>::_M_compare(const wchar_t*, const wchar_t*) const throw(); |
| 845 | |
| 846 | template<> |
| 847 | size_t |
| 848 | collate<wchar_t>::_M_transform(wchar_t*, const wchar_t*, size_t) const throw(); |
| 849 | #endif |
| 850 | |
| 851 | /// class collate_byname [22.2.4.2]. |
| 852 | template<typename _CharT> |
| 853 | class _GLIBCXX_NAMESPACE_CXX11 collate_byname : public collate<_CharT> |
| 854 | { |
| 855 | public: |
| 856 | ///@{ |
| 857 | /// Public typedefs |
| 858 | typedef _CharT char_type; |
| 859 | typedef basic_string<_CharT> string_type; |
| 860 | ///@} |
| 861 | |
| 862 | explicit |
| 863 | collate_byname(const char* __s, size_t __refs = 0) |
| 864 | : collate<_CharT>(__refs) |
| 865 | { |
| 866 | if (__builtin_strcmp(__s, "C" ) != 0 |
| 867 | && __builtin_strcmp(__s, "POSIX" ) != 0) |
| 868 | { |
| 869 | this->_S_destroy_c_locale(this->_M_c_locale_collate); |
| 870 | this->_S_create_c_locale(this->_M_c_locale_collate, __s); |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | #if __cplusplus >= 201103L |
| 875 | explicit |
| 876 | collate_byname(const string& __s, size_t __refs = 0) |
| 877 | : collate_byname(__s.c_str(), __refs) { } |
| 878 | #endif |
| 879 | |
| 880 | protected: |
| 881 | virtual |
| 882 | ~collate_byname() { } |
| 883 | }; |
| 884 | |
| 885 | _GLIBCXX_END_NAMESPACE_VERSION |
| 886 | } // namespace |
| 887 | |
| 888 | # include <bits/locale_classes.tcc> |
| 889 | |
| 890 | #endif |
| 891 | |