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.

2658 lines
90KB

  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_facets.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_FACETS_H
  28. #define _LOCALE_FACETS_H 1
  29. #pragma GCC system_header
  30. #include <cwctype> // For wctype_t
  31. #include <cctype>
  32. #include <bits/ctype_base.h>
  33. #include <iosfwd>
  34. #include <bits/ios_base.h> // For ios_base, ios_base::iostate
  35. #include <streambuf>
  36. #include <bits/cpp_type_traits.h>
  37. #include <ext/type_traits.h>
  38. #include <ext/numeric_traits.h>
  39. #include <bits/streambuf_iterator.h>
  40. namespace std _GLIBCXX_VISIBILITY(default)
  41. {
  42. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  43. // NB: Don't instantiate required wchar_t facets if no wchar_t support.
  44. #ifdef _GLIBCXX_USE_WCHAR_T
  45. # define _GLIBCXX_NUM_FACETS 28
  46. # define _GLIBCXX_NUM_CXX11_FACETS 16
  47. #else
  48. # define _GLIBCXX_NUM_FACETS 14
  49. # define _GLIBCXX_NUM_CXX11_FACETS 8
  50. #endif
  51. #ifdef _GLIBCXX_USE_CHAR8_T
  52. # define _GLIBCXX_NUM_UNICODE_FACETS 4
  53. #else
  54. # define _GLIBCXX_NUM_UNICODE_FACETS 2
  55. #endif
  56. // Convert string to numeric value of type _Tp and store results.
  57. // NB: This is specialized for all required types, there is no
  58. // generic definition.
  59. template<typename _Tp>
  60. void
  61. __convert_to_v(const char*, _Tp&, ios_base::iostate&,
  62. const __c_locale&) throw();
  63. // Explicit specializations for required types.
  64. template<>
  65. void
  66. __convert_to_v(const char*, float&, ios_base::iostate&,
  67. const __c_locale&) throw();
  68. template<>
  69. void
  70. __convert_to_v(const char*, double&, ios_base::iostate&,
  71. const __c_locale&) throw();
  72. template<>
  73. void
  74. __convert_to_v(const char*, long double&, ios_base::iostate&,
  75. const __c_locale&) throw();
  76. // NB: __pad is a struct, rather than a function, so it can be
  77. // partially-specialized.
  78. template<typename _CharT, typename _Traits>
  79. struct __pad
  80. {
  81. static void
  82. _S_pad(ios_base& __io, _CharT __fill, _CharT* __news,
  83. const _CharT* __olds, streamsize __newlen, streamsize __oldlen);
  84. };
  85. // Used by both numeric and monetary facets.
  86. // Inserts "group separator" characters into an array of characters.
  87. // It's recursive, one iteration per group. It moves the characters
  88. // in the buffer this way: "xxxx12345" -> "12,345xxx". Call this
  89. // only with __gsize != 0.
  90. template<typename _CharT>
  91. _CharT*
  92. __add_grouping(_CharT* __s, _CharT __sep,
  93. const char* __gbeg, size_t __gsize,
  94. const _CharT* __first, const _CharT* __last);
  95. // This template permits specializing facet output code for
  96. // ostreambuf_iterator. For ostreambuf_iterator, sputn is
  97. // significantly more efficient than incrementing iterators.
  98. template<typename _CharT>
  99. inline
  100. ostreambuf_iterator<_CharT>
  101. __write(ostreambuf_iterator<_CharT> __s, const _CharT* __ws, int __len)
  102. {
  103. __s._M_put(__ws, __len);
  104. return __s;
  105. }
  106. // This is the unspecialized form of the template.
  107. template<typename _CharT, typename _OutIter>
  108. inline
  109. _OutIter
  110. __write(_OutIter __s, const _CharT* __ws, int __len)
  111. {
  112. for (int __j = 0; __j < __len; __j++, ++__s)
  113. *__s = __ws[__j];
  114. return __s;
  115. }
  116. // 22.2.1.1 Template class ctype
  117. // Include host and configuration specific ctype enums for ctype_base.
  118. /**
  119. * @brief Common base for ctype facet
  120. *
  121. * This template class provides implementations of the public functions
  122. * that forward to the protected virtual functions.
  123. *
  124. * This template also provides abstract stubs for the protected virtual
  125. * functions.
  126. */
  127. template<typename _CharT>
  128. class __ctype_abstract_base : public locale::facet, public ctype_base
  129. {
  130. public:
  131. // Types:
  132. /// Typedef for the template parameter
  133. typedef _CharT char_type;
  134. /**
  135. * @brief Test char_type classification.
  136. *
  137. * This function finds a mask M for @a __c and compares it to
  138. * mask @a __m. It does so by returning the value of
  139. * ctype<char_type>::do_is().
  140. *
  141. * @param __c The char_type to compare the mask of.
  142. * @param __m The mask to compare against.
  143. * @return (M & __m) != 0.
  144. */
  145. bool
  146. is(mask __m, char_type __c) const
  147. { return this->do_is(__m, __c); }
  148. /**
  149. * @brief Return a mask array.
  150. *
  151. * This function finds the mask for each char_type in the range [lo,hi)
  152. * and successively writes it to vec. vec must have as many elements
  153. * as the char array. It does so by returning the value of
  154. * ctype<char_type>::do_is().
  155. *
  156. * @param __lo Pointer to start of range.
  157. * @param __hi Pointer to end of range.
  158. * @param __vec Pointer to an array of mask storage.
  159. * @return @a __hi.
  160. */
  161. const char_type*
  162. is(const char_type *__lo, const char_type *__hi, mask *__vec) const
  163. { return this->do_is(__lo, __hi, __vec); }
  164. /**
  165. * @brief Find char_type matching a mask
  166. *
  167. * This function searches for and returns the first char_type c in
  168. * [lo,hi) for which is(m,c) is true. It does so by returning
  169. * ctype<char_type>::do_scan_is().
  170. *
  171. * @param __m The mask to compare against.
  172. * @param __lo Pointer to start of range.
  173. * @param __hi Pointer to end of range.
  174. * @return Pointer to matching char_type if found, else @a __hi.
  175. */
  176. const char_type*
  177. scan_is(mask __m, const char_type* __lo, const char_type* __hi) const
  178. { return this->do_scan_is(__m, __lo, __hi); }
  179. /**
  180. * @brief Find char_type not matching a mask
  181. *
  182. * This function searches for and returns the first char_type c in
  183. * [lo,hi) for which is(m,c) is false. It does so by returning
  184. * ctype<char_type>::do_scan_not().
  185. *
  186. * @param __m The mask to compare against.
  187. * @param __lo Pointer to first char in range.
  188. * @param __hi Pointer to end of range.
  189. * @return Pointer to non-matching char if found, else @a __hi.
  190. */
  191. const char_type*
  192. scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
  193. { return this->do_scan_not(__m, __lo, __hi); }
  194. /**
  195. * @brief Convert to uppercase.
  196. *
  197. * This function converts the argument to uppercase if possible.
  198. * If not possible (for example, '2'), returns the argument. It does
  199. * so by returning ctype<char_type>::do_toupper().
  200. *
  201. * @param __c The char_type to convert.
  202. * @return The uppercase char_type if convertible, else @a __c.
  203. */
  204. char_type
  205. toupper(char_type __c) const
  206. { return this->do_toupper(__c); }
  207. /**
  208. * @brief Convert array to uppercase.
  209. *
  210. * This function converts each char_type in the range [lo,hi) to
  211. * uppercase if possible. Other elements remain untouched. It does so
  212. * by returning ctype<char_type>:: do_toupper(lo, hi).
  213. *
  214. * @param __lo Pointer to start of range.
  215. * @param __hi Pointer to end of range.
  216. * @return @a __hi.
  217. */
  218. const char_type*
  219. toupper(char_type *__lo, const char_type* __hi) const
  220. { return this->do_toupper(__lo, __hi); }
  221. /**
  222. * @brief Convert to lowercase.
  223. *
  224. * This function converts the argument to lowercase if possible. If
  225. * not possible (for example, '2'), returns the argument. It does so
  226. * by returning ctype<char_type>::do_tolower(c).
  227. *
  228. * @param __c The char_type to convert.
  229. * @return The lowercase char_type if convertible, else @a __c.
  230. */
  231. char_type
  232. tolower(char_type __c) const
  233. { return this->do_tolower(__c); }
  234. /**
  235. * @brief Convert array to lowercase.
  236. *
  237. * This function converts each char_type in the range [__lo,__hi) to
  238. * lowercase if possible. Other elements remain untouched. It does so
  239. * by returning ctype<char_type>:: do_tolower(__lo, __hi).
  240. *
  241. * @param __lo Pointer to start of range.
  242. * @param __hi Pointer to end of range.
  243. * @return @a __hi.
  244. */
  245. const char_type*
  246. tolower(char_type* __lo, const char_type* __hi) const
  247. { return this->do_tolower(__lo, __hi); }
  248. /**
  249. * @brief Widen char to char_type
  250. *
  251. * This function converts the char argument to char_type using the
  252. * simplest reasonable transformation. It does so by returning
  253. * ctype<char_type>::do_widen(c).
  254. *
  255. * Note: this is not what you want for codepage conversions. See
  256. * codecvt for that.
  257. *
  258. * @param __c The char to convert.
  259. * @return The converted char_type.
  260. */
  261. char_type
  262. widen(char __c) const
  263. { return this->do_widen(__c); }
  264. /**
  265. * @brief Widen array to char_type
  266. *
  267. * This function converts each char in the input to char_type using the
  268. * simplest reasonable transformation. It does so by returning
  269. * ctype<char_type>::do_widen(c).
  270. *
  271. * Note: this is not what you want for codepage conversions. See
  272. * codecvt for that.
  273. *
  274. * @param __lo Pointer to start of range.
  275. * @param __hi Pointer to end of range.
  276. * @param __to Pointer to the destination array.
  277. * @return @a __hi.
  278. */
  279. const char*
  280. widen(const char* __lo, const char* __hi, char_type* __to) const
  281. { return this->do_widen(__lo, __hi, __to); }
  282. /**
  283. * @brief Narrow char_type to char
  284. *
  285. * This function converts the char_type to char using the simplest
  286. * reasonable transformation. If the conversion fails, dfault is
  287. * returned instead. It does so by returning
  288. * ctype<char_type>::do_narrow(__c).
  289. *
  290. * Note: this is not what you want for codepage conversions. See
  291. * codecvt for that.
  292. *
  293. * @param __c The char_type to convert.
  294. * @param __dfault Char to return if conversion fails.
  295. * @return The converted char.
  296. */
  297. char
  298. narrow(char_type __c, char __dfault) const
  299. { return this->do_narrow(__c, __dfault); }
  300. /**
  301. * @brief Narrow array to char array
  302. *
  303. * This function converts each char_type in the input to char using the
  304. * simplest reasonable transformation and writes the results to the
  305. * destination array. For any char_type in the input that cannot be
  306. * converted, @a dfault is used instead. It does so by returning
  307. * ctype<char_type>::do_narrow(__lo, __hi, __dfault, __to).
  308. *
  309. * Note: this is not what you want for codepage conversions. See
  310. * codecvt for that.
  311. *
  312. * @param __lo Pointer to start of range.
  313. * @param __hi Pointer to end of range.
  314. * @param __dfault Char to use if conversion fails.
  315. * @param __to Pointer to the destination array.
  316. * @return @a __hi.
  317. */
  318. const char_type*
  319. narrow(const char_type* __lo, const char_type* __hi,
  320. char __dfault, char* __to) const
  321. { return this->do_narrow(__lo, __hi, __dfault, __to); }
  322. protected:
  323. explicit
  324. __ctype_abstract_base(size_t __refs = 0): facet(__refs) { }
  325. virtual
  326. ~__ctype_abstract_base() { }
  327. /**
  328. * @brief Test char_type classification.
  329. *
  330. * This function finds a mask M for @a c and compares it to mask @a m.
  331. *
  332. * do_is() is a hook for a derived facet to change the behavior of
  333. * classifying. do_is() must always return the same result for the
  334. * same input.
  335. *
  336. * @param __c The char_type to find the mask of.
  337. * @param __m The mask to compare against.
  338. * @return (M & __m) != 0.
  339. */
  340. virtual bool
  341. do_is(mask __m, char_type __c) const = 0;
  342. /**
  343. * @brief Return a mask array.
  344. *
  345. * This function finds the mask for each char_type in the range [lo,hi)
  346. * and successively writes it to vec. vec must have as many elements
  347. * as the input.
  348. *
  349. * do_is() is a hook for a derived facet to change the behavior of
  350. * classifying. do_is() must always return the same result for the
  351. * same input.
  352. *
  353. * @param __lo Pointer to start of range.
  354. * @param __hi Pointer to end of range.
  355. * @param __vec Pointer to an array of mask storage.
  356. * @return @a __hi.
  357. */
  358. virtual const char_type*
  359. do_is(const char_type* __lo, const char_type* __hi,
  360. mask* __vec) const = 0;
  361. /**
  362. * @brief Find char_type matching mask
  363. *
  364. * This function searches for and returns the first char_type c in
  365. * [__lo,__hi) for which is(__m,c) is true.
  366. *
  367. * do_scan_is() is a hook for a derived facet to change the behavior of
  368. * match searching. do_is() must always return the same result for the
  369. * same input.
  370. *
  371. * @param __m The mask to compare against.
  372. * @param __lo Pointer to start of range.
  373. * @param __hi Pointer to end of range.
  374. * @return Pointer to a matching char_type if found, else @a __hi.
  375. */
  376. virtual const char_type*
  377. do_scan_is(mask __m, const char_type* __lo,
  378. const char_type* __hi) const = 0;
  379. /**
  380. * @brief Find char_type not matching mask
  381. *
  382. * This function searches for and returns a pointer to the first
  383. * char_type c of [lo,hi) for which is(m,c) is false.
  384. *
  385. * do_scan_is() is a hook for a derived facet to change the behavior of
  386. * match searching. do_is() must always return the same result for the
  387. * same input.
  388. *
  389. * @param __m The mask to compare against.
  390. * @param __lo Pointer to start of range.
  391. * @param __hi Pointer to end of range.
  392. * @return Pointer to a non-matching char_type if found, else @a __hi.
  393. */
  394. virtual const char_type*
  395. do_scan_not(mask __m, const char_type* __lo,
  396. const char_type* __hi) const = 0;
  397. /**
  398. * @brief Convert to uppercase.
  399. *
  400. * This virtual function converts the char_type argument to uppercase
  401. * if possible. If not possible (for example, '2'), returns the
  402. * argument.
  403. *
  404. * do_toupper() is a hook for a derived facet to change the behavior of
  405. * uppercasing. do_toupper() must always return the same result for
  406. * the same input.
  407. *
  408. * @param __c The char_type to convert.
  409. * @return The uppercase char_type if convertible, else @a __c.
  410. */
  411. virtual char_type
  412. do_toupper(char_type __c) const = 0;
  413. /**
  414. * @brief Convert array to uppercase.
  415. *
  416. * This virtual function converts each char_type in the range [__lo,__hi)
  417. * to uppercase if possible. Other elements remain untouched.
  418. *
  419. * do_toupper() is a hook for a derived facet to change the behavior of
  420. * uppercasing. do_toupper() must always return the same result for
  421. * the same input.
  422. *
  423. * @param __lo Pointer to start of range.
  424. * @param __hi Pointer to end of range.
  425. * @return @a __hi.
  426. */
  427. virtual const char_type*
  428. do_toupper(char_type* __lo, const char_type* __hi) const = 0;
  429. /**
  430. * @brief Convert to lowercase.
  431. *
  432. * This virtual function converts the argument to lowercase if
  433. * possible. If not possible (for example, '2'), returns the argument.
  434. *
  435. * do_tolower() is a hook for a derived facet to change the behavior of
  436. * lowercasing. do_tolower() must always return the same result for
  437. * the same input.
  438. *
  439. * @param __c The char_type to convert.
  440. * @return The lowercase char_type if convertible, else @a __c.
  441. */
  442. virtual char_type
  443. do_tolower(char_type __c) const = 0;
  444. /**
  445. * @brief Convert array to lowercase.
  446. *
  447. * This virtual function converts each char_type in the range [__lo,__hi)
  448. * to lowercase if possible. Other elements remain untouched.
  449. *
  450. * do_tolower() is a hook for a derived facet to change the behavior of
  451. * lowercasing. do_tolower() must always return the same result for
  452. * the same input.
  453. *
  454. * @param __lo Pointer to start of range.
  455. * @param __hi Pointer to end of range.
  456. * @return @a __hi.
  457. */
  458. virtual const char_type*
  459. do_tolower(char_type* __lo, const char_type* __hi) const = 0;
  460. /**
  461. * @brief Widen char
  462. *
  463. * This virtual function converts the char to char_type using the
  464. * simplest reasonable transformation.
  465. *
  466. * do_widen() is a hook for a derived facet to change the behavior of
  467. * widening. do_widen() must always return the same result for the
  468. * same input.
  469. *
  470. * Note: this is not what you want for codepage conversions. See
  471. * codecvt for that.
  472. *
  473. * @param __c The char to convert.
  474. * @return The converted char_type
  475. */
  476. virtual char_type
  477. do_widen(char __c) const = 0;
  478. /**
  479. * @brief Widen char array
  480. *
  481. * This function converts each char in the input to char_type using the
  482. * simplest reasonable transformation.
  483. *
  484. * do_widen() is a hook for a derived facet to change the behavior of
  485. * widening. do_widen() must always return the same result for the
  486. * same input.
  487. *
  488. * Note: this is not what you want for codepage conversions. See
  489. * codecvt for that.
  490. *
  491. * @param __lo Pointer to start range.
  492. * @param __hi Pointer to end of range.
  493. * @param __to Pointer to the destination array.
  494. * @return @a __hi.
  495. */
  496. virtual const char*
  497. do_widen(const char* __lo, const char* __hi, char_type* __to) const = 0;
  498. /**
  499. * @brief Narrow char_type to char
  500. *
  501. * This virtual function converts the argument to char using the
  502. * simplest reasonable transformation. If the conversion fails, dfault
  503. * is returned instead.
  504. *
  505. * do_narrow() is a hook for a derived facet to change the behavior of
  506. * narrowing. do_narrow() must always return the same result for the
  507. * same input.
  508. *
  509. * Note: this is not what you want for codepage conversions. See
  510. * codecvt for that.
  511. *
  512. * @param __c The char_type to convert.
  513. * @param __dfault Char to return if conversion fails.
  514. * @return The converted char.
  515. */
  516. virtual char
  517. do_narrow(char_type __c, char __dfault) const = 0;
  518. /**
  519. * @brief Narrow char_type array to char
  520. *
  521. * This virtual function converts each char_type in the range
  522. * [__lo,__hi) to char using the simplest reasonable
  523. * transformation and writes the results to the destination
  524. * array. For any element in the input that cannot be
  525. * converted, @a __dfault is used instead.
  526. *
  527. * do_narrow() is a hook for a derived facet to change the behavior of
  528. * narrowing. do_narrow() must always return the same result for the
  529. * same input.
  530. *
  531. * Note: this is not what you want for codepage conversions. See
  532. * codecvt for that.
  533. *
  534. * @param __lo Pointer to start of range.
  535. * @param __hi Pointer to end of range.
  536. * @param __dfault Char to use if conversion fails.
  537. * @param __to Pointer to the destination array.
  538. * @return @a __hi.
  539. */
  540. virtual const char_type*
  541. do_narrow(const char_type* __lo, const char_type* __hi,
  542. char __dfault, char* __to) const = 0;
  543. };
  544. /**
  545. * @brief Primary class template ctype facet.
  546. * @ingroup locales
  547. *
  548. * This template class defines classification and conversion functions for
  549. * character sets. It wraps cctype functionality. Ctype gets used by
  550. * streams for many I/O operations.
  551. *
  552. * This template provides the protected virtual functions the developer
  553. * will have to replace in a derived class or specialization to make a
  554. * working facet. The public functions that access them are defined in
  555. * __ctype_abstract_base, to allow for implementation flexibility. See
  556. * ctype<wchar_t> for an example. The functions are documented in
  557. * __ctype_abstract_base.
  558. *
  559. * Note: implementations are provided for all the protected virtual
  560. * functions, but will likely not be useful.
  561. */
  562. template<typename _CharT>
  563. class ctype : public __ctype_abstract_base<_CharT>
  564. {
  565. public:
  566. // Types:
  567. typedef _CharT char_type;
  568. typedef typename __ctype_abstract_base<_CharT>::mask mask;
  569. /// The facet id for ctype<char_type>
  570. static locale::id id;
  571. explicit
  572. ctype(size_t __refs = 0) : __ctype_abstract_base<_CharT>(__refs) { }
  573. protected:
  574. virtual
  575. ~ctype();
  576. virtual bool
  577. do_is(mask __m, char_type __c) const;
  578. virtual const char_type*
  579. do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const;
  580. virtual const char_type*
  581. do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const;
  582. virtual const char_type*
  583. do_scan_not(mask __m, const char_type* __lo,
  584. const char_type* __hi) const;
  585. virtual char_type
  586. do_toupper(char_type __c) const;
  587. virtual const char_type*
  588. do_toupper(char_type* __lo, const char_type* __hi) const;
  589. virtual char_type
  590. do_tolower(char_type __c) const;
  591. virtual const char_type*
  592. do_tolower(char_type* __lo, const char_type* __hi) const;
  593. virtual char_type
  594. do_widen(char __c) const;
  595. virtual const char*
  596. do_widen(const char* __lo, const char* __hi, char_type* __dest) const;
  597. virtual char
  598. do_narrow(char_type, char __dfault) const;
  599. virtual const char_type*
  600. do_narrow(const char_type* __lo, const char_type* __hi,
  601. char __dfault, char* __to) const;
  602. };
  603. template<typename _CharT>
  604. locale::id ctype<_CharT>::id;
  605. /**
  606. * @brief The ctype<char> specialization.
  607. * @ingroup locales
  608. *
  609. * This class defines classification and conversion functions for
  610. * the char type. It gets used by char streams for many I/O
  611. * operations. The char specialization provides a number of
  612. * optimizations as well.
  613. */
  614. template<>
  615. class ctype<char> : public locale::facet, public ctype_base
  616. {
  617. public:
  618. // Types:
  619. /// Typedef for the template parameter char.
  620. typedef char char_type;
  621. protected:
  622. // Data Members:
  623. __c_locale _M_c_locale_ctype;
  624. bool _M_del;
  625. __to_type _M_toupper;
  626. __to_type _M_tolower;
  627. const mask* _M_table;
  628. mutable char _M_widen_ok;
  629. mutable char _M_widen[1 + static_cast<unsigned char>(-1)];
  630. mutable char _M_narrow[1 + static_cast<unsigned char>(-1)];
  631. mutable char _M_narrow_ok; // 0 uninitialized, 1 init,
  632. // 2 memcpy can't be used
  633. public:
  634. /// The facet id for ctype<char>
  635. static locale::id id;
  636. /// The size of the mask table. It is SCHAR_MAX + 1.
  637. static const size_t table_size = 1 + static_cast<unsigned char>(-1);
  638. /**
  639. * @brief Constructor performs initialization.
  640. *
  641. * This is the constructor provided by the standard.
  642. *
  643. * @param __table If non-zero, table is used as the per-char mask.
  644. * Else classic_table() is used.
  645. * @param __del If true, passes ownership of table to this facet.
  646. * @param __refs Passed to the base facet class.
  647. */
  648. explicit
  649. ctype(const mask* __table = 0, bool __del = false, size_t __refs = 0);
  650. /**
  651. * @brief Constructor performs static initialization.
  652. *
  653. * This constructor is used to construct the initial C locale facet.
  654. *
  655. * @param __cloc Handle to C locale data.
  656. * @param __table If non-zero, table is used as the per-char mask.
  657. * @param __del If true, passes ownership of table to this facet.
  658. * @param __refs Passed to the base facet class.
  659. */
  660. explicit
  661. ctype(__c_locale __cloc, const mask* __table = 0, bool __del = false,
  662. size_t __refs = 0);
  663. /**
  664. * @brief Test char classification.
  665. *
  666. * This function compares the mask table[c] to @a __m.
  667. *
  668. * @param __c The char to compare the mask of.
  669. * @param __m The mask to compare against.
  670. * @return True if __m & table[__c] is true, false otherwise.
  671. */
  672. inline bool
  673. is(mask __m, char __c) const;
  674. /**
  675. * @brief Return a mask array.
  676. *
  677. * This function finds the mask for each char in the range [lo, hi) and
  678. * successively writes it to vec. vec must have as many elements as
  679. * the char array.
  680. *
  681. * @param __lo Pointer to start of range.
  682. * @param __hi Pointer to end of range.
  683. * @param __vec Pointer to an array of mask storage.
  684. * @return @a __hi.
  685. */
  686. inline const char*
  687. is(const char* __lo, const char* __hi, mask* __vec) const;
  688. /**
  689. * @brief Find char matching a mask
  690. *
  691. * This function searches for and returns the first char in [lo,hi) for
  692. * which is(m,char) is true.
  693. *
  694. * @param __m The mask to compare against.
  695. * @param __lo Pointer to start of range.
  696. * @param __hi Pointer to end of range.
  697. * @return Pointer to a matching char if found, else @a __hi.
  698. */
  699. inline const char*
  700. scan_is(mask __m, const char* __lo, const char* __hi) const;
  701. /**
  702. * @brief Find char not matching a mask
  703. *
  704. * This function searches for and returns a pointer to the first char
  705. * in [__lo,__hi) for which is(m,char) is false.
  706. *
  707. * @param __m The mask to compare against.
  708. * @param __lo Pointer to start of range.
  709. * @param __hi Pointer to end of range.
  710. * @return Pointer to a non-matching char if found, else @a __hi.
  711. */
  712. inline const char*
  713. scan_not(mask __m, const char* __lo, const char* __hi) const;
  714. /**
  715. * @brief Convert to uppercase.
  716. *
  717. * This function converts the char argument to uppercase if possible.
  718. * If not possible (for example, '2'), returns the argument.
  719. *
  720. * toupper() acts as if it returns ctype<char>::do_toupper(c).
  721. * do_toupper() must always return the same result for the same input.
  722. *
  723. * @param __c The char to convert.
  724. * @return The uppercase char if convertible, else @a __c.
  725. */
  726. char_type
  727. toupper(char_type __c) const
  728. { return this->do_toupper(__c); }
  729. /**
  730. * @brief Convert array to uppercase.
  731. *
  732. * This function converts each char in the range [__lo,__hi) to uppercase
  733. * if possible. Other chars remain untouched.
  734. *
  735. * toupper() acts as if it returns ctype<char>:: do_toupper(__lo, __hi).
  736. * do_toupper() must always return the same result for the same input.
  737. *
  738. * @param __lo Pointer to first char in range.
  739. * @param __hi Pointer to end of range.
  740. * @return @a __hi.
  741. */
  742. const char_type*
  743. toupper(char_type *__lo, const char_type* __hi) const
  744. { return this->do_toupper(__lo, __hi); }
  745. /**
  746. * @brief Convert to lowercase.
  747. *
  748. * This function converts the char argument to lowercase if possible.
  749. * If not possible (for example, '2'), returns the argument.
  750. *
  751. * tolower() acts as if it returns ctype<char>::do_tolower(__c).
  752. * do_tolower() must always return the same result for the same input.
  753. *
  754. * @param __c The char to convert.
  755. * @return The lowercase char if convertible, else @a __c.
  756. */
  757. char_type
  758. tolower(char_type __c) const
  759. { return this->do_tolower(__c); }
  760. /**
  761. * @brief Convert array to lowercase.
  762. *
  763. * This function converts each char in the range [lo,hi) to lowercase
  764. * if possible. Other chars remain untouched.
  765. *
  766. * tolower() acts as if it returns ctype<char>:: do_tolower(__lo, __hi).
  767. * do_tolower() must always return the same result for the same input.
  768. *
  769. * @param __lo Pointer to first char in range.
  770. * @param __hi Pointer to end of range.
  771. * @return @a __hi.
  772. */
  773. const char_type*
  774. tolower(char_type* __lo, const char_type* __hi) const
  775. { return this->do_tolower(__lo, __hi); }
  776. /**
  777. * @brief Widen char
  778. *
  779. * This function converts the char to char_type using the simplest
  780. * reasonable transformation. For an underived ctype<char> facet, the
  781. * argument will be returned unchanged.
  782. *
  783. * This function works as if it returns ctype<char>::do_widen(c).
  784. * do_widen() must always return the same result for the same input.
  785. *
  786. * Note: this is not what you want for codepage conversions. See
  787. * codecvt for that.
  788. *
  789. * @param __c The char to convert.
  790. * @return The converted character.
  791. */
  792. char_type
  793. widen(char __c) const
  794. {
  795. if (_M_widen_ok)
  796. return _M_widen[static_cast<unsigned char>(__c)];
  797. this->_M_widen_init();
  798. return this->do_widen(__c);
  799. }
  800. /**
  801. * @brief Widen char array
  802. *
  803. * This function converts each char in the input to char using the
  804. * simplest reasonable transformation. For an underived ctype<char>
  805. * facet, the argument will be copied unchanged.
  806. *
  807. * This function works as if it returns ctype<char>::do_widen(c).
  808. * do_widen() must always return the same result for the same input.
  809. *
  810. * Note: this is not what you want for codepage conversions. See
  811. * codecvt for that.
  812. *
  813. * @param __lo Pointer to first char in range.
  814. * @param __hi Pointer to end of range.
  815. * @param __to Pointer to the destination array.
  816. * @return @a __hi.
  817. */
  818. const char*
  819. widen(const char* __lo, const char* __hi, char_type* __to) const
  820. {
  821. if (_M_widen_ok == 1)
  822. {
  823. if (__builtin_expect(__hi != __lo, true))
  824. __builtin_memcpy(__to, __lo, __hi - __lo);
  825. return __hi;
  826. }
  827. if (!_M_widen_ok)
  828. _M_widen_init();
  829. return this->do_widen(__lo, __hi, __to);
  830. }
  831. /**
  832. * @brief Narrow char
  833. *
  834. * This function converts the char to char using the simplest
  835. * reasonable transformation. If the conversion fails, dfault is
  836. * returned instead. For an underived ctype<char> facet, @a c
  837. * will be returned unchanged.
  838. *
  839. * This function works as if it returns ctype<char>::do_narrow(c).
  840. * do_narrow() must always return the same result for the same input.
  841. *
  842. * Note: this is not what you want for codepage conversions. See
  843. * codecvt for that.
  844. *
  845. * @param __c The char to convert.
  846. * @param __dfault Char to return if conversion fails.
  847. * @return The converted character.
  848. */
  849. char
  850. narrow(char_type __c, char __dfault) const
  851. {
  852. if (_M_narrow[static_cast<unsigned char>(__c)])
  853. return _M_narrow[static_cast<unsigned char>(__c)];
  854. const char __t = do_narrow(__c, __dfault);
  855. if (__t != __dfault)
  856. _M_narrow[static_cast<unsigned char>(__c)] = __t;
  857. return __t;
  858. }
  859. /**
  860. * @brief Narrow char array
  861. *
  862. * This function converts each char in the input to char using the
  863. * simplest reasonable transformation and writes the results to the
  864. * destination array. For any char in the input that cannot be
  865. * converted, @a dfault is used instead. For an underived ctype<char>
  866. * facet, the argument will be copied unchanged.
  867. *
  868. * This function works as if it returns ctype<char>::do_narrow(lo, hi,
  869. * dfault, to). do_narrow() must always return the same result for the
  870. * same input.
  871. *
  872. * Note: this is not what you want for codepage conversions. See
  873. * codecvt for that.
  874. *
  875. * @param __lo Pointer to start of range.
  876. * @param __hi Pointer to end of range.
  877. * @param __dfault Char to use if conversion fails.
  878. * @param __to Pointer to the destination array.
  879. * @return @a __hi.
  880. */
  881. const char_type*
  882. narrow(const char_type* __lo, const char_type* __hi,
  883. char __dfault, char* __to) const
  884. {
  885. if (__builtin_expect(_M_narrow_ok == 1, true))
  886. {
  887. if (__builtin_expect(__hi != __lo, true))
  888. __builtin_memcpy(__to, __lo, __hi - __lo);
  889. return __hi;
  890. }
  891. if (!_M_narrow_ok)
  892. _M_narrow_init();
  893. return this->do_narrow(__lo, __hi, __dfault, __to);
  894. }
  895. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  896. // DR 695. ctype<char>::classic_table() not accessible.
  897. /// Returns a pointer to the mask table provided to the constructor, or
  898. /// the default from classic_table() if none was provided.
  899. const mask*
  900. table() const throw()
  901. { return _M_table; }
  902. /// Returns a pointer to the C locale mask table.
  903. static const mask*
  904. classic_table() throw();
  905. protected:
  906. /**
  907. * @brief Destructor.
  908. *
  909. * This function deletes table() if @a del was true in the
  910. * constructor.
  911. */
  912. virtual
  913. ~ctype();
  914. /**
  915. * @brief Convert to uppercase.
  916. *
  917. * This virtual function converts the char argument to uppercase if
  918. * possible. If not possible (for example, '2'), returns the argument.
  919. *
  920. * do_toupper() is a hook for a derived facet to change the behavior of
  921. * uppercasing. do_toupper() must always return the same result for
  922. * the same input.
  923. *
  924. * @param __c The char to convert.
  925. * @return The uppercase char if convertible, else @a __c.
  926. */
  927. virtual char_type
  928. do_toupper(char_type __c) const;
  929. /**
  930. * @brief Convert array to uppercase.
  931. *
  932. * This virtual function converts each char in the range [lo,hi) to
  933. * uppercase if possible. Other chars remain untouched.
  934. *
  935. * do_toupper() is a hook for a derived facet to change the behavior of
  936. * uppercasing. do_toupper() must always return the same result for
  937. * the same input.
  938. *
  939. * @param __lo Pointer to start of range.
  940. * @param __hi Pointer to end of range.
  941. * @return @a __hi.
  942. */
  943. virtual const char_type*
  944. do_toupper(char_type* __lo, const char_type* __hi) const;
  945. /**
  946. * @brief Convert to lowercase.
  947. *
  948. * This virtual function converts the char argument to lowercase if
  949. * possible. If not possible (for example, '2'), returns the argument.
  950. *
  951. * do_tolower() is a hook for a derived facet to change the behavior of
  952. * lowercasing. do_tolower() must always return the same result for
  953. * the same input.
  954. *
  955. * @param __c The char to convert.
  956. * @return The lowercase char if convertible, else @a __c.
  957. */
  958. virtual char_type
  959. do_tolower(char_type __c) const;
  960. /**
  961. * @brief Convert array to lowercase.
  962. *
  963. * This virtual function converts each char in the range [lo,hi) to
  964. * lowercase if possible. Other chars remain untouched.
  965. *
  966. * do_tolower() is a hook for a derived facet to change the behavior of
  967. * lowercasing. do_tolower() must always return the same result for
  968. * the same input.
  969. *
  970. * @param __lo Pointer to first char in range.
  971. * @param __hi Pointer to end of range.
  972. * @return @a __hi.
  973. */
  974. virtual const char_type*
  975. do_tolower(char_type* __lo, const char_type* __hi) const;
  976. /**
  977. * @brief Widen char
  978. *
  979. * This virtual function converts the char to char using the simplest
  980. * reasonable transformation. For an underived ctype<char> facet, the
  981. * argument will be returned unchanged.
  982. *
  983. * do_widen() is a hook for a derived facet to change the behavior of
  984. * widening. do_widen() must always return the same result for the
  985. * same input.
  986. *
  987. * Note: this is not what you want for codepage conversions. See
  988. * codecvt for that.
  989. *
  990. * @param __c The char to convert.
  991. * @return The converted character.
  992. */
  993. virtual char_type
  994. do_widen(char __c) const
  995. { return __c; }
  996. /**
  997. * @brief Widen char array
  998. *
  999. * This function converts each char in the range [lo,hi) to char using
  1000. * the simplest reasonable transformation. For an underived
  1001. * ctype<char> facet, the argument will be copied unchanged.
  1002. *
  1003. * do_widen() is a hook for a derived facet to change the behavior of
  1004. * widening. do_widen() must always return the same result for the
  1005. * same input.
  1006. *
  1007. * Note: this is not what you want for codepage conversions. See
  1008. * codecvt for that.
  1009. *
  1010. * @param __lo Pointer to start of range.
  1011. * @param __hi Pointer to end of range.
  1012. * @param __to Pointer to the destination array.
  1013. * @return @a __hi.
  1014. */
  1015. virtual const char*
  1016. do_widen(const char* __lo, const char* __hi, char_type* __to) const
  1017. {
  1018. if (__builtin_expect(__hi != __lo, true))
  1019. __builtin_memcpy(__to, __lo, __hi - __lo);
  1020. return __hi;
  1021. }
  1022. /**
  1023. * @brief Narrow char
  1024. *
  1025. * This virtual function converts the char to char using the simplest
  1026. * reasonable transformation. If the conversion fails, dfault is
  1027. * returned instead. For an underived ctype<char> facet, @a c will be
  1028. * returned unchanged.
  1029. *
  1030. * do_narrow() is a hook for a derived facet to change the behavior of
  1031. * narrowing. do_narrow() must always return the same result for the
  1032. * same input.
  1033. *
  1034. * Note: this is not what you want for codepage conversions. See
  1035. * codecvt for that.
  1036. *
  1037. * @param __c The char to convert.
  1038. * @param __dfault Char to return if conversion fails.
  1039. * @return The converted char.
  1040. */
  1041. virtual char
  1042. do_narrow(char_type __c, char __dfault __attribute__((__unused__))) const
  1043. { return __c; }
  1044. /**
  1045. * @brief Narrow char array to char array
  1046. *
  1047. * This virtual function converts each char in the range [lo,hi) to
  1048. * char using the simplest reasonable transformation and writes the
  1049. * results to the destination array. For any char in the input that
  1050. * cannot be converted, @a dfault is used instead. For an underived
  1051. * ctype<char> facet, the argument will be copied unchanged.
  1052. *
  1053. * do_narrow() is a hook for a derived facet to change the behavior of
  1054. * narrowing. do_narrow() must always return the same result for the
  1055. * same input.
  1056. *
  1057. * Note: this is not what you want for codepage conversions. See
  1058. * codecvt for that.
  1059. *
  1060. * @param __lo Pointer to start of range.
  1061. * @param __hi Pointer to end of range.
  1062. * @param __dfault Char to use if conversion fails.
  1063. * @param __to Pointer to the destination array.
  1064. * @return @a __hi.
  1065. */
  1066. virtual const char_type*
  1067. do_narrow(const char_type* __lo, const char_type* __hi,
  1068. char __dfault __attribute__((__unused__)), char* __to) const
  1069. {
  1070. if (__builtin_expect(__hi != __lo, true))
  1071. __builtin_memcpy(__to, __lo, __hi - __lo);
  1072. return __hi;
  1073. }
  1074. private:
  1075. void _M_narrow_init() const;
  1076. void _M_widen_init() const;
  1077. };
  1078. #ifdef _GLIBCXX_USE_WCHAR_T
  1079. /**
  1080. * @brief The ctype<wchar_t> specialization.
  1081. * @ingroup locales
  1082. *
  1083. * This class defines classification and conversion functions for the
  1084. * wchar_t type. It gets used by wchar_t streams for many I/O operations.
  1085. * The wchar_t specialization provides a number of optimizations as well.
  1086. *
  1087. * ctype<wchar_t> inherits its public methods from
  1088. * __ctype_abstract_base<wchar_t>.
  1089. */
  1090. template<>
  1091. class ctype<wchar_t> : public __ctype_abstract_base<wchar_t>
  1092. {
  1093. public:
  1094. // Types:
  1095. /// Typedef for the template parameter wchar_t.
  1096. typedef wchar_t char_type;
  1097. typedef wctype_t __wmask_type;
  1098. protected:
  1099. __c_locale _M_c_locale_ctype;
  1100. // Pre-computed narrowed and widened chars.
  1101. bool _M_narrow_ok;
  1102. char _M_narrow[128];
  1103. wint_t _M_widen[1 + static_cast<unsigned char>(-1)];
  1104. // Pre-computed elements for do_is.
  1105. mask _M_bit[16];
  1106. __wmask_type _M_wmask[16];
  1107. public:
  1108. // Data Members:
  1109. /// The facet id for ctype<wchar_t>
  1110. static locale::id id;
  1111. /**
  1112. * @brief Constructor performs initialization.
  1113. *
  1114. * This is the constructor provided by the standard.
  1115. *
  1116. * @param __refs Passed to the base facet class.
  1117. */
  1118. explicit
  1119. ctype(size_t __refs = 0);
  1120. /**
  1121. * @brief Constructor performs static initialization.
  1122. *
  1123. * This constructor is used to construct the initial C locale facet.
  1124. *
  1125. * @param __cloc Handle to C locale data.
  1126. * @param __refs Passed to the base facet class.
  1127. */
  1128. explicit
  1129. ctype(__c_locale __cloc, size_t __refs = 0);
  1130. protected:
  1131. __wmask_type
  1132. _M_convert_to_wmask(const mask __m) const throw();
  1133. /// Destructor
  1134. virtual
  1135. ~ctype();
  1136. /**
  1137. * @brief Test wchar_t classification.
  1138. *
  1139. * This function finds a mask M for @a c and compares it to mask @a m.
  1140. *
  1141. * do_is() is a hook for a derived facet to change the behavior of
  1142. * classifying. do_is() must always return the same result for the
  1143. * same input.
  1144. *
  1145. * @param __c The wchar_t to find the mask of.
  1146. * @param __m The mask to compare against.
  1147. * @return (M & __m) != 0.
  1148. */
  1149. virtual bool
  1150. do_is(mask __m, char_type __c) const;
  1151. /**
  1152. * @brief Return a mask array.
  1153. *
  1154. * This function finds the mask for each wchar_t in the range [lo,hi)
  1155. * and successively writes it to vec. vec must have as many elements
  1156. * as the input.
  1157. *
  1158. * do_is() is a hook for a derived facet to change the behavior of
  1159. * classifying. do_is() must always return the same result for the
  1160. * same input.
  1161. *
  1162. * @param __lo Pointer to start of range.
  1163. * @param __hi Pointer to end of range.
  1164. * @param __vec Pointer to an array of mask storage.
  1165. * @return @a __hi.
  1166. */
  1167. virtual const char_type*
  1168. do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const;
  1169. /**
  1170. * @brief Find wchar_t matching mask
  1171. *
  1172. * This function searches for and returns the first wchar_t c in
  1173. * [__lo,__hi) for which is(__m,c) is true.
  1174. *
  1175. * do_scan_is() is a hook for a derived facet to change the behavior of
  1176. * match searching. do_is() must always return the same result for the
  1177. * same input.
  1178. *
  1179. * @param __m The mask to compare against.
  1180. * @param __lo Pointer to start of range.
  1181. * @param __hi Pointer to end of range.
  1182. * @return Pointer to a matching wchar_t if found, else @a __hi.
  1183. */
  1184. virtual const char_type*
  1185. do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const;
  1186. /**
  1187. * @brief Find wchar_t not matching mask
  1188. *
  1189. * This function searches for and returns a pointer to the first
  1190. * wchar_t c of [__lo,__hi) for which is(__m,c) is false.
  1191. *
  1192. * do_scan_is() is a hook for a derived facet to change the behavior of
  1193. * match searching. do_is() must always return the same result for the
  1194. * same input.
  1195. *
  1196. * @param __m The mask to compare against.
  1197. * @param __lo Pointer to start of range.
  1198. * @param __hi Pointer to end of range.
  1199. * @return Pointer to a non-matching wchar_t if found, else @a __hi.
  1200. */
  1201. virtual const char_type*
  1202. do_scan_not(mask __m, const char_type* __lo,
  1203. const char_type* __hi) const;
  1204. /**
  1205. * @brief Convert to uppercase.
  1206. *
  1207. * This virtual function converts the wchar_t argument to uppercase if
  1208. * possible. If not possible (for example, '2'), returns the argument.
  1209. *
  1210. * do_toupper() is a hook for a derived facet to change the behavior of
  1211. * uppercasing. do_toupper() must always return the same result for
  1212. * the same input.
  1213. *
  1214. * @param __c The wchar_t to convert.
  1215. * @return The uppercase wchar_t if convertible, else @a __c.
  1216. */
  1217. virtual char_type
  1218. do_toupper(char_type __c) const;
  1219. /**
  1220. * @brief Convert array to uppercase.
  1221. *
  1222. * This virtual function converts each wchar_t in the range [lo,hi) to
  1223. * uppercase if possible. Other elements remain untouched.
  1224. *
  1225. * do_toupper() is a hook for a derived facet to change the behavior of
  1226. * uppercasing. do_toupper() must always return the same result for
  1227. * the same input.
  1228. *
  1229. * @param __lo Pointer to start of range.
  1230. * @param __hi Pointer to end of range.
  1231. * @return @a __hi.
  1232. */
  1233. virtual const char_type*
  1234. do_toupper(char_type* __lo, const char_type* __hi) const;
  1235. /**
  1236. * @brief Convert to lowercase.
  1237. *
  1238. * This virtual function converts the argument to lowercase if
  1239. * possible. If not possible (for example, '2'), returns the argument.
  1240. *
  1241. * do_tolower() is a hook for a derived facet to change the behavior of
  1242. * lowercasing. do_tolower() must always return the same result for
  1243. * the same input.
  1244. *
  1245. * @param __c The wchar_t to convert.
  1246. * @return The lowercase wchar_t if convertible, else @a __c.
  1247. */
  1248. virtual char_type
  1249. do_tolower(char_type __c) const;
  1250. /**
  1251. * @brief Convert array to lowercase.
  1252. *
  1253. * This virtual function converts each wchar_t in the range [lo,hi) to
  1254. * lowercase if possible. Other elements remain untouched.
  1255. *
  1256. * do_tolower() is a hook for a derived facet to change the behavior of
  1257. * lowercasing. do_tolower() must always return the same result for
  1258. * the same input.
  1259. *
  1260. * @param __lo Pointer to start of range.
  1261. * @param __hi Pointer to end of range.
  1262. * @return @a __hi.
  1263. */
  1264. virtual const char_type*
  1265. do_tolower(char_type* __lo, const char_type* __hi) const;
  1266. /**
  1267. * @brief Widen char to wchar_t
  1268. *
  1269. * This virtual function converts the char to wchar_t using the
  1270. * simplest reasonable transformation. For an underived ctype<wchar_t>
  1271. * facet, the argument will be cast to wchar_t.
  1272. *
  1273. * do_widen() is a hook for a derived facet to change the behavior of
  1274. * widening. do_widen() must always return the same result for the
  1275. * same input.
  1276. *
  1277. * Note: this is not what you want for codepage conversions. See
  1278. * codecvt for that.
  1279. *
  1280. * @param __c The char to convert.
  1281. * @return The converted wchar_t.
  1282. */
  1283. virtual char_type
  1284. do_widen(char __c) const;
  1285. /**
  1286. * @brief Widen char array to wchar_t array
  1287. *
  1288. * This function converts each char in the input to wchar_t using the
  1289. * simplest reasonable transformation. For an underived ctype<wchar_t>
  1290. * facet, the argument will be copied, casting each element to wchar_t.
  1291. *
  1292. * do_widen() is a hook for a derived facet to change the behavior of
  1293. * widening. do_widen() must always return the same result for the
  1294. * same input.
  1295. *
  1296. * Note: this is not what you want for codepage conversions. See
  1297. * codecvt for that.
  1298. *
  1299. * @param __lo Pointer to start range.
  1300. * @param __hi Pointer to end of range.
  1301. * @param __to Pointer to the destination array.
  1302. * @return @a __hi.
  1303. */
  1304. virtual const char*
  1305. do_widen(const char* __lo, const char* __hi, char_type* __to) const;
  1306. /**
  1307. * @brief Narrow wchar_t to char
  1308. *
  1309. * This virtual function converts the argument to char using
  1310. * the simplest reasonable transformation. If the conversion
  1311. * fails, dfault is returned instead. For an underived
  1312. * ctype<wchar_t> facet, @a c will be cast to char and
  1313. * returned.
  1314. *
  1315. * do_narrow() is a hook for a derived facet to change the
  1316. * behavior of narrowing. do_narrow() must always return the
  1317. * same result for the same input.
  1318. *
  1319. * Note: this is not what you want for codepage conversions. See
  1320. * codecvt for that.
  1321. *
  1322. * @param __c The wchar_t to convert.
  1323. * @param __dfault Char to return if conversion fails.
  1324. * @return The converted char.
  1325. */
  1326. virtual char
  1327. do_narrow(char_type __c, char __dfault) const;
  1328. /**
  1329. * @brief Narrow wchar_t array to char array
  1330. *
  1331. * This virtual function converts each wchar_t in the range [lo,hi) to
  1332. * char using the simplest reasonable transformation and writes the
  1333. * results to the destination array. For any wchar_t in the input that
  1334. * cannot be converted, @a dfault is used instead. For an underived
  1335. * ctype<wchar_t> facet, the argument will be copied, casting each
  1336. * element to char.
  1337. *
  1338. * do_narrow() is a hook for a derived facet to change the behavior of
  1339. * narrowing. do_narrow() must always return the same result for the
  1340. * same input.
  1341. *
  1342. * Note: this is not what you want for codepage conversions. See
  1343. * codecvt for that.
  1344. *
  1345. * @param __lo Pointer to start of range.
  1346. * @param __hi Pointer to end of range.
  1347. * @param __dfault Char to use if conversion fails.
  1348. * @param __to Pointer to the destination array.
  1349. * @return @a __hi.
  1350. */
  1351. virtual const char_type*
  1352. do_narrow(const char_type* __lo, const char_type* __hi,
  1353. char __dfault, char* __to) const;
  1354. // For use at construction time only.
  1355. void
  1356. _M_initialize_ctype() throw();
  1357. };
  1358. #endif //_GLIBCXX_USE_WCHAR_T
  1359. /// class ctype_byname [22.2.1.2].
  1360. template<typename _CharT>
  1361. class ctype_byname : public ctype<_CharT>
  1362. {
  1363. public:
  1364. typedef typename ctype<_CharT>::mask mask;
  1365. explicit
  1366. ctype_byname(const char* __s, size_t __refs = 0);
  1367. #if __cplusplus >= 201103L
  1368. explicit
  1369. ctype_byname(const string& __s, size_t __refs = 0)
  1370. : ctype_byname(__s.c_str(), __refs) { }
  1371. #endif
  1372. protected:
  1373. virtual
  1374. ~ctype_byname() { }
  1375. };
  1376. /// 22.2.1.4 Class ctype_byname specializations.
  1377. template<>
  1378. class ctype_byname<char> : public ctype<char>
  1379. {
  1380. public:
  1381. explicit
  1382. ctype_byname(const char* __s, size_t __refs = 0);
  1383. #if __cplusplus >= 201103L
  1384. explicit
  1385. ctype_byname(const string& __s, size_t __refs = 0);
  1386. #endif
  1387. protected:
  1388. virtual
  1389. ~ctype_byname();
  1390. };
  1391. #ifdef _GLIBCXX_USE_WCHAR_T
  1392. template<>
  1393. class ctype_byname<wchar_t> : public ctype<wchar_t>
  1394. {
  1395. public:
  1396. explicit
  1397. ctype_byname(const char* __s, size_t __refs = 0);
  1398. #if __cplusplus >= 201103L
  1399. explicit
  1400. ctype_byname(const string& __s, size_t __refs = 0);
  1401. #endif
  1402. protected:
  1403. virtual
  1404. ~ctype_byname();
  1405. };
  1406. #endif
  1407. _GLIBCXX_END_NAMESPACE_VERSION
  1408. } // namespace
  1409. // Include host and configuration specific ctype inlines.
  1410. #include <bits/ctype_inline.h>
  1411. namespace std _GLIBCXX_VISIBILITY(default)
  1412. {
  1413. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  1414. // 22.2.2 The numeric category.
  1415. class __num_base
  1416. {
  1417. public:
  1418. // NB: Code depends on the order of _S_atoms_out elements.
  1419. // Below are the indices into _S_atoms_out.
  1420. enum
  1421. {
  1422. _S_ominus,
  1423. _S_oplus,
  1424. _S_ox,
  1425. _S_oX,
  1426. _S_odigits,
  1427. _S_odigits_end = _S_odigits + 16,
  1428. _S_oudigits = _S_odigits_end,
  1429. _S_oudigits_end = _S_oudigits + 16,
  1430. _S_oe = _S_odigits + 14, // For scientific notation, 'e'
  1431. _S_oE = _S_oudigits + 14, // For scientific notation, 'E'
  1432. _S_oend = _S_oudigits_end
  1433. };
  1434. // A list of valid numeric literals for output. This array
  1435. // contains chars that will be passed through the current locale's
  1436. // ctype<_CharT>.widen() and then used to render numbers.
  1437. // For the standard "C" locale, this is
  1438. // "-+xX0123456789abcdef0123456789ABCDEF".
  1439. static const char* _S_atoms_out;
  1440. // String literal of acceptable (narrow) input, for num_get.
  1441. // "-+xX0123456789abcdefABCDEF"
  1442. static const char* _S_atoms_in;
  1443. enum
  1444. {
  1445. _S_iminus,
  1446. _S_iplus,
  1447. _S_ix,
  1448. _S_iX,
  1449. _S_izero,
  1450. _S_ie = _S_izero + 14,
  1451. _S_iE = _S_izero + 20,
  1452. _S_iend = 26
  1453. };
  1454. // num_put
  1455. // Construct and return valid scanf format for floating point types.
  1456. static void
  1457. _S_format_float(const ios_base& __io, char* __fptr, char __mod) throw();
  1458. };
  1459. template<typename _CharT>
  1460. struct __numpunct_cache : public locale::facet
  1461. {
  1462. const char* _M_grouping;
  1463. size_t _M_grouping_size;
  1464. bool _M_use_grouping;
  1465. const _CharT* _M_truename;
  1466. size_t _M_truename_size;
  1467. const _CharT* _M_falsename;
  1468. size_t _M_falsename_size;
  1469. _CharT _M_decimal_point;
  1470. _CharT _M_thousands_sep;
  1471. // A list of valid numeric literals for output: in the standard
  1472. // "C" locale, this is "-+xX0123456789abcdef0123456789ABCDEF".
  1473. // This array contains the chars after having been passed
  1474. // through the current locale's ctype<_CharT>.widen().
  1475. _CharT _M_atoms_out[__num_base::_S_oend];
  1476. // A list of valid numeric literals for input: in the standard
  1477. // "C" locale, this is "-+xX0123456789abcdefABCDEF"
  1478. // This array contains the chars after having been passed
  1479. // through the current locale's ctype<_CharT>.widen().
  1480. _CharT _M_atoms_in[__num_base::_S_iend];
  1481. bool _M_allocated;
  1482. __numpunct_cache(size_t __refs = 0)
  1483. : facet(__refs), _M_grouping(0), _M_grouping_size(0),
  1484. _M_use_grouping(false),
  1485. _M_truename(0), _M_truename_size(0), _M_falsename(0),
  1486. _M_falsename_size(0), _M_decimal_point(_CharT()),
  1487. _M_thousands_sep(_CharT()), _M_allocated(false)
  1488. { }
  1489. ~__numpunct_cache();
  1490. void
  1491. _M_cache(const locale& __loc);
  1492. private:
  1493. __numpunct_cache&
  1494. operator=(const __numpunct_cache&);
  1495. explicit
  1496. __numpunct_cache(const __numpunct_cache&);
  1497. };
  1498. template<typename _CharT>
  1499. __numpunct_cache<_CharT>::~__numpunct_cache()
  1500. {
  1501. if (_M_allocated)
  1502. {
  1503. delete [] _M_grouping;
  1504. delete [] _M_truename;
  1505. delete [] _M_falsename;
  1506. }
  1507. }
  1508. _GLIBCXX_BEGIN_NAMESPACE_CXX11
  1509. /**
  1510. * @brief Primary class template numpunct.
  1511. * @ingroup locales
  1512. *
  1513. * This facet stores several pieces of information related to printing and
  1514. * scanning numbers, such as the decimal point character. It takes a
  1515. * template parameter specifying the char type. The numpunct facet is
  1516. * used by streams for many I/O operations involving numbers.
  1517. *
  1518. * The numpunct template uses protected virtual functions to provide the
  1519. * actual results. The public accessors forward the call to the virtual
  1520. * functions. These virtual functions are hooks for developers to
  1521. * implement the behavior they require from a numpunct facet.
  1522. */
  1523. template<typename _CharT>
  1524. class numpunct : public locale::facet
  1525. {
  1526. public:
  1527. // Types:
  1528. //@{
  1529. /// Public typedefs
  1530. typedef _CharT char_type;
  1531. typedef basic_string<_CharT> string_type;
  1532. //@}
  1533. typedef __numpunct_cache<_CharT> __cache_type;
  1534. protected:
  1535. __cache_type* _M_data;
  1536. public:
  1537. /// Numpunct facet id.
  1538. static locale::id id;
  1539. /**
  1540. * @brief Numpunct constructor.
  1541. *
  1542. * @param __refs Refcount to pass to the base class.
  1543. */
  1544. explicit
  1545. numpunct(size_t __refs = 0)
  1546. : facet(__refs), _M_data(0)
  1547. { _M_initialize_numpunct(); }
  1548. /**
  1549. * @brief Internal constructor. Not for general use.
  1550. *
  1551. * This is a constructor for use by the library itself to set up the
  1552. * predefined locale facets.
  1553. *
  1554. * @param __cache __numpunct_cache object.
  1555. * @param __refs Refcount to pass to the base class.
  1556. */
  1557. explicit
  1558. numpunct(__cache_type* __cache, size_t __refs = 0)
  1559. : facet(__refs), _M_data(__cache)
  1560. { _M_initialize_numpunct(); }
  1561. /**
  1562. * @brief Internal constructor. Not for general use.
  1563. *
  1564. * This is a constructor for use by the library itself to set up new
  1565. * locales.
  1566. *
  1567. * @param __cloc The C locale.
  1568. * @param __refs Refcount to pass to the base class.
  1569. */
  1570. explicit
  1571. numpunct(__c_locale __cloc, size_t __refs = 0)
  1572. : facet(__refs), _M_data(0)
  1573. { _M_initialize_numpunct(__cloc); }
  1574. /**
  1575. * @brief Return decimal point character.
  1576. *
  1577. * This function returns a char_type to use as a decimal point. It
  1578. * does so by returning returning
  1579. * numpunct<char_type>::do_decimal_point().
  1580. *
  1581. * @return @a char_type representing a decimal point.
  1582. */
  1583. char_type
  1584. decimal_point() const
  1585. { return this->do_decimal_point(); }
  1586. /**
  1587. * @brief Return thousands separator character.
  1588. *
  1589. * This function returns a char_type to use as a thousands
  1590. * separator. It does so by returning returning
  1591. * numpunct<char_type>::do_thousands_sep().
  1592. *
  1593. * @return char_type representing a thousands separator.
  1594. */
  1595. char_type
  1596. thousands_sep() const
  1597. { return this->do_thousands_sep(); }
  1598. /**
  1599. * @brief Return grouping specification.
  1600. *
  1601. * This function returns a string representing groupings for the
  1602. * integer part of a number. Groupings indicate where thousands
  1603. * separators should be inserted in the integer part of a number.
  1604. *
  1605. * Each char in the return string is interpret as an integer
  1606. * rather than a character. These numbers represent the number
  1607. * of digits in a group. The first char in the string
  1608. * represents the number of digits in the least significant
  1609. * group. If a char is negative, it indicates an unlimited
  1610. * number of digits for the group. If more chars from the
  1611. * string are required to group a number, the last char is used
  1612. * repeatedly.
  1613. *
  1614. * For example, if the grouping() returns "\003\002" and is
  1615. * applied to the number 123456789, this corresponds to
  1616. * 12,34,56,789. Note that if the string was "32", this would
  1617. * put more than 50 digits into the least significant group if
  1618. * the character set is ASCII.
  1619. *
  1620. * The string is returned by calling
  1621. * numpunct<char_type>::do_grouping().
  1622. *
  1623. * @return string representing grouping specification.
  1624. */
  1625. string
  1626. grouping() const
  1627. { return this->do_grouping(); }
  1628. /**
  1629. * @brief Return string representation of bool true.
  1630. *
  1631. * This function returns a string_type containing the text
  1632. * representation for true bool variables. It does so by calling
  1633. * numpunct<char_type>::do_truename().
  1634. *
  1635. * @return string_type representing printed form of true.
  1636. */
  1637. string_type
  1638. truename() const
  1639. { return this->do_truename(); }
  1640. /**
  1641. * @brief Return string representation of bool false.
  1642. *
  1643. * This function returns a string_type containing the text
  1644. * representation for false bool variables. It does so by calling
  1645. * numpunct<char_type>::do_falsename().
  1646. *
  1647. * @return string_type representing printed form of false.
  1648. */
  1649. string_type
  1650. falsename() const
  1651. { return this->do_falsename(); }
  1652. protected:
  1653. /// Destructor.
  1654. virtual
  1655. ~numpunct();
  1656. /**
  1657. * @brief Return decimal point character.
  1658. *
  1659. * Returns a char_type to use as a decimal point. This function is a
  1660. * hook for derived classes to change the value returned.
  1661. *
  1662. * @return @a char_type representing a decimal point.
  1663. */
  1664. virtual char_type
  1665. do_decimal_point() const
  1666. { return _M_data->_M_decimal_point; }
  1667. /**
  1668. * @brief Return thousands separator character.
  1669. *
  1670. * Returns a char_type to use as a thousands separator. This function
  1671. * is a hook for derived classes to change the value returned.
  1672. *
  1673. * @return @a char_type representing a thousands separator.
  1674. */
  1675. virtual char_type
  1676. do_thousands_sep() const
  1677. { return _M_data->_M_thousands_sep; }
  1678. /**
  1679. * @brief Return grouping specification.
  1680. *
  1681. * Returns a string representing groupings for the integer part of a
  1682. * number. This function is a hook for derived classes to change the
  1683. * value returned. @see grouping() for details.
  1684. *
  1685. * @return String representing grouping specification.
  1686. */
  1687. virtual string
  1688. do_grouping() const
  1689. { return _M_data->_M_grouping; }
  1690. /**
  1691. * @brief Return string representation of bool true.
  1692. *
  1693. * Returns a string_type containing the text representation for true
  1694. * bool variables. This function is a hook for derived classes to
  1695. * change the value returned.
  1696. *
  1697. * @return string_type representing printed form of true.
  1698. */
  1699. virtual string_type
  1700. do_truename() const
  1701. { return _M_data->_M_truename; }
  1702. /**
  1703. * @brief Return string representation of bool false.
  1704. *
  1705. * Returns a string_type containing the text representation for false
  1706. * bool variables. This function is a hook for derived classes to
  1707. * change the value returned.
  1708. *
  1709. * @return string_type representing printed form of false.
  1710. */
  1711. virtual string_type
  1712. do_falsename() const
  1713. { return _M_data->_M_falsename; }
  1714. // For use at construction time only.
  1715. void
  1716. _M_initialize_numpunct(__c_locale __cloc = 0);
  1717. };
  1718. template<typename _CharT>
  1719. locale::id numpunct<_CharT>::id;
  1720. template<>
  1721. numpunct<char>::~numpunct();
  1722. template<>
  1723. void
  1724. numpunct<char>::_M_initialize_numpunct(__c_locale __cloc);
  1725. #ifdef _GLIBCXX_USE_WCHAR_T
  1726. template<>
  1727. numpunct<wchar_t>::~numpunct();
  1728. template<>
  1729. void
  1730. numpunct<wchar_t>::_M_initialize_numpunct(__c_locale __cloc);
  1731. #endif
  1732. /// class numpunct_byname [22.2.3.2].
  1733. template<typename _CharT>
  1734. class numpunct_byname : public numpunct<_CharT>
  1735. {
  1736. public:
  1737. typedef _CharT char_type;
  1738. typedef basic_string<_CharT> string_type;
  1739. explicit
  1740. numpunct_byname(const char* __s, size_t __refs = 0)
  1741. : numpunct<_CharT>(__refs)
  1742. {
  1743. if (__builtin_strcmp(__s, "C") != 0
  1744. && __builtin_strcmp(__s, "POSIX") != 0)
  1745. {
  1746. __c_locale __tmp;
  1747. this->_S_create_c_locale(__tmp, __s);
  1748. this->_M_initialize_numpunct(__tmp);
  1749. this->_S_destroy_c_locale(__tmp);
  1750. }
  1751. }
  1752. #if __cplusplus >= 201103L
  1753. explicit
  1754. numpunct_byname(const string& __s, size_t __refs = 0)
  1755. : numpunct_byname(__s.c_str(), __refs) { }
  1756. #endif
  1757. protected:
  1758. virtual
  1759. ~numpunct_byname() { }
  1760. };
  1761. _GLIBCXX_END_NAMESPACE_CXX11
  1762. _GLIBCXX_BEGIN_NAMESPACE_LDBL
  1763. /**
  1764. * @brief Primary class template num_get.
  1765. * @ingroup locales
  1766. *
  1767. * This facet encapsulates the code to parse and return a number
  1768. * from a string. It is used by the istream numeric extraction
  1769. * operators.
  1770. *
  1771. * The num_get template uses protected virtual functions to provide the
  1772. * actual results. The public accessors forward the call to the virtual
  1773. * functions. These virtual functions are hooks for developers to
  1774. * implement the behavior they require from the num_get facet.
  1775. */
  1776. template<typename _CharT, typename _InIter>
  1777. class num_get : public locale::facet
  1778. {
  1779. public:
  1780. // Types:
  1781. //@{
  1782. /// Public typedefs
  1783. typedef _CharT char_type;
  1784. typedef _InIter iter_type;
  1785. //@}
  1786. /// Numpunct facet id.
  1787. static locale::id id;
  1788. /**
  1789. * @brief Constructor performs initialization.
  1790. *
  1791. * This is the constructor provided by the standard.
  1792. *
  1793. * @param __refs Passed to the base facet class.
  1794. */
  1795. explicit
  1796. num_get(size_t __refs = 0) : facet(__refs) { }
  1797. /**
  1798. * @brief Numeric parsing.
  1799. *
  1800. * Parses the input stream into the bool @a v. It does so by calling
  1801. * num_get::do_get().
  1802. *
  1803. * If ios_base::boolalpha is set, attempts to read
  1804. * ctype<CharT>::truename() or ctype<CharT>::falsename(). Sets
  1805. * @a v to true or false if successful. Sets err to
  1806. * ios_base::failbit if reading the string fails. Sets err to
  1807. * ios_base::eofbit if the stream is emptied.
  1808. *
  1809. * If ios_base::boolalpha is not set, proceeds as with reading a long,
  1810. * except if the value is 1, sets @a v to true, if the value is 0, sets
  1811. * @a v to false, and otherwise set err to ios_base::failbit.
  1812. *
  1813. * @param __in Start of input stream.
  1814. * @param __end End of input stream.
  1815. * @param __io Source of locale and flags.
  1816. * @param __err Error flags to set.
  1817. * @param __v Value to format and insert.
  1818. * @return Iterator after reading.
  1819. */
  1820. iter_type
  1821. get(iter_type __in, iter_type __end, ios_base& __io,
  1822. ios_base::iostate& __err, bool& __v) const
  1823. { return this->do_get(__in, __end, __io, __err, __v); }
  1824. //@{
  1825. /**
  1826. * @brief Numeric parsing.
  1827. *
  1828. * Parses the input stream into the integral variable @a v. It does so
  1829. * by calling num_get::do_get().
  1830. *
  1831. * Parsing is affected by the flag settings in @a io.
  1832. *
  1833. * The basic parse is affected by the value of io.flags() &
  1834. * ios_base::basefield. If equal to ios_base::oct, parses like the
  1835. * scanf %o specifier. Else if equal to ios_base::hex, parses like %X
  1836. * specifier. Else if basefield equal to 0, parses like the %i
  1837. * specifier. Otherwise, parses like %d for signed and %u for unsigned
  1838. * types. The matching type length modifier is also used.
  1839. *
  1840. * Digit grouping is interpreted according to
  1841. * numpunct::grouping() and numpunct::thousands_sep(). If the
  1842. * pattern of digit groups isn't consistent, sets err to
  1843. * ios_base::failbit.
  1844. *
  1845. * If parsing the string yields a valid value for @a v, @a v is set.
  1846. * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
  1847. * Sets err to ios_base::eofbit if the stream is emptied.
  1848. *
  1849. * @param __in Start of input stream.
  1850. * @param __end End of input stream.
  1851. * @param __io Source of locale and flags.
  1852. * @param __err Error flags to set.
  1853. * @param __v Value to format and insert.
  1854. * @return Iterator after reading.
  1855. */
  1856. iter_type
  1857. get(iter_type __in, iter_type __end, ios_base& __io,
  1858. ios_base::iostate& __err, long& __v) const
  1859. { return this->do_get(__in, __end, __io, __err, __v); }
  1860. iter_type
  1861. get(iter_type __in, iter_type __end, ios_base& __io,
  1862. ios_base::iostate& __err, unsigned short& __v) const
  1863. { return this->do_get(__in, __end, __io, __err, __v); }
  1864. iter_type
  1865. get(iter_type __in, iter_type __end, ios_base& __io,
  1866. ios_base::iostate& __err, unsigned int& __v) const
  1867. { return this->do_get(__in, __end, __io, __err, __v); }
  1868. iter_type
  1869. get(iter_type __in, iter_type __end, ios_base& __io,
  1870. ios_base::iostate& __err, unsigned long& __v) const
  1871. { return this->do_get(__in, __end, __io, __err, __v); }
  1872. #ifdef _GLIBCXX_USE_LONG_LONG
  1873. iter_type
  1874. get(iter_type __in, iter_type __end, ios_base& __io,
  1875. ios_base::iostate& __err, long long& __v) const
  1876. { return this->do_get(__in, __end, __io, __err, __v); }
  1877. iter_type
  1878. get(iter_type __in, iter_type __end, ios_base& __io,
  1879. ios_base::iostate& __err, unsigned long long& __v) const
  1880. { return this->do_get(__in, __end, __io, __err, __v); }
  1881. #endif
  1882. //@}
  1883. //@{
  1884. /**
  1885. * @brief Numeric parsing.
  1886. *
  1887. * Parses the input stream into the integral variable @a v. It does so
  1888. * by calling num_get::do_get().
  1889. *
  1890. * The input characters are parsed like the scanf %g specifier. The
  1891. * matching type length modifier is also used.
  1892. *
  1893. * The decimal point character used is numpunct::decimal_point().
  1894. * Digit grouping is interpreted according to
  1895. * numpunct::grouping() and numpunct::thousands_sep(). If the
  1896. * pattern of digit groups isn't consistent, sets err to
  1897. * ios_base::failbit.
  1898. *
  1899. * If parsing the string yields a valid value for @a v, @a v is set.
  1900. * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
  1901. * Sets err to ios_base::eofbit if the stream is emptied.
  1902. *
  1903. * @param __in Start of input stream.
  1904. * @param __end End of input stream.
  1905. * @param __io Source of locale and flags.
  1906. * @param __err Error flags to set.
  1907. * @param __v Value to format and insert.
  1908. * @return Iterator after reading.
  1909. */
  1910. iter_type
  1911. get(iter_type __in, iter_type __end, ios_base& __io,
  1912. ios_base::iostate& __err, float& __v) const
  1913. { return this->do_get(__in, __end, __io, __err, __v); }
  1914. iter_type
  1915. get(iter_type __in, iter_type __end, ios_base& __io,
  1916. ios_base::iostate& __err, double& __v) const
  1917. { return this->do_get(__in, __end, __io, __err, __v); }
  1918. iter_type
  1919. get(iter_type __in, iter_type __end, ios_base& __io,
  1920. ios_base::iostate& __err, long double& __v) const
  1921. { return this->do_get(__in, __end, __io, __err, __v); }
  1922. //@}
  1923. /**
  1924. * @brief Numeric parsing.
  1925. *
  1926. * Parses the input stream into the pointer variable @a v. It does so
  1927. * by calling num_get::do_get().
  1928. *
  1929. * The input characters are parsed like the scanf %p specifier.
  1930. *
  1931. * Digit grouping is interpreted according to
  1932. * numpunct::grouping() and numpunct::thousands_sep(). If the
  1933. * pattern of digit groups isn't consistent, sets err to
  1934. * ios_base::failbit.
  1935. *
  1936. * Note that the digit grouping effect for pointers is a bit ambiguous
  1937. * in the standard and shouldn't be relied on. See DR 344.
  1938. *
  1939. * If parsing the string yields a valid value for @a v, @a v is set.
  1940. * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
  1941. * Sets err to ios_base::eofbit if the stream is emptied.
  1942. *
  1943. * @param __in Start of input stream.
  1944. * @param __end End of input stream.
  1945. * @param __io Source of locale and flags.
  1946. * @param __err Error flags to set.
  1947. * @param __v Value to format and insert.
  1948. * @return Iterator after reading.
  1949. */
  1950. iter_type
  1951. get(iter_type __in, iter_type __end, ios_base& __io,
  1952. ios_base::iostate& __err, void*& __v) const
  1953. { return this->do_get(__in, __end, __io, __err, __v); }
  1954. protected:
  1955. /// Destructor.
  1956. virtual ~num_get() { }
  1957. _GLIBCXX_DEFAULT_ABI_TAG
  1958. iter_type
  1959. _M_extract_float(iter_type, iter_type, ios_base&, ios_base::iostate&,
  1960. string&) const;
  1961. template<typename _ValueT>
  1962. _GLIBCXX_DEFAULT_ABI_TAG
  1963. iter_type
  1964. _M_extract_int(iter_type, iter_type, ios_base&, ios_base::iostate&,
  1965. _ValueT&) const;
  1966. template<typename _CharT2>
  1967. typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, int>::__type
  1968. _M_find(const _CharT2*, size_t __len, _CharT2 __c) const
  1969. {
  1970. int __ret = -1;
  1971. if (__len <= 10)
  1972. {
  1973. if (__c >= _CharT2('0') && __c < _CharT2(_CharT2('0') + __len))
  1974. __ret = __c - _CharT2('0');
  1975. }
  1976. else
  1977. {
  1978. if (__c >= _CharT2('0') && __c <= _CharT2('9'))
  1979. __ret = __c - _CharT2('0');
  1980. else if (__c >= _CharT2('a') && __c <= _CharT2('f'))
  1981. __ret = 10 + (__c - _CharT2('a'));
  1982. else if (__c >= _CharT2('A') && __c <= _CharT2('F'))
  1983. __ret = 10 + (__c - _CharT2('A'));
  1984. }
  1985. return __ret;
  1986. }
  1987. template<typename _CharT2>
  1988. typename __gnu_cxx::__enable_if<!__is_char<_CharT2>::__value,
  1989. int>::__type
  1990. _M_find(const _CharT2* __zero, size_t __len, _CharT2 __c) const
  1991. {
  1992. int __ret = -1;
  1993. const char_type* __q = char_traits<_CharT2>::find(__zero, __len, __c);
  1994. if (__q)
  1995. {
  1996. __ret = __q - __zero;
  1997. if (__ret > 15)
  1998. __ret -= 6;
  1999. }
  2000. return __ret;
  2001. }
  2002. //@{
  2003. /**
  2004. * @brief Numeric parsing.
  2005. *
  2006. * Parses the input stream into the variable @a v. This function is a
  2007. * hook for derived classes to change the value returned. @see get()
  2008. * for more details.
  2009. *
  2010. * @param __beg Start of input stream.
  2011. * @param __end End of input stream.
  2012. * @param __io Source of locale and flags.
  2013. * @param __err Error flags to set.
  2014. * @param __v Value to format and insert.
  2015. * @return Iterator after reading.
  2016. */
  2017. virtual iter_type
  2018. do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, bool&) const;
  2019. virtual iter_type
  2020. do_get(iter_type __beg, iter_type __end, ios_base& __io,
  2021. ios_base::iostate& __err, long& __v) const
  2022. { return _M_extract_int(__beg, __end, __io, __err, __v); }
  2023. virtual iter_type
  2024. do_get(iter_type __beg, iter_type __end, ios_base& __io,
  2025. ios_base::iostate& __err, unsigned short& __v) const
  2026. { return _M_extract_int(__beg, __end, __io, __err, __v); }
  2027. virtual iter_type
  2028. do_get(iter_type __beg, iter_type __end, ios_base& __io,
  2029. ios_base::iostate& __err, unsigned int& __v) const
  2030. { return _M_extract_int(__beg, __end, __io, __err, __v); }
  2031. virtual iter_type
  2032. do_get(iter_type __beg, iter_type __end, ios_base& __io,
  2033. ios_base::iostate& __err, unsigned long& __v) const
  2034. { return _M_extract_int(__beg, __end, __io, __err, __v); }
  2035. #ifdef _GLIBCXX_USE_LONG_LONG
  2036. virtual iter_type
  2037. do_get(iter_type __beg, iter_type __end, ios_base& __io,
  2038. ios_base::iostate& __err, long long& __v) const
  2039. { return _M_extract_int(__beg, __end, __io, __err, __v); }
  2040. virtual iter_type
  2041. do_get(iter_type __beg, iter_type __end, ios_base& __io,
  2042. ios_base::iostate& __err, unsigned long long& __v) const
  2043. { return _M_extract_int(__beg, __end, __io, __err, __v); }
  2044. #endif
  2045. virtual iter_type
  2046. do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, float&) const;
  2047. virtual iter_type
  2048. do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
  2049. double&) const;
  2050. // XXX GLIBCXX_ABI Deprecated
  2051. #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
  2052. virtual iter_type
  2053. __do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
  2054. double&) const;
  2055. #else
  2056. virtual iter_type
  2057. do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
  2058. long double&) const;
  2059. #endif
  2060. virtual iter_type
  2061. do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, void*&) const;
  2062. // XXX GLIBCXX_ABI Deprecated
  2063. #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
  2064. virtual iter_type
  2065. do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
  2066. long double&) const;
  2067. #endif
  2068. //@}
  2069. };
  2070. template<typename _CharT, typename _InIter>
  2071. locale::id num_get<_CharT, _InIter>::id;
  2072. /**
  2073. * @brief Primary class template num_put.
  2074. * @ingroup locales
  2075. *
  2076. * This facet encapsulates the code to convert a number to a string. It is
  2077. * used by the ostream numeric insertion operators.
  2078. *
  2079. * The num_put template uses protected virtual functions to provide the
  2080. * actual results. The public accessors forward the call to the virtual
  2081. * functions. These virtual functions are hooks for developers to
  2082. * implement the behavior they require from the num_put facet.
  2083. */
  2084. template<typename _CharT, typename _OutIter>
  2085. class num_put : public locale::facet
  2086. {
  2087. public:
  2088. // Types:
  2089. //@{
  2090. /// Public typedefs
  2091. typedef _CharT char_type;
  2092. typedef _OutIter iter_type;
  2093. //@}
  2094. /// Numpunct facet id.
  2095. static locale::id id;
  2096. /**
  2097. * @brief Constructor performs initialization.
  2098. *
  2099. * This is the constructor provided by the standard.
  2100. *
  2101. * @param __refs Passed to the base facet class.
  2102. */
  2103. explicit
  2104. num_put(size_t __refs = 0) : facet(__refs) { }
  2105. /**
  2106. * @brief Numeric formatting.
  2107. *
  2108. * Formats the boolean @a v and inserts it into a stream. It does so
  2109. * by calling num_put::do_put().
  2110. *
  2111. * If ios_base::boolalpha is set, writes ctype<CharT>::truename() or
  2112. * ctype<CharT>::falsename(). Otherwise formats @a v as an int.
  2113. *
  2114. * @param __s Stream to write to.
  2115. * @param __io Source of locale and flags.
  2116. * @param __fill Char_type to use for filling.
  2117. * @param __v Value to format and insert.
  2118. * @return Iterator after writing.
  2119. */
  2120. iter_type
  2121. put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const
  2122. { return this->do_put(__s, __io, __fill, __v); }
  2123. //@{
  2124. /**
  2125. * @brief Numeric formatting.
  2126. *
  2127. * Formats the integral value @a v and inserts it into a
  2128. * stream. It does so by calling num_put::do_put().
  2129. *
  2130. * Formatting is affected by the flag settings in @a io.
  2131. *
  2132. * The basic format is affected by the value of io.flags() &
  2133. * ios_base::basefield. If equal to ios_base::oct, formats like the
  2134. * printf %o specifier. Else if equal to ios_base::hex, formats like
  2135. * %x or %X with ios_base::uppercase unset or set respectively.
  2136. * Otherwise, formats like %d, %ld, %lld for signed and %u, %lu, %llu
  2137. * for unsigned values. Note that if both oct and hex are set, neither
  2138. * will take effect.
  2139. *
  2140. * If ios_base::showpos is set, '+' is output before positive values.
  2141. * If ios_base::showbase is set, '0' precedes octal values (except 0)
  2142. * and '0[xX]' precedes hex values.
  2143. *
  2144. * The decimal point character used is numpunct::decimal_point().
  2145. * Thousands separators are inserted according to
  2146. * numpunct::grouping() and numpunct::thousands_sep().
  2147. *
  2148. * If io.width() is non-zero, enough @a fill characters are inserted to
  2149. * make the result at least that wide. If
  2150. * (io.flags() & ios_base::adjustfield) == ios_base::left, result is
  2151. * padded at the end. If ios_base::internal, then padding occurs
  2152. * immediately after either a '+' or '-' or after '0x' or '0X'.
  2153. * Otherwise, padding occurs at the beginning.
  2154. *
  2155. * @param __s Stream to write to.
  2156. * @param __io Source of locale and flags.
  2157. * @param __fill Char_type to use for filling.
  2158. * @param __v Value to format and insert.
  2159. * @return Iterator after writing.
  2160. */
  2161. iter_type
  2162. put(iter_type __s, ios_base& __io, char_type __fill, long __v) const
  2163. { return this->do_put(__s, __io, __fill, __v); }
  2164. iter_type
  2165. put(iter_type __s, ios_base& __io, char_type __fill,
  2166. unsigned long __v) const
  2167. { return this->do_put(__s, __io, __fill, __v); }
  2168. #ifdef _GLIBCXX_USE_LONG_LONG
  2169. iter_type
  2170. put(iter_type __s, ios_base& __io, char_type __fill, long long __v) const
  2171. { return this->do_put(__s, __io, __fill, __v); }
  2172. iter_type
  2173. put(iter_type __s, ios_base& __io, char_type __fill,
  2174. unsigned long long __v) const
  2175. { return this->do_put(__s, __io, __fill, __v); }
  2176. #endif
  2177. //@}
  2178. //@{
  2179. /**
  2180. * @brief Numeric formatting.
  2181. *
  2182. * Formats the floating point value @a v and inserts it into a stream.
  2183. * It does so by calling num_put::do_put().
  2184. *
  2185. * Formatting is affected by the flag settings in @a io.
  2186. *
  2187. * The basic format is affected by the value of io.flags() &
  2188. * ios_base::floatfield. If equal to ios_base::fixed, formats like the
  2189. * printf %f specifier. Else if equal to ios_base::scientific, formats
  2190. * like %e or %E with ios_base::uppercase unset or set respectively.
  2191. * Otherwise, formats like %g or %G depending on uppercase. Note that
  2192. * if both fixed and scientific are set, the effect will also be like
  2193. * %g or %G.
  2194. *
  2195. * The output precision is given by io.precision(). This precision is
  2196. * capped at numeric_limits::digits10 + 2 (different for double and
  2197. * long double). The default precision is 6.
  2198. *
  2199. * If ios_base::showpos is set, '+' is output before positive values.
  2200. * If ios_base::showpoint is set, a decimal point will always be
  2201. * output.
  2202. *
  2203. * The decimal point character used is numpunct::decimal_point().
  2204. * Thousands separators are inserted according to
  2205. * numpunct::grouping() and numpunct::thousands_sep().
  2206. *
  2207. * If io.width() is non-zero, enough @a fill characters are inserted to
  2208. * make the result at least that wide. If
  2209. * (io.flags() & ios_base::adjustfield) == ios_base::left, result is
  2210. * padded at the end. If ios_base::internal, then padding occurs
  2211. * immediately after either a '+' or '-' or after '0x' or '0X'.
  2212. * Otherwise, padding occurs at the beginning.
  2213. *
  2214. * @param __s Stream to write to.
  2215. * @param __io Source of locale and flags.
  2216. * @param __fill Char_type to use for filling.
  2217. * @param __v Value to format and insert.
  2218. * @return Iterator after writing.
  2219. */
  2220. iter_type
  2221. put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
  2222. { return this->do_put(__s, __io, __fill, __v); }
  2223. iter_type
  2224. put(iter_type __s, ios_base& __io, char_type __fill,
  2225. long double __v) const
  2226. { return this->do_put(__s, __io, __fill, __v); }
  2227. //@}
  2228. /**
  2229. * @brief Numeric formatting.
  2230. *
  2231. * Formats the pointer value @a v and inserts it into a stream. It
  2232. * does so by calling num_put::do_put().
  2233. *
  2234. * This function formats @a v as an unsigned long with ios_base::hex
  2235. * and ios_base::showbase set.
  2236. *
  2237. * @param __s Stream to write to.
  2238. * @param __io Source of locale and flags.
  2239. * @param __fill Char_type to use for filling.
  2240. * @param __v Value to format and insert.
  2241. * @return Iterator after writing.
  2242. */
  2243. iter_type
  2244. put(iter_type __s, ios_base& __io, char_type __fill,
  2245. const void* __v) const
  2246. { return this->do_put(__s, __io, __fill, __v); }
  2247. protected:
  2248. template<typename _ValueT>
  2249. iter_type
  2250. _M_insert_float(iter_type, ios_base& __io, char_type __fill,
  2251. char __mod, _ValueT __v) const;
  2252. void
  2253. _M_group_float(const char* __grouping, size_t __grouping_size,
  2254. char_type __sep, const char_type* __p, char_type* __new,
  2255. char_type* __cs, int& __len) const;
  2256. template<typename _ValueT>
  2257. iter_type
  2258. _M_insert_int(iter_type, ios_base& __io, char_type __fill,
  2259. _ValueT __v) const;
  2260. void
  2261. _M_group_int(const char* __grouping, size_t __grouping_size,
  2262. char_type __sep, ios_base& __io, char_type* __new,
  2263. char_type* __cs, int& __len) const;
  2264. void
  2265. _M_pad(char_type __fill, streamsize __w, ios_base& __io,
  2266. char_type* __new, const char_type* __cs, int& __len) const;
  2267. /// Destructor.
  2268. virtual
  2269. ~num_put() { }
  2270. //@{
  2271. /**
  2272. * @brief Numeric formatting.
  2273. *
  2274. * These functions do the work of formatting numeric values and
  2275. * inserting them into a stream. This function is a hook for derived
  2276. * classes to change the value returned.
  2277. *
  2278. * @param __s Stream to write to.
  2279. * @param __io Source of locale and flags.
  2280. * @param __fill Char_type to use for filling.
  2281. * @param __v Value to format and insert.
  2282. * @return Iterator after writing.
  2283. */
  2284. virtual iter_type
  2285. do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const;
  2286. virtual iter_type
  2287. do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const
  2288. { return _M_insert_int(__s, __io, __fill, __v); }
  2289. virtual iter_type
  2290. do_put(iter_type __s, ios_base& __io, char_type __fill,
  2291. unsigned long __v) const
  2292. { return _M_insert_int(__s, __io, __fill, __v); }
  2293. #ifdef _GLIBCXX_USE_LONG_LONG
  2294. virtual iter_type
  2295. do_put(iter_type __s, ios_base& __io, char_type __fill,
  2296. long long __v) const
  2297. { return _M_insert_int(__s, __io, __fill, __v); }
  2298. virtual iter_type
  2299. do_put(iter_type __s, ios_base& __io, char_type __fill,
  2300. unsigned long long __v) const
  2301. { return _M_insert_int(__s, __io, __fill, __v); }
  2302. #endif
  2303. virtual iter_type
  2304. do_put(iter_type, ios_base&, char_type, double) const;
  2305. // XXX GLIBCXX_ABI Deprecated
  2306. #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
  2307. virtual iter_type
  2308. __do_put(iter_type, ios_base&, char_type, double) const;
  2309. #else
  2310. virtual iter_type
  2311. do_put(iter_type, ios_base&, char_type, long double) const;
  2312. #endif
  2313. virtual iter_type
  2314. do_put(iter_type, ios_base&, char_type, const void*) const;
  2315. // XXX GLIBCXX_ABI Deprecated
  2316. #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
  2317. virtual iter_type
  2318. do_put(iter_type, ios_base&, char_type, long double) const;
  2319. #endif
  2320. //@}
  2321. };
  2322. template <typename _CharT, typename _OutIter>
  2323. locale::id num_put<_CharT, _OutIter>::id;
  2324. _GLIBCXX_END_NAMESPACE_LDBL
  2325. // Subclause convenience interfaces, inlines.
  2326. // NB: These are inline because, when used in a loop, some compilers
  2327. // can hoist the body out of the loop; then it's just as fast as the
  2328. // C is*() function.
  2329. /// Convenience interface to ctype.is(ctype_base::space, __c).
  2330. template<typename _CharT>
  2331. inline bool
  2332. isspace(_CharT __c, const locale& __loc)
  2333. { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c); }
  2334. /// Convenience interface to ctype.is(ctype_base::print, __c).
  2335. template<typename _CharT>
  2336. inline bool
  2337. isprint(_CharT __c, const locale& __loc)
  2338. { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c); }
  2339. /// Convenience interface to ctype.is(ctype_base::cntrl, __c).
  2340. template<typename _CharT>
  2341. inline bool
  2342. iscntrl(_CharT __c, const locale& __loc)
  2343. { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c); }
  2344. /// Convenience interface to ctype.is(ctype_base::upper, __c).
  2345. template<typename _CharT>
  2346. inline bool
  2347. isupper(_CharT __c, const locale& __loc)
  2348. { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c); }
  2349. /// Convenience interface to ctype.is(ctype_base::lower, __c).
  2350. template<typename _CharT>
  2351. inline bool
  2352. islower(_CharT __c, const locale& __loc)
  2353. { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c); }
  2354. /// Convenience interface to ctype.is(ctype_base::alpha, __c).
  2355. template<typename _CharT>
  2356. inline bool
  2357. isalpha(_CharT __c, const locale& __loc)
  2358. { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c); }
  2359. /// Convenience interface to ctype.is(ctype_base::digit, __c).
  2360. template<typename _CharT>
  2361. inline bool
  2362. isdigit(_CharT __c, const locale& __loc)
  2363. { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c); }
  2364. /// Convenience interface to ctype.is(ctype_base::punct, __c).
  2365. template<typename _CharT>
  2366. inline bool
  2367. ispunct(_CharT __c, const locale& __loc)
  2368. { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c); }
  2369. /// Convenience interface to ctype.is(ctype_base::xdigit, __c).
  2370. template<typename _CharT>
  2371. inline bool
  2372. isxdigit(_CharT __c, const locale& __loc)
  2373. { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c); }
  2374. /// Convenience interface to ctype.is(ctype_base::alnum, __c).
  2375. template<typename _CharT>
  2376. inline bool
  2377. isalnum(_CharT __c, const locale& __loc)
  2378. { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c); }
  2379. /// Convenience interface to ctype.is(ctype_base::graph, __c).
  2380. template<typename _CharT>
  2381. inline bool
  2382. isgraph(_CharT __c, const locale& __loc)
  2383. { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c); }
  2384. #if __cplusplus >= 201103L
  2385. /// Convenience interface to ctype.is(ctype_base::blank, __c).
  2386. template<typename _CharT>
  2387. inline bool
  2388. isblank(_CharT __c, const locale& __loc)
  2389. { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::blank, __c); }
  2390. #endif
  2391. /// Convenience interface to ctype.toupper(__c).
  2392. template<typename _CharT>
  2393. inline _CharT
  2394. toupper(_CharT __c, const locale& __loc)
  2395. { return use_facet<ctype<_CharT> >(__loc).toupper(__c); }
  2396. /// Convenience interface to ctype.tolower(__c).
  2397. template<typename _CharT>
  2398. inline _CharT
  2399. tolower(_CharT __c, const locale& __loc)
  2400. { return use_facet<ctype<_CharT> >(__loc).tolower(__c); }
  2401. _GLIBCXX_END_NAMESPACE_VERSION
  2402. } // namespace std
  2403. # include <bits/locale_facets.tcc>
  2404. #endif