Embedded Template Library 1.0
Loading...
Searching...
No Matches
wstring.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_WSTRING_INCLUDED
32#define ETL_WSTRING_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::wstring_view operator ""_sv(const wchar_t* str, size_t length) ETL_NOEXCEPT
50 {
51 return etl::wstring_view{ str, length };
52 }
53 }
54 }
55#endif
56
57 typedef ibasic_string<wchar_t> iwstring;
58
59 //***************************************************************************
63 //***************************************************************************
64 template <size_t MAX_SIZE_>
65 class wstring : public iwstring
66 {
67 public:
68
69 typedef iwstring base_type;
70 typedef iwstring interface_type;
71
72 typedef iwstring::value_type value_type;
73
74 static const size_t MAX_SIZE = MAX_SIZE_;
75
76 //*************************************************************************
78 //*************************************************************************
80 : iwstring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
81 {
82 this->initialise();
83 }
84
85 //*************************************************************************
88 //*************************************************************************
90 : iwstring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
91 {
92 this->initialise();
93 this->assign(other);
94 }
95
96 //*************************************************************************
99 //*************************************************************************
100 wstring(const etl::iwstring& other)
101 : iwstring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
102 {
103 this->initialise();
104 this->assign(other);
105 }
106
107 //*************************************************************************
112 //*************************************************************************
113 wstring(const etl::iwstring& other, size_type position, size_type length = npos)
114 : iwstring(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 wstring(const value_type* text)
127 : iwstring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
128 {
129 this->initialise();
130 this->assign(text);
131 }
132
133 //*************************************************************************
137 //*************************************************************************
138 wstring(const value_type* text, size_type count)
139 : iwstring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
140 {
141 this->initialise();
142 this->assign(text, text + count);
143 }
144
145 //*************************************************************************
149 //*************************************************************************
150 wstring(size_type count, value_type c)
151 : iwstring(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 wstring(TIterator first, TIterator last, typename etl::enable_if<!etl::is_integral<TIterator>::value, int>::type = 0)
165 : iwstring(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 wstring(std::initializer_list<value_type> init)
176 : iwstring(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 wstring(const etl::wstring_view& view)
188 : iwstring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
189 {
190 this->initialise();
191 this->assign(view.begin(), view.end());
192 }
193
194 //*************************************************************************
198 //*************************************************************************
199 etl::wstring<MAX_SIZE_> substr(size_type position = 0, size_type length_ = npos) const
200 {
201 etl::wstring<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 wstring& operator = (const value_type* text)
232 {
233 this->assign(text);
234
235 return *this;
236 }
237
238 //*************************************************************************
240 //*************************************************************************
241 wstring& operator = (const etl::wstring_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 wstring_ext : public iwstring
270 {
271 public:
272
273 typedef iwstring base_type;
274 typedef iwstring interface_type;
275
276 typedef iwstring::value_type value_type;
277 typedef iwstring::size_type size_type;
278
279 //*************************************************************************
281 //*************************************************************************
282 wstring_ext(value_type* buffer, size_type buffer_size)
283 : iwstring(buffer, buffer_size - 1U)
284 {
285 this->initialise();
286 }
287
288 //*************************************************************************
291 //*************************************************************************
292 wstring_ext(const etl::wstring_ext& other, value_type* buffer, size_type buffer_size)
293 : iwstring(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 wstring_ext(const etl::iwstring& other, value_type* buffer, size_type buffer_size)
311 : iwstring(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 wstring_ext(const etl::iwstring& other, value_type* buffer, size_type buffer_size, size_type position, size_type length = npos)
331 : iwstring(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 wstring_ext(TPointer text, value_type* buffer, size_type buffer_size,
353 : iwstring(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 wstring_ext(const value_type (&literal)[Size], value_type* buffer, size_type buffer_size)
372 : iwstring(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 wstring_ext(const value_type* text, size_type count, value_type* buffer, size_type buffer_size)
391 : iwstring(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 wstring_ext(size_type count, value_type c, value_type* buffer, size_type buffer_size)
410 : iwstring(buffer, buffer_size - 1U)
411 {
412 this->initialise();
413 this->resize(count, c);
414 }
415
416 //*************************************************************************
419 //*************************************************************************
420 explicit wstring_ext(const etl::wstring_view& view, value_type* buffer, size_type buffer_size)
421 : iwstring(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 wstring_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 : iwstring(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 wstring_ext(std::initializer_list<value_type> init, value_type* buffer, size_type buffer_size)
460 : iwstring(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 wstring_ext& operator = (const iwstring& rhs)
484 {
485 if (&rhs != this)
486 {
487 this->assign(rhs);
488 }
489
490 return *this;
491 }
492
493 //*************************************************************************
495 //*************************************************************************
496 wstring_ext& operator = (const value_type* text)
497 {
498 this->assign(text);
499
500 return *this;
501 }
502
503 //*************************************************************************
505 //*************************************************************************
506 wstring_ext& operator = (const etl::wstring_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 wstring_ext(const wstring_ext& other) ETL_DELETE;
530 };
531
532 //*************************************************************************
534 //*************************************************************************
535#if ETL_USING_8BIT_TYPES
536 template <>
537 struct hash<etl::iwstring>
538 {
539 size_t operator()(const etl::iwstring& 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::wstring<SIZE> >
548 {
549 size_t operator()(const etl::wstring<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::wstring_ext>
558 {
559 size_t operator()(const etl::wstring_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::wstring<Array_Size - 1U> make_string(const wchar_t(&text)[Array_Size])
572 {
573 return etl::wstring<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::wstring<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< wchar_t > &other)
Definition basic_string.h:683
pointer data()
Definition basic_string.h:646
void initialise()
Definition basic_string.h:2491
void repair_buffer(wchar_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 wstring.h:270
wstring_ext(const etl::iwstring &other, value_type *buffer, size_type buffer_size)
Definition wstring.h:310
wstring_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 wstring.h:351
wstring_ext(size_type count, value_type c, value_type *buffer, size_type buffer_size)
Definition wstring.h:409
wstring_ext(value_type *buffer, size_type buffer_size)
Constructor.
Definition wstring.h:282
wstring_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 wstring.h:441
wstring_ext & operator=(const wstring_ext &rhs)
Assignment operator.
Definition wstring.h:470
wstring_ext(const value_type(&literal)[Size], value_type *buffer, size_type buffer_size)
Definition wstring.h:371
wstring_ext(const etl::wstring_view &view, value_type *buffer, size_type buffer_size)
Definition wstring.h:420
wstring_ext(const etl::iwstring &other, value_type *buffer, size_type buffer_size, size_type position, size_type length=npos)
Definition wstring.h:330
void repair()
Fix the internal pointers after a low level memory copy.
Definition wstring.h:519
wstring_ext(const etl::wstring_ext &other, value_type *buffer, size_type buffer_size)
Definition wstring.h:292
wstring_ext(const value_type *text, size_type count, value_type *buffer, size_type buffer_size)
Definition wstring.h:390
Definition wstring.h:66
wstring(const value_type *text, size_type count)
Definition wstring.h:138
wstring(const etl::iwstring &other)
Definition wstring.h:100
wstring(const etl::iwstring &other, size_type position, size_type length=npos)
Definition wstring.h:113
wstring(const etl::wstring_view &view)
Definition wstring.h:187
ETL_EXPLICIT_STRING_FROM_CHAR wstring(const value_type *text)
Definition wstring.h:126
wstring & operator=(const wstring &rhs)
Assignment operator.
Definition wstring.h:218
void repair()
Fix the internal pointers after a low level memory copy.
Definition wstring.h:254
wstring(TIterator first, TIterator last, typename etl::enable_if<!etl::is_integral< TIterator >::value, int >::type=0)
Definition wstring.h:164
wstring()
Constructor.
Definition wstring.h:79
wstring(size_type count, value_type c)
Definition wstring.h:150
wstring(const etl::wstring< MAX_SIZE_ > &other)
Definition wstring.h:89
etl::wstring< MAX_SIZE_ > substr(size_type position=0, size_type length_=npos) const
Definition wstring.h:199
#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