1// Character traits template for the -*- C++ -*- string classes.
2// Copyright (C) 1994 Free Software Foundation
3
4// This file is part of the GNU ANSI C++ Library.  This library is free
5// software; you can redistribute it and/or modify it under the
6// terms of the GNU General Public License as published by the
7// Free Software Foundation; either version 2, or (at your option)
8// any later version.
9
10// This library is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14
15// You should have received a copy of the GNU General Public License
16// along with this library; see the file COPYING.  If not, write to the Free
17// Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19// As a special exception, if you link this library with files
20// compiled with a GNU compiler to produce an executable, this does not cause
21// the resulting executable to be covered by the GNU General Public License.
22// This exception does not however invalidate any other reasons why
23// the executable file might be covered by the GNU General Public License.
24
25// Written by Jason Merrill based upon the specification by Takanori Adachi
26// in ANSI X3J16/94-0013R2.
27
28#ifndef __STRING_CHAR_TRAITS__
29#define __STRING_CHAR_TRAITS__
30
31#ifdef __GNUG__
32// For string_char_traits <char>
33#pragma interface "std/straits.h"
34#endif
35
36#include <cstddef>
37
38extern "C++" {
39template <class charT>
40struct string_char_traits {
41  typedef charT char_type; // for users to acquire the basic character type
42
43  // constraints
44
45  static void assign (char_type& c1, const char_type& c2)
46    { c1 = c2; }
47  static bool eq (const char_type& c1, const char_type& c2)
48    { return (c1 == c2); }
49  static bool ne (const char_type& c1, const char_type& c2)
50    { return !(c1 == c2); }
51  static bool lt (const char_type& c1, const char_type& c2)
52    { return (c1 < c2); }
53  static char_type eos () { return char_type(); } // the null character
54  static bool is_del(char_type a) { return 0; }
55  // characteristic function for delimiters of charT
56
57  // speed-up functions
58
59  static int compare (const char_type* s1, const char_type* s2, size_t n)
60    {
61      size_t i;
62      for (i = 0; i < n; ++i)
63	if (ne (s1[i], s2[i]))
64	  return lt (s1[i], s2[i]) ? -1 : 1;
65
66      return 0;
67    }
68
69  static size_t length (const char_type* s)
70    {
71      size_t l = 0;
72      while (ne (*s++, eos ()))
73	++l;
74      return l;
75    }
76
77  static char_type* copy (char_type* s1, const char_type* s2, size_t n)
78    {
79      for (; n--; )
80	assign (s1[n], s2[n]);
81      return s1;
82    }
83
84  static char_type* move (char_type* s1, const char_type* s2, size_t n)
85    {
86      char_type a[n];
87      size_t i;
88      for (i = 0; i < n; ++i)
89	assign (a[i], s2[i]);
90      for (i = 0; i < n; ++i)
91	assign (s1[i], a[i]);
92      return s1;
93    }
94
95  static char_type* set (char_type* s1, const char_type& c, size_t n)
96    {
97      for (; n--; )
98	assign (s1[n], c);
99      return s1;
100    }
101};
102
103class istream;
104class ostream;
105#include <cctype>
106#include <cstring>
107
108struct string_char_traits <char> {
109  typedef char char_type;
110
111  static void assign (char_type& c1, const char_type& c2)
112    { c1 = c2; }
113  static bool eq (const char_type & c1, const char_type& c2)
114    { return (c1 == c2); }
115  static bool ne (const char_type& c1, const char_type& c2)
116    { return (c1 != c2); }
117  static bool lt (const char_type& c1, const char_type& c2)
118    { return (c1 < c2); }
119  static char_type eos () { return 0; }
120  static bool is_del(char_type a) { return isspace(a); }
121
122  static int compare (const char_type* s1, const char_type* s2, size_t n)
123    { return memcmp (s1, s2, n); }
124  static size_t length (const char_type* s)
125    { return strlen (s); }
126  static char_type* copy (char_type* s1, const char_type* s2, size_t n)
127    { return (char_type*) memcpy (s1, s2, n); }
128  static char_type* move (char_type* s1, const char_type* s2, size_t n)
129    { return (char_type*) memmove (s1, s2, n); }
130  static char_type* set (char_type* s1, const char_type& c, size_t n)
131    { return (char_type*) memset (s1, c, n); }
132};
133
134#if 0
135#include <cwctype>
136struct string_char_traits <wchar_t> {
137  typedef wchar_t char_type;
138
139  static void assign (char_type& c1, const char_type& c2)
140    { c1 = c2; }
141  static bool eq (const char_type & c1, const char_type& c2)
142    { return (c1 == c2); }
143  static bool ne (const char_type& c1, const char_type& c2)
144    { return (c1 != c2); }
145  static bool lt (const char_type& c1, const char_type& c2)
146    { return (c1 < c2); }
147  static char_type eos () { return 0; }
148  static bool is_del(char_type a) { return iswspace(a); }
149
150  static int compare (const char_type* s1, const char_type* s2, size_t n)
151    { return wmemcmp (s1, s2, n); }
152  static size_t length (const char_type* s)
153    { return wcslen (s); }
154  static char_type* copy (char_type* s1, const char_type* s2, size_t n)
155    { return wmemcpy (s1, s2, n); }
156  static char_type* set (char_type* s1, const char_type& c, size_t n)
157    { return wmemset (s1, c, n); }
158};
159#endif
160} // extern "C++"
161#endif
162