Stefan Scholz / ETL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers char_traits.h Source File

char_traits.h

Go to the documentation of this file.
00001 ///\file
00002 
00003 /******************************************************************************
00004 The MIT License(MIT)
00005 
00006 Embedded Template Library.
00007 https://github.com/ETLCPP/etl
00008 http://www.etlcpp.com
00009 
00010 Copyright(c) 2016 jwellbelove
00011 
00012 Permission is hereby granted, free of charge, to any person obtaining a copy
00013 of this software and associated documentation files(the "Software"), to deal
00014 in the Software without restriction, including without limitation the rights
00015 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
00016 copies of the Software, and to permit persons to whom the Software is
00017 furnished to do so, subject to the following conditions :
00018 
00019 The above copyright notice and this permission notice shall be included in all
00020 copies or substantial portions of the Software.
00021 
00022 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00023 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00024 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
00025 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00026 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00027 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00028 SOFTWARE.
00029 ******************************************************************************/
00030 
00031 #ifndef __ETL_CHAR_TRAITS__
00032 #define __ETL_CHAR_TRAITS__
00033 
00034 #include <algorithm>
00035 
00036 #include "platform.h "
00037 #include <stdint.h>
00038 #include "algorithm.h "
00039 
00040 //*****************************************************************************
00041 ///\defgroup char_traits char_traits
00042 /// Character traits
00043 ///\ingroup string
00044 //*****************************************************************************
00045 
00046 // Define the large character types if necessary.
00047 #if (ETL_NO_LARGE_CHAR_SUPPORT)
00048 typedef int16_t char16_t;
00049 typedef int32_t char32_t;
00050 #endif
00051 
00052 namespace etl
00053 {
00054   template<typename T> struct char_traits_types;
00055   
00056   template<> struct char_traits_types<char>
00057   {
00058     typedef char      char_type;
00059     typedef int       int_type;
00060     typedef long long off_type;
00061     typedef size_t    pos_type;
00062     typedef char      state_type;
00063   };
00064   
00065   template<> struct char_traits_types<wchar_t>
00066   {
00067     typedef wchar_t   char_type;
00068     typedef wchar_t   int_type;
00069     typedef long long off_type;
00070     typedef size_t    pos_type;
00071     typedef char      state_type;
00072   };
00073   
00074   template<> struct char_traits_types<char16_t>
00075   {
00076     typedef char16_t       char_type;
00077     typedef uint_least16_t int_type;
00078     typedef long long      off_type;
00079     typedef size_t         pos_type;
00080     typedef char           state_type; 
00081   };
00082   
00083   template<> struct char_traits_types<char32_t>
00084   {
00085     typedef char32_t       char_type;
00086     typedef uint_least32_t int_type;
00087     typedef long long      off_type;
00088     typedef size_t         pos_type;
00089     typedef char           state_type;  
00090   };
00091   
00092   //***************************************************************************
00093   /// Character traits for any character type.
00094   //***************************************************************************
00095   template<typename T>
00096   struct char_traits : public char_traits_types<T>
00097   {   
00098     typedef typename char_traits_types<T>::char_type  char_type;
00099     typedef typename char_traits_types<T>::int_type   int_type;
00100     typedef typename char_traits_types<T>::off_type   off_type;
00101     typedef typename char_traits_types<T>::pos_type   pos_type;
00102     typedef typename char_traits_types<T>::state_type state_type;
00103     
00104     //*************************************************************************
00105     static bool eq(char_type a, char_type b)
00106     {
00107       return a == b;
00108     }
00109     
00110     //*************************************************************************
00111     static bool lt(char_type a, char_type b)
00112     {
00113       return a < b;
00114     }     
00115     
00116     //*************************************************************************
00117     static size_t length(const char_type* str)
00118     {
00119       size_t count = 0;
00120           
00121       if (str != 0)
00122       {
00123         while (*str++ != 0)
00124         {
00125           ++count;
00126         }
00127       }
00128       
00129       return count;
00130     }
00131     
00132     //*************************************************************************
00133     static void assign(char_type& r, const char_type& c)
00134     {
00135       r = c;
00136     }
00137     
00138     //*************************************************************************
00139     static char_type* assign(char_type* p, size_t n, char_type c)
00140     {
00141       if (p != 0)
00142       {
00143         std::fill_n(p, n, c);
00144       }
00145       
00146       return p;
00147     }
00148     
00149     //*************************************************************************
00150     static char_type* move(char_type* dest, const char_type* src, size_t count)
00151     {
00152       if ((dest < src) || (dest > (src + count)))
00153       {
00154         etl::copy_n(src, src + count, dest);
00155       }
00156       else
00157       {
00158         etl::copy_n(std::reverse_iterator<char_type*>(src + count),
00159                     count,
00160                     std::reverse_iterator<char_type*>(dest + count));
00161       }
00162       
00163       return dest;
00164     }
00165     
00166     //*************************************************************************
00167     static char_type* copy(char_type* dest, const char_type* src, size_t count)
00168     {
00169       etl::copy_n(src, src + count, dest);
00170       
00171       return dest;
00172     }
00173     
00174     //*************************************************************************
00175     static int compare(const char_type* s1, const char_type* s2, size_t count)
00176     {       
00177       for (size_t i = 0; i < count; ++i)
00178       {
00179         if (*s1 < *s2)
00180         {
00181           return -1;
00182         }
00183           else if (*s1 > *s2)
00184         {
00185           return 1;
00186         }
00187         
00188         ++s1;
00189         ++s2;
00190       }
00191         
00192       return 0;
00193     }
00194     
00195     //*************************************************************************
00196     static const char_type* find(const char_type* p, size_t count, const char_type& ch)
00197     {     
00198       for (size_t i = 0; i < count; ++i)
00199       {
00200         if (*p == ch)
00201         {
00202           return p;
00203         }
00204         
00205         ++p;
00206       }
00207       
00208       return 0;
00209     }
00210     
00211     //*************************************************************************
00212     static char_type to_char_type(int_type c)
00213     {
00214       return static_cast<char_type>(c);
00215     }
00216     
00217     //*************************************************************************
00218     static int_type to_int_type(char_type c)
00219     {
00220       return static_cast<int_type>(c);
00221     }
00222     
00223     //*************************************************************************
00224     static bool eq_int_type(int_type c1, int_type c2)
00225     {
00226       return (c1 == c2);
00227     }
00228     
00229     //*************************************************************************
00230     static int_type eof()
00231     {
00232       return -1;
00233     }
00234     
00235     //*************************************************************************
00236     static int_type not_eof(int_type e)
00237     {
00238       return (e == eof()) ? eof() - 1 : e;
00239     }
00240   };
00241 }
00242 
00243 #endif
00244