OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libstdc++-v3/] [testsuite/] [testsuite_character.h] - Blame information for rev 19

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 jlechner
// -*- C++ -*-
2
 
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.
13
//
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.
18
//
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.
23
//
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.
32
 
33
#ifndef _GLIBCXX_TESTSUITE_CHARACTER_H
34
#define _GLIBCXX_TESTSUITE_CHARACTER_H
35
 
36
#include <climits>
37
#include <string> // for char_traits
38
#include <locale> // for codecvt
39
#include <ext/pod_char_traits.h>
40
 
41
namespace __gnu_test
42
{
43
  struct pod_int
44
  {
45
    int value;
46
  };
47
 
48
  inline bool
49
  operator==(const pod_int& lhs, const pod_int& rhs)
50
  { return lhs.value == rhs.value; }
51
 
52
  inline bool
53
  operator<(const pod_int& lhs, const pod_int& rhs)
54
  { return lhs.value < rhs.value; }
55
 
56
  struct pod_state
57
  {
58
    unsigned long value;
59
  };
60
 
61
  inline bool
62
  operator==(const pod_state& lhs, const pod_state& rhs)
63
  { return lhs.value == rhs.value; }
64
 
65
  inline bool
66
  operator<(const pod_state& lhs, const pod_state& rhs)
67
  { return lhs.value < rhs.value; }
68
 
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;
75
}
76
 
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)
84
      {
85
        char_type ret = { static_cast<value_type>(v.value) };
86
        return ret;
87
      }
88
 
89
  template<>
90
    template<typename V2>
91
      inline V2
92
      __gnu_test::pod_char::char_type::to(const char_type& c)
93
      {
94
        V2 ret = { c.value };
95
        return ret;
96
      }
97
 
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)
102
      {
103
        char_type ret;
104
        ret.value = (v >> 5);
105
        return ret;
106
      }
107
 
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
114
 
115
namespace std
116
{
117
  // codecvt specialization
118
  //
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.
126
  //
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>
137
    {
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;
145
 
146
      explicit codecvt(size_t refs = 0) : base_type(refs)
147
      { }
148
 
149
      static locale::id id;
150
 
151
    protected:
152
      ~codecvt()
153
      { }
154
 
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
160
      {
161
        while (from < from_end && to < to_limit)
162
          {
163
            unsigned char tmp = (state.value ^ from->value);
164
            if (state.value & 0x8)
165
              {
166
                if (to >= to_limit - 2)
167
                  break;
168
                *to++ = (tmp & 0x7);
169
                *to++ = ((tmp >> 3) & 0x7);
170
                *to++ = ((tmp >> 6) & 0x3);
171
              }
172
            else
173
              {
174
                if (to >= to_limit - 1)
175
                  break;
176
                *to++ = (tmp & 0xf);
177
                *to++ = ((tmp >> 4) & 0xf);
178
              }
179
            state.value = tmp;
180
            ++from;
181
          }
182
 
183
        from_next = from;
184
        to_next = to;
185
        return (from < from_end) ? partial : ok;
186
      }
187
 
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
193
      {
194
        while (from < from_end && to < to_limit)
195
          {
196
            unsigned char c = *from;
197
            if (c & 0xc0)
198
              {
199
                // Unshift sequence
200
                state.value &= c;
201
                ++from;
202
                continue;
203
              }
204
 
205
            unsigned char tmp;
206
            if (state.value & 0x8)
207
              {
208
                if (from >= from_end - 2)
209
                  break;
210
                tmp = (*from++ & 0x7);
211
                tmp |= ((*from++ << 3) & 0x38);
212
                tmp |= ((*from++ << 6) & 0xc0);
213
              }
214
            else
215
              {
216
                if (from >= from_end - 1)
217
                  break;
218
                tmp = (*from++ & 0xf);
219
                tmp |= ((*from++ << 4) & 0xf0);
220
              }
221
            to->value = (tmp ^ state.value);
222
            state.value = tmp;
223
            ++to;
224
          }
225
 
226
        from_next = from;
227
        to_next = to;
228
        return (from < from_end) ? partial : ok;
229
      }
230
 
231
      virtual result
232
      do_unshift(state_type& state, extern_type* to, extern_type* to_limit,
233
                 extern_type*& to_next) const
234
      {
235
        for (unsigned int i = 0; i < CHAR_BIT; ++i)
236
          {
237
            unsigned int mask = (1 << i);
238
            if (state.value & mask)
239
              {
240
                if (to == to_limit)
241
                  {
242
                    to_next = to;
243
                    return partial;
244
                  }
245
 
246
                state.value &= ~mask;
247
                *to++ = static_cast<unsigned char>(~mask);
248
              }
249
          }
250
 
251
        to_next = to;
252
        return state.value == 0 ? ok : error;
253
      }
254
 
255
      virtual int
256
      do_encoding() const throw()
257
      { return -1; }
258
 
259
      virtual bool
260
      do_always_noconv() const throw()
261
      { return false; }
262
 
263
      virtual int
264
      do_length(state_type& state, const extern_type* from,
265
                const extern_type* end, size_t max) const
266
      {
267
        const extern_type* beg = from;
268
        while (from < end && max)
269
          {
270
            unsigned char c = *from;
271
            if (c & 0xc0)
272
              {
273
                // Unshift sequence
274
                state.value &= c;
275
                ++from;
276
                continue;
277
              }
278
 
279
            unsigned char tmp;
280
            if (state.value & 0x8)
281
              {
282
                if (from >= end - 2)
283
                  break;
284
                tmp = (*from++ & 0x7);
285
                tmp |= ((*from++ << 3) & 0x38);
286
                tmp |= ((*from++ << 6) & 0xc0);
287
              }
288
            else
289
              {
290
                if (from >= end - 1)
291
                  break;
292
                tmp = (*from++ & 0xf);
293
                tmp |= ((*from++ << 4) & 0xf0);
294
              }
295
            state.value = tmp;
296
            --max;
297
          }
298
        return from - beg;
299
      }
300
 
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; }
306
    };
307
 
308
  template<>
309
    class ctype<__gnu_test::pod_uchar>
310
    : public __ctype_abstract_base<__gnu_test::pod_uchar>
311
    {
312
    public:
313
      typedef __gnu_test::pod_uchar char_type;
314
 
315
      explicit ctype(size_t refs  = 0)
316
      : __ctype_abstract_base<__gnu_test::pod_uchar>(refs) { }
317
 
318
      static locale::id id;
319
 
320
    protected:
321
      ~ctype()
322
      { }
323
 
324
      virtual bool
325
      do_is(mask, char_type) const
326
      { return false; }
327
 
328
      virtual const char_type*
329
      do_is(const char_type* low, const char_type* high, mask* vec) const
330
      {
331
        fill_n(vec, high - low, mask());
332
        return high;
333
      }
334
 
335
      virtual const char_type*
336
      do_scan_is(mask, const char_type*, const char_type* high) const
337
      { return high; }
338
 
339
      virtual const char_type*
340
      do_scan_not(mask, const char_type* low, const char_type*) const
341
      { return low; }
342
 
343
      virtual char_type
344
      do_toupper(char_type c) const
345
      { return c; }
346
 
347
      virtual const char_type*
348
      do_toupper(char_type*, const char_type*  high) const
349
      { return high; }
350
 
351
      virtual char_type
352
      do_tolower(char_type c) const
353
      { return c; }
354
 
355
      virtual const char_type*
356
      do_tolower(char_type*, const char_type*  high) const
357
      { return high; }
358
 
359
      virtual char_type
360
      do_widen(char c) const
361
      { return __gnu_test::pod_uchar::from<char>(c); }
362
 
363
      virtual const char*
364
      do_widen(const char* low, const char* high, char_type* dest) const
365
      {
366
        transform(low, high, dest, &__gnu_test::pod_uchar::from<char>);
367
        return high;
368
      }
369
 
370
      virtual char
371
      do_narrow(char_type, char dfault) const
372
      { return dfault; }
373
 
374
      virtual const char_type*
375
      do_narrow(const char_type* low, const char_type* high,
376
                char dfault, char*  dest) const
377
      {
378
        fill_n(dest, high - low, dfault);
379
        return high;
380
      }
381
    };
382
 
383
  // numpunct specializations
384
  template<>
385
    class numpunct<__gnu_test::pod_uint>
386
    : public locale::facet
387
    {
388
    public:
389
      typedef __gnu_test::pod_uint    char_type;
390
      typedef basic_string<char_type> string_type;
391
 
392
      static locale::id id;
393
 
394
      explicit
395
      numpunct(size_t refs = 0)
396
      : locale::facet(refs)
397
      { }
398
 
399
      char_type
400
      decimal_point() const
401
      { return this->do_decimal_point(); }
402
 
403
      char_type
404
      thousands_sep() const
405
      { return this->do_thousands_sep(); }
406
 
407
      string
408
      grouping() const
409
      { return this->do_grouping(); }
410
 
411
      string_type
412
      truename() const
413
      { return this->do_truename(); }
414
 
415
      string_type
416
      falsename() const
417
      { return this->do_falsename(); }
418
 
419
    protected:
420
      ~numpunct()
421
      { }
422
 
423
      virtual char_type
424
      do_decimal_point() const
425
      { return char_type(); }
426
 
427
      virtual char_type
428
      do_thousands_sep() const
429
      { return char_type(); }
430
 
431
      virtual string
432
      do_grouping() const
433
      { return string(); }
434
 
435
      virtual string_type
436
      do_truename() const
437
      { return string_type(); }
438
 
439
      virtual string_type
440
      do_falsename() const
441
      { return string_type(); }
442
    };
443
 
444
  template<>
445
    class moneypunct<__gnu_test::pod_uint>
446
    : public locale::facet, public money_base
447
    {
448
    public:
449
      typedef __gnu_test::pod_uint    char_type;
450
      typedef basic_string<char_type> string_type;
451
 
452
      static locale::id id;
453
      static const bool intl = false;
454
 
455
      explicit
456
      moneypunct(size_t refs = 0)
457
      : locale::facet(refs)
458
      { }
459
 
460
      char_type
461
      decimal_point() const
462
      { return this->do_decimal_point(); }
463
 
464
      char_type
465
      thousands_sep() const
466
      { return this->do_thousands_sep(); }
467
 
468
      string
469
      grouping() const
470
      { return this->do_grouping(); }
471
 
472
      string_type
473
      curr_symbol() const
474
      { return this->do_curr_symbol(); }
475
 
476
      string_type
477
      positive_sign() const
478
      { return this->do_positive_sign(); }
479
 
480
      string_type
481
      negative_sign() const
482
      { return this->do_negative_sign(); }
483
 
484
      int
485
      frac_digits() const
486
      { return this->do_frac_digits(); }
487
 
488
      pattern
489
      pos_format() const
490
      { return this->do_pos_format(); }
491
 
492
      pattern
493
      neg_format() const
494
      { return this->do_neg_format(); }
495
 
496
    protected:
497
      ~moneypunct()
498
      { }
499
 
500
      virtual char_type
501
      do_decimal_point() const
502
      { return char_type(); }
503
 
504
      virtual char_type
505
      do_thousands_sep() const
506
      { return char_type(); }
507
 
508
      virtual string
509
      do_grouping() const
510
      { return string(); }
511
 
512
      virtual string_type
513
      do_curr_symbol() const
514
      { return string_type(); }
515
 
516
      string_type
517
      do_positive_sign() const
518
      { return string_type(); }
519
 
520
      string_type
521
      do_negative_sign() const
522
      { return string_type(); }
523
 
524
      int
525
      do_frac_digits() const
526
      { return 0; }
527
 
528
      pattern
529
      do_pos_format() const
530
      { return pattern(); }
531
 
532
      pattern
533
      do_neg_format() const
534
      { return pattern(); }
535
     };
536
} // namespace std
537
 
538
#endif // _GLIBCXX_TESTSUITE_CHARACTER_H
539
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.