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