Embedded Template Library 1.0
Loading...
Searching...
No Matches
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<signed char>
60 {
61 typedef signed char char_type;
62 typedef int int_type;
63 typedef long long off_type;
64 typedef size_t pos_type;
65 typedef signed char state_type;
66 };
67
68 template<> struct char_traits_types<unsigned char>
69 {
70 typedef unsigned char char_type;
71 typedef int int_type;
72 typedef long long off_type;
73 typedef size_t pos_type;
74 typedef unsigned char state_type;
75 };
76
77 template<> struct char_traits_types<wchar_t>
78 {
79 typedef wchar_t char_type;
80 typedef uint_least16_t int_type;
81 typedef long long off_type;
82 typedef size_t pos_type;
83 typedef char state_type;
84 };
85
86#if ETL_USING_CPP20
87 template<> struct char_traits_types<char8_t>
88 {
89 typedef char8_t char_type;
90 typedef unsigned int int_type;
91 typedef long long off_type;
92 typedef size_t pos_type;
93 typedef char state_type;
94 };
95#endif
96
97 template<> struct char_traits_types<char16_t>
98 {
99 typedef char16_t char_type;
100 typedef uint_least16_t int_type;
101 typedef long long off_type;
102 typedef size_t pos_type;
103 typedef char state_type;
104 };
105
106 template<> struct char_traits_types<char32_t>
107 {
108 typedef char32_t char_type;
109 typedef uint_least32_t int_type;
110 typedef long long off_type;
111 typedef size_t pos_type;
112 typedef char state_type;
113 };
114
115 //***************************************************************************
117 //***************************************************************************
118 template<typename T>
120 {
121 typedef typename char_traits_types<T>::char_type char_type;
122 typedef typename char_traits_types<T>::int_type int_type;
123 typedef typename char_traits_types<T>::off_type off_type;
124 typedef typename char_traits_types<T>::pos_type pos_type;
125 typedef typename char_traits_types<T>::state_type state_type;
126
127 //*************************************************************************
128 static ETL_CONSTEXPR bool eq(char_type a, char_type b) ETL_NOEXCEPT
129 {
130 return a == b;
131 }
132
133 //*************************************************************************
134 static ETL_CONSTEXPR bool lt(char_type a, char_type b) ETL_NOEXCEPT
135 {
136 return a < b;
137 }
138
139 //*************************************************************************
140 static ETL_CONSTEXPR14 size_t length(const char_type* begin) ETL_NOEXCEPT
141 {
142 if (begin == ETL_NULLPTR)
143 {
144 return 0;
145 }
146
147 const char_type* end = begin;
148
149 while (*end++ != 0)
150 {
151 // Do nothing.
152 }
153
154 return size_t(end - begin) - 1;
155 }
156
157 //*************************************************************************
158 static ETL_CONSTEXPR14 size_t length(const char_type* str, size_t max_length) ETL_NOEXCEPT
159 {
160 size_t count = 0UL;
161
162 if (str != 0)
163 {
164 while ((count < max_length) && (*str++ != 0))
165 {
166 ++count;
167 }
168 }
169
170 return count;
171 }
172
173 //*************************************************************************
174 static ETL_CONSTEXPR14 void assign(char_type& r, const char_type& c) ETL_NOEXCEPT
175 {
176 r = c;
177 }
178
179 //*************************************************************************
180 static ETL_CONSTEXPR14 char_type* assign(char_type* p, size_t n, char_type c) ETL_NOEXCEPT
181 {
182 if (p != ETL_NULLPTR)
183 {
184 etl::fill_n(p, n, c);
185 }
186
187 return p;
188 }
189
190 //*************************************************************************
191 static ETL_CONSTEXPR14 char_type* move(char_type* dst, const char_type* src, size_t count) ETL_NOEXCEPT
192 {
193 if ((dst < src) || (dst > (src + count)))
194 {
195 etl::copy_n(src, count, dst);
196 }
197 else
198 {
199 etl::copy_n(ETL_OR_STD::reverse_iterator<const char_type*>(src + count),
200 count,
201 ETL_OR_STD::reverse_iterator<char_type*>(dst + count));
202 }
203
204 return dst;
205 }
206
207 //*************************************************************************
208 static ETL_CONSTEXPR14 char_type* copy(char_type* dst, const char_type* src, size_t count) ETL_NOEXCEPT
209 {
210 etl::copy_n(src, count, dst);
211
212 return dst;
213 }
214
215 //*************************************************************************
216 static ETL_CONSTEXPR14 int compare(const char_type* s1, const char_type* s2, size_t count) ETL_NOEXCEPT
217 {
218 for (size_t i = 0UL; i < count; ++i)
219 {
220 const char_type c1 = *s1++;
221 const char_type c2 = *s2++;
222
223 if (c1 < c2)
224 {
225 return -1;
226 }
227 else if (c1 > c2)
228 {
229 return 1;
230 }
231 }
232
233 return 0;
234 }
235
236 //*************************************************************************
237 static ETL_CONSTEXPR14 const char_type* find(const char_type* p, size_t count, const char_type& ch) ETL_NOEXCEPT
238 {
239 for (size_t i = 0UL; i < count; ++i)
240 {
241 if (*p == ch)
242 {
243 return p;
244 }
245
246 ++p;
247 }
248
249 return 0;
250 }
251
252 //*************************************************************************
253 static ETL_CONSTEXPR char_type to_char_type(int_type c) ETL_NOEXCEPT
254 {
255 return static_cast<char_type>(c);
256 }
257
258 //*************************************************************************
259 static ETL_CONSTEXPR int_type to_int_type(char_type c) ETL_NOEXCEPT
260 {
261 return static_cast<int_type>(c);
262 }
263
264 //*************************************************************************
265 static ETL_CONSTEXPR bool eq_int_type(int_type c1, int_type c2) ETL_NOEXCEPT
266 {
267 return (c1 == c2);
268 }
269
270 //*************************************************************************
271 static ETL_CONSTEXPR int_type eof() ETL_NOEXCEPT
272 {
273 return -1;
274 }
275
276 //*************************************************************************
277 static ETL_CONSTEXPR int_type not_eof(int_type e) ETL_NOEXCEPT
278 {
279 return (e == eof()) ? eof() - 1 : e;
280 }
281 };
282
283 //***************************************************************************
285 //***************************************************************************
286 template <typename T>
287 ETL_CONSTEXPR14 size_t strlen(const T* t) ETL_NOEXCEPT
288 {
289 return etl::char_traits<T>::length(t);
290 }
291
292 //***************************************************************************
294 //***************************************************************************
295 template <typename T>
296 ETL_CONSTEXPR14 size_t strlen(const T* t, size_t max_length) ETL_NOEXCEPT
297 {
298 return etl::char_traits<T>::length(t, max_length);
299 }
300
301 //***************************************************************************
303 //***************************************************************************
304 template <typename T>
305 ETL_CONSTEXPR14 int strcmp(const T* t1, const T* t2) ETL_NOEXCEPT
306 {
307 while ((*t1 != 0) || (*t2 != 0))
308 {
309 if (*t1 > *t2)
310 {
311 return 1;
312 }
313
314 if (*t1 < *t2)
315 {
316 return -1;
317 }
318
319 ++t1;
320 ++t2;
321 }
322
323 return 0;
324 }
325
326 //***************************************************************************
328 //***************************************************************************
329 template <typename T>
330 ETL_CONSTEXPR14 int strncmp(const T* t1, const T* t2, size_t n) ETL_NOEXCEPT
331 {
332 while (((*t1 != 0) || (*t2 != 0)) && (n != 0))
333 {
334 if (*t1 < *t2)
335 {
336 return -1;
337 }
338 else if (*t1 > *t2)
339 {
340 return 1;
341 }
342
343 ++t1;
344 ++t2;
345 --n;
346 }
347
348 return 0;
349 }
350
351 //***************************************************************************
353 //***************************************************************************
354 template <typename T>
355 ETL_CONSTEXPR14 T* strcpy(T* dst, const T* src) ETL_NOEXCEPT
356 {
357 T* result = dst;
358
359 while (*src != 0)
360 {
361 *dst++ = *src++;
362 }
363
364 *dst = 0;
365
366 return result;
367 }
368
369 //***************************************************************************
371 //***************************************************************************
372 template <typename T>
373 ETL_CONSTEXPR14 T* strncpy(T* dst, const T* src, size_t n) ETL_NOEXCEPT
374 {
375 T* result = dst;
376
377 while ((*src != 0) && (n != 0))
378 {
379 *dst++ = *src++;
380 --n;
381 }
382
383 *dst = 0;
384
385 return result;
386 }
387}
388
389#endif
bitset_ext
Definition absolute.h:39
ETL_CONSTEXPR14 int strncmp(const T *t1, const T *t2, size_t n) ETL_NOEXCEPT
Alternative strncmp for all character types.
Definition char_traits.h:330
ETL_CONSTEXPR14 T * strncpy(T *dst, const T *src, size_t n) ETL_NOEXCEPT
Alternative strncpy for all character types.
Definition char_traits.h:373
ETL_CONSTEXPR TContainer::iterator begin(TContainer &container)
Definition iterator.h:962
ETL_CONSTEXPR14 T * strcpy(T *dst, const T *src) ETL_NOEXCEPT
Alternative strcpy for all character types.
Definition char_traits.h:355
ETL_CONSTEXPR14 int strcmp(const T *t1, const T *t2) ETL_NOEXCEPT
Alternative strcmp for all character types.
Definition char_traits.h:305
ETL_CONSTEXPR14 size_t strlen(const T *t) ETL_NOEXCEPT
Alternative strlen for all character types.
Definition char_traits.h:287
ETL_CONSTEXPR TContainer::iterator end(TContainer &container)
Definition iterator.h:992
Definition char_traits.h:48
Character traits for any character type.
Definition char_traits.h:120