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