- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

# 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, 2005, 2006 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 vcsn
    00024 {
    00025   namespace misc
    00026   {
    00027
    00028   /*-----------------.
    00029   | generic_int_type |
    00030   `-----------------*/
    00031
    00032     template <class CharT>
    00033     generic_int_type<CharT>::generic_int_type () :
    00034       //     val_ (char_type ()),
    00035       eof_ (true)
    00036     {
    00037     }
    00038
    00039     template <class CharT>
    00040     generic_int_type<CharT>::generic_int_type (const char_type& val) :
    00041       val_ (val), eof_ (false)
    00042     {
    00043     }
    00044
    00045     template <class CharT>
    00046     generic_int_type<CharT>::operator CharT () const
    00047     {
    00048       return val_;
    00049     }
    00050
    00051     template <class CharT>
    00052     bool
    00053     generic_int_type<CharT>::
    00054     operator== (const generic_int_type<CharT>& rhs) const
    00055     {
    00056       if (rhs.eof_)
    00057         return eof_;
    00058       else
    00059         return not eof_ and (rhs.val_ == val_);
    00060     }
    00061
    00062     template <class CharT>
    00063     bool
    00064     generic_int_type<CharT>::eof () const
    00065     {
    00066       return eof_;
    00067     }
    00068
    00069   /*------------.
    00070   | char_traits |
    00071   `------------*/
    00072
    00073     template <typename CharT>
    00074     void
    00075     char_traits<CharT>::assign (char_type& lhs, const char_type& rhs)
    00076     {
    00077       lhs = rhs;
    00078     }
    00079
    00080     template <typename CharT>
    00081     bool
    00082     char_traits<CharT>::eq (const char_type& lhs, const char_type& rhs)
    00083     {
    00084       return lhs == rhs;
    00085     }
    00086
    00087     template <typename CharT>
    00088     bool
    00089     char_traits<CharT>::lt (const char_type& lhs, const char_type& rhs)
    00090     {
    00091       return lhs < rhs;
    00092     }
    00093
    00094     template <typename CharT>
    00095     int
    00096     char_traits<CharT>::compare (const char_type* p,
    00097                                 const char_type* q,
    00098                                 size_t n)
    00099     {
    00100       size_t i;
    00101
    00102       for (i = 0; (i < n) and eq (p[i], q[i]); ++i)
    00103         ;
    00104       if (i == n)
    00105         return 0;
    00106       else
    00107         return lt (p[i], q[i]) ? -1 : 1;
    00108     }
    00109
    00110     template <typename CharT>
    00111     size_t
    00112     char_traits<CharT>::length (const char_type* p)
    00113     {
    00114       size_t i = 0;
    00115       while (not eq (p[i], char_type ()))
    00116         ++i;
    00117       return i;
    00118     }
    00119
    00120     template <typename CharT>
    00121     const typename char_traits<CharT>::char_type*
    00122     char_traits<CharT>::find (const char_type* p,
    00123                              size_t n,
    00124                              const char_type& c)
    00125     {
    00126       size_t i;
    00127
    00128       for (i = 0; (i < n) and not eq (p[i], c); ++i)
    00129         ;
    00130       return i < n ? p + i: 0;
    00131     }
    00132
    00133     template <typename CharT>
    00134     typename char_traits<CharT>::char_type*
    00135     char_traits<CharT>::move (char_type* s, const char_type* p, size_t n)
    00136     {
    00137       // FIXME:  This  code has  been  (almost)  dummy  pasted from  the
    00138       // standard library. Maybe it should  be usefull to have a special
    00139       // optimized version when pointers do not overlap.
    00140
    00141       char_type* tmp = new char_type[n];
    00142       copy (tmp, p, n);
    00143       copy (s, tmp, n);
    00144       delete[] tmp;
    00145       return s;
    00146     }
    00147
    00148     template <typename CharT>
    00149     typename char_traits<CharT>::char_type*
    00150     char_traits<CharT>::copy (char_type* s, const char_type* p, size_t n)
    00151     {
    00152       precondition ((p < s) or (p > s + n));
    00153
    00154       for (size_t i = 0; i < n; ++i)
    00155         assign (s[i], p[i]);
    00156       return s;
    00157     }
    00158
    00159     template <typename CharT>
    00160     typename char_traits<CharT>::char_type*
    00161     char_traits<CharT>::assign (char_type* s, size_t n, char_type c)
    00162     {
    00163       for (size_t i = 0; i < n; ++i)
    00164         assign (s[i], c);
    00165       return s;
    00166     }
    00167
    00168     template <typename CharT>
    00169     typename char_traits<CharT>::int_type
    00170     char_traits<CharT>::not_eof (const int_type& e)
    00171     {
    00172       return eq_int_type (e, eof ()) ? int_type (char_type ()) : e;
    00173     }
    00174
    00175     template <typename CharT>
    00176     typename char_traits<CharT>::char_type
    00177     char_traits<CharT>::to_char_type (const int_type& e)
    00178     {
    00179       return e; // Calls generic_int_type<char_type>::operator CharT ().
    00180     }
    00181
    00182     template <typename CharT>
    00183     typename char_traits<CharT>::int_type
    00184     char_traits<CharT>::to_int_type (const char_type& e)
    00185     {
    00186       return int_type (e);
    00187     }
    00188
    00189     template <typename CharT>
    00190     bool
    00191     char_traits<CharT>::eq_int_type (const int_type& e, const int_type& f)
    00192     {
    00193       return e == f;
    00194     }
    00195
    00196     template <typename CharT>
    00197     typename char_traits<CharT>::int_type
    00198     char_traits<CharT>::eof ()
    00199     {
    00200       return int_type ();
    00201     }
    00202
    00203   } // end of namespace misc
    00204 } // end of namespace vcsn
    00205
    00206 #endif // ! VCSN_MISC_CHAR_TRAITS_HXX