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 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->assign(other);
93 }
94
95 //*************************************************************************
98 //*************************************************************************
100 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
101 {
102 this->assign(other);
103 }
104
105 //*************************************************************************
110 //*************************************************************************
112 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
113 {
114 ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
115
116 this->assign(other, position, length);
117 }
118
119 //*************************************************************************
122 //*************************************************************************
123 ETL_EXPLICIT_STRING_FROM_CHAR u16string(const value_type* text)
124 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
125 {
126 this->assign(text, text + etl::char_traits<value_type>::length(text));
127 }
128
129 //*************************************************************************
133 //*************************************************************************
134 u16string(const value_type* text, size_type count)
135 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
136 {
137 this->assign(text, text + count);
138 }
139
140 //*************************************************************************
144 //*************************************************************************
145 u16string(size_type count, value_type c)
146 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
147 {
148 this->initialise();
149 this->resize(count, c);
150 }
151
152 //*************************************************************************
157 //*************************************************************************
158 template <typename TIterator>
160 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
161 {
162 this->assign(first, last);
163 }
164
165#if ETL_HAS_INITIALIZER_LIST
166 //*************************************************************************
168 //*************************************************************************
169 u16string(std::initializer_list<value_type> init)
170 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
171 {
172 this->assign(init.begin(), init.end());
173 }
174#endif
175
176 //*************************************************************************
179 //*************************************************************************
181 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
182 {
183 this->assign(view.begin(), view.end());
184 }
185
186 //*************************************************************************
190 //*************************************************************************
192 {
194
195 if (position != size())
196 {
197 ETL_ASSERT(position < size(), ETL_ERROR(string_out_of_bounds));
198
199 length_ = etl::min(length_, size() - position);
200
201 new_string.assign(buffer + position, buffer + position + length_);
202 }
203
204 return new_string;
205 }
206
207 //*************************************************************************
209 //*************************************************************************
211 {
212 if (&rhs != this)
213 {
214 this->assign(rhs);
215 }
216
217 return *this;
218 }
219
220 //*************************************************************************
222 //*************************************************************************
223 u16string& operator = (const value_type* text)
224 {
225 this->assign(text);
226
227 return *this;
228 }
229
230 //*************************************************************************
232 //*************************************************************************
233#if ETL_HAS_ISTRING_REPAIR
234 virtual void repair() ETL_OVERRIDE
235#else
236 void repair()
237#endif
238 {
240 }
241
242 private:
243
244 value_type buffer[MAX_SIZE + 1];
245 };
246
247 //***************************************************************************
250 //***************************************************************************
252 {
253 public:
254
255 typedef iu16string base_type;
257
258 typedef iu16string::value_type value_type;
259
260 //*************************************************************************
262 //*************************************************************************
263 u16string_ext(value_type* buffer, size_type buffer_size)
264 : iu16string(buffer, buffer_size - 1U)
265 {
266 this->initialise();
267 }
268
269 //*************************************************************************
272 //*************************************************************************
273 u16string_ext(const etl::u16string_ext& other, value_type* buffer, size_type buffer_size)
274 : iu16string(buffer, buffer_size - 1U)
275 {
276 this->assign(other);
277 }
278
279 //*************************************************************************
282 //*************************************************************************
283 u16string_ext(const etl::iu16string& other, value_type* buffer, size_type buffer_size)
284 : iu16string(buffer, buffer_size - 1U)
285 {
286 this->assign(other);
287 }
288
289 //*************************************************************************
294 //*************************************************************************
295 u16string_ext(const etl::iu16string& other, value_type* buffer, size_type buffer_size, size_type position, size_type length = npos)
296 : iu16string(buffer, buffer_size - 1U)
297 {
298 ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
299
300 this->assign(other, position, length);
301 }
302
303 //*************************************************************************
306 //*************************************************************************
307 u16string_ext(const value_type* text, value_type* buffer, size_type buffer_size)
308 : iu16string(buffer, buffer_size - 1U)
309 {
310 // Is the initial text at the same address as the buffer?
311 if (text == buffer)
312 {
313 this->current_size = etl::strlen(buffer);
314 }
315 else
316 {
317 this->assign(text, text + etl::strlen(text));
318 }
319 }
320
321 //*************************************************************************
325 //*************************************************************************
326 u16string_ext(const value_type* text, size_type count, value_type* buffer, size_type buffer_size)
327 : iu16string(buffer, buffer_size - 1U)
328 {
329 this->assign(text, text + count);
330 }
331
332 //*************************************************************************
336 //*************************************************************************
337 u16string_ext(size_type count, value_type c, value_type* buffer, size_type buffer_size)
338 : iu16string(buffer, buffer_size - 1U)
339 {
340 this->initialise();
341 this->resize(count, c);
342 }
343
344 //*************************************************************************
349 //*************************************************************************
350 template <typename TIterator>
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)
352 : iu16string(buffer, buffer_size - 1U)
353 {
354 this->assign(first, last);
355 }
356
357#if ETL_HAS_INITIALIZER_LIST
358 //*************************************************************************
360 //*************************************************************************
361 u16string_ext(std::initializer_list<value_type> init, value_type* buffer, size_type buffer_size)
362 : iu16string(buffer, buffer_size - 1U)
363 {
364 this->assign(init.begin(), init.end());
365 }
366#endif
367
368 //*************************************************************************
371 //*************************************************************************
372 explicit u16string_ext(const etl::u16string_view& view, value_type* buffer, size_type buffer_size)
373 : iu16string(buffer, buffer_size - 1U)
374 {
375 this->assign(view.begin(), view.end());
376 }
377
378 //*************************************************************************
380 //*************************************************************************
382 {
383 if (&rhs != this)
384 {
385 this->assign(rhs);
386 }
387
388 return *this;
389 }
390
391
392 //*************************************************************************
394 //*************************************************************************
396 {
397 if (&rhs != this)
398 {
399 this->assign(rhs);
400 }
401
402 return *this;
403 }
404
405 //*************************************************************************
407 //*************************************************************************
408 u16string_ext& operator = (const value_type* text)
409 {
410 this->assign(text);
411
412 return *this;
413 }
414
415 //*************************************************************************
417 //*************************************************************************
418#if ETL_HAS_ISTRING_REPAIR
419 virtual void repair() ETL_OVERRIDE
420#else
421 void repair()
422#endif
423 {
424 }
425
426 private:
427
428 //*************************************************************************
430 //*************************************************************************
431 u16string_ext(const u16string_ext& other) ETL_DELETE;
432 };
433
434 //*************************************************************************
436 //*************************************************************************
437#if ETL_USING_8BIT_TYPES
438 template <>
439 struct hash<etl::iu16string>
440 {
441 size_t operator()(const etl::iu16string& text) const
442 {
443 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
444 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
445 }
446 };
447
448 template <size_t SIZE>
449 struct hash<etl::u16string<SIZE> >
450 {
451 size_t operator()(const etl::u16string<SIZE>& text) const
452 {
453 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
454 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
455 }
456 };
457
458 template <>
459 struct hash<etl::u16string_ext >
460 {
461 size_t operator()(const etl::u16string_ext& text) const
462 {
463 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
464 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
465 }
466 };
467#endif
468
469 //***************************************************************************
471 //***************************************************************************
472 template<size_t Array_Size>
473 etl::u16string<Array_Size - 1U> make_string(const char16_t(&text)[Array_Size])
474 {
475 return etl::u16string<Array_Size - 1U>(text, etl::strlen(text, Array_Size - 1U));
476 }
477
478 //***************************************************************************
480 //***************************************************************************
481 template<size_t MAX_SIZE, size_t SIZE>
483 {
484 return etl::u16string<MAX_SIZE>(text, etl::strlen(text, SIZE));
485 }
486}
487
488#include "private/minmax_pop.h"
489
490#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 u16string.h:252
u16string_ext & operator=(const u16string_ext &rhs)
Assignment operator.
Definition u16string.h:381
u16string_ext(size_type count, value_type c, value_type *buffer, size_type buffer_size)
Definition u16string.h:337
u16string_ext(value_type *buffer, size_type buffer_size)
Constructor.
Definition u16string.h:263
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:351
u16string_ext(const etl::u16string_view &view, value_type *buffer, size_type buffer_size)
Definition u16string.h:372
u16string_ext(const value_type *text, size_type count, value_type *buffer, size_type buffer_size)
Definition u16string.h:326
u16string_ext(const etl::iu16string &other, value_type *buffer, size_type buffer_size, size_type position, size_type length=npos)
Definition u16string.h:295
u16string_ext(const etl::iu16string &other, value_type *buffer, size_type buffer_size)
Definition u16string.h:283
void repair()
Fix the internal pointers after a low level memory copy.
Definition u16string.h:421
u16string_ext(const value_type *text, value_type *buffer, size_type buffer_size)
Definition u16string.h:307
u16string_ext(const etl::u16string_ext &other, value_type *buffer, size_type buffer_size)
Definition u16string.h:273
Definition u16string.h:66
u16string(const etl::iu16string &other)
Definition u16string.h:99
u16string(const etl::iu16string &other, size_type position, size_type length=npos)
Definition u16string.h:111
etl::u16string< MAX_SIZE_ > substr(size_type position=0, size_type length_=npos) const
Definition u16string.h:191
u16string(size_type count, value_type c)
Definition u16string.h:145
u16string(TIterator first, TIterator last, typename etl::enable_if<!etl::is_integral< TIterator >::value, int >::type=0)
Definition u16string.h:159
ETL_EXPLICIT_STRING_FROM_CHAR u16string(const value_type *text)
Definition u16string.h:123
u16string & operator=(const u16string &rhs)
Assignment operator.
Definition u16string.h:210
u16string(const etl::u16string_view &view)
Definition u16string.h:180
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:134
void repair()
Fix the internal pointers after a low level memory copy.
Definition u16string.h:236
#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