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