Vaucanson  1.4.1
char_traits.hxx
1 // char_traits.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2004, 2005, 2006 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 #ifndef VCSN_MISC_CHAR_TRAITS_HXX
18 # define VCSN_MISC_CHAR_TRAITS_HXX
19 
22 
23 namespace vcsn
24 {
25  namespace misc
26  {
27 
28  /*-----------------.
29  | generic_int_type |
30  `-----------------*/
31 
32  template <class CharT>
33  generic_int_type<CharT>::generic_int_type () :
34  // val_ (char_type ()),
35  eof_ (true)
36  {
37  }
38 
39  template <class CharT>
40  generic_int_type<CharT>::generic_int_type (const char_type& val) :
41  val_ (val), eof_ (false)
42  {
43  }
44 
45  template <class CharT>
46  generic_int_type<CharT>::operator CharT () const
47  {
48  return val_;
49  }
50 
51  template <class CharT>
52  bool
53  generic_int_type<CharT>::
54  operator== (const generic_int_type<CharT>& rhs) const
55  {
56  if (rhs.eof_)
57  return eof_;
58  else
59  return not eof_ and (rhs.val_ == val_);
60  }
61 
62  template <class CharT>
63  bool
64  generic_int_type<CharT>::eof () const
65  {
66  return eof_;
67  }
68 
69  /*------------.
70  | char_traits |
71  `------------*/
72 
73  template <typename CharT>
74  void
75  char_traits<CharT>::assign (char_type& lhs, const char_type& rhs)
76  {
77  lhs = rhs;
78  }
79 
80  template <typename CharT>
81  bool
82  char_traits<CharT>::eq (const char_type& lhs, const char_type& rhs)
83  {
84  return lhs == rhs;
85  }
86 
87  template <typename CharT>
88  bool
89  char_traits<CharT>::lt (const char_type& lhs, const char_type& rhs)
90  {
91  return lhs < rhs;
92  }
93 
94  template <typename CharT>
95  int
96  char_traits<CharT>::compare (const char_type* p,
97  const char_type* q,
98  size_t n)
99  {
100  size_t i;
101 
102  for (i = 0; (i < n) and eq (p[i], q[i]); ++i)
103  ;
104  if (i == n)
105  return 0;
106  else
107  return lt (p[i], q[i]) ? -1 : 1;
108  }
109 
110  template <typename CharT>
111  size_t
112  char_traits<CharT>::length (const char_type* p)
113  {
114  size_t i = 0;
115  while (not eq (p[i], char_type ()))
116  ++i;
117  return i;
118  }
119 
120  template <typename CharT>
121  const typename char_traits<CharT>::char_type*
122  char_traits<CharT>::find (const char_type* p,
123  size_t n,
124  const char_type& c)
125  {
126  size_t i;
127 
128  for (i = 0; (i < n) and not eq (p[i], c); ++i)
129  ;
130  return i < n ? p + i: 0;
131  }
132 
133  template <typename CharT>
134  typename char_traits<CharT>::char_type*
135  char_traits<CharT>::move (char_type* s, const char_type* p, size_t n)
136  {
137  // FIXME: This code has been (almost) dummy pasted from the
138  // standard library. Maybe it should be usefull to have a special
139  // optimized version when pointers do not overlap.
140 
141  char_type* tmp = new char_type[n];
142  copy (tmp, p, n);
143  copy (s, tmp, n);
144  delete[] tmp;
145  return s;
146  }
147 
148  template <typename CharT>
149  typename char_traits<CharT>::char_type*
150  char_traits<CharT>::copy (char_type* s, const char_type* p, size_t n)
151  {
152  precondition ((p < s) or (p > s + n));
153 
154  for (size_t i = 0; i < n; ++i)
155  assign (s[i], p[i]);
156  return s;
157  }
158 
159  template <typename CharT>
160  typename char_traits<CharT>::char_type*
161  char_traits<CharT>::assign (char_type* s, size_t n, char_type c)
162  {
163  for (size_t i = 0; i < n; ++i)
164  assign (s[i], c);
165  return s;
166  }
167 
168  template <typename CharT>
169  typename char_traits<CharT>::int_type
170  char_traits<CharT>::not_eof (const int_type& e)
171  {
172  return eq_int_type (e, eof ()) ? int_type (char_type ()) : e;
173  }
174 
175  template <typename CharT>
176  typename char_traits<CharT>::char_type
177  char_traits<CharT>::to_char_type (const int_type& e)
178  {
179  return e; // Calls generic_int_type<char_type>::operator CharT ().
180  }
181 
182  template <typename CharT>
183  typename char_traits<CharT>::int_type
184  char_traits<CharT>::to_int_type (const char_type& e)
185  {
186  return int_type (e);
187  }
188 
189  template <typename CharT>
190  bool
191  char_traits<CharT>::eq_int_type (const int_type& e, const int_type& f)
192  {
193  return e == f;
194  }
195 
196  template <typename CharT>
197  typename char_traits<CharT>::int_type
198  char_traits<CharT>::eof ()
199  {
200  return int_type ();
201  }
202 
203  } // end of namespace misc
204 } // end of namespace vcsn
205 
206 #endif // ! VCSN_MISC_CHAR_TRAITS_HXX