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 | locale |
244 | combine(const locale& __other) const; |
245 | |
246 | // Locale operations: |
247 | /** |
248 | * @brief Return locale name. |
249 | * @return Locale name or "*" if unnamed. |
250 | */ |
251 | _GLIBCXX_DEFAULT_ABI_TAG |
252 | string |
253 | name() const; |
254 | |
255 | #ifdef __glibcxx_text_encoding |
256 | # if __CHAR_BIT__ == 8 |
257 | text_encoding |
258 | encoding() const; |
259 | # else |
260 | text_encoding |
261 | encoding() const = delete; |
262 | # endif |
263 | #endif |
264 | |
265 | /** |
266 | * @brief Locale equality. |
267 | * |
268 | * @param __other The locale to compare against. |
269 | * @return True if other and this refer to the same locale instance, are |
270 | * copies, or have the same name. False otherwise. |
271 | */ |
272 | bool |
273 | operator==(const locale& __other) const throw(); |
274 | |
275 | #if __cpp_impl_three_way_comparison < 201907L |
276 | /** |
277 | * @brief Locale inequality. |
278 | * |
279 | * @param __other The locale to compare against. |
280 | * @return ! (*this == __other) |
281 | */ |
282 | bool |
283 | operator!=(const locale& __other) const throw() |
284 | { return !(this->operator==(__other)); } |
285 | #endif |
286 | |
287 | /** |
288 | * @brief Compare two strings according to collate. |
289 | * |
290 | * Template operator to compare two strings using the compare function of |
291 | * the collate facet in this locale. One use is to provide the locale to |
292 | * the sort function. For example, a vector v of strings could be sorted |
293 | * according to locale loc by doing: |
294 | * @code |
295 | * std::sort(v.begin(), v.end(), loc); |
296 | * @endcode |
297 | * |
298 | * @param __s1 First string to compare. |
299 | * @param __s2 Second string to compare. |
300 | * @return True if collate<_Char> facet compares __s1 < __s2, else false. |
301 | */ |
302 | template<typename _Char, typename _Traits, typename _Alloc> |
303 | bool |
304 | operator()(const basic_string<_Char, _Traits, _Alloc>& __s1, |
305 | const basic_string<_Char, _Traits, _Alloc>& __s2) const; |
306 | |
307 | // Global locale objects: |
308 | /** |
309 | * @brief Set global locale |
310 | * |
311 | * This function sets the global locale to the argument and returns a |
312 | * copy of the previous global locale. If the argument has a name, it |
313 | * will also call std::setlocale(LC_ALL, loc.name()). |
314 | * |
315 | * @param __loc The new locale to make global. |
316 | * @return Copy of the old global locale. |
317 | */ |
318 | static locale |
319 | global(const locale& __loc); |
320 | |
321 | /** |
322 | * @brief Return reference to the C locale. |
323 | */ |
324 | static const locale& |
325 | classic(); |
326 | |
327 | private: |
328 | // The (shared) implementation |
329 | _Impl* _M_impl; |
330 | |
331 | // The "C" reference locale |
332 | static _Impl* _S_classic; |
333 | |
334 | // Current global locale |
335 | static _Impl* _S_global; |
336 | |
337 | // Names of underlying locale categories. |
338 | // NB: locale::global() has to know how to modify all the |
339 | // underlying categories, not just the ones required by the C++ |
340 | // standard. |
341 | static const char* const* const _S_categories; |
342 | |
343 | // Number of standard categories. For C++, these categories are |
344 | // collate, ctype, monetary, numeric, time, and messages. These |
345 | // directly correspond to ISO C99 macros LC_COLLATE, LC_CTYPE, |
346 | // LC_MONETARY, LC_NUMERIC, and LC_TIME. In addition, POSIX (IEEE |
347 | // 1003.1-2001) specifies LC_MESSAGES. |
348 | // In addition to the standard categories, the underlying |
349 | // operating system is allowed to define extra LC_* |
350 | // macros. For GNU systems, the following are also valid: |
351 | // LC_PAPER, LC_NAME, LC_ADDRESS, LC_TELEPHONE, LC_MEASUREMENT, |
352 | // and LC_IDENTIFICATION. |
353 | enum { _S_categories_size = 6 + _GLIBCXX_NUM_CATEGORIES }; |
354 | |
355 | #ifdef __GTHREADS |
356 | static __gthread_once_t _S_once; |
357 | #endif |
358 | |
359 | explicit |
360 | locale(_Impl*) throw(); |
361 | |
362 | static void |
363 | _S_initialize(); |
364 | |
365 | static void |
366 | _S_initialize_once() throw(); |
367 | |
368 | static category |
369 | _S_normalize_category(category); |
370 | |
371 | void |
372 | _M_coalesce(const locale& __base, const locale& __add, category __cat); |
373 | |
374 | #if _GLIBCXX_USE_CXX11_ABI |
375 | static const id* const _S_twinned_facets[]; |
376 | #endif |
377 | }; |
378 | |
379 | |
380 | // 22.1.1.1.2 Class locale::facet |
381 | /** |
382 | * @brief Localization functionality base class. |
383 | * @ingroup locales |
384 | * |
385 | * The facet class is the base class for a localization feature, such as |
386 | * money, time, and number printing. It provides common support for facets |
387 | * and reference management. |
388 | * |
389 | * Facets may not be copied or assigned. |
390 | */ |
391 | class locale::facet |
392 | { |
393 | private: |
394 | friend class locale; |
395 | friend class locale::_Impl; |
396 | |
397 | mutable _Atomic_word _M_refcount; |
398 | |
399 | // Contains data from the underlying "C" library for the classic locale. |
400 | static __c_locale _S_c_locale; |
401 | |
402 | // String literal for the name of the classic locale. |
403 | static const char _S_c_name[2]; |
404 | |
405 | #ifdef __GTHREADS |
406 | static __gthread_once_t _S_once; |
407 | #endif |
408 | |
409 | static void |
410 | _S_initialize_once(); |
411 | |
412 | protected: |
413 | /** |
414 | * @brief Facet constructor. |
415 | * |
416 | * This is the constructor provided by the standard. If refs is 0, the |
417 | * facet is destroyed when the last referencing locale is destroyed. |
418 | * Otherwise the facet will never be destroyed. |
419 | * |
420 | * @param __refs The initial value for reference count. |
421 | */ |
422 | explicit |
423 | facet(size_t __refs = 0) throw() : _M_refcount(__refs ? 1 : 0) |
424 | { } |
425 | |
426 | /// Facet destructor. |
427 | virtual |
428 | ~facet(); |
429 | |
430 | static void |
431 | _S_create_c_locale(__c_locale& __cloc, const char* __s, |
432 | __c_locale __old = 0); |
433 | |
434 | static __c_locale |
435 | _S_clone_c_locale(__c_locale& __cloc) throw(); |
436 | |
437 | static void |
438 | _S_destroy_c_locale(__c_locale& __cloc); |
439 | |
440 | static __c_locale |
441 | _S_lc_ctype_c_locale(__c_locale __cloc, const char* __s); |
442 | |
443 | // Returns data from the underlying "C" library data for the |
444 | // classic locale. |
445 | static __c_locale |
446 | _S_get_c_locale(); |
447 | |
448 | _GLIBCXX_CONST static const char* |
449 | _S_get_c_name() throw(); |
450 | |
451 | #if __cplusplus < 201103L |
452 | private: |
453 | facet(const facet&); // Not defined. |
454 | |
455 | facet& |
456 | operator=(const facet&); // Not defined. |
457 | #else |
458 | facet(const facet&) = delete; |
459 | |
460 | facet& |
461 | operator=(const facet&) = delete; |
462 | #endif |
463 | |
464 | private: |
465 | void |
466 | _M_add_reference() const throw() |
467 | { __gnu_cxx::__atomic_add_dispatch(mem: &_M_refcount, val: 1); } |
468 | |
469 | void |
470 | _M_remove_reference() const throw() |
471 | { |
472 | // Be race-detector-friendly. For more info see bits/c++config. |
473 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_refcount); |
474 | if (__gnu_cxx::__exchange_and_add_dispatch(mem: &_M_refcount, val: -1) == 1) |
475 | { |
476 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_refcount); |
477 | __try |
478 | { delete this; } |
479 | __catch(...) |
480 | { } |
481 | } |
482 | } |
483 | |
484 | const facet* _M_sso_shim(const id*) const; |
485 | const facet* _M_cow_shim(const id*) const; |
486 | |
487 | protected: |
488 | class __shim; // For internal use only. |
489 | }; |
490 | |
491 | |
492 | // 22.1.1.1.3 Class locale::id |
493 | /** |
494 | * @brief Facet ID class. |
495 | * @ingroup locales |
496 | * |
497 | * The ID class provides facets with an index used to identify them. |
498 | * Every facet class must define a public static member locale::id, or be |
499 | * derived from a facet that provides this member, otherwise the facet |
500 | * cannot be used in a locale. The locale::id ensures that each class |
501 | * type gets a unique identifier. |
502 | */ |
503 | class locale::id |
504 | { |
505 | private: |
506 | friend class locale; |
507 | friend class locale::_Impl; |
508 | |
509 | template<typename _Facet> |
510 | friend const _Facet& |
511 | use_facet(const locale&); |
512 | |
513 | template<typename _Facet> |
514 | friend bool |
515 | has_facet(const locale&) throw(); |
516 | |
517 | template<typename _Facet> |
518 | friend const _Facet* |
519 | __try_use_facet(const locale&) _GLIBCXX_NOTHROW; |
520 | |
521 | // NB: There is no accessor for _M_index because it may be used |
522 | // before the constructor is run; the effect of calling a member |
523 | // function (even an inline) would be undefined. |
524 | mutable size_t _M_index; |
525 | |
526 | // Last id number assigned. |
527 | static _Atomic_word _S_refcount; |
528 | |
529 | void |
530 | operator=(const id&); // Not defined. |
531 | |
532 | id(const id&); // Not defined. |
533 | |
534 | public: |
535 | // NB: This class is always a static data member, and thus can be |
536 | // counted on to be zero-initialized. |
537 | /// Constructor. |
538 | id() { } |
539 | |
540 | size_t |
541 | _M_id() const throw(); |
542 | }; |
543 | |
544 | |
545 | // Implementation object for locale. |
546 | class locale::_Impl |
547 | { |
548 | public: |
549 | // Friends. |
550 | friend class locale; |
551 | friend class locale::facet; |
552 | |
553 | template<typename _Facet> |
554 | friend bool |
555 | has_facet(const locale&) throw(); |
556 | |
557 | template<typename _Facet> |
558 | friend const _Facet& |
559 | use_facet(const locale&); |
560 | |
561 | template<typename _Facet> |
562 | friend const _Facet* |
563 | __try_use_facet(const locale&) _GLIBCXX_NOTHROW; |
564 | |
565 | template<typename _Cache> |
566 | friend struct __use_cache; |
567 | |
568 | private: |
569 | // Data Members. |
570 | _Atomic_word _M_refcount; |
571 | const facet** _M_facets; |
572 | size_t _M_facets_size; |
573 | const facet** _M_caches; |
574 | char** _M_names; |
575 | static const locale::id* const _S_id_ctype[]; |
576 | static const locale::id* const _S_id_numeric[]; |
577 | static const locale::id* const _S_id_collate[]; |
578 | static const locale::id* const _S_id_time[]; |
579 | static const locale::id* const _S_id_monetary[]; |
580 | static const locale::id* const _S_id_messages[]; |
581 | static const locale::id* const* const _S_facet_categories[]; |
582 | |
583 | void |
584 | _M_add_reference() throw() |
585 | { __gnu_cxx::__atomic_add_dispatch(mem: &_M_refcount, val: 1); } |
586 | |
587 | void |
588 | _M_remove_reference() throw() |
589 | { |
590 | // Be race-detector-friendly. For more info see bits/c++config. |
591 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_refcount); |
592 | if (__gnu_cxx::__exchange_and_add_dispatch(mem: &_M_refcount, val: -1) == 1) |
593 | { |
594 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_refcount); |
595 | __try |
596 | { delete this; } |
597 | __catch(...) |
598 | { } |
599 | } |
600 | } |
601 | |
602 | _Impl(const _Impl&, size_t); |
603 | _Impl(const char*, size_t); |
604 | _Impl(size_t) throw(); |
605 | |
606 | ~_Impl() throw(); |
607 | |
608 | _Impl(const _Impl&); // Not defined. |
609 | |
610 | void |
611 | operator=(const _Impl&); // Not defined. |
612 | |
613 | bool |
614 | _M_check_same_name() |
615 | { |
616 | bool __ret = true; |
617 | if (_M_names[1]) |
618 | // We must actually compare all the _M_names: can be all equal! |
619 | for (size_t __i = 0; __ret && __i < _S_categories_size - 1; ++__i) |
620 | __ret = __builtin_strcmp(_M_names[__i], _M_names[__i + 1]) == 0; |
621 | return __ret; |
622 | } |
623 | |
624 | void |
625 | _M_replace_categories(const _Impl*, category); |
626 | |
627 | void |
628 | _M_replace_category(const _Impl*, const locale::id* const*); |
629 | |
630 | void |
631 | _M_replace_facet(const _Impl*, const locale::id*); |
632 | |
633 | void |
634 | _M_install_facet(const locale::id*, const facet*); |
635 | |
636 | template<typename _Facet> |
637 | void |
638 | _M_init_facet(_Facet* __facet) |
639 | { _M_install_facet(&_Facet::id, __facet); } |
640 | |
641 | template<typename _Facet> |
642 | void |
643 | _M_init_facet_unchecked(_Facet* __facet) |
644 | { |
645 | __facet->_M_add_reference(); |
646 | _M_facets[_Facet::id._M_id()] = __facet; |
647 | } |
648 | |
649 | void |
650 | _M_install_cache(const facet*, size_t); |
651 | |
652 | void (facet**); |
653 | void (void*, void*, const char*, const char*); |
654 | |
655 | #ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT |
656 | void _M_init_extra_ldbl128(bool); |
657 | #endif |
658 | }; |
659 | |
660 | |
661 | /** |
662 | * @brief Facet for localized string comparison. |
663 | * |
664 | * This facet encapsulates the code to compare strings in a localized |
665 | * manner. |
666 | * |
667 | * The collate template uses protected virtual functions to provide |
668 | * the actual results. The public accessors forward the call to |
669 | * the virtual functions. These virtual functions are hooks for |
670 | * developers to implement the behavior they require from the |
671 | * collate facet. |
672 | */ |
673 | template<typename _CharT> |
674 | class _GLIBCXX_NAMESPACE_CXX11 collate : public locale::facet |
675 | { |
676 | public: |
677 | // Types: |
678 | ///@{ |
679 | /// Public typedefs |
680 | typedef _CharT char_type; |
681 | typedef basic_string<_CharT> string_type; |
682 | ///@} |
683 | |
684 | protected: |
685 | // Underlying "C" library locale information saved from |
686 | // initialization, needed by collate_byname as well. |
687 | __c_locale _M_c_locale_collate; |
688 | |
689 | public: |
690 | /// Numpunct facet id. |
691 | static locale::id id; |
692 | |
693 | /** |
694 | * @brief Constructor performs initialization. |
695 | * |
696 | * This is the constructor provided by the standard. |
697 | * |
698 | * @param __refs Passed to the base facet class. |
699 | */ |
700 | explicit |
701 | collate(size_t __refs = 0) |
702 | : facet(__refs), _M_c_locale_collate(_S_get_c_locale()) |
703 | { } |
704 | |
705 | /** |
706 | * @brief Internal constructor. Not for general use. |
707 | * |
708 | * This is a constructor for use by the library itself to set up new |
709 | * locales. |
710 | * |
711 | * @param __cloc The C locale. |
712 | * @param __refs Passed to the base facet class. |
713 | */ |
714 | explicit |
715 | collate(__c_locale __cloc, size_t __refs = 0) |
716 | : facet(__refs), _M_c_locale_collate(_S_clone_c_locale(__cloc)) |
717 | { } |
718 | |
719 | /** |
720 | * @brief Compare two strings. |
721 | * |
722 | * This function compares two strings and returns the result by calling |
723 | * collate::do_compare(). |
724 | * |
725 | * @param __lo1 Start of string 1. |
726 | * @param __hi1 End of string 1. |
727 | * @param __lo2 Start of string 2. |
728 | * @param __hi2 End of string 2. |
729 | * @return 1 if string1 > string2, -1 if string1 < string2, else 0. |
730 | */ |
731 | int |
732 | compare(const _CharT* __lo1, const _CharT* __hi1, |
733 | const _CharT* __lo2, const _CharT* __hi2) const |
734 | { return this->do_compare(__lo1, __hi1, __lo2, __hi2); } |
735 | |
736 | /** |
737 | * @brief Transform string to comparable form. |
738 | * |
739 | * This function is a wrapper for strxfrm functionality. It takes the |
740 | * input string and returns a modified string that can be directly |
741 | * compared to other transformed strings. In the C locale, this |
742 | * function just returns a copy of the input string. In some other |
743 | * locales, it may replace two chars with one, change a char for |
744 | * another, etc. It does so by returning collate::do_transform(). |
745 | * |
746 | * @param __lo Start of string. |
747 | * @param __hi End of string. |
748 | * @return Transformed string_type. |
749 | */ |
750 | string_type |
751 | transform(const _CharT* __lo, const _CharT* __hi) const |
752 | { return this->do_transform(__lo, __hi); } |
753 | |
754 | /** |
755 | * @brief Return hash of a string. |
756 | * |
757 | * This function computes and returns a hash on the input string. It |
758 | * does so by returning collate::do_hash(). |
759 | * |
760 | * @param __lo Start of string. |
761 | * @param __hi End of string. |
762 | * @return Hash value. |
763 | */ |
764 | long |
765 | hash(const _CharT* __lo, const _CharT* __hi) const |
766 | { return this->do_hash(__lo, __hi); } |
767 | |
768 | // Used to abstract out _CharT bits in virtual member functions, below. |
769 | int |
770 | _M_compare(const _CharT*, const _CharT*) const throw(); |
771 | |
772 | size_t |
773 | _M_transform(_CharT*, const _CharT*, size_t) const throw(); |
774 | |
775 | protected: |
776 | /// Destructor. |
777 | virtual |
778 | ~collate() |
779 | { _S_destroy_c_locale(cloc&: _M_c_locale_collate); } |
780 | |
781 | /** |
782 | * @brief Compare two strings. |
783 | * |
784 | * This function is a hook for derived classes to change the value |
785 | * returned. @see compare(). |
786 | * |
787 | * @param __lo1 Start of string 1. |
788 | * @param __hi1 End of string 1. |
789 | * @param __lo2 Start of string 2. |
790 | * @param __hi2 End of string 2. |
791 | * @return 1 if string1 > string2, -1 if string1 < string2, else 0. |
792 | */ |
793 | virtual int |
794 | do_compare(const _CharT* __lo1, const _CharT* __hi1, |
795 | const _CharT* __lo2, const _CharT* __hi2) const; |
796 | |
797 | /** |
798 | * @brief Transform string to comparable form. |
799 | * |
800 | * This function is a hook for derived classes to change the value |
801 | * returned. |
802 | * |
803 | * @param __lo Start. |
804 | * @param __hi End. |
805 | * @return transformed string. |
806 | */ |
807 | virtual string_type |
808 | do_transform(const _CharT* __lo, const _CharT* __hi) const; |
809 | |
810 | /** |
811 | * @brief Return hash of a string. |
812 | * |
813 | * This function computes and returns a hash on the input string. This |
814 | * function is a hook for derived classes to change the value returned. |
815 | * |
816 | * @param __lo Start of string. |
817 | * @param __hi End of string. |
818 | * @return Hash value. |
819 | */ |
820 | virtual long |
821 | do_hash(const _CharT* __lo, const _CharT* __hi) const; |
822 | }; |
823 | |
824 | template<typename _CharT> |
825 | locale::id collate<_CharT>::id; |
826 | |
827 | // Specializations. |
828 | template<> |
829 | int |
830 | collate<char>::_M_compare(const char*, const char*) const throw(); |
831 | |
832 | template<> |
833 | size_t |
834 | collate<char>::_M_transform(char*, const char*, size_t) const throw(); |
835 | |
836 | #ifdef _GLIBCXX_USE_WCHAR_T |
837 | template<> |
838 | int |
839 | collate<wchar_t>::_M_compare(const wchar_t*, const wchar_t*) const throw(); |
840 | |
841 | template<> |
842 | size_t |
843 | collate<wchar_t>::_M_transform(wchar_t*, const wchar_t*, size_t) const throw(); |
844 | #endif |
845 | |
846 | /// class collate_byname [22.2.4.2]. |
847 | template<typename _CharT> |
848 | class _GLIBCXX_NAMESPACE_CXX11 collate_byname : public collate<_CharT> |
849 | { |
850 | public: |
851 | ///@{ |
852 | /// Public typedefs |
853 | typedef _CharT char_type; |
854 | typedef basic_string<_CharT> string_type; |
855 | ///@} |
856 | |
857 | explicit |
858 | collate_byname(const char* __s, size_t __refs = 0) |
859 | : collate<_CharT>(__refs) |
860 | { |
861 | if (__builtin_strcmp(__s, "C" ) != 0 |
862 | && __builtin_strcmp(__s, "POSIX" ) != 0) |
863 | { |
864 | this->_S_destroy_c_locale(this->_M_c_locale_collate); |
865 | this->_S_create_c_locale(this->_M_c_locale_collate, __s); |
866 | } |
867 | } |
868 | |
869 | #if __cplusplus >= 201103L |
870 | explicit |
871 | collate_byname(const string& __s, size_t __refs = 0) |
872 | : collate_byname(__s.c_str(), __refs) { } |
873 | #endif |
874 | |
875 | protected: |
876 | virtual |
877 | ~collate_byname() { } |
878 | }; |
879 | |
880 | _GLIBCXX_END_NAMESPACE_VERSION |
881 | } // namespace |
882 | |
883 | # include <bits/locale_classes.tcc> |
884 | |
885 | #endif |
886 | |