ucn_traits.hh

Go to the documentation of this file.
00001 /*
00002    The lestes compiler suite
00003    Copyright (C) 2002, 2003, 2004, 2005 Miroslav Tichy
00004    Copyright (C) 2002, 2003, 2004, 2005 Petr Zika
00005    Copyright (C) 2002, 2003, 2004, 2005 Vojtech Hala
00006    Copyright (C) 2002, 2003, 2004, 2005 Jiri Kosina
00007    Copyright (C) 2002, 2003, 2004, 2005 Pavel Sanda
00008    Copyright (C) 2002, 2003, 2004, 2005 Jan Zouhar
00009    Copyright (C) 2002, 2003, 2004, 2005 Rudolf Thomas
00010 
00011    This program is free software; you can redistribute it and/or modify
00012    it under the terms of the GNU General Public License as published by
00013    the Free Software Foundation; version 2 of the License.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    See the full text of the GNU General Public License version 2, and
00021    the limitations in the file doc/LICENSE.
00022 
00023    By accepting the license the licensee waives any and all claims
00024    against the copyright holder(s) related in whole or in part to the
00025    work, its use, and/or the inability to use it.
00026  
00027  */
00028 #ifndef lestes__std___ucn_traits_hh___included
00029 #define lestes__std___ucn_traits_hh___included
00030 
00031 /*! \file
00032   Specialization of char_traits for ucn.
00033 */
00034 
00035 #include <cstdlib>
00036 #include <string>
00037 #include <lestes/common.hh>
00038 #include <lestes/std/character.hh>
00039 
00040 // this is ::std NOT ::lestes::std
00041 package(std);
00042 
00043 /*!
00044   Defines ucn specific char traits for ::std::basic_string.
00045 */
00046 template<>
00047 struct char_traits< ::lestes::std::ucn > {
00048         typedef ::lestes::std::ucn              char_type;
00049         // int_type has at least one extra bit than ucn
00050         typedef ::lestes::lc_host_uint_least32  int_type;
00051         typedef streampos                       pos_type;
00052         typedef streamoff                       off_type;
00053         typedef mbstate_t                       state_type;
00054 
00055         static void 
00056         assign(char_type& c1, const char_type& c2)
00057         {
00058                 c1 = c2;
00059         }
00060 
00061         static bool 
00062         eq(const char_type& c1, const char_type& c2)
00063         {
00064                 return c1 == c2;
00065         }
00066 
00067         static bool 
00068         lt(const char_type& c1, const char_type& c2)
00069         {
00070                 return c1 < c2;
00071         }
00072 
00073         static int 
00074         compare(const char_type* s1, const char_type* s2, size_t n)
00075         {
00076                 while (n && *s1 == *s2) {
00077                         s1++;
00078                         s2++;
00079                         n--;
00080                 }
00081                 return (n == 0) ? 0 : (*s1 > *s2) ? 1 : -1;
00082         }
00083 
00084         static size_t
00085         length(const char_type* s)
00086         {
00087                 size_t n = 0;
00088                 while (*s++)
00089                   n++;
00090 
00091                 return n; 
00092         }
00093 
00094         static const char_type* 
00095         find(const char_type* s, size_t n, const char_type& a)
00096         {
00097                 while (n--) {
00098                         if (*s == a)
00099                                 return s;
00100                         s++;
00101                 }
00102 
00103                 return static_cast<const char_type*>(0);
00104         }
00105 
00106         static char_type* 
00107         move(char_type* s1, const char_type* s2, size_t n)
00108         {
00109                 return static_cast<char_type*>(memmove(s1,s2,n * sizeof(char_type)));
00110         }
00111 
00112         static char_type* 
00113         copy(char_type* s1, const char_type* s2, size_t n)
00114         {
00115                 std::copy(s2,s2 + n,s1);
00116                 return s1;
00117         }
00118 
00119         static char_type* 
00120         assign(char_type* s, size_t n, char_type a)
00121         {
00122                 fill_n(s,n,a);
00123                 return s;
00124         }
00125 
00126         static char_type 
00127         to_char_type(const int_type& c)
00128         {
00129                 return static_cast<char_type>(c);
00130         }
00131 
00132         static int_type 
00133         to_int_type(const char_type& c)
00134         {
00135                 return static_cast<int_type>(c);
00136         }
00137 
00138         static bool 
00139         eq_int_type(const int_type& c1, const int_type& c2)
00140         {
00141                 return c1 == c2;
00142         }
00143 
00144         static int_type 
00145         eof(void) 
00146         {
00147                 return ~static_cast<int_type>(0);
00148         }
00149 
00150         static int_type 
00151         not_eof(const int_type& c)
00152         {
00153                 return (c == eof()) ? 0 : c;
00154         }
00155 };
00156 
00157 end_package(std);
00158 
00159 #endif
00160 /* vim: set ft=lestes : */

Generated on Mon Feb 12 18:27:11 2007 for lestes by doxygen 1.5.1-20070107