You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

locale_classes.h 24KB

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