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