-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocale_facets.h
4688 lines (4230 loc) · 157 KB
/
locale_facets.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Locale support -*- C++ -*-
// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
// Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// As a special exception, you may use this file as part of a free software
// library without restriction. Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License. This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.
/** @file locale_facets.h
* This is an internal header file, included by other library headers.
* You should not attempt to use it directly.
*/
//
// ISO C++ 14882: 22.1 Locales
//
#ifndef _LOCALE_FACETS_H
#define _LOCALE_FACETS_H 1
#pragma GCC system_header
#include <ctime> // For struct tm
#include <cwctype> // For wctype_t
#include <bits/ctype_base.h>
#include <iosfwd>
#include <bits/ios_base.h> // For ios_base, ios_base::iostate
#include <streambuf>
#include <bits/cpp_type_traits.h>
namespace std {
// NB: Don't instantiate required wchar_t facets if no wchar_t support.
#ifdef _GLIBCXX_USE_WCHAR_T
# define _GLIBCXX_NUM_FACETS 28
#else
# define _GLIBCXX_NUM_FACETS 14
#endif
// Convert string to numeric value of type _Tv and store results.
// NB: This is specialized for all required types, there is no
// generic definition.
template<typename _Tv>
void
__convert_to_v(const char* __in, _Tv& __out, ios_base::iostate& __err,
const __c_locale& __cloc);
// Explicit specializations for required types.
template<>
void
__convert_to_v(const char*, float&, ios_base::iostate&,
const __c_locale&);
template<>
void
__convert_to_v(const char*, double&, ios_base::iostate&,
const __c_locale&);
template<>
void
__convert_to_v(const char*, long double&, ios_base::iostate&,
const __c_locale&);
// NB: __pad is a struct, rather than a function, so it can be
// partially-specialized.
template<typename _CharT, typename _Traits>
struct __pad
{
static void
_S_pad(ios_base& __io, _CharT __fill, _CharT* __news,
const _CharT* __olds, const streamsize __newlen,
const streamsize __oldlen, const bool __num);
};
// Used by both numeric and monetary facets.
// Inserts "group separator" characters into an array of characters.
// It's recursive, one iteration per group. It moves the characters
// in the buffer this way: "xxxx12345" -> "12,345xxx". Call this
// only with __glen != 0.
template<typename _CharT>
_CharT*
__add_grouping(_CharT* __s, _CharT __sep,
const char* __gbeg, size_t __gsize,
const _CharT* __first, const _CharT* __last);
// This template permits specializing facet output code for
// ostreambuf_iterator. For ostreambuf_iterator, sputn is
// significantly more efficient than incrementing iterators.
template<typename _CharT>
inline
ostreambuf_iterator<_CharT>
__write(ostreambuf_iterator<_CharT> __s, const _CharT* __ws, int __len)
{
__s._M_put(__ws, __len);
return __s;
}
// This is the unspecialized form of the template.
template<typename _CharT, typename _OutIter>
inline
_OutIter
__write(_OutIter __s, const _CharT* __ws, int __len)
{
for (int __j = 0; __j < __len; __j++, ++__s)
*__s = __ws[__j];
return __s;
}
// 22.2.1.1 Template class ctype
// Include host and configuration specific ctype enums for ctype_base.
// Common base for ctype<_CharT>.
/**
* @brief Common base for ctype facet
*
* This template class provides implementations of the public functions
* that forward to the protected virtual functions.
*
* This template also provides abtract stubs for the protected virtual
* functions.
*/
template<typename _CharT>
class __ctype_abstract_base : public locale::facet, public ctype_base
{
public:
// Types:
/// Typedef for the template parameter
typedef _CharT char_type;
/**
* @brief Test char_type classification.
*
* This function finds a mask M for @a c and compares it to mask @a m.
* It does so by returning the value of ctype<char_type>::do_is().
*
* @param c The char_type to compare the mask of.
* @param m The mask to compare against.
* @return (M & m) != 0.
*/
bool
is(mask __m, char_type __c) const
{ return this->do_is(__m, __c); }
/**
* @brief Return a mask array.
*
* This function finds the mask for each char_type in the range [lo,hi)
* and successively writes it to vec. vec must have as many elements
* as the char array. It does so by returning the value of
* ctype<char_type>::do_is().
*
* @param lo Pointer to start of range.
* @param hi Pointer to end of range.
* @param vec Pointer to an array of mask storage.
* @return @a hi.
*/
const char_type*
is(const char_type *__lo, const char_type *__hi, mask *__vec) const
{ return this->do_is(__lo, __hi, __vec); }
/**
* @brief Find char_type matching a mask
*
* This function searches for and returns the first char_type c in
* [lo,hi) for which is(m,c) is true. It does so by returning
* ctype<char_type>::do_scan_is().
*
* @param m The mask to compare against.
* @param lo Pointer to start of range.
* @param hi Pointer to end of range.
* @return Pointer to matching char_type if found, else @a hi.
*/
const char_type*
scan_is(mask __m, const char_type* __lo, const char_type* __hi) const
{ return this->do_scan_is(__m, __lo, __hi); }
/**
* @brief Find char_type not matching a mask
*
* This function searches for and returns the first char_type c in
* [lo,hi) for which is(m,c) is false. It does so by returning
* ctype<char_type>::do_scan_not().
*
* @param m The mask to compare against.
* @param lo Pointer to first char in range.
* @param hi Pointer to end of range.
* @return Pointer to non-matching char if found, else @a hi.
*/
const char_type*
scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
{ return this->do_scan_not(__m, __lo, __hi); }
/**
* @brief Convert to uppercase.
*
* This function converts the argument to uppercase if possible.
* If not possible (for example, '2'), returns the argument. It does
* so by returning ctype<char_type>::do_toupper().
*
* @param c The char_type to convert.
* @return The uppercase char_type if convertible, else @a c.
*/
char_type
toupper(char_type __c) const
{ return this->do_toupper(__c); }
/**
* @brief Convert array to uppercase.
*
* This function converts each char_type in the range [lo,hi) to
* uppercase if possible. Other elements remain untouched. It does so
* by returning ctype<char_type>:: do_toupper(lo, hi).
*
* @param lo Pointer to start of range.
* @param hi Pointer to end of range.
* @return @a hi.
*/
const char_type*
toupper(char_type *__lo, const char_type* __hi) const
{ return this->do_toupper(__lo, __hi); }
/**
* @brief Convert to lowercase.
*
* This function converts the argument to lowercase if possible. If
* not possible (for example, '2'), returns the argument. It does so
* by returning ctype<char_type>::do_tolower(c).
*
* @param c The char_type to convert.
* @return The lowercase char_type if convertible, else @a c.
*/
char_type
tolower(char_type __c) const
{ return this->do_tolower(__c); }
/**
* @brief Convert array to lowercase.
*
* This function converts each char_type in the range [lo,hi) to
* lowercase if possible. Other elements remain untouched. It does so
* by returning ctype<char_type>:: do_tolower(lo, hi).
*
* @param lo Pointer to start of range.
* @param hi Pointer to end of range.
* @return @a hi.
*/
const char_type*
tolower(char_type* __lo, const char_type* __hi) const
{ return this->do_tolower(__lo, __hi); }
/**
* @brief Widen char to char_type
*
* This function converts the char argument to char_type using the
* simplest reasonable transformation. It does so by returning
* ctype<char_type>::do_widen(c).
*
* Note: this is not what you want for codepage conversions. See
* codecvt for that.
*
* @param c The char to convert.
* @return The converted char_type.
*/
char_type
widen(char __c) const
{ return this->do_widen(__c); }
/**
* @brief Widen array to char_type
*
* This function converts each char in the input to char_type using the
* simplest reasonable transformation. It does so by returning
* ctype<char_type>::do_widen(c).
*
* Note: this is not what you want for codepage conversions. See
* codecvt for that.
*
* @param lo Pointer to start of range.
* @param hi Pointer to end of range.
* @param to Pointer to the destination array.
* @return @a hi.
*/
const char*
widen(const char* __lo, const char* __hi, char_type* __to) const
{ return this->do_widen(__lo, __hi, __to); }
/**
* @brief Narrow char_type to char
*
* This function converts the char_type to char using the simplest
* reasonable transformation. If the conversion fails, dfault is
* returned instead. It does so by returning
* ctype<char_type>::do_narrow(c).
*
* Note: this is not what you want for codepage conversions. See
* codecvt for that.
*
* @param c The char_type to convert.
* @param dfault Char to return if conversion fails.
* @return The converted char.
*/
char
narrow(char_type __c, char __dfault) const
{ return this->do_narrow(__c, __dfault); }
/**
* @brief Narrow array to char array
*
* This function converts each char_type in the input to char using the
* simplest reasonable transformation and writes the results to the
* destination array. For any char_type in the input that cannot be
* converted, @a dfault is used instead. It does so by returning
* ctype<char_type>::do_narrow(lo, hi, dfault, to).
*
* Note: this is not what you want for codepage conversions. See
* codecvt for that.
*
* @param lo Pointer to start of range.
* @param hi Pointer to end of range.
* @param dfault Char to use if conversion fails.
* @param to Pointer to the destination array.
* @return @a hi.
*/
const char_type*
narrow(const char_type* __lo, const char_type* __hi,
char __dfault, char *__to) const
{ return this->do_narrow(__lo, __hi, __dfault, __to); }
protected:
explicit
__ctype_abstract_base(size_t __refs = 0): facet(__refs) { }
virtual
~__ctype_abstract_base() { }
/**
* @brief Test char_type classification.
*
* This function finds a mask M for @a c and compares it to mask @a m.
*
* do_is() is a hook for a derived facet to change the behavior of
* classifying. do_is() must always return the same result for the
* same input.
*
* @param c The char_type to find the mask of.
* @param m The mask to compare against.
* @return (M & m) != 0.
*/
virtual bool
do_is(mask __m, char_type __c) const = 0;
/**
* @brief Return a mask array.
*
* This function finds the mask for each char_type in the range [lo,hi)
* and successively writes it to vec. vec must have as many elements
* as the input.
*
* do_is() is a hook for a derived facet to change the behavior of
* classifying. do_is() must always return the same result for the
* same input.
*
* @param lo Pointer to start of range.
* @param hi Pointer to end of range.
* @param vec Pointer to an array of mask storage.
* @return @a hi.
*/
virtual const char_type*
do_is(const char_type* __lo, const char_type* __hi,
mask* __vec) const = 0;
/**
* @brief Find char_type matching mask
*
* This function searches for and returns the first char_type c in
* [lo,hi) for which is(m,c) is true.
*
* do_scan_is() is a hook for a derived facet to change the behavior of
* match searching. do_is() must always return the same result for the
* same input.
*
* @param m The mask to compare against.
* @param lo Pointer to start of range.
* @param hi Pointer to end of range.
* @return Pointer to a matching char_type if found, else @a hi.
*/
virtual const char_type*
do_scan_is(mask __m, const char_type* __lo,
const char_type* __hi) const = 0;
/**
* @brief Find char_type not matching mask
*
* This function searches for and returns a pointer to the first
* char_type c of [lo,hi) for which is(m,c) is false.
*
* do_scan_is() is a hook for a derived facet to change the behavior of
* match searching. do_is() must always return the same result for the
* same input.
*
* @param m The mask to compare against.
* @param lo Pointer to start of range.
* @param hi Pointer to end of range.
* @return Pointer to a non-matching char_type if found, else @a hi.
*/
virtual const char_type*
do_scan_not(mask __m, const char_type* __lo,
const char_type* __hi) const = 0;
/**
* @brief Convert to uppercase.
*
* This virtual function converts the char_type argument to uppercase
* if possible. If not possible (for example, '2'), returns the
* argument.
*
* do_toupper() is a hook for a derived facet to change the behavior of
* uppercasing. do_toupper() must always return the same result for
* the same input.
*
* @param c The char_type to convert.
* @return The uppercase char_type if convertible, else @a c.
*/
virtual char_type
do_toupper(char_type) const = 0;
/**
* @brief Convert array to uppercase.
*
* This virtual function converts each char_type in the range [lo,hi)
* to uppercase if possible. Other elements remain untouched.
*
* do_toupper() is a hook for a derived facet to change the behavior of
* uppercasing. do_toupper() must always return the same result for
* the same input.
*
* @param lo Pointer to start of range.
* @param hi Pointer to end of range.
* @return @a hi.
*/
virtual const char_type*
do_toupper(char_type* __lo, const char_type* __hi) const = 0;
/**
* @brief Convert to lowercase.
*
* This virtual function converts the argument to lowercase if
* possible. If not possible (for example, '2'), returns the argument.
*
* do_tolower() is a hook for a derived facet to change the behavior of
* lowercasing. do_tolower() must always return the same result for
* the same input.
*
* @param c The char_type to convert.
* @return The lowercase char_type if convertible, else @a c.
*/
virtual char_type
do_tolower(char_type) const = 0;
/**
* @brief Convert array to lowercase.
*
* This virtual function converts each char_type in the range [lo,hi)
* to lowercase if possible. Other elements remain untouched.
*
* do_tolower() is a hook for a derived facet to change the behavior of
* lowercasing. do_tolower() must always return the same result for
* the same input.
*
* @param lo Pointer to start of range.
* @param hi Pointer to end of range.
* @return @a hi.
*/
virtual const char_type*
do_tolower(char_type* __lo, const char_type* __hi) const = 0;
/**
* @brief Widen char
*
* This virtual function converts the char to char_type using the
* simplest reasonable transformation.
*
* do_widen() is a hook for a derived facet to change the behavior of
* widening. do_widen() must always return the same result for the
* same input.
*
* Note: this is not what you want for codepage conversions. See
* codecvt for that.
*
* @param c The char to convert.
* @return The converted char_type
*/
virtual char_type
do_widen(char) const = 0;
/**
* @brief Widen char array
*
* This function converts each char in the input to char_type using the
* simplest reasonable transformation.
*
* do_widen() is a hook for a derived facet to change the behavior of
* widening. do_widen() must always return the same result for the
* same input.
*
* Note: this is not what you want for codepage conversions. See
* codecvt for that.
*
* @param lo Pointer to start range.
* @param hi Pointer to end of range.
* @param to Pointer to the destination array.
* @return @a hi.
*/
virtual const char*
do_widen(const char* __lo, const char* __hi,
char_type* __dest) const = 0;
/**
* @brief Narrow char_type to char
*
* This virtual function converts the argument to char using the
* simplest reasonable transformation. If the conversion fails, dfault
* is returned instead.
*
* do_narrow() is a hook for a derived facet to change the behavior of
* narrowing. do_narrow() must always return the same result for the
* same input.
*
* Note: this is not what you want for codepage conversions. See
* codecvt for that.
*
* @param c The char_type to convert.
* @param dfault Char to return if conversion fails.
* @return The converted char.
*/
virtual char
do_narrow(char_type, char __dfault) const = 0;
/**
* @brief Narrow char_type array to char
*
* This virtual function converts each char_type in the range [lo,hi) to
* char using the simplest reasonable transformation and writes the
* results to the destination array. For any element in the input that
* cannot be converted, @a dfault is used instead.
*
* do_narrow() is a hook for a derived facet to change the behavior of
* narrowing. do_narrow() must always return the same result for the
* same input.
*
* Note: this is not what you want for codepage conversions. See
* codecvt for that.
*
* @param lo Pointer to start of range.
* @param hi Pointer to end of range.
* @param dfault Char to use if conversion fails.
* @param to Pointer to the destination array.
* @return @a hi.
*/
virtual const char_type*
do_narrow(const char_type* __lo, const char_type* __hi,
char __dfault, char* __dest) const = 0;
};
// NB: Generic, mostly useless implementation.
/**
* @brief Template ctype facet
*
* This template class defines classification and conversion functions for
* character sets. It wraps <cctype> functionality. Ctype gets used by
* streams for many I/O operations.
*
* This template provides the protected virtual functions the developer
* will have to replace in a derived class or specialization to make a
* working facet. The public functions that access them are defined in
* __ctype_abstract_base, to allow for implementation flexibility. See
* ctype<wchar_t> for an example. The functions are documented in
* __ctype_abstract_base.
*
* Note: implementations are provided for all the protected virtual
* functions, but will likely not be useful.
*/
template<typename _CharT>
class ctype : public __ctype_abstract_base<_CharT>
{
public:
// Types:
typedef _CharT char_type;
typedef typename __ctype_abstract_base<_CharT>::mask mask;
/// The facet id for ctype<char_type>
static locale::id id;
explicit
ctype(size_t __refs = 0) : __ctype_abstract_base<_CharT>(__refs) { }
protected:
virtual
~ctype();
virtual bool
do_is(mask __m, char_type __c) const;
virtual const char_type*
do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const;
virtual const char_type*
do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const;
virtual const char_type*
do_scan_not(mask __m, const char_type* __lo,
const char_type* __hi) const;
virtual char_type
do_toupper(char_type __c) const;
virtual const char_type*
do_toupper(char_type* __lo, const char_type* __hi) const;
virtual char_type
do_tolower(char_type __c) const;
virtual const char_type*
do_tolower(char_type* __lo, const char_type* __hi) const;
virtual char_type
do_widen(char __c) const;
virtual const char*
do_widen(const char* __lo, const char* __hi, char_type* __dest) const;
virtual char
do_narrow(char_type, char __dfault) const;
virtual const char_type*
do_narrow(const char_type* __lo, const char_type* __hi,
char __dfault, char* __dest) const;
};
template<typename _CharT>
locale::id ctype<_CharT>::id;
// 22.2.1.3 ctype<char> specialization.
/**
* @brief The ctype<char> specialization.
*
* This class defines classification and conversion functions for
* the char type. It gets used by char streams for many I/O
* operations. The char specialization provides a number of
* optimizations as well.
*/
template<>
class ctype<char> : public locale::facet, public ctype_base
{
public:
// Types:
/// Typedef for the template parameter char.
typedef char char_type;
protected:
// Data Members:
__c_locale _M_c_locale_ctype;
bool _M_del;
__to_type _M_toupper;
__to_type _M_tolower;
const mask* _M_table;
mutable char _M_widen_ok;
mutable char _M_widen[1 + static_cast<unsigned char>(-1)];
mutable char _M_narrow[1 + static_cast<unsigned char>(-1)];
mutable char _M_narrow_ok; // 0 uninitialized, 1 init,
// 2 memcpy can't be used
public:
/// The facet id for ctype<char>
static locale::id id;
/// The size of the mask table. It is SCHAR_MAX + 1.
static const size_t table_size = 1 + static_cast<unsigned char>(-1);
/**
* @brief Constructor performs initialization.
*
* This is the constructor provided by the standard.
*
* @param table If non-zero, table is used as the per-char mask.
* Else classic_table() is used.
* @param del If true, passes ownership of table to this facet.
* @param refs Passed to the base facet class.
*/
explicit
ctype(const mask* __table = 0, bool __del = false, size_t __refs = 0);
/**
* @brief Constructor performs static initialization.
*
* This constructor is used to construct the initial C locale facet.
*
* @param cloc Handle to C locale data.
* @param table If non-zero, table is used as the per-char mask.
* @param del If true, passes ownership of table to this facet.
* @param refs Passed to the base facet class.
*/
explicit
ctype(__c_locale __cloc, const mask* __table = 0, bool __del = false,
size_t __refs = 0);
/**
* @brief Test char classification.
*
* This function compares the mask table[c] to @a m.
*
* @param c The char to compare the mask of.
* @param m The mask to compare against.
* @return True if m & table[c] is true, false otherwise.
*/
inline bool
is(mask __m, char __c) const;
/**
* @brief Return a mask array.
*
* This function finds the mask for each char in the range [lo, hi) and
* successively writes it to vec. vec must have as many elements as
* the char array.
*
* @param lo Pointer to start of range.
* @param hi Pointer to end of range.
* @param vec Pointer to an array of mask storage.
* @return @a hi.
*/
inline const char*
is(const char* __lo, const char* __hi, mask* __vec) const;
/**
* @brief Find char matching a mask
*
* This function searches for and returns the first char in [lo,hi) for
* which is(m,char) is true.
*
* @param m The mask to compare against.
* @param lo Pointer to start of range.
* @param hi Pointer to end of range.
* @return Pointer to a matching char if found, else @a hi.
*/
inline const char*
scan_is(mask __m, const char* __lo, const char* __hi) const;
/**
* @brief Find char not matching a mask
*
* This function searches for and returns a pointer to the first char
* in [lo,hi) for which is(m,char) is false.
*
* @param m The mask to compare against.
* @param lo Pointer to start of range.
* @param hi Pointer to end of range.
* @return Pointer to a non-matching char if found, else @a hi.
*/
inline const char*
scan_not(mask __m, const char* __lo, const char* __hi) const;
/**
* @brief Convert to uppercase.
*
* This function converts the char argument to uppercase if possible.
* If not possible (for example, '2'), returns the argument.
*
* toupper() acts as if it returns ctype<char>::do_toupper(c).
* do_toupper() must always return the same result for the same input.
*
* @param c The char to convert.
* @return The uppercase char if convertible, else @a c.
*/
char_type
toupper(char_type __c) const
{ return this->do_toupper(__c); }
/**
* @brief Convert array to uppercase.
*
* This function converts each char in the range [lo,hi) to uppercase
* if possible. Other chars remain untouched.
*
* toupper() acts as if it returns ctype<char>:: do_toupper(lo, hi).
* do_toupper() must always return the same result for the same input.
*
* @param lo Pointer to first char in range.
* @param hi Pointer to end of range.
* @return @a hi.
*/
const char_type*
toupper(char_type *__lo, const char_type* __hi) const
{ return this->do_toupper(__lo, __hi); }
/**
* @brief Convert to lowercase.
*
* This function converts the char argument to lowercase if possible.
* If not possible (for example, '2'), returns the argument.
*
* tolower() acts as if it returns ctype<char>::do_tolower(c).
* do_tolower() must always return the same result for the same input.
*
* @param c The char to convert.
* @return The lowercase char if convertible, else @a c.
*/
char_type
tolower(char_type __c) const
{ return this->do_tolower(__c); }
/**
* @brief Convert array to lowercase.
*
* This function converts each char in the range [lo,hi) to lowercase
* if possible. Other chars remain untouched.
*
* tolower() acts as if it returns ctype<char>:: do_tolower(lo, hi).
* do_tolower() must always return the same result for the same input.
*
* @param lo Pointer to first char in range.
* @param hi Pointer to end of range.
* @return @a hi.
*/
const char_type*
tolower(char_type* __lo, const char_type* __hi) const
{ return this->do_tolower(__lo, __hi); }
/**
* @brief Widen char
*
* This function converts the char to char_type using the simplest
* reasonable transformation. For an underived ctype<char> facet, the
* argument will be returned unchanged.
*
* This function works as if it returns ctype<char>::do_widen(c).
* do_widen() must always return the same result for the same input.
*
* Note: this is not what you want for codepage conversions. See
* codecvt for that.
*
* @param c The char to convert.
* @return The converted character.
*/
char_type
widen(char __c) const
{
if (_M_widen_ok)
return _M_widen[static_cast<unsigned char>(__c)];
this->_M_widen_init();
return this->do_widen(__c);
}
/**
* @brief Widen char array
*
* This function converts each char in the input to char using the
* simplest reasonable transformation. For an underived ctype<char>
* facet, the argument will be copied unchanged.
*
* This function works as if it returns ctype<char>::do_widen(c).
* do_widen() must always return the same result for the same input.
*
* Note: this is not what you want for codepage conversions. See
* codecvt for that.
*
* @param lo Pointer to first char in range.
* @param hi Pointer to end of range.
* @param to Pointer to the destination array.
* @return @a hi.
*/
const char*
widen(const char* __lo, const char* __hi, char_type* __to) const
{
if (_M_widen_ok == 1)
{
memcpy(__to, __lo, __hi - __lo);
return __hi;
}
if (!_M_widen_ok)
_M_widen_init();
return this->do_widen(__lo, __hi, __to);
}
/**
* @brief Narrow char
*
* This function converts the char to char using the simplest
* reasonable transformation. If the conversion fails, dfault is
* returned instead. For an underived ctype<char> facet, @a c
* will be returned unchanged.
*
* This function works as if it returns ctype<char>::do_narrow(c).
* do_narrow() must always return the same result for the same input.
*
* Note: this is not what you want for codepage conversions. See
* codecvt for that.
*
* @param c The char to convert.
* @param dfault Char to return if conversion fails.
* @return The converted character.
*/
char
narrow(char_type __c, char __dfault) const
{
if (_M_narrow[static_cast<unsigned char>(__c)])
return _M_narrow[static_cast<unsigned char>(__c)];
const char __t = do_narrow(__c, __dfault);
if (__t != __dfault)
_M_narrow[static_cast<unsigned char>(__c)] = __t;
return __t;
}
/**
* @brief Narrow char array
*
* This function converts each char in the input to char using the
* simplest reasonable transformation and writes the results to the
* destination array. For any char in the input that cannot be
* converted, @a dfault is used instead. For an underived ctype<char>
* facet, the argument will be copied unchanged.
*
* This function works as if it returns ctype<char>::do_narrow(lo, hi,
* dfault, to). do_narrow() must always return the same result for the
* same input.
*
* Note: this is not what you want for codepage conversions. See
* codecvt for that.
*
* @param lo Pointer to start of range.
* @param hi Pointer to end of range.
* @param dfault Char to use if conversion fails.
* @param to Pointer to the destination array.
* @return @a hi.
*/
const char_type*
narrow(const char_type* __lo, const char_type* __hi,
char __dfault, char *__to) const
{
if (__builtin_expect(_M_narrow_ok == 1, true))
{
memcpy(__to, __lo, __hi - __lo);
return __hi;
}
if (!_M_narrow_ok)
_M_narrow_init();
return this->do_narrow(__lo, __hi, __dfault, __to);
}
protected:
/// Returns a pointer to the mask table provided to the constructor, or
/// the default from classic_table() if none was provided.
const mask*
table() const throw()
{ return _M_table; }
/// Returns a pointer to the C locale mask table.
static const mask*
classic_table() throw();
/**
* @brief Destructor.
*
* This function deletes table() if @a del was true in the
* constructor.
*/
virtual
~ctype();
/**
* @brief Convert to uppercase.
*
* This virtual function converts the char argument to uppercase if
* possible. If not possible (for example, '2'), returns the argument.
*
* do_toupper() is a hook for a derived facet to change the behavior of
* uppercasing. do_toupper() must always return the same result for
* the same input.
*