Mark as release
[official-gcc.git] / libstdc++-v3 / include / bits / locale_facets.h
blobe31ae1751cc53a1f8b4139b2f1ce735d6b392528
1 // Locale support -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 /** @file locale_facets.h
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
37 // ISO C++ 14882: 22.1 Locales
40 #ifndef _LOCALE_FACETS_H
41 #define _LOCALE_FACETS_H 1
43 #pragma GCC system_header
45 #include <ctime> // For struct tm
46 #include <cwctype> // For wctype_t
47 #include <bits/ctype_base.h>
48 #include <iosfwd>
49 #include <bits/ios_base.h> // For ios_base, ios_base::iostate
50 #include <streambuf>
51 #include <bits/cpp_type_traits.h>
53 _GLIBCXX_BEGIN_NAMESPACE(std)
55 // NB: Don't instantiate required wchar_t facets if no wchar_t support.
56 #ifdef _GLIBCXX_USE_WCHAR_T
57 # define _GLIBCXX_NUM_FACETS 28
58 #else
59 # define _GLIBCXX_NUM_FACETS 14
60 #endif
62 // Convert string to numeric value of type _Tv and store results.
63 // NB: This is specialized for all required types, there is no
64 // generic definition.
65 template<typename _Tv>
66 void
67 __convert_to_v(const char* __in, _Tv& __out, ios_base::iostate& __err,
68 const __c_locale& __cloc);
70 // Explicit specializations for required types.
71 template<>
72 void
73 __convert_to_v(const char*, float&, ios_base::iostate&,
74 const __c_locale&);
76 template<>
77 void
78 __convert_to_v(const char*, double&, ios_base::iostate&,
79 const __c_locale&);
81 template<>
82 void
83 __convert_to_v(const char*, long double&, ios_base::iostate&,
84 const __c_locale&);
86 // NB: __pad is a struct, rather than a function, so it can be
87 // partially-specialized.
88 template<typename _CharT, typename _Traits>
89 struct __pad
91 static void
92 _S_pad(ios_base& __io, _CharT __fill, _CharT* __news,
93 const _CharT* __olds, const streamsize __newlen,
94 const streamsize __oldlen, const bool __num);
97 // Used by both numeric and monetary facets.
98 // Inserts "group separator" characters into an array of characters.
99 // It's recursive, one iteration per group. It moves the characters
100 // in the buffer this way: "xxxx12345" -> "12,345xxx". Call this
101 // only with __glen != 0.
102 template<typename _CharT>
103 _CharT*
104 __add_grouping(_CharT* __s, _CharT __sep,
105 const char* __gbeg, size_t __gsize,
106 const _CharT* __first, const _CharT* __last);
108 // This template permits specializing facet output code for
109 // ostreambuf_iterator. For ostreambuf_iterator, sputn is
110 // significantly more efficient than incrementing iterators.
111 template<typename _CharT>
112 inline
113 ostreambuf_iterator<_CharT>
114 __write(ostreambuf_iterator<_CharT> __s, const _CharT* __ws, int __len)
116 __s._M_put(__ws, __len);
117 return __s;
120 // This is the unspecialized form of the template.
121 template<typename _CharT, typename _OutIter>
122 inline
123 _OutIter
124 __write(_OutIter __s, const _CharT* __ws, int __len)
126 for (int __j = 0; __j < __len; __j++, ++__s)
127 *__s = __ws[__j];
128 return __s;
132 // 22.2.1.1 Template class ctype
133 // Include host and configuration specific ctype enums for ctype_base.
135 // Common base for ctype<_CharT>.
137 * @brief Common base for ctype facet
139 * This template class provides implementations of the public functions
140 * that forward to the protected virtual functions.
142 * This template also provides abtract stubs for the protected virtual
143 * functions.
145 template<typename _CharT>
146 class __ctype_abstract_base : public locale::facet, public ctype_base
148 public:
149 // Types:
150 /// Typedef for the template parameter
151 typedef _CharT char_type;
154 * @brief Test char_type classification.
156 * This function finds a mask M for @a c and compares it to mask @a m.
157 * It does so by returning the value of ctype<char_type>::do_is().
159 * @param c The char_type to compare the mask of.
160 * @param m The mask to compare against.
161 * @return (M & m) != 0.
163 bool
164 is(mask __m, char_type __c) const
165 { return this->do_is(__m, __c); }
168 * @brief Return a mask array.
170 * This function finds the mask for each char_type in the range [lo,hi)
171 * and successively writes it to vec. vec must have as many elements
172 * as the char array. It does so by returning the value of
173 * ctype<char_type>::do_is().
175 * @param lo Pointer to start of range.
176 * @param hi Pointer to end of range.
177 * @param vec Pointer to an array of mask storage.
178 * @return @a hi.
180 const char_type*
181 is(const char_type *__lo, const char_type *__hi, mask *__vec) const
182 { return this->do_is(__lo, __hi, __vec); }
185 * @brief Find char_type matching a mask
187 * This function searches for and returns the first char_type c in
188 * [lo,hi) for which is(m,c) is true. It does so by returning
189 * ctype<char_type>::do_scan_is().
191 * @param m The mask to compare against.
192 * @param lo Pointer to start of range.
193 * @param hi Pointer to end of range.
194 * @return Pointer to matching char_type if found, else @a hi.
196 const char_type*
197 scan_is(mask __m, const char_type* __lo, const char_type* __hi) const
198 { return this->do_scan_is(__m, __lo, __hi); }
201 * @brief Find char_type not matching a mask
203 * This function searches for and returns the first char_type c in
204 * [lo,hi) for which is(m,c) is false. It does so by returning
205 * ctype<char_type>::do_scan_not().
207 * @param m The mask to compare against.
208 * @param lo Pointer to first char in range.
209 * @param hi Pointer to end of range.
210 * @return Pointer to non-matching char if found, else @a hi.
212 const char_type*
213 scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
214 { return this->do_scan_not(__m, __lo, __hi); }
217 * @brief Convert to uppercase.
219 * This function converts the argument to uppercase if possible.
220 * If not possible (for example, '2'), returns the argument. It does
221 * so by returning ctype<char_type>::do_toupper().
223 * @param c The char_type to convert.
224 * @return The uppercase char_type if convertible, else @a c.
226 char_type
227 toupper(char_type __c) const
228 { return this->do_toupper(__c); }
231 * @brief Convert array to uppercase.
233 * This function converts each char_type in the range [lo,hi) to
234 * uppercase if possible. Other elements remain untouched. It does so
235 * by returning ctype<char_type>:: do_toupper(lo, hi).
237 * @param lo Pointer to start of range.
238 * @param hi Pointer to end of range.
239 * @return @a hi.
241 const char_type*
242 toupper(char_type *__lo, const char_type* __hi) const
243 { return this->do_toupper(__lo, __hi); }
246 * @brief Convert to lowercase.
248 * This function converts the argument to lowercase if possible. If
249 * not possible (for example, '2'), returns the argument. It does so
250 * by returning ctype<char_type>::do_tolower(c).
252 * @param c The char_type to convert.
253 * @return The lowercase char_type if convertible, else @a c.
255 char_type
256 tolower(char_type __c) const
257 { return this->do_tolower(__c); }
260 * @brief Convert array to lowercase.
262 * This function converts each char_type in the range [lo,hi) to
263 * lowercase if possible. Other elements remain untouched. It does so
264 * by returning ctype<char_type>:: do_tolower(lo, hi).
266 * @param lo Pointer to start of range.
267 * @param hi Pointer to end of range.
268 * @return @a hi.
270 const char_type*
271 tolower(char_type* __lo, const char_type* __hi) const
272 { return this->do_tolower(__lo, __hi); }
275 * @brief Widen char to char_type
277 * This function converts the char argument to char_type using the
278 * simplest reasonable transformation. It does so by returning
279 * ctype<char_type>::do_widen(c).
281 * Note: this is not what you want for codepage conversions. See
282 * codecvt for that.
284 * @param c The char to convert.
285 * @return The converted char_type.
287 char_type
288 widen(char __c) const
289 { return this->do_widen(__c); }
292 * @brief Widen array to char_type
294 * This function converts each char in the input to char_type using the
295 * simplest reasonable transformation. It does so by returning
296 * ctype<char_type>::do_widen(c).
298 * Note: this is not what you want for codepage conversions. See
299 * codecvt for that.
301 * @param lo Pointer to start of range.
302 * @param hi Pointer to end of range.
303 * @param to Pointer to the destination array.
304 * @return @a hi.
306 const char*
307 widen(const char* __lo, const char* __hi, char_type* __to) const
308 { return this->do_widen(__lo, __hi, __to); }
311 * @brief Narrow char_type to char
313 * This function converts the char_type to char using the simplest
314 * reasonable transformation. If the conversion fails, dfault is
315 * returned instead. It does so by returning
316 * ctype<char_type>::do_narrow(c).
318 * Note: this is not what you want for codepage conversions. See
319 * codecvt for that.
321 * @param c The char_type to convert.
322 * @param dfault Char to return if conversion fails.
323 * @return The converted char.
325 char
326 narrow(char_type __c, char __dfault) const
327 { return this->do_narrow(__c, __dfault); }
330 * @brief Narrow array to char array
332 * This function converts each char_type in the input to char using the
333 * simplest reasonable transformation and writes the results to the
334 * destination array. For any char_type in the input that cannot be
335 * converted, @a dfault is used instead. It does so by returning
336 * ctype<char_type>::do_narrow(lo, hi, dfault, to).
338 * Note: this is not what you want for codepage conversions. See
339 * codecvt for that.
341 * @param lo Pointer to start of range.
342 * @param hi Pointer to end of range.
343 * @param dfault Char to use if conversion fails.
344 * @param to Pointer to the destination array.
345 * @return @a hi.
347 const char_type*
348 narrow(const char_type* __lo, const char_type* __hi,
349 char __dfault, char *__to) const
350 { return this->do_narrow(__lo, __hi, __dfault, __to); }
352 protected:
353 explicit
354 __ctype_abstract_base(size_t __refs = 0): facet(__refs) { }
356 virtual
357 ~__ctype_abstract_base() { }
360 * @brief Test char_type classification.
362 * This function finds a mask M for @a c and compares it to mask @a m.
364 * do_is() is a hook for a derived facet to change the behavior of
365 * classifying. do_is() must always return the same result for the
366 * same input.
368 * @param c The char_type to find the mask of.
369 * @param m The mask to compare against.
370 * @return (M & m) != 0.
372 virtual bool
373 do_is(mask __m, char_type __c) const = 0;
376 * @brief Return a mask array.
378 * This function finds the mask for each char_type in the range [lo,hi)
379 * and successively writes it to vec. vec must have as many elements
380 * as the input.
382 * do_is() is a hook for a derived facet to change the behavior of
383 * classifying. do_is() must always return the same result for the
384 * same input.
386 * @param lo Pointer to start of range.
387 * @param hi Pointer to end of range.
388 * @param vec Pointer to an array of mask storage.
389 * @return @a hi.
391 virtual const char_type*
392 do_is(const char_type* __lo, const char_type* __hi,
393 mask* __vec) const = 0;
396 * @brief Find char_type matching mask
398 * This function searches for and returns the first char_type c in
399 * [lo,hi) for which is(m,c) is true.
401 * do_scan_is() is a hook for a derived facet to change the behavior of
402 * match searching. do_is() must always return the same result for the
403 * same input.
405 * @param m The mask to compare against.
406 * @param lo Pointer to start of range.
407 * @param hi Pointer to end of range.
408 * @return Pointer to a matching char_type if found, else @a hi.
410 virtual const char_type*
411 do_scan_is(mask __m, const char_type* __lo,
412 const char_type* __hi) const = 0;
415 * @brief Find char_type not matching mask
417 * This function searches for and returns a pointer to the first
418 * char_type c of [lo,hi) for which is(m,c) is false.
420 * do_scan_is() is a hook for a derived facet to change the behavior of
421 * match searching. do_is() must always return the same result for the
422 * same input.
424 * @param m The mask to compare against.
425 * @param lo Pointer to start of range.
426 * @param hi Pointer to end of range.
427 * @return Pointer to a non-matching char_type if found, else @a hi.
429 virtual const char_type*
430 do_scan_not(mask __m, const char_type* __lo,
431 const char_type* __hi) const = 0;
434 * @brief Convert to uppercase.
436 * This virtual function converts the char_type argument to uppercase
437 * if possible. If not possible (for example, '2'), returns the
438 * argument.
440 * do_toupper() is a hook for a derived facet to change the behavior of
441 * uppercasing. do_toupper() must always return the same result for
442 * the same input.
444 * @param c The char_type to convert.
445 * @return The uppercase char_type if convertible, else @a c.
447 virtual char_type
448 do_toupper(char_type) const = 0;
451 * @brief Convert array to uppercase.
453 * This virtual function converts each char_type in the range [lo,hi)
454 * to uppercase if possible. Other elements remain untouched.
456 * do_toupper() is a hook for a derived facet to change the behavior of
457 * uppercasing. do_toupper() must always return the same result for
458 * the same input.
460 * @param lo Pointer to start of range.
461 * @param hi Pointer to end of range.
462 * @return @a hi.
464 virtual const char_type*
465 do_toupper(char_type* __lo, const char_type* __hi) const = 0;
468 * @brief Convert to lowercase.
470 * This virtual function converts the argument to lowercase if
471 * possible. If not possible (for example, '2'), returns the argument.
473 * do_tolower() is a hook for a derived facet to change the behavior of
474 * lowercasing. do_tolower() must always return the same result for
475 * the same input.
477 * @param c The char_type to convert.
478 * @return The lowercase char_type if convertible, else @a c.
480 virtual char_type
481 do_tolower(char_type) const = 0;
484 * @brief Convert array to lowercase.
486 * This virtual function converts each char_type in the range [lo,hi)
487 * to lowercase if possible. Other elements remain untouched.
489 * do_tolower() is a hook for a derived facet to change the behavior of
490 * lowercasing. do_tolower() must always return the same result for
491 * the same input.
493 * @param lo Pointer to start of range.
494 * @param hi Pointer to end of range.
495 * @return @a hi.
497 virtual const char_type*
498 do_tolower(char_type* __lo, const char_type* __hi) const = 0;
501 * @brief Widen char
503 * This virtual function converts the char to char_type using the
504 * simplest reasonable transformation.
506 * do_widen() is a hook for a derived facet to change the behavior of
507 * widening. do_widen() must always return the same result for the
508 * same input.
510 * Note: this is not what you want for codepage conversions. See
511 * codecvt for that.
513 * @param c The char to convert.
514 * @return The converted char_type
516 virtual char_type
517 do_widen(char) const = 0;
520 * @brief Widen char array
522 * This function converts each char in the input to char_type using the
523 * simplest reasonable transformation.
525 * do_widen() is a hook for a derived facet to change the behavior of
526 * widening. do_widen() must always return the same result for the
527 * same input.
529 * Note: this is not what you want for codepage conversions. See
530 * codecvt for that.
532 * @param lo Pointer to start range.
533 * @param hi Pointer to end of range.
534 * @param to Pointer to the destination array.
535 * @return @a hi.
537 virtual const char*
538 do_widen(const char* __lo, const char* __hi,
539 char_type* __dest) const = 0;
542 * @brief Narrow char_type to char
544 * This virtual function converts the argument to char using the
545 * simplest reasonable transformation. If the conversion fails, dfault
546 * is returned instead.
548 * do_narrow() is a hook for a derived facet to change the behavior of
549 * narrowing. do_narrow() must always return the same result for the
550 * same input.
552 * Note: this is not what you want for codepage conversions. See
553 * codecvt for that.
555 * @param c The char_type to convert.
556 * @param dfault Char to return if conversion fails.
557 * @return The converted char.
559 virtual char
560 do_narrow(char_type, char __dfault) const = 0;
563 * @brief Narrow char_type array to char
565 * This virtual function converts each char_type in the range [lo,hi) to
566 * char using the simplest reasonable transformation and writes the
567 * results to the destination array. For any element in the input that
568 * cannot be converted, @a dfault is used instead.
570 * do_narrow() is a hook for a derived facet to change the behavior of
571 * narrowing. do_narrow() must always return the same result for the
572 * same input.
574 * Note: this is not what you want for codepage conversions. See
575 * codecvt for that.
577 * @param lo Pointer to start of range.
578 * @param hi Pointer to end of range.
579 * @param dfault Char to use if conversion fails.
580 * @param to Pointer to the destination array.
581 * @return @a hi.
583 virtual const char_type*
584 do_narrow(const char_type* __lo, const char_type* __hi,
585 char __dfault, char* __dest) const = 0;
588 // NB: Generic, mostly useless implementation.
590 * @brief Template ctype facet
592 * This template class defines classification and conversion functions for
593 * character sets. It wraps <cctype> functionality. Ctype gets used by
594 * streams for many I/O operations.
596 * This template provides the protected virtual functions the developer
597 * will have to replace in a derived class or specialization to make a
598 * working facet. The public functions that access them are defined in
599 * __ctype_abstract_base, to allow for implementation flexibility. See
600 * ctype<wchar_t> for an example. The functions are documented in
601 * __ctype_abstract_base.
603 * Note: implementations are provided for all the protected virtual
604 * functions, but will likely not be useful.
606 template<typename _CharT>
607 class ctype : public __ctype_abstract_base<_CharT>
609 public:
610 // Types:
611 typedef _CharT char_type;
612 typedef typename __ctype_abstract_base<_CharT>::mask mask;
614 /// The facet id for ctype<char_type>
615 static locale::id id;
617 explicit
618 ctype(size_t __refs = 0) : __ctype_abstract_base<_CharT>(__refs) { }
620 protected:
621 virtual
622 ~ctype();
624 virtual bool
625 do_is(mask __m, char_type __c) const;
627 virtual const char_type*
628 do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const;
630 virtual const char_type*
631 do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const;
633 virtual const char_type*
634 do_scan_not(mask __m, const char_type* __lo,
635 const char_type* __hi) const;
637 virtual char_type
638 do_toupper(char_type __c) const;
640 virtual const char_type*
641 do_toupper(char_type* __lo, const char_type* __hi) const;
643 virtual char_type
644 do_tolower(char_type __c) const;
646 virtual const char_type*
647 do_tolower(char_type* __lo, const char_type* __hi) const;
649 virtual char_type
650 do_widen(char __c) const;
652 virtual const char*
653 do_widen(const char* __lo, const char* __hi, char_type* __dest) const;
655 virtual char
656 do_narrow(char_type, char __dfault) const;
658 virtual const char_type*
659 do_narrow(const char_type* __lo, const char_type* __hi,
660 char __dfault, char* __dest) const;
663 template<typename _CharT>
664 locale::id ctype<_CharT>::id;
666 // 22.2.1.3 ctype<char> specialization.
668 * @brief The ctype<char> specialization.
670 * This class defines classification and conversion functions for
671 * the char type. It gets used by char streams for many I/O
672 * operations. The char specialization provides a number of
673 * optimizations as well.
675 template<>
676 class ctype<char> : public locale::facet, public ctype_base
678 public:
679 // Types:
680 /// Typedef for the template parameter char.
681 typedef char char_type;
683 protected:
684 // Data Members:
685 __c_locale _M_c_locale_ctype;
686 bool _M_del;
687 __to_type _M_toupper;
688 __to_type _M_tolower;
689 const mask* _M_table;
690 mutable char _M_widen_ok;
691 mutable char _M_widen[1 + static_cast<unsigned char>(-1)];
692 mutable char _M_narrow[1 + static_cast<unsigned char>(-1)];
693 mutable char _M_narrow_ok; // 0 uninitialized, 1 init,
694 // 2 memcpy can't be used
696 public:
697 /// The facet id for ctype<char>
698 static locale::id id;
699 /// The size of the mask table. It is SCHAR_MAX + 1.
700 static const size_t table_size = 1 + static_cast<unsigned char>(-1);
703 * @brief Constructor performs initialization.
705 * This is the constructor provided by the standard.
707 * @param table If non-zero, table is used as the per-char mask.
708 * Else classic_table() is used.
709 * @param del If true, passes ownership of table to this facet.
710 * @param refs Passed to the base facet class.
712 explicit
713 ctype(const mask* __table = 0, bool __del = false, size_t __refs = 0);
716 * @brief Constructor performs static initialization.
718 * This constructor is used to construct the initial C locale facet.
720 * @param cloc Handle to C locale data.
721 * @param table If non-zero, table is used as the per-char mask.
722 * @param del If true, passes ownership of table to this facet.
723 * @param refs Passed to the base facet class.
725 explicit
726 ctype(__c_locale __cloc, const mask* __table = 0, bool __del = false,
727 size_t __refs = 0);
730 * @brief Test char classification.
732 * This function compares the mask table[c] to @a m.
734 * @param c The char to compare the mask of.
735 * @param m The mask to compare against.
736 * @return True if m & table[c] is true, false otherwise.
738 inline bool
739 is(mask __m, char __c) const;
742 * @brief Return a mask array.
744 * This function finds the mask for each char in the range [lo, hi) and
745 * successively writes it to vec. vec must have as many elements as
746 * the char array.
748 * @param lo Pointer to start of range.
749 * @param hi Pointer to end of range.
750 * @param vec Pointer to an array of mask storage.
751 * @return @a hi.
753 inline const char*
754 is(const char* __lo, const char* __hi, mask* __vec) const;
757 * @brief Find char matching a mask
759 * This function searches for and returns the first char in [lo,hi) for
760 * which is(m,char) is true.
762 * @param m The mask to compare against.
763 * @param lo Pointer to start of range.
764 * @param hi Pointer to end of range.
765 * @return Pointer to a matching char if found, else @a hi.
767 inline const char*
768 scan_is(mask __m, const char* __lo, const char* __hi) const;
771 * @brief Find char not matching a mask
773 * This function searches for and returns a pointer to the first char
774 * in [lo,hi) for which is(m,char) is false.
776 * @param m The mask to compare against.
777 * @param lo Pointer to start of range.
778 * @param hi Pointer to end of range.
779 * @return Pointer to a non-matching char if found, else @a hi.
781 inline const char*
782 scan_not(mask __m, const char* __lo, const char* __hi) const;
785 * @brief Convert to uppercase.
787 * This function converts the char argument to uppercase if possible.
788 * If not possible (for example, '2'), returns the argument.
790 * toupper() acts as if it returns ctype<char>::do_toupper(c).
791 * do_toupper() must always return the same result for the same input.
793 * @param c The char to convert.
794 * @return The uppercase char if convertible, else @a c.
796 char_type
797 toupper(char_type __c) const
798 { return this->do_toupper(__c); }
801 * @brief Convert array to uppercase.
803 * This function converts each char in the range [lo,hi) to uppercase
804 * if possible. Other chars remain untouched.
806 * toupper() acts as if it returns ctype<char>:: do_toupper(lo, hi).
807 * do_toupper() must always return the same result for the same input.
809 * @param lo Pointer to first char in range.
810 * @param hi Pointer to end of range.
811 * @return @a hi.
813 const char_type*
814 toupper(char_type *__lo, const char_type* __hi) const
815 { return this->do_toupper(__lo, __hi); }
818 * @brief Convert to lowercase.
820 * This function converts the char argument to lowercase if possible.
821 * If not possible (for example, '2'), returns the argument.
823 * tolower() acts as if it returns ctype<char>::do_tolower(c).
824 * do_tolower() must always return the same result for the same input.
826 * @param c The char to convert.
827 * @return The lowercase char if convertible, else @a c.
829 char_type
830 tolower(char_type __c) const
831 { return this->do_tolower(__c); }
834 * @brief Convert array to lowercase.
836 * This function converts each char in the range [lo,hi) to lowercase
837 * if possible. Other chars remain untouched.
839 * tolower() acts as if it returns ctype<char>:: do_tolower(lo, hi).
840 * do_tolower() must always return the same result for the same input.
842 * @param lo Pointer to first char in range.
843 * @param hi Pointer to end of range.
844 * @return @a hi.
846 const char_type*
847 tolower(char_type* __lo, const char_type* __hi) const
848 { return this->do_tolower(__lo, __hi); }
851 * @brief Widen char
853 * This function converts the char to char_type using the simplest
854 * reasonable transformation. For an underived ctype<char> facet, the
855 * argument will be returned unchanged.
857 * This function works as if it returns ctype<char>::do_widen(c).
858 * do_widen() must always return the same result for the same input.
860 * Note: this is not what you want for codepage conversions. See
861 * codecvt for that.
863 * @param c The char to convert.
864 * @return The converted character.
866 char_type
867 widen(char __c) const
869 if (_M_widen_ok)
870 return _M_widen[static_cast<unsigned char>(__c)];
871 this->_M_widen_init();
872 return this->do_widen(__c);
876 * @brief Widen char array
878 * This function converts each char in the input to char using the
879 * simplest reasonable transformation. For an underived ctype<char>
880 * facet, the argument will be copied unchanged.
882 * This function works as if it returns ctype<char>::do_widen(c).
883 * do_widen() must always return the same result for the same input.
885 * Note: this is not what you want for codepage conversions. See
886 * codecvt for that.
888 * @param lo Pointer to first char in range.
889 * @param hi Pointer to end of range.
890 * @param to Pointer to the destination array.
891 * @return @a hi.
893 const char*
894 widen(const char* __lo, const char* __hi, char_type* __to) const
896 if (_M_widen_ok == 1)
898 memcpy(__to, __lo, __hi - __lo);
899 return __hi;
901 if (!_M_widen_ok)
902 _M_widen_init();
903 return this->do_widen(__lo, __hi, __to);
907 * @brief Narrow char
909 * This function converts the char to char using the simplest
910 * reasonable transformation. If the conversion fails, dfault is
911 * returned instead. For an underived ctype<char> facet, @a c
912 * will be returned unchanged.
914 * This function works as if it returns ctype<char>::do_narrow(c).
915 * do_narrow() must always return the same result for the same input.
917 * Note: this is not what you want for codepage conversions. See
918 * codecvt for that.
920 * @param c The char to convert.
921 * @param dfault Char to return if conversion fails.
922 * @return The converted character.
924 char
925 narrow(char_type __c, char __dfault) const
927 if (_M_narrow[static_cast<unsigned char>(__c)])
928 return _M_narrow[static_cast<unsigned char>(__c)];
929 const char __t = do_narrow(__c, __dfault);
930 if (__t != __dfault)
931 _M_narrow[static_cast<unsigned char>(__c)] = __t;
932 return __t;
936 * @brief Narrow char array
938 * This function converts each char in the input to char using the
939 * simplest reasonable transformation and writes the results to the
940 * destination array. For any char in the input that cannot be
941 * converted, @a dfault is used instead. For an underived ctype<char>
942 * facet, the argument will be copied unchanged.
944 * This function works as if it returns ctype<char>::do_narrow(lo, hi,
945 * dfault, to). do_narrow() must always return the same result for the
946 * same input.
948 * Note: this is not what you want for codepage conversions. See
949 * codecvt for that.
951 * @param lo Pointer to start of range.
952 * @param hi Pointer to end of range.
953 * @param dfault Char to use if conversion fails.
954 * @param to Pointer to the destination array.
955 * @return @a hi.
957 const char_type*
958 narrow(const char_type* __lo, const char_type* __hi,
959 char __dfault, char *__to) const
961 if (__builtin_expect(_M_narrow_ok == 1, true))
963 memcpy(__to, __lo, __hi - __lo);
964 return __hi;
966 if (!_M_narrow_ok)
967 _M_narrow_init();
968 return this->do_narrow(__lo, __hi, __dfault, __to);
971 protected:
972 /// Returns a pointer to the mask table provided to the constructor, or
973 /// the default from classic_table() if none was provided.
974 const mask*
975 table() const throw()
976 { return _M_table; }
978 /// Returns a pointer to the C locale mask table.
979 static const mask*
980 classic_table() throw();
983 * @brief Destructor.
985 * This function deletes table() if @a del was true in the
986 * constructor.
988 virtual
989 ~ctype();
992 * @brief Convert to uppercase.
994 * This virtual function converts the char argument to uppercase if
995 * possible. If not possible (for example, '2'), returns the argument.
997 * do_toupper() is a hook for a derived facet to change the behavior of
998 * uppercasing. do_toupper() must always return the same result for
999 * the same input.
1001 * @param c The char to convert.
1002 * @return The uppercase char if convertible, else @a c.
1004 virtual char_type
1005 do_toupper(char_type) const;
1008 * @brief Convert array to uppercase.
1010 * This virtual function converts each char in the range [lo,hi) to
1011 * uppercase if possible. Other chars remain untouched.
1013 * do_toupper() is a hook for a derived facet to change the behavior of
1014 * uppercasing. do_toupper() must always return the same result for
1015 * the same input.
1017 * @param lo Pointer to start of range.
1018 * @param hi Pointer to end of range.
1019 * @return @a hi.
1021 virtual const char_type*
1022 do_toupper(char_type* __lo, const char_type* __hi) const;
1025 * @brief Convert to lowercase.
1027 * This virtual function converts the char argument to lowercase if
1028 * possible. If not possible (for example, '2'), returns the argument.
1030 * do_tolower() is a hook for a derived facet to change the behavior of
1031 * lowercasing. do_tolower() must always return the same result for
1032 * the same input.
1034 * @param c The char to convert.
1035 * @return The lowercase char if convertible, else @a c.
1037 virtual char_type
1038 do_tolower(char_type) const;
1041 * @brief Convert array to lowercase.
1043 * This virtual function converts each char in the range [lo,hi) to
1044 * lowercase if possible. Other chars remain untouched.
1046 * do_tolower() is a hook for a derived facet to change the behavior of
1047 * lowercasing. do_tolower() must always return the same result for
1048 * the same input.
1050 * @param lo Pointer to first char in range.
1051 * @param hi Pointer to end of range.
1052 * @return @a hi.
1054 virtual const char_type*
1055 do_tolower(char_type* __lo, const char_type* __hi) const;
1058 * @brief Widen char
1060 * This virtual function converts the char to char using the simplest
1061 * reasonable transformation. For an underived ctype<char> facet, the
1062 * argument will be returned unchanged.
1064 * do_widen() is a hook for a derived facet to change the behavior of
1065 * widening. do_widen() must always return the same result for the
1066 * same input.
1068 * Note: this is not what you want for codepage conversions. See
1069 * codecvt for that.
1071 * @param c The char to convert.
1072 * @return The converted character.
1074 virtual char_type
1075 do_widen(char __c) const
1076 { return __c; }
1079 * @brief Widen char array
1081 * This function converts each char in the range [lo,hi) to char using
1082 * the simplest reasonable transformation. For an underived
1083 * ctype<char> facet, the argument will be copied unchanged.
1085 * do_widen() is a hook for a derived facet to change the behavior of
1086 * widening. do_widen() must always return the same result for the
1087 * same input.
1089 * Note: this is not what you want for codepage conversions. See
1090 * codecvt for that.
1092 * @param lo Pointer to start of range.
1093 * @param hi Pointer to end of range.
1094 * @param to Pointer to the destination array.
1095 * @return @a hi.
1097 virtual const char*
1098 do_widen(const char* __lo, const char* __hi, char_type* __dest) const
1100 memcpy(__dest, __lo, __hi - __lo);
1101 return __hi;
1105 * @brief Narrow char
1107 * This virtual function converts the char to char using the simplest
1108 * reasonable transformation. If the conversion fails, dfault is
1109 * returned instead. For an underived ctype<char> facet, @a c will be
1110 * returned unchanged.
1112 * do_narrow() is a hook for a derived facet to change the behavior of
1113 * narrowing. do_narrow() must always return the same result for the
1114 * same input.
1116 * Note: this is not what you want for codepage conversions. See
1117 * codecvt for that.
1119 * @param c The char to convert.
1120 * @param dfault Char to return if conversion fails.
1121 * @return The converted char.
1123 virtual char
1124 do_narrow(char_type __c, char) const
1125 { return __c; }
1128 * @brief Narrow char array to char array
1130 * This virtual function converts each char in the range [lo,hi) to
1131 * char using the simplest reasonable transformation and writes the
1132 * results to the destination array. For any char in the input that
1133 * cannot be converted, @a dfault is used instead. For an underived
1134 * ctype<char> facet, the argument will be copied unchanged.
1136 * do_narrow() is a hook for a derived facet to change the behavior of
1137 * narrowing. do_narrow() must always return the same result for the
1138 * same input.
1140 * Note: this is not what you want for codepage conversions. See
1141 * codecvt for that.
1143 * @param lo Pointer to start of range.
1144 * @param hi Pointer to end of range.
1145 * @param dfault Char to use if conversion fails.
1146 * @param to Pointer to the destination array.
1147 * @return @a hi.
1149 virtual const char_type*
1150 do_narrow(const char_type* __lo, const char_type* __hi,
1151 char, char* __dest) const
1153 memcpy(__dest, __lo, __hi - __lo);
1154 return __hi;
1157 private:
1159 void _M_widen_init() const
1161 char __tmp[sizeof(_M_widen)];
1162 for (size_t __i = 0; __i < sizeof(_M_widen); ++__i)
1163 __tmp[__i] = __i;
1164 do_widen(__tmp, __tmp + sizeof(__tmp), _M_widen);
1166 _M_widen_ok = 1;
1167 // Set _M_widen_ok to 2 if memcpy can't be used.
1168 if (memcmp(__tmp, _M_widen, sizeof(_M_widen)))
1169 _M_widen_ok = 2;
1172 // Fill in the narrowing cache and flag whether all values are
1173 // valid or not. _M_narrow_ok is set to 2 if memcpy can't
1174 // be used.
1175 void _M_narrow_init() const
1177 char __tmp[sizeof(_M_narrow)];
1178 for (size_t __i = 0; __i < sizeof(_M_narrow); ++__i)
1179 __tmp[__i] = __i;
1180 do_narrow(__tmp, __tmp + sizeof(__tmp), 0, _M_narrow);
1182 _M_narrow_ok = 1;
1183 if (memcmp(__tmp, _M_narrow, sizeof(_M_narrow)))
1184 _M_narrow_ok = 2;
1185 else
1187 // Deal with the special case of zero: renarrow with a
1188 // different default and compare.
1189 char __c;
1190 do_narrow(__tmp, __tmp + 1, 1, &__c);
1191 if (__c == 1)
1192 _M_narrow_ok = 2;
1197 template<>
1198 const ctype<char>&
1199 use_facet<ctype<char> >(const locale& __loc);
1201 #ifdef _GLIBCXX_USE_WCHAR_T
1202 // 22.2.1.3 ctype<wchar_t> specialization
1204 * @brief The ctype<wchar_t> specialization.
1206 * This class defines classification and conversion functions for the
1207 * wchar_t type. It gets used by wchar_t streams for many I/O operations.
1208 * The wchar_t specialization provides a number of optimizations as well.
1210 * ctype<wchar_t> inherits its public methods from
1211 * __ctype_abstract_base<wchar_t>.
1213 template<>
1214 class ctype<wchar_t> : public __ctype_abstract_base<wchar_t>
1216 public:
1217 // Types:
1218 /// Typedef for the template parameter wchar_t.
1219 typedef wchar_t char_type;
1220 typedef wctype_t __wmask_type;
1222 protected:
1223 __c_locale _M_c_locale_ctype;
1225 // Pre-computed narrowed and widened chars.
1226 bool _M_narrow_ok;
1227 char _M_narrow[128];
1228 wint_t _M_widen[1 + static_cast<unsigned char>(-1)];
1230 // Pre-computed elements for do_is.
1231 mask _M_bit[16];
1232 __wmask_type _M_wmask[16];
1234 public:
1235 // Data Members:
1236 /// The facet id for ctype<wchar_t>
1237 static locale::id id;
1240 * @brief Constructor performs initialization.
1242 * This is the constructor provided by the standard.
1244 * @param refs Passed to the base facet class.
1246 explicit
1247 ctype(size_t __refs = 0);
1250 * @brief Constructor performs static initialization.
1252 * This constructor is used to construct the initial C locale facet.
1254 * @param cloc Handle to C locale data.
1255 * @param refs Passed to the base facet class.
1257 explicit
1258 ctype(__c_locale __cloc, size_t __refs = 0);
1260 protected:
1261 __wmask_type
1262 _M_convert_to_wmask(const mask __m) const;
1264 /// Destructor
1265 virtual
1266 ~ctype();
1269 * @brief Test wchar_t classification.
1271 * This function finds a mask M for @a c and compares it to mask @a m.
1273 * do_is() is a hook for a derived facet to change the behavior of
1274 * classifying. do_is() must always return the same result for the
1275 * same input.
1277 * @param c The wchar_t to find the mask of.
1278 * @param m The mask to compare against.
1279 * @return (M & m) != 0.
1281 virtual bool
1282 do_is(mask __m, char_type __c) const;
1285 * @brief Return a mask array.
1287 * This function finds the mask for each wchar_t in the range [lo,hi)
1288 * and successively writes it to vec. vec must have as many elements
1289 * as the input.
1291 * do_is() is a hook for a derived facet to change the behavior of
1292 * classifying. do_is() must always return the same result for the
1293 * same input.
1295 * @param lo Pointer to start of range.
1296 * @param hi Pointer to end of range.
1297 * @param vec Pointer to an array of mask storage.
1298 * @return @a hi.
1300 virtual const char_type*
1301 do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const;
1304 * @brief Find wchar_t matching mask
1306 * This function searches for and returns the first wchar_t c in
1307 * [lo,hi) for which is(m,c) is true.
1309 * do_scan_is() is a hook for a derived facet to change the behavior of
1310 * match searching. do_is() must always return the same result for the
1311 * same input.
1313 * @param m The mask to compare against.
1314 * @param lo Pointer to start of range.
1315 * @param hi Pointer to end of range.
1316 * @return Pointer to a matching wchar_t if found, else @a hi.
1318 virtual const char_type*
1319 do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const;
1322 * @brief Find wchar_t not matching mask
1324 * This function searches for and returns a pointer to the first
1325 * wchar_t c of [lo,hi) for which is(m,c) is false.
1327 * do_scan_is() is a hook for a derived facet to change the behavior of
1328 * match searching. do_is() must always return the same result for the
1329 * same input.
1331 * @param m The mask to compare against.
1332 * @param lo Pointer to start of range.
1333 * @param hi Pointer to end of range.
1334 * @return Pointer to a non-matching wchar_t if found, else @a hi.
1336 virtual const char_type*
1337 do_scan_not(mask __m, const char_type* __lo,
1338 const char_type* __hi) const;
1341 * @brief Convert to uppercase.
1343 * This virtual function converts the wchar_t argument to uppercase if
1344 * possible. If not possible (for example, '2'), returns the argument.
1346 * do_toupper() is a hook for a derived facet to change the behavior of
1347 * uppercasing. do_toupper() must always return the same result for
1348 * the same input.
1350 * @param c The wchar_t to convert.
1351 * @return The uppercase wchar_t if convertible, else @a c.
1353 virtual char_type
1354 do_toupper(char_type) const;
1357 * @brief Convert array to uppercase.
1359 * This virtual function converts each wchar_t in the range [lo,hi) to
1360 * uppercase if possible. Other elements remain untouched.
1362 * do_toupper() is a hook for a derived facet to change the behavior of
1363 * uppercasing. do_toupper() must always return the same result for
1364 * the same input.
1366 * @param lo Pointer to start of range.
1367 * @param hi Pointer to end of range.
1368 * @return @a hi.
1370 virtual const char_type*
1371 do_toupper(char_type* __lo, const char_type* __hi) const;
1374 * @brief Convert to lowercase.
1376 * This virtual function converts the argument to lowercase if
1377 * possible. If not possible (for example, '2'), returns the argument.
1379 * do_tolower() is a hook for a derived facet to change the behavior of
1380 * lowercasing. do_tolower() must always return the same result for
1381 * the same input.
1383 * @param c The wchar_t to convert.
1384 * @return The lowercase wchar_t if convertible, else @a c.
1386 virtual char_type
1387 do_tolower(char_type) const;
1390 * @brief Convert array to lowercase.
1392 * This virtual function converts each wchar_t in the range [lo,hi) to
1393 * lowercase if possible. Other elements remain untouched.
1395 * do_tolower() is a hook for a derived facet to change the behavior of
1396 * lowercasing. do_tolower() must always return the same result for
1397 * the same input.
1399 * @param lo Pointer to start of range.
1400 * @param hi Pointer to end of range.
1401 * @return @a hi.
1403 virtual const char_type*
1404 do_tolower(char_type* __lo, const char_type* __hi) const;
1407 * @brief Widen char to wchar_t
1409 * This virtual function converts the char to wchar_t using the
1410 * simplest reasonable transformation. For an underived ctype<wchar_t>
1411 * facet, the argument will be cast to wchar_t.
1413 * do_widen() is a hook for a derived facet to change the behavior of
1414 * widening. do_widen() must always return the same result for the
1415 * same input.
1417 * Note: this is not what you want for codepage conversions. See
1418 * codecvt for that.
1420 * @param c The char to convert.
1421 * @return The converted wchar_t.
1423 virtual char_type
1424 do_widen(char) const;
1427 * @brief Widen char array to wchar_t array
1429 * This function converts each char in the input to wchar_t using the
1430 * simplest reasonable transformation. For an underived ctype<wchar_t>
1431 * facet, the argument will be copied, casting each element to wchar_t.
1433 * do_widen() is a hook for a derived facet to change the behavior of
1434 * widening. do_widen() must always return the same result for the
1435 * same input.
1437 * Note: this is not what you want for codepage conversions. See
1438 * codecvt for that.
1440 * @param lo Pointer to start range.
1441 * @param hi Pointer to end of range.
1442 * @param to Pointer to the destination array.
1443 * @return @a hi.
1445 virtual const char*
1446 do_widen(const char* __lo, const char* __hi, char_type* __dest) const;
1449 * @brief Narrow wchar_t to char
1451 * This virtual function converts the argument to char using
1452 * the simplest reasonable transformation. If the conversion
1453 * fails, dfault is returned instead. For an underived
1454 * ctype<wchar_t> facet, @a c will be cast to char and
1455 * returned.
1457 * do_narrow() is a hook for a derived facet to change the
1458 * behavior of narrowing. do_narrow() must always return the
1459 * same result for the same input.
1461 * Note: this is not what you want for codepage conversions. See
1462 * codecvt for that.
1464 * @param c The wchar_t to convert.
1465 * @param dfault Char to return if conversion fails.
1466 * @return The converted char.
1468 virtual char
1469 do_narrow(char_type, char __dfault) const;
1472 * @brief Narrow wchar_t array to char array
1474 * This virtual function converts each wchar_t in the range [lo,hi) to
1475 * char using the simplest reasonable transformation and writes the
1476 * results to the destination array. For any wchar_t in the input that
1477 * cannot be converted, @a dfault is used instead. For an underived
1478 * ctype<wchar_t> facet, the argument will be copied, casting each
1479 * element to char.
1481 * do_narrow() is a hook for a derived facet to change the behavior of
1482 * narrowing. do_narrow() must always return the same result for the
1483 * same input.
1485 * Note: this is not what you want for codepage conversions. See
1486 * codecvt for that.
1488 * @param lo Pointer to start of range.
1489 * @param hi Pointer to end of range.
1490 * @param dfault Char to use if conversion fails.
1491 * @param to Pointer to the destination array.
1492 * @return @a hi.
1494 virtual const char_type*
1495 do_narrow(const char_type* __lo, const char_type* __hi,
1496 char __dfault, char* __dest) const;
1498 // For use at construction time only.
1499 void
1500 _M_initialize_ctype();
1503 template<>
1504 const ctype<wchar_t>&
1505 use_facet<ctype<wchar_t> >(const locale& __loc);
1506 #endif //_GLIBCXX_USE_WCHAR_T
1508 /// @brief class ctype_byname [22.2.1.2].
1509 template<typename _CharT>
1510 class ctype_byname : public ctype<_CharT>
1512 public:
1513 typedef _CharT char_type;
1515 explicit
1516 ctype_byname(const char* __s, size_t __refs = 0);
1518 protected:
1519 virtual
1520 ~ctype_byname() { };
1523 /// 22.2.1.4 Class ctype_byname specializations.
1524 template<>
1525 ctype_byname<char>::ctype_byname(const char*, size_t refs);
1527 template<>
1528 ctype_byname<wchar_t>::ctype_byname(const char*, size_t refs);
1530 _GLIBCXX_END_NAMESPACE
1532 // Include host and configuration specific ctype inlines.
1533 #include <bits/ctype_inline.h>
1535 // 22.2.1.5 Template class codecvt
1536 #include <bits/codecvt.h>
1538 _GLIBCXX_BEGIN_NAMESPACE(std)
1540 // 22.2.2 The numeric category.
1541 class __num_base
1543 public:
1544 // NB: Code depends on the order of _S_atoms_out elements.
1545 // Below are the indices into _S_atoms_out.
1546 enum
1548 _S_ominus,
1549 _S_oplus,
1550 _S_ox,
1551 _S_oX,
1552 _S_odigits,
1553 _S_odigits_end = _S_odigits + 16,
1554 _S_oudigits = _S_odigits_end,
1555 _S_oudigits_end = _S_oudigits + 16,
1556 _S_oe = _S_odigits + 14, // For scientific notation, 'e'
1557 _S_oE = _S_oudigits + 14, // For scientific notation, 'E'
1558 _S_oend = _S_oudigits_end
1561 // A list of valid numeric literals for output. This array
1562 // contains chars that will be passed through the current locale's
1563 // ctype<_CharT>.widen() and then used to render numbers.
1564 // For the standard "C" locale, this is
1565 // "-+xX0123456789abcdef0123456789ABCDEF".
1566 static const char* _S_atoms_out;
1568 // String literal of acceptable (narrow) input, for num_get.
1569 // "-+xX0123456789abcdefABCDEF"
1570 static const char* _S_atoms_in;
1572 enum
1574 _S_iminus,
1575 _S_iplus,
1576 _S_ix,
1577 _S_iX,
1578 _S_izero,
1579 _S_ie = _S_izero + 14,
1580 _S_iE = _S_izero + 20,
1581 _S_iend = 26
1584 // num_put
1585 // Construct and return valid scanf format for floating point types.
1586 static void
1587 _S_format_float(const ios_base& __io, char* __fptr, char __mod);
1590 template<typename _CharT>
1591 struct __numpunct_cache : public locale::facet
1593 const char* _M_grouping;
1594 size_t _M_grouping_size;
1595 bool _M_use_grouping;
1596 const _CharT* _M_truename;
1597 size_t _M_truename_size;
1598 const _CharT* _M_falsename;
1599 size_t _M_falsename_size;
1600 _CharT _M_decimal_point;
1601 _CharT _M_thousands_sep;
1603 // A list of valid numeric literals for output: in the standard
1604 // "C" locale, this is "-+xX0123456789abcdef0123456789ABCDEF".
1605 // This array contains the chars after having been passed
1606 // through the current locale's ctype<_CharT>.widen().
1607 _CharT _M_atoms_out[__num_base::_S_oend];
1609 // A list of valid numeric literals for input: in the standard
1610 // "C" locale, this is "-+xX0123456789abcdefABCDEF"
1611 // This array contains the chars after having been passed
1612 // through the current locale's ctype<_CharT>.widen().
1613 _CharT _M_atoms_in[__num_base::_S_iend];
1615 bool _M_allocated;
1617 __numpunct_cache(size_t __refs = 0) : facet(__refs),
1618 _M_grouping(NULL), _M_grouping_size(0), _M_use_grouping(false),
1619 _M_truename(NULL), _M_truename_size(0), _M_falsename(NULL),
1620 _M_falsename_size(0), _M_decimal_point(_CharT()),
1621 _M_thousands_sep(_CharT()), _M_allocated(false)
1624 ~__numpunct_cache();
1626 void
1627 _M_cache(const locale& __loc);
1629 private:
1630 __numpunct_cache&
1631 operator=(const __numpunct_cache&);
1633 explicit
1634 __numpunct_cache(const __numpunct_cache&);
1637 template<typename _CharT>
1638 __numpunct_cache<_CharT>::~__numpunct_cache()
1640 if (_M_allocated)
1642 delete [] _M_grouping;
1643 delete [] _M_truename;
1644 delete [] _M_falsename;
1649 * @brief Numpunct facet.
1651 * This facet stores several pieces of information related to printing and
1652 * scanning numbers, such as the decimal point character. It takes a
1653 * template parameter specifying the char type. The numpunct facet is
1654 * used by streams for many I/O operations involving numbers.
1656 * The numpunct template uses protected virtual functions to provide the
1657 * actual results. The public accessors forward the call to the virtual
1658 * functions. These virtual functions are hooks for developers to
1659 * implement the behavior they require from a numpunct facet.
1661 template<typename _CharT>
1662 class numpunct : public locale::facet
1664 public:
1665 // Types:
1666 //@{
1667 /// Public typedefs
1668 typedef _CharT char_type;
1669 typedef basic_string<_CharT> string_type;
1670 //@}
1671 typedef __numpunct_cache<_CharT> __cache_type;
1673 protected:
1674 __cache_type* _M_data;
1676 public:
1677 /// Numpunct facet id.
1678 static locale::id id;
1681 * @brief Numpunct constructor.
1683 * @param refs Refcount to pass to the base class.
1685 explicit
1686 numpunct(size_t __refs = 0) : facet(__refs), _M_data(NULL)
1687 { _M_initialize_numpunct(); }
1690 * @brief Internal constructor. Not for general use.
1692 * This is a constructor for use by the library itself to set up the
1693 * predefined locale facets.
1695 * @param cache __numpunct_cache object.
1696 * @param refs Refcount to pass to the base class.
1698 explicit
1699 numpunct(__cache_type* __cache, size_t __refs = 0)
1700 : facet(__refs), _M_data(__cache)
1701 { _M_initialize_numpunct(); }
1704 * @brief Internal constructor. Not for general use.
1706 * This is a constructor for use by the library itself to set up new
1707 * locales.
1709 * @param cloc The "C" locale.
1710 * @param refs Refcount to pass to the base class.
1712 explicit
1713 numpunct(__c_locale __cloc, size_t __refs = 0)
1714 : facet(__refs), _M_data(NULL)
1715 { _M_initialize_numpunct(__cloc); }
1718 * @brief Return decimal point character.
1720 * This function returns a char_type to use as a decimal point. It
1721 * does so by returning returning
1722 * numpunct<char_type>::do_decimal_point().
1724 * @return @a char_type representing a decimal point.
1726 char_type
1727 decimal_point() const
1728 { return this->do_decimal_point(); }
1731 * @brief Return thousands separator character.
1733 * This function returns a char_type to use as a thousands
1734 * separator. It does so by returning returning
1735 * numpunct<char_type>::do_thousands_sep().
1737 * @return char_type representing a thousands separator.
1739 char_type
1740 thousands_sep() const
1741 { return this->do_thousands_sep(); }
1744 * @brief Return grouping specification.
1746 * This function returns a string representing groupings for the
1747 * integer part of a number. Groupings indicate where thousands
1748 * separators should be inserted in the integer part of a number.
1750 * Each char in the return string is interpret as an integer
1751 * rather than a character. These numbers represent the number
1752 * of digits in a group. The first char in the string
1753 * represents the number of digits in the least significant
1754 * group. If a char is negative, it indicates an unlimited
1755 * number of digits for the group. If more chars from the
1756 * string are required to group a number, the last char is used
1757 * repeatedly.
1759 * For example, if the grouping() returns "\003\002" and is
1760 * applied to the number 123456789, this corresponds to
1761 * 12,34,56,789. Note that if the string was "32", this would
1762 * put more than 50 digits into the least significant group if
1763 * the character set is ASCII.
1765 * The string is returned by calling
1766 * numpunct<char_type>::do_grouping().
1768 * @return string representing grouping specification.
1770 string
1771 grouping() const
1772 { return this->do_grouping(); }
1775 * @brief Return string representation of bool true.
1777 * This function returns a string_type containing the text
1778 * representation for true bool variables. It does so by calling
1779 * numpunct<char_type>::do_truename().
1781 * @return string_type representing printed form of true.
1783 string_type
1784 truename() const
1785 { return this->do_truename(); }
1788 * @brief Return string representation of bool false.
1790 * This function returns a string_type containing the text
1791 * representation for false bool variables. It does so by calling
1792 * numpunct<char_type>::do_falsename().
1794 * @return string_type representing printed form of false.
1796 string_type
1797 falsename() const
1798 { return this->do_falsename(); }
1800 protected:
1801 /// Destructor.
1802 virtual
1803 ~numpunct();
1806 * @brief Return decimal point character.
1808 * Returns a char_type to use as a decimal point. This function is a
1809 * hook for derived classes to change the value returned.
1811 * @return @a char_type representing a decimal point.
1813 virtual char_type
1814 do_decimal_point() const
1815 { return _M_data->_M_decimal_point; }
1818 * @brief Return thousands separator character.
1820 * Returns a char_type to use as a thousands separator. This function
1821 * is a hook for derived classes to change the value returned.
1823 * @return @a char_type representing a thousands separator.
1825 virtual char_type
1826 do_thousands_sep() const
1827 { return _M_data->_M_thousands_sep; }
1830 * @brief Return grouping specification.
1832 * Returns a string representing groupings for the integer part of a
1833 * number. This function is a hook for derived classes to change the
1834 * value returned. @see grouping() for details.
1836 * @return String representing grouping specification.
1838 virtual string
1839 do_grouping() const
1840 { return _M_data->_M_grouping; }
1843 * @brief Return string representation of bool true.
1845 * Returns a string_type containing the text representation for true
1846 * bool variables. This function is a hook for derived classes to
1847 * change the value returned.
1849 * @return string_type representing printed form of true.
1851 virtual string_type
1852 do_truename() const
1853 { return _M_data->_M_truename; }
1856 * @brief Return string representation of bool false.
1858 * Returns a string_type containing the text representation for false
1859 * bool variables. This function is a hook for derived classes to
1860 * change the value returned.
1862 * @return string_type representing printed form of false.
1864 virtual string_type
1865 do_falsename() const
1866 { return _M_data->_M_falsename; }
1868 // For use at construction time only.
1869 void
1870 _M_initialize_numpunct(__c_locale __cloc = NULL);
1873 template<typename _CharT>
1874 locale::id numpunct<_CharT>::id;
1876 template<>
1877 numpunct<char>::~numpunct();
1879 template<>
1880 void
1881 numpunct<char>::_M_initialize_numpunct(__c_locale __cloc);
1883 #ifdef _GLIBCXX_USE_WCHAR_T
1884 template<>
1885 numpunct<wchar_t>::~numpunct();
1887 template<>
1888 void
1889 numpunct<wchar_t>::_M_initialize_numpunct(__c_locale __cloc);
1890 #endif
1892 /// @brief class numpunct_byname [22.2.3.2].
1893 template<typename _CharT>
1894 class numpunct_byname : public numpunct<_CharT>
1896 public:
1897 typedef _CharT char_type;
1898 typedef basic_string<_CharT> string_type;
1900 explicit
1901 numpunct_byname(const char* __s, size_t __refs = 0)
1902 : numpunct<_CharT>(__refs)
1904 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
1906 __c_locale __tmp;
1907 this->_S_create_c_locale(__tmp, __s);
1908 this->_M_initialize_numpunct(__tmp);
1909 this->_S_destroy_c_locale(__tmp);
1913 protected:
1914 virtual
1915 ~numpunct_byname() { }
1918 _GLIBCXX_BEGIN_LDBL_NAMESPACE
1920 * @brief Facet for parsing number strings.
1922 * This facet encapsulates the code to parse and return a number
1923 * from a string. It is used by the istream numeric extraction
1924 * operators.
1926 * The num_get template uses protected virtual functions to provide the
1927 * actual results. The public accessors forward the call to the virtual
1928 * functions. These virtual functions are hooks for developers to
1929 * implement the behavior they require from the num_get facet.
1931 template<typename _CharT, typename _InIter>
1932 class num_get : public locale::facet
1934 public:
1935 // Types:
1936 //@{
1937 /// Public typedefs
1938 typedef _CharT char_type;
1939 typedef _InIter iter_type;
1940 //@}
1942 /// Numpunct facet id.
1943 static locale::id id;
1946 * @brief Constructor performs initialization.
1948 * This is the constructor provided by the standard.
1950 * @param refs Passed to the base facet class.
1952 explicit
1953 num_get(size_t __refs = 0) : facet(__refs) { }
1956 * @brief Numeric parsing.
1958 * Parses the input stream into the bool @a v. It does so by calling
1959 * num_get::do_get().
1961 * If ios_base::boolalpha is set, attempts to read
1962 * ctype<CharT>::truename() or ctype<CharT>::falsename(). Sets
1963 * @a v to true or false if successful. Sets err to
1964 * ios_base::failbit if reading the string fails. Sets err to
1965 * ios_base::eofbit if the stream is emptied.
1967 * If ios_base::boolalpha is not set, proceeds as with reading a long,
1968 * except if the value is 1, sets @a v to true, if the value is 0, sets
1969 * @a v to false, and otherwise set err to ios_base::failbit.
1971 * @param in Start of input stream.
1972 * @param end End of input stream.
1973 * @param io Source of locale and flags.
1974 * @param err Error flags to set.
1975 * @param v Value to format and insert.
1976 * @return Iterator after reading.
1978 iter_type
1979 get(iter_type __in, iter_type __end, ios_base& __io,
1980 ios_base::iostate& __err, bool& __v) const
1981 { return this->do_get(__in, __end, __io, __err, __v); }
1983 //@{
1985 * @brief Numeric parsing.
1987 * Parses the input stream into the integral variable @a v. It does so
1988 * by calling num_get::do_get().
1990 * Parsing is affected by the flag settings in @a io.
1992 * The basic parse is affected by the value of io.flags() &
1993 * ios_base::basefield. If equal to ios_base::oct, parses like the
1994 * scanf %o specifier. Else if equal to ios_base::hex, parses like %X
1995 * specifier. Else if basefield equal to 0, parses like the %i
1996 * specifier. Otherwise, parses like %d for signed and %u for unsigned
1997 * types. The matching type length modifier is also used.
1999 * Digit grouping is intrepreted according to numpunct::grouping() and
2000 * numpunct::thousands_sep(). If the pattern of digit groups isn't
2001 * consistent, sets err to ios_base::failbit.
2003 * If parsing the string yields a valid value for @a v, @a v is set.
2004 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
2005 * Sets err to ios_base::eofbit if the stream is emptied.
2007 * @param in Start of input stream.
2008 * @param end End of input stream.
2009 * @param io Source of locale and flags.
2010 * @param err Error flags to set.
2011 * @param v Value to format and insert.
2012 * @return Iterator after reading.
2014 iter_type
2015 get(iter_type __in, iter_type __end, ios_base& __io,
2016 ios_base::iostate& __err, long& __v) const
2017 { return this->do_get(__in, __end, __io, __err, __v); }
2019 iter_type
2020 get(iter_type __in, iter_type __end, ios_base& __io,
2021 ios_base::iostate& __err, unsigned short& __v) const
2022 { return this->do_get(__in, __end, __io, __err, __v); }
2024 iter_type
2025 get(iter_type __in, iter_type __end, ios_base& __io,
2026 ios_base::iostate& __err, unsigned int& __v) const
2027 { return this->do_get(__in, __end, __io, __err, __v); }
2029 iter_type
2030 get(iter_type __in, iter_type __end, ios_base& __io,
2031 ios_base::iostate& __err, unsigned long& __v) const
2032 { return this->do_get(__in, __end, __io, __err, __v); }
2034 #ifdef _GLIBCXX_USE_LONG_LONG
2035 iter_type
2036 get(iter_type __in, iter_type __end, ios_base& __io,
2037 ios_base::iostate& __err, long long& __v) const
2038 { return this->do_get(__in, __end, __io, __err, __v); }
2040 iter_type
2041 get(iter_type __in, iter_type __end, ios_base& __io,
2042 ios_base::iostate& __err, unsigned long long& __v) const
2043 { return this->do_get(__in, __end, __io, __err, __v); }
2044 #endif
2045 //@}
2047 //@{
2049 * @brief Numeric parsing.
2051 * Parses the input stream into the integral variable @a v. It does so
2052 * by calling num_get::do_get().
2054 * The input characters are parsed like the scanf %g specifier. The
2055 * matching type length modifier is also used.
2057 * The decimal point character used is numpunct::decimal_point().
2058 * Digit grouping is intrepreted according to numpunct::grouping() and
2059 * numpunct::thousands_sep(). If the pattern of digit groups isn't
2060 * consistent, sets err to ios_base::failbit.
2062 * If parsing the string yields a valid value for @a v, @a v is set.
2063 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
2064 * Sets err to ios_base::eofbit if the stream is emptied.
2066 * @param in Start of input stream.
2067 * @param end End of input stream.
2068 * @param io Source of locale and flags.
2069 * @param err Error flags to set.
2070 * @param v Value to format and insert.
2071 * @return Iterator after reading.
2073 iter_type
2074 get(iter_type __in, iter_type __end, ios_base& __io,
2075 ios_base::iostate& __err, float& __v) const
2076 { return this->do_get(__in, __end, __io, __err, __v); }
2078 iter_type
2079 get(iter_type __in, iter_type __end, ios_base& __io,
2080 ios_base::iostate& __err, double& __v) const
2081 { return this->do_get(__in, __end, __io, __err, __v); }
2083 iter_type
2084 get(iter_type __in, iter_type __end, ios_base& __io,
2085 ios_base::iostate& __err, long double& __v) const
2086 { return this->do_get(__in, __end, __io, __err, __v); }
2087 //@}
2090 * @brief Numeric parsing.
2092 * Parses the input stream into the pointer variable @a v. It does so
2093 * by calling num_get::do_get().
2095 * The input characters are parsed like the scanf %p specifier.
2097 * Digit grouping is intrepreted according to numpunct::grouping() and
2098 * numpunct::thousands_sep(). If the pattern of digit groups isn't
2099 * consistent, sets err to ios_base::failbit.
2101 * Note that the digit grouping effect for pointers is a bit ambiguous
2102 * in the standard and shouldn't be relied on. See DR 344.
2104 * If parsing the string yields a valid value for @a v, @a v is set.
2105 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
2106 * Sets err to ios_base::eofbit if the stream is emptied.
2108 * @param in Start of input stream.
2109 * @param end End of input stream.
2110 * @param io Source of locale and flags.
2111 * @param err Error flags to set.
2112 * @param v Value to format and insert.
2113 * @return Iterator after reading.
2115 iter_type
2116 get(iter_type __in, iter_type __end, ios_base& __io,
2117 ios_base::iostate& __err, void*& __v) const
2118 { return this->do_get(__in, __end, __io, __err, __v); }
2120 protected:
2121 /// Destructor.
2122 virtual ~num_get() { }
2124 iter_type
2125 _M_extract_float(iter_type, iter_type, ios_base&, ios_base::iostate&,
2126 string& __xtrc) const;
2128 template<typename _ValueT>
2129 iter_type
2130 _M_extract_int(iter_type, iter_type, ios_base&, ios_base::iostate&,
2131 _ValueT& __v) const;
2133 template<typename _CharT2>
2134 typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, int>::__type
2135 _M_find(const _CharT2*, size_t __len, _CharT2 __c) const
2137 int __ret = -1;
2138 if (__len <= 10)
2140 if (__c >= _CharT2('0') && __c < _CharT2(_CharT2('0') + __len))
2141 __ret = __c - _CharT2('0');
2143 else
2145 if (__c >= _CharT2('0') && __c <= _CharT2('9'))
2146 __ret = __c - _CharT2('0');
2147 else if (__c >= _CharT2('a') && __c <= _CharT2('f'))
2148 __ret = 10 + (__c - _CharT2('a'));
2149 else if (__c >= _CharT2('A') && __c <= _CharT2('F'))
2150 __ret = 10 + (__c - _CharT2('A'));
2152 return __ret;
2155 template<typename _CharT2>
2156 typename __gnu_cxx::__enable_if<!__is_char<_CharT2>::__value,
2157 int>::__type
2158 _M_find(const _CharT2* __zero, size_t __len, _CharT2 __c) const
2160 int __ret = -1;
2161 const char_type* __q = char_traits<_CharT2>::find(__zero, __len, __c);
2162 if (__q)
2164 __ret = __q - __zero;
2165 if (__ret > 15)
2166 __ret -= 6;
2168 return __ret;
2171 //@{
2173 * @brief Numeric parsing.
2175 * Parses the input stream into the variable @a v. This function is a
2176 * hook for derived classes to change the value returned. @see get()
2177 * for more details.
2179 * @param in Start of input stream.
2180 * @param end End of input stream.
2181 * @param io Source of locale and flags.
2182 * @param err Error flags to set.
2183 * @param v Value to format and insert.
2184 * @return Iterator after reading.
2186 virtual iter_type
2187 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, bool&) const;
2190 virtual iter_type
2191 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, long&) const;
2193 virtual iter_type
2194 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2195 unsigned short&) const;
2197 virtual iter_type
2198 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2199 unsigned int&) const;
2201 virtual iter_type
2202 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2203 unsigned long&) const;
2205 #ifdef _GLIBCXX_USE_LONG_LONG
2206 virtual iter_type
2207 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2208 long long&) const;
2210 virtual iter_type
2211 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2212 unsigned long long&) const;
2213 #endif
2215 virtual iter_type
2216 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2217 float&) const;
2219 virtual iter_type
2220 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2221 double&) const;
2223 // XXX GLIBCXX_ABI Deprecated
2224 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2225 virtual iter_type
2226 __do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2227 double&) const;
2228 #else
2229 virtual iter_type
2230 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2231 long double&) const;
2232 #endif
2234 virtual iter_type
2235 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2236 void*&) const;
2238 // XXX GLIBCXX_ABI Deprecated
2239 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2240 virtual iter_type
2241 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2242 long double&) const;
2243 #endif
2244 //@}
2247 template<typename _CharT, typename _InIter>
2248 locale::id num_get<_CharT, _InIter>::id;
2252 * @brief Facet for converting numbers to strings.
2254 * This facet encapsulates the code to convert a number to a string. It is
2255 * used by the ostream numeric insertion operators.
2257 * The num_put template uses protected virtual functions to provide the
2258 * actual results. The public accessors forward the call to the virtual
2259 * functions. These virtual functions are hooks for developers to
2260 * implement the behavior they require from the num_put facet.
2262 template<typename _CharT, typename _OutIter>
2263 class num_put : public locale::facet
2265 public:
2266 // Types:
2267 //@{
2268 /// Public typedefs
2269 typedef _CharT char_type;
2270 typedef _OutIter iter_type;
2271 //@}
2273 /// Numpunct facet id.
2274 static locale::id id;
2277 * @brief Constructor performs initialization.
2279 * This is the constructor provided by the standard.
2281 * @param refs Passed to the base facet class.
2283 explicit
2284 num_put(size_t __refs = 0) : facet(__refs) { }
2287 * @brief Numeric formatting.
2289 * Formats the boolean @a v and inserts it into a stream. It does so
2290 * by calling num_put::do_put().
2292 * If ios_base::boolalpha is set, writes ctype<CharT>::truename() or
2293 * ctype<CharT>::falsename(). Otherwise formats @a v as an int.
2295 * @param s Stream to write to.
2296 * @param io Source of locale and flags.
2297 * @param fill Char_type to use for filling.
2298 * @param v Value to format and insert.
2299 * @return Iterator after writing.
2301 iter_type
2302 put(iter_type __s, ios_base& __f, char_type __fill, bool __v) const
2303 { return this->do_put(__s, __f, __fill, __v); }
2305 //@{
2307 * @brief Numeric formatting.
2309 * Formats the integral value @a v and inserts it into a
2310 * stream. It does so by calling num_put::do_put().
2312 * Formatting is affected by the flag settings in @a io.
2314 * The basic format is affected by the value of io.flags() &
2315 * ios_base::basefield. If equal to ios_base::oct, formats like the
2316 * printf %o specifier. Else if equal to ios_base::hex, formats like
2317 * %x or %X with ios_base::uppercase unset or set respectively.
2318 * Otherwise, formats like %d, %ld, %lld for signed and %u, %lu, %llu
2319 * for unsigned values. Note that if both oct and hex are set, neither
2320 * will take effect.
2322 * If ios_base::showpos is set, '+' is output before positive values.
2323 * If ios_base::showbase is set, '0' precedes octal values (except 0)
2324 * and '0[xX]' precedes hex values.
2326 * Thousands separators are inserted according to numpunct::grouping()
2327 * and numpunct::thousands_sep(). The decimal point character used is
2328 * numpunct::decimal_point().
2330 * If io.width() is non-zero, enough @a fill characters are inserted to
2331 * make the result at least that wide. If
2332 * (io.flags() & ios_base::adjustfield) == ios_base::left, result is
2333 * padded at the end. If ios_base::internal, then padding occurs
2334 * immediately after either a '+' or '-' or after '0x' or '0X'.
2335 * Otherwise, padding occurs at the beginning.
2337 * @param s Stream to write to.
2338 * @param io Source of locale and flags.
2339 * @param fill Char_type to use for filling.
2340 * @param v Value to format and insert.
2341 * @return Iterator after writing.
2343 iter_type
2344 put(iter_type __s, ios_base& __f, char_type __fill, long __v) const
2345 { return this->do_put(__s, __f, __fill, __v); }
2347 iter_type
2348 put(iter_type __s, ios_base& __f, char_type __fill,
2349 unsigned long __v) const
2350 { return this->do_put(__s, __f, __fill, __v); }
2352 #ifdef _GLIBCXX_USE_LONG_LONG
2353 iter_type
2354 put(iter_type __s, ios_base& __f, char_type __fill, long long __v) const
2355 { return this->do_put(__s, __f, __fill, __v); }
2357 iter_type
2358 put(iter_type __s, ios_base& __f, char_type __fill,
2359 unsigned long long __v) const
2360 { return this->do_put(__s, __f, __fill, __v); }
2361 #endif
2362 //@}
2364 //@{
2366 * @brief Numeric formatting.
2368 * Formats the floating point value @a v and inserts it into a stream.
2369 * It does so by calling num_put::do_put().
2371 * Formatting is affected by the flag settings in @a io.
2373 * The basic format is affected by the value of io.flags() &
2374 * ios_base::floatfield. If equal to ios_base::fixed, formats like the
2375 * printf %f specifier. Else if equal to ios_base::scientific, formats
2376 * like %e or %E with ios_base::uppercase unset or set respectively.
2377 * Otherwise, formats like %g or %G depending on uppercase. Note that
2378 * if both fixed and scientific are set, the effect will also be like
2379 * %g or %G.
2381 * The output precision is given by io.precision(). This precision is
2382 * capped at numeric_limits::digits10 + 2 (different for double and
2383 * long double). The default precision is 6.
2385 * If ios_base::showpos is set, '+' is output before positive values.
2386 * If ios_base::showpoint is set, a decimal point will always be
2387 * output.
2389 * Thousands separators are inserted according to numpunct::grouping()
2390 * and numpunct::thousands_sep(). The decimal point character used is
2391 * numpunct::decimal_point().
2393 * If io.width() is non-zero, enough @a fill characters are inserted to
2394 * make the result at least that wide. If
2395 * (io.flags() & ios_base::adjustfield) == ios_base::left, result is
2396 * padded at the end. If ios_base::internal, then padding occurs
2397 * immediately after either a '+' or '-' or after '0x' or '0X'.
2398 * Otherwise, padding occurs at the beginning.
2400 * @param s Stream to write to.
2401 * @param io Source of locale and flags.
2402 * @param fill Char_type to use for filling.
2403 * @param v Value to format and insert.
2404 * @return Iterator after writing.
2406 iter_type
2407 put(iter_type __s, ios_base& __f, char_type __fill, double __v) const
2408 { return this->do_put(__s, __f, __fill, __v); }
2410 iter_type
2411 put(iter_type __s, ios_base& __f, char_type __fill,
2412 long double __v) const
2413 { return this->do_put(__s, __f, __fill, __v); }
2414 //@}
2417 * @brief Numeric formatting.
2419 * Formats the pointer value @a v and inserts it into a stream. It
2420 * does so by calling num_put::do_put().
2422 * This function formats @a v as an unsigned long with ios_base::hex
2423 * and ios_base::showbase set.
2425 * @param s Stream to write to.
2426 * @param io Source of locale and flags.
2427 * @param fill Char_type to use for filling.
2428 * @param v Value to format and insert.
2429 * @return Iterator after writing.
2431 iter_type
2432 put(iter_type __s, ios_base& __f, char_type __fill,
2433 const void* __v) const
2434 { return this->do_put(__s, __f, __fill, __v); }
2436 protected:
2437 template<typename _ValueT>
2438 iter_type
2439 _M_insert_float(iter_type, ios_base& __io, char_type __fill,
2440 char __mod, _ValueT __v) const;
2442 void
2443 _M_group_float(const char* __grouping, size_t __grouping_size,
2444 char_type __sep, const char_type* __p, char_type* __new,
2445 char_type* __cs, int& __len) const;
2447 template<typename _ValueT>
2448 iter_type
2449 _M_insert_int(iter_type, ios_base& __io, char_type __fill,
2450 _ValueT __v) const;
2452 void
2453 _M_group_int(const char* __grouping, size_t __grouping_size,
2454 char_type __sep, ios_base& __io, char_type* __new,
2455 char_type* __cs, int& __len) const;
2457 void
2458 _M_pad(char_type __fill, streamsize __w, ios_base& __io,
2459 char_type* __new, const char_type* __cs, int& __len) const;
2461 /// Destructor.
2462 virtual
2463 ~num_put() { };
2465 //@{
2467 * @brief Numeric formatting.
2469 * These functions do the work of formatting numeric values and
2470 * inserting them into a stream. This function is a hook for derived
2471 * classes to change the value returned.
2473 * @param s Stream to write to.
2474 * @param io Source of locale and flags.
2475 * @param fill Char_type to use for filling.
2476 * @param v Value to format and insert.
2477 * @return Iterator after writing.
2479 virtual iter_type
2480 do_put(iter_type, ios_base&, char_type __fill, bool __v) const;
2482 virtual iter_type
2483 do_put(iter_type, ios_base&, char_type __fill, long __v) const;
2485 virtual iter_type
2486 do_put(iter_type, ios_base&, char_type __fill, unsigned long) const;
2488 #ifdef _GLIBCXX_USE_LONG_LONG
2489 virtual iter_type
2490 do_put(iter_type, ios_base&, char_type __fill, long long __v) const;
2492 virtual iter_type
2493 do_put(iter_type, ios_base&, char_type __fill, unsigned long long) const;
2494 #endif
2496 virtual iter_type
2497 do_put(iter_type, ios_base&, char_type __fill, double __v) const;
2499 // XXX GLIBCXX_ABI Deprecated
2500 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2501 virtual iter_type
2502 __do_put(iter_type, ios_base&, char_type __fill, double __v) const;
2503 #else
2504 virtual iter_type
2505 do_put(iter_type, ios_base&, char_type __fill, long double __v) const;
2506 #endif
2508 virtual iter_type
2509 do_put(iter_type, ios_base&, char_type __fill, const void* __v) const;
2511 // XXX GLIBCXX_ABI Deprecated
2512 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2513 virtual iter_type
2514 do_put(iter_type, ios_base&, char_type __fill, long double __v) const;
2515 #endif
2516 //@}
2519 template <typename _CharT, typename _OutIter>
2520 locale::id num_put<_CharT, _OutIter>::id;
2522 _GLIBCXX_END_LDBL_NAMESPACE
2525 * @brief Facet for localized string comparison.
2527 * This facet encapsulates the code to compare strings in a localized
2528 * manner.
2530 * The collate template uses protected virtual functions to provide
2531 * the actual results. The public accessors forward the call to
2532 * the virtual functions. These virtual functions are hooks for
2533 * developers to implement the behavior they require from the
2534 * collate facet.
2536 template<typename _CharT>
2537 class collate : public locale::facet
2539 public:
2540 // Types:
2541 //@{
2542 /// Public typedefs
2543 typedef _CharT char_type;
2544 typedef basic_string<_CharT> string_type;
2545 //@}
2547 protected:
2548 // Underlying "C" library locale information saved from
2549 // initialization, needed by collate_byname as well.
2550 __c_locale _M_c_locale_collate;
2552 public:
2553 /// Numpunct facet id.
2554 static locale::id id;
2557 * @brief Constructor performs initialization.
2559 * This is the constructor provided by the standard.
2561 * @param refs Passed to the base facet class.
2563 explicit
2564 collate(size_t __refs = 0)
2565 : facet(__refs), _M_c_locale_collate(_S_get_c_locale())
2569 * @brief Internal constructor. Not for general use.
2571 * This is a constructor for use by the library itself to set up new
2572 * locales.
2574 * @param cloc The "C" locale.
2575 * @param refs Passed to the base facet class.
2577 explicit
2578 collate(__c_locale __cloc, size_t __refs = 0)
2579 : facet(__refs), _M_c_locale_collate(_S_clone_c_locale(__cloc))
2583 * @brief Compare two strings.
2585 * This function compares two strings and returns the result by calling
2586 * collate::do_compare().
2588 * @param lo1 Start of string 1.
2589 * @param hi1 End of string 1.
2590 * @param lo2 Start of string 2.
2591 * @param hi2 End of string 2.
2592 * @return 1 if string1 > string2, -1 if string1 < string2, else 0.
2595 compare(const _CharT* __lo1, const _CharT* __hi1,
2596 const _CharT* __lo2, const _CharT* __hi2) const
2597 { return this->do_compare(__lo1, __hi1, __lo2, __hi2); }
2600 * @brief Transform string to comparable form.
2602 * This function is a wrapper for strxfrm functionality. It takes the
2603 * input string and returns a modified string that can be directly
2604 * compared to other transformed strings. In the "C" locale, this
2605 * function just returns a copy of the input string. In some other
2606 * locales, it may replace two chars with one, change a char for
2607 * another, etc. It does so by returning collate::do_transform().
2609 * @param lo Start of string.
2610 * @param hi End of string.
2611 * @return Transformed string_type.
2613 string_type
2614 transform(const _CharT* __lo, const _CharT* __hi) const
2615 { return this->do_transform(__lo, __hi); }
2618 * @brief Return hash of a string.
2620 * This function computes and returns a hash on the input string. It
2621 * does so by returning collate::do_hash().
2623 * @param lo Start of string.
2624 * @param hi End of string.
2625 * @return Hash value.
2627 long
2628 hash(const _CharT* __lo, const _CharT* __hi) const
2629 { return this->do_hash(__lo, __hi); }
2631 // Used to abstract out _CharT bits in virtual member functions, below.
2633 _M_compare(const _CharT*, const _CharT*) const;
2635 size_t
2636 _M_transform(_CharT*, const _CharT*, size_t) const;
2638 protected:
2639 /// Destructor.
2640 virtual
2641 ~collate()
2642 { _S_destroy_c_locale(_M_c_locale_collate); }
2645 * @brief Compare two strings.
2647 * This function is a hook for derived classes to change the value
2648 * returned. @see compare().
2650 * @param lo1 Start of string 1.
2651 * @param hi1 End of string 1.
2652 * @param lo2 Start of string 2.
2653 * @param hi2 End of string 2.
2654 * @return 1 if string1 > string2, -1 if string1 < string2, else 0.
2656 virtual int
2657 do_compare(const _CharT* __lo1, const _CharT* __hi1,
2658 const _CharT* __lo2, const _CharT* __hi2) const;
2661 * @brief Transform string to comparable form.
2663 * This function is a hook for derived classes to change the value
2664 * returned.
2666 * @param lo1 Start of string 1.
2667 * @param hi1 End of string 1.
2668 * @param lo2 Start of string 2.
2669 * @param hi2 End of string 2.
2670 * @return 1 if string1 > string2, -1 if string1 < string2, else 0.
2672 virtual string_type
2673 do_transform(const _CharT* __lo, const _CharT* __hi) const;
2676 * @brief Return hash of a string.
2678 * This function computes and returns a hash on the input string. This
2679 * function is a hook for derived classes to change the value returned.
2681 * @param lo Start of string.
2682 * @param hi End of string.
2683 * @return Hash value.
2685 virtual long
2686 do_hash(const _CharT* __lo, const _CharT* __hi) const;
2689 template<typename _CharT>
2690 locale::id collate<_CharT>::id;
2692 // Specializations.
2693 template<>
2695 collate<char>::_M_compare(const char*, const char*) const;
2697 template<>
2698 size_t
2699 collate<char>::_M_transform(char*, const char*, size_t) const;
2701 #ifdef _GLIBCXX_USE_WCHAR_T
2702 template<>
2704 collate<wchar_t>::_M_compare(const wchar_t*, const wchar_t*) const;
2706 template<>
2707 size_t
2708 collate<wchar_t>::_M_transform(wchar_t*, const wchar_t*, size_t) const;
2709 #endif
2711 /// @brief class collate_byname [22.2.4.2].
2712 template<typename _CharT>
2713 class collate_byname : public collate<_CharT>
2715 public:
2716 //@{
2717 /// Public typedefs
2718 typedef _CharT char_type;
2719 typedef basic_string<_CharT> string_type;
2720 //@}
2722 explicit
2723 collate_byname(const char* __s, size_t __refs = 0)
2724 : collate<_CharT>(__refs)
2726 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
2728 this->_S_destroy_c_locale(this->_M_c_locale_collate);
2729 this->_S_create_c_locale(this->_M_c_locale_collate, __s);
2733 protected:
2734 virtual
2735 ~collate_byname() { }
2740 * @brief Time format ordering data.
2742 * This class provides an enum representing different orderings of day,
2743 * month, and year.
2745 class time_base
2747 public:
2748 enum dateorder { no_order, dmy, mdy, ymd, ydm };
2751 template<typename _CharT>
2752 struct __timepunct_cache : public locale::facet
2754 // List of all known timezones, with GMT first.
2755 static const _CharT* _S_timezones[14];
2757 const _CharT* _M_date_format;
2758 const _CharT* _M_date_era_format;
2759 const _CharT* _M_time_format;
2760 const _CharT* _M_time_era_format;
2761 const _CharT* _M_date_time_format;
2762 const _CharT* _M_date_time_era_format;
2763 const _CharT* _M_am;
2764 const _CharT* _M_pm;
2765 const _CharT* _M_am_pm_format;
2767 // Day names, starting with "C"'s Sunday.
2768 const _CharT* _M_day1;
2769 const _CharT* _M_day2;
2770 const _CharT* _M_day3;
2771 const _CharT* _M_day4;
2772 const _CharT* _M_day5;
2773 const _CharT* _M_day6;
2774 const _CharT* _M_day7;
2776 // Abbreviated day names, starting with "C"'s Sun.
2777 const _CharT* _M_aday1;
2778 const _CharT* _M_aday2;
2779 const _CharT* _M_aday3;
2780 const _CharT* _M_aday4;
2781 const _CharT* _M_aday5;
2782 const _CharT* _M_aday6;
2783 const _CharT* _M_aday7;
2785 // Month names, starting with "C"'s January.
2786 const _CharT* _M_month01;
2787 const _CharT* _M_month02;
2788 const _CharT* _M_month03;
2789 const _CharT* _M_month04;
2790 const _CharT* _M_month05;
2791 const _CharT* _M_month06;
2792 const _CharT* _M_month07;
2793 const _CharT* _M_month08;
2794 const _CharT* _M_month09;
2795 const _CharT* _M_month10;
2796 const _CharT* _M_month11;
2797 const _CharT* _M_month12;
2799 // Abbreviated month names, starting with "C"'s Jan.
2800 const _CharT* _M_amonth01;
2801 const _CharT* _M_amonth02;
2802 const _CharT* _M_amonth03;
2803 const _CharT* _M_amonth04;
2804 const _CharT* _M_amonth05;
2805 const _CharT* _M_amonth06;
2806 const _CharT* _M_amonth07;
2807 const _CharT* _M_amonth08;
2808 const _CharT* _M_amonth09;
2809 const _CharT* _M_amonth10;
2810 const _CharT* _M_amonth11;
2811 const _CharT* _M_amonth12;
2813 bool _M_allocated;
2815 __timepunct_cache(size_t __refs = 0) : facet(__refs),
2816 _M_date_format(NULL), _M_date_era_format(NULL), _M_time_format(NULL),
2817 _M_time_era_format(NULL), _M_date_time_format(NULL),
2818 _M_date_time_era_format(NULL), _M_am(NULL), _M_pm(NULL),
2819 _M_am_pm_format(NULL), _M_day1(NULL), _M_day2(NULL), _M_day3(NULL),
2820 _M_day4(NULL), _M_day5(NULL), _M_day6(NULL), _M_day7(NULL),
2821 _M_aday1(NULL), _M_aday2(NULL), _M_aday3(NULL), _M_aday4(NULL),
2822 _M_aday5(NULL), _M_aday6(NULL), _M_aday7(NULL), _M_month01(NULL),
2823 _M_month02(NULL), _M_month03(NULL), _M_month04(NULL), _M_month05(NULL),
2824 _M_month06(NULL), _M_month07(NULL), _M_month08(NULL), _M_month09(NULL),
2825 _M_month10(NULL), _M_month11(NULL), _M_month12(NULL), _M_amonth01(NULL),
2826 _M_amonth02(NULL), _M_amonth03(NULL), _M_amonth04(NULL),
2827 _M_amonth05(NULL), _M_amonth06(NULL), _M_amonth07(NULL),
2828 _M_amonth08(NULL), _M_amonth09(NULL), _M_amonth10(NULL),
2829 _M_amonth11(NULL), _M_amonth12(NULL), _M_allocated(false)
2832 ~__timepunct_cache();
2834 void
2835 _M_cache(const locale& __loc);
2837 private:
2838 __timepunct_cache&
2839 operator=(const __timepunct_cache&);
2841 explicit
2842 __timepunct_cache(const __timepunct_cache&);
2845 template<typename _CharT>
2846 __timepunct_cache<_CharT>::~__timepunct_cache()
2848 if (_M_allocated)
2850 // Unused.
2854 // Specializations.
2855 template<>
2856 const char*
2857 __timepunct_cache<char>::_S_timezones[14];
2859 #ifdef _GLIBCXX_USE_WCHAR_T
2860 template<>
2861 const wchar_t*
2862 __timepunct_cache<wchar_t>::_S_timezones[14];
2863 #endif
2865 // Generic.
2866 template<typename _CharT>
2867 const _CharT* __timepunct_cache<_CharT>::_S_timezones[14];
2869 template<typename _CharT>
2870 class __timepunct : public locale::facet
2872 public:
2873 // Types:
2874 typedef _CharT __char_type;
2875 typedef basic_string<_CharT> __string_type;
2876 typedef __timepunct_cache<_CharT> __cache_type;
2878 protected:
2879 __cache_type* _M_data;
2880 __c_locale _M_c_locale_timepunct;
2881 const char* _M_name_timepunct;
2883 public:
2884 /// Numpunct facet id.
2885 static locale::id id;
2887 explicit
2888 __timepunct(size_t __refs = 0);
2890 explicit
2891 __timepunct(__cache_type* __cache, size_t __refs = 0);
2894 * @brief Internal constructor. Not for general use.
2896 * This is a constructor for use by the library itself to set up new
2897 * locales.
2899 * @param cloc The "C" locale.
2900 * @param s The name of a locale.
2901 * @param refs Passed to the base facet class.
2903 explicit
2904 __timepunct(__c_locale __cloc, const char* __s, size_t __refs = 0);
2906 // FIXME: for error checking purposes _M_put should return the return
2907 // value of strftime/wcsftime.
2908 void
2909 _M_put(_CharT* __s, size_t __maxlen, const _CharT* __format,
2910 const tm* __tm) const;
2912 void
2913 _M_date_formats(const _CharT** __date) const
2915 // Always have default first.
2916 __date[0] = _M_data->_M_date_format;
2917 __date[1] = _M_data->_M_date_era_format;
2920 void
2921 _M_time_formats(const _CharT** __time) const
2923 // Always have default first.
2924 __time[0] = _M_data->_M_time_format;
2925 __time[1] = _M_data->_M_time_era_format;
2928 void
2929 _M_date_time_formats(const _CharT** __dt) const
2931 // Always have default first.
2932 __dt[0] = _M_data->_M_date_time_format;
2933 __dt[1] = _M_data->_M_date_time_era_format;
2936 void
2937 _M_am_pm_format(const _CharT* __ampm) const
2938 { __ampm = _M_data->_M_am_pm_format; }
2940 void
2941 _M_am_pm(const _CharT** __ampm) const
2943 __ampm[0] = _M_data->_M_am;
2944 __ampm[1] = _M_data->_M_pm;
2947 void
2948 _M_days(const _CharT** __days) const
2950 __days[0] = _M_data->_M_day1;
2951 __days[1] = _M_data->_M_day2;
2952 __days[2] = _M_data->_M_day3;
2953 __days[3] = _M_data->_M_day4;
2954 __days[4] = _M_data->_M_day5;
2955 __days[5] = _M_data->_M_day6;
2956 __days[6] = _M_data->_M_day7;
2959 void
2960 _M_days_abbreviated(const _CharT** __days) const
2962 __days[0] = _M_data->_M_aday1;
2963 __days[1] = _M_data->_M_aday2;
2964 __days[2] = _M_data->_M_aday3;
2965 __days[3] = _M_data->_M_aday4;
2966 __days[4] = _M_data->_M_aday5;
2967 __days[5] = _M_data->_M_aday6;
2968 __days[6] = _M_data->_M_aday7;
2971 void
2972 _M_months(const _CharT** __months) const
2974 __months[0] = _M_data->_M_month01;
2975 __months[1] = _M_data->_M_month02;
2976 __months[2] = _M_data->_M_month03;
2977 __months[3] = _M_data->_M_month04;
2978 __months[4] = _M_data->_M_month05;
2979 __months[5] = _M_data->_M_month06;
2980 __months[6] = _M_data->_M_month07;
2981 __months[7] = _M_data->_M_month08;
2982 __months[8] = _M_data->_M_month09;
2983 __months[9] = _M_data->_M_month10;
2984 __months[10] = _M_data->_M_month11;
2985 __months[11] = _M_data->_M_month12;
2988 void
2989 _M_months_abbreviated(const _CharT** __months) const
2991 __months[0] = _M_data->_M_amonth01;
2992 __months[1] = _M_data->_M_amonth02;
2993 __months[2] = _M_data->_M_amonth03;
2994 __months[3] = _M_data->_M_amonth04;
2995 __months[4] = _M_data->_M_amonth05;
2996 __months[5] = _M_data->_M_amonth06;
2997 __months[6] = _M_data->_M_amonth07;
2998 __months[7] = _M_data->_M_amonth08;
2999 __months[8] = _M_data->_M_amonth09;
3000 __months[9] = _M_data->_M_amonth10;
3001 __months[10] = _M_data->_M_amonth11;
3002 __months[11] = _M_data->_M_amonth12;
3005 protected:
3006 virtual
3007 ~__timepunct();
3009 // For use at construction time only.
3010 void
3011 _M_initialize_timepunct(__c_locale __cloc = NULL);
3014 template<typename _CharT>
3015 locale::id __timepunct<_CharT>::id;
3017 // Specializations.
3018 template<>
3019 void
3020 __timepunct<char>::_M_initialize_timepunct(__c_locale __cloc);
3022 template<>
3023 void
3024 __timepunct<char>::_M_put(char*, size_t, const char*, const tm*) const;
3026 #ifdef _GLIBCXX_USE_WCHAR_T
3027 template<>
3028 void
3029 __timepunct<wchar_t>::_M_initialize_timepunct(__c_locale __cloc);
3031 template<>
3032 void
3033 __timepunct<wchar_t>::_M_put(wchar_t*, size_t, const wchar_t*,
3034 const tm*) const;
3035 #endif
3037 _GLIBCXX_END_NAMESPACE
3039 // Include host and configuration specific timepunct functions.
3040 #include <bits/time_members.h>
3042 _GLIBCXX_BEGIN_NAMESPACE(std)
3045 * @brief Facet for parsing dates and times.
3047 * This facet encapsulates the code to parse and return a date or
3048 * time from a string. It is used by the istream numeric
3049 * extraction operators.
3051 * The time_get template uses protected virtual functions to provide the
3052 * actual results. The public accessors forward the call to the virtual
3053 * functions. These virtual functions are hooks for developers to
3054 * implement the behavior they require from the time_get facet.
3056 template<typename _CharT, typename _InIter>
3057 class time_get : public locale::facet, public time_base
3059 public:
3060 // Types:
3061 //@{
3062 /// Public typedefs
3063 typedef _CharT char_type;
3064 typedef _InIter iter_type;
3065 //@}
3066 typedef basic_string<_CharT> __string_type;
3068 /// Numpunct facet id.
3069 static locale::id id;
3072 * @brief Constructor performs initialization.
3074 * This is the constructor provided by the standard.
3076 * @param refs Passed to the base facet class.
3078 explicit
3079 time_get(size_t __refs = 0)
3080 : facet (__refs) { }
3083 * @brief Return preferred order of month, day, and year.
3085 * This function returns an enum from timebase::dateorder giving the
3086 * preferred ordering if the format "x" given to time_put::put() only
3087 * uses month, day, and year. If the format "x" for the associated
3088 * locale uses other fields, this function returns
3089 * timebase::dateorder::noorder.
3091 * NOTE: The library always returns noorder at the moment.
3093 * @return A member of timebase::dateorder.
3095 dateorder
3096 date_order() const
3097 { return this->do_date_order(); }
3100 * @brief Parse input time string.
3102 * This function parses a time according to the format "x" and puts the
3103 * results into a user-supplied struct tm. The result is returned by
3104 * calling time_get::do_get_time().
3106 * If there is a valid time string according to format "x", @a tm will
3107 * be filled in accordingly and the returned iterator will point to the
3108 * first character beyond the time string. If an error occurs before
3109 * the end, err |= ios_base::failbit. If parsing reads all the
3110 * characters, err |= ios_base::eofbit.
3112 * @param beg Start of string to parse.
3113 * @param end End of string to parse.
3114 * @param io Source of the locale.
3115 * @param err Error flags to set.
3116 * @param tm Pointer to struct tm to fill in.
3117 * @return Iterator to first char beyond time string.
3119 iter_type
3120 get_time(iter_type __beg, iter_type __end, ios_base& __io,
3121 ios_base::iostate& __err, tm* __tm) const
3122 { return this->do_get_time(__beg, __end, __io, __err, __tm); }
3125 * @brief Parse input date string.
3127 * This function parses a date according to the format "X" and puts the
3128 * results into a user-supplied struct tm. The result is returned by
3129 * calling time_get::do_get_date().
3131 * If there is a valid date string according to format "X", @a tm will
3132 * be filled in accordingly and the returned iterator will point to the
3133 * first character beyond the date string. If an error occurs before
3134 * the end, err |= ios_base::failbit. If parsing reads all the
3135 * characters, err |= ios_base::eofbit.
3137 * @param beg Start of string to parse.
3138 * @param end End of string to parse.
3139 * @param io Source of the locale.
3140 * @param err Error flags to set.
3141 * @param tm Pointer to struct tm to fill in.
3142 * @return Iterator to first char beyond date string.
3144 iter_type
3145 get_date(iter_type __beg, iter_type __end, ios_base& __io,
3146 ios_base::iostate& __err, tm* __tm) const
3147 { return this->do_get_date(__beg, __end, __io, __err, __tm); }
3150 * @brief Parse input weekday string.
3152 * This function parses a weekday name and puts the results into a
3153 * user-supplied struct tm. The result is returned by calling
3154 * time_get::do_get_weekday().
3156 * Parsing starts by parsing an abbreviated weekday name. If a valid
3157 * abbreviation is followed by a character that would lead to the full
3158 * weekday name, parsing continues until the full name is found or an
3159 * error occurs. Otherwise parsing finishes at the end of the
3160 * abbreviated name.
3162 * If an error occurs before the end, err |= ios_base::failbit. If
3163 * parsing reads all the characters, err |= ios_base::eofbit.
3165 * @param beg Start of string to parse.
3166 * @param end End of string to parse.
3167 * @param io Source of the locale.
3168 * @param err Error flags to set.
3169 * @param tm Pointer to struct tm to fill in.
3170 * @return Iterator to first char beyond weekday name.
3172 iter_type
3173 get_weekday(iter_type __beg, iter_type __end, ios_base& __io,
3174 ios_base::iostate& __err, tm* __tm) const
3175 { return this->do_get_weekday(__beg, __end, __io, __err, __tm); }
3178 * @brief Parse input month string.
3180 * This function parses a month name and puts the results into a
3181 * user-supplied struct tm. The result is returned by calling
3182 * time_get::do_get_monthname().
3184 * Parsing starts by parsing an abbreviated month name. If a valid
3185 * abbreviation is followed by a character that would lead to the full
3186 * month name, parsing continues until the full name is found or an
3187 * error occurs. Otherwise parsing finishes at the end of the
3188 * abbreviated name.
3190 * If an error occurs before the end, err |= ios_base::failbit. If
3191 * parsing reads all the characters, err |=
3192 * ios_base::eofbit.
3194 * @param beg Start of string to parse.
3195 * @param end End of string to parse.
3196 * @param io Source of the locale.
3197 * @param err Error flags to set.
3198 * @param tm Pointer to struct tm to fill in.
3199 * @return Iterator to first char beyond month name.
3201 iter_type
3202 get_monthname(iter_type __beg, iter_type __end, ios_base& __io,
3203 ios_base::iostate& __err, tm* __tm) const
3204 { return this->do_get_monthname(__beg, __end, __io, __err, __tm); }
3207 * @brief Parse input year string.
3209 * This function reads up to 4 characters to parse a year string and
3210 * puts the results into a user-supplied struct tm. The result is
3211 * returned by calling time_get::do_get_year().
3213 * 4 consecutive digits are interpreted as a full year. If there are
3214 * exactly 2 consecutive digits, the library interprets this as the
3215 * number of years since 1900.
3217 * If an error occurs before the end, err |= ios_base::failbit. If
3218 * parsing reads all the characters, err |= ios_base::eofbit.
3220 * @param beg Start of string to parse.
3221 * @param end End of string to parse.
3222 * @param io Source of the locale.
3223 * @param err Error flags to set.
3224 * @param tm Pointer to struct tm to fill in.
3225 * @return Iterator to first char beyond year.
3227 iter_type
3228 get_year(iter_type __beg, iter_type __end, ios_base& __io,
3229 ios_base::iostate& __err, tm* __tm) const
3230 { return this->do_get_year(__beg, __end, __io, __err, __tm); }
3232 protected:
3233 /// Destructor.
3234 virtual
3235 ~time_get() { }
3238 * @brief Return preferred order of month, day, and year.
3240 * This function returns an enum from timebase::dateorder giving the
3241 * preferred ordering if the format "x" given to time_put::put() only
3242 * uses month, day, and year. This function is a hook for derived
3243 * classes to change the value returned.
3245 * @return A member of timebase::dateorder.
3247 virtual dateorder
3248 do_date_order() const;
3251 * @brief Parse input time string.
3253 * This function parses a time according to the format "x" and puts the
3254 * results into a user-supplied struct tm. This function is a hook for
3255 * derived classes to change the value returned. @see get_time() for
3256 * details.
3258 * @param beg Start of string to parse.
3259 * @param end End of string to parse.
3260 * @param io Source of the locale.
3261 * @param err Error flags to set.
3262 * @param tm Pointer to struct tm to fill in.
3263 * @return Iterator to first char beyond time string.
3265 virtual iter_type
3266 do_get_time(iter_type __beg, iter_type __end, ios_base& __io,
3267 ios_base::iostate& __err, tm* __tm) const;
3270 * @brief Parse input date string.
3272 * This function parses a date according to the format "X" and puts the
3273 * results into a user-supplied struct tm. This function is a hook for
3274 * derived classes to change the value returned. @see get_date() for
3275 * details.
3277 * @param beg Start of string to parse.
3278 * @param end End of string to parse.
3279 * @param io Source of the locale.
3280 * @param err Error flags to set.
3281 * @param tm Pointer to struct tm to fill in.
3282 * @return Iterator to first char beyond date string.
3284 virtual iter_type
3285 do_get_date(iter_type __beg, iter_type __end, ios_base& __io,
3286 ios_base::iostate& __err, tm* __tm) const;
3289 * @brief Parse input weekday string.
3291 * This function parses a weekday name and puts the results into a
3292 * user-supplied struct tm. This function is a hook for derived
3293 * classes to change the value returned. @see get_weekday() for
3294 * details.
3296 * @param beg Start of string to parse.
3297 * @param end End of string to parse.
3298 * @param io Source of the locale.
3299 * @param err Error flags to set.
3300 * @param tm Pointer to struct tm to fill in.
3301 * @return Iterator to first char beyond weekday name.
3303 virtual iter_type
3304 do_get_weekday(iter_type __beg, iter_type __end, ios_base&,
3305 ios_base::iostate& __err, tm* __tm) const;
3308 * @brief Parse input month string.
3310 * This function parses a month name and puts the results into a
3311 * user-supplied struct tm. This function is a hook for derived
3312 * classes to change the value returned. @see get_monthname() for
3313 * details.
3315 * @param beg Start of string to parse.
3316 * @param end End of string to parse.
3317 * @param io Source of the locale.
3318 * @param err Error flags to set.
3319 * @param tm Pointer to struct tm to fill in.
3320 * @return Iterator to first char beyond month name.
3322 virtual iter_type
3323 do_get_monthname(iter_type __beg, iter_type __end, ios_base&,
3324 ios_base::iostate& __err, tm* __tm) const;
3327 * @brief Parse input year string.
3329 * This function reads up to 4 characters to parse a year string and
3330 * puts the results into a user-supplied struct tm. This function is a
3331 * hook for derived classes to change the value returned. @see
3332 * get_year() for details.
3334 * @param beg Start of string to parse.
3335 * @param end End of string to parse.
3336 * @param io Source of the locale.
3337 * @param err Error flags to set.
3338 * @param tm Pointer to struct tm to fill in.
3339 * @return Iterator to first char beyond year.
3341 virtual iter_type
3342 do_get_year(iter_type __beg, iter_type __end, ios_base& __io,
3343 ios_base::iostate& __err, tm* __tm) const;
3345 // Extract numeric component of length __len.
3346 iter_type
3347 _M_extract_num(iter_type __beg, iter_type __end, int& __member,
3348 int __min, int __max, size_t __len,
3349 ios_base& __io, ios_base::iostate& __err) const;
3351 // Extract day or month name, or any unique array of string
3352 // literals in a const _CharT* array.
3353 iter_type
3354 _M_extract_name(iter_type __beg, iter_type __end, int& __member,
3355 const _CharT** __names, size_t __indexlen,
3356 ios_base& __io, ios_base::iostate& __err) const;
3358 // Extract on a component-by-component basis, via __format argument.
3359 iter_type
3360 _M_extract_via_format(iter_type __beg, iter_type __end, ios_base& __io,
3361 ios_base::iostate& __err, tm* __tm,
3362 const _CharT* __format) const;
3365 template<typename _CharT, typename _InIter>
3366 locale::id time_get<_CharT, _InIter>::id;
3368 /// @brief class time_get_byname [22.2.5.2].
3369 template<typename _CharT, typename _InIter>
3370 class time_get_byname : public time_get<_CharT, _InIter>
3372 public:
3373 // Types:
3374 typedef _CharT char_type;
3375 typedef _InIter iter_type;
3377 explicit
3378 time_get_byname(const char*, size_t __refs = 0)
3379 : time_get<_CharT, _InIter>(__refs) { }
3381 protected:
3382 virtual
3383 ~time_get_byname() { }
3387 * @brief Facet for outputting dates and times.
3389 * This facet encapsulates the code to format and output dates and times
3390 * according to formats used by strftime().
3392 * The time_put template uses protected virtual functions to provide the
3393 * actual results. The public accessors forward the call to the virtual
3394 * functions. These virtual functions are hooks for developers to
3395 * implement the behavior they require from the time_put facet.
3397 template<typename _CharT, typename _OutIter>
3398 class time_put : public locale::facet
3400 public:
3401 // Types:
3402 //@{
3403 /// Public typedefs
3404 typedef _CharT char_type;
3405 typedef _OutIter iter_type;
3406 //@}
3408 /// Numpunct facet id.
3409 static locale::id id;
3412 * @brief Constructor performs initialization.
3414 * This is the constructor provided by the standard.
3416 * @param refs Passed to the base facet class.
3418 explicit
3419 time_put(size_t __refs = 0)
3420 : facet(__refs) { }
3423 * @brief Format and output a time or date.
3425 * This function formats the data in struct tm according to the
3426 * provided format string. The format string is interpreted as by
3427 * strftime().
3429 * @param s The stream to write to.
3430 * @param io Source of locale.
3431 * @param fill char_type to use for padding.
3432 * @param tm Struct tm with date and time info to format.
3433 * @param beg Start of format string.
3434 * @param end End of format string.
3435 * @return Iterator after writing.
3437 iter_type
3438 put(iter_type __s, ios_base& __io, char_type __fill, const tm* __tm,
3439 const _CharT* __beg, const _CharT* __end) const;
3442 * @brief Format and output a time or date.
3444 * This function formats the data in struct tm according to the
3445 * provided format char and optional modifier. The format and modifier
3446 * are interpreted as by strftime(). It does so by returning
3447 * time_put::do_put().
3449 * @param s The stream to write to.
3450 * @param io Source of locale.
3451 * @param fill char_type to use for padding.
3452 * @param tm Struct tm with date and time info to format.
3453 * @param format Format char.
3454 * @param mod Optional modifier char.
3455 * @return Iterator after writing.
3457 iter_type
3458 put(iter_type __s, ios_base& __io, char_type __fill,
3459 const tm* __tm, char __format, char __mod = 0) const
3460 { return this->do_put(__s, __io, __fill, __tm, __format, __mod); }
3462 protected:
3463 /// Destructor.
3464 virtual
3465 ~time_put()
3469 * @brief Format and output a time or date.
3471 * This function formats the data in struct tm according to the
3472 * provided format char and optional modifier. This function is a hook
3473 * for derived classes to change the value returned. @see put() for
3474 * more details.
3476 * @param s The stream to write to.
3477 * @param io Source of locale.
3478 * @param fill char_type to use for padding.
3479 * @param tm Struct tm with date and time info to format.
3480 * @param format Format char.
3481 * @param mod Optional modifier char.
3482 * @return Iterator after writing.
3484 virtual iter_type
3485 do_put(iter_type __s, ios_base& __io, char_type __fill, const tm* __tm,
3486 char __format, char __mod) const;
3489 template<typename _CharT, typename _OutIter>
3490 locale::id time_put<_CharT, _OutIter>::id;
3492 /// @brief class time_put_byname [22.2.5.4].
3493 template<typename _CharT, typename _OutIter>
3494 class time_put_byname : public time_put<_CharT, _OutIter>
3496 public:
3497 // Types:
3498 typedef _CharT char_type;
3499 typedef _OutIter iter_type;
3501 explicit
3502 time_put_byname(const char*, size_t __refs = 0)
3503 : time_put<_CharT, _OutIter>(__refs)
3504 { };
3506 protected:
3507 virtual
3508 ~time_put_byname() { }
3513 * @brief Money format ordering data.
3515 * This class contains an ordered array of 4 fields to represent the
3516 * pattern for formatting a money amount. Each field may contain one entry
3517 * from the part enum. symbol, sign, and value must be present and the
3518 * remaining field must contain either none or space. @see
3519 * moneypunct::pos_format() and moneypunct::neg_format() for details of how
3520 * these fields are interpreted.
3522 class money_base
3524 public:
3525 enum part { none, space, symbol, sign, value };
3526 struct pattern { char field[4]; };
3528 static const pattern _S_default_pattern;
3530 enum
3532 _S_minus,
3533 _S_zero,
3534 _S_end = 11
3537 // String literal of acceptable (narrow) input/output, for
3538 // money_get/money_put. "-0123456789"
3539 static const char* _S_atoms;
3541 // Construct and return valid pattern consisting of some combination of:
3542 // space none symbol sign value
3543 static pattern
3544 _S_construct_pattern(char __precedes, char __space, char __posn);
3547 template<typename _CharT, bool _Intl>
3548 struct __moneypunct_cache : public locale::facet
3550 const char* _M_grouping;
3551 size_t _M_grouping_size;
3552 bool _M_use_grouping;
3553 _CharT _M_decimal_point;
3554 _CharT _M_thousands_sep;
3555 const _CharT* _M_curr_symbol;
3556 size_t _M_curr_symbol_size;
3557 const _CharT* _M_positive_sign;
3558 size_t _M_positive_sign_size;
3559 const _CharT* _M_negative_sign;
3560 size_t _M_negative_sign_size;
3561 int _M_frac_digits;
3562 money_base::pattern _M_pos_format;
3563 money_base::pattern _M_neg_format;
3565 // A list of valid numeric literals for input and output: in the standard
3566 // "C" locale, this is "-0123456789". This array contains the chars after
3567 // having been passed through the current locale's ctype<_CharT>.widen().
3568 _CharT _M_atoms[money_base::_S_end];
3570 bool _M_allocated;
3572 __moneypunct_cache(size_t __refs = 0) : facet(__refs),
3573 _M_grouping(NULL), _M_grouping_size(0), _M_use_grouping(false),
3574 _M_decimal_point(_CharT()), _M_thousands_sep(_CharT()),
3575 _M_curr_symbol(NULL), _M_curr_symbol_size(0),
3576 _M_positive_sign(NULL), _M_positive_sign_size(0),
3577 _M_negative_sign(NULL), _M_negative_sign_size(0),
3578 _M_frac_digits(0),
3579 _M_pos_format(money_base::pattern()),
3580 _M_neg_format(money_base::pattern()), _M_allocated(false)
3583 ~__moneypunct_cache();
3585 void
3586 _M_cache(const locale& __loc);
3588 private:
3589 __moneypunct_cache&
3590 operator=(const __moneypunct_cache&);
3592 explicit
3593 __moneypunct_cache(const __moneypunct_cache&);
3596 template<typename _CharT, bool _Intl>
3597 __moneypunct_cache<_CharT, _Intl>::~__moneypunct_cache()
3599 if (_M_allocated)
3601 delete [] _M_grouping;
3602 delete [] _M_curr_symbol;
3603 delete [] _M_positive_sign;
3604 delete [] _M_negative_sign;
3609 * @brief Facet for formatting data for money amounts.
3611 * This facet encapsulates the punctuation, grouping and other formatting
3612 * features of money amount string representations.
3614 template<typename _CharT, bool _Intl>
3615 class moneypunct : public locale::facet, public money_base
3617 public:
3618 // Types:
3619 //@{
3620 /// Public typedefs
3621 typedef _CharT char_type;
3622 typedef basic_string<_CharT> string_type;
3623 //@}
3624 typedef __moneypunct_cache<_CharT, _Intl> __cache_type;
3626 private:
3627 __cache_type* _M_data;
3629 public:
3630 /// This value is provided by the standard, but no reason for its
3631 /// existence.
3632 static const bool intl = _Intl;
3633 /// Numpunct facet id.
3634 static locale::id id;
3637 * @brief Constructor performs initialization.
3639 * This is the constructor provided by the standard.
3641 * @param refs Passed to the base facet class.
3643 explicit
3644 moneypunct(size_t __refs = 0) : facet(__refs), _M_data(NULL)
3645 { _M_initialize_moneypunct(); }
3648 * @brief Constructor performs initialization.
3650 * This is an internal constructor.
3652 * @param cache Cache for optimization.
3653 * @param refs Passed to the base facet class.
3655 explicit
3656 moneypunct(__cache_type* __cache, size_t __refs = 0)
3657 : facet(__refs), _M_data(__cache)
3658 { _M_initialize_moneypunct(); }
3661 * @brief Internal constructor. Not for general use.
3663 * This is a constructor for use by the library itself to set up new
3664 * locales.
3666 * @param cloc The "C" locale.
3667 * @param s The name of a locale.
3668 * @param refs Passed to the base facet class.
3670 explicit
3671 moneypunct(__c_locale __cloc, const char* __s, size_t __refs = 0)
3672 : facet(__refs), _M_data(NULL)
3673 { _M_initialize_moneypunct(__cloc, __s); }
3676 * @brief Return decimal point character.
3678 * This function returns a char_type to use as a decimal point. It
3679 * does so by returning returning
3680 * moneypunct<char_type>::do_decimal_point().
3682 * @return @a char_type representing a decimal point.
3684 char_type
3685 decimal_point() const
3686 { return this->do_decimal_point(); }
3689 * @brief Return thousands separator character.
3691 * This function returns a char_type to use as a thousands
3692 * separator. It does so by returning returning
3693 * moneypunct<char_type>::do_thousands_sep().
3695 * @return char_type representing a thousands separator.
3697 char_type
3698 thousands_sep() const
3699 { return this->do_thousands_sep(); }
3702 * @brief Return grouping specification.
3704 * This function returns a string representing groupings for the
3705 * integer part of an amount. Groupings indicate where thousands
3706 * separators should be inserted.
3708 * Each char in the return string is interpret as an integer rather
3709 * than a character. These numbers represent the number of digits in a
3710 * group. The first char in the string represents the number of digits
3711 * in the least significant group. If a char is negative, it indicates
3712 * an unlimited number of digits for the group. If more chars from the
3713 * string are required to group a number, the last char is used
3714 * repeatedly.
3716 * For example, if the grouping() returns "\003\002" and is applied to
3717 * the number 123456789, this corresponds to 12,34,56,789. Note that
3718 * if the string was "32", this would put more than 50 digits into the
3719 * least significant group if the character set is ASCII.
3721 * The string is returned by calling
3722 * moneypunct<char_type>::do_grouping().
3724 * @return string representing grouping specification.
3726 string
3727 grouping() const
3728 { return this->do_grouping(); }
3731 * @brief Return currency symbol string.
3733 * This function returns a string_type to use as a currency symbol. It
3734 * does so by returning returning
3735 * moneypunct<char_type>::do_curr_symbol().
3737 * @return @a string_type representing a currency symbol.
3739 string_type
3740 curr_symbol() const
3741 { return this->do_curr_symbol(); }
3744 * @brief Return positive sign string.
3746 * This function returns a string_type to use as a sign for positive
3747 * amounts. It does so by returning returning
3748 * moneypunct<char_type>::do_positive_sign().
3750 * If the return value contains more than one character, the first
3751 * character appears in the position indicated by pos_format() and the
3752 * remainder appear at the end of the formatted string.
3754 * @return @a string_type representing a positive sign.
3756 string_type
3757 positive_sign() const
3758 { return this->do_positive_sign(); }
3761 * @brief Return negative sign string.
3763 * This function returns a string_type to use as a sign for negative
3764 * amounts. It does so by returning returning
3765 * moneypunct<char_type>::do_negative_sign().
3767 * If the return value contains more than one character, the first
3768 * character appears in the position indicated by neg_format() and the
3769 * remainder appear at the end of the formatted string.
3771 * @return @a string_type representing a negative sign.
3773 string_type
3774 negative_sign() const
3775 { return this->do_negative_sign(); }
3778 * @brief Return number of digits in fraction.
3780 * This function returns the exact number of digits that make up the
3781 * fractional part of a money amount. It does so by returning
3782 * returning moneypunct<char_type>::do_frac_digits().
3784 * The fractional part of a money amount is optional. But if it is
3785 * present, there must be frac_digits() digits.
3787 * @return Number of digits in amount fraction.
3790 frac_digits() const
3791 { return this->do_frac_digits(); }
3793 //@{
3795 * @brief Return pattern for money values.
3797 * This function returns a pattern describing the formatting of a
3798 * positive or negative valued money amount. It does so by returning
3799 * returning moneypunct<char_type>::do_pos_format() or
3800 * moneypunct<char_type>::do_neg_format().
3802 * The pattern has 4 fields describing the ordering of symbol, sign,
3803 * value, and none or space. There must be one of each in the pattern.
3804 * The none and space enums may not appear in the first field and space
3805 * may not appear in the final field.
3807 * The parts of a money string must appear in the order indicated by
3808 * the fields of the pattern. The symbol field indicates that the
3809 * value of curr_symbol() may be present. The sign field indicates
3810 * that the value of positive_sign() or negative_sign() must be
3811 * present. The value field indicates that the absolute value of the
3812 * money amount is present. none indicates 0 or more whitespace
3813 * characters, except at the end, where it permits no whitespace.
3814 * space indicates that 1 or more whitespace characters must be
3815 * present.
3817 * For example, for the US locale and pos_format() pattern
3818 * {symbol,sign,value,none}, curr_symbol() == '$' positive_sign() ==
3819 * '+', and value 10.01, and options set to force the symbol, the
3820 * corresponding string is "$+10.01".
3822 * @return Pattern for money values.
3824 pattern
3825 pos_format() const
3826 { return this->do_pos_format(); }
3828 pattern
3829 neg_format() const
3830 { return this->do_neg_format(); }
3831 //@}
3833 protected:
3834 /// Destructor.
3835 virtual
3836 ~moneypunct();
3839 * @brief Return decimal point character.
3841 * Returns a char_type to use as a decimal point. This function is a
3842 * hook for derived classes to change the value returned.
3844 * @return @a char_type representing a decimal point.
3846 virtual char_type
3847 do_decimal_point() const
3848 { return _M_data->_M_decimal_point; }
3851 * @brief Return thousands separator character.
3853 * Returns a char_type to use as a thousands separator. This function
3854 * is a hook for derived classes to change the value returned.
3856 * @return @a char_type representing a thousands separator.
3858 virtual char_type
3859 do_thousands_sep() const
3860 { return _M_data->_M_thousands_sep; }
3863 * @brief Return grouping specification.
3865 * Returns a string representing groupings for the integer part of a
3866 * number. This function is a hook for derived classes to change the
3867 * value returned. @see grouping() for details.
3869 * @return String representing grouping specification.
3871 virtual string
3872 do_grouping() const
3873 { return _M_data->_M_grouping; }
3876 * @brief Return currency symbol string.
3878 * This function returns a string_type to use as a currency symbol.
3879 * This function is a hook for derived classes to change the value
3880 * returned. @see curr_symbol() for details.
3882 * @return @a string_type representing a currency symbol.
3884 virtual string_type
3885 do_curr_symbol() const
3886 { return _M_data->_M_curr_symbol; }
3889 * @brief Return positive sign string.
3891 * This function returns a string_type to use as a sign for positive
3892 * amounts. This function is a hook for derived classes to change the
3893 * value returned. @see positive_sign() for details.
3895 * @return @a string_type representing a positive sign.
3897 virtual string_type
3898 do_positive_sign() const
3899 { return _M_data->_M_positive_sign; }
3902 * @brief Return negative sign string.
3904 * This function returns a string_type to use as a sign for negative
3905 * amounts. This function is a hook for derived classes to change the
3906 * value returned. @see negative_sign() for details.
3908 * @return @a string_type representing a negative sign.
3910 virtual string_type
3911 do_negative_sign() const
3912 { return _M_data->_M_negative_sign; }
3915 * @brief Return number of digits in fraction.
3917 * This function returns the exact number of digits that make up the
3918 * fractional part of a money amount. This function is a hook for
3919 * derived classes to change the value returned. @see frac_digits()
3920 * for details.
3922 * @return Number of digits in amount fraction.
3924 virtual int
3925 do_frac_digits() const
3926 { return _M_data->_M_frac_digits; }
3929 * @brief Return pattern for money values.
3931 * This function returns a pattern describing the formatting of a
3932 * positive valued money amount. This function is a hook for derived
3933 * classes to change the value returned. @see pos_format() for
3934 * details.
3936 * @return Pattern for money values.
3938 virtual pattern
3939 do_pos_format() const
3940 { return _M_data->_M_pos_format; }
3943 * @brief Return pattern for money values.
3945 * This function returns a pattern describing the formatting of a
3946 * negative valued money amount. This function is a hook for derived
3947 * classes to change the value returned. @see neg_format() for
3948 * details.
3950 * @return Pattern for money values.
3952 virtual pattern
3953 do_neg_format() const
3954 { return _M_data->_M_neg_format; }
3956 // For use at construction time only.
3957 void
3958 _M_initialize_moneypunct(__c_locale __cloc = NULL,
3959 const char* __name = NULL);
3962 template<typename _CharT, bool _Intl>
3963 locale::id moneypunct<_CharT, _Intl>::id;
3965 template<typename _CharT, bool _Intl>
3966 const bool moneypunct<_CharT, _Intl>::intl;
3968 template<>
3969 moneypunct<char, true>::~moneypunct();
3971 template<>
3972 moneypunct<char, false>::~moneypunct();
3974 template<>
3975 void
3976 moneypunct<char, true>::_M_initialize_moneypunct(__c_locale, const char*);
3978 template<>
3979 void
3980 moneypunct<char, false>::_M_initialize_moneypunct(__c_locale, const char*);
3982 #ifdef _GLIBCXX_USE_WCHAR_T
3983 template<>
3984 moneypunct<wchar_t, true>::~moneypunct();
3986 template<>
3987 moneypunct<wchar_t, false>::~moneypunct();
3989 template<>
3990 void
3991 moneypunct<wchar_t, true>::_M_initialize_moneypunct(__c_locale,
3992 const char*);
3994 template<>
3995 void
3996 moneypunct<wchar_t, false>::_M_initialize_moneypunct(__c_locale,
3997 const char*);
3998 #endif
4000 /// @brief class moneypunct_byname [22.2.6.4].
4001 template<typename _CharT, bool _Intl>
4002 class moneypunct_byname : public moneypunct<_CharT, _Intl>
4004 public:
4005 typedef _CharT char_type;
4006 typedef basic_string<_CharT> string_type;
4008 static const bool intl = _Intl;
4010 explicit
4011 moneypunct_byname(const char* __s, size_t __refs = 0)
4012 : moneypunct<_CharT, _Intl>(__refs)
4014 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
4016 __c_locale __tmp;
4017 this->_S_create_c_locale(__tmp, __s);
4018 this->_M_initialize_moneypunct(__tmp);
4019 this->_S_destroy_c_locale(__tmp);
4023 protected:
4024 virtual
4025 ~moneypunct_byname() { }
4028 template<typename _CharT, bool _Intl>
4029 const bool moneypunct_byname<_CharT, _Intl>::intl;
4031 _GLIBCXX_BEGIN_LDBL_NAMESPACE
4033 * @brief Facet for parsing monetary amounts.
4035 * This facet encapsulates the code to parse and return a monetary
4036 * amount from a string.
4038 * The money_get template uses protected virtual functions to
4039 * provide the actual results. The public accessors forward the
4040 * call to the virtual functions. These virtual functions are
4041 * hooks for developers to implement the behavior they require from
4042 * the money_get facet.
4044 template<typename _CharT, typename _InIter>
4045 class money_get : public locale::facet
4047 public:
4048 // Types:
4049 //@{
4050 /// Public typedefs
4051 typedef _CharT char_type;
4052 typedef _InIter iter_type;
4053 typedef basic_string<_CharT> string_type;
4054 //@}
4056 /// Numpunct facet id.
4057 static locale::id id;
4060 * @brief Constructor performs initialization.
4062 * This is the constructor provided by the standard.
4064 * @param refs Passed to the base facet class.
4066 explicit
4067 money_get(size_t __refs = 0) : facet(__refs) { }
4070 * @brief Read and parse a monetary value.
4072 * This function reads characters from @a s, interprets them as a
4073 * monetary value according to moneypunct and ctype facets retrieved
4074 * from io.getloc(), and returns the result in @a units as an integral
4075 * value moneypunct::frac_digits() * the actual amount. For example,
4076 * the string $10.01 in a US locale would store 1001 in @a units.
4078 * Any characters not part of a valid money amount are not consumed.
4080 * If a money value cannot be parsed from the input stream, sets
4081 * err=(err|io.failbit). If the stream is consumed before finishing
4082 * parsing, sets err=(err|io.failbit|io.eofbit). @a units is
4083 * unchanged if parsing fails.
4085 * This function works by returning the result of do_get().
4087 * @param s Start of characters to parse.
4088 * @param end End of characters to parse.
4089 * @param intl Parameter to use_facet<moneypunct<CharT,intl> >.
4090 * @param io Source of facets and io state.
4091 * @param err Error field to set if parsing fails.
4092 * @param units Place to store result of parsing.
4093 * @return Iterator referencing first character beyond valid money
4094 * amount.
4096 iter_type
4097 get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4098 ios_base::iostate& __err, long double& __units) const
4099 { return this->do_get(__s, __end, __intl, __io, __err, __units); }
4102 * @brief Read and parse a monetary value.
4104 * This function reads characters from @a s, interprets them as a
4105 * monetary value according to moneypunct and ctype facets retrieved
4106 * from io.getloc(), and returns the result in @a digits. For example,
4107 * the string $10.01 in a US locale would store "1001" in @a digits.
4109 * Any characters not part of a valid money amount are not consumed.
4111 * If a money value cannot be parsed from the input stream, sets
4112 * err=(err|io.failbit). If the stream is consumed before finishing
4113 * parsing, sets err=(err|io.failbit|io.eofbit).
4115 * This function works by returning the result of do_get().
4117 * @param s Start of characters to parse.
4118 * @param end End of characters to parse.
4119 * @param intl Parameter to use_facet<moneypunct<CharT,intl> >.
4120 * @param io Source of facets and io state.
4121 * @param err Error field to set if parsing fails.
4122 * @param digits Place to store result of parsing.
4123 * @return Iterator referencing first character beyond valid money
4124 * amount.
4126 iter_type
4127 get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4128 ios_base::iostate& __err, string_type& __digits) const
4129 { return this->do_get(__s, __end, __intl, __io, __err, __digits); }
4131 protected:
4132 /// Destructor.
4133 virtual
4134 ~money_get() { }
4137 * @brief Read and parse a monetary value.
4139 * This function reads and parses characters representing a monetary
4140 * value. This function is a hook for derived classes to change the
4141 * value returned. @see get() for details.
4143 // XXX GLIBCXX_ABI Deprecated
4144 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
4145 virtual iter_type
4146 __do_get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4147 ios_base::iostate& __err, double& __units) const;
4148 #else
4149 virtual iter_type
4150 do_get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4151 ios_base::iostate& __err, long double& __units) const;
4152 #endif
4155 * @brief Read and parse a monetary value.
4157 * This function reads and parses characters representing a monetary
4158 * value. This function is a hook for derived classes to change the
4159 * value returned. @see get() for details.
4161 virtual iter_type
4162 do_get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4163 ios_base::iostate& __err, string_type& __digits) const;
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, long double& __units) const;
4170 #endif
4172 template<bool _Intl>
4173 iter_type
4174 _M_extract(iter_type __s, iter_type __end, ios_base& __io,
4175 ios_base::iostate& __err, string& __digits) const;
4178 template<typename _CharT, typename _InIter>
4179 locale::id money_get<_CharT, _InIter>::id;
4182 * @brief Facet for outputting monetary amounts.
4184 * This facet encapsulates the code to format and output a monetary
4185 * amount.
4187 * The money_put template uses protected virtual functions to
4188 * provide the actual results. The public accessors forward the
4189 * call to the virtual functions. These virtual functions are
4190 * hooks for developers to implement the behavior they require from
4191 * the money_put facet.
4193 template<typename _CharT, typename _OutIter>
4194 class money_put : public locale::facet
4196 public:
4197 //@{
4198 /// Public typedefs
4199 typedef _CharT char_type;
4200 typedef _OutIter iter_type;
4201 typedef basic_string<_CharT> string_type;
4202 //@}
4204 /// Numpunct facet id.
4205 static locale::id id;
4208 * @brief Constructor performs initialization.
4210 * This is the constructor provided by the standard.
4212 * @param refs Passed to the base facet class.
4214 explicit
4215 money_put(size_t __refs = 0) : facet(__refs) { }
4218 * @brief Format and output a monetary value.
4220 * This function formats @a units as a monetary value according to
4221 * moneypunct and ctype facets retrieved from io.getloc(), and writes
4222 * the resulting characters to @a s. For example, the value 1001 in a
4223 * US locale would write "$10.01" to @a s.
4225 * This function works by returning the result of do_put().
4227 * @param s The stream to write to.
4228 * @param intl Parameter to use_facet<moneypunct<CharT,intl> >.
4229 * @param io Source of facets and io state.
4230 * @param fill char_type to use for padding.
4231 * @param units Place to store result of parsing.
4232 * @return Iterator after writing.
4234 iter_type
4235 put(iter_type __s, bool __intl, ios_base& __io,
4236 char_type __fill, long double __units) const
4237 { return this->do_put(__s, __intl, __io, __fill, __units); }
4240 * @brief Format and output a monetary value.
4242 * This function formats @a digits 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 string "1001" in
4245 * a US locale would write "$10.01" to @a s.
4247 * This function works by returning the result of do_put().
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.
4256 iter_type
4257 put(iter_type __s, bool __intl, ios_base& __io,
4258 char_type __fill, const string_type& __digits) const
4259 { return this->do_put(__s, __intl, __io, __fill, __digits); }
4261 protected:
4262 /// Destructor.
4263 virtual
4264 ~money_put() { }
4267 * @brief Format and output a monetary value.
4269 * This function formats @a units as a monetary value according to
4270 * moneypunct and ctype facets retrieved from io.getloc(), and writes
4271 * the resulting characters to @a s. For example, the value 1001 in a
4272 * US locale would write "$10.01" to @a s.
4274 * This function is a hook for derived classes to change the value
4275 * returned. @see put().
4277 * @param s The stream to write to.
4278 * @param intl Parameter to use_facet<moneypunct<CharT,intl> >.
4279 * @param io Source of facets and io state.
4280 * @param fill char_type to use for padding.
4281 * @param units Place to store result of parsing.
4282 * @return Iterator after writing.
4284 // XXX GLIBCXX_ABI Deprecated
4285 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
4286 virtual iter_type
4287 __do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
4288 double __units) const;
4289 #else
4290 virtual iter_type
4291 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
4292 long double __units) const;
4293 #endif
4296 * @brief Format and output a monetary value.
4298 * This function formats @a digits as a monetary value according to
4299 * moneypunct and ctype facets retrieved from io.getloc(), and writes
4300 * the resulting characters to @a s. For example, the string "1001" in
4301 * a US locale would write "$10.01" to @a s.
4303 * This function is a hook for derived classes to change the value
4304 * returned. @see put().
4306 * @param s The stream to write to.
4307 * @param intl Parameter to use_facet<moneypunct<CharT,intl> >.
4308 * @param io Source of facets and io state.
4309 * @param fill char_type to use for padding.
4310 * @param units Place to store result of parsing.
4311 * @return Iterator after writing.
4313 virtual iter_type
4314 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
4315 const string_type& __digits) const;
4317 // XXX GLIBCXX_ABI Deprecated
4318 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
4319 virtual iter_type
4320 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
4321 long double __units) const;
4322 #endif
4324 template<bool _Intl>
4325 iter_type
4326 _M_insert(iter_type __s, ios_base& __io, char_type __fill,
4327 const string_type& __digits) const;
4330 template<typename _CharT, typename _OutIter>
4331 locale::id money_put<_CharT, _OutIter>::id;
4333 _GLIBCXX_END_LDBL_NAMESPACE
4336 * @brief Messages facet base class providing catalog typedef.
4338 struct messages_base
4340 typedef int catalog;
4344 * @brief Facet for handling message catalogs
4346 * This facet encapsulates the code to retrieve messages from
4347 * message catalogs. The only thing defined by the standard for this facet
4348 * is the interface. All underlying functionality is
4349 * implementation-defined.
4351 * This library currently implements 3 versions of the message facet. The
4352 * first version (gnu) is a wrapper around gettext, provided by libintl.
4353 * The second version (ieee) is a wrapper around catgets. The final
4354 * version (default) does no actual translation. These implementations are
4355 * only provided for char and wchar_t instantiations.
4357 * The messages template uses protected virtual functions to
4358 * provide the actual results. The public accessors forward the
4359 * call to the virtual functions. These virtual functions are
4360 * hooks for developers to implement the behavior they require from
4361 * the messages facet.
4363 template<typename _CharT>
4364 class messages : public locale::facet, public messages_base
4366 public:
4367 // Types:
4368 //@{
4369 /// Public typedefs
4370 typedef _CharT char_type;
4371 typedef basic_string<_CharT> string_type;
4372 //@}
4374 protected:
4375 // Underlying "C" library locale information saved from
4376 // initialization, needed by messages_byname as well.
4377 __c_locale _M_c_locale_messages;
4378 const char* _M_name_messages;
4380 public:
4381 /// Numpunct facet id.
4382 static locale::id id;
4385 * @brief Constructor performs initialization.
4387 * This is the constructor provided by the standard.
4389 * @param refs Passed to the base facet class.
4391 explicit
4392 messages(size_t __refs = 0);
4394 // Non-standard.
4396 * @brief Internal constructor. Not for general use.
4398 * This is a constructor for use by the library itself to set up new
4399 * locales.
4401 * @param cloc The "C" locale.
4402 * @param s The name of a locale.
4403 * @param refs Refcount to pass to the base class.
4405 explicit
4406 messages(__c_locale __cloc, const char* __s, size_t __refs = 0);
4409 * @brief Open a message catalog.
4411 * This function opens and returns a handle to a message catalog by
4412 * returning do_open(s, loc).
4414 * @param s The catalog to open.
4415 * @param loc Locale to use for character set conversions.
4416 * @return Handle to the catalog or value < 0 if open fails.
4418 catalog
4419 open(const basic_string<char>& __s, const locale& __loc) const
4420 { return this->do_open(__s, __loc); }
4422 // Non-standard and unorthodox, yet effective.
4424 * @brief Open a message catalog.
4426 * This non-standard function opens and returns a handle to a message
4427 * catalog by returning do_open(s, loc). The third argument provides a
4428 * message catalog root directory for gnu gettext and is ignored
4429 * otherwise.
4431 * @param s The catalog to open.
4432 * @param loc Locale to use for character set conversions.
4433 * @param dir Message catalog root directory.
4434 * @return Handle to the catalog or value < 0 if open fails.
4436 catalog
4437 open(const basic_string<char>&, const locale&, const char*) const;
4440 * @brief Look up a string in a message catalog.
4442 * This function retrieves and returns a message from a catalog by
4443 * returning do_get(c, set, msgid, s).
4445 * For gnu, @a set and @a msgid are ignored. Returns gettext(s).
4446 * For default, returns s. For ieee, returns catgets(c,set,msgid,s).
4448 * @param c The catalog to access.
4449 * @param set Implementation-defined.
4450 * @param msgid Implementation-defined.
4451 * @param s Default return value if retrieval fails.
4452 * @return Retrieved message or @a s if get fails.
4454 string_type
4455 get(catalog __c, int __set, int __msgid, const string_type& __s) const
4456 { return this->do_get(__c, __set, __msgid, __s); }
4459 * @brief Close a message catalog.
4461 * Closes catalog @a c by calling do_close(c).
4463 * @param c The catalog to close.
4465 void
4466 close(catalog __c) const
4467 { return this->do_close(__c); }
4469 protected:
4470 /// Destructor.
4471 virtual
4472 ~messages();
4475 * @brief Open a message catalog.
4477 * This function opens and returns a handle to a message catalog in an
4478 * implementation-defined manner. This function is a hook for derived
4479 * classes to change the value returned.
4481 * @param s The catalog to open.
4482 * @param loc Locale to use for character set conversions.
4483 * @return Handle to the opened catalog, value < 0 if open failed.
4485 virtual catalog
4486 do_open(const basic_string<char>&, const locale&) const;
4489 * @brief Look up a string in a message catalog.
4491 * This function retrieves and returns a message from a catalog in an
4492 * implementation-defined manner. This function is a hook for derived
4493 * classes to change the value returned.
4495 * For gnu, @a set and @a msgid are ignored. Returns gettext(s).
4496 * For default, returns s. For ieee, returns catgets(c,set,msgid,s).
4498 * @param c The catalog to access.
4499 * @param set Implementation-defined.
4500 * @param msgid Implementation-defined.
4501 * @param s Default return value if retrieval fails.
4502 * @return Retrieved message or @a s if get fails.
4504 virtual string_type
4505 do_get(catalog, int, int, const string_type& __dfault) const;
4508 * @brief Close a message catalog.
4510 * @param c The catalog to close.
4512 virtual void
4513 do_close(catalog) const;
4515 // Returns a locale and codeset-converted string, given a char* message.
4516 char*
4517 _M_convert_to_char(const string_type& __msg) const
4519 // XXX
4520 return reinterpret_cast<char*>(const_cast<_CharT*>(__msg.c_str()));
4523 // Returns a locale and codeset-converted string, given a char* message.
4524 string_type
4525 _M_convert_from_char(char*) const
4527 #if 0
4528 // Length of message string without terminating null.
4529 size_t __len = char_traits<char>::length(__msg) - 1;
4531 // "everybody can easily convert the string using
4532 // mbsrtowcs/wcsrtombs or with iconv()"
4534 // Convert char* to _CharT in locale used to open catalog.
4535 // XXX need additional template parameter on messages class for this..
4536 // typedef typename codecvt<char, _CharT, _StateT> __codecvt_type;
4537 typedef typename codecvt<char, _CharT, mbstate_t> __codecvt_type;
4539 __codecvt_type::state_type __state;
4540 // XXX may need to initialize state.
4541 //initialize_state(__state._M_init());
4543 char* __from_next;
4544 // XXX what size for this string?
4545 _CharT* __to = static_cast<_CharT*>(__builtin_alloca(__len + 1));
4546 const __codecvt_type& __cvt = use_facet<__codecvt_type>(_M_locale_conv);
4547 __cvt.out(__state, __msg, __msg + __len, __from_next,
4548 __to, __to + __len + 1, __to_next);
4549 return string_type(__to);
4550 #endif
4551 #if 0
4552 typedef ctype<_CharT> __ctype_type;
4553 // const __ctype_type& __cvt = use_facet<__ctype_type>(_M_locale_msg);
4554 const __ctype_type& __cvt = use_facet<__ctype_type>(locale());
4555 // XXX Again, proper length of converted string an issue here.
4556 // For now, assume the converted length is not larger.
4557 _CharT* __dest = static_cast<_CharT*>(__builtin_alloca(__len + 1));
4558 __cvt.widen(__msg, __msg + __len, __dest);
4559 return basic_string<_CharT>(__dest);
4560 #endif
4561 return string_type();
4565 template<typename _CharT>
4566 locale::id messages<_CharT>::id;
4568 // Specializations for required instantiations.
4569 template<>
4570 string
4571 messages<char>::do_get(catalog, int, int, const string&) const;
4573 #ifdef _GLIBCXX_USE_WCHAR_T
4574 template<>
4575 wstring
4576 messages<wchar_t>::do_get(catalog, int, int, const wstring&) const;
4577 #endif
4579 /// @brief class messages_byname [22.2.7.2].
4580 template<typename _CharT>
4581 class messages_byname : public messages<_CharT>
4583 public:
4584 typedef _CharT char_type;
4585 typedef basic_string<_CharT> string_type;
4587 explicit
4588 messages_byname(const char* __s, size_t __refs = 0);
4590 protected:
4591 virtual
4592 ~messages_byname()
4596 _GLIBCXX_END_NAMESPACE
4598 // Include host and configuration specific messages functions.
4599 #include <bits/messages_members.h>
4601 _GLIBCXX_BEGIN_NAMESPACE(std)
4603 // Subclause convenience interfaces, inlines.
4604 // NB: These are inline because, when used in a loop, some compilers
4605 // can hoist the body out of the loop; then it's just as fast as the
4606 // C is*() function.
4608 /// Convenience interface to ctype.is(ctype_base::space, __c).
4609 template<typename _CharT>
4610 inline bool
4611 isspace(_CharT __c, const locale& __loc)
4612 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c); }
4614 /// Convenience interface to ctype.is(ctype_base::print, __c).
4615 template<typename _CharT>
4616 inline bool
4617 isprint(_CharT __c, const locale& __loc)
4618 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c); }
4620 /// Convenience interface to ctype.is(ctype_base::cntrl, __c).
4621 template<typename _CharT>
4622 inline bool
4623 iscntrl(_CharT __c, const locale& __loc)
4624 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c); }
4626 /// Convenience interface to ctype.is(ctype_base::upper, __c).
4627 template<typename _CharT>
4628 inline bool
4629 isupper(_CharT __c, const locale& __loc)
4630 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c); }
4632 /// Convenience interface to ctype.is(ctype_base::lower, __c).
4633 template<typename _CharT>
4634 inline bool
4635 islower(_CharT __c, const locale& __loc)
4636 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c); }
4638 /// Convenience interface to ctype.is(ctype_base::alpha, __c).
4639 template<typename _CharT>
4640 inline bool
4641 isalpha(_CharT __c, const locale& __loc)
4642 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c); }
4644 /// Convenience interface to ctype.is(ctype_base::digit, __c).
4645 template<typename _CharT>
4646 inline bool
4647 isdigit(_CharT __c, const locale& __loc)
4648 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c); }
4650 /// Convenience interface to ctype.is(ctype_base::punct, __c).
4651 template<typename _CharT>
4652 inline bool
4653 ispunct(_CharT __c, const locale& __loc)
4654 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c); }
4656 /// Convenience interface to ctype.is(ctype_base::xdigit, __c).
4657 template<typename _CharT>
4658 inline bool
4659 isxdigit(_CharT __c, const locale& __loc)
4660 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c); }
4662 /// Convenience interface to ctype.is(ctype_base::alnum, __c).
4663 template<typename _CharT>
4664 inline bool
4665 isalnum(_CharT __c, const locale& __loc)
4666 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c); }
4668 /// Convenience interface to ctype.is(ctype_base::graph, __c).
4669 template<typename _CharT>
4670 inline bool
4671 isgraph(_CharT __c, const locale& __loc)
4672 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c); }
4674 /// Convenience interface to ctype.toupper(__c).
4675 template<typename _CharT>
4676 inline _CharT
4677 toupper(_CharT __c, const locale& __loc)
4678 { return use_facet<ctype<_CharT> >(__loc).toupper(__c); }
4680 /// Convenience interface to ctype.tolower(__c).
4681 template<typename _CharT>
4682 inline _CharT
4683 tolower(_CharT __c, const locale& __loc)
4684 { return use_facet<ctype<_CharT> >(__loc).tolower(__c); }
4686 _GLIBCXX_END_NAMESPACE
4688 #endif