Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libstdc++-v3 / testsuite / testsuite_character.h
blob3d6a3f2d1c23016446fa154e4833b15bd2b1ed87
1 // -*- C++ -*-
3 // Testing character type and state type with char_traits and codecvt
4 // specializations for the C++ library testsuite.
5 //
6 // Copyright (C) 2003, 2005 Free Software Foundation, Inc.
7 //
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)
12 // any later version.
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,
22 // USA.
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
36 #include <climits>
37 #include <string> // for char_traits
38 #include <locale> // for codecvt
39 #include <ext/pod_char_traits.h>
41 namespace __gnu_test
43 struct pod_int
45 int value;
48 inline bool
49 operator==(const pod_int& lhs, const pod_int& rhs)
50 { return lhs.value == rhs.value; }
52 inline bool
53 operator<(const pod_int& lhs, const pod_int& rhs)
54 { return lhs.value < rhs.value; }
56 struct pod_state
58 unsigned long value;
61 inline bool
62 operator==(const pod_state& lhs, const pod_state& rhs)
63 { return lhs.value == rhs.value; }
65 inline bool
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;
77 namespace __gnu_cxx {
78 // Specializations.
79 // pod_char
80 template<>
81 template<typename V2>
82 inline __gnu_test::pod_char::char_type
83 __gnu_test::pod_char::char_type::from(const V2& v)
85 char_type ret = { static_cast<value_type>(v.value) };
86 return ret;
89 template<>
90 template<typename V2>
91 inline V2
92 __gnu_test::pod_char::char_type::to(const char_type& c)
94 V2 ret = { c.value };
95 return ret;
98 template<>
99 template<typename V2>
100 inline __gnu_test::pod_uchar::char_type
101 __gnu_test::pod_uchar::char_type::from(const V2& v)
103 char_type ret;
104 ret.value = (v >> 5);
105 return ret;
108 template<>
109 template<typename V2>
110 inline V2
111 __gnu_test::pod_uchar::char_type::to(const char_type& c)
112 { return static_cast<V2>(c.value << 5); }
113 }; // namespace __gnu_test
115 namespace std
117 // codecvt specialization
119 // The conversion performed by the specialization is not supposed to
120 // be useful, rather it has been designed to demonstrate the
121 // essential features of stateful conversions:
122 // * Number and value of bytes for each internal character depends on the
123 // state in addition to the character itself.
124 // * Unshift produces an unshift sequence and resets the state. On input
125 // the unshift sequence causes the state to be reset.
127 // The conversion for output is as follows:
128 // 1. Calculate the value tmp by xor-ing the state and the internal
129 // character
130 // 2. Split tmp into either two or three bytes depending on the value of
131 // state. Output those bytes.
132 // 3. tmp becomes the new value of state.
133 template<>
134 class codecvt<__gnu_test::pod_uchar, char, __gnu_test::pod_state>
135 : public __codecvt_abstract_base<__gnu_test::pod_uchar, char,
136 __gnu_test::pod_state>
138 public:
139 typedef codecvt_base::result result;
140 typedef __gnu_test::pod_uchar intern_type;
141 typedef char extern_type;
142 typedef __gnu_test::pod_state state_type;
143 typedef __codecvt_abstract_base<intern_type, extern_type, state_type>
144 base_type;
146 explicit codecvt(size_t refs = 0) : base_type(refs)
149 static locale::id id;
151 protected:
152 ~codecvt()
155 virtual result
156 do_out(state_type& state, const intern_type* from,
157 const intern_type* from_end, const intern_type*& from_next,
158 extern_type* to, extern_type* to_limit,
159 extern_type*& to_next) const
161 while (from < from_end && to < to_limit)
163 unsigned char tmp = (state.value ^ from->value);
164 if (state.value & 0x8)
166 if (to >= to_limit - 2)
167 break;
168 *to++ = (tmp & 0x7);
169 *to++ = ((tmp >> 3) & 0x7);
170 *to++ = ((tmp >> 6) & 0x3);
172 else
174 if (to >= to_limit - 1)
175 break;
176 *to++ = (tmp & 0xf);
177 *to++ = ((tmp >> 4) & 0xf);
179 state.value = tmp;
180 ++from;
183 from_next = from;
184 to_next = to;
185 return (from < from_end) ? partial : ok;
188 virtual result
189 do_in(state_type& state, const extern_type* from,
190 const extern_type* from_end, const extern_type*& from_next,
191 intern_type* to, intern_type* to_limit,
192 intern_type*& to_next) const
194 while (from < from_end && to < to_limit)
196 unsigned char c = *from;
197 if (c & 0xc0)
199 // Unshift sequence
200 state.value &= c;
201 ++from;
202 continue;
205 unsigned char tmp;
206 if (state.value & 0x8)
208 if (from >= from_end - 2)
209 break;
210 tmp = (*from++ & 0x7);
211 tmp |= ((*from++ << 3) & 0x38);
212 tmp |= ((*from++ << 6) & 0xc0);
214 else
216 if (from >= from_end - 1)
217 break;
218 tmp = (*from++ & 0xf);
219 tmp |= ((*from++ << 4) & 0xf0);
221 to->value = (tmp ^ state.value);
222 state.value = tmp;
223 ++to;
226 from_next = from;
227 to_next = to;
228 return (from < from_end) ? partial : ok;
231 virtual result
232 do_unshift(state_type& state, extern_type* to, extern_type* to_limit,
233 extern_type*& to_next) const
235 for (unsigned int i = 0; i < CHAR_BIT; ++i)
237 unsigned int mask = (1 << i);
238 if (state.value & mask)
240 if (to == to_limit)
242 to_next = to;
243 return partial;
246 state.value &= ~mask;
247 *to++ = static_cast<unsigned char>(~mask);
251 to_next = to;
252 return state.value == 0 ? ok : error;
255 virtual int
256 do_encoding() const throw()
257 { return -1; }
259 virtual bool
260 do_always_noconv() const throw()
261 { return false; }
263 virtual int
264 do_length(state_type& state, const extern_type* from,
265 const extern_type* end, size_t max) const
267 const extern_type* beg = from;
268 while (from < end && max)
270 unsigned char c = *from;
271 if (c & 0xc0)
273 // Unshift sequence
274 state.value &= c;
275 ++from;
276 continue;
279 unsigned char tmp;
280 if (state.value & 0x8)
282 if (from >= end - 2)
283 break;
284 tmp = (*from++ & 0x7);
285 tmp |= ((*from++ << 3) & 0x38);
286 tmp |= ((*from++ << 6) & 0xc0);
288 else
290 if (from >= end - 1)
291 break;
292 tmp = (*from++ & 0xf);
293 tmp |= ((*from++ << 4) & 0xf0);
295 state.value = tmp;
296 --max;
298 return from - beg;
301 // Maximum 8 bytes unshift sequence followed by max 3 bytes for
302 // one character.
303 virtual int
304 do_max_length() const throw()
305 { return 11; }
308 template<>
309 class ctype<__gnu_test::pod_uchar>
310 : public __ctype_abstract_base<__gnu_test::pod_uchar>
312 public:
313 typedef __gnu_test::pod_uchar char_type;
315 explicit ctype(size_t refs = 0)
316 : __ctype_abstract_base<__gnu_test::pod_uchar>(refs) { }
318 static locale::id id;
320 protected:
321 ~ctype()
324 virtual bool
325 do_is(mask m, char_type c) const
326 { return false; }
328 virtual const char_type*
329 do_is(const char_type* low, const char_type* high, mask* vec) const
331 fill_n(vec, high - low, mask());
332 return high;
335 virtual const char_type*
336 do_scan_is(mask m, const char_type* low, const char_type* high) const
337 { return high; }
339 virtual const char_type*
340 do_scan_not(mask m, const char_type* low, const char_type* high) const
341 { return low; }
343 virtual char_type
344 do_toupper(char_type c) const
345 { return c; }
347 virtual const char_type*
348 do_toupper(char_type* low, const char_type* high) const
349 { return high; }
351 virtual char_type
352 do_tolower(char_type c) const
353 { return c; }
355 virtual const char_type*
356 do_tolower(char_type* low, const char_type* high) const
357 { return high; }
359 virtual char_type
360 do_widen(char c) const
361 { return __gnu_test::pod_uchar::from<char>(c); }
363 virtual const char*
364 do_widen(const char* low, const char* high, char_type* dest) const
366 transform(low, high, dest, &__gnu_test::pod_uchar::from<char>);
367 return high;
370 virtual char
371 do_narrow(char_type, char dfault) const
372 { return dfault; }
374 virtual const char_type*
375 do_narrow(const char_type* low, const char_type* high,
376 char dfault, char* dest) const
378 fill_n(dest, high - low, dfault);
379 return high;
383 // numpunct specializations
384 template<>
385 class numpunct<__gnu_test::pod_uint>
386 : public locale::facet
388 public:
389 typedef __gnu_test::pod_uint char_type;
390 typedef basic_string<char_type> string_type;
392 static locale::id id;
394 explicit
395 numpunct(size_t refs = 0)
396 : locale::facet(refs)
397 { }
399 char_type
400 decimal_point() const
401 { return this->do_decimal_point(); }
403 char_type
404 thousands_sep() const
405 { return this->do_thousands_sep(); }
407 string
408 grouping() const
409 { return this->do_grouping(); }
411 string_type
412 truename() const
413 { return this->do_truename(); }
415 string_type
416 falsename() const
417 { return this->do_falsename(); }
419 protected:
420 ~numpunct()
421 { }
423 virtual char_type
424 do_decimal_point() const
425 { return char_type(); }
427 virtual char_type
428 do_thousands_sep() const
429 { return char_type(); }
431 virtual string
432 do_grouping() const
433 { return string(); }
435 virtual string_type
436 do_truename() const
437 { return string_type(); }
439 virtual string_type
440 do_falsename() const
441 { return string_type(); }
444 template<>
445 class moneypunct<__gnu_test::pod_uint>
446 : public locale::facet, public money_base
448 public:
449 typedef __gnu_test::pod_uint char_type;
450 typedef basic_string<char_type> string_type;
452 static locale::id id;
453 static const bool intl = false;
455 explicit
456 moneypunct(size_t refs = 0)
457 : locale::facet(refs)
460 char_type
461 decimal_point() const
462 { return this->do_decimal_point(); }
464 char_type
465 thousands_sep() const
466 { return this->do_thousands_sep(); }
468 string
469 grouping() const
470 { return this->do_grouping(); }
472 string_type
473 curr_symbol() const
474 { return this->do_curr_symbol(); }
476 string_type
477 positive_sign() const
478 { return this->do_positive_sign(); }
480 string_type
481 negative_sign() const
482 { return this->do_negative_sign(); }
485 frac_digits() const
486 { return this->do_frac_digits(); }
488 pattern
489 pos_format() const
490 { return this->do_pos_format(); }
492 pattern
493 neg_format() const
494 { return this->do_neg_format(); }
496 protected:
497 ~moneypunct()
498 { }
500 virtual char_type
501 do_decimal_point() const
502 { return char_type(); }
504 virtual char_type
505 do_thousands_sep() const
506 { return char_type(); }
508 virtual string
509 do_grouping() const
510 { return string(); }
512 virtual string_type
513 do_curr_symbol() const
514 { return string_type(); }
516 string_type
517 do_positive_sign() const
518 { return string_type(); }
520 string_type
521 do_negative_sign() const
522 { return string_type(); }
525 do_frac_digits() const
526 { return 0; }
528 pattern
529 do_pos_format() const
530 { return pattern(); }
532 pattern
533 do_neg_format() const
534 { return pattern(); }
536 } // namespace std
538 #endif // _GLIBCXX_TESTSUITE_CHARACTER_H