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;
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)
129 {
130 return a == b;
131 }
132
133 //*************************************************************************
134 static ETL_CONSTEXPR bool lt(char_type a, char_type b)
135 {
136 return a < b;
137 }
138
139 //*************************************************************************
140 static ETL_CONSTEXPR14 size_t length(const char_type* str)
141 {
142 size_t count = 0UL;
143
144 if (str != 0)
145 {
146 while (*str++ != 0)
147 {
148 ++count;
149 }
150 }
151
152 return count;
153 }
154
155 //*************************************************************************
156 static ETL_CONSTEXPR14 size_t length(const char_type* str, size_t max_length)
157 {
158 size_t count = 0UL;
159
160 if (str != 0)
161 {
162 while ((count < max_length) && (*str++ != 0))
163 {
164 ++count;
165 }
166 }
167
168 return count;
169 }
170
171 //*************************************************************************
172 static ETL_CONSTEXPR14 void assign(char_type& r, const char_type& c)
173 {
174 r = c;
175 }
176
177 //*************************************************************************
178 static ETL_CONSTEXPR14 char_type* assign(char_type* p, size_t n, char_type c)
179 {
180 if (p != ETL_NULLPTR)
181 {
182 etl::fill_n(p, n, c);
183 }
184
185 return p;
186 }
187
188 //*************************************************************************
189 static ETL_CONSTEXPR14 char_type* move(char_type* dst, const char_type* src, size_t count)
190 {
191 if ((dst < src) || (dst > (src + count)))
192 {
193 etl::copy_n(src, count, dst);
194 }
195 else
196 {
197 etl::copy_n(ETL_OR_STD::reverse_iterator<const char_type*>(src + count),
198 count,
199 ETL_OR_STD::reverse_iterator<char_type*>(dst + count));
200 }
201
202 return dst;
203 }
204
205 //*************************************************************************
206 static ETL_CONSTEXPR14 char_type* copy(char_type* dst, const char_type* src, size_t count)
207 {
208 etl::copy_n(src, count, dst);
209
210 return dst;
211 }
212
213 //*************************************************************************
214 static ETL_CONSTEXPR14 int compare(const char_type* s1, const char_type* s2, size_t count)
215 {
216 for (size_t i = 0UL; i < count; ++i)
217 {
218 const char_type c1 = *s1++;
219 const char_type c2 = *s2++;
220
221 if (c1 < c2)
222 {
223 return -1;
224 }
225 else if (c1 > c2)
226 {
227 return 1;
228 }
229 }
230
231 return 0;
232 }
233
234 //*************************************************************************
235 static ETL_CONSTEXPR14 const char_type* find(const char_type* p, size_t count, const char_type& ch)
236 {
237 for (size_t i = 0UL; i < count; ++i)
238 {
239 if (*p == ch)
240 {
241 return p;
242 }
243
244 ++p;
245 }
246
247 return 0;
248 }
249
250 //*************************************************************************
251 static ETL_CONSTEXPR char_type to_char_type(int_type c)
252 {
253 return static_cast<char_type>(c);
254 }
255
256 //*************************************************************************
257 static ETL_CONSTEXPR int_type to_int_type(char_type c)
258 {
259 return static_cast<int_type>(c);
260 }
261
262 //*************************************************************************
263 static ETL_CONSTEXPR bool eq_int_type(int_type c1, int_type c2)
264 {
265 return (c1 == c2);
266 }
267
268 //*************************************************************************
269 static ETL_CONSTEXPR int_type eof()
270 {
271 return -1;
272 }
273
274 //*************************************************************************
275 static ETL_CONSTEXPR int_type not_eof(int_type e)
276 {
277 return (e == eof()) ? eof() - 1 : e;
278 }
279 };
280
281 //***************************************************************************
283 //***************************************************************************
284 template <typename T>
285 ETL_CONSTEXPR14 size_t strlen(const T* t)
286 {
288 }
289
290 //***************************************************************************
292 //***************************************************************************
293 template <typename T>
294 ETL_CONSTEXPR14 size_t strlen(const T* t, size_t max_length)
295 {
297 }
298
299 //***************************************************************************
301 //***************************************************************************
302 template <typename T>
303 ETL_CONSTEXPR14 int strcmp(const T* t1, const T* t2)
304 {
305 while ((*t1 != 0) || (*t2 != 0))
306 {
307 if (*t1 > *t2)
308 {
309 return 1;
310 }
311
312 if (*t1 < *t2)
313 {
314 return -1;
315 }
316
317 ++t1;
318 ++t2;
319 }
320
321 return 0;
322 }
323
324 //***************************************************************************
326 //***************************************************************************
327 template <typename T>
328 ETL_CONSTEXPR14 int strncmp(const T* t1, const T* t2, size_t n)
329 {
330 while (((*t1 != 0) || (*t2 != 0)) && (n != 0))
331 {
332 if (*t1 < *t2)
333 {
334 return -1;
335 }
336 else if (*t1 > *t2)
337 {
338 return 1;
339 }
340
341 ++t1;
342 ++t2;
343 --n;
344 }
345
346 return 0;
347 }
348
349 //***************************************************************************
351 //***************************************************************************
352 template <typename T>
353 ETL_CONSTEXPR14 T* strcpy(T* dst, const T* src)
354 {
355 T* result = dst;
356
357 while (*src != 0)
358 {
359 *dst++ = *src++;
360 }
361
362 *dst = 0;
363
364 return result;
365 }
366
367 //***************************************************************************
369 //***************************************************************************
370 template <typename T>
371 ETL_CONSTEXPR14 T* strncpy(T* dst, const T* src, size_t n)
372 {
373 T* result = dst;
374
375 while ((*src != 0) && (n != 0))
376 {
377 *dst++ = *src++;
378 --n;
379 }
380
381 *dst = 0;
382
383 return result;
384 }
385}
386
387#endif
bitset_ext
Definition absolute.h:38
ETL_CONSTEXPR14 int strncmp(const T *t1, const T *t2, size_t n)
Alternative strncmp for all character types.
Definition char_traits.h:328
ETL_CONSTEXPR14 size_t strlen(const T *t)
Alternative strlen for all character types.
Definition char_traits.h:285
ETL_CONSTEXPR14 T * strncpy(T *dst, const T *src, size_t n)
Alternative strncpy for all character types.
Definition char_traits.h:371
ETL_CONSTEXPR14 int strcmp(const T *t1, const T *t2)
Alternative strcmp for all character types.
Definition char_traits.h:303
ETL_CONSTEXPR14 T * strcpy(T *dst, const T *src)
Alternative strcpy for all character types.
Definition char_traits.h:353
Definition char_traits.h:48
Character traits for any character type.
Definition char_traits.h:120
Definition compare.h:51
pair holds two objects of arbitrary type
Definition utility.h:164