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) 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;
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 //*************************************************************************
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>
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 //*************************************************************************
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 {
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 //*************************************************************************
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
278 //*************************************************************************
280 //*************************************************************************
281 wstring_ext(value_type* buffer, size_type buffer_size)
282 : iwstring(buffer, buffer_size - 1U)
283 {
284 this->initialise();
285 }
286
287 //*************************************************************************
290 //*************************************************************************
291 wstring_ext(const etl::wstring_ext& other, value_type* buffer, size_type buffer_size)
292 : iwstring(buffer, buffer_size - 1U)
293 {
294 this->initialise();
295 this->assign(other);
296 }
297
298 //*************************************************************************
301 //*************************************************************************
302 wstring_ext(const etl::iwstring& other, value_type* buffer, size_type buffer_size)
303 : iwstring(buffer, buffer_size - 1U)
304 {
305 this->initialise();
306 this->assign(other);
307 }
308
309 //*************************************************************************
314 //*************************************************************************
315 wstring_ext(const etl::iwstring& other, value_type* buffer, size_type buffer_size, size_type position, size_type length = npos)
316 : iwstring(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 wstring_ext(const value_type* text, value_type* buffer, size_type buffer_size)
329 : iwstring(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 wstring_ext(const value_type* text, size_type count, value_type* buffer, size_type buffer_size)
349 : iwstring(buffer, buffer_size - 1U)
350 {
351 this->initialise();
352 this->assign(text, text + count);
353 }
354
355 //*************************************************************************
359 //*************************************************************************
360 wstring_ext(size_type count, value_type c, value_type* buffer, size_type buffer_size)
361 : iwstring(buffer, buffer_size - 1U)
362 {
363 this->initialise();
364 this->resize(count, c);
365 }
366
367 //*************************************************************************
372 //*************************************************************************
373 template <typename TIterator>
374 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)
375 : iwstring(buffer, buffer_size - 1U)
376 {
377 this->initialise();
378 this->assign(first, last);
379 }
380
381#if ETL_HAS_INITIALIZER_LIST
382 //*************************************************************************
384 //*************************************************************************
385 wstring_ext(std::initializer_list<value_type> init, value_type* buffer, size_type buffer_size)
386 : iwstring(buffer, buffer_size - 1U)
387 {
388 this->initialise();
389 this->assign(init.begin(), init.end());
390 }
391#endif
392
393 //*************************************************************************
396 //*************************************************************************
397 explicit wstring_ext(const etl::wstring_view& view, value_type* buffer, size_type buffer_size)
398 : iwstring(buffer, buffer_size - 1U)
399 {
400 this->initialise();
401 this->assign(view.begin(), view.end());
402 }
403
404 //*************************************************************************
406 //*************************************************************************
408 {
409 if (&rhs != this)
410 {
411 this->assign(rhs);
412 }
413
414 return *this;
415 }
416
417 //*************************************************************************
419 //*************************************************************************
421 {
422 if (&rhs != this)
423 {
424 this->assign(rhs);
425 }
426
427 return *this;
428 }
429
430 //*************************************************************************
432 //*************************************************************************
433 wstring_ext& operator = (const value_type* text)
434 {
435 this->assign(text);
436
437 return *this;
438 }
439
440 //*************************************************************************
442 //*************************************************************************
444 {
445 this->assign(view);
446
447 return *this;
448 }
449
450 //*************************************************************************
452 //*************************************************************************
453#if ETL_HAS_ISTRING_REPAIR
454 virtual void repair() ETL_OVERRIDE
455#else
456 void repair()
457#endif
458 {
459 }
460
461 private:
462
463 //*************************************************************************
465 //*************************************************************************
466 wstring_ext(const wstring_ext& other) ETL_DELETE;
467 };
468
469 //*************************************************************************
471 //*************************************************************************
472#if ETL_USING_8BIT_TYPES
473 template <>
474 struct hash<etl::iwstring>
475 {
476 size_t operator()(const etl::iwstring& text) const
477 {
478 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
479 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
480 }
481 };
482
483 template <size_t SIZE>
484 struct hash<etl::wstring<SIZE> >
485 {
486 size_t operator()(const etl::wstring<SIZE>& text) const
487 {
488 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
489 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
490 }
491 };
492
493 template <>
494 struct hash<etl::wstring_ext>
495 {
496 size_t operator()(const etl::wstring_ext& text) const
497 {
498 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
499 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
500 }
501 };
502#endif
503
504 //***************************************************************************
506 //***************************************************************************
507 template<size_t Array_Size>
508 etl::wstring<Array_Size - 1U> make_string(const wchar_t(&text)[Array_Size])
509 {
510 return etl::wstring<Array_Size - 1U>(text, etl::strlen(text, Array_Size - 1U));
511 }
512
513 //***************************************************************************
515 //***************************************************************************
516 template<size_t MAX_SIZE, size_t SIZE>
518 {
519 return etl::wstring<MAX_SIZE>(text, etl::strlen(text, SIZE));
520 }
521}
522
523#include "private/minmax_pop.h"
524
525#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 wstring.h:270
wstring_ext(const etl::iwstring &other, value_type *buffer, size_type buffer_size)
Definition wstring.h:302
wstring_ext(size_type count, value_type c, value_type *buffer, size_type buffer_size)
Definition wstring.h:360
wstring_ext(const value_type *text, value_type *buffer, size_type buffer_size)
Definition wstring.h:328
wstring_ext(value_type *buffer, size_type buffer_size)
Constructor.
Definition wstring.h:281
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:374
wstring_ext & operator=(const wstring_ext &rhs)
Assignment operator.
Definition wstring.h:407
wstring_ext(const etl::wstring_view &view, value_type *buffer, size_type buffer_size)
Definition wstring.h:397
wstring_ext(const etl::iwstring &other, value_type *buffer, size_type buffer_size, size_type position, size_type length=npos)
Definition wstring.h:315
void repair()
Fix the internal pointers after a low level memory copy.
Definition wstring.h:456
wstring_ext(const etl::wstring_ext &other, value_type *buffer, size_type buffer_size)
Definition wstring.h:291
wstring_ext(const value_type *text, size_type count, value_type *buffer, size_type buffer_size)
Definition wstring.h:348
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
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