char_traits.hxx

00001 // char_traits.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2004 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 #ifndef VCSN_MISC_CHAR_TRAITS_HXX
00018 # define VCSN_MISC_CHAR_TRAITS_HXX
00019 
00020 # include <vaucanson/misc/char_traits.hh>
00021 # include <vaucanson/misc/contract.hh>
00022 
00023 namespace utility
00024 {
00025 
00026   /*-----------------.
00027   | generic_int_type |
00028   `-----------------*/
00029 
00030   template <class CharT>
00031   generic_int_type<CharT>::generic_int_type() :
00032 //     val_ (char_type()),
00033     eof_ (true)
00034   {
00035   }
00036 
00037   template <class CharT>
00038   generic_int_type<CharT>::generic_int_type(const char_type& val) :
00039     val_ (val),
00040     eof_ (false)
00041   {
00042   }
00043 
00044   template <class CharT>
00045   generic_int_type<CharT>::operator CharT () const
00046   {
00047     return val_;
00048   }
00049 
00050   template <class CharT>
00051   bool
00052   generic_int_type<CharT>::
00053   operator == (const generic_int_type<CharT>& rhs) const
00054   {
00055     if (rhs.eof_)
00056       return eof_;
00057     else
00058       return not eof_ and (rhs.val_ == val_);
00059   }
00060 
00061   template <class CharT>
00062   bool
00063   generic_int_type<CharT>::eof() const
00064   {
00065     return eof_;
00066   }
00067 
00068   /*------------.
00069   | char_traits |
00070   `------------*/
00071 
00072   template <typename CharT>
00073   void
00074   char_traits<CharT>::assign(char_type& lhs, const char_type& rhs)
00075   {
00076     lhs = rhs;
00077   }
00078 
00079   template <typename CharT>
00080   bool
00081   char_traits<CharT>::eq(const char_type& lhs, const char_type& rhs)
00082   {
00083     return lhs == rhs;
00084   }
00085 
00086   template <typename CharT>
00087   bool
00088   char_traits<CharT>::lt(const char_type& lhs, const char_type& rhs)
00089   {
00090     return lhs < rhs;
00091   }
00092 
00093   template <typename CharT>
00094   int
00095   char_traits<CharT>::compare(const char_type* p,
00096                               const char_type* q,
00097                               size_t n)
00098   {
00099     size_t i;
00100 
00101     for (i = 0; (i < n) and eq(p[i], q[i]); ++i)
00102       ;
00103     if (i == n)
00104       return 0;
00105     else
00106       return lt(p[i], q[i]) ? -1 : 1;
00107   }
00108 
00109   template <typename CharT>
00110   size_t
00111   char_traits<CharT>::length(const char_type* p)
00112   {
00113     size_t i = 0;
00114     while (not eq(p[i], char_type()))
00115       ++i;
00116     return i;
00117   }
00118 
00119   template <typename CharT>
00120   const typename char_traits<CharT>::char_type*
00121   char_traits<CharT>::find(const char_type* p,
00122                            size_t n,
00123                            const char_type& c)
00124   {
00125     size_t i;
00126 
00127     for (i = 0; (i < n) and not eq(p[i], c); ++i)
00128       ;
00129     return i < n ? p + i: 0;
00130   }
00131 
00132   template <typename CharT>
00133   typename char_traits<CharT>::char_type*
00134   char_traits<CharT>::move(char_type* s, const char_type* p, size_t n)
00135   {
00136     // FIXME:  This  code has  been  (almost)  dummy  pasted from  the
00137     // standard library. Maybe it should  be usefull to have a special
00138     // optimized version when pointers do not overlap.
00139 
00140     char_type* tmp = new char_type[n];
00141     copy(tmp, p, n);
00142     copy(s, tmp, n);
00143     delete[] tmp;
00144     return s;
00145   }
00146 
00147   template <typename CharT>
00148   typename char_traits<CharT>::char_type*
00149   char_traits<CharT>::copy(char_type* s, const char_type* p, size_t n)
00150   {
00151     precondition((p < s) or (p > s + n));
00152 
00153     for (size_t i = 0; i < n; ++i)
00154       assign(s[i], p[i]);
00155     return s;
00156   }
00157 
00158   template <typename CharT>
00159   typename char_traits<CharT>::char_type*
00160   char_traits<CharT>::assign(char_type* s, size_t n, char_type c)
00161   {
00162     for (size_t i = 0; i < n; ++i)
00163       assign(s[i], c);
00164     return s;
00165   }
00166 
00167   template <typename CharT>
00168   typename char_traits<CharT>::int_type
00169   char_traits<CharT>::not_eof(const int_type& e)
00170   {
00171     return eq_int_type(e, eof()) ? int_type(char_type()) : e;
00172   }
00173 
00174   template <typename CharT>
00175   typename char_traits<CharT>::char_type
00176   char_traits<CharT>::to_char_type(const int_type& e)
00177   {
00178     return e; // Calls generic_int_type<char_type>::operator char_type ().
00179   }
00180 
00181   template <typename CharT>
00182   typename char_traits<CharT>::int_type
00183   char_traits<CharT>::to_int_type(const char_type& e)
00184   {
00185     return int_type(e);
00186   }
00187 
00188   template <typename CharT>
00189   bool
00190   char_traits<CharT>::eq_int_type(const int_type& e, const int_type& f)
00191   {
00192     return e == f;
00193   }
00194 
00195   template <typename CharT>
00196   typename char_traits<CharT>::int_type
00197   char_traits<CharT>::eof()
00198   {
00199     return int_type();
00200   }
00201 
00202 } // end of namespace utility
00203 
00204 #endif // ! VCSN_MISC_CHAR_TRAITS_HXX

Generated on Fri Jul 28 12:18:30 2006 for Vaucanson by  doxygen 1.4.6