3 // Testing character type and state type with char_traits and codecvt
4 // specializations for the C++ library testsuite.
6 // Copyright (C) 2003, 2005 Free Software Foundation, Inc.
8 // This file is part of the GNU ISO C++ Library. This library is free
9 // software; you can redistribute it and/or modify it under the
10 // terms of the GNU General Public License as published by the
11 // Free Software Foundation; either version 2, or (at your option)
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License along
20 // with this library; see the file COPYING. If not, write to the Free
21 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
24 // As a special exception, you may use this file as part of a free software
25 // library without restriction. Specifically, if other files instantiate
26 // templates or use macros or inline functions from this file, or you compile
27 // this file and link it with other files to produce an executable, this
28 // file does not by itself cause the resulting executable to be covered by
29 // the GNU General Public License. This exception does not however
30 // invalidate any other reasons why the executable file might be covered by
31 // the GNU General Public License.
33 #ifndef _GLIBCXX_TESTSUITE_CHARACTER_H
34 #define _GLIBCXX_TESTSUITE_CHARACTER_H
37 #include <string> // for char_traits
38 #include <locale> // for codecvt
39 #include <ext/pod_char_traits.h>
49 operator==(const pod_int
& lhs
, const pod_int
& rhs
)
50 { return lhs
.value
== rhs
.value
; }
53 operator<(const pod_int
& lhs
, const pod_int
& rhs
)
54 { return lhs
.value
< rhs
.value
; }
62 operator==(const pod_state
& lhs
, const pod_state
& rhs
)
63 { return lhs
.value
== rhs
.value
; }
66 operator<(const pod_state
& lhs
, const pod_state
& rhs
)
67 { return lhs
.value
< rhs
.value
; }
69 // Alternate character types.
70 using __gnu_cxx::character
;
71 typedef character
<unsigned char, pod_int
, pod_state
> pod_char
;
72 typedef character
<unsigned char, unsigned int, pod_state
> pod_uchar
;
73 typedef character
<unsigned short, unsigned int> pod_ushort
;
74 typedef character
<unsigned int, unsigned long> pod_uint
;
83 inline __gnu_test::pod_char::char_type
84 __gnu_test::pod_char::char_type::from(const V2
& v
)
86 char_type ret
= { static_cast<value_type
>(v
.value
) };
93 __gnu_test::pod_char::char_type::to(const char_type
& c
)
100 template<typename V2
>
101 inline __gnu_test::pod_uchar::char_type
102 __gnu_test::pod_uchar::char_type::from(const V2
& v
)
105 ret
.value
= (v
>> 5);
110 template<typename V2
>
112 __gnu_test::pod_uchar::char_type::to(const char_type
& c
)
113 { return static_cast<V2
>(c
.value
<< 5); }
114 }; // namespace __gnu_test
118 // codecvt specialization
120 // The conversion performed by the specialization is not supposed to
121 // be useful, rather it has been designed to demonstrate the
122 // essential features of stateful conversions:
123 // * Number and value of bytes for each internal character depends on the
124 // state in addition to the character itself.
125 // * Unshift produces an unshift sequence and resets the state. On input
126 // the unshift sequence causes the state to be reset.
128 // The conversion for output is as follows:
129 // 1. Calculate the value tmp by xor-ing the state and the internal
131 // 2. Split tmp into either two or three bytes depending on the value of
132 // state. Output those bytes.
133 // 3. tmp becomes the new value of state.
135 class codecvt
<__gnu_test::pod_uchar
, char, __gnu_test::pod_state
>
136 : public __codecvt_abstract_base
<__gnu_test::pod_uchar
, char,
137 __gnu_test::pod_state
>
140 typedef codecvt_base::result result
;
141 typedef __gnu_test::pod_uchar intern_type
;
142 typedef char extern_type
;
143 typedef __gnu_test::pod_state state_type
;
144 typedef __codecvt_abstract_base
<intern_type
, extern_type
, state_type
>
147 explicit codecvt(size_t refs
= 0) : base_type(refs
)
150 static locale::id id
;
157 do_out(state_type
& state
, const intern_type
* from
,
158 const intern_type
* from_end
, const intern_type
*& from_next
,
159 extern_type
* to
, extern_type
* to_limit
,
160 extern_type
*& to_next
) const
162 while (from
< from_end
&& to
< to_limit
)
164 unsigned char tmp
= (state
.value
^ from
->value
);
165 if (state
.value
& 0x8)
167 if (to
>= to_limit
- 2)
170 *to
++ = ((tmp
>> 3) & 0x7);
171 *to
++ = ((tmp
>> 6) & 0x3);
175 if (to
>= to_limit
- 1)
178 *to
++ = ((tmp
>> 4) & 0xf);
186 return (from
< from_end
) ? partial
: ok
;
190 do_in(state_type
& state
, const extern_type
* from
,
191 const extern_type
* from_end
, const extern_type
*& from_next
,
192 intern_type
* to
, intern_type
* to_limit
,
193 intern_type
*& to_next
) const
195 while (from
< from_end
&& to
< to_limit
)
197 unsigned char c
= *from
;
207 if (state
.value
& 0x8)
209 if (from
>= from_end
- 2)
211 tmp
= (*from
++ & 0x7);
212 tmp
|= ((*from
++ << 3) & 0x38);
213 tmp
|= ((*from
++ << 6) & 0xc0);
217 if (from
>= from_end
- 1)
219 tmp
= (*from
++ & 0xf);
220 tmp
|= ((*from
++ << 4) & 0xf0);
222 to
->value
= (tmp
^ state
.value
);
229 return (from
< from_end
) ? partial
: ok
;
233 do_unshift(state_type
& state
, extern_type
* to
, extern_type
* to_limit
,
234 extern_type
*& to_next
) const
236 for (unsigned int i
= 0; i
< CHAR_BIT
; ++i
)
238 unsigned int mask
= (1 << i
);
239 if (state
.value
& mask
)
247 state
.value
&= ~mask
;
248 *to
++ = static_cast<unsigned char>(~mask
);
253 return state
.value
== 0 ? ok
: error
;
257 do_encoding() const throw()
261 do_always_noconv() const throw()
265 do_length(state_type
& state
, const extern_type
* from
,
266 const extern_type
* end
, size_t max
) const
268 const extern_type
* beg
= from
;
269 while (from
< end
&& max
)
271 unsigned char c
= *from
;
281 if (state
.value
& 0x8)
285 tmp
= (*from
++ & 0x7);
286 tmp
|= ((*from
++ << 3) & 0x38);
287 tmp
|= ((*from
++ << 6) & 0xc0);
293 tmp
= (*from
++ & 0xf);
294 tmp
|= ((*from
++ << 4) & 0xf0);
302 // Maximum 8 bytes unshift sequence followed by max 3 bytes for
305 do_max_length() const throw()
310 class ctype
<__gnu_test::pod_uchar
>
311 : public __ctype_abstract_base
<__gnu_test::pod_uchar
>
314 typedef __gnu_test::pod_uchar char_type
;
316 explicit ctype(size_t refs
= 0)
317 : __ctype_abstract_base
<__gnu_test::pod_uchar
>(refs
) { }
319 static locale::id id
;
326 do_is(mask
, char_type
) const
329 virtual const char_type
*
330 do_is(const char_type
* low
, const char_type
* high
, mask
* vec
) const
332 fill_n(vec
, high
- low
, mask());
336 virtual const char_type
*
337 do_scan_is(mask
, const char_type
*, const char_type
* high
) const
340 virtual const char_type
*
341 do_scan_not(mask
, const char_type
* low
, const char_type
*) const
345 do_toupper(char_type c
) const
348 virtual const char_type
*
349 do_toupper(char_type
*, const char_type
* high
) const
353 do_tolower(char_type c
) const
356 virtual const char_type
*
357 do_tolower(char_type
*, const char_type
* high
) const
361 do_widen(char c
) const
362 { return __gnu_test::pod_uchar::from
<char>(c
); }
365 do_widen(const char* low
, const char* high
, char_type
* dest
) const
367 transform(low
, high
, dest
, &__gnu_test::pod_uchar::from
<char>);
372 do_narrow(char_type
, char dfault
) const
375 virtual const char_type
*
376 do_narrow(const char_type
* low
, const char_type
* high
,
377 char dfault
, char* dest
) const
379 fill_n(dest
, high
- low
, dfault
);
384 // numpunct specializations
386 class numpunct
<__gnu_test::pod_uint
>
387 : public locale::facet
390 typedef __gnu_test::pod_uint char_type
;
391 typedef basic_string
<char_type
> string_type
;
393 static locale::id id
;
396 numpunct(size_t refs
= 0)
397 : locale::facet(refs
)
401 decimal_point() const
402 { return this->do_decimal_point(); }
405 thousands_sep() const
406 { return this->do_thousands_sep(); }
410 { return this->do_grouping(); }
414 { return this->do_truename(); }
418 { return this->do_falsename(); }
425 do_decimal_point() const
426 { return char_type(); }
429 do_thousands_sep() const
430 { return char_type(); }
438 { return string_type(); }
442 { return string_type(); }
446 class moneypunct
<__gnu_test::pod_uint
>
447 : public locale::facet
, public money_base
450 typedef __gnu_test::pod_uint char_type
;
451 typedef basic_string
<char_type
> string_type
;
453 static locale::id id
;
454 static const bool intl
= false;
457 moneypunct(size_t refs
= 0)
458 : locale::facet(refs
)
462 decimal_point() const
463 { return this->do_decimal_point(); }
466 thousands_sep() const
467 { return this->do_thousands_sep(); }
471 { return this->do_grouping(); }
475 { return this->do_curr_symbol(); }
478 positive_sign() const
479 { return this->do_positive_sign(); }
482 negative_sign() const
483 { return this->do_negative_sign(); }
487 { return this->do_frac_digits(); }
491 { return this->do_pos_format(); }
495 { return this->do_neg_format(); }
502 do_decimal_point() const
503 { return char_type(); }
506 do_thousands_sep() const
507 { return char_type(); }
514 do_curr_symbol() const
515 { return string_type(); }
518 do_positive_sign() const
519 { return string_type(); }
522 do_negative_sign() const
523 { return string_type(); }
526 do_frac_digits() const
530 do_pos_format() const
531 { return pattern(); }
534 do_neg_format() const
535 { return pattern(); }
539 #endif // _GLIBCXX_TESTSUITE_CHARACTER_H