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