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) 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;
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 //*************************************************************************
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>
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 //*************************************************************************
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 {
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 //*************************************************************************
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 //***************************************************************************
270 {
271 public:
272
273 typedef iu16string base_type;
275
276 typedef iu16string::value_type value_type;
277
278 //*************************************************************************
280 //*************************************************************************
281 u16string_ext(value_type* buffer, size_type buffer_size)
282 : iu16string(buffer, buffer_size - 1U)
283 {
284 this->initialise();
285 }
286
287 //*************************************************************************
290 //*************************************************************************
291 u16string_ext(const etl::u16string_ext& other, value_type* buffer, size_type buffer_size)
292 : iu16string(buffer, buffer_size - 1U)
293 {
294 this->initialise();
295 this->assign(other);
296 }
297
298 //*************************************************************************
301 //*************************************************************************
302 u16string_ext(const etl::iu16string& other, value_type* buffer, size_type buffer_size)
303 : iu16string(buffer, buffer_size - 1U)
304 {
305 this->initialise();
306 this->assign(other);
307 }
308
309 //*************************************************************************
314 //*************************************************************************
315 u16string_ext(const etl::iu16string& other, value_type* buffer, size_type buffer_size, size_type position, size_type length = npos)
316 : iu16string(buffer, buffer_size - 1U)
317 {
318 ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
319
320 this->initialise();
321 this->assign(other, position, length);
322 }
323
324 //*************************************************************************
327 //*************************************************************************
328 u16string_ext(const value_type* text, value_type* buffer, size_type buffer_size)
329 : iu16string(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->initialise();
339 this->assign(text, text + etl::strlen(text));
340 }
341 }
342
343 //*************************************************************************
347 //*************************************************************************
348 u16string_ext(const value_type* text, size_type count, value_type* buffer, size_type buffer_size)
349 : iu16string(buffer, buffer_size - 1U)
350 {
351 this->initialise();
352 this->assign(text, text + count);
353 }
354
355 //*************************************************************************
359 //*************************************************************************
360 u16string_ext(size_type count, value_type c, value_type* buffer, size_type buffer_size)
361 : iu16string(buffer, buffer_size - 1U)
362 {
363 this->initialise();
364 this->resize(count, c);
365 }
366
367 //*************************************************************************
372 //*************************************************************************
373 template <typename TIterator>
374 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)
375 : iu16string(buffer, buffer_size - 1U)
376 {
377 this->initialise();
378 this->assign(first, last);
379 }
380
381#if ETL_HAS_INITIALIZER_LIST
382 //*************************************************************************
384 //*************************************************************************
385 u16string_ext(std::initializer_list<value_type> init, value_type* buffer, size_type buffer_size)
386 : iu16string(buffer, buffer_size - 1U)
387 {
388 this->assign(init.begin(), init.end());
389 }
390#endif
391
392 //*************************************************************************
395 //*************************************************************************
396 explicit u16string_ext(const etl::u16string_view& view, value_type* buffer, size_type buffer_size)
397 : iu16string(buffer, buffer_size - 1U)
398 {
399 this->initialise();
400 this->assign(view.begin(), view.end());
401 }
402
403 //*************************************************************************
405 //*************************************************************************
407 {
408 if (&rhs != this)
409 {
410 this->assign(rhs);
411 }
412
413 return *this;
414 }
415
416 //*************************************************************************
418 //*************************************************************************
420 {
421 if (&rhs != this)
422 {
423 this->assign(rhs);
424 }
425
426 return *this;
427 }
428
429 //*************************************************************************
431 //*************************************************************************
432 u16string_ext& operator = (const value_type* text)
433 {
434 this->assign(text);
435
436 return *this;
437 }
438
439 //*************************************************************************
441 //*************************************************************************
443 {
444 this->assign(view);
445
446 return *this;
447 }
448
449 //*************************************************************************
451 //*************************************************************************
452#if ETL_HAS_ISTRING_REPAIR
453 virtual void repair() ETL_OVERRIDE
454#else
455 void repair()
456#endif
457 {
458 }
459
460 private:
461
462 //*************************************************************************
464 //*************************************************************************
465 u16string_ext(const u16string_ext& other) ETL_DELETE;
466 };
467
468 //*************************************************************************
470 //*************************************************************************
471#if ETL_USING_8BIT_TYPES
472 template <>
473 struct hash<etl::iu16string>
474 {
475 size_t operator()(const etl::iu16string& text) const
476 {
477 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
478 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
479 }
480 };
481
482 template <size_t SIZE>
483 struct hash<etl::u16string<SIZE> >
484 {
485 size_t operator()(const etl::u16string<SIZE>& text) const
486 {
487 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
488 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
489 }
490 };
491
492 template <>
493 struct hash<etl::u16string_ext >
494 {
495 size_t operator()(const etl::u16string_ext& text) const
496 {
497 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
498 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
499 }
500 };
501#endif
502
503 //***************************************************************************
505 //***************************************************************************
506 template<size_t Array_Size>
507 etl::u16string<Array_Size - 1U> make_string(const char16_t(&text)[Array_Size])
508 {
509 return etl::u16string<Array_Size - 1U>(text, etl::strlen(text, Array_Size - 1U));
510 }
511
512 //***************************************************************************
514 //***************************************************************************
515 template<size_t MAX_SIZE, size_t SIZE>
517 {
518 return etl::u16string<MAX_SIZE>(text, etl::strlen(text, SIZE));
519 }
520}
521
522#include "private/minmax_pop.h"
523
524#endif
String view.
Definition string_view.h:104
ETL_CONSTEXPR const_iterator begin() const
Returns a const iterator to the beginning of the array.
Definition string_view.h:204
Definition basic_string.h:351
void resize(size_type new_size)
Definition basic_string.h:481
void assign(const etl::ibasic_string< T > &other)
Definition basic_string.h:677
pointer data()
Definition basic_string.h:640
void initialise()
Initialise the string.
Definition basic_string.h:2548
void repair_buffer(T *p_buffer_)
Fix the internal pointers after a low level memory copy.
Definition basic_string.h:2560
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:406
u16string_ext(size_type count, value_type c, value_type *buffer, size_type buffer_size)
Definition u16string.h:360
u16string_ext(value_type *buffer, size_type buffer_size)
Constructor.
Definition u16string.h:281
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:374
u16string_ext(const etl::u16string_view &view, value_type *buffer, size_type buffer_size)
Definition u16string.h:396
u16string_ext(const value_type *text, size_type count, value_type *buffer, size_type buffer_size)
Definition u16string.h:348
u16string_ext(const etl::iu16string &other, value_type *buffer, size_type buffer_size, size_type position, size_type length=npos)
Definition u16string.h:315
u16string_ext(const etl::iu16string &other, value_type *buffer, size_type buffer_size)
Definition u16string.h:302
void repair()
Fix the internal pointers after a low level memory copy.
Definition u16string.h:455
u16string_ext(const value_type *text, value_type *buffer, size_type buffer_size)
Definition u16string.h:328
u16string_ext(const etl::u16string_ext &other, value_type *buffer, size_type buffer_size)
Definition u16string.h:291
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
enable_if
Definition type_traits_generator.h:1230
is_integral
Definition type_traits_generator.h:1040
bitset_ext
Definition absolute.h:38
etl::string< Array_Size - 1U > make_string(const char(&text)[Array_Size])
Hash function.
Definition string.h:526
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:535
ETL_CONSTEXPR14 size_t strlen(const T *t)
Alternative strlen for all character types.
Definition char_traits.h:287
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_even_unscaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:314