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 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->assign(other);
93 }
94
95 //*************************************************************************
98 //*************************************************************************
100 : iwstring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
101 {
102 this->assign(other);
103 }
104
105 //*************************************************************************
110 //*************************************************************************
112 : iwstring(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 wstring(const value_type* text)
124 : iwstring(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 wstring(const value_type* text, size_type count)
135 : iwstring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
136 {
137 this->assign(text, text + count);
138 }
139
140 //*************************************************************************
144 //*************************************************************************
145 wstring(size_type count, value_type c)
146 : iwstring(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 : iwstring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
161 {
162 this->assign(first, last);
163 }
164
165#if ETL_HAS_INITIALIZER_LIST
166 //*************************************************************************
168 //*************************************************************************
169 wstring(std::initializer_list<value_type> init)
170 : iwstring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
171 {
172 this->assign(init.begin(), init.end());
173 }
174#endif
175
176 //*************************************************************************
179 //*************************************************************************
181 : iwstring(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 wstring& 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
235#endif
236 void repair()
237 {
239 }
240
241 private:
242
243 value_type buffer[MAX_SIZE + 1];
244 };
245
246 //***************************************************************************
249 //***************************************************************************
250 class wstring_ext : public iwstring
251 {
252 public:
253
254 typedef iwstring base_type;
255 typedef iwstring interface_type;
256
257 typedef iwstring::value_type value_type;
258
259 //*************************************************************************
261 //*************************************************************************
262 wstring_ext(value_type* buffer, size_type buffer_size)
263 : iwstring(buffer, buffer_size - 1U)
264 {
265 this->initialise();
266 }
267
268 //*************************************************************************
271 //*************************************************************************
272 wstring_ext(const etl::wstring_ext& other, value_type* buffer, size_type buffer_size)
273 : iwstring(buffer, buffer_size - 1U)
274 {
275 this->assign(other);
276 }
277
278 //*************************************************************************
281 //*************************************************************************
282 wstring_ext(const etl::iwstring& other, value_type* buffer, size_type buffer_size)
283 : iwstring(buffer, buffer_size - 1U)
284 {
285 this->assign(other);
286 }
287
288 //*************************************************************************
293 //*************************************************************************
294 wstring_ext(const etl::iwstring& other, value_type* buffer, size_type buffer_size, size_type position, size_type length = npos)
295 : iwstring(buffer, buffer_size - 1U)
296 {
297 ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
298
299 this->assign(other, position, length);
300 }
301
302 //*************************************************************************
305 //*************************************************************************
306 wstring_ext(const value_type* text, value_type* buffer, size_type buffer_size)
307 : iwstring(buffer, buffer_size - 1U)
308 {
309 // Is the initial text at the same address as the buffer?
310 if (text == buffer)
311 {
312 this->current_size = etl::strlen(buffer);
313 }
314 else
315 {
316 this->assign(text, text + etl::strlen(text));
317 }
318 }
319
320 //*************************************************************************
324 //*************************************************************************
325 wstring_ext(const value_type* text, size_type count, value_type* buffer, size_type buffer_size)
326 : iwstring(buffer, buffer_size - 1U)
327 {
328 this->assign(text, text + count);
329 }
330
331 //*************************************************************************
335 //*************************************************************************
336 wstring_ext(size_type count, value_type c, value_type* buffer, size_type buffer_size)
337 : iwstring(buffer, buffer_size - 1U)
338 {
339 this->initialise();
340 this->resize(count, c);
341 }
342
343 //*************************************************************************
348 //*************************************************************************
349 template <typename TIterator>
350 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)
351 : iwstring(buffer, buffer_size - 1U)
352 {
353 this->assign(first, last);
354 }
355
356#if ETL_HAS_INITIALIZER_LIST
357 //*************************************************************************
359 //*************************************************************************
360 wstring_ext(std::initializer_list<value_type> init, value_type* buffer, size_type buffer_size)
361 : iwstring(buffer, buffer_size - 1U)
362 {
363 this->assign(init.begin(), init.end());
364 }
365#endif
366
367 //*************************************************************************
370 //*************************************************************************
371 explicit wstring_ext(const etl::wstring_view& view, value_type* buffer, size_type buffer_size)
372 : iwstring(buffer, buffer_size - 1U)
373 {
374 this->assign(view.begin(), view.end());
375 }
376
377 //*************************************************************************
379 //*************************************************************************
381 {
382 if (&rhs != this)
383 {
384 this->assign(rhs);
385 }
386
387 return *this;
388 }
389
390
391 //*************************************************************************
393 //*************************************************************************
395 {
396 if (&rhs != this)
397 {
398 this->assign(rhs);
399 }
400
401 return *this;
402 }
403
404 //*************************************************************************
406 //*************************************************************************
407 wstring_ext& operator = (const value_type* text)
408 {
409 this->assign(text);
410
411 return *this;
412 }
413
414 //*************************************************************************
416 //*************************************************************************
417 void repair()
418#if ETL_HAS_ISTRING_REPAIR
419 ETL_OVERRIDE
420#endif
421 {
422 }
423
424 private:
425
426 //*************************************************************************
428 //*************************************************************************
429 wstring_ext(const wstring_ext& other) ETL_DELETE;
430 };
431
432 //*************************************************************************
434 //*************************************************************************
435#if ETL_USING_8BIT_TYPES
436 template <>
437 struct hash<etl::iwstring>
438 {
439 size_t operator()(const etl::iwstring& text) const
440 {
441 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
442 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
443 }
444 };
445
446 template <size_t SIZE>
447 struct hash<etl::wstring<SIZE> >
448 {
449 size_t operator()(const etl::wstring<SIZE>& text) const
450 {
451 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
452 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
453 }
454 };
455
456 template <>
457 struct hash<etl::wstring_ext>
458 {
459 size_t operator()(const etl::wstring_ext& text) const
460 {
461 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
462 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
463 }
464 };
465#endif
466
467 //***************************************************************************
469 //***************************************************************************
470 template<size_t Array_Size>
471 etl::wstring<Array_Size - 1U> make_string(const wchar_t(&text)[Array_Size])
472 {
473 return etl::wstring<Array_Size - 1U>(text, etl::strlen(text, Array_Size - 1U));
474 }
475
476 //***************************************************************************
478 //***************************************************************************
479 template<size_t MAX_SIZE, size_t SIZE>
481 {
482 return etl::wstring<MAX_SIZE>(text, etl::strlen(text, SIZE));
483 }
484}
485
486#include "private/minmax_pop.h"
487
488#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:626
pointer data()
Definition basic_string.h:589
void initialise()
Initialise the string.
Definition basic_string.h:2289
void repair_buffer(T *p_buffer_)
Fix the internal pointers after a low level memory copy.
Definition basic_string.h:2302
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 wstring.h:251
wstring_ext(const etl::iwstring &other, value_type *buffer, size_type buffer_size)
Definition wstring.h:282
wstring_ext(size_type count, value_type c, value_type *buffer, size_type buffer_size)
Definition wstring.h:336
wstring_ext(const value_type *text, value_type *buffer, size_type buffer_size)
Definition wstring.h:306
wstring_ext(value_type *buffer, size_type buffer_size)
Constructor.
Definition wstring.h:262
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:350
wstring_ext & operator=(const wstring_ext &rhs)
Assignment operator.
Definition wstring.h:380
wstring_ext(const etl::wstring_view &view, value_type *buffer, size_type buffer_size)
Definition wstring.h:371
wstring_ext(const etl::iwstring &other, value_type *buffer, size_type buffer_size, size_type position, size_type length=npos)
Definition wstring.h:294
void repair()
Fix the internal pointers after a low level memory copy.
Definition wstring.h:417
wstring_ext(const etl::wstring_ext &other, value_type *buffer, size_type buffer_size)
Definition wstring.h:272
wstring_ext(const value_type *text, size_type count, value_type *buffer, size_type buffer_size)
Definition wstring.h:325
Definition wstring.h:66
wstring(const value_type *text, size_type count)
Definition wstring.h:134
wstring(const etl::iwstring &other)
Definition wstring.h:99
wstring(const etl::iwstring &other, size_type position, size_type length=npos)
Definition wstring.h:111
wstring(const etl::wstring_view &view)
Definition wstring.h:180
ETL_EXPLICIT_STRING_FROM_CHAR wstring(const value_type *text)
Definition wstring.h:123
wstring & operator=(const wstring &rhs)
Assignment operator.
Definition wstring.h:210
void repair()
Fix the internal pointers after a low level memory copy.
Definition wstring.h:236
wstring(TIterator first, TIterator last, typename etl::enable_if<!etl::is_integral< TIterator >::value, int >::type=0)
Definition wstring.h:159
wstring()
Constructor.
Definition wstring.h:79
wstring(size_type count, value_type c)
Definition wstring.h:145
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:191
#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:491
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:500
ETL_CONSTEXPR size_t strlen(const T *t)
Alternative strlen for all character types.
Definition char_traits.h:267
Character traits for any character type.
Definition char_traits.h:102
pair holds two objects of arbitrary type
Definition utility.h:164
ETL_CONSTEXPR pair()
Default constructor.
Definition utility.h:176