Embedded Template Library  1.0
char_traits.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 https://github.com/ETLCPP/etl
8 https://www.etlcpp.com
9 
10 Copyright(c) 2016 jwellbelove
11 
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files(the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions :
18 
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29 ******************************************************************************/
30 
31 #ifndef ETL_CHAR_TRAITS_INCLUDED
32 #define ETL_CHAR_TRAITS_INCLUDED
33 
34 #include <stdint.h>
35 
36 #include "platform.h"
37 #include "algorithm.h"
38 #include "iterator.h"
39 
40 //*****************************************************************************
44 //*****************************************************************************
45 
46 // Define the large character types if necessary.
47 #if (ETL_NO_LARGE_CHAR_SUPPORT)
48 typedef int16_t char16_t;
49 typedef int32_t char32_t;
50 #endif
51 
52 namespace etl
53 {
54  template<typename T> struct char_traits_types;
55 
56  template<> struct char_traits_types<char>
57  {
58  typedef char char_type;
59  typedef int int_type;
60  typedef long long off_type;
61  typedef size_t pos_type;
62  typedef char state_type;
63  };
64 
65  template<> struct char_traits_types<wchar_t>
66  {
67  typedef wchar_t char_type;
68  typedef wchar_t int_type;
69  typedef long long off_type;
70  typedef size_t pos_type;
71  typedef char state_type;
72  };
73 
74  template<> struct char_traits_types<char16_t>
75  {
76  typedef char16_t char_type;
77  typedef uint_least16_t int_type;
78  typedef long long off_type;
79  typedef size_t pos_type;
80  typedef char state_type;
81  };
82 
83  template<> struct char_traits_types<char32_t>
84  {
85  typedef char32_t char_type;
86  typedef uint_least32_t int_type;
87  typedef long long off_type;
88  typedef size_t pos_type;
89  typedef char state_type;
90  };
91 
92  //***************************************************************************
94  //***************************************************************************
95  template<typename T>
96  struct char_traits : public char_traits_types<T>
97  {
98  typedef typename char_traits_types<T>::char_type char_type;
99  typedef typename char_traits_types<T>::int_type int_type;
100  typedef typename char_traits_types<T>::off_type off_type;
101  typedef typename char_traits_types<T>::pos_type pos_type;
102  typedef typename char_traits_types<T>::state_type state_type;
103 
104  //*************************************************************************
105  ETL_CONSTEXPR static bool eq(char_type a, char_type b)
106  {
107  return a == b;
108  }
109 
110  //*************************************************************************
111  ETL_CONSTEXPR static bool lt(char_type a, char_type b)
112  {
113  return a < b;
114  }
115 
116  //*************************************************************************
117  ETL_CONSTEXPR14 static size_t length(const char_type* str)
118  {
119  size_t count = 0;
120 
121  if (str != 0)
122  {
123  while (*str++ != 0)
124  {
125  ++count;
126  }
127  }
128 
129  return count;
130  }
131 
132  //*************************************************************************
133  static void assign(char_type& r, const char_type& c)
134  {
135  r = c;
136  }
137 
138  //*************************************************************************
139  static char_type* assign(char_type* p, size_t n, char_type c)
140  {
141  if (p != 0)
142  {
143  etl::fill_n(p, n, c);
144  }
145 
146  return p;
147  }
148 
149  //*************************************************************************
150  static char_type* move(char_type* dest, const char_type* src, size_t count)
151  {
152  if ((dest < src) || (dest > (src + count)))
153  {
154  etl::copy_n(src, src + count, dest);
155  }
156  else
157  {
158  etl::copy_n(ETL_OR_STD::reverse_iterator<char_type*>(src + count),
159  count,
160  ETL_OR_STD::reverse_iterator<char_type*>(dest + count));
161  }
162 
163  return dest;
164  }
165 
166  //*************************************************************************
167  static char_type* copy(char_type* dest, const char_type* src, size_t count)
168  {
169  etl::copy_n(src, src + count, dest);
170 
171  return dest;
172  }
173 
174  //*************************************************************************
175  static int compare(const char_type* s1, const char_type* s2, size_t count)
176  {
177  for (size_t i = 0; i < count; ++i)
178  {
179  if (*s1 < *s2)
180  {
181  return -1;
182  }
183  else if (*s1 > *s2)
184  {
185  return 1;
186  }
187 
188  ++s1;
189  ++s2;
190  }
191 
192  return 0;
193  }
194 
195  //*************************************************************************
196  static const char_type* find(const char_type* p, size_t count, const char_type& ch)
197  {
198  for (size_t i = 0; i < count; ++i)
199  {
200  if (*p == ch)
201  {
202  return p;
203  }
204 
205  ++p;
206  }
207 
208  return 0;
209  }
210 
211  //*************************************************************************
212  static char_type to_char_type(int_type c)
213  {
214  return static_cast<char_type>(c);
215  }
216 
217  //*************************************************************************
218  static int_type to_int_type(char_type c)
219  {
220  return static_cast<int_type>(c);
221  }
222 
223  //*************************************************************************
224  static bool eq_int_type(int_type c1, int_type c2)
225  {
226  return (c1 == c2);
227  }
228 
229  //*************************************************************************
230  static int_type eof()
231  {
232  return -1;
233  }
234 
235  //*************************************************************************
236  static int_type not_eof(int_type e)
237  {
238  return (e == eof()) ? eof() - 1 : e;
239  }
240  };
241 
242 
243  //***************************************************************************
245  //***************************************************************************
246  template <typename T>
247  size_t strlen(const T* t)
248  {
249  return etl::char_traits<T>::length(t);
250  }
251 }
252 
253 #endif
Definition: absolute.h:37
size_t strlen(const T *t)
Alternative strlen for all character types.
Definition: char_traits.h:247
Definition: char_traits.h:54
Character traits for any character type.
Definition: char_traits.h:97
Definition: compare.h:52