Embedded Template Library 1.0
Loading...
Searching...
No Matches
u8string.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) 2023 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_U8STRING_INCLUDED
32#define ETL_U8STRING_INCLUDED
33
34#include "platform.h"
35#include "basic_string.h"
36#include "string_view.h"
37#include "hash.h"
38#include "initializer_list.h"
39
40#include <ctype.h>
41
42#include "private/minmax_push.h"
43
44#if ETL_HAS_CHAR8_T
45namespace etl
46{
47#if ETL_USING_CPP11 && ETL_HAS_NATIVE_CHAR8_T
48 inline namespace literals
49 {
50 inline namespace string_literals
51 {
52 constexpr etl::u8string_view operator ""_sv(const char8_t* str, size_t length) noexcept
53 {
54 return etl::u8string_view{ str, length };
55 }
56 }
57 }
58#endif
59
60 typedef etl::ibasic_string<char8_t> iu8string;
61
62 //***************************************************************************
66 //***************************************************************************
67 template <size_t MAX_SIZE_>
68 class u8string : public iu8string
69 {
70 public:
71
72 typedef iu8string base_type;
74
75 typedef iu8string::value_type value_type;
76
77 static ETL_CONSTANT size_t MAX_SIZE = MAX_SIZE_;
78
79 //*************************************************************************
81 //*************************************************************************
83 : iu8string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
84 {
85 this->initialise();
86 }
87
88 //*************************************************************************
91 //*************************************************************************
93 : iu8string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
94 {
95 this->assign(other);
96 }
97
98 //*************************************************************************
101 //*************************************************************************
103 : iu8string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
104 {
105 this->assign(other);
106 }
107
108 //*************************************************************************
113 //*************************************************************************
114 u8string(const etl::iu8string& other, size_t position, size_t length = npos)
115 : iu8string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
116 {
117 ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
118
119 this->assign(other, position, length);
120 }
121
122 //*************************************************************************
125 //*************************************************************************
126 ETL_EXPLICIT_STRING_FROM_CHAR u8string(const value_type* text)
127 : iu8string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
128 {
129 this->assign(text, text + etl::char_traits<value_type>::length(text));
130 }
131
132 //*************************************************************************
136 //*************************************************************************
137 u8string(const value_type* text, size_t count)
138 : iu8string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
139 {
140 this->assign(text, text + count);
141 }
142
143 //*************************************************************************
147 //*************************************************************************
148 u8string(size_type count, value_type c)
149 : iu8string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
150 {
151 this->initialise();
152 this->resize(count, c);
153 }
154
155 //*************************************************************************
160 //*************************************************************************
161 template <typename TIterator>
163 : iu8string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
164 {
165 this->assign(first, last);
166 }
167
168#if ETL_HAS_INITIALIZER_LIST
169 //*************************************************************************
171 //*************************************************************************
172 u8string(std::initializer_list<value_type> init)
173 : iu8string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
174 {
175 this->assign(init.begin(), init.end());
176 }
177#endif
178
179 //*************************************************************************
182 //*************************************************************************
184 : iu8string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
185 {
186 this->assign(view.begin(), view.end());
187 }
188
189 //*************************************************************************
193 //*************************************************************************
195 {
197
198 if (position != this->size())
199 {
201
202 length_ = etl::min(length_, this->size() - position);
203
204 new_string.assign(buffer + position, buffer + position + length_);
205 }
206
207 return new_string;
208 }
209
210 //*************************************************************************
212 //*************************************************************************
214 {
215 if (&rhs != this)
216 {
217 this->assign(rhs);
218 }
219
220 return *this;
221 }
222
223
224 //*************************************************************************
226 //*************************************************************************
228 {
229 if (&rhs != this)
230 {
231 this->assign(rhs);
232 }
233
234 return *this;
235 }
236
237 //*************************************************************************
239 //*************************************************************************
240 u8string& operator = (const value_type* text)
241 {
242 this->assign(text);
243
244 return *this;
245 }
246
247 //*************************************************************************
249 //*************************************************************************
251 {
252 this->assign(view);
253
254 return *this;
255 }
256
257 //*************************************************************************
259 //*************************************************************************
260#if ETL_HAS_ISTRING_REPAIR
261 virtual void repair() ETL_OVERRIDE
262#else
263 void repair()
264#endif
265 {
267 }
268
269 private:
270
271 value_type buffer[MAX_SIZE + 1];
272 };
273
274 template <size_t MAX_SIZE_>
275 ETL_CONSTANT size_t u8string<MAX_SIZE_>::MAX_SIZE;
276
277 //***************************************************************************
280 //***************************************************************************
281 class u8string_ext : public iu8string
282 {
283 public:
284
285 typedef iu8string base_type;
287
288 typedef iu8string::value_type value_type;
290
291 //*************************************************************************
293 //*************************************************************************
294 u8string_ext(value_type* buffer, size_type buffer_size)
295 : iu8string(buffer, buffer_size - 1U)
296 {
297 this->initialise();
298 }
299
300 //*************************************************************************
303 //*************************************************************************
304 u8string_ext(const etl::u8string_ext& other, value_type* buffer, size_type buffer_size)
305 : iu8string(buffer, buffer_size - 1U)
306 {
307 this->assign(other);
308 }
309
310 //*************************************************************************
313 //*************************************************************************
314 u8string_ext(const etl::iu8string& other, value_type* buffer, size_type buffer_size)
315 : iu8string(buffer, buffer_size - 1U)
316 {
317 this->assign(other);
318 }
319
320 //*************************************************************************
325 //*************************************************************************
326 u8string_ext(const etl::iu8string& other, value_type* buffer, size_type buffer_size, size_type position, size_type length = npos)
327 : iu8string(buffer, buffer_size - 1U)
328 {
329 ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
330
331 this->assign(other, position, length);
332 }
333
334 //*************************************************************************
337 //*************************************************************************
338 u8string_ext(const char8_t* text, char8_t* buffer, size_type buffer_size)
339 : iu8string(buffer, buffer_size - 1U)
340 {
341 // Is the initial text at the same address as the buffer?
342 if (text == buffer)
343 {
344 this->current_size = etl::strlen(buffer);
345 }
346 else
347 {
348 this->assign(text, text + etl::strlen(text));
349 }
350 }
351
352 //*************************************************************************
356 //*************************************************************************
357 u8string_ext(const value_type* text, size_type count, value_type* buffer, size_type buffer_size)
358 : iu8string(buffer, buffer_size - 1U)
359 {
360 this->assign(text, text + count);
361 }
362
363 //*************************************************************************
367 //*************************************************************************
368 u8string_ext(size_type count, value_type c, value_type* buffer, size_type buffer_size)
369 : iu8string(buffer, buffer_size - 1U)
370 {
371 this->initialise();
372 this->resize(count, c);
373 }
374
375 //*************************************************************************
380 //*************************************************************************
381 template <typename TIterator>
382 u8string_ext(TIterator first, TIterator last, value_type* buffer, size_type buffer_size, typename etl::enable_if<!etl::is_integral<TIterator>::value, int>::type = 0)
383 : iu8string(buffer, buffer_size - 1U)
384 {
385 this->assign(first, last);
386 }
387
388#if ETL_HAS_INITIALIZER_LIST
389 //*************************************************************************
391 //*************************************************************************
392 u8string_ext(std::initializer_list<value_type> init, value_type* buffer, size_type buffer_size)
393 : iu8string(buffer, buffer_size - 1U)
394 {
395 this->assign(init.begin(), init.end());
396 }
397#endif
398
399 //*************************************************************************
402 //*************************************************************************
403 explicit u8string_ext(const etl::u8string_view& view, value_type* buffer, size_type buffer_size)
404 : iu8string(buffer, buffer_size - 1U)
405 {
406 this->assign(view.begin(), view.end());
407 }
408
409 //*************************************************************************
411 //*************************************************************************
413 {
414 if (&rhs != this)
415 {
416 this->assign(rhs);
417 }
418
419 return *this;
420 }
421
422 //*************************************************************************
424 //*************************************************************************
426 {
427 if (&rhs != this)
428 {
429 this->assign(rhs);
430 }
431
432 return *this;
433 }
434
435 //*************************************************************************
437 //*************************************************************************
438 u8string_ext& operator = (const value_type* text)
439 {
440 this->assign(text);
441
442 return *this;
443 }
444
445 //*************************************************************************
447 //*************************************************************************
449 {
450 this->assign(view);
451
452 return *this;
453 }
454
455 //*************************************************************************
457 //*************************************************************************
458#if ETL_HAS_ISTRING_REPAIR
459 virtual void repair() ETL_OVERRIDE
460#else
461 void repair()
462#endif
463 {
464 }
465
466 private:
467
468 //*************************************************************************
470 //*************************************************************************
471 u8string_ext(const u8string_ext& other) ETL_DELETE;
472 };
473
474 //*************************************************************************
476 //*************************************************************************
477#if ETL_USING_8BIT_TYPES
479 template <>
480 struct hash<etl::iu8string>
481 {
482 size_t operator()(const etl::iu8string& text) const
483 {
484 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
485 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
486 }
487 };
488
489 template <size_t SIZE>
490 struct hash<etl::u8string<SIZE> >
491 {
492 size_t operator()(const etl::u8string<SIZE>& text) const
493 {
494 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
495 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
496 }
497 };
498
499 template <>
500 struct hash<etl::u8string_ext>
501 {
502 size_t operator()(const etl::u8string_ext& text) const
503 {
504 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
505 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
506 }
507 };
509#endif
510
511 //***************************************************************************
513 //***************************************************************************
514 template<size_t Array_Size>
515 etl::u8string<Array_Size - 1U> make_string(const char(&text)[Array_Size])
516 {
517 return etl::u8string<Array_Size - 1U>(text, etl::strlen(text, Array_Size - 1));
518 }
519
520 //***************************************************************************
522 //***************************************************************************
523 template<size_t MAX_SIZE, size_t SIZE>
525 {
526 return etl::u8string<MAX_SIZE>(text, etl::strlen(text, SIZE));
527 }
528}
529#endif
530
531#include "private/minmax_pop.h"
532
533#endif
String view.
Definition string_view.h:100
ETL_CONSTEXPR const_iterator begin() const
Returns a const iterator to the beginning of the array.
Definition string_view.h:197
Definition basic_string.h:337
void resize(size_type new_size)
Definition basic_string.h:467
void assign(const etl::ibasic_string< T > &other)
Definition basic_string.h:663
pointer data()
Definition basic_string.h:626
void initialise()
Initialise the string.
Definition basic_string.h:2525
void repair_buffer(T *p_buffer_)
Fix the internal pointers after a low level memory copy.
Definition basic_string.h:2538
size_type length() const
Definition basic_string.h:196
size_type current_size
The current number of elements in the string.
Definition basic_string.h:322
size_type size() const
Definition basic_string.h:187
Definition basic_string.h:109
Definition u8string.h:282
u8string_ext(TIterator first, TIterator last, value_type *buffer, size_type buffer_size, typename etl::enable_if<!etl::is_integral< TIterator >::value, int >::type=0)
Definition u8string.h:382
u8string_ext(const char8_t *text, char8_t *buffer, size_type buffer_size)
Definition u8string.h:338
u8string_ext & operator=(const u8string_ext &rhs)
Assignment operator.
Definition u8string.h:412
u8string_ext(const etl::u8string_ext &other, value_type *buffer, size_type buffer_size)
Definition u8string.h:304
u8string_ext(const etl::iu8string &other, value_type *buffer, size_type buffer_size, size_type position, size_type length=npos)
Definition u8string.h:326
void repair()
Fix the internal pointers after a low level memory copy.
Definition u8string.h:461
u8string_ext(value_type *buffer, size_type buffer_size)
Constructor.
Definition u8string.h:294
u8string_ext(const value_type *text, size_type count, value_type *buffer, size_type buffer_size)
Definition u8string.h:357
u8string_ext(const etl::iu8string &other, value_type *buffer, size_type buffer_size)
Definition u8string.h:314
u8string_ext(size_type count, value_type c, value_type *buffer, size_type buffer_size)
Definition u8string.h:368
u8string_ext(const etl::u8string_view &view, value_type *buffer, size_type buffer_size)
Definition u8string.h:403
Definition u8string.h:69
u8string & operator=(const u8string &rhs)
Assignment operator.
Definition u8string.h:213
u8string(const value_type *text, size_t count)
Definition u8string.h:137
u8string(size_type count, value_type c)
Definition u8string.h:148
void repair()
Fix the internal pointers after a low level memory copy.
Definition u8string.h:263
etl::u8string< MAX_SIZE_ > substr(size_type position=0, size_type length_=npos) const
Definition u8string.h:194
u8string(const etl::u8string_view &view)
Definition u8string.h:183
u8string(const etl::iu8string &other, size_t position, size_t length=npos)
Definition u8string.h:114
u8string(const etl::u8string< MAX_SIZE_ > &other)
Definition u8string.h:92
ETL_EXPLICIT_STRING_FROM_CHAR u8string(const value_type *text)
Definition u8string.h:126
u8string()
Constructor.
Definition u8string.h:82
u8string(TIterator first, TIterator last, typename etl::enable_if<!etl::is_integral< TIterator >::value, int >::type=0)
Definition u8string.h:162
u8string(const etl::iu8string &other)
Definition u8string.h:102
#define ETL_ASSERT(b, e)
Definition error_handler.h:356
enable_if
Definition type_traits_generator.h:1186
is_integral
Definition type_traits_generator.h:996
bitset_ext
Definition absolute.h:38
etl::string< Array_Size - 1U > make_string(const char(&text)[Array_Size])
Hash function.
Definition string.h:513
etl::string< MAX_SIZE > make_string_with_capacity(const char(&text)[SIZE])
Make string with max capacity from string literal or array.
Definition string.h:522
ETL_CONSTEXPR14 size_t strlen(const T *t)
Alternative strlen for all character types.
Definition char_traits.h:285
Character traits for any character type.
Definition char_traits.h:120
pair holds two objects of arbitrary type
Definition utility.h:164
ETL_CONSTEXPR pair()
Default constructor.
Definition utility.h:176