Embedded Template Library 1.0
Loading...
Searching...
No Matches
u16string.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_U16STRING_INCLUDED
32#define ETL_U16STRING_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 "private/minmax_push.h"
41
42namespace etl
43{
44#if ETL_USING_CPP11
45 inline namespace literals
46 {
47 inline namespace string_literals
48 {
49 inline constexpr etl::u16string_view operator ""_sv(const char16_t* str, size_t length) ETL_NOEXCEPT
50 {
51 return etl::u16string_view{ str, length };
52 }
53 }
54 }
55#endif
56
57 typedef ibasic_string<char16_t> iu16string;
58
59 //***************************************************************************
63 //***************************************************************************
64 template <size_t MAX_SIZE_>
65 class u16string : public iu16string
66 {
67 public:
68
69 typedef iu16string base_type;
70 typedef iu16string interface_type;
71
72 typedef iu16string::value_type value_type;
73
74 static const size_t MAX_SIZE = MAX_SIZE_;
75
76 //*************************************************************************
78 //*************************************************************************
80 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
81 {
82 this->initialise();
83 }
84
85 //*************************************************************************
88 //*************************************************************************
90 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
91 {
92 this->initialise();
93 this->assign(other);
94 }
95
96 //*************************************************************************
99 //*************************************************************************
100 u16string(const etl::iu16string& other)
101 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
102 {
103 this->initialise();
104 this->assign(other);
105 }
106
107 //*************************************************************************
112 //*************************************************************************
113 u16string(const etl::iu16string& other, size_type position, size_type length = npos)
114 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
115 {
116 ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
117
118 this->initialise();
119 this->assign(other, position, length);
120 }
121
122 //*************************************************************************
125 //*************************************************************************
126 ETL_EXPLICIT_STRING_FROM_CHAR u16string(const value_type* text)
127 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
128 {
129 this->initialise();
130 this->assign(text);
131 }
132
133 //*************************************************************************
137 //*************************************************************************
138 u16string(const value_type* text, size_type count)
139 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
140 {
141 this->initialise();
142 this->assign(text, text + count);
143 }
144
145 //*************************************************************************
149 //*************************************************************************
150 u16string(size_type count, value_type c)
151 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
152 {
153 this->initialise();
154 this->resize(count, c);
155 }
156
157 //*************************************************************************
162 //*************************************************************************
163 template <typename TIterator>
164 u16string(TIterator first, TIterator last, typename etl::enable_if<!etl::is_integral<TIterator>::value, int>::type = 0)
165 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
166 {
167 this->initialise();
168 this->assign(first, last);
169 }
170
171#if ETL_HAS_INITIALIZER_LIST
172 //*************************************************************************
174 //*************************************************************************
175 u16string(std::initializer_list<value_type> init)
176 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
177 {
178 this->initialise();
179 this->assign(init.begin(), init.end());
180 }
181#endif
182
183 //*************************************************************************
186 //*************************************************************************
187 explicit u16string(const etl::u16string_view& view)
188 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
189 {
190 this->initialise();
191 this->assign(view.begin(), view.end());
192 }
193
194 //*************************************************************************
198 //*************************************************************************
199 etl::u16string<MAX_SIZE_> substr(size_type position = 0, size_type length_ = npos) const
200 {
201 etl::u16string<MAX_SIZE_> new_string;
202
203 if (position != size())
204 {
205 ETL_ASSERT(position < size(), ETL_ERROR(string_out_of_bounds));
206
207 length_ = etl::min(length_, size() - position);
208
209 new_string.assign(buffer + position, buffer + position + length_);
210 }
211
212 return new_string;
213 }
214
215 //*************************************************************************
217 //*************************************************************************
219 {
220 if (&rhs != this)
221 {
222 this->assign(rhs);
223 }
224
225 return *this;
226 }
227
228 //*************************************************************************
230 //*************************************************************************
231 u16string& operator = (const value_type* text)
232 {
233 this->assign(text);
234
235 return *this;
236 }
237
238 //*************************************************************************
240 //*************************************************************************
241 u16string& operator = (const etl::u16string_view& view)
242 {
243 this->assign(view);
244
245 return *this;
246 }
247
248 //*************************************************************************
250 //*************************************************************************
251#if ETL_HAS_ISTRING_REPAIR
252 virtual void repair() ETL_OVERRIDE
253#else
254 void repair()
255#endif
256 {
258 }
259
260 private:
261
262 value_type buffer[MAX_SIZE + 1];
263 };
264
265 //***************************************************************************
268 //***************************************************************************
269 class u16string_ext : public iu16string
270 {
271 public:
272
273 typedef iu16string base_type;
274 typedef iu16string interface_type;
275
276 typedef iu16string::value_type value_type;
277 typedef iu16string::size_type size_type;
278
279 //*************************************************************************
281 //*************************************************************************
282 u16string_ext(value_type* buffer, size_type buffer_size)
283 : iu16string(buffer, buffer_size - 1U)
284 {
285 this->initialise();
286 }
287
288 //*************************************************************************
291 //*************************************************************************
292 u16string_ext(const etl::u16string_ext& other, value_type* buffer, size_type buffer_size)
293 : iu16string(buffer, buffer_size - 1U)
294 {
295 if (this->is_within_buffer(other.data()))
296 {
297 this->current_size = other.size();
298 }
299 else
300 {
301 this->initialise();
302 this->assign(other);
303 }
304 }
305
306 //*************************************************************************
309 //*************************************************************************
310 u16string_ext(const etl::iu16string& other, value_type* buffer, size_type buffer_size)
311 : iu16string(buffer, buffer_size - 1U)
312 {
313 if (this->is_within_buffer(other.data()))
314 {
315 this->current_size = other.size();
316 }
317 else
318 {
319 this->initialise();
320 this->assign(other);
321 }
322 }
323
324 //*************************************************************************
329 //*************************************************************************
330 u16string_ext(const etl::iu16string& other, value_type* buffer, size_type buffer_size, size_type position, size_type length = npos)
331 : iu16string(buffer, buffer_size - 1U)
332 {
333 ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
334
335 if (this->is_within_buffer(other.data()))
336 {
337 this->current_size = other.size();
338 }
339 else
340 {
341 this->initialise();
342 this->assign(other, position, length);
343 }
344 }
345
346 //*************************************************************************
349 //*************************************************************************
350 template <typename TPointer>
351 u16string_ext(TPointer text, value_type* buffer, size_type buffer_size,
353 : iu16string(buffer, buffer_size - 1U)
354 {
355 if (this->is_within_buffer(text))
356 {
357 this->current_size = etl::strlen(buffer);
358 }
359 else
360 {
361 this->initialise();
362 this->assign(text, text + etl::strlen(text));
363 }
364 }
365
366 //*************************************************************************
369 //*************************************************************************
370 template <size_t Size>
371 u16string_ext(const value_type (&literal)[Size], value_type* buffer, size_type buffer_size)
372 : iu16string(buffer, buffer_size - 1U)
373 {
374 if (this->is_within_buffer(literal))
375 {
376 this->current_size = etl::strlen(literal);
377 }
378 else
379 {
380 this->initialise();
381 this->assign(literal);
382 }
383 }
384
385 //*************************************************************************
389 //*************************************************************************
390 u16string_ext(const value_type* text, size_type count, value_type* buffer, size_type buffer_size)
391 : iu16string(buffer, buffer_size - 1U)
392 {
393 if (this->is_within_buffer(text))
394 {
395 this->current_size = count;
396 }
397 else
398 {
399 this->initialise();
400 this->assign(text, text + count);
401 }
402 }
403
404 //*************************************************************************
408 //*************************************************************************
409 u16string_ext(size_type count, value_type c, value_type* buffer, size_type buffer_size)
410 : iu16string(buffer, buffer_size - 1U)
411 {
412 this->initialise();
413 this->resize(count, c);
414 }
415
416 //*************************************************************************
419 //*************************************************************************
420 explicit u16string_ext(const etl::u16string_view& view, value_type* buffer, size_type buffer_size)
421 : iu16string(buffer, buffer_size - 1U)
422 {
423 if (this->is_within_buffer(view.data()))
424 {
425 this->current_size = view.size();
426 }
427 else
428 {
429 this->initialise();
430 this->assign(view.begin(), view.end());
431 }
432 }
433
434 //*************************************************************************
439 //*************************************************************************
440 template <typename TIterator>
441 u16string_ext(TIterator first, TIterator last, value_type* buffer, size_type buffer_size, typename etl::enable_if<!etl::is_integral<TIterator>::value, int>::type = 0)
442 : iu16string(buffer, buffer_size - 1U)
443 {
444 if (this->is_within_buffer(reinterpret_cast<const_iterator>(etl::addressof(*first))))
445 {
446 this->current_size = etl::distance(first, last);
447 }
448 else
449 {
450 this->initialise();
451 this->assign(first, last);
452 }
453 }
454
455#if ETL_HAS_INITIALIZER_LIST
456 //*************************************************************************
458 //*************************************************************************
459 u16string_ext(std::initializer_list<value_type> init, value_type* buffer, size_type buffer_size)
460 : iu16string(buffer, buffer_size - 1U)
461 {
462 this->initialise();
463 this->assign(init.begin(), init.end());
464 }
465#endif
466
467 //*************************************************************************
469 //*************************************************************************
471 {
472 if (&rhs != this)
473 {
474 this->assign(rhs);
475 }
476
477 return *this;
478 }
479
480 //*************************************************************************
482 //*************************************************************************
483 u16string_ext& operator = (const iu16string& rhs)
484 {
485 if (&rhs != this)
486 {
487 this->assign(rhs);
488 }
489
490 return *this;
491 }
492
493 //*************************************************************************
495 //*************************************************************************
496 u16string_ext& operator = (const value_type* text)
497 {
498 this->assign(text);
499
500 return *this;
501 }
502
503 //*************************************************************************
505 //*************************************************************************
506 u16string_ext& operator = (const etl::u16string_view& view)
507 {
508 this->assign(view);
509
510 return *this;
511 }
512
513 //*************************************************************************
515 //*************************************************************************
516#if ETL_HAS_ISTRING_REPAIR
517 virtual void repair() ETL_OVERRIDE
518#else
519 void repair()
520#endif
521 {
522 }
523
524 private:
525
526 //*************************************************************************
528 //*************************************************************************
529 u16string_ext(const u16string_ext& other) ETL_DELETE;
530 };
531
532 //*************************************************************************
534 //*************************************************************************
535#if ETL_USING_8BIT_TYPES
536 template <>
537 struct hash<etl::iu16string>
538 {
539 size_t operator()(const etl::iu16string& text) const
540 {
541 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
542 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
543 }
544 };
545
546 template <size_t SIZE>
547 struct hash<etl::u16string<SIZE> >
548 {
549 size_t operator()(const etl::u16string<SIZE>& text) const
550 {
551 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
552 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
553 }
554 };
555
556 template <>
557 struct hash<etl::u16string_ext >
558 {
559 size_t operator()(const etl::u16string_ext& text) const
560 {
561 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
562 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
563 }
564 };
565#endif
566
567 //***************************************************************************
569 //***************************************************************************
570 template<size_t Array_Size>
571 etl::u16string<Array_Size - 1U> make_string(const char16_t(&text)[Array_Size])
572 {
573 return etl::u16string<Array_Size - 1U>(text, etl::strlen(text, Array_Size - 1U));
574 }
575
576 //***************************************************************************
578 //***************************************************************************
579 template<size_t MAX_SIZE, size_t SIZE>
581 {
582 return etl::u16string<MAX_SIZE>(text, etl::strlen(text, SIZE));
583 }
584}
585
586#include "private/minmax_pop.h"
587
588#endif
ETL_CONSTEXPR const_pointer data() const ETL_NOEXCEPT
Returns a const pointer to the first element of the internal storage.
Definition string_view.h:196
ETL_CONSTEXPR const_iterator end() const ETL_NOEXCEPT
Returns a const iterator to the end of the array.
Definition string_view.h:220
ETL_CONSTEXPR const_iterator begin() const ETL_NOEXCEPT
Returns a const iterator to the beginning of the array.
Definition string_view.h:204
Definition basic_string.h:351
bool is_within_buffer(const_pointer ptr) const
Definition basic_string.h:2718
void resize(size_type new_size)
Definition basic_string.h:481
void assign(const etl::ibasic_string< char16_t > &other)
Definition basic_string.h:683
pointer data()
Definition basic_string.h:646
void initialise()
Definition basic_string.h:2491
void repair_buffer(char16_t *p_buffer_)
Definition basic_string.h:2503
size_type length() const
Definition basic_string.h:202
size_type current_size
The current number of elements in the string.
Definition basic_string.h:336
size_type size() const
Definition basic_string.h:193
Definition basic_string.h:115
Definition u16string.h:270
u16string_ext & operator=(const u16string_ext &rhs)
Assignment operator.
Definition u16string.h:470
u16string_ext(size_type count, value_type c, value_type *buffer, size_type buffer_size)
Definition u16string.h:409
u16string_ext(value_type *buffer, size_type buffer_size)
Constructor.
Definition u16string.h:282
u16string_ext(TPointer text, value_type *buffer, size_type buffer_size, typename etl::enable_if< etl::is_same< const value_type *, TPointer >::value, int >::type *=ETL_NULLPTR)
Definition u16string.h:351
u16string_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 u16string.h:441
u16string_ext(const etl::u16string_view &view, value_type *buffer, size_type buffer_size)
Definition u16string.h:420
u16string_ext(const value_type(&literal)[Size], value_type *buffer, size_type buffer_size)
Definition u16string.h:371
u16string_ext(const value_type *text, size_type count, value_type *buffer, size_type buffer_size)
Definition u16string.h:390
u16string_ext(const etl::iu16string &other, value_type *buffer, size_type buffer_size, size_type position, size_type length=npos)
Definition u16string.h:330
u16string_ext(const etl::iu16string &other, value_type *buffer, size_type buffer_size)
Definition u16string.h:310
void repair()
Fix the internal pointers after a low level memory copy.
Definition u16string.h:519
u16string_ext(const etl::u16string_ext &other, value_type *buffer, size_type buffer_size)
Definition u16string.h:292
Definition u16string.h:66
u16string(const etl::iu16string &other)
Definition u16string.h:100
u16string(const etl::iu16string &other, size_type position, size_type length=npos)
Definition u16string.h:113
etl::u16string< MAX_SIZE_ > substr(size_type position=0, size_type length_=npos) const
Definition u16string.h:199
u16string(size_type count, value_type c)
Definition u16string.h:150
u16string(TIterator first, TIterator last, typename etl::enable_if<!etl::is_integral< TIterator >::value, int >::type=0)
Definition u16string.h:164
ETL_EXPLICIT_STRING_FROM_CHAR u16string(const value_type *text)
Definition u16string.h:126
u16string & operator=(const u16string &rhs)
Assignment operator.
Definition u16string.h:218
u16string(const etl::u16string_view &view)
Definition u16string.h:187
u16string(const etl::u16string< MAX_SIZE_ > &other)
Definition u16string.h:89
u16string()
Constructor.
Definition u16string.h:79
u16string(const value_type *text, size_type count)
Definition u16string.h:138
void repair()
Fix the internal pointers after a low level memory copy.
Definition u16string.h:254
#define ETL_ASSERT(b, e)
Definition error_handler.h:356
ETL_CONSTEXPR17 etl::enable_if<!etl::is_same< T, etl::nullptr_t >::value, T >::type * addressof(T &t)
Definition addressof.h:52
enable_if
Definition type_traits_generator.h:1254
is_same
Definition type_traits_generator.h:1104
bitset_ext
Definition absolute.h:39
etl::string< Array_Size - 1U > make_string(const char(&text)[Array_Size])
Hash function.
Definition string.h:591
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:600
ETL_CONSTEXPR14 size_t strlen(const T *t) ETL_NOEXCEPT
Alternative strlen for all character types.
Definition char_traits.h:287