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 //*************************************************************************
250#if ETL_HAS_ISTRING_REPAIR
251 virtual void repair() ETL_OVERRIDE
252#else
253 void repair()
254#endif
255 {
257 }
258
259 private:
260
261 value_type buffer[MAX_SIZE + 1];
262 };
263
264 template <size_t MAX_SIZE_>
265 ETL_CONSTANT size_t u8string<MAX_SIZE_>::MAX_SIZE;
266
267 //***************************************************************************
270 //***************************************************************************
271 class u8string_ext : public iu8string
272 {
273 public:
274
275 typedef iu8string base_type;
277
278 typedef iu8string::value_type value_type;
280
281 //*************************************************************************
283 //*************************************************************************
284 u8string_ext(value_type* buffer, size_type buffer_size)
285 : iu8string(buffer, buffer_size - 1U)
286 {
287 this->initialise();
288 }
289
290 //*************************************************************************
293 //*************************************************************************
294 u8string_ext(const etl::u8string_ext& other, value_type* buffer, size_type buffer_size)
295 : iu8string(buffer, buffer_size - 1U)
296 {
297 this->assign(other);
298 }
299
300 //*************************************************************************
303 //*************************************************************************
304 u8string_ext(const etl::iu8string& other, value_type* buffer, size_type buffer_size)
305 : iu8string(buffer, buffer_size - 1U)
306 {
307 this->assign(other);
308 }
309
310 //*************************************************************************
315 //*************************************************************************
316 u8string_ext(const etl::iu8string& other, value_type* buffer, size_type buffer_size, size_type position, size_type length = npos)
317 : iu8string(buffer, buffer_size - 1U)
318 {
319 ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
320
321 this->assign(other, position, length);
322 }
323
324 //*************************************************************************
327 //*************************************************************************
328 u8string_ext(const char8_t* text, char8_t* buffer, size_type buffer_size)
329 : iu8string(buffer, buffer_size - 1U)
330 {
331 // Is the initial text at the same address as the buffer?
332 if (text == buffer)
333 {
334 this->current_size = etl::strlen(buffer);
335 }
336 else
337 {
338 this->assign(text, text + etl::strlen(text));
339 }
340 }
341
342 //*************************************************************************
346 //*************************************************************************
347 u8string_ext(const value_type* text, size_type count, value_type* buffer, size_type buffer_size)
348 : iu8string(buffer, buffer_size - 1U)
349 {
350 this->assign(text, text + count);
351 }
352
353 //*************************************************************************
357 //*************************************************************************
358 u8string_ext(size_type count, value_type c, value_type* buffer, size_type buffer_size)
359 : iu8string(buffer, buffer_size - 1U)
360 {
361 this->initialise();
362 this->resize(count, c);
363 }
364
365 //*************************************************************************
370 //*************************************************************************
371 template <typename TIterator>
372 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)
373 : iu8string(buffer, buffer_size - 1U)
374 {
375 this->assign(first, last);
376 }
377
378#if ETL_HAS_INITIALIZER_LIST
379 //*************************************************************************
381 //*************************************************************************
382 u8string_ext(std::initializer_list<value_type> init, value_type* buffer, size_type buffer_size)
383 : iu8string(buffer, buffer_size - 1U)
384 {
385 this->assign(init.begin(), init.end());
386 }
387#endif
388
389 //*************************************************************************
392 //*************************************************************************
393 explicit u8string_ext(const etl::u8string_view& view, value_type* buffer, size_type buffer_size)
394 : iu8string(buffer, buffer_size - 1U)
395 {
396 this->assign(view.begin(), view.end());
397 }
398
399 //*************************************************************************
401 //*************************************************************************
403 {
404 if (&rhs != this)
405 {
406 this->assign(rhs);
407 }
408
409 return *this;
410 }
411
412
413 //*************************************************************************
415 //*************************************************************************
417 {
418 if (&rhs != this)
419 {
420 this->assign(rhs);
421 }
422
423 return *this;
424 }
425
426 //*************************************************************************
428 //*************************************************************************
429 u8string_ext& operator = (const value_type* text)
430 {
431 this->assign(text);
432
433 return *this;
434 }
435
436 //*************************************************************************
438 //*************************************************************************
439#if ETL_HAS_ISTRING_REPAIR
440 virtual void repair() ETL_OVERRIDE
441#else
442 void repair()
443#endif
444 {
445 }
446
447 private:
448
449 //*************************************************************************
451 //*************************************************************************
452 u8string_ext(const u8string_ext& other) ETL_DELETE;
453 };
454
455 //*************************************************************************
457 //*************************************************************************
458#if ETL_USING_8BIT_TYPES
460 template <>
461 struct hash<etl::iu8string>
462 {
463 size_t operator()(const etl::iu8string& text) const
464 {
465 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
466 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
467 }
468 };
469
470 template <size_t SIZE>
471 struct hash<etl::u8string<SIZE> >
472 {
473 size_t operator()(const etl::u8string<SIZE>& text) const
474 {
475 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
476 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
477 }
478 };
479
480 template <>
481 struct hash<etl::u8string_ext>
482 {
483 size_t operator()(const etl::u8string_ext& text) const
484 {
485 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
486 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
487 }
488 };
490#endif
491
492 //***************************************************************************
494 //***************************************************************************
495 template<size_t Array_Size>
496 etl::u8string<Array_Size - 1U> make_string(const char(&text)[Array_Size])
497 {
498 return etl::u8string<Array_Size - 1U>(text, etl::strlen(text, Array_Size - 1));
499 }
500
501 //***************************************************************************
503 //***************************************************************************
504 template<size_t MAX_SIZE, size_t SIZE>
506 {
507 return etl::u8string<MAX_SIZE>(text, etl::strlen(text, SIZE));
508 }
509}
510#endif
511
512#include "private/minmax_pop.h"
513
514#endif
Definition basic_string.h:326
void resize(size_type new_size)
Definition basic_string.h:456
void assign(const etl::ibasic_string< T > &other)
Definition basic_string.h:636
pointer data()
Definition basic_string.h:599
void initialise()
Initialise the string.
Definition basic_string.h:2299
void repair_buffer(T *p_buffer_)
Fix the internal pointers after a low level memory copy.
Definition basic_string.h:2312
size_type length() const
Definition basic_string.h:185
size_type current_size
The current number of elements in the string.
Definition basic_string.h:311
size_type size() const
Definition basic_string.h:176
Definition basic_string.h:98
Definition u8string.h:272
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:372
u8string_ext(const char8_t *text, char8_t *buffer, size_type buffer_size)
Definition u8string.h:328
u8string_ext & operator=(const u8string_ext &rhs)
Assignment operator.
Definition u8string.h:402
u8string_ext(const etl::u8string_ext &other, value_type *buffer, size_type buffer_size)
Definition u8string.h:294
u8string_ext(const etl::iu8string &other, value_type *buffer, size_type buffer_size, size_type position, size_type length=npos)
Definition u8string.h:316
void repair()
Fix the internal pointers after a low level memory copy.
Definition u8string.h:442
u8string_ext(value_type *buffer, size_type buffer_size)
Constructor.
Definition u8string.h:284
u8string_ext(const value_type *text, size_type count, value_type *buffer, size_type buffer_size)
Definition u8string.h:347
u8string_ext(const etl::iu8string &other, value_type *buffer, size_type buffer_size)
Definition u8string.h:304
u8string_ext(size_type count, value_type c, value_type *buffer, size_type buffer_size)
Definition u8string.h:358
u8string_ext(const etl::u8string_view &view, value_type *buffer, size_type buffer_size)
Definition u8string.h:393
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:253
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:316
enable_if
Definition type_traits_generator.h:1191
is_integral
Definition type_traits_generator.h:1001
bitset_ext
Definition absolute.h:38
etl::string< Array_Size - 1U > make_string(const char(&text)[Array_Size])
Hash function.
Definition string.h:493
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:502
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