summaryrefslogtreecommitdiffstats
path: root/libstdc++-v3/testsuite/testsuite_character.h
blob: 9442fe13341e6a9b4bfd10ab065d7a4df3e91466 (plain)
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
// -*- C++ -*-
// Testing character type and state type with char_traits and codecvt
// specializations for the C++ library testsuite.
//
// Copyright (C) 2003 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// 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.

#ifndef _GLIBCXX_TESTSUITE_CHARACTER_H
#define _GLIBCXX_TESTSUITE_CHARACTER_H

#include <string> // for char_traits
#include <locale> // for codecvt
#include <climits>

namespace __gnu_test
{  
  // Character type
  struct character
  {
    unsigned char val;

    static character from_char(char c)
    {
      character ret;
      ret.val = c;
      return ret;
    }
  };

  inline bool
  operator==(const character& lhs, const character& rhs)
  { return lhs.val == rhs.val; }

  // State type.
  struct conversion_state
  {
    unsigned int state;
  };
}; // namespace __gnu_test

namespace std
{
  // char_traits specialization. Meets the additional requirements for
  // basic_filebuf.
  template<>
    struct char_traits<__gnu_test::character>
    {
      typedef __gnu_test::character char_type;
      typedef unsigned int int_type;
      typedef __gnu_test::conversion_state state_type;
      typedef streamoff off_type;
      typedef fpos<state_type> pos_type;

      static void
      assign(char_type& c1, const char_type& c2)
      { c1 = c2; }

      static bool
      eq(const char_type& c1, const char_type& c2)
      { return c1.val == c2.val; }

      static bool
      lt(const char_type& c1, const char_type& c2)
      { return c1.val < c2.val; }

      static int
      compare(const char_type* s1, const char_type* s2, size_t n)
      {
	for (size_t i = 0; i < n; ++i)
	  {
	    if (lt(s1[i], s2[i]))
	      return -1;
	    else if (lt(s2[i], s1[i]))
	      return 1;
	  }
	return 0;
      }

      static size_t
      length(const char_type* s)
      {
	size_t n = 0;
	while (!eq(s[n], char_type()))
	  ++n;
	return n;
      }

      static const char_type*
      find(const char_type* s, size_t n, const char_type& a)
      {
	for (size_t i = 0; i < n; ++i)
	  {
	    if (eq(s[i], a))
	      return s + i;
	  }
	return NULL;
      }

      static char_type*
      move(char_type* s1, const char_type* s2, size_t n)
      {
	if (s1 > s2)
	  {
	    for (size_t i = 0; i < n; ++i)
	      assign(s1[n - i - 1], s2[n - i - 1]);
	  }
	else
	  {
	    for (size_t i = 0; i < n; ++i)
	      assign(s1[i], s2[i]);
	  }
	return s1;
      }

      static char_type*
      copy(char_type* s1, const char_type* s2, size_t n)
      {
	for (size_t i = 0; i < n; ++i)
	  assign(s1[i], s2[i]);
	return s1;
      }

      static char_type*
      assign(char_type* s, size_t n, char_type a)
      {
	for (size_t i = 0; i < n; ++i)
	  assign(s[i], a);
	return s;
      }

      static int_type
      not_eof(const int_type& c)
      {
	if (eq_int_type(c, eof()))
	  return 0;
	return c;
      }

      // Note non-trivial conversion to maximize chance of catching bugs
      static char_type
      to_char_type(const int_type& c)
      {
	char_type ret;
	ret.val = (c >> 5);
	return ret;
      }

      static int_type
      to_int_type(const char_type& c)
      {
	return c.val << 5;
      }

      static bool
      eq_int_type(const int_type& c1, const int_type& c2)
      { return c1 == c2; }

      static int_type eof()
      { return 0xf; }
    };

  // codecvt specialization
  //
  // The conversion performed by the specialization is not supposed to
  // be useful, rather it has been designed to demonstrate the
  // essential features of stateful conversions:
  // * Number and value of bytes for each internal character depends on the
  //   state in addition to the character itself.
  // * Unshift produces an unshift sequence and resets the state. On input
  //   the unshift sequence causes the state to be reset.
  //
  // The conversion for output is as follows:
  // 1. Calculate the value tmp by xor-ing the state and the internal
  //    character
  // 2. Split tmp into either two or three bytes depending on the value of
  //    state. Output those bytes.
  // 3. tmp becomes the new value of state.
  template<>
    class codecvt<__gnu_test::character, char, __gnu_test::conversion_state>
      : public locale::facet, public codecvt_base
    {
    public:
      typedef __gnu_test::character intern_type;
      typedef char extern_type;
      typedef __gnu_test::conversion_state state_type;

      explicit codecvt(size_t refs = 0)
      : locale::facet(refs)
      { }

      result
      out(state_type& state, const intern_type* from,
	  const intern_type* from_end, const intern_type*& from_next,
	  extern_type* to, extern_type* to_limit, extern_type*& to_next) const
      {
	return do_out(state, from, from_end, from_next,
		      to, to_limit, to_next);
      }

      result
      unshift(state_type& state, extern_type* to, extern_type* to_limit,
	      extern_type*& to_next) const
      { return do_unshift(state, to, to_limit, to_next); }

      result
      in(state_type& state, const extern_type* from,
	 const extern_type* from_end, const extern_type*& from_next,
	 intern_type* to, intern_type* to_limit, intern_type*& to_next) const
      {
	return do_in(state, from, from_end, from_next,
		     to, to_limit, to_next);
      }

      int
      encoding() const throw()
      { return do_encoding(); }

      bool
      always_noconv() const throw()
      { return do_always_noconv(); }
      
      int
      length(state_type& state, const extern_type* from,
	     const extern_type* end, size_t max) const
      { return do_length(state, from, end, max); }
      
      int
      max_length() const throw()
      { return do_max_length(); }

      static locale::id id;

    protected:
      ~codecvt()
      { }

      virtual result
      do_out(state_type& state, const intern_type* from,
	     const intern_type* from_end, const intern_type*& from_next,
	     extern_type* to, extern_type* to_limit,
	     extern_type*& to_next) const
      {
	while (from < from_end && to < to_limit)
	  {
	    unsigned char tmp = (state.state ^ from->val);
	    if (state.state & 0x8)
	      {
		if (to >= to_limit - 2)
		  break;
		*to++ = (tmp & 0x7);
		*to++ = ((tmp >> 3) & 0x7);
		*to++ = ((tmp >> 6) & 0x3);
	      }
	    else
	      {
		if (to >= to_limit - 1)
		  break;
		*to++ = (tmp & 0xf);
		*to++ = ((tmp >> 4) & 0xf);
	      }
	    state.state = tmp;
	    ++from;
	  }

	from_next = from;
	to_next = to;
	return (from < from_end) ? partial : ok;
      }

      virtual result
      do_in(state_type& state, const extern_type* from,
	    const extern_type* from_end, const extern_type*& from_next,
	    intern_type* to, intern_type* to_limit,
	    intern_type*& to_next) const
      {
	while (from < from_end && to < to_limit)
	  {
	    unsigned char c = *from;
	    if (c & 0xc0)
	      {
		// Unshift sequence
		state.state &= c;
		++from;
		continue;
	      }

	    unsigned char tmp;
	    if (state.state & 0x8)
	      {
		if (from >= from_end - 2)
		  break;
		tmp = (*from++ & 0x7);
		tmp |= ((*from++ << 3) & 0x38);
		tmp |= ((*from++ << 6) & 0xc0);
	      }
	    else
	      {
		if (from >= from_end - 1)
		  break;
		tmp = (*from++ & 0xf);
		tmp |= ((*from++ << 4) & 0xf0);
	      }
	    to->val = (tmp ^ state.state);
	    state.state = tmp;
	    ++to;
	  }

	from_next = from;
	to_next = to;
	return (from < from_end) ? partial : ok;
      }

      virtual result
      do_unshift(state_type& state, extern_type* to, extern_type* to_limit,
		 extern_type*& to_next) const
      {
	for (unsigned int i = 0; i < CHAR_BIT; ++i)
	  {
	    unsigned int mask = (1 << i);
	    if (state.state & mask)
	      {
		if (to == to_limit)
		  {
		    to_next = to;
		    return partial;
		  }

		state.state &= ~mask;
		*to++ = static_cast<unsigned char>(~mask);
	      }
	  }

	to_next = to;
	return state.state == 0 ? ok : error;
      }

      virtual int
      do_encoding() const throw()
      { return -1; }

      virtual bool
      do_always_noconv() const throw()
      { return false; }

      virtual int
      do_length(state_type& state, const extern_type* from,
		const extern_type* end, size_t max) const
      {
	const extern_type* beg = from;
	while (from < end && max)
	  {
	    unsigned char c = *from;
	    if (c & 0xc0)
	      {
		// Unshift sequence
		state.state &= c;
		++from;
		continue;
	      }

	    unsigned char tmp;
	    if (state.state & 0x8)
	      {
		if (from >= end - 2)
		  break;
		tmp = (*from++ & 0x7);
		tmp |= ((*from++ << 3) & 0x38);
		tmp |= ((*from++ << 6) & 0xc0);
	      }
	    else
	      {
		if (from >= end - 1)
		  break;
		tmp = (*from++ & 0xf);
		tmp |= ((*from++ << 4) & 0xf0);
	      }
	    state.state = tmp;
	    --max;
	  }
	return from - beg;
      }

      // Maximum 8 bytes unshift sequence followed by max 3 bytes for
      // one character.
      virtual int
      do_max_length() const throw()
      { return 11; }
    };

  locale::id
  codecvt<__gnu_test::character, char, __gnu_test::conversion_state>::id;
} // namespace std

#endif // _GLIBCXX_TESTSUITE_CHARACTER_H

OpenPOWER on IntegriCloud