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