Embedded Template Library 1.0
Loading...
Searching...
No Matches
u32string.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_U32STRING_INCLUDED
32#define ETL_U32STRING_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::u32string_view operator ""_sv(const char32_t* str, size_t length) noexcept
50 {
51 return etl::u32string_view{ str, length };
52 }
53 }
54 }
55#endif
56
57 typedef ibasic_string<char32_t> iu32string;
58
59 //***************************************************************************
63 //***************************************************************************
64 template <size_t MAX_SIZE_>
65 class u32string : public iu32string
66 {
67 public:
68
69 typedef iu32string base_type;
71
72 typedef iu32string::value_type value_type;
73
74 static const size_t MAX_SIZE = MAX_SIZE_;
75
76 //*************************************************************************
78 //*************************************************************************
80 : iu32string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
81 {
82 this->initialise();
83 }
84
85 //*************************************************************************
88 //*************************************************************************
90 : iu32string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
91 {
92 this->assign(other);
93 }
94
95 //*************************************************************************
98 //*************************************************************************
100 : iu32string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
101 {
102 this->assign(other);
103 }
104
105 //*************************************************************************
110 //*************************************************************************
112 : iu32string(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 u32string(const value_type* text)
124 : iu32string(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 u32string(const value_type* text, size_type count)
135 : iu32string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
136 {
137 this->assign(text, text + count);
138 }
139
140 //*************************************************************************
144 //*************************************************************************
145 u32string(size_type count, value_type c)
146 : iu32string(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 : iu32string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
161 {
162 this->assign(first, last);
163 }
164
165#if ETL_HAS_INITIALIZER_LIST
166 //*************************************************************************
168 //*************************************************************************
169 u32string(std::initializer_list<value_type> init)
170 : iu32string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
171 {
172 this->assign(init.begin(), init.end());
173 }
174#endif
175
176 //*************************************************************************
179 //*************************************************************************
181 : iu32string(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 u32string& operator = (const value_type* text)
224 {
225 this->assign(text);
226
227 return *this;
228 }
229
230 //*************************************************************************
232 //*************************************************************************
234 {
235 this->assign(view);
236
237 return *this;
238 }
239
240 //*************************************************************************
242 //*************************************************************************
243#if ETL_HAS_ISTRING_REPAIR
244 virtual void repair() ETL_OVERRIDE
245#else
246 void repair()
247#endif
248 {
250 }
251
252 private:
253
254 value_type buffer[MAX_SIZE + 1];
255 };
256
257 //***************************************************************************
260 //***************************************************************************
262 {
263 public:
264
265 typedef iu32string base_type;
267
268 typedef iu32string::value_type value_type;
269
270 //*************************************************************************
272 //*************************************************************************
273 u32string_ext(value_type* buffer, size_type buffer_size)
274 : iu32string(buffer, buffer_size - 1U)
275 {
276 this->initialise();
277 }
278
279 //*************************************************************************
282 //*************************************************************************
283 u32string_ext(const etl::u32string_ext& other, value_type* buffer, size_type buffer_size)
284 : iu32string(buffer, buffer_size - 1U)
285 {
286 this->assign(other);
287 }
288
289 //*************************************************************************
292 //*************************************************************************
293 u32string_ext(const etl::iu32string& other, value_type* buffer, size_type buffer_size)
294 : iu32string(buffer, buffer_size - 1U)
295 {
296 this->assign(other);
297 }
298
299 //*************************************************************************
304 //*************************************************************************
305 u32string_ext(const etl::iu32string& other, value_type* buffer, size_type buffer_size, size_type position, size_type length = npos)
306 : iu32string(buffer, buffer_size - 1U)
307 {
308 ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
309
310 this->assign(other, position, length);
311 }
312
313 //*************************************************************************
316 //*************************************************************************
317 ETL_EXPLICIT_STRING_FROM_CHAR u32string_ext(const value_type* text, value_type* buffer, size_type buffer_size)
318 : iu32string(buffer, buffer_size - 1U)
319 {
320 // Is the initial text at the same address as the buffer?
321 if (text == buffer)
322 {
323 this->current_size = etl::strlen(buffer);;
324 }
325 else
326 {
327 this->assign(text, text + etl::strlen(text));
328 }
329 }
330
331 //*************************************************************************
335 //*************************************************************************
336 u32string_ext(const value_type* text, size_type count, value_type* buffer, size_type buffer_size)
337 : iu32string(buffer, buffer_size - 1U)
338 {
339 this->assign(text, text + count);
340 }
341
342 //*************************************************************************
346 //*************************************************************************
347 u32string_ext(size_type count, value_type c, value_type* buffer, size_type buffer_size)
348 : iu32string(buffer, buffer_size - 1U)
349 {
350 this->initialise();
351 this->resize(count, c);
352 }
353
354 //*************************************************************************
359 //*************************************************************************
360 template <typename TIterator>
361 u32string_ext(TIterator first, TIterator last, value_type* buffer, size_type buffer_size, typename etl::enable_if<!etl::is_integral<TIterator>::value, int>::type = 0)
362 : iu32string(buffer, buffer_size - 1U)
363 {
364 this->assign(first, last);
365 }
366
367#if ETL_HAS_INITIALIZER_LIST
368 //*************************************************************************
370 //*************************************************************************
371 u32string_ext(std::initializer_list<value_type> init, value_type* buffer, size_type buffer_size)
372 : iu32string(buffer, buffer_size - 1U)
373 {
374 this->assign(init.begin(), init.end());
375 }
376#endif
377
378 //*************************************************************************
381 //*************************************************************************
382 explicit u32string_ext(const etl::u32string_view& view, value_type* buffer, size_type buffer_size)
383 : iu32string(buffer, buffer_size - 1U)
384 {
385 this->assign(view.begin(), view.end());
386 }
387
388 //*************************************************************************
390 //*************************************************************************
392 {
393 if (&rhs != this)
394 {
395 this->assign(rhs);
396 }
397
398 return *this;
399 }
400
401 //*************************************************************************
403 //*************************************************************************
405 {
406 if (&rhs != this)
407 {
408 this->assign(rhs);
409 }
410
411 return *this;
412 }
413
414 //*************************************************************************
416 //*************************************************************************
417 u32string_ext& operator = (const value_type* text)
418 {
419 this->assign(text);
420
421 return *this;
422 }
423
424 //*************************************************************************
426 //*************************************************************************
428 {
429 this->assign(view);
430
431 return *this;
432 }
433
434 //*************************************************************************
436 //*************************************************************************
437#if ETL_HAS_ISTRING_REPAIR
438 virtual void repair() ETL_OVERRIDE
439#else
440 void repair()
441#endif
442 {
443 }
444
445 private:
446
447 //*************************************************************************
449 //*************************************************************************
450 u32string_ext(const u32string_ext& other) ETL_DELETE;
451 };
452
453 //*************************************************************************
455 //*************************************************************************
456#if ETL_USING_8BIT_TYPES
457 template <>
458 struct hash<etl::iu32string>
459 {
460 size_t operator()(const etl::iu32string& text) const
461 {
462 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
463 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
464 }
465 };
466
467 template <size_t SIZE>
468 struct hash<etl::u32string<SIZE> >
469 {
470 size_t operator()(const etl::u32string<SIZE>& text) const
471 {
472 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
473 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
474 }
475 };
476
477 template <>
478 struct hash<etl::u32string_ext >
479 {
480 size_t operator()(const etl::u32string_ext& text) const
481 {
482 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
483 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
484 }
485 };
486#endif
487
488 //***************************************************************************
490 //***************************************************************************
491 template<size_t Array_Size>
492 etl::u32string<Array_Size - 1U> make_string(const char32_t(&text)[Array_Size])
493 {
494 return etl::u32string<Array_Size - 1U>(text, etl::strlen(text, Array_Size - 1U));
495 }
496
497 //***************************************************************************
499 //***************************************************************************
500 template<size_t MAX_SIZE, size_t SIZE>
502 {
503 return etl::u32string<MAX_SIZE>(text, etl::strlen(text, SIZE));
504 }
505}
506
507#include "private/minmax_pop.h"
508
509#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 u32string.h:262
u32string_ext(const value_type *text, size_type count, value_type *buffer, size_type buffer_size)
Definition u32string.h:336
u32string_ext & operator=(const u32string_ext &rhs)
Assignment operator.
Definition u32string.h:391
ETL_EXPLICIT_STRING_FROM_CHAR u32string_ext(const value_type *text, value_type *buffer, size_type buffer_size)
Definition u32string.h:317
u32string_ext(const etl::u32string_ext &other, value_type *buffer, size_type buffer_size)
Definition u32string.h:283
u32string_ext(size_type count, value_type c, value_type *buffer, size_type buffer_size)
Definition u32string.h:347
void repair()
Fix the internal pointers after a low level memory copy.
Definition u32string.h:440
u32string_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 u32string.h:361
u32string_ext(const etl::u32string_view &view, value_type *buffer, size_type buffer_size)
Definition u32string.h:382
u32string_ext(const etl::iu32string &other, value_type *buffer, size_type buffer_size, size_type position, size_type length=npos)
Definition u32string.h:305
u32string_ext(const etl::iu32string &other, value_type *buffer, size_type buffer_size)
Definition u32string.h:293
u32string_ext(value_type *buffer, size_type buffer_size)
Constructor.
Definition u32string.h:273
Definition u32string.h:66
u32string(TIterator first, TIterator last, typename etl::enable_if<!etl::is_integral< TIterator >::value, int >::type=0)
Definition u32string.h:159
ETL_EXPLICIT_STRING_FROM_CHAR u32string(const value_type *text)
Definition u32string.h:123
u32string()
Constructor.
Definition u32string.h:79
u32string(const etl::u32string_view &view)
Definition u32string.h:180
u32string(const etl::iu32string &other, size_type position, size_type length=npos)
Definition u32string.h:111
u32string(const etl::u32string< MAX_SIZE_ > &other)
Definition u32string.h:89
etl::u32string< MAX_SIZE_ > substr(size_type position=0, size_type length_=npos) const
Definition u32string.h:191
void repair()
Fix the internal pointers after a low level memory copy.
Definition u32string.h:246
u32string(const etl::iu32string &other)
Definition u32string.h:99
u32string(size_type count, value_type c)
Definition u32string.h:145
u32string & operator=(const u32string &rhs)
Assignment operator.
Definition u32string.h:210
u32string(const value_type *text, size_type count)
Definition u32string.h:134
#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