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