]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/locale_facets.h
PR libstdc++/28080 (partial)
[thirdparty/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
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 <ctime> // For struct tm
47 #include <cwctype> // For wctype_t
48 #include <cctype>
49 #include <bits/ctype_base.h>
50 #include <iosfwd>
51 #include <bits/ios_base.h> // For ios_base, ios_base::iostate
52 #include <streambuf>
53 #include <bits/cpp_type_traits.h>
54
55 _GLIBCXX_BEGIN_NAMESPACE(std)
56
57 // NB: Don't instantiate required wchar_t facets if no wchar_t support.
58 #ifdef _GLIBCXX_USE_WCHAR_T
59 # define _GLIBCXX_NUM_FACETS 28
60 #else
61 # define _GLIBCXX_NUM_FACETS 14
62 #endif
63
64 // Convert string to numeric value of type _Tv and store results.
65 // NB: This is specialized for all required types, there is no
66 // generic definition.
67 template<typename _Tv>
68 void
69 __convert_to_v(const char* __in, _Tv& __out, ios_base::iostate& __err,
70 const __c_locale& __cloc);
71
72 // Explicit specializations for required types.
73 template<>
74 void
75 __convert_to_v(const char*, float&, ios_base::iostate&,
76 const __c_locale&);
77
78 template<>
79 void
80 __convert_to_v(const char*, double&, ios_base::iostate&,
81 const __c_locale&);
82
83 template<>
84 void
85 __convert_to_v(const char*, long double&, ios_base::iostate&,
86 const __c_locale&);
87
88 // NB: __pad is a struct, rather than a function, so it can be
89 // partially-specialized.
90 template<typename _CharT, typename _Traits>
91 struct __pad
92 {
93 static void
94 _S_pad(ios_base& __io, _CharT __fill, _CharT* __news,
95 const _CharT* __olds, const streamsize __newlen,
96 const streamsize __oldlen, const bool __num);
97 };
98
99 // Used by both numeric and monetary facets.
100 // Inserts "group separator" characters into an array of characters.
101 // It's recursive, one iteration per group. It moves the characters
102 // in the buffer this way: "xxxx12345" -> "12,345xxx". Call this
103 // only with __glen != 0.
104 template<typename _CharT>
105 _CharT*
106 __add_grouping(_CharT* __s, _CharT __sep,
107 const char* __gbeg, size_t __gsize,
108 const _CharT* __first, const _CharT* __last);
109
110 // This template permits specializing facet output code for
111 // ostreambuf_iterator. For ostreambuf_iterator, sputn is
112 // significantly more efficient than incrementing iterators.
113 template<typename _CharT>
114 inline
115 ostreambuf_iterator<_CharT>
116 __write(ostreambuf_iterator<_CharT> __s, const _CharT* __ws, int __len)
117 {
118 __s._M_put(__ws, __len);
119 return __s;
120 }
121
122 // This is the unspecialized form of the template.
123 template<typename _CharT, typename _OutIter>
124 inline
125 _OutIter
126 __write(_OutIter __s, const _CharT* __ws, int __len)
127 {
128 for (int __j = 0; __j < __len; __j++, ++__s)
129 *__s = __ws[__j];
130 return __s;
131 }
132
133
134 // 22.2.1.1 Template class ctype
135 // Include host and configuration specific ctype enums for ctype_base.
136
137 // Common base for ctype<_CharT>.
138 /**
139 * @brief Common base for ctype facet
140 *
141 * This template class provides implementations of the public functions
142 * that forward to the protected virtual functions.
143 *
144 * This template also provides abtract stubs for the protected virtual
145 * functions.
146 */
147 template<typename _CharT>
148 class __ctype_abstract_base : public locale::facet, public ctype_base
149 {
150 public:
151 // Types:
152 /// Typedef for the template parameter
153 typedef _CharT char_type;
154
155 /**
156 * @brief Test char_type classification.
157 *
158 * This function finds a mask M for @a c and compares it to mask @a m.
159 * It does so by returning the value of ctype<char_type>::do_is().
160 *
161 * @param c The char_type to compare the mask of.
162 * @param m The mask to compare against.
163 * @return (M & m) != 0.
164 */
165 bool
166 is(mask __m, char_type __c) const
167 { return this->do_is(__m, __c); }
168
169 /**
170 * @brief Return a mask array.
171 *
172 * This function finds the mask for each char_type in the range [lo,hi)
173 * and successively writes it to vec. vec must have as many elements
174 * as the char array. It does so by returning the value of
175 * ctype<char_type>::do_is().
176 *
177 * @param lo Pointer to start of range.
178 * @param hi Pointer to end of range.
179 * @param vec Pointer to an array of mask storage.
180 * @return @a hi.
181 */
182 const char_type*
183 is(const char_type *__lo, const char_type *__hi, mask *__vec) const
184 { return this->do_is(__lo, __hi, __vec); }
185
186 /**
187 * @brief Find char_type matching a mask
188 *
189 * This function searches for and returns the first char_type c in
190 * [lo,hi) for which is(m,c) is true. It does so by returning
191 * ctype<char_type>::do_scan_is().
192 *
193 * @param m The mask to compare against.
194 * @param lo Pointer to start of range.
195 * @param hi Pointer to end of range.
196 * @return Pointer to matching char_type if found, else @a hi.
197 */
198 const char_type*
199 scan_is(mask __m, const char_type* __lo, const char_type* __hi) const
200 { return this->do_scan_is(__m, __lo, __hi); }
201
202 /**
203 * @brief Find char_type not matching a mask
204 *
205 * This function searches for and returns the first char_type c in
206 * [lo,hi) for which is(m,c) is false. It does so by returning
207 * ctype<char_type>::do_scan_not().
208 *
209 * @param m The mask to compare against.
210 * @param lo Pointer to first char in range.
211 * @param hi Pointer to end of range.
212 * @return Pointer to non-matching char if found, else @a hi.
213 */
214 const char_type*
215 scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
216 { return this->do_scan_not(__m, __lo, __hi); }
217
218 /**
219 * @brief Convert to uppercase.
220 *
221 * This function converts the argument to uppercase if possible.
222 * If not possible (for example, '2'), returns the argument. It does
223 * so by returning ctype<char_type>::do_toupper().
224 *
225 * @param c The char_type to convert.
226 * @return The uppercase char_type if convertible, else @a c.
227 */
228 char_type
229 toupper(char_type __c) const
230 { return this->do_toupper(__c); }
231
232 /**
233 * @brief Convert array to uppercase.
234 *
235 * This function converts each char_type in the range [lo,hi) to
236 * uppercase if possible. Other elements remain untouched. It does so
237 * by returning ctype<char_type>:: do_toupper(lo, hi).
238 *
239 * @param lo Pointer to start of range.
240 * @param hi Pointer to end of range.
241 * @return @a hi.
242 */
243 const char_type*
244 toupper(char_type *__lo, const char_type* __hi) const
245 { return this->do_toupper(__lo, __hi); }
246
247 /**
248 * @brief Convert to lowercase.
249 *
250 * This function converts the argument to lowercase if possible. If
251 * not possible (for example, '2'), returns the argument. It does so
252 * by returning ctype<char_type>::do_tolower(c).
253 *
254 * @param c The char_type to convert.
255 * @return The lowercase char_type if convertible, else @a c.
256 */
257 char_type
258 tolower(char_type __c) const
259 { return this->do_tolower(__c); }
260
261 /**
262 * @brief Convert array to lowercase.
263 *
264 * This function converts each char_type in the range [lo,hi) to
265 * lowercase if possible. Other elements remain untouched. It does so
266 * by returning ctype<char_type>:: do_tolower(lo, hi).
267 *
268 * @param lo Pointer to start of range.
269 * @param hi Pointer to end of range.
270 * @return @a hi.
271 */
272 const char_type*
273 tolower(char_type* __lo, const char_type* __hi) const
274 { return this->do_tolower(__lo, __hi); }
275
276 /**
277 * @brief Widen char to char_type
278 *
279 * This function converts the char argument to char_type using the
280 * simplest reasonable transformation. It does so by returning
281 * ctype<char_type>::do_widen(c).
282 *
283 * Note: this is not what you want for codepage conversions. See
284 * codecvt for that.
285 *
286 * @param c The char to convert.
287 * @return The converted char_type.
288 */
289 char_type
290 widen(char __c) const
291 { return this->do_widen(__c); }
292
293 /**
294 * @brief Widen array to char_type
295 *
296 * This function converts each char in the input to char_type using the
297 * simplest reasonable transformation. It does so by returning
298 * ctype<char_type>::do_widen(c).
299 *
300 * Note: this is not what you want for codepage conversions. See
301 * codecvt for that.
302 *
303 * @param lo Pointer to start of range.
304 * @param hi Pointer to end of range.
305 * @param to Pointer to the destination array.
306 * @return @a hi.
307 */
308 const char*
309 widen(const char* __lo, const char* __hi, char_type* __to) const
310 { return this->do_widen(__lo, __hi, __to); }
311
312 /**
313 * @brief Narrow char_type to char
314 *
315 * This function converts the char_type to char using the simplest
316 * reasonable transformation. If the conversion fails, dfault is
317 * returned instead. It does so by returning
318 * ctype<char_type>::do_narrow(c).
319 *
320 * Note: this is not what you want for codepage conversions. See
321 * codecvt for that.
322 *
323 * @param c The char_type to convert.
324 * @param dfault Char to return if conversion fails.
325 * @return The converted char.
326 */
327 char
328 narrow(char_type __c, char __dfault) const
329 { return this->do_narrow(__c, __dfault); }
330
331 /**
332 * @brief Narrow array to char array
333 *
334 * This function converts each char_type in the input to char using the
335 * simplest reasonable transformation and writes the results to the
336 * destination array. For any char_type in the input that cannot be
337 * converted, @a dfault is used instead. It does so by returning
338 * ctype<char_type>::do_narrow(lo, hi, dfault, to).
339 *
340 * Note: this is not what you want for codepage conversions. See
341 * codecvt for that.
342 *
343 * @param lo Pointer to start of range.
344 * @param hi Pointer to end of range.
345 * @param dfault Char to use if conversion fails.
346 * @param to Pointer to the destination array.
347 * @return @a hi.
348 */
349 const char_type*
350 narrow(const char_type* __lo, const char_type* __hi,
351 char __dfault, char *__to) const
352 { return this->do_narrow(__lo, __hi, __dfault, __to); }
353
354 protected:
355 explicit
356 __ctype_abstract_base(size_t __refs = 0): facet(__refs) { }
357
358 virtual
359 ~__ctype_abstract_base() { }
360
361 /**
362 * @brief Test char_type classification.
363 *
364 * This function finds a mask M for @a c and compares it to mask @a m.
365 *
366 * do_is() is a hook for a derived facet to change the behavior of
367 * classifying. do_is() must always return the same result for the
368 * same input.
369 *
370 * @param c The char_type to find the mask of.
371 * @param m The mask to compare against.
372 * @return (M & m) != 0.
373 */
374 virtual bool
375 do_is(mask __m, char_type __c) const = 0;
376
377 /**
378 * @brief Return a mask array.
379 *
380 * This function finds the mask for each char_type in the range [lo,hi)
381 * and successively writes it to vec. vec must have as many elements
382 * as the input.
383 *
384 * do_is() is a hook for a derived facet to change the behavior of
385 * classifying. do_is() must always return the same result for the
386 * same input.
387 *
388 * @param lo Pointer to start of range.
389 * @param hi Pointer to end of range.
390 * @param vec Pointer to an array of mask storage.
391 * @return @a hi.
392 */
393 virtual const char_type*
394 do_is(const char_type* __lo, const char_type* __hi,
395 mask* __vec) const = 0;
396
397 /**
398 * @brief Find char_type matching mask
399 *
400 * This function searches for and returns the first char_type c in
401 * [lo,hi) for which is(m,c) is true.
402 *
403 * do_scan_is() is a hook for a derived facet to change the behavior of
404 * match searching. do_is() must always return the same result for the
405 * same input.
406 *
407 * @param m The mask to compare against.
408 * @param lo Pointer to start of range.
409 * @param hi Pointer to end of range.
410 * @return Pointer to a matching char_type if found, else @a hi.
411 */
412 virtual const char_type*
413 do_scan_is(mask __m, const char_type* __lo,
414 const char_type* __hi) const = 0;
415
416 /**
417 * @brief Find char_type not matching mask
418 *
419 * This function searches for and returns a pointer to the first
420 * char_type c of [lo,hi) for which is(m,c) is false.
421 *
422 * do_scan_is() is a hook for a derived facet to change the behavior of
423 * match searching. do_is() must always return the same result for the
424 * same input.
425 *
426 * @param m The mask to compare against.
427 * @param lo Pointer to start of range.
428 * @param hi Pointer to end of range.
429 * @return Pointer to a non-matching char_type if found, else @a hi.
430 */
431 virtual const char_type*
432 do_scan_not(mask __m, const char_type* __lo,
433 const char_type* __hi) const = 0;
434
435 /**
436 * @brief Convert to uppercase.
437 *
438 * This virtual function converts the char_type argument to uppercase
439 * if possible. If not possible (for example, '2'), returns the
440 * argument.
441 *
442 * do_toupper() is a hook for a derived facet to change the behavior of
443 * uppercasing. do_toupper() must always return the same result for
444 * the same input.
445 *
446 * @param c The char_type to convert.
447 * @return The uppercase char_type if convertible, else @a c.
448 */
449 virtual char_type
450 do_toupper(char_type) const = 0;
451
452 /**
453 * @brief Convert array to uppercase.
454 *
455 * This virtual function converts each char_type in the range [lo,hi)
456 * to uppercase if possible. Other elements remain untouched.
457 *
458 * do_toupper() is a hook for a derived facet to change the behavior of
459 * uppercasing. do_toupper() must always return the same result for
460 * the same input.
461 *
462 * @param lo Pointer to start of range.
463 * @param hi Pointer to end of range.
464 * @return @a hi.
465 */
466 virtual const char_type*
467 do_toupper(char_type* __lo, const char_type* __hi) const = 0;
468
469 /**
470 * @brief Convert to lowercase.
471 *
472 * This virtual function converts the argument to lowercase if
473 * possible. If not possible (for example, '2'), returns the argument.
474 *
475 * do_tolower() is a hook for a derived facet to change the behavior of
476 * lowercasing. do_tolower() must always return the same result for
477 * the same input.
478 *
479 * @param c The char_type to convert.
480 * @return The lowercase char_type if convertible, else @a c.
481 */
482 virtual char_type
483 do_tolower(char_type) const = 0;
484
485 /**
486 * @brief Convert array to lowercase.
487 *
488 * This virtual function converts each char_type in the range [lo,hi)
489 * to lowercase if possible. Other elements remain untouched.
490 *
491 * do_tolower() is a hook for a derived facet to change the behavior of
492 * lowercasing. do_tolower() must always return the same result for
493 * the same input.
494 *
495 * @param lo Pointer to start of range.
496 * @param hi Pointer to end of range.
497 * @return @a hi.
498 */
499 virtual const char_type*
500 do_tolower(char_type* __lo, const char_type* __hi) const = 0;
501
502 /**
503 * @brief Widen char
504 *
505 * This virtual function converts the char to char_type using the
506 * simplest reasonable transformation.
507 *
508 * do_widen() is a hook for a derived facet to change the behavior of
509 * widening. do_widen() must always return the same result for the
510 * same input.
511 *
512 * Note: this is not what you want for codepage conversions. See
513 * codecvt for that.
514 *
515 * @param c The char to convert.
516 * @return The converted char_type
517 */
518 virtual char_type
519 do_widen(char) const = 0;
520
521 /**
522 * @brief Widen char array
523 *
524 * This function converts each char in the input to char_type using the
525 * simplest reasonable transformation.
526 *
527 * do_widen() is a hook for a derived facet to change the behavior of
528 * widening. do_widen() must always return the same result for the
529 * same input.
530 *
531 * Note: this is not what you want for codepage conversions. See
532 * codecvt for that.
533 *
534 * @param lo Pointer to start range.
535 * @param hi Pointer to end of range.
536 * @param to Pointer to the destination array.
537 * @return @a hi.
538 */
539 virtual const char*
540 do_widen(const char* __lo, const char* __hi,
541 char_type* __dest) const = 0;
542
543 /**
544 * @brief Narrow char_type to char
545 *
546 * This virtual function converts the argument to char using the
547 * simplest reasonable transformation. If the conversion fails, dfault
548 * is returned instead.
549 *
550 * do_narrow() is a hook for a derived facet to change the behavior of
551 * narrowing. do_narrow() must always return the same result for the
552 * same input.
553 *
554 * Note: this is not what you want for codepage conversions. See
555 * codecvt for that.
556 *
557 * @param c The char_type to convert.
558 * @param dfault Char to return if conversion fails.
559 * @return The converted char.
560 */
561 virtual char
562 do_narrow(char_type, char __dfault) const = 0;
563
564 /**
565 * @brief Narrow char_type array to char
566 *
567 * This virtual function converts each char_type in the range [lo,hi) to
568 * char using the simplest reasonable transformation and writes the
569 * results to the destination array. For any element in the input that
570 * cannot be converted, @a dfault is used instead.
571 *
572 * do_narrow() is a hook for a derived facet to change the behavior of
573 * narrowing. do_narrow() must always return the same result for the
574 * same input.
575 *
576 * Note: this is not what you want for codepage conversions. See
577 * codecvt for that.
578 *
579 * @param lo Pointer to start of range.
580 * @param hi Pointer to end of range.
581 * @param dfault Char to use if conversion fails.
582 * @param to Pointer to the destination array.
583 * @return @a hi.
584 */
585 virtual const char_type*
586 do_narrow(const char_type* __lo, const char_type* __hi,
587 char __dfault, char* __dest) const = 0;
588 };
589
590 // NB: Generic, mostly useless implementation.
591 /**
592 * @brief Template ctype facet
593 *
594 * This template class defines classification and conversion functions for
595 * character sets. It wraps <cctype> functionality. Ctype gets used by
596 * streams for many I/O operations.
597 *
598 * This template provides the protected virtual functions the developer
599 * will have to replace in a derived class or specialization to make a
600 * working facet. The public functions that access them are defined in
601 * __ctype_abstract_base, to allow for implementation flexibility. See
602 * ctype<wchar_t> for an example. The functions are documented in
603 * __ctype_abstract_base.
604 *
605 * Note: implementations are provided for all the protected virtual
606 * functions, but will likely not be useful.
607 */
608 template<typename _CharT>
609 class ctype : public __ctype_abstract_base<_CharT>
610 {
611 public:
612 // Types:
613 typedef _CharT char_type;
614 typedef typename __ctype_abstract_base<_CharT>::mask mask;
615
616 /// The facet id for ctype<char_type>
617 static locale::id id;
618
619 explicit
620 ctype(size_t __refs = 0) : __ctype_abstract_base<_CharT>(__refs) { }
621
622 protected:
623 virtual
624 ~ctype();
625
626 virtual bool
627 do_is(mask __m, char_type __c) const;
628
629 virtual const char_type*
630 do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const;
631
632 virtual const char_type*
633 do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const;
634
635 virtual const char_type*
636 do_scan_not(mask __m, const char_type* __lo,
637 const char_type* __hi) const;
638
639 virtual char_type
640 do_toupper(char_type __c) const;
641
642 virtual const char_type*
643 do_toupper(char_type* __lo, const char_type* __hi) const;
644
645 virtual char_type
646 do_tolower(char_type __c) const;
647
648 virtual const char_type*
649 do_tolower(char_type* __lo, const char_type* __hi) const;
650
651 virtual char_type
652 do_widen(char __c) const;
653
654 virtual const char*
655 do_widen(const char* __lo, const char* __hi, char_type* __dest) const;
656
657 virtual char
658 do_narrow(char_type, char __dfault) const;
659
660 virtual const char_type*
661 do_narrow(const char_type* __lo, const char_type* __hi,
662 char __dfault, char* __dest) const;
663 };
664
665 template<typename _CharT>
666 locale::id ctype<_CharT>::id;
667
668 // 22.2.1.3 ctype<char> specialization.
669 /**
670 * @brief The ctype<char> specialization.
671 *
672 * This class defines classification and conversion functions for
673 * the char type. It gets used by char streams for many I/O
674 * operations. The char specialization provides a number of
675 * optimizations as well.
676 */
677 template<>
678 class ctype<char> : public locale::facet, public ctype_base
679 {
680 public:
681 // Types:
682 /// Typedef for the template parameter char.
683 typedef char char_type;
684
685 protected:
686 // Data Members:
687 __c_locale _M_c_locale_ctype;
688 bool _M_del;
689 __to_type _M_toupper;
690 __to_type _M_tolower;
691 const mask* _M_table;
692 mutable char _M_widen_ok;
693 mutable char _M_widen[1 + static_cast<unsigned char>(-1)];
694 mutable char _M_narrow[1 + static_cast<unsigned char>(-1)];
695 mutable char _M_narrow_ok; // 0 uninitialized, 1 init,
696 // 2 memcpy can't be used
697
698 public:
699 /// The facet id for ctype<char>
700 static locale::id id;
701 /// The size of the mask table. It is SCHAR_MAX + 1.
702 static const size_t table_size = 1 + static_cast<unsigned char>(-1);
703
704 /**
705 * @brief Constructor performs initialization.
706 *
707 * This is the constructor provided by the standard.
708 *
709 * @param table If non-zero, table is used as the per-char mask.
710 * Else classic_table() is used.
711 * @param del If true, passes ownership of table to this facet.
712 * @param refs Passed to the base facet class.
713 */
714 explicit
715 ctype(const mask* __table = 0, bool __del = false, size_t __refs = 0);
716
717 /**
718 * @brief Constructor performs static initialization.
719 *
720 * This constructor is used to construct the initial C locale facet.
721 *
722 * @param cloc Handle to C locale data.
723 * @param table If non-zero, table is used as the per-char mask.
724 * @param del If true, passes ownership of table to this facet.
725 * @param refs Passed to the base facet class.
726 */
727 explicit
728 ctype(__c_locale __cloc, const mask* __table = 0, bool __del = false,
729 size_t __refs = 0);
730
731 /**
732 * @brief Test char classification.
733 *
734 * This function compares the mask table[c] to @a m.
735 *
736 * @param c The char to compare the mask of.
737 * @param m The mask to compare against.
738 * @return True if m & table[c] is true, false otherwise.
739 */
740 inline bool
741 is(mask __m, char __c) const;
742
743 /**
744 * @brief Return a mask array.
745 *
746 * This function finds the mask for each char in the range [lo, hi) and
747 * successively writes it to vec. vec must have as many elements as
748 * the char array.
749 *
750 * @param lo Pointer to start of range.
751 * @param hi Pointer to end of range.
752 * @param vec Pointer to an array of mask storage.
753 * @return @a hi.
754 */
755 inline const char*
756 is(const char* __lo, const char* __hi, mask* __vec) const;
757
758 /**
759 * @brief Find char matching a mask
760 *
761 * This function searches for and returns the first char in [lo,hi) for
762 * which is(m,char) is true.
763 *
764 * @param m The mask to compare against.
765 * @param lo Pointer to start of range.
766 * @param hi Pointer to end of range.
767 * @return Pointer to a matching char if found, else @a hi.
768 */
769 inline const char*
770 scan_is(mask __m, const char* __lo, const char* __hi) const;
771
772 /**
773 * @brief Find char not matching a mask
774 *
775 * This function searches for and returns a pointer to the first char
776 * in [lo,hi) for which is(m,char) is false.
777 *
778 * @param m The mask to compare against.
779 * @param lo Pointer to start of range.
780 * @param hi Pointer to end of range.
781 * @return Pointer to a non-matching char if found, else @a hi.
782 */
783 inline const char*
784 scan_not(mask __m, const char* __lo, const char* __hi) const;
785
786 /**
787 * @brief Convert to uppercase.
788 *
789 * This function converts the char argument to uppercase if possible.
790 * If not possible (for example, '2'), returns the argument.
791 *
792 * toupper() acts as if it returns ctype<char>::do_toupper(c).
793 * do_toupper() must always return the same result for the same input.
794 *
795 * @param c The char to convert.
796 * @return The uppercase char if convertible, else @a c.
797 */
798 char_type
799 toupper(char_type __c) const
800 { return this->do_toupper(__c); }
801
802 /**
803 * @brief Convert array to uppercase.
804 *
805 * This function converts each char in the range [lo,hi) to uppercase
806 * if possible. Other chars remain untouched.
807 *
808 * toupper() acts as if it returns ctype<char>:: do_toupper(lo, hi).
809 * do_toupper() must always return the same result for the same input.
810 *
811 * @param lo Pointer to first char in range.
812 * @param hi Pointer to end of range.
813 * @return @a hi.
814 */
815 const char_type*
816 toupper(char_type *__lo, const char_type* __hi) const
817 { return this->do_toupper(__lo, __hi); }
818
819 /**
820 * @brief Convert to lowercase.
821 *
822 * This function converts the char argument to lowercase if possible.
823 * If not possible (for example, '2'), returns the argument.
824 *
825 * tolower() acts as if it returns ctype<char>::do_tolower(c).
826 * do_tolower() must always return the same result for the same input.
827 *
828 * @param c The char to convert.
829 * @return The lowercase char if convertible, else @a c.
830 */
831 char_type
832 tolower(char_type __c) const
833 { return this->do_tolower(__c); }
834
835 /**
836 * @brief Convert array to lowercase.
837 *
838 * This function converts each char in the range [lo,hi) to lowercase
839 * if possible. Other chars remain untouched.
840 *
841 * tolower() acts as if it returns ctype<char>:: do_tolower(lo, hi).
842 * do_tolower() must always return the same result for the same input.
843 *
844 * @param lo Pointer to first char in range.
845 * @param hi Pointer to end of range.
846 * @return @a hi.
847 */
848 const char_type*
849 tolower(char_type* __lo, const char_type* __hi) const
850 { return this->do_tolower(__lo, __hi); }
851
852 /**
853 * @brief Widen char
854 *
855 * This function converts the char to char_type using the simplest
856 * reasonable transformation. For an underived ctype<char> facet, the
857 * argument will be returned unchanged.
858 *
859 * This function works as if it returns ctype<char>::do_widen(c).
860 * do_widen() must always return the same result for the same input.
861 *
862 * Note: this is not what you want for codepage conversions. See
863 * codecvt for that.
864 *
865 * @param c The char to convert.
866 * @return The converted character.
867 */
868 char_type
869 widen(char __c) const
870 {
871 if (_M_widen_ok)
872 return _M_widen[static_cast<unsigned char>(__c)];
873 this->_M_widen_init();
874 return this->do_widen(__c);
875 }
876
877 /**
878 * @brief Widen char array
879 *
880 * This function converts each char in the input to char using the
881 * simplest reasonable transformation. For an underived ctype<char>
882 * facet, the argument will be copied unchanged.
883 *
884 * This function works as if it returns ctype<char>::do_widen(c).
885 * do_widen() must always return the same result for the same input.
886 *
887 * Note: this is not what you want for codepage conversions. See
888 * codecvt for that.
889 *
890 * @param lo Pointer to first char in range.
891 * @param hi Pointer to end of range.
892 * @param to Pointer to the destination array.
893 * @return @a hi.
894 */
895 const char*
896 widen(const char* __lo, const char* __hi, char_type* __to) const
897 {
898 if (_M_widen_ok == 1)
899 {
900 memcpy(__to, __lo, __hi - __lo);
901 return __hi;
902 }
903 if (!_M_widen_ok)
904 _M_widen_init();
905 return this->do_widen(__lo, __hi, __to);
906 }
907
908 /**
909 * @brief Narrow char
910 *
911 * This function converts the char to char using the simplest
912 * reasonable transformation. If the conversion fails, dfault is
913 * returned instead. For an underived ctype<char> facet, @a c
914 * will be returned unchanged.
915 *
916 * This function works as if it returns ctype<char>::do_narrow(c).
917 * do_narrow() must always return the same result for the same input.
918 *
919 * Note: this is not what you want for codepage conversions. See
920 * codecvt for that.
921 *
922 * @param c The char to convert.
923 * @param dfault Char to return if conversion fails.
924 * @return The converted character.
925 */
926 char
927 narrow(char_type __c, char __dfault) const
928 {
929 if (_M_narrow[static_cast<unsigned char>(__c)])
930 return _M_narrow[static_cast<unsigned char>(__c)];
931 const char __t = do_narrow(__c, __dfault);
932 if (__t != __dfault)
933 _M_narrow[static_cast<unsigned char>(__c)] = __t;
934 return __t;
935 }
936
937 /**
938 * @brief Narrow char array
939 *
940 * This function converts each char in the input to char using the
941 * simplest reasonable transformation and writes the results to the
942 * destination array. For any char in the input that cannot be
943 * converted, @a dfault is used instead. For an underived ctype<char>
944 * facet, the argument will be copied unchanged.
945 *
946 * This function works as if it returns ctype<char>::do_narrow(lo, hi,
947 * dfault, to). do_narrow() must always return the same result for the
948 * same input.
949 *
950 * Note: this is not what you want for codepage conversions. See
951 * codecvt for that.
952 *
953 * @param lo Pointer to start of range.
954 * @param hi Pointer to end of range.
955 * @param dfault Char to use if conversion fails.
956 * @param to Pointer to the destination array.
957 * @return @a hi.
958 */
959 const char_type*
960 narrow(const char_type* __lo, const char_type* __hi,
961 char __dfault, char *__to) const
962 {
963 if (__builtin_expect(_M_narrow_ok == 1, true))
964 {
965 memcpy(__to, __lo, __hi - __lo);
966 return __hi;
967 }
968 if (!_M_narrow_ok)
969 _M_narrow_init();
970 return this->do_narrow(__lo, __hi, __dfault, __to);
971 }
972
973 protected:
974 /// Returns a pointer to the mask table provided to the constructor, or
975 /// the default from classic_table() if none was provided.
976 const mask*
977 table() const throw()
978 { return _M_table; }
979
980 /// Returns a pointer to the C locale mask table.
981 static const mask*
982 classic_table() throw();
983
984 /**
985 * @brief Destructor.
986 *
987 * This function deletes table() if @a del was true in the
988 * constructor.
989 */
990 virtual
991 ~ctype();
992
993 /**
994 * @brief Convert to uppercase.
995 *
996 * This virtual function converts the char argument to uppercase if
997 * possible. If not possible (for example, '2'), returns the argument.
998 *
999 * do_toupper() is a hook for a derived facet to change the behavior of
1000 * uppercasing. do_toupper() must always return the same result for
1001 * the same input.
1002 *
1003 * @param c The char to convert.
1004 * @return The uppercase char if convertible, else @a c.
1005 */
1006 virtual char_type
1007 do_toupper(char_type) const;
1008
1009 /**
1010 * @brief Convert array to uppercase.
1011 *
1012 * This virtual function converts each char in the range [lo,hi) to
1013 * uppercase if possible. Other chars remain untouched.
1014 *
1015 * do_toupper() is a hook for a derived facet to change the behavior of
1016 * uppercasing. do_toupper() must always return the same result for
1017 * the same input.
1018 *
1019 * @param lo Pointer to start of range.
1020 * @param hi Pointer to end of range.
1021 * @return @a hi.
1022 */
1023 virtual const char_type*
1024 do_toupper(char_type* __lo, const char_type* __hi) const;
1025
1026 /**
1027 * @brief Convert to lowercase.
1028 *
1029 * This virtual function converts the char argument to lowercase if
1030 * possible. If not possible (for example, '2'), returns the argument.
1031 *
1032 * do_tolower() is a hook for a derived facet to change the behavior of
1033 * lowercasing. do_tolower() must always return the same result for
1034 * the same input.
1035 *
1036 * @param c The char to convert.
1037 * @return The lowercase char if convertible, else @a c.
1038 */
1039 virtual char_type
1040 do_tolower(char_type) const;
1041
1042 /**
1043 * @brief Convert array to lowercase.
1044 *
1045 * This virtual function converts each char in the range [lo,hi) to
1046 * lowercase if possible. Other chars remain untouched.
1047 *
1048 * do_tolower() is a hook for a derived facet to change the behavior of
1049 * lowercasing. do_tolower() must always return the same result for
1050 * the same input.
1051 *
1052 * @param lo Pointer to first char in range.
1053 * @param hi Pointer to end of range.
1054 * @return @a hi.
1055 */
1056 virtual const char_type*
1057 do_tolower(char_type* __lo, const char_type* __hi) const;
1058
1059 /**
1060 * @brief Widen char
1061 *
1062 * This virtual function converts the char to char using the simplest
1063 * reasonable transformation. For an underived ctype<char> facet, the
1064 * argument will be returned unchanged.
1065 *
1066 * do_widen() is a hook for a derived facet to change the behavior of
1067 * widening. do_widen() must always return the same result for the
1068 * same input.
1069 *
1070 * Note: this is not what you want for codepage conversions. See
1071 * codecvt for that.
1072 *
1073 * @param c The char to convert.
1074 * @return The converted character.
1075 */
1076 virtual char_type
1077 do_widen(char __c) const
1078 { return __c; }
1079
1080 /**
1081 * @brief Widen char array
1082 *
1083 * This function converts each char in the range [lo,hi) to char using
1084 * the simplest reasonable transformation. For an underived
1085 * ctype<char> facet, the argument will be copied unchanged.
1086 *
1087 * do_widen() is a hook for a derived facet to change the behavior of
1088 * widening. do_widen() must always return the same result for the
1089 * same input.
1090 *
1091 * Note: this is not what you want for codepage conversions. See
1092 * codecvt for that.
1093 *
1094 * @param lo Pointer to start of range.
1095 * @param hi Pointer to end of range.
1096 * @param to Pointer to the destination array.
1097 * @return @a hi.
1098 */
1099 virtual const char*
1100 do_widen(const char* __lo, const char* __hi, char_type* __dest) const
1101 {
1102 memcpy(__dest, __lo, __hi - __lo);
1103 return __hi;
1104 }
1105
1106 /**
1107 * @brief Narrow char
1108 *
1109 * This virtual function converts the char to char using the simplest
1110 * reasonable transformation. If the conversion fails, dfault is
1111 * returned instead. For an underived ctype<char> facet, @a c will be
1112 * returned unchanged.
1113 *
1114 * do_narrow() is a hook for a derived facet to change the behavior of
1115 * narrowing. do_narrow() must always return the same result for the
1116 * same input.
1117 *
1118 * Note: this is not what you want for codepage conversions. See
1119 * codecvt for that.
1120 *
1121 * @param c The char to convert.
1122 * @param dfault Char to return if conversion fails.
1123 * @return The converted char.
1124 */
1125 virtual char
1126 do_narrow(char_type __c, char) const
1127 { return __c; }
1128
1129 /**
1130 * @brief Narrow char array to char array
1131 *
1132 * This virtual function converts each char in the range [lo,hi) to
1133 * char using the simplest reasonable transformation and writes the
1134 * results to the destination array. For any char in the input that
1135 * cannot be converted, @a dfault is used instead. For an underived
1136 * ctype<char> facet, the argument will be copied unchanged.
1137 *
1138 * do_narrow() is a hook for a derived facet to change the behavior of
1139 * narrowing. do_narrow() must always return the same result for the
1140 * same input.
1141 *
1142 * Note: this is not what you want for codepage conversions. See
1143 * codecvt for that.
1144 *
1145 * @param lo Pointer to start of range.
1146 * @param hi Pointer to end of range.
1147 * @param dfault Char to use if conversion fails.
1148 * @param to Pointer to the destination array.
1149 * @return @a hi.
1150 */
1151 virtual const char_type*
1152 do_narrow(const char_type* __lo, const char_type* __hi,
1153 char, char* __dest) const
1154 {
1155 memcpy(__dest, __lo, __hi - __lo);
1156 return __hi;
1157 }
1158
1159 private:
1160
1161 void _M_widen_init() const
1162 {
1163 char __tmp[sizeof(_M_widen)];
1164 for (size_t __i = 0; __i < sizeof(_M_widen); ++__i)
1165 __tmp[__i] = __i;
1166 do_widen(__tmp, __tmp + sizeof(__tmp), _M_widen);
1167
1168 _M_widen_ok = 1;
1169 // Set _M_widen_ok to 2 if memcpy can't be used.
1170 if (memcmp(__tmp, _M_widen, sizeof(_M_widen)))
1171 _M_widen_ok = 2;
1172 }
1173
1174 // Fill in the narrowing cache and flag whether all values are
1175 // valid or not. _M_narrow_ok is set to 2 if memcpy can't
1176 // be used.
1177 void _M_narrow_init() const
1178 {
1179 char __tmp[sizeof(_M_narrow)];
1180 for (size_t __i = 0; __i < sizeof(_M_narrow); ++__i)
1181 __tmp[__i] = __i;
1182 do_narrow(__tmp, __tmp + sizeof(__tmp), 0, _M_narrow);
1183
1184 _M_narrow_ok = 1;
1185 if (memcmp(__tmp, _M_narrow, sizeof(_M_narrow)))
1186 _M_narrow_ok = 2;
1187 else
1188 {
1189 // Deal with the special case of zero: renarrow with a
1190 // different default and compare.
1191 char __c;
1192 do_narrow(__tmp, __tmp + 1, 1, &__c);
1193 if (__c == 1)
1194 _M_narrow_ok = 2;
1195 }
1196 }
1197 };
1198
1199 template<>
1200 const ctype<char>&
1201 use_facet<ctype<char> >(const locale& __loc);
1202
1203 #ifdef _GLIBCXX_USE_WCHAR_T
1204 // 22.2.1.3 ctype<wchar_t> specialization
1205 /**
1206 * @brief The ctype<wchar_t> specialization.
1207 *
1208 * This class defines classification and conversion functions for the
1209 * wchar_t type. It gets used by wchar_t streams for many I/O operations.
1210 * The wchar_t specialization provides a number of optimizations as well.
1211 *
1212 * ctype<wchar_t> inherits its public methods from
1213 * __ctype_abstract_base<wchar_t>.
1214 */
1215 template<>
1216 class ctype<wchar_t> : public __ctype_abstract_base<wchar_t>
1217 {
1218 public:
1219 // Types:
1220 /// Typedef for the template parameter wchar_t.
1221 typedef wchar_t char_type;
1222 typedef wctype_t __wmask_type;
1223
1224 protected:
1225 __c_locale _M_c_locale_ctype;
1226
1227 // Pre-computed narrowed and widened chars.
1228 bool _M_narrow_ok;
1229 char _M_narrow[128];
1230 wint_t _M_widen[1 + static_cast<unsigned char>(-1)];
1231
1232 // Pre-computed elements for do_is.
1233 mask _M_bit[16];
1234 __wmask_type _M_wmask[16];
1235
1236 public:
1237 // Data Members:
1238 /// The facet id for ctype<wchar_t>
1239 static locale::id id;
1240
1241 /**
1242 * @brief Constructor performs initialization.
1243 *
1244 * This is the constructor provided by the standard.
1245 *
1246 * @param refs Passed to the base facet class.
1247 */
1248 explicit
1249 ctype(size_t __refs = 0);
1250
1251 /**
1252 * @brief Constructor performs static initialization.
1253 *
1254 * This constructor is used to construct the initial C locale facet.
1255 *
1256 * @param cloc Handle to C locale data.
1257 * @param refs Passed to the base facet class.
1258 */
1259 explicit
1260 ctype(__c_locale __cloc, size_t __refs = 0);
1261
1262 protected:
1263 __wmask_type
1264 _M_convert_to_wmask(const mask __m) const;
1265
1266 /// Destructor
1267 virtual
1268 ~ctype();
1269
1270 /**
1271 * @brief Test wchar_t classification.
1272 *
1273 * This function finds a mask M for @a c and compares it to mask @a m.
1274 *
1275 * do_is() is a hook for a derived facet to change the behavior of
1276 * classifying. do_is() must always return the same result for the
1277 * same input.
1278 *
1279 * @param c The wchar_t to find the mask of.
1280 * @param m The mask to compare against.
1281 * @return (M & m) != 0.
1282 */
1283 virtual bool
1284 do_is(mask __m, char_type __c) const;
1285
1286 /**
1287 * @brief Return a mask array.
1288 *
1289 * This function finds the mask for each wchar_t in the range [lo,hi)
1290 * and successively writes it to vec. vec must have as many elements
1291 * as the input.
1292 *
1293 * do_is() is a hook for a derived facet to change the behavior of
1294 * classifying. do_is() must always return the same result for the
1295 * same input.
1296 *
1297 * @param lo Pointer to start of range.
1298 * @param hi Pointer to end of range.
1299 * @param vec Pointer to an array of mask storage.
1300 * @return @a hi.
1301 */
1302 virtual const char_type*
1303 do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const;
1304
1305 /**
1306 * @brief Find wchar_t matching mask
1307 *
1308 * This function searches for and returns the first wchar_t c in
1309 * [lo,hi) for which is(m,c) is true.
1310 *
1311 * do_scan_is() is a hook for a derived facet to change the behavior of
1312 * match searching. do_is() must always return the same result for the
1313 * same input.
1314 *
1315 * @param m The mask to compare against.
1316 * @param lo Pointer to start of range.
1317 * @param hi Pointer to end of range.
1318 * @return Pointer to a matching wchar_t if found, else @a hi.
1319 */
1320 virtual const char_type*
1321 do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const;
1322
1323 /**
1324 * @brief Find wchar_t not matching mask
1325 *
1326 * This function searches for and returns a pointer to the first
1327 * wchar_t c of [lo,hi) for which is(m,c) is false.
1328 *
1329 * do_scan_is() is a hook for a derived facet to change the behavior of
1330 * match searching. do_is() must always return the same result for the
1331 * same input.
1332 *
1333 * @param m The mask to compare against.
1334 * @param lo Pointer to start of range.
1335 * @param hi Pointer to end of range.
1336 * @return Pointer to a non-matching wchar_t if found, else @a hi.
1337 */
1338 virtual const char_type*
1339 do_scan_not(mask __m, const char_type* __lo,
1340 const char_type* __hi) const;
1341
1342 /**
1343 * @brief Convert to uppercase.
1344 *
1345 * This virtual function converts the wchar_t argument to uppercase if
1346 * possible. If not possible (for example, '2'), returns the argument.
1347 *
1348 * do_toupper() is a hook for a derived facet to change the behavior of
1349 * uppercasing. do_toupper() must always return the same result for
1350 * the same input.
1351 *
1352 * @param c The wchar_t to convert.
1353 * @return The uppercase wchar_t if convertible, else @a c.
1354 */
1355 virtual char_type
1356 do_toupper(char_type) const;
1357
1358 /**
1359 * @brief Convert array to uppercase.
1360 *
1361 * This virtual function converts each wchar_t in the range [lo,hi) to
1362 * uppercase if possible. Other elements remain untouched.
1363 *
1364 * do_toupper() is a hook for a derived facet to change the behavior of
1365 * uppercasing. do_toupper() must always return the same result for
1366 * the same input.
1367 *
1368 * @param lo Pointer to start of range.
1369 * @param hi Pointer to end of range.
1370 * @return @a hi.
1371 */
1372 virtual const char_type*
1373 do_toupper(char_type* __lo, const char_type* __hi) const;
1374
1375 /**
1376 * @brief Convert to lowercase.
1377 *
1378 * This virtual function converts the argument to lowercase if
1379 * possible. If not possible (for example, '2'), returns the argument.
1380 *
1381 * do_tolower() is a hook for a derived facet to change the behavior of
1382 * lowercasing. do_tolower() must always return the same result for
1383 * the same input.
1384 *
1385 * @param c The wchar_t to convert.
1386 * @return The lowercase wchar_t if convertible, else @a c.
1387 */
1388 virtual char_type
1389 do_tolower(char_type) const;
1390
1391 /**
1392 * @brief Convert array to lowercase.
1393 *
1394 * This virtual function converts each wchar_t in the range [lo,hi) to
1395 * lowercase if possible. Other elements remain untouched.
1396 *
1397 * do_tolower() is a hook for a derived facet to change the behavior of
1398 * lowercasing. do_tolower() must always return the same result for
1399 * the same input.
1400 *
1401 * @param lo Pointer to start of range.
1402 * @param hi Pointer to end of range.
1403 * @return @a hi.
1404 */
1405 virtual const char_type*
1406 do_tolower(char_type* __lo, const char_type* __hi) const;
1407
1408 /**
1409 * @brief Widen char to wchar_t
1410 *
1411 * This virtual function converts the char to wchar_t using the
1412 * simplest reasonable transformation. For an underived ctype<wchar_t>
1413 * facet, the argument will be cast to wchar_t.
1414 *
1415 * do_widen() is a hook for a derived facet to change the behavior of
1416 * widening. do_widen() must always return the same result for the
1417 * same input.
1418 *
1419 * Note: this is not what you want for codepage conversions. See
1420 * codecvt for that.
1421 *
1422 * @param c The char to convert.
1423 * @return The converted wchar_t.
1424 */
1425 virtual char_type
1426 do_widen(char) const;
1427
1428 /**
1429 * @brief Widen char array to wchar_t array
1430 *
1431 * This function converts each char in the input to wchar_t using the
1432 * simplest reasonable transformation. For an underived ctype<wchar_t>
1433 * facet, the argument will be copied, casting each element to wchar_t.
1434 *
1435 * do_widen() is a hook for a derived facet to change the behavior of
1436 * widening. do_widen() must always return the same result for the
1437 * same input.
1438 *
1439 * Note: this is not what you want for codepage conversions. See
1440 * codecvt for that.
1441 *
1442 * @param lo Pointer to start range.
1443 * @param hi Pointer to end of range.
1444 * @param to Pointer to the destination array.
1445 * @return @a hi.
1446 */
1447 virtual const char*
1448 do_widen(const char* __lo, const char* __hi, char_type* __dest) const;
1449
1450 /**
1451 * @brief Narrow wchar_t to char
1452 *
1453 * This virtual function converts the argument to char using
1454 * the simplest reasonable transformation. If the conversion
1455 * fails, dfault is returned instead. For an underived
1456 * ctype<wchar_t> facet, @a c will be cast to char and
1457 * returned.
1458 *
1459 * do_narrow() is a hook for a derived facet to change the
1460 * behavior of narrowing. do_narrow() must always return the
1461 * same result for the same input.
1462 *
1463 * Note: this is not what you want for codepage conversions. See
1464 * codecvt for that.
1465 *
1466 * @param c The wchar_t to convert.
1467 * @param dfault Char to return if conversion fails.
1468 * @return The converted char.
1469 */
1470 virtual char
1471 do_narrow(char_type, char __dfault) const;
1472
1473 /**
1474 * @brief Narrow wchar_t array to char array
1475 *
1476 * This virtual function converts each wchar_t in the range [lo,hi) to
1477 * char using the simplest reasonable transformation and writes the
1478 * results to the destination array. For any wchar_t in the input that
1479 * cannot be converted, @a dfault is used instead. For an underived
1480 * ctype<wchar_t> facet, the argument will be copied, casting each
1481 * element to char.
1482 *
1483 * do_narrow() is a hook for a derived facet to change the behavior of
1484 * narrowing. do_narrow() must always return the same result for the
1485 * same input.
1486 *
1487 * Note: this is not what you want for codepage conversions. See
1488 * codecvt for that.
1489 *
1490 * @param lo Pointer to start of range.
1491 * @param hi Pointer to end of range.
1492 * @param dfault Char to use if conversion fails.
1493 * @param to Pointer to the destination array.
1494 * @return @a hi.
1495 */
1496 virtual const char_type*
1497 do_narrow(const char_type* __lo, const char_type* __hi,
1498 char __dfault, char* __dest) const;
1499
1500 // For use at construction time only.
1501 void
1502 _M_initialize_ctype();
1503 };
1504
1505 template<>
1506 const ctype<wchar_t>&
1507 use_facet<ctype<wchar_t> >(const locale& __loc);
1508 #endif //_GLIBCXX_USE_WCHAR_T
1509
1510 /// @brief class ctype_byname [22.2.1.2].
1511 template<typename _CharT>
1512 class ctype_byname : public ctype<_CharT>
1513 {
1514 public:
1515 typedef typename ctype<_CharT>::mask mask;
1516
1517 explicit
1518 ctype_byname(const char* __s, size_t __refs = 0);
1519
1520 protected:
1521 virtual
1522 ~ctype_byname() { };
1523 };
1524
1525 /// 22.2.1.4 Class ctype_byname specializations.
1526 template<>
1527 class ctype_byname<char> : public ctype<char>
1528 {
1529 public:
1530 explicit
1531 ctype_byname(const char* __s, size_t __refs = 0);
1532
1533 protected:
1534 virtual
1535 ~ctype_byname();
1536 };
1537
1538 #ifdef _GLIBCXX_USE_WCHAR_T
1539 template<>
1540 class ctype_byname<wchar_t> : public ctype<wchar_t>
1541 {
1542 public:
1543 explicit
1544 ctype_byname(const char* __s, size_t __refs = 0);
1545
1546 protected:
1547 virtual
1548 ~ctype_byname();
1549 };
1550 #endif
1551
1552 _GLIBCXX_END_NAMESPACE
1553
1554 // Include host and configuration specific ctype inlines.
1555 #include <bits/ctype_inline.h>
1556
1557 // 22.2.1.5 Template class codecvt
1558 #include <bits/codecvt.h>
1559
1560 _GLIBCXX_BEGIN_NAMESPACE(std)
1561
1562 // 22.2.2 The numeric category.
1563 class __num_base
1564 {
1565 public:
1566 // NB: Code depends on the order of _S_atoms_out elements.
1567 // Below are the indices into _S_atoms_out.
1568 enum
1569 {
1570 _S_ominus,
1571 _S_oplus,
1572 _S_ox,
1573 _S_oX,
1574 _S_odigits,
1575 _S_odigits_end = _S_odigits + 16,
1576 _S_oudigits = _S_odigits_end,
1577 _S_oudigits_end = _S_oudigits + 16,
1578 _S_oe = _S_odigits + 14, // For scientific notation, 'e'
1579 _S_oE = _S_oudigits + 14, // For scientific notation, 'E'
1580 _S_oend = _S_oudigits_end
1581 };
1582
1583 // A list of valid numeric literals for output. This array
1584 // contains chars that will be passed through the current locale's
1585 // ctype<_CharT>.widen() and then used to render numbers.
1586 // For the standard "C" locale, this is
1587 // "-+xX0123456789abcdef0123456789ABCDEF".
1588 static const char* _S_atoms_out;
1589
1590 // String literal of acceptable (narrow) input, for num_get.
1591 // "-+xX0123456789abcdefABCDEF"
1592 static const char* _S_atoms_in;
1593
1594 enum
1595 {
1596 _S_iminus,
1597 _S_iplus,
1598 _S_ix,
1599 _S_iX,
1600 _S_izero,
1601 _S_ie = _S_izero + 14,
1602 _S_iE = _S_izero + 20,
1603 _S_iend = 26
1604 };
1605
1606 // num_put
1607 // Construct and return valid scanf format for floating point types.
1608 static void
1609 _S_format_float(const ios_base& __io, char* __fptr, char __mod);
1610 };
1611
1612 template<typename _CharT>
1613 struct __numpunct_cache : public locale::facet
1614 {
1615 const char* _M_grouping;
1616 size_t _M_grouping_size;
1617 bool _M_use_grouping;
1618 const _CharT* _M_truename;
1619 size_t _M_truename_size;
1620 const _CharT* _M_falsename;
1621 size_t _M_falsename_size;
1622 _CharT _M_decimal_point;
1623 _CharT _M_thousands_sep;
1624
1625 // A list of valid numeric literals for output: in the standard
1626 // "C" locale, this is "-+xX0123456789abcdef0123456789ABCDEF".
1627 // This array contains the chars after having been passed
1628 // through the current locale's ctype<_CharT>.widen().
1629 _CharT _M_atoms_out[__num_base::_S_oend];
1630
1631 // A list of valid numeric literals for input: in the standard
1632 // "C" locale, this is "-+xX0123456789abcdefABCDEF"
1633 // This array contains the chars after having been passed
1634 // through the current locale's ctype<_CharT>.widen().
1635 _CharT _M_atoms_in[__num_base::_S_iend];
1636
1637 bool _M_allocated;
1638
1639 __numpunct_cache(size_t __refs = 0) : facet(__refs),
1640 _M_grouping(NULL), _M_grouping_size(0), _M_use_grouping(false),
1641 _M_truename(NULL), _M_truename_size(0), _M_falsename(NULL),
1642 _M_falsename_size(0), _M_decimal_point(_CharT()),
1643 _M_thousands_sep(_CharT()), _M_allocated(false)
1644 { }
1645
1646 ~__numpunct_cache();
1647
1648 void
1649 _M_cache(const locale& __loc);
1650
1651 private:
1652 __numpunct_cache&
1653 operator=(const __numpunct_cache&);
1654
1655 explicit
1656 __numpunct_cache(const __numpunct_cache&);
1657 };
1658
1659 template<typename _CharT>
1660 __numpunct_cache<_CharT>::~__numpunct_cache()
1661 {
1662 if (_M_allocated)
1663 {
1664 delete [] _M_grouping;
1665 delete [] _M_truename;
1666 delete [] _M_falsename;
1667 }
1668 }
1669
1670 /**
1671 * @brief Numpunct facet.
1672 *
1673 * This facet stores several pieces of information related to printing and
1674 * scanning numbers, such as the decimal point character. It takes a
1675 * template parameter specifying the char type. The numpunct facet is
1676 * used by streams for many I/O operations involving numbers.
1677 *
1678 * The numpunct template uses protected virtual functions to provide the
1679 * actual results. The public accessors forward the call to the virtual
1680 * functions. These virtual functions are hooks for developers to
1681 * implement the behavior they require from a numpunct facet.
1682 */
1683 template<typename _CharT>
1684 class numpunct : public locale::facet
1685 {
1686 public:
1687 // Types:
1688 //@{
1689 /// Public typedefs
1690 typedef _CharT char_type;
1691 typedef basic_string<_CharT> string_type;
1692 //@}
1693 typedef __numpunct_cache<_CharT> __cache_type;
1694
1695 protected:
1696 __cache_type* _M_data;
1697
1698 public:
1699 /// Numpunct facet id.
1700 static locale::id id;
1701
1702 /**
1703 * @brief Numpunct constructor.
1704 *
1705 * @param refs Refcount to pass to the base class.
1706 */
1707 explicit
1708 numpunct(size_t __refs = 0) : facet(__refs), _M_data(NULL)
1709 { _M_initialize_numpunct(); }
1710
1711 /**
1712 * @brief Internal constructor. Not for general use.
1713 *
1714 * This is a constructor for use by the library itself to set up the
1715 * predefined locale facets.
1716 *
1717 * @param cache __numpunct_cache object.
1718 * @param refs Refcount to pass to the base class.
1719 */
1720 explicit
1721 numpunct(__cache_type* __cache, size_t __refs = 0)
1722 : facet(__refs), _M_data(__cache)
1723 { _M_initialize_numpunct(); }
1724
1725 /**
1726 * @brief Internal constructor. Not for general use.
1727 *
1728 * This is a constructor for use by the library itself to set up new
1729 * locales.
1730 *
1731 * @param cloc The "C" locale.
1732 * @param refs Refcount to pass to the base class.
1733 */
1734 explicit
1735 numpunct(__c_locale __cloc, size_t __refs = 0)
1736 : facet(__refs), _M_data(NULL)
1737 { _M_initialize_numpunct(__cloc); }
1738
1739 /**
1740 * @brief Return decimal point character.
1741 *
1742 * This function returns a char_type to use as a decimal point. It
1743 * does so by returning returning
1744 * numpunct<char_type>::do_decimal_point().
1745 *
1746 * @return @a char_type representing a decimal point.
1747 */
1748 char_type
1749 decimal_point() const
1750 { return this->do_decimal_point(); }
1751
1752 /**
1753 * @brief Return thousands separator character.
1754 *
1755 * This function returns a char_type to use as a thousands
1756 * separator. It does so by returning returning
1757 * numpunct<char_type>::do_thousands_sep().
1758 *
1759 * @return char_type representing a thousands separator.
1760 */
1761 char_type
1762 thousands_sep() const
1763 { return this->do_thousands_sep(); }
1764
1765 /**
1766 * @brief Return grouping specification.
1767 *
1768 * This function returns a string representing groupings for the
1769 * integer part of a number. Groupings indicate where thousands
1770 * separators should be inserted in the integer part of a number.
1771 *
1772 * Each char in the return string is interpret as an integer
1773 * rather than a character. These numbers represent the number
1774 * of digits in a group. The first char in the string
1775 * represents the number of digits in the least significant
1776 * group. If a char is negative, it indicates an unlimited
1777 * number of digits for the group. If more chars from the
1778 * string are required to group a number, the last char is used
1779 * repeatedly.
1780 *
1781 * For example, if the grouping() returns "\003\002" and is
1782 * applied to the number 123456789, this corresponds to
1783 * 12,34,56,789. Note that if the string was "32", this would
1784 * put more than 50 digits into the least significant group if
1785 * the character set is ASCII.
1786 *
1787 * The string is returned by calling
1788 * numpunct<char_type>::do_grouping().
1789 *
1790 * @return string representing grouping specification.
1791 */
1792 string
1793 grouping() const
1794 { return this->do_grouping(); }
1795
1796 /**
1797 * @brief Return string representation of bool true.
1798 *
1799 * This function returns a string_type containing the text
1800 * representation for true bool variables. It does so by calling
1801 * numpunct<char_type>::do_truename().
1802 *
1803 * @return string_type representing printed form of true.
1804 */
1805 string_type
1806 truename() const
1807 { return this->do_truename(); }
1808
1809 /**
1810 * @brief Return string representation of bool false.
1811 *
1812 * This function returns a string_type containing the text
1813 * representation for false bool variables. It does so by calling
1814 * numpunct<char_type>::do_falsename().
1815 *
1816 * @return string_type representing printed form of false.
1817 */
1818 string_type
1819 falsename() const
1820 { return this->do_falsename(); }
1821
1822 protected:
1823 /// Destructor.
1824 virtual
1825 ~numpunct();
1826
1827 /**
1828 * @brief Return decimal point character.
1829 *
1830 * Returns a char_type to use as a decimal point. This function is a
1831 * hook for derived classes to change the value returned.
1832 *
1833 * @return @a char_type representing a decimal point.
1834 */
1835 virtual char_type
1836 do_decimal_point() const
1837 { return _M_data->_M_decimal_point; }
1838
1839 /**
1840 * @brief Return thousands separator character.
1841 *
1842 * Returns a char_type to use as a thousands separator. This function
1843 * is a hook for derived classes to change the value returned.
1844 *
1845 * @return @a char_type representing a thousands separator.
1846 */
1847 virtual char_type
1848 do_thousands_sep() const
1849 { return _M_data->_M_thousands_sep; }
1850
1851 /**
1852 * @brief Return grouping specification.
1853 *
1854 * Returns a string representing groupings for the integer part of a
1855 * number. This function is a hook for derived classes to change the
1856 * value returned. @see grouping() for details.
1857 *
1858 * @return String representing grouping specification.
1859 */
1860 virtual string
1861 do_grouping() const
1862 { return _M_data->_M_grouping; }
1863
1864 /**
1865 * @brief Return string representation of bool true.
1866 *
1867 * Returns a string_type containing the text representation for true
1868 * bool variables. This function is a hook for derived classes to
1869 * change the value returned.
1870 *
1871 * @return string_type representing printed form of true.
1872 */
1873 virtual string_type
1874 do_truename() const
1875 { return _M_data->_M_truename; }
1876
1877 /**
1878 * @brief Return string representation of bool false.
1879 *
1880 * Returns a string_type containing the text representation for false
1881 * bool variables. This function is a hook for derived classes to
1882 * change the value returned.
1883 *
1884 * @return string_type representing printed form of false.
1885 */
1886 virtual string_type
1887 do_falsename() const
1888 { return _M_data->_M_falsename; }
1889
1890 // For use at construction time only.
1891 void
1892 _M_initialize_numpunct(__c_locale __cloc = NULL);
1893 };
1894
1895 template<typename _CharT>
1896 locale::id numpunct<_CharT>::id;
1897
1898 template<>
1899 numpunct<char>::~numpunct();
1900
1901 template<>
1902 void
1903 numpunct<char>::_M_initialize_numpunct(__c_locale __cloc);
1904
1905 #ifdef _GLIBCXX_USE_WCHAR_T
1906 template<>
1907 numpunct<wchar_t>::~numpunct();
1908
1909 template<>
1910 void
1911 numpunct<wchar_t>::_M_initialize_numpunct(__c_locale __cloc);
1912 #endif
1913
1914 /// @brief class numpunct_byname [22.2.3.2].
1915 template<typename _CharT>
1916 class numpunct_byname : public numpunct<_CharT>
1917 {
1918 public:
1919 typedef _CharT char_type;
1920 typedef basic_string<_CharT> string_type;
1921
1922 explicit
1923 numpunct_byname(const char* __s, size_t __refs = 0)
1924 : numpunct<_CharT>(__refs)
1925 {
1926 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
1927 {
1928 __c_locale __tmp;
1929 this->_S_create_c_locale(__tmp, __s);
1930 this->_M_initialize_numpunct(__tmp);
1931 this->_S_destroy_c_locale(__tmp);
1932 }
1933 }
1934
1935 protected:
1936 virtual
1937 ~numpunct_byname() { }
1938 };
1939
1940 _GLIBCXX_BEGIN_LDBL_NAMESPACE
1941 /**
1942 * @brief Facet for parsing number strings.
1943 *
1944 * This facet encapsulates the code to parse and return a number
1945 * from a string. It is used by the istream numeric extraction
1946 * operators.
1947 *
1948 * The num_get template uses protected virtual functions to provide the
1949 * actual results. The public accessors forward the call to the virtual
1950 * functions. These virtual functions are hooks for developers to
1951 * implement the behavior they require from the num_get facet.
1952 */
1953 template<typename _CharT, typename _InIter>
1954 class num_get : public locale::facet
1955 {
1956 public:
1957 // Types:
1958 //@{
1959 /// Public typedefs
1960 typedef _CharT char_type;
1961 typedef _InIter iter_type;
1962 //@}
1963
1964 /// Numpunct facet id.
1965 static locale::id id;
1966
1967 /**
1968 * @brief Constructor performs initialization.
1969 *
1970 * This is the constructor provided by the standard.
1971 *
1972 * @param refs Passed to the base facet class.
1973 */
1974 explicit
1975 num_get(size_t __refs = 0) : facet(__refs) { }
1976
1977 /**
1978 * @brief Numeric parsing.
1979 *
1980 * Parses the input stream into the bool @a v. It does so by calling
1981 * num_get::do_get().
1982 *
1983 * If ios_base::boolalpha is set, attempts to read
1984 * ctype<CharT>::truename() or ctype<CharT>::falsename(). Sets
1985 * @a v to true or false if successful. Sets err to
1986 * ios_base::failbit if reading the string fails. Sets err to
1987 * ios_base::eofbit if the stream is emptied.
1988 *
1989 * If ios_base::boolalpha is not set, proceeds as with reading a long,
1990 * except if the value is 1, sets @a v to true, if the value is 0, sets
1991 * @a v to false, and otherwise set err to ios_base::failbit.
1992 *
1993 * @param in Start of input stream.
1994 * @param end End of input stream.
1995 * @param io Source of locale and flags.
1996 * @param err Error flags to set.
1997 * @param v Value to format and insert.
1998 * @return Iterator after reading.
1999 */
2000 iter_type
2001 get(iter_type __in, iter_type __end, ios_base& __io,
2002 ios_base::iostate& __err, bool& __v) const
2003 { return this->do_get(__in, __end, __io, __err, __v); }
2004
2005 //@{
2006 /**
2007 * @brief Numeric parsing.
2008 *
2009 * Parses the input stream into the integral variable @a v. It does so
2010 * by calling num_get::do_get().
2011 *
2012 * Parsing is affected by the flag settings in @a io.
2013 *
2014 * The basic parse is affected by the value of io.flags() &
2015 * ios_base::basefield. If equal to ios_base::oct, parses like the
2016 * scanf %o specifier. Else if equal to ios_base::hex, parses like %X
2017 * specifier. Else if basefield equal to 0, parses like the %i
2018 * specifier. Otherwise, parses like %d for signed and %u for unsigned
2019 * types. The matching type length modifier is also used.
2020 *
2021 * Digit grouping is intrepreted according to numpunct::grouping() and
2022 * numpunct::thousands_sep(). If the pattern of digit groups isn't
2023 * consistent, sets err to ios_base::failbit.
2024 *
2025 * If parsing the string yields a valid value for @a v, @a v is set.
2026 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
2027 * Sets err to ios_base::eofbit if the stream is emptied.
2028 *
2029 * @param in Start of input stream.
2030 * @param end End of input stream.
2031 * @param io Source of locale and flags.
2032 * @param err Error flags to set.
2033 * @param v Value to format and insert.
2034 * @return Iterator after reading.
2035 */
2036 iter_type
2037 get(iter_type __in, iter_type __end, ios_base& __io,
2038 ios_base::iostate& __err, long& __v) const
2039 { return this->do_get(__in, __end, __io, __err, __v); }
2040
2041 iter_type
2042 get(iter_type __in, iter_type __end, ios_base& __io,
2043 ios_base::iostate& __err, unsigned short& __v) const
2044 { return this->do_get(__in, __end, __io, __err, __v); }
2045
2046 iter_type
2047 get(iter_type __in, iter_type __end, ios_base& __io,
2048 ios_base::iostate& __err, unsigned int& __v) const
2049 { return this->do_get(__in, __end, __io, __err, __v); }
2050
2051 iter_type
2052 get(iter_type __in, iter_type __end, ios_base& __io,
2053 ios_base::iostate& __err, unsigned long& __v) const
2054 { return this->do_get(__in, __end, __io, __err, __v); }
2055
2056 #ifdef _GLIBCXX_USE_LONG_LONG
2057 iter_type
2058 get(iter_type __in, iter_type __end, ios_base& __io,
2059 ios_base::iostate& __err, long long& __v) const
2060 { return this->do_get(__in, __end, __io, __err, __v); }
2061
2062 iter_type
2063 get(iter_type __in, iter_type __end, ios_base& __io,
2064 ios_base::iostate& __err, unsigned long long& __v) const
2065 { return this->do_get(__in, __end, __io, __err, __v); }
2066 #endif
2067 //@}
2068
2069 //@{
2070 /**
2071 * @brief Numeric parsing.
2072 *
2073 * Parses the input stream into the integral variable @a v. It does so
2074 * by calling num_get::do_get().
2075 *
2076 * The input characters are parsed like the scanf %g specifier. The
2077 * matching type length modifier is also used.
2078 *
2079 * The decimal point character used is numpunct::decimal_point().
2080 * Digit grouping is intrepreted according to numpunct::grouping() and
2081 * numpunct::thousands_sep(). If the pattern of digit groups isn't
2082 * consistent, sets err to ios_base::failbit.
2083 *
2084 * If parsing the string yields a valid value for @a v, @a v is set.
2085 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
2086 * Sets err to ios_base::eofbit if the stream is emptied.
2087 *
2088 * @param in Start of input stream.
2089 * @param end End of input stream.
2090 * @param io Source of locale and flags.
2091 * @param err Error flags to set.
2092 * @param v Value to format and insert.
2093 * @return Iterator after reading.
2094 */
2095 iter_type
2096 get(iter_type __in, iter_type __end, ios_base& __io,
2097 ios_base::iostate& __err, float& __v) const
2098 { return this->do_get(__in, __end, __io, __err, __v); }
2099
2100 iter_type
2101 get(iter_type __in, iter_type __end, ios_base& __io,
2102 ios_base::iostate& __err, double& __v) const
2103 { return this->do_get(__in, __end, __io, __err, __v); }
2104
2105 iter_type
2106 get(iter_type __in, iter_type __end, ios_base& __io,
2107 ios_base::iostate& __err, long double& __v) const
2108 { return this->do_get(__in, __end, __io, __err, __v); }
2109 //@}
2110
2111 /**
2112 * @brief Numeric parsing.
2113 *
2114 * Parses the input stream into the pointer variable @a v. It does so
2115 * by calling num_get::do_get().
2116 *
2117 * The input characters are parsed like the scanf %p specifier.
2118 *
2119 * Digit grouping is intrepreted according to numpunct::grouping() and
2120 * numpunct::thousands_sep(). If the pattern of digit groups isn't
2121 * consistent, sets err to ios_base::failbit.
2122 *
2123 * Note that the digit grouping effect for pointers is a bit ambiguous
2124 * in the standard and shouldn't be relied on. See DR 344.
2125 *
2126 * If parsing the string yields a valid value for @a v, @a v is set.
2127 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
2128 * Sets err to ios_base::eofbit if the stream is emptied.
2129 *
2130 * @param in Start of input stream.
2131 * @param end End of input stream.
2132 * @param io Source of locale and flags.
2133 * @param err Error flags to set.
2134 * @param v Value to format and insert.
2135 * @return Iterator after reading.
2136 */
2137 iter_type
2138 get(iter_type __in, iter_type __end, ios_base& __io,
2139 ios_base::iostate& __err, void*& __v) const
2140 { return this->do_get(__in, __end, __io, __err, __v); }
2141
2142 protected:
2143 /// Destructor.
2144 virtual ~num_get() { }
2145
2146 iter_type
2147 _M_extract_float(iter_type, iter_type, ios_base&, ios_base::iostate&,
2148 string& __xtrc) const;
2149
2150 template<typename _ValueT>
2151 iter_type
2152 _M_extract_int(iter_type, iter_type, ios_base&, ios_base::iostate&,
2153 _ValueT& __v) const;
2154
2155 template<typename _CharT2>
2156 typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, int>::__type
2157 _M_find(const _CharT2*, size_t __len, _CharT2 __c) const
2158 {
2159 int __ret = -1;
2160 if (__len <= 10)
2161 {
2162 if (__c >= _CharT2('0') && __c < _CharT2(_CharT2('0') + __len))
2163 __ret = __c - _CharT2('0');
2164 }
2165 else
2166 {
2167 if (__c >= _CharT2('0') && __c <= _CharT2('9'))
2168 __ret = __c - _CharT2('0');
2169 else if (__c >= _CharT2('a') && __c <= _CharT2('f'))
2170 __ret = 10 + (__c - _CharT2('a'));
2171 else if (__c >= _CharT2('A') && __c <= _CharT2('F'))
2172 __ret = 10 + (__c - _CharT2('A'));
2173 }
2174 return __ret;
2175 }
2176
2177 template<typename _CharT2>
2178 typename __gnu_cxx::__enable_if<!__is_char<_CharT2>::__value,
2179 int>::__type
2180 _M_find(const _CharT2* __zero, size_t __len, _CharT2 __c) const
2181 {
2182 int __ret = -1;
2183 const char_type* __q = char_traits<_CharT2>::find(__zero, __len, __c);
2184 if (__q)
2185 {
2186 __ret = __q - __zero;
2187 if (__ret > 15)
2188 __ret -= 6;
2189 }
2190 return __ret;
2191 }
2192
2193 //@{
2194 /**
2195 * @brief Numeric parsing.
2196 *
2197 * Parses the input stream into the variable @a v. This function is a
2198 * hook for derived classes to change the value returned. @see get()
2199 * for more details.
2200 *
2201 * @param in Start of input stream.
2202 * @param end End of input stream.
2203 * @param io Source of locale and flags.
2204 * @param err Error flags to set.
2205 * @param v Value to format and insert.
2206 * @return Iterator after reading.
2207 */
2208 virtual iter_type
2209 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, bool&) const;
2210
2211
2212 virtual iter_type
2213 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, long&) const;
2214
2215 virtual iter_type
2216 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2217 unsigned short&) const;
2218
2219 virtual iter_type
2220 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2221 unsigned int&) const;
2222
2223 virtual iter_type
2224 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2225 unsigned long&) const;
2226
2227 #ifdef _GLIBCXX_USE_LONG_LONG
2228 virtual iter_type
2229 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2230 long long&) const;
2231
2232 virtual iter_type
2233 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2234 unsigned long long&) const;
2235 #endif
2236
2237 virtual iter_type
2238 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2239 float&) const;
2240
2241 virtual iter_type
2242 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2243 double&) const;
2244
2245 // XXX GLIBCXX_ABI Deprecated
2246 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2247 virtual iter_type
2248 __do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2249 double&) const;
2250 #else
2251 virtual iter_type
2252 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2253 long double&) const;
2254 #endif
2255
2256 virtual iter_type
2257 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2258 void*&) const;
2259
2260 // XXX GLIBCXX_ABI Deprecated
2261 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2262 virtual iter_type
2263 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2264 long double&) const;
2265 #endif
2266 //@}
2267 };
2268
2269 template<typename _CharT, typename _InIter>
2270 locale::id num_get<_CharT, _InIter>::id;
2271
2272
2273 /**
2274 * @brief Facet for converting numbers to strings.
2275 *
2276 * This facet encapsulates the code to convert a number to a string. It is
2277 * used by the ostream numeric insertion operators.
2278 *
2279 * The num_put template uses protected virtual functions to provide the
2280 * actual results. The public accessors forward the call to the virtual
2281 * functions. These virtual functions are hooks for developers to
2282 * implement the behavior they require from the num_put facet.
2283 */
2284 template<typename _CharT, typename _OutIter>
2285 class num_put : public locale::facet
2286 {
2287 public:
2288 // Types:
2289 //@{
2290 /// Public typedefs
2291 typedef _CharT char_type;
2292 typedef _OutIter iter_type;
2293 //@}
2294
2295 /// Numpunct facet id.
2296 static locale::id id;
2297
2298 /**
2299 * @brief Constructor performs initialization.
2300 *
2301 * This is the constructor provided by the standard.
2302 *
2303 * @param refs Passed to the base facet class.
2304 */
2305 explicit
2306 num_put(size_t __refs = 0) : facet(__refs) { }
2307
2308 /**
2309 * @brief Numeric formatting.
2310 *
2311 * Formats the boolean @a v and inserts it into a stream. It does so
2312 * by calling num_put::do_put().
2313 *
2314 * If ios_base::boolalpha is set, writes ctype<CharT>::truename() or
2315 * ctype<CharT>::falsename(). Otherwise formats @a v as an int.
2316 *
2317 * @param s Stream to write to.
2318 * @param io Source of locale and flags.
2319 * @param fill Char_type to use for filling.
2320 * @param v Value to format and insert.
2321 * @return Iterator after writing.
2322 */
2323 iter_type
2324 put(iter_type __s, ios_base& __f, char_type __fill, bool __v) const
2325 { return this->do_put(__s, __f, __fill, __v); }
2326
2327 //@{
2328 /**
2329 * @brief Numeric formatting.
2330 *
2331 * Formats the integral value @a v and inserts it into a
2332 * stream. It does so by calling num_put::do_put().
2333 *
2334 * Formatting is affected by the flag settings in @a io.
2335 *
2336 * The basic format is affected by the value of io.flags() &
2337 * ios_base::basefield. If equal to ios_base::oct, formats like the
2338 * printf %o specifier. Else if equal to ios_base::hex, formats like
2339 * %x or %X with ios_base::uppercase unset or set respectively.
2340 * Otherwise, formats like %d, %ld, %lld for signed and %u, %lu, %llu
2341 * for unsigned values. Note that if both oct and hex are set, neither
2342 * will take effect.
2343 *
2344 * If ios_base::showpos is set, '+' is output before positive values.
2345 * If ios_base::showbase is set, '0' precedes octal values (except 0)
2346 * and '0[xX]' precedes hex values.
2347 *
2348 * Thousands separators are inserted according to numpunct::grouping()
2349 * and numpunct::thousands_sep(). The decimal point character used is
2350 * numpunct::decimal_point().
2351 *
2352 * If io.width() is non-zero, enough @a fill characters are inserted to
2353 * make the result at least that wide. If
2354 * (io.flags() & ios_base::adjustfield) == ios_base::left, result is
2355 * padded at the end. If ios_base::internal, then padding occurs
2356 * immediately after either a '+' or '-' or after '0x' or '0X'.
2357 * Otherwise, padding occurs at the beginning.
2358 *
2359 * @param s Stream to write to.
2360 * @param io Source of locale and flags.
2361 * @param fill Char_type to use for filling.
2362 * @param v Value to format and insert.
2363 * @return Iterator after writing.
2364 */
2365 iter_type
2366 put(iter_type __s, ios_base& __f, char_type __fill, long __v) const
2367 { return this->do_put(__s, __f, __fill, __v); }
2368
2369 iter_type
2370 put(iter_type __s, ios_base& __f, char_type __fill,
2371 unsigned long __v) const
2372 { return this->do_put(__s, __f, __fill, __v); }
2373
2374 #ifdef _GLIBCXX_USE_LONG_LONG
2375 iter_type
2376 put(iter_type __s, ios_base& __f, char_type __fill, long long __v) const
2377 { return this->do_put(__s, __f, __fill, __v); }
2378
2379 iter_type
2380 put(iter_type __s, ios_base& __f, char_type __fill,
2381 unsigned long long __v) const
2382 { return this->do_put(__s, __f, __fill, __v); }
2383 #endif
2384 //@}
2385
2386 //@{
2387 /**
2388 * @brief Numeric formatting.
2389 *
2390 * Formats the floating point value @a v and inserts it into a stream.
2391 * It does so by calling num_put::do_put().
2392 *
2393 * Formatting is affected by the flag settings in @a io.
2394 *
2395 * The basic format is affected by the value of io.flags() &
2396 * ios_base::floatfield. If equal to ios_base::fixed, formats like the
2397 * printf %f specifier. Else if equal to ios_base::scientific, formats
2398 * like %e or %E with ios_base::uppercase unset or set respectively.
2399 * Otherwise, formats like %g or %G depending on uppercase. Note that
2400 * if both fixed and scientific are set, the effect will also be like
2401 * %g or %G.
2402 *
2403 * The output precision is given by io.precision(). This precision is
2404 * capped at numeric_limits::digits10 + 2 (different for double and
2405 * long double). The default precision is 6.
2406 *
2407 * If ios_base::showpos is set, '+' is output before positive values.
2408 * If ios_base::showpoint is set, a decimal point will always be
2409 * output.
2410 *
2411 * Thousands separators are inserted according to numpunct::grouping()
2412 * and numpunct::thousands_sep(). The decimal point character used is
2413 * numpunct::decimal_point().
2414 *
2415 * If io.width() is non-zero, enough @a fill characters are inserted to
2416 * make the result at least that wide. If
2417 * (io.flags() & ios_base::adjustfield) == ios_base::left, result is
2418 * padded at the end. If ios_base::internal, then padding occurs
2419 * immediately after either a '+' or '-' or after '0x' or '0X'.
2420 * Otherwise, padding occurs at the beginning.
2421 *
2422 * @param s Stream to write to.
2423 * @param io Source of locale and flags.
2424 * @param fill Char_type to use for filling.
2425 * @param v Value to format and insert.
2426 * @return Iterator after writing.
2427 */
2428 iter_type
2429 put(iter_type __s, ios_base& __f, char_type __fill, double __v) const
2430 { return this->do_put(__s, __f, __fill, __v); }
2431
2432 iter_type
2433 put(iter_type __s, ios_base& __f, char_type __fill,
2434 long double __v) const
2435 { return this->do_put(__s, __f, __fill, __v); }
2436 //@}
2437
2438 /**
2439 * @brief Numeric formatting.
2440 *
2441 * Formats the pointer value @a v and inserts it into a stream. It
2442 * does so by calling num_put::do_put().
2443 *
2444 * This function formats @a v as an unsigned long with ios_base::hex
2445 * and ios_base::showbase set.
2446 *
2447 * @param s Stream to write to.
2448 * @param io Source of locale and flags.
2449 * @param fill Char_type to use for filling.
2450 * @param v Value to format and insert.
2451 * @return Iterator after writing.
2452 */
2453 iter_type
2454 put(iter_type __s, ios_base& __f, char_type __fill,
2455 const void* __v) const
2456 { return this->do_put(__s, __f, __fill, __v); }
2457
2458 protected:
2459 template<typename _ValueT>
2460 iter_type
2461 _M_insert_float(iter_type, ios_base& __io, char_type __fill,
2462 char __mod, _ValueT __v) const;
2463
2464 void
2465 _M_group_float(const char* __grouping, size_t __grouping_size,
2466 char_type __sep, const char_type* __p, char_type* __new,
2467 char_type* __cs, int& __len) const;
2468
2469 template<typename _ValueT>
2470 iter_type
2471 _M_insert_int(iter_type, ios_base& __io, char_type __fill,
2472 _ValueT __v) const;
2473
2474 void
2475 _M_group_int(const char* __grouping, size_t __grouping_size,
2476 char_type __sep, ios_base& __io, char_type* __new,
2477 char_type* __cs, int& __len) const;
2478
2479 void
2480 _M_pad(char_type __fill, streamsize __w, ios_base& __io,
2481 char_type* __new, const char_type* __cs, int& __len) const;
2482
2483 /// Destructor.
2484 virtual
2485 ~num_put() { };
2486
2487 //@{
2488 /**
2489 * @brief Numeric formatting.
2490 *
2491 * These functions do the work of formatting numeric values and
2492 * inserting them into a stream. This function is a hook for derived
2493 * classes to change the value returned.
2494 *
2495 * @param s Stream to write to.
2496 * @param io Source of locale and flags.
2497 * @param fill Char_type to use for filling.
2498 * @param v Value to format and insert.
2499 * @return Iterator after writing.
2500 */
2501 virtual iter_type
2502 do_put(iter_type, ios_base&, char_type __fill, bool __v) const;
2503
2504 virtual iter_type
2505 do_put(iter_type, ios_base&, char_type __fill, long __v) const;
2506
2507 virtual iter_type
2508 do_put(iter_type, ios_base&, char_type __fill, unsigned long) const;
2509
2510 #ifdef _GLIBCXX_USE_LONG_LONG
2511 virtual iter_type
2512 do_put(iter_type, ios_base&, char_type __fill, long long __v) const;
2513
2514 virtual iter_type
2515 do_put(iter_type, ios_base&, char_type __fill, unsigned long long) const;
2516 #endif
2517
2518 virtual iter_type
2519 do_put(iter_type, ios_base&, char_type __fill, double __v) const;
2520
2521 // XXX GLIBCXX_ABI Deprecated
2522 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2523 virtual iter_type
2524 __do_put(iter_type, ios_base&, char_type __fill, double __v) const;
2525 #else
2526 virtual iter_type
2527 do_put(iter_type, ios_base&, char_type __fill, long double __v) const;
2528 #endif
2529
2530 virtual iter_type
2531 do_put(iter_type, ios_base&, char_type __fill, const void* __v) const;
2532
2533 // XXX GLIBCXX_ABI Deprecated
2534 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2535 virtual iter_type
2536 do_put(iter_type, ios_base&, char_type __fill, long double __v) const;
2537 #endif
2538 //@}
2539 };
2540
2541 template <typename _CharT, typename _OutIter>
2542 locale::id num_put<_CharT, _OutIter>::id;
2543
2544 _GLIBCXX_END_LDBL_NAMESPACE
2545
2546 /**
2547 * @brief Facet for localized string comparison.
2548 *
2549 * This facet encapsulates the code to compare strings in a localized
2550 * manner.
2551 *
2552 * The collate template uses protected virtual functions to provide
2553 * the actual results. The public accessors forward the call to
2554 * the virtual functions. These virtual functions are hooks for
2555 * developers to implement the behavior they require from the
2556 * collate facet.
2557 */
2558 template<typename _CharT>
2559 class collate : public locale::facet
2560 {
2561 public:
2562 // Types:
2563 //@{
2564 /// Public typedefs
2565 typedef _CharT char_type;
2566 typedef basic_string<_CharT> string_type;
2567 //@}
2568
2569 protected:
2570 // Underlying "C" library locale information saved from
2571 // initialization, needed by collate_byname as well.
2572 __c_locale _M_c_locale_collate;
2573
2574 public:
2575 /// Numpunct facet id.
2576 static locale::id id;
2577
2578 /**
2579 * @brief Constructor performs initialization.
2580 *
2581 * This is the constructor provided by the standard.
2582 *
2583 * @param refs Passed to the base facet class.
2584 */
2585 explicit
2586 collate(size_t __refs = 0)
2587 : facet(__refs), _M_c_locale_collate(_S_get_c_locale())
2588 { }
2589
2590 /**
2591 * @brief Internal constructor. Not for general use.
2592 *
2593 * This is a constructor for use by the library itself to set up new
2594 * locales.
2595 *
2596 * @param cloc The "C" locale.
2597 * @param refs Passed to the base facet class.
2598 */
2599 explicit
2600 collate(__c_locale __cloc, size_t __refs = 0)
2601 : facet(__refs), _M_c_locale_collate(_S_clone_c_locale(__cloc))
2602 { }
2603
2604 /**
2605 * @brief Compare two strings.
2606 *
2607 * This function compares two strings and returns the result by calling
2608 * collate::do_compare().
2609 *
2610 * @param lo1 Start of string 1.
2611 * @param hi1 End of string 1.
2612 * @param lo2 Start of string 2.
2613 * @param hi2 End of string 2.
2614 * @return 1 if string1 > string2, -1 if string1 < string2, else 0.
2615 */
2616 int
2617 compare(const _CharT* __lo1, const _CharT* __hi1,
2618 const _CharT* __lo2, const _CharT* __hi2) const
2619 { return this->do_compare(__lo1, __hi1, __lo2, __hi2); }
2620
2621 /**
2622 * @brief Transform string to comparable form.
2623 *
2624 * This function is a wrapper for strxfrm functionality. It takes the
2625 * input string and returns a modified string that can be directly
2626 * compared to other transformed strings. In the "C" locale, this
2627 * function just returns a copy of the input string. In some other
2628 * locales, it may replace two chars with one, change a char for
2629 * another, etc. It does so by returning collate::do_transform().
2630 *
2631 * @param lo Start of string.
2632 * @param hi End of string.
2633 * @return Transformed string_type.
2634 */
2635 string_type
2636 transform(const _CharT* __lo, const _CharT* __hi) const
2637 { return this->do_transform(__lo, __hi); }
2638
2639 /**
2640 * @brief Return hash of a string.
2641 *
2642 * This function computes and returns a hash on the input string. It
2643 * does so by returning collate::do_hash().
2644 *
2645 * @param lo Start of string.
2646 * @param hi End of string.
2647 * @return Hash value.
2648 */
2649 long
2650 hash(const _CharT* __lo, const _CharT* __hi) const
2651 { return this->do_hash(__lo, __hi); }
2652
2653 // Used to abstract out _CharT bits in virtual member functions, below.
2654 int
2655 _M_compare(const _CharT*, const _CharT*) const;
2656
2657 size_t
2658 _M_transform(_CharT*, const _CharT*, size_t) const;
2659
2660 protected:
2661 /// Destructor.
2662 virtual
2663 ~collate()
2664 { _S_destroy_c_locale(_M_c_locale_collate); }
2665
2666 /**
2667 * @brief Compare two strings.
2668 *
2669 * This function is a hook for derived classes to change the value
2670 * returned. @see compare().
2671 *
2672 * @param lo1 Start of string 1.
2673 * @param hi1 End of string 1.
2674 * @param lo2 Start of string 2.
2675 * @param hi2 End of string 2.
2676 * @return 1 if string1 > string2, -1 if string1 < string2, else 0.
2677 */
2678 virtual int
2679 do_compare(const _CharT* __lo1, const _CharT* __hi1,
2680 const _CharT* __lo2, const _CharT* __hi2) const;
2681
2682 /**
2683 * @brief Transform string to comparable form.
2684 *
2685 * This function is a hook for derived classes to change the value
2686 * returned.
2687 *
2688 * @param lo1 Start of string 1.
2689 * @param hi1 End of string 1.
2690 * @param lo2 Start of string 2.
2691 * @param hi2 End of string 2.
2692 * @return 1 if string1 > string2, -1 if string1 < string2, else 0.
2693 */
2694 virtual string_type
2695 do_transform(const _CharT* __lo, const _CharT* __hi) const;
2696
2697 /**
2698 * @brief Return hash of a string.
2699 *
2700 * This function computes and returns a hash on the input string. This
2701 * function is a hook for derived classes to change the value returned.
2702 *
2703 * @param lo Start of string.
2704 * @param hi End of string.
2705 * @return Hash value.
2706 */
2707 virtual long
2708 do_hash(const _CharT* __lo, const _CharT* __hi) const;
2709 };
2710
2711 template<typename _CharT>
2712 locale::id collate<_CharT>::id;
2713
2714 // Specializations.
2715 template<>
2716 int
2717 collate<char>::_M_compare(const char*, const char*) const;
2718
2719 template<>
2720 size_t
2721 collate<char>::_M_transform(char*, const char*, size_t) const;
2722
2723 #ifdef _GLIBCXX_USE_WCHAR_T
2724 template<>
2725 int
2726 collate<wchar_t>::_M_compare(const wchar_t*, const wchar_t*) const;
2727
2728 template<>
2729 size_t
2730 collate<wchar_t>::_M_transform(wchar_t*, const wchar_t*, size_t) const;
2731 #endif
2732
2733 /// @brief class collate_byname [22.2.4.2].
2734 template<typename _CharT>
2735 class collate_byname : public collate<_CharT>
2736 {
2737 public:
2738 //@{
2739 /// Public typedefs
2740 typedef _CharT char_type;
2741 typedef basic_string<_CharT> string_type;
2742 //@}
2743
2744 explicit
2745 collate_byname(const char* __s, size_t __refs = 0)
2746 : collate<_CharT>(__refs)
2747 {
2748 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
2749 {
2750 this->_S_destroy_c_locale(this->_M_c_locale_collate);
2751 this->_S_create_c_locale(this->_M_c_locale_collate, __s);
2752 }
2753 }
2754
2755 protected:
2756 virtual
2757 ~collate_byname() { }
2758 };
2759
2760
2761 /**
2762 * @brief Time format ordering data.
2763 *
2764 * This class provides an enum representing different orderings of day,
2765 * month, and year.
2766 */
2767 class time_base
2768 {
2769 public:
2770 enum dateorder { no_order, dmy, mdy, ymd, ydm };
2771 };
2772
2773 template<typename _CharT>
2774 struct __timepunct_cache : public locale::facet
2775 {
2776 // List of all known timezones, with GMT first.
2777 static const _CharT* _S_timezones[14];
2778
2779 const _CharT* _M_date_format;
2780 const _CharT* _M_date_era_format;
2781 const _CharT* _M_time_format;
2782 const _CharT* _M_time_era_format;
2783 const _CharT* _M_date_time_format;
2784 const _CharT* _M_date_time_era_format;
2785 const _CharT* _M_am;
2786 const _CharT* _M_pm;
2787 const _CharT* _M_am_pm_format;
2788
2789 // Day names, starting with "C"'s Sunday.
2790 const _CharT* _M_day1;
2791 const _CharT* _M_day2;
2792 const _CharT* _M_day3;
2793 const _CharT* _M_day4;
2794 const _CharT* _M_day5;
2795 const _CharT* _M_day6;
2796 const _CharT* _M_day7;
2797
2798 // Abbreviated day names, starting with "C"'s Sun.
2799 const _CharT* _M_aday1;
2800 const _CharT* _M_aday2;
2801 const _CharT* _M_aday3;
2802 const _CharT* _M_aday4;
2803 const _CharT* _M_aday5;
2804 const _CharT* _M_aday6;
2805 const _CharT* _M_aday7;
2806
2807 // Month names, starting with "C"'s January.
2808 const _CharT* _M_month01;
2809 const _CharT* _M_month02;
2810 const _CharT* _M_month03;
2811 const _CharT* _M_month04;
2812 const _CharT* _M_month05;
2813 const _CharT* _M_month06;
2814 const _CharT* _M_month07;
2815 const _CharT* _M_month08;
2816 const _CharT* _M_month09;
2817 const _CharT* _M_month10;
2818 const _CharT* _M_month11;
2819 const _CharT* _M_month12;
2820
2821 // Abbreviated month names, starting with "C"'s Jan.
2822 const _CharT* _M_amonth01;
2823 const _CharT* _M_amonth02;
2824 const _CharT* _M_amonth03;
2825 const _CharT* _M_amonth04;
2826 const _CharT* _M_amonth05;
2827 const _CharT* _M_amonth06;
2828 const _CharT* _M_amonth07;
2829 const _CharT* _M_amonth08;
2830 const _CharT* _M_amonth09;
2831 const _CharT* _M_amonth10;
2832 const _CharT* _M_amonth11;
2833 const _CharT* _M_amonth12;
2834
2835 bool _M_allocated;
2836
2837 __timepunct_cache(size_t __refs = 0) : facet(__refs),
2838 _M_date_format(NULL), _M_date_era_format(NULL), _M_time_format(NULL),
2839 _M_time_era_format(NULL), _M_date_time_format(NULL),
2840 _M_date_time_era_format(NULL), _M_am(NULL), _M_pm(NULL),
2841 _M_am_pm_format(NULL), _M_day1(NULL), _M_day2(NULL), _M_day3(NULL),
2842 _M_day4(NULL), _M_day5(NULL), _M_day6(NULL), _M_day7(NULL),
2843 _M_aday1(NULL), _M_aday2(NULL), _M_aday3(NULL), _M_aday4(NULL),
2844 _M_aday5(NULL), _M_aday6(NULL), _M_aday7(NULL), _M_month01(NULL),
2845 _M_month02(NULL), _M_month03(NULL), _M_month04(NULL), _M_month05(NULL),
2846 _M_month06(NULL), _M_month07(NULL), _M_month08(NULL), _M_month09(NULL),
2847 _M_month10(NULL), _M_month11(NULL), _M_month12(NULL), _M_amonth01(NULL),
2848 _M_amonth02(NULL), _M_amonth03(NULL), _M_amonth04(NULL),
2849 _M_amonth05(NULL), _M_amonth06(NULL), _M_amonth07(NULL),
2850 _M_amonth08(NULL), _M_amonth09(NULL), _M_amonth10(NULL),
2851 _M_amonth11(NULL), _M_amonth12(NULL), _M_allocated(false)
2852 { }
2853
2854 ~__timepunct_cache();
2855
2856 void
2857 _M_cache(const locale& __loc);
2858
2859 private:
2860 __timepunct_cache&
2861 operator=(const __timepunct_cache&);
2862
2863 explicit
2864 __timepunct_cache(const __timepunct_cache&);
2865 };
2866
2867 template<typename _CharT>
2868 __timepunct_cache<_CharT>::~__timepunct_cache()
2869 {
2870 if (_M_allocated)
2871 {
2872 // Unused.
2873 }
2874 }
2875
2876 // Specializations.
2877 template<>
2878 const char*
2879 __timepunct_cache<char>::_S_timezones[14];
2880
2881 #ifdef _GLIBCXX_USE_WCHAR_T
2882 template<>
2883 const wchar_t*
2884 __timepunct_cache<wchar_t>::_S_timezones[14];
2885 #endif
2886
2887 // Generic.
2888 template<typename _CharT>
2889 const _CharT* __timepunct_cache<_CharT>::_S_timezones[14];
2890
2891 template<typename _CharT>
2892 class __timepunct : public locale::facet
2893 {
2894 public:
2895 // Types:
2896 typedef _CharT __char_type;
2897 typedef basic_string<_CharT> __string_type;
2898 typedef __timepunct_cache<_CharT> __cache_type;
2899
2900 protected:
2901 __cache_type* _M_data;
2902 __c_locale _M_c_locale_timepunct;
2903 const char* _M_name_timepunct;
2904
2905 public:
2906 /// Numpunct facet id.
2907 static locale::id id;
2908
2909 explicit
2910 __timepunct(size_t __refs = 0);
2911
2912 explicit
2913 __timepunct(__cache_type* __cache, size_t __refs = 0);
2914
2915 /**
2916 * @brief Internal constructor. Not for general use.
2917 *
2918 * This is a constructor for use by the library itself to set up new
2919 * locales.
2920 *
2921 * @param cloc The "C" locale.
2922 * @param s The name of a locale.
2923 * @param refs Passed to the base facet class.
2924 */
2925 explicit
2926 __timepunct(__c_locale __cloc, const char* __s, size_t __refs = 0);
2927
2928 // FIXME: for error checking purposes _M_put should return the return
2929 // value of strftime/wcsftime.
2930 void
2931 _M_put(_CharT* __s, size_t __maxlen, const _CharT* __format,
2932 const tm* __tm) const;
2933
2934 void
2935 _M_date_formats(const _CharT** __date) const
2936 {
2937 // Always have default first.
2938 __date[0] = _M_data->_M_date_format;
2939 __date[1] = _M_data->_M_date_era_format;
2940 }
2941
2942 void
2943 _M_time_formats(const _CharT** __time) const
2944 {
2945 // Always have default first.
2946 __time[0] = _M_data->_M_time_format;
2947 __time[1] = _M_data->_M_time_era_format;
2948 }
2949
2950 void
2951 _M_date_time_formats(const _CharT** __dt) const
2952 {
2953 // Always have default first.
2954 __dt[0] = _M_data->_M_date_time_format;
2955 __dt[1] = _M_data->_M_date_time_era_format;
2956 }
2957
2958 void
2959 _M_am_pm_format(const _CharT* __ampm) const
2960 { __ampm = _M_data->_M_am_pm_format; }
2961
2962 void
2963 _M_am_pm(const _CharT** __ampm) const
2964 {
2965 __ampm[0] = _M_data->_M_am;
2966 __ampm[1] = _M_data->_M_pm;
2967 }
2968
2969 void
2970 _M_days(const _CharT** __days) const
2971 {
2972 __days[0] = _M_data->_M_day1;
2973 __days[1] = _M_data->_M_day2;
2974 __days[2] = _M_data->_M_day3;
2975 __days[3] = _M_data->_M_day4;
2976 __days[4] = _M_data->_M_day5;
2977 __days[5] = _M_data->_M_day6;
2978 __days[6] = _M_data->_M_day7;
2979 }
2980
2981 void
2982 _M_days_abbreviated(const _CharT** __days) const
2983 {
2984 __days[0] = _M_data->_M_aday1;
2985 __days[1] = _M_data->_M_aday2;
2986 __days[2] = _M_data->_M_aday3;
2987 __days[3] = _M_data->_M_aday4;
2988 __days[4] = _M_data->_M_aday5;
2989 __days[5] = _M_data->_M_aday6;
2990 __days[6] = _M_data->_M_aday7;
2991 }
2992
2993 void
2994 _M_months(const _CharT** __months) const
2995 {
2996 __months[0] = _M_data->_M_month01;
2997 __months[1] = _M_data->_M_month02;
2998 __months[2] = _M_data->_M_month03;
2999 __months[3] = _M_data->_M_month04;
3000 __months[4] = _M_data->_M_month05;
3001 __months[5] = _M_data->_M_month06;
3002 __months[6] = _M_data->_M_month07;
3003 __months[7] = _M_data->_M_month08;
3004 __months[8] = _M_data->_M_month09;
3005 __months[9] = _M_data->_M_month10;
3006 __months[10] = _M_data->_M_month11;
3007 __months[11] = _M_data->_M_month12;
3008 }
3009
3010 void
3011 _M_months_abbreviated(const _CharT** __months) const
3012 {
3013 __months[0] = _M_data->_M_amonth01;
3014 __months[1] = _M_data->_M_amonth02;
3015 __months[2] = _M_data->_M_amonth03;
3016 __months[3] = _M_data->_M_amonth04;
3017 __months[4] = _M_data->_M_amonth05;
3018 __months[5] = _M_data->_M_amonth06;
3019 __months[6] = _M_data->_M_amonth07;
3020 __months[7] = _M_data->_M_amonth08;
3021 __months[8] = _M_data->_M_amonth09;
3022 __months[9] = _M_data->_M_amonth10;
3023 __months[10] = _M_data->_M_amonth11;
3024 __months[11] = _M_data->_M_amonth12;
3025 }
3026
3027 protected:
3028 virtual
3029 ~__timepunct();
3030
3031 // For use at construction time only.
3032 void
3033 _M_initialize_timepunct(__c_locale __cloc = NULL);
3034 };
3035
3036 template<typename _CharT>
3037 locale::id __timepunct<_CharT>::id;
3038
3039 // Specializations.
3040 template<>
3041 void
3042 __timepunct<char>::_M_initialize_timepunct(__c_locale __cloc);
3043
3044 template<>
3045 void
3046 __timepunct<char>::_M_put(char*, size_t, const char*, const tm*) const;
3047
3048 #ifdef _GLIBCXX_USE_WCHAR_T
3049 template<>
3050 void
3051 __timepunct<wchar_t>::_M_initialize_timepunct(__c_locale __cloc);
3052
3053 template<>
3054 void
3055 __timepunct<wchar_t>::_M_put(wchar_t*, size_t, const wchar_t*,
3056 const tm*) const;
3057 #endif
3058
3059 _GLIBCXX_END_NAMESPACE
3060
3061 // Include host and configuration specific timepunct functions.
3062 #include <bits/time_members.h>
3063
3064 _GLIBCXX_BEGIN_NAMESPACE(std)
3065
3066 /**
3067 * @brief Facet for parsing dates and times.
3068 *
3069 * This facet encapsulates the code to parse and return a date or
3070 * time from a string. It is used by the istream numeric
3071 * extraction operators.
3072 *
3073 * The time_get template uses protected virtual functions to provide the
3074 * actual results. The public accessors forward the call to the virtual
3075 * functions. These virtual functions are hooks for developers to
3076 * implement the behavior they require from the time_get facet.
3077 */
3078 template<typename _CharT, typename _InIter>
3079 class time_get : public locale::facet, public time_base
3080 {
3081 public:
3082 // Types:
3083 //@{
3084 /// Public typedefs
3085 typedef _CharT char_type;
3086 typedef _InIter iter_type;
3087 //@}
3088 typedef basic_string<_CharT> __string_type;
3089
3090 /// Numpunct facet id.
3091 static locale::id id;
3092
3093 /**
3094 * @brief Constructor performs initialization.
3095 *
3096 * This is the constructor provided by the standard.
3097 *
3098 * @param refs Passed to the base facet class.
3099 */
3100 explicit
3101 time_get(size_t __refs = 0)
3102 : facet (__refs) { }
3103
3104 /**
3105 * @brief Return preferred order of month, day, and year.
3106 *
3107 * This function returns an enum from timebase::dateorder giving the
3108 * preferred ordering if the format "x" given to time_put::put() only
3109 * uses month, day, and year. If the format "x" for the associated
3110 * locale uses other fields, this function returns
3111 * timebase::dateorder::noorder.
3112 *
3113 * NOTE: The library always returns noorder at the moment.
3114 *
3115 * @return A member of timebase::dateorder.
3116 */
3117 dateorder
3118 date_order() const
3119 { return this->do_date_order(); }
3120
3121 /**
3122 * @brief Parse input time string.
3123 *
3124 * This function parses a time according to the format "x" and puts the
3125 * results into a user-supplied struct tm. The result is returned by
3126 * calling time_get::do_get_time().
3127 *
3128 * If there is a valid time string according to format "x", @a tm will
3129 * be filled in accordingly and the returned iterator will point to the
3130 * first character beyond the time string. If an error occurs before
3131 * the end, err |= ios_base::failbit. If parsing reads all the
3132 * characters, err |= ios_base::eofbit.
3133 *
3134 * @param beg Start of string to parse.
3135 * @param end End of string to parse.
3136 * @param io Source of the locale.
3137 * @param err Error flags to set.
3138 * @param tm Pointer to struct tm to fill in.
3139 * @return Iterator to first char beyond time string.
3140 */
3141 iter_type
3142 get_time(iter_type __beg, iter_type __end, ios_base& __io,
3143 ios_base::iostate& __err, tm* __tm) const
3144 { return this->do_get_time(__beg, __end, __io, __err, __tm); }
3145
3146 /**
3147 * @brief Parse input date string.
3148 *
3149 * This function parses a date according to the format "X" and puts the
3150 * results into a user-supplied struct tm. The result is returned by
3151 * calling time_get::do_get_date().
3152 *
3153 * If there is a valid date string according to format "X", @a tm will
3154 * be filled in accordingly and the returned iterator will point to the
3155 * first character beyond the date string. If an error occurs before
3156 * the end, err |= ios_base::failbit. If parsing reads all the
3157 * characters, err |= ios_base::eofbit.
3158 *
3159 * @param beg Start of string to parse.
3160 * @param end End of string to parse.
3161 * @param io Source of the locale.
3162 * @param err Error flags to set.
3163 * @param tm Pointer to struct tm to fill in.
3164 * @return Iterator to first char beyond date string.
3165 */
3166 iter_type
3167 get_date(iter_type __beg, iter_type __end, ios_base& __io,
3168 ios_base::iostate& __err, tm* __tm) const
3169 { return this->do_get_date(__beg, __end, __io, __err, __tm); }
3170
3171 /**
3172 * @brief Parse input weekday string.
3173 *
3174 * This function parses a weekday name and puts the results into a
3175 * user-supplied struct tm. The result is returned by calling
3176 * time_get::do_get_weekday().
3177 *
3178 * Parsing starts by parsing an abbreviated weekday name. If a valid
3179 * abbreviation is followed by a character that would lead to the full
3180 * weekday name, parsing continues until the full name is found or an
3181 * error occurs. Otherwise parsing finishes at the end of the
3182 * abbreviated name.
3183 *
3184 * If an error occurs before the end, err |= ios_base::failbit. If
3185 * parsing reads all the characters, err |= ios_base::eofbit.
3186 *
3187 * @param beg Start of string to parse.
3188 * @param end End of string to parse.
3189 * @param io Source of the locale.
3190 * @param err Error flags to set.
3191 * @param tm Pointer to struct tm to fill in.
3192 * @return Iterator to first char beyond weekday name.
3193 */
3194 iter_type
3195 get_weekday(iter_type __beg, iter_type __end, ios_base& __io,
3196 ios_base::iostate& __err, tm* __tm) const
3197 { return this->do_get_weekday(__beg, __end, __io, __err, __tm); }
3198
3199 /**
3200 * @brief Parse input month string.
3201 *
3202 * This function parses a month name and puts the results into a
3203 * user-supplied struct tm. The result is returned by calling
3204 * time_get::do_get_monthname().
3205 *
3206 * Parsing starts by parsing an abbreviated month name. If a valid
3207 * abbreviation is followed by a character that would lead to the full
3208 * month name, parsing continues until the full name is found or an
3209 * error occurs. Otherwise parsing finishes at the end of the
3210 * abbreviated name.
3211 *
3212 * If an error occurs before the end, err |= ios_base::failbit. If
3213 * parsing reads all the characters, err |=
3214 * ios_base::eofbit.
3215 *
3216 * @param beg Start of string to parse.
3217 * @param end End of string to parse.
3218 * @param io Source of the locale.
3219 * @param err Error flags to set.
3220 * @param tm Pointer to struct tm to fill in.
3221 * @return Iterator to first char beyond month name.
3222 */
3223 iter_type
3224 get_monthname(iter_type __beg, iter_type __end, ios_base& __io,
3225 ios_base::iostate& __err, tm* __tm) const
3226 { return this->do_get_monthname(__beg, __end, __io, __err, __tm); }
3227
3228 /**
3229 * @brief Parse input year string.
3230 *
3231 * This function reads up to 4 characters to parse a year string and
3232 * puts the results into a user-supplied struct tm. The result is
3233 * returned by calling time_get::do_get_year().
3234 *
3235 * 4 consecutive digits are interpreted as a full year. If there are
3236 * exactly 2 consecutive digits, the library interprets this as the
3237 * number of years since 1900.
3238 *
3239 * If an error occurs before the end, err |= ios_base::failbit. If
3240 * parsing reads all the characters, err |= ios_base::eofbit.
3241 *
3242 * @param beg Start of string to parse.
3243 * @param end End of string to parse.
3244 * @param io Source of the locale.
3245 * @param err Error flags to set.
3246 * @param tm Pointer to struct tm to fill in.
3247 * @return Iterator to first char beyond year.
3248 */
3249 iter_type
3250 get_year(iter_type __beg, iter_type __end, ios_base& __io,
3251 ios_base::iostate& __err, tm* __tm) const
3252 { return this->do_get_year(__beg, __end, __io, __err, __tm); }
3253
3254 protected:
3255 /// Destructor.
3256 virtual
3257 ~time_get() { }
3258
3259 /**
3260 * @brief Return preferred order of month, day, and year.
3261 *
3262 * This function returns an enum from timebase::dateorder giving the
3263 * preferred ordering if the format "x" given to time_put::put() only
3264 * uses month, day, and year. This function is a hook for derived
3265 * classes to change the value returned.
3266 *
3267 * @return A member of timebase::dateorder.
3268 */
3269 virtual dateorder
3270 do_date_order() const;
3271
3272 /**
3273 * @brief Parse input time string.
3274 *
3275 * This function parses a time according to the format "x" and puts the
3276 * results into a user-supplied struct tm. This function is a hook for
3277 * derived classes to change the value returned. @see get_time() for
3278 * details.
3279 *
3280 * @param beg Start of string to parse.
3281 * @param end End of string to parse.
3282 * @param io Source of the locale.
3283 * @param err Error flags to set.
3284 * @param tm Pointer to struct tm to fill in.
3285 * @return Iterator to first char beyond time string.
3286 */
3287 virtual iter_type
3288 do_get_time(iter_type __beg, iter_type __end, ios_base& __io,
3289 ios_base::iostate& __err, tm* __tm) const;
3290
3291 /**
3292 * @brief Parse input date string.
3293 *
3294 * This function parses a date according to the format "X" and puts the
3295 * results into a user-supplied struct tm. This function is a hook for
3296 * derived classes to change the value returned. @see get_date() for
3297 * details.
3298 *
3299 * @param beg Start of string to parse.
3300 * @param end End of string to parse.
3301 * @param io Source of the locale.
3302 * @param err Error flags to set.
3303 * @param tm Pointer to struct tm to fill in.
3304 * @return Iterator to first char beyond date string.
3305 */
3306 virtual iter_type
3307 do_get_date(iter_type __beg, iter_type __end, ios_base& __io,
3308 ios_base::iostate& __err, tm* __tm) const;
3309
3310 /**
3311 * @brief Parse input weekday string.
3312 *
3313 * This function parses a weekday name and puts the results into a
3314 * user-supplied struct tm. This function is a hook for derived
3315 * classes to change the value returned. @see get_weekday() for
3316 * details.
3317 *
3318 * @param beg Start of string to parse.
3319 * @param end End of string to parse.
3320 * @param io Source of the locale.
3321 * @param err Error flags to set.
3322 * @param tm Pointer to struct tm to fill in.
3323 * @return Iterator to first char beyond weekday name.
3324 */
3325 virtual iter_type
3326 do_get_weekday(iter_type __beg, iter_type __end, ios_base&,
3327 ios_base::iostate& __err, tm* __tm) const;
3328
3329 /**
3330 * @brief Parse input month string.
3331 *
3332 * This function parses a month name and puts the results into a
3333 * user-supplied struct tm. This function is a hook for derived
3334 * classes to change the value returned. @see get_monthname() for
3335 * details.
3336 *
3337 * @param beg Start of string to parse.
3338 * @param end End of string to parse.
3339 * @param io Source of the locale.
3340 * @param err Error flags to set.
3341 * @param tm Pointer to struct tm to fill in.
3342 * @return Iterator to first char beyond month name.
3343 */
3344 virtual iter_type
3345 do_get_monthname(iter_type __beg, iter_type __end, ios_base&,
3346 ios_base::iostate& __err, tm* __tm) const;
3347
3348 /**
3349 * @brief Parse input year string.
3350 *
3351 * This function reads up to 4 characters to parse a year string and
3352 * puts the results into a user-supplied struct tm. This function is a
3353 * hook for derived classes to change the value returned. @see
3354 * get_year() for details.
3355 *
3356 * @param beg Start of string to parse.
3357 * @param end End of string to parse.
3358 * @param io Source of the locale.
3359 * @param err Error flags to set.
3360 * @param tm Pointer to struct tm to fill in.
3361 * @return Iterator to first char beyond year.
3362 */
3363 virtual iter_type
3364 do_get_year(iter_type __beg, iter_type __end, ios_base& __io,
3365 ios_base::iostate& __err, tm* __tm) const;
3366
3367 // Extract numeric component of length __len.
3368 iter_type
3369 _M_extract_num(iter_type __beg, iter_type __end, int& __member,
3370 int __min, int __max, size_t __len,
3371 ios_base& __io, ios_base::iostate& __err) const;
3372
3373 // Extract day or month name, or any unique array of string
3374 // literals in a const _CharT* array.
3375 iter_type
3376 _M_extract_name(iter_type __beg, iter_type __end, int& __member,
3377 const _CharT** __names, size_t __indexlen,
3378 ios_base& __io, ios_base::iostate& __err) const;
3379
3380 // Extract on a component-by-component basis, via __format argument.
3381 iter_type
3382 _M_extract_via_format(iter_type __beg, iter_type __end, ios_base& __io,
3383 ios_base::iostate& __err, tm* __tm,
3384 const _CharT* __format) const;
3385 };
3386
3387 template<typename _CharT, typename _InIter>
3388 locale::id time_get<_CharT, _InIter>::id;
3389
3390 /// @brief class time_get_byname [22.2.5.2].
3391 template<typename _CharT, typename _InIter>
3392 class time_get_byname : public time_get<_CharT, _InIter>
3393 {
3394 public:
3395 // Types:
3396 typedef _CharT char_type;
3397 typedef _InIter iter_type;
3398
3399 explicit
3400 time_get_byname(const char*, size_t __refs = 0)
3401 : time_get<_CharT, _InIter>(__refs) { }
3402
3403 protected:
3404 virtual
3405 ~time_get_byname() { }
3406 };
3407
3408 /**
3409 * @brief Facet for outputting dates and times.
3410 *
3411 * This facet encapsulates the code to format and output dates and times
3412 * according to formats used by strftime().
3413 *
3414 * The time_put template uses protected virtual functions to provide the
3415 * actual results. The public accessors forward the call to the virtual
3416 * functions. These virtual functions are hooks for developers to
3417 * implement the behavior they require from the time_put facet.
3418 */
3419 template<typename _CharT, typename _OutIter>
3420 class time_put : public locale::facet
3421 {
3422 public:
3423 // Types:
3424 //@{
3425 /// Public typedefs
3426 typedef _CharT char_type;
3427 typedef _OutIter iter_type;
3428 //@}
3429
3430 /// Numpunct facet id.
3431 static locale::id id;
3432
3433 /**
3434 * @brief Constructor performs initialization.
3435 *
3436 * This is the constructor provided by the standard.
3437 *
3438 * @param refs Passed to the base facet class.
3439 */
3440 explicit
3441 time_put(size_t __refs = 0)
3442 : facet(__refs) { }
3443
3444 /**
3445 * @brief Format and output a time or date.
3446 *
3447 * This function formats the data in struct tm according to the
3448 * provided format string. The format string is interpreted as by
3449 * strftime().
3450 *
3451 * @param s The stream to write to.
3452 * @param io Source of locale.
3453 * @param fill char_type to use for padding.
3454 * @param tm Struct tm with date and time info to format.
3455 * @param beg Start of format string.
3456 * @param end End of format string.
3457 * @return Iterator after writing.
3458 */
3459 iter_type
3460 put(iter_type __s, ios_base& __io, char_type __fill, const tm* __tm,
3461 const _CharT* __beg, const _CharT* __end) const;
3462
3463 /**
3464 * @brief Format and output a time or date.
3465 *
3466 * This function formats the data in struct tm according to the
3467 * provided format char and optional modifier. The format and modifier
3468 * are interpreted as by strftime(). It does so by returning
3469 * time_put::do_put().
3470 *
3471 * @param s The stream to write to.
3472 * @param io Source of locale.
3473 * @param fill char_type to use for padding.
3474 * @param tm Struct tm with date and time info to format.
3475 * @param format Format char.
3476 * @param mod Optional modifier char.
3477 * @return Iterator after writing.
3478 */
3479 iter_type
3480 put(iter_type __s, ios_base& __io, char_type __fill,
3481 const tm* __tm, char __format, char __mod = 0) const
3482 { return this->do_put(__s, __io, __fill, __tm, __format, __mod); }
3483
3484 protected:
3485 /// Destructor.
3486 virtual
3487 ~time_put()
3488 { }
3489
3490 /**
3491 * @brief Format and output a time or date.
3492 *
3493 * This function formats the data in struct tm according to the
3494 * provided format char and optional modifier. This function is a hook
3495 * for derived classes to change the value returned. @see put() for
3496 * more details.
3497 *
3498 * @param s The stream to write to.
3499 * @param io Source of locale.
3500 * @param fill char_type to use for padding.
3501 * @param tm Struct tm with date and time info to format.
3502 * @param format Format char.
3503 * @param mod Optional modifier char.
3504 * @return Iterator after writing.
3505 */
3506 virtual iter_type
3507 do_put(iter_type __s, ios_base& __io, char_type __fill, const tm* __tm,
3508 char __format, char __mod) const;
3509 };
3510
3511 template<typename _CharT, typename _OutIter>
3512 locale::id time_put<_CharT, _OutIter>::id;
3513
3514 /// @brief class time_put_byname [22.2.5.4].
3515 template<typename _CharT, typename _OutIter>
3516 class time_put_byname : public time_put<_CharT, _OutIter>
3517 {
3518 public:
3519 // Types:
3520 typedef _CharT char_type;
3521 typedef _OutIter iter_type;
3522
3523 explicit
3524 time_put_byname(const char*, size_t __refs = 0)
3525 : time_put<_CharT, _OutIter>(__refs)
3526 { };
3527
3528 protected:
3529 virtual
3530 ~time_put_byname() { }
3531 };
3532
3533
3534 /**
3535 * @brief Money format ordering data.
3536 *
3537 * This class contains an ordered array of 4 fields to represent the
3538 * pattern for formatting a money amount. Each field may contain one entry
3539 * from the part enum. symbol, sign, and value must be present and the
3540 * remaining field must contain either none or space. @see
3541 * moneypunct::pos_format() and moneypunct::neg_format() for details of how
3542 * these fields are interpreted.
3543 */
3544 class money_base
3545 {
3546 public:
3547 enum part { none, space, symbol, sign, value };
3548 struct pattern { char field[4]; };
3549
3550 static const pattern _S_default_pattern;
3551
3552 enum
3553 {
3554 _S_minus,
3555 _S_zero,
3556 _S_end = 11
3557 };
3558
3559 // String literal of acceptable (narrow) input/output, for
3560 // money_get/money_put. "-0123456789"
3561 static const char* _S_atoms;
3562
3563 // Construct and return valid pattern consisting of some combination of:
3564 // space none symbol sign value
3565 static pattern
3566 _S_construct_pattern(char __precedes, char __space, char __posn);
3567 };
3568
3569 template<typename _CharT, bool _Intl>
3570 struct __moneypunct_cache : public locale::facet
3571 {
3572 const char* _M_grouping;
3573 size_t _M_grouping_size;
3574 bool _M_use_grouping;
3575 _CharT _M_decimal_point;
3576 _CharT _M_thousands_sep;
3577 const _CharT* _M_curr_symbol;
3578 size_t _M_curr_symbol_size;
3579 const _CharT* _M_positive_sign;
3580 size_t _M_positive_sign_size;
3581 const _CharT* _M_negative_sign;
3582 size_t _M_negative_sign_size;
3583 int _M_frac_digits;
3584 money_base::pattern _M_pos_format;
3585 money_base::pattern _M_neg_format;
3586
3587 // A list of valid numeric literals for input and output: in the standard
3588 // "C" locale, this is "-0123456789". This array contains the chars after
3589 // having been passed through the current locale's ctype<_CharT>.widen().
3590 _CharT _M_atoms[money_base::_S_end];
3591
3592 bool _M_allocated;
3593
3594 __moneypunct_cache(size_t __refs = 0) : facet(__refs),
3595 _M_grouping(NULL), _M_grouping_size(0), _M_use_grouping(false),
3596 _M_decimal_point(_CharT()), _M_thousands_sep(_CharT()),
3597 _M_curr_symbol(NULL), _M_curr_symbol_size(0),
3598 _M_positive_sign(NULL), _M_positive_sign_size(0),
3599 _M_negative_sign(NULL), _M_negative_sign_size(0),
3600 _M_frac_digits(0),
3601 _M_pos_format(money_base::pattern()),
3602 _M_neg_format(money_base::pattern()), _M_allocated(false)
3603 { }
3604
3605 ~__moneypunct_cache();
3606
3607 void
3608 _M_cache(const locale& __loc);
3609
3610 private:
3611 __moneypunct_cache&
3612 operator=(const __moneypunct_cache&);
3613
3614 explicit
3615 __moneypunct_cache(const __moneypunct_cache&);
3616 };
3617
3618 template<typename _CharT, bool _Intl>
3619 __moneypunct_cache<_CharT, _Intl>::~__moneypunct_cache()
3620 {
3621 if (_M_allocated)
3622 {
3623 delete [] _M_grouping;
3624 delete [] _M_curr_symbol;
3625 delete [] _M_positive_sign;
3626 delete [] _M_negative_sign;
3627 }
3628 }
3629
3630 /**
3631 * @brief Facet for formatting data for money amounts.
3632 *
3633 * This facet encapsulates the punctuation, grouping and other formatting
3634 * features of money amount string representations.
3635 */
3636 template<typename _CharT, bool _Intl>
3637 class moneypunct : public locale::facet, public money_base
3638 {
3639 public:
3640 // Types:
3641 //@{
3642 /// Public typedefs
3643 typedef _CharT char_type;
3644 typedef basic_string<_CharT> string_type;
3645 //@}
3646 typedef __moneypunct_cache<_CharT, _Intl> __cache_type;
3647
3648 private:
3649 __cache_type* _M_data;
3650
3651 public:
3652 /// This value is provided by the standard, but no reason for its
3653 /// existence.
3654 static const bool intl = _Intl;
3655 /// Numpunct facet id.
3656 static locale::id id;
3657
3658 /**
3659 * @brief Constructor performs initialization.
3660 *
3661 * This is the constructor provided by the standard.
3662 *
3663 * @param refs Passed to the base facet class.
3664 */
3665 explicit
3666 moneypunct(size_t __refs = 0) : facet(__refs), _M_data(NULL)
3667 { _M_initialize_moneypunct(); }
3668
3669 /**
3670 * @brief Constructor performs initialization.
3671 *
3672 * This is an internal constructor.
3673 *
3674 * @param cache Cache for optimization.
3675 * @param refs Passed to the base facet class.
3676 */
3677 explicit
3678 moneypunct(__cache_type* __cache, size_t __refs = 0)
3679 : facet(__refs), _M_data(__cache)
3680 { _M_initialize_moneypunct(); }
3681
3682 /**
3683 * @brief Internal constructor. Not for general use.
3684 *
3685 * This is a constructor for use by the library itself to set up new
3686 * locales.
3687 *
3688 * @param cloc The "C" locale.
3689 * @param s The name of a locale.
3690 * @param refs Passed to the base facet class.
3691 */
3692 explicit
3693 moneypunct(__c_locale __cloc, const char* __s, size_t __refs = 0)
3694 : facet(__refs), _M_data(NULL)
3695 { _M_initialize_moneypunct(__cloc, __s); }
3696
3697 /**
3698 * @brief Return decimal point character.
3699 *
3700 * This function returns a char_type to use as a decimal point. It
3701 * does so by returning returning
3702 * moneypunct<char_type>::do_decimal_point().
3703 *
3704 * @return @a char_type representing a decimal point.
3705 */
3706 char_type
3707 decimal_point() const
3708 { return this->do_decimal_point(); }
3709
3710 /**
3711 * @brief Return thousands separator character.
3712 *
3713 * This function returns a char_type to use as a thousands
3714 * separator. It does so by returning returning
3715 * moneypunct<char_type>::do_thousands_sep().
3716 *
3717 * @return char_type representing a thousands separator.
3718 */
3719 char_type
3720 thousands_sep() const
3721 { return this->do_thousands_sep(); }
3722
3723 /**
3724 * @brief Return grouping specification.
3725 *
3726 * This function returns a string representing groupings for the
3727 * integer part of an amount. Groupings indicate where thousands
3728 * separators should be inserted.
3729 *
3730 * Each char in the return string is interpret as an integer rather
3731 * than a character. These numbers represent the number of digits in a
3732 * group. The first char in the string represents the number of digits
3733 * in the least significant group. If a char is negative, it indicates
3734 * an unlimited number of digits for the group. If more chars from the
3735 * string are required to group a number, the last char is used
3736 * repeatedly.
3737 *
3738 * For example, if the grouping() returns "\003\002" and is applied to
3739 * the number 123456789, this corresponds to 12,34,56,789. Note that
3740 * if the string was "32", this would put more than 50 digits into the
3741 * least significant group if the character set is ASCII.
3742 *
3743 * The string is returned by calling
3744 * moneypunct<char_type>::do_grouping().
3745 *
3746 * @return string representing grouping specification.
3747 */
3748 string
3749 grouping() const
3750 { return this->do_grouping(); }
3751
3752 /**
3753 * @brief Return currency symbol string.
3754 *
3755 * This function returns a string_type to use as a currency symbol. It
3756 * does so by returning returning
3757 * moneypunct<char_type>::do_curr_symbol().
3758 *
3759 * @return @a string_type representing a currency symbol.
3760 */
3761 string_type
3762 curr_symbol() const
3763 { return this->do_curr_symbol(); }
3764
3765 /**
3766 * @brief Return positive sign string.
3767 *
3768 * This function returns a string_type to use as a sign for positive
3769 * amounts. It does so by returning returning
3770 * moneypunct<char_type>::do_positive_sign().
3771 *
3772 * If the return value contains more than one character, the first
3773 * character appears in the position indicated by pos_format() and the
3774 * remainder appear at the end of the formatted string.
3775 *
3776 * @return @a string_type representing a positive sign.
3777 */
3778 string_type
3779 positive_sign() const
3780 { return this->do_positive_sign(); }
3781
3782 /**
3783 * @brief Return negative sign string.
3784 *
3785 * This function returns a string_type to use as a sign for negative
3786 * amounts. It does so by returning returning
3787 * moneypunct<char_type>::do_negative_sign().
3788 *
3789 * If the return value contains more than one character, the first
3790 * character appears in the position indicated by neg_format() and the
3791 * remainder appear at the end of the formatted string.
3792 *
3793 * @return @a string_type representing a negative sign.
3794 */
3795 string_type
3796 negative_sign() const
3797 { return this->do_negative_sign(); }
3798
3799 /**
3800 * @brief Return number of digits in fraction.
3801 *
3802 * This function returns the exact number of digits that make up the
3803 * fractional part of a money amount. It does so by returning
3804 * returning moneypunct<char_type>::do_frac_digits().
3805 *
3806 * The fractional part of a money amount is optional. But if it is
3807 * present, there must be frac_digits() digits.
3808 *
3809 * @return Number of digits in amount fraction.
3810 */
3811 int
3812 frac_digits() const
3813 { return this->do_frac_digits(); }
3814
3815 //@{
3816 /**
3817 * @brief Return pattern for money values.
3818 *
3819 * This function returns a pattern describing the formatting of a
3820 * positive or negative valued money amount. It does so by returning
3821 * returning moneypunct<char_type>::do_pos_format() or
3822 * moneypunct<char_type>::do_neg_format().
3823 *
3824 * The pattern has 4 fields describing the ordering of symbol, sign,
3825 * value, and none or space. There must be one of each in the pattern.
3826 * The none and space enums may not appear in the first field and space
3827 * may not appear in the final field.
3828 *
3829 * The parts of a money string must appear in the order indicated by
3830 * the fields of the pattern. The symbol field indicates that the
3831 * value of curr_symbol() may be present. The sign field indicates
3832 * that the value of positive_sign() or negative_sign() must be
3833 * present. The value field indicates that the absolute value of the
3834 * money amount is present. none indicates 0 or more whitespace
3835 * characters, except at the end, where it permits no whitespace.
3836 * space indicates that 1 or more whitespace characters must be
3837 * present.
3838 *
3839 * For example, for the US locale and pos_format() pattern
3840 * {symbol,sign,value,none}, curr_symbol() == '$' positive_sign() ==
3841 * '+', and value 10.01, and options set to force the symbol, the
3842 * corresponding string is "$+10.01".
3843 *
3844 * @return Pattern for money values.
3845 */
3846 pattern
3847 pos_format() const
3848 { return this->do_pos_format(); }
3849
3850 pattern
3851 neg_format() const
3852 { return this->do_neg_format(); }
3853 //@}
3854
3855 protected:
3856 /// Destructor.
3857 virtual
3858 ~moneypunct();
3859
3860 /**
3861 * @brief Return decimal point character.
3862 *
3863 * Returns a char_type to use as a decimal point. This function is a
3864 * hook for derived classes to change the value returned.
3865 *
3866 * @return @a char_type representing a decimal point.
3867 */
3868 virtual char_type
3869 do_decimal_point() const
3870 { return _M_data->_M_decimal_point; }
3871
3872 /**
3873 * @brief Return thousands separator character.
3874 *
3875 * Returns a char_type to use as a thousands separator. This function
3876 * is a hook for derived classes to change the value returned.
3877 *
3878 * @return @a char_type representing a thousands separator.
3879 */
3880 virtual char_type
3881 do_thousands_sep() const
3882 { return _M_data->_M_thousands_sep; }
3883
3884 /**
3885 * @brief Return grouping specification.
3886 *
3887 * Returns a string representing groupings for the integer part of a
3888 * number. This function is a hook for derived classes to change the
3889 * value returned. @see grouping() for details.
3890 *
3891 * @return String representing grouping specification.
3892 */
3893 virtual string
3894 do_grouping() const
3895 { return _M_data->_M_grouping; }
3896
3897 /**
3898 * @brief Return currency symbol string.
3899 *
3900 * This function returns a string_type to use as a currency symbol.
3901 * This function is a hook for derived classes to change the value
3902 * returned. @see curr_symbol() for details.
3903 *
3904 * @return @a string_type representing a currency symbol.
3905 */
3906 virtual string_type
3907 do_curr_symbol() const
3908 { return _M_data->_M_curr_symbol; }
3909
3910 /**
3911 * @brief Return positive sign string.
3912 *
3913 * This function returns a string_type to use as a sign for positive
3914 * amounts. This function is a hook for derived classes to change the
3915 * value returned. @see positive_sign() for details.
3916 *
3917 * @return @a string_type representing a positive sign.
3918 */
3919 virtual string_type
3920 do_positive_sign() const
3921 { return _M_data->_M_positive_sign; }
3922
3923 /**
3924 * @brief Return negative sign string.
3925 *
3926 * This function returns a string_type to use as a sign for negative
3927 * amounts. This function is a hook for derived classes to change the
3928 * value returned. @see negative_sign() for details.
3929 *
3930 * @return @a string_type representing a negative sign.
3931 */
3932 virtual string_type
3933 do_negative_sign() const
3934 { return _M_data->_M_negative_sign; }
3935
3936 /**
3937 * @brief Return number of digits in fraction.
3938 *
3939 * This function returns the exact number of digits that make up the
3940 * fractional part of a money amount. This function is a hook for
3941 * derived classes to change the value returned. @see frac_digits()
3942 * for details.
3943 *
3944 * @return Number of digits in amount fraction.
3945 */
3946 virtual int
3947 do_frac_digits() const
3948 { return _M_data->_M_frac_digits; }
3949
3950 /**
3951 * @brief Return pattern for money values.
3952 *
3953 * This function returns a pattern describing the formatting of a
3954 * positive valued money amount. This function is a hook for derived
3955 * classes to change the value returned. @see pos_format() for
3956 * details.
3957 *
3958 * @return Pattern for money values.
3959 */
3960 virtual pattern
3961 do_pos_format() const
3962 { return _M_data->_M_pos_format; }
3963
3964 /**
3965 * @brief Return pattern for money values.
3966 *
3967 * This function returns a pattern describing the formatting of a
3968 * negative valued money amount. This function is a hook for derived
3969 * classes to change the value returned. @see neg_format() for
3970 * details.
3971 *
3972 * @return Pattern for money values.
3973 */
3974 virtual pattern
3975 do_neg_format() const
3976 { return _M_data->_M_neg_format; }
3977
3978 // For use at construction time only.
3979 void
3980 _M_initialize_moneypunct(__c_locale __cloc = NULL,
3981 const char* __name = NULL);
3982 };
3983
3984 template<typename _CharT, bool _Intl>
3985 locale::id moneypunct<_CharT, _Intl>::id;
3986
3987 template<typename _CharT, bool _Intl>
3988 const bool moneypunct<_CharT, _Intl>::intl;
3989
3990 template<>
3991 moneypunct<char, true>::~moneypunct();
3992
3993 template<>
3994 moneypunct<char, false>::~moneypunct();
3995
3996 template<>
3997 void
3998 moneypunct<char, true>::_M_initialize_moneypunct(__c_locale, const char*);
3999
4000 template<>
4001 void
4002 moneypunct<char, false>::_M_initialize_moneypunct(__c_locale, const char*);
4003
4004 #ifdef _GLIBCXX_USE_WCHAR_T
4005 template<>
4006 moneypunct<wchar_t, true>::~moneypunct();
4007
4008 template<>
4009 moneypunct<wchar_t, false>::~moneypunct();
4010
4011 template<>
4012 void
4013 moneypunct<wchar_t, true>::_M_initialize_moneypunct(__c_locale,
4014 const char*);
4015
4016 template<>
4017 void
4018 moneypunct<wchar_t, false>::_M_initialize_moneypunct(__c_locale,
4019 const char*);
4020 #endif
4021
4022 /// @brief class moneypunct_byname [22.2.6.4].
4023 template<typename _CharT, bool _Intl>
4024 class moneypunct_byname : public moneypunct<_CharT, _Intl>
4025 {
4026 public:
4027 typedef _CharT char_type;
4028 typedef basic_string<_CharT> string_type;
4029
4030 static const bool intl = _Intl;
4031
4032 explicit
4033 moneypunct_byname(const char* __s, size_t __refs = 0)
4034 : moneypunct<_CharT, _Intl>(__refs)
4035 {
4036 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
4037 {
4038 __c_locale __tmp;
4039 this->_S_create_c_locale(__tmp, __s);
4040 this->_M_initialize_moneypunct(__tmp);
4041 this->_S_destroy_c_locale(__tmp);
4042 }
4043 }
4044
4045 protected:
4046 virtual
4047 ~moneypunct_byname() { }
4048 };
4049
4050 template<typename _CharT, bool _Intl>
4051 const bool moneypunct_byname<_CharT, _Intl>::intl;
4052
4053 _GLIBCXX_BEGIN_LDBL_NAMESPACE
4054 /**
4055 * @brief Facet for parsing monetary amounts.
4056 *
4057 * This facet encapsulates the code to parse and return a monetary
4058 * amount from a string.
4059 *
4060 * The money_get template uses protected virtual functions to
4061 * provide the actual results. The public accessors forward the
4062 * call to the virtual functions. These virtual functions are
4063 * hooks for developers to implement the behavior they require from
4064 * the money_get facet.
4065 */
4066 template<typename _CharT, typename _InIter>
4067 class money_get : public locale::facet
4068 {
4069 public:
4070 // Types:
4071 //@{
4072 /// Public typedefs
4073 typedef _CharT char_type;
4074 typedef _InIter iter_type;
4075 typedef basic_string<_CharT> string_type;
4076 //@}
4077
4078 /// Numpunct facet id.
4079 static locale::id id;
4080
4081 /**
4082 * @brief Constructor performs initialization.
4083 *
4084 * This is the constructor provided by the standard.
4085 *
4086 * @param refs Passed to the base facet class.
4087 */
4088 explicit
4089 money_get(size_t __refs = 0) : facet(__refs) { }
4090
4091 /**
4092 * @brief Read and parse a monetary value.
4093 *
4094 * This function reads characters from @a s, interprets them as a
4095 * monetary value according to moneypunct and ctype facets retrieved
4096 * from io.getloc(), and returns the result in @a units as an integral
4097 * value moneypunct::frac_digits() * the actual amount. For example,
4098 * the string $10.01 in a US locale would store 1001 in @a units.
4099 *
4100 * Any characters not part of a valid money amount are not consumed.
4101 *
4102 * If a money value cannot be parsed from the input stream, sets
4103 * err=(err|io.failbit). If the stream is consumed before finishing
4104 * parsing, sets err=(err|io.failbit|io.eofbit). @a units is
4105 * unchanged if parsing fails.
4106 *
4107 * This function works by returning the result of do_get().
4108 *
4109 * @param s Start of characters to parse.
4110 * @param end End of characters to parse.
4111 * @param intl Parameter to use_facet<moneypunct<CharT,intl> >.
4112 * @param io Source of facets and io state.
4113 * @param err Error field to set if parsing fails.
4114 * @param units Place to store result of parsing.
4115 * @return Iterator referencing first character beyond valid money
4116 * amount.
4117 */
4118 iter_type
4119 get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4120 ios_base::iostate& __err, long double& __units) const
4121 { return this->do_get(__s, __end, __intl, __io, __err, __units); }
4122
4123 /**
4124 * @brief Read and parse a monetary value.
4125 *
4126 * This function reads characters from @a s, interprets them as a
4127 * monetary value according to moneypunct and ctype facets retrieved
4128 * from io.getloc(), and returns the result in @a digits. For example,
4129 * the string $10.01 in a US locale would store "1001" in @a digits.
4130 *
4131 * Any characters not part of a valid money amount are not consumed.
4132 *
4133 * If a money value cannot be parsed from the input stream, sets
4134 * err=(err|io.failbit). If the stream is consumed before finishing
4135 * parsing, sets err=(err|io.failbit|io.eofbit).
4136 *
4137 * This function works by returning the result of do_get().
4138 *
4139 * @param s Start of characters to parse.
4140 * @param end End of characters to parse.
4141 * @param intl Parameter to use_facet<moneypunct<CharT,intl> >.
4142 * @param io Source of facets and io state.
4143 * @param err Error field to set if parsing fails.
4144 * @param digits Place to store result of parsing.
4145 * @return Iterator referencing first character beyond valid money
4146 * amount.
4147 */
4148 iter_type
4149 get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4150 ios_base::iostate& __err, string_type& __digits) const
4151 { return this->do_get(__s, __end, __intl, __io, __err, __digits); }
4152
4153 protected:
4154 /// Destructor.
4155 virtual
4156 ~money_get() { }
4157
4158 /**
4159 * @brief Read and parse a monetary value.
4160 *
4161 * This function reads and parses characters representing a monetary
4162 * value. This function is a hook for derived classes to change the
4163 * value returned. @see get() for details.
4164 */
4165 // XXX GLIBCXX_ABI Deprecated
4166 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
4167 virtual iter_type
4168 __do_get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4169 ios_base::iostate& __err, double& __units) const;
4170 #else
4171 virtual iter_type
4172 do_get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4173 ios_base::iostate& __err, long double& __units) const;
4174 #endif
4175
4176 /**
4177 * @brief Read and parse a monetary value.
4178 *
4179 * This function reads and parses characters representing a monetary
4180 * value. This function is a hook for derived classes to change the
4181 * value returned. @see get() for details.
4182 */
4183 virtual iter_type
4184 do_get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4185 ios_base::iostate& __err, string_type& __digits) const;
4186
4187 // XXX GLIBCXX_ABI Deprecated
4188 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
4189 virtual iter_type
4190 do_get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4191 ios_base::iostate& __err, long double& __units) const;
4192 #endif
4193
4194 template<bool _Intl>
4195 iter_type
4196 _M_extract(iter_type __s, iter_type __end, ios_base& __io,
4197 ios_base::iostate& __err, string& __digits) const;
4198 };
4199
4200 template<typename _CharT, typename _InIter>
4201 locale::id money_get<_CharT, _InIter>::id;
4202
4203 /**
4204 * @brief Facet for outputting monetary amounts.
4205 *
4206 * This facet encapsulates the code to format and output a monetary
4207 * amount.
4208 *
4209 * The money_put template uses protected virtual functions to
4210 * provide the actual results. The public accessors forward the
4211 * call to the virtual functions. These virtual functions are
4212 * hooks for developers to implement the behavior they require from
4213 * the money_put facet.
4214 */
4215 template<typename _CharT, typename _OutIter>
4216 class money_put : public locale::facet
4217 {
4218 public:
4219 //@{
4220 /// Public typedefs
4221 typedef _CharT char_type;
4222 typedef _OutIter iter_type;
4223 typedef basic_string<_CharT> string_type;
4224 //@}
4225
4226 /// Numpunct facet id.
4227 static locale::id id;
4228
4229 /**
4230 * @brief Constructor performs initialization.
4231 *
4232 * This is the constructor provided by the standard.
4233 *
4234 * @param refs Passed to the base facet class.
4235 */
4236 explicit
4237 money_put(size_t __refs = 0) : facet(__refs) { }
4238
4239 /**
4240 * @brief Format and output a monetary value.
4241 *
4242 * This function formats @a units as a monetary value according to
4243 * moneypunct and ctype facets retrieved from io.getloc(), and writes
4244 * the resulting characters to @a s. For example, the value 1001 in a
4245 * US locale would write "$10.01" to @a s.
4246 *
4247 * This function works by returning the result of do_put().
4248 *
4249 * @param s The stream to write to.
4250 * @param intl Parameter to use_facet<moneypunct<CharT,intl> >.
4251 * @param io Source of facets and io state.
4252 * @param fill char_type to use for padding.
4253 * @param units Place to store result of parsing.
4254 * @return Iterator after writing.
4255 */
4256 iter_type
4257 put(iter_type __s, bool __intl, ios_base& __io,
4258 char_type __fill, long double __units) const
4259 { return this->do_put(__s, __intl, __io, __fill, __units); }
4260
4261 /**
4262 * @brief Format and output a monetary value.
4263 *
4264 * This function formats @a digits as a monetary value according to
4265 * moneypunct and ctype facets retrieved from io.getloc(), and writes
4266 * the resulting characters to @a s. For example, the string "1001" in
4267 * a US locale would write "$10.01" to @a s.
4268 *
4269 * This function works by returning the result of do_put().
4270 *
4271 * @param s The stream to write to.
4272 * @param intl Parameter to use_facet<moneypunct<CharT,intl> >.
4273 * @param io Source of facets and io state.
4274 * @param fill char_type to use for padding.
4275 * @param units Place to store result of parsing.
4276 * @return Iterator after writing.
4277 */
4278 iter_type
4279 put(iter_type __s, bool __intl, ios_base& __io,
4280 char_type __fill, const string_type& __digits) const
4281 { return this->do_put(__s, __intl, __io, __fill, __digits); }
4282
4283 protected:
4284 /// Destructor.
4285 virtual
4286 ~money_put() { }
4287
4288 /**
4289 * @brief Format and output a monetary value.
4290 *
4291 * This function formats @a units as a monetary value according to
4292 * moneypunct and ctype facets retrieved from io.getloc(), and writes
4293 * the resulting characters to @a s. For example, the value 1001 in a
4294 * US locale would write "$10.01" to @a s.
4295 *
4296 * This function is a hook for derived classes to change the value
4297 * returned. @see put().
4298 *
4299 * @param s The stream to write to.
4300 * @param intl Parameter to use_facet<moneypunct<CharT,intl> >.
4301 * @param io Source of facets and io state.
4302 * @param fill char_type to use for padding.
4303 * @param units Place to store result of parsing.
4304 * @return Iterator after writing.
4305 */
4306 // XXX GLIBCXX_ABI Deprecated
4307 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
4308 virtual iter_type
4309 __do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
4310 double __units) const;
4311 #else
4312 virtual iter_type
4313 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
4314 long double __units) const;
4315 #endif
4316
4317 /**
4318 * @brief Format and output a monetary value.
4319 *
4320 * This function formats @a digits as a monetary value according to
4321 * moneypunct and ctype facets retrieved from io.getloc(), and writes
4322 * the resulting characters to @a s. For example, the string "1001" in
4323 * a US locale would write "$10.01" to @a s.
4324 *
4325 * This function is a hook for derived classes to change the value
4326 * returned. @see put().
4327 *
4328 * @param s The stream to write to.
4329 * @param intl Parameter to use_facet<moneypunct<CharT,intl> >.
4330 * @param io Source of facets and io state.
4331 * @param fill char_type to use for padding.
4332 * @param units Place to store result of parsing.
4333 * @return Iterator after writing.
4334 */
4335 virtual iter_type
4336 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
4337 const string_type& __digits) const;
4338
4339 // XXX GLIBCXX_ABI Deprecated
4340 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
4341 virtual iter_type
4342 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
4343 long double __units) const;
4344 #endif
4345
4346 template<bool _Intl>
4347 iter_type
4348 _M_insert(iter_type __s, ios_base& __io, char_type __fill,
4349 const string_type& __digits) const;
4350 };
4351
4352 template<typename _CharT, typename _OutIter>
4353 locale::id money_put<_CharT, _OutIter>::id;
4354
4355 _GLIBCXX_END_LDBL_NAMESPACE
4356
4357 /**
4358 * @brief Messages facet base class providing catalog typedef.
4359 */
4360 struct messages_base
4361 {
4362 typedef int catalog;
4363 };
4364
4365 /**
4366 * @brief Facet for handling message catalogs
4367 *
4368 * This facet encapsulates the code to retrieve messages from
4369 * message catalogs. The only thing defined by the standard for this facet
4370 * is the interface. All underlying functionality is
4371 * implementation-defined.
4372 *
4373 * This library currently implements 3 versions of the message facet. The
4374 * first version (gnu) is a wrapper around gettext, provided by libintl.
4375 * The second version (ieee) is a wrapper around catgets. The final
4376 * version (default) does no actual translation. These implementations are
4377 * only provided for char and wchar_t instantiations.
4378 *
4379 * The messages template uses protected virtual functions to
4380 * provide the actual results. The public accessors forward the
4381 * call to the virtual functions. These virtual functions are
4382 * hooks for developers to implement the behavior they require from
4383 * the messages facet.
4384 */
4385 template<typename _CharT>
4386 class messages : public locale::facet, public messages_base
4387 {
4388 public:
4389 // Types:
4390 //@{
4391 /// Public typedefs
4392 typedef _CharT char_type;
4393 typedef basic_string<_CharT> string_type;
4394 //@}
4395
4396 protected:
4397 // Underlying "C" library locale information saved from
4398 // initialization, needed by messages_byname as well.
4399 __c_locale _M_c_locale_messages;
4400 const char* _M_name_messages;
4401
4402 public:
4403 /// Numpunct facet id.
4404 static locale::id id;
4405
4406 /**
4407 * @brief Constructor performs initialization.
4408 *
4409 * This is the constructor provided by the standard.
4410 *
4411 * @param refs Passed to the base facet class.
4412 */
4413 explicit
4414 messages(size_t __refs = 0);
4415
4416 // Non-standard.
4417 /**
4418 * @brief Internal constructor. Not for general use.
4419 *
4420 * This is a constructor for use by the library itself to set up new
4421 * locales.
4422 *
4423 * @param cloc The "C" locale.
4424 * @param s The name of a locale.
4425 * @param refs Refcount to pass to the base class.
4426 */
4427 explicit
4428 messages(__c_locale __cloc, const char* __s, size_t __refs = 0);
4429
4430 /*
4431 * @brief Open a message catalog.
4432 *
4433 * This function opens and returns a handle to a message catalog by
4434 * returning do_open(s, loc).
4435 *
4436 * @param s The catalog to open.
4437 * @param loc Locale to use for character set conversions.
4438 * @return Handle to the catalog or value < 0 if open fails.
4439 */
4440 catalog
4441 open(const basic_string<char>& __s, const locale& __loc) const
4442 { return this->do_open(__s, __loc); }
4443
4444 // Non-standard and unorthodox, yet effective.
4445 /*
4446 * @brief Open a message catalog.
4447 *
4448 * This non-standard function opens and returns a handle to a message
4449 * catalog by returning do_open(s, loc). The third argument provides a
4450 * message catalog root directory for gnu gettext and is ignored
4451 * otherwise.
4452 *
4453 * @param s The catalog to open.
4454 * @param loc Locale to use for character set conversions.
4455 * @param dir Message catalog root directory.
4456 * @return Handle to the catalog or value < 0 if open fails.
4457 */
4458 catalog
4459 open(const basic_string<char>&, const locale&, const char*) const;
4460
4461 /*
4462 * @brief Look up a string in a message catalog.
4463 *
4464 * This function retrieves and returns a message from a catalog by
4465 * returning do_get(c, set, msgid, s).
4466 *
4467 * For gnu, @a set and @a msgid are ignored. Returns gettext(s).
4468 * For default, returns s. For ieee, returns catgets(c,set,msgid,s).
4469 *
4470 * @param c The catalog to access.
4471 * @param set Implementation-defined.
4472 * @param msgid Implementation-defined.
4473 * @param s Default return value if retrieval fails.
4474 * @return Retrieved message or @a s if get fails.
4475 */
4476 string_type
4477 get(catalog __c, int __set, int __msgid, const string_type& __s) const
4478 { return this->do_get(__c, __set, __msgid, __s); }
4479
4480 /*
4481 * @brief Close a message catalog.
4482 *
4483 * Closes catalog @a c by calling do_close(c).
4484 *
4485 * @param c The catalog to close.
4486 */
4487 void
4488 close(catalog __c) const
4489 { return this->do_close(__c); }
4490
4491 protected:
4492 /// Destructor.
4493 virtual
4494 ~messages();
4495
4496 /*
4497 * @brief Open a message catalog.
4498 *
4499 * This function opens and returns a handle to a message catalog in an
4500 * implementation-defined manner. This function is a hook for derived
4501 * classes to change the value returned.
4502 *
4503 * @param s The catalog to open.
4504 * @param loc Locale to use for character set conversions.
4505 * @return Handle to the opened catalog, value < 0 if open failed.
4506 */
4507 virtual catalog
4508 do_open(const basic_string<char>&, const locale&) const;
4509
4510 /*
4511 * @brief Look up a string in a message catalog.
4512 *
4513 * This function retrieves and returns a message from a catalog in an
4514 * implementation-defined manner. This function is a hook for derived
4515 * classes to change the value returned.
4516 *
4517 * For gnu, @a set and @a msgid are ignored. Returns gettext(s).
4518 * For default, returns s. For ieee, returns catgets(c,set,msgid,s).
4519 *
4520 * @param c The catalog to access.
4521 * @param set Implementation-defined.
4522 * @param msgid Implementation-defined.
4523 * @param s Default return value if retrieval fails.
4524 * @return Retrieved message or @a s if get fails.
4525 */
4526 virtual string_type
4527 do_get(catalog, int, int, const string_type& __dfault) const;
4528
4529 /*
4530 * @brief Close a message catalog.
4531 *
4532 * @param c The catalog to close.
4533 */
4534 virtual void
4535 do_close(catalog) const;
4536
4537 // Returns a locale and codeset-converted string, given a char* message.
4538 char*
4539 _M_convert_to_char(const string_type& __msg) const
4540 {
4541 // XXX
4542 return reinterpret_cast<char*>(const_cast<_CharT*>(__msg.c_str()));
4543 }
4544
4545 // Returns a locale and codeset-converted string, given a char* message.
4546 string_type
4547 _M_convert_from_char(char*) const
4548 {
4549 #if 0
4550 // Length of message string without terminating null.
4551 size_t __len = char_traits<char>::length(__msg) - 1;
4552
4553 // "everybody can easily convert the string using
4554 // mbsrtowcs/wcsrtombs or with iconv()"
4555
4556 // Convert char* to _CharT in locale used to open catalog.
4557 // XXX need additional template parameter on messages class for this..
4558 // typedef typename codecvt<char, _CharT, _StateT> __codecvt_type;
4559 typedef typename codecvt<char, _CharT, mbstate_t> __codecvt_type;
4560
4561 __codecvt_type::state_type __state;
4562 // XXX may need to initialize state.
4563 //initialize_state(__state._M_init());
4564
4565 char* __from_next;
4566 // XXX what size for this string?
4567 _CharT* __to = static_cast<_CharT*>(__builtin_alloca(__len + 1));
4568 const __codecvt_type& __cvt = use_facet<__codecvt_type>(_M_locale_conv);
4569 __cvt.out(__state, __msg, __msg + __len, __from_next,
4570 __to, __to + __len + 1, __to_next);
4571 return string_type(__to);
4572 #endif
4573 #if 0
4574 typedef ctype<_CharT> __ctype_type;
4575 // const __ctype_type& __cvt = use_facet<__ctype_type>(_M_locale_msg);
4576 const __ctype_type& __cvt = use_facet<__ctype_type>(locale());
4577 // XXX Again, proper length of converted string an issue here.
4578 // For now, assume the converted length is not larger.
4579 _CharT* __dest = static_cast<_CharT*>(__builtin_alloca(__len + 1));
4580 __cvt.widen(__msg, __msg + __len, __dest);
4581 return basic_string<_CharT>(__dest);
4582 #endif
4583 return string_type();
4584 }
4585 };
4586
4587 template<typename _CharT>
4588 locale::id messages<_CharT>::id;
4589
4590 // Specializations for required instantiations.
4591 template<>
4592 string
4593 messages<char>::do_get(catalog, int, int, const string&) const;
4594
4595 #ifdef _GLIBCXX_USE_WCHAR_T
4596 template<>
4597 wstring
4598 messages<wchar_t>::do_get(catalog, int, int, const wstring&) const;
4599 #endif
4600
4601 /// @brief class messages_byname [22.2.7.2].
4602 template<typename _CharT>
4603 class messages_byname : public messages<_CharT>
4604 {
4605 public:
4606 typedef _CharT char_type;
4607 typedef basic_string<_CharT> string_type;
4608
4609 explicit
4610 messages_byname(const char* __s, size_t __refs = 0);
4611
4612 protected:
4613 virtual
4614 ~messages_byname()
4615 { }
4616 };
4617
4618 _GLIBCXX_END_NAMESPACE
4619
4620 // Include host and configuration specific messages functions.
4621 #include <bits/messages_members.h>
4622
4623 _GLIBCXX_BEGIN_NAMESPACE(std)
4624
4625 // Subclause convenience interfaces, inlines.
4626 // NB: These are inline because, when used in a loop, some compilers
4627 // can hoist the body out of the loop; then it's just as fast as the
4628 // C is*() function.
4629
4630 /// Convenience interface to ctype.is(ctype_base::space, __c).
4631 template<typename _CharT>
4632 inline bool
4633 isspace(_CharT __c, const locale& __loc)
4634 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c); }
4635
4636 /// Convenience interface to ctype.is(ctype_base::print, __c).
4637 template<typename _CharT>
4638 inline bool
4639 isprint(_CharT __c, const locale& __loc)
4640 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c); }
4641
4642 /// Convenience interface to ctype.is(ctype_base::cntrl, __c).
4643 template<typename _CharT>
4644 inline bool
4645 iscntrl(_CharT __c, const locale& __loc)
4646 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c); }
4647
4648 /// Convenience interface to ctype.is(ctype_base::upper, __c).
4649 template<typename _CharT>
4650 inline bool
4651 isupper(_CharT __c, const locale& __loc)
4652 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c); }
4653
4654 /// Convenience interface to ctype.is(ctype_base::lower, __c).
4655 template<typename _CharT>
4656 inline bool
4657 islower(_CharT __c, const locale& __loc)
4658 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c); }
4659
4660 /// Convenience interface to ctype.is(ctype_base::alpha, __c).
4661 template<typename _CharT>
4662 inline bool
4663 isalpha(_CharT __c, const locale& __loc)
4664 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c); }
4665
4666 /// Convenience interface to ctype.is(ctype_base::digit, __c).
4667 template<typename _CharT>
4668 inline bool
4669 isdigit(_CharT __c, const locale& __loc)
4670 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c); }
4671
4672 /// Convenience interface to ctype.is(ctype_base::punct, __c).
4673 template<typename _CharT>
4674 inline bool
4675 ispunct(_CharT __c, const locale& __loc)
4676 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c); }
4677
4678 /// Convenience interface to ctype.is(ctype_base::xdigit, __c).
4679 template<typename _CharT>
4680 inline bool
4681 isxdigit(_CharT __c, const locale& __loc)
4682 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c); }
4683
4684 /// Convenience interface to ctype.is(ctype_base::alnum, __c).
4685 template<typename _CharT>
4686 inline bool
4687 isalnum(_CharT __c, const locale& __loc)
4688 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c); }
4689
4690 /// Convenience interface to ctype.is(ctype_base::graph, __c).
4691 template<typename _CharT>
4692 inline bool
4693 isgraph(_CharT __c, const locale& __loc)
4694 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c); }
4695
4696 /// Convenience interface to ctype.toupper(__c).
4697 template<typename _CharT>
4698 inline _CharT
4699 toupper(_CharT __c, const locale& __loc)
4700 { return use_facet<ctype<_CharT> >(__loc).toupper(__c); }
4701
4702 /// Convenience interface to ctype.tolower(__c).
4703 template<typename _CharT>
4704 inline _CharT
4705 tolower(_CharT __c, const locale& __loc)
4706 { return use_facet<ctype<_CharT> >(__loc).tolower(__c); }
4707
4708 _GLIBCXX_END_NAMESPACE
4709
4710 #endif