Embedded Template Library 1.0
Loading...
Searching...
No Matches
string.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_STRING_INCLUDED
32#define ETL_STRING_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 <ctype.h>
41
42#include "private/minmax_push.h"
43
44namespace etl
45{
46#if ETL_USING_CPP11
47 inline namespace literals
48 {
49 inline namespace string_literals
50 {
51 inline constexpr etl::string_view operator ""_sv(const char* str, size_t length) noexcept
52 {
53 return etl::string_view{ str, length };
54 }
55 }
56 }
57#endif
58
59 typedef etl::ibasic_string<char> istring;
60
61 //***************************************************************************
65 //***************************************************************************
66 template <size_t MAX_SIZE_>
67 class string : public istring
68 {
69 public:
70
71 typedef istring base_type;
72 typedef istring interface_type;
73
74 typedef istring::value_type value_type;
75 typedef istring::size_type size_type;
76
77 static ETL_CONSTANT size_t MAX_SIZE = MAX_SIZE_;
78
79 //*************************************************************************
81 //*************************************************************************
83 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
84 {
85 this->initialise();
86 }
87
88 //*************************************************************************
91 //*************************************************************************
93 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
94 {
95 this->initialise();
96 this->assign(other);
97 }
98
99 //*************************************************************************
102 //*************************************************************************
104 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
105 {
106 this->initialise();
107 this->assign(other);
108 }
109
110 //*************************************************************************
115 //*************************************************************************
116 string(const etl::istring& other, size_t position, size_t length = npos)
117 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
118 {
119 ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
120
121 this->initialise();
122 this->assign(other, position, length);
123 }
124
125 //*************************************************************************
128 //*************************************************************************
129 ETL_EXPLICIT_STRING_FROM_CHAR string(const value_type* text)
130 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
131 {
132 this->initialise();
133 this->assign(text);
134 }
135
136 //*************************************************************************
140 //*************************************************************************
141 string(const value_type* text, size_t count)
142 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
143 {
144 this->initialise();
145 this->assign(text, text + count);
146 }
147
148 //*************************************************************************
152 //*************************************************************************
153 string(size_type count, value_type c)
154 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
155 {
156 this->initialise();
157 this->resize(count, c);
158 }
159
160 //*************************************************************************
165 //*************************************************************************
166 template <typename TIterator>
168 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
169 {
170 this->initialise();
171 this->assign(first, last);
172 }
173
174#if ETL_HAS_INITIALIZER_LIST
175 //*************************************************************************
177 //*************************************************************************
178 string(std::initializer_list<value_type> init)
179 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
180 {
181 this->initialise();
182 this->assign(init.begin(), init.end());
183 }
184#endif
185
186 //*************************************************************************
189 //*************************************************************************
190 explicit string(const etl::string_view& view)
191 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
192 {
193 this->initialise();
194 this->assign(view.begin(), view.end());
195 }
196
197 //*************************************************************************
201 //*************************************************************************
202 etl::string<MAX_SIZE_> substr(size_type position = 0, size_type length_ = npos) const
203 {
205
206 if (position != this->size())
207 {
209
210 length_ = etl::min(length_, this->size() - position);
211
212 new_string.assign(buffer + position, buffer + position + length_);
213 }
214
215 return new_string;
216 }
217
218 //*************************************************************************
220 //*************************************************************************
221 string& operator = (const string& rhs)
222 {
223 if (&rhs != this)
224 {
225 this->assign(rhs);
226 }
227
228 return *this;
229 }
230
231
232 //*************************************************************************
234 //*************************************************************************
235 string& operator = (const istring& rhs)
236 {
237 if (&rhs != this)
238 {
239 this->assign(rhs);
240 }
241
242 return *this;
243 }
244
245 //*************************************************************************
247 //*************************************************************************
248 string& operator = (const value_type* text)
249 {
250 this->assign(text);
251
252 return *this;
253 }
254
255 //*************************************************************************
257 //*************************************************************************
259 {
260 this->assign(view);
261
262 return *this;
263 }
264
265 //*************************************************************************
267 //*************************************************************************
268#if ETL_HAS_ISTRING_REPAIR
269 virtual void repair() ETL_OVERRIDE
270#else
271 void repair()
272#endif
273 {
275 }
276
277 private:
278
279 value_type buffer[MAX_SIZE + 1];
280 };
281
282 template <size_t MAX_SIZE_>
283 ETL_CONSTANT typename string<MAX_SIZE_>::size_type string<MAX_SIZE_>::MAX_SIZE;
284
285 //***************************************************************************
288 //***************************************************************************
289 class string_ext : public istring
290 {
291 public:
292
293 typedef istring base_type;
294 typedef istring interface_type;
295
296 typedef istring::value_type value_type;
297 typedef istring::size_type size_type;
298
299 //*************************************************************************
301 //*************************************************************************
302 string_ext(value_type* buffer, size_type buffer_size)
303 : istring(buffer, buffer_size - 1U)
304 {
305 this->initialise();
306 }
307
308 //*************************************************************************
311 //*************************************************************************
312 string_ext(const etl::string_ext& other, value_type* buffer, size_type buffer_size)
313 : istring(buffer, buffer_size - 1U)
314 {
315 this->initialise();
316 this->assign(other);
317 }
318
319 //*************************************************************************
322 //*************************************************************************
323 string_ext(const etl::istring& other, value_type* buffer, size_type buffer_size)
324 : istring(buffer, buffer_size - 1U)
325 {
326 this->initialise();
327 this->assign(other);
328 }
329
330 //*************************************************************************
335 //*************************************************************************
336 string_ext(const etl::istring& other, value_type* buffer, size_type buffer_size, size_type position, size_type length = npos)
337 : istring(buffer, buffer_size - 1U)
338 {
339 ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
340
341 this->initialise();
342 this->assign(other, position, length);
343 }
344
345 //*************************************************************************
348 //*************************************************************************
349 string_ext(const char* text, char* buffer, size_type buffer_size)
350 : istring(buffer, buffer_size - 1U)
351 {
352 // Is the initial text at the same address as the buffer?
353 if (text == buffer)
354 {
355 this->current_size = etl::strlen(buffer);
356 }
357 else
358 {
359 this->initialise();
360 this->assign(text, text + etl::strlen(text));
361 }
362 }
363
364 //*************************************************************************
368 //*************************************************************************
369 string_ext(const value_type* text, size_type count, value_type* buffer, size_type buffer_size)
370 : istring(buffer, buffer_size - 1U)
371 {
372 this->initialise();
373 this->assign(text, text + count);
374 }
375
376 //*************************************************************************
380 //*************************************************************************
381 string_ext(size_type count, value_type c, value_type* buffer, size_type buffer_size)
382 : istring(buffer, buffer_size - 1U)
383 {
384 this->initialise();
385 this->resize(count, c);
386 }
387
388 //*************************************************************************
391 //*************************************************************************
392 explicit string_ext(const etl::string_view& view, value_type* buffer, size_type buffer_size)
393 : istring(buffer, buffer_size - 1U)
394 {
395 this->assign(view.begin(), view.end());
396 }
397
398 //*************************************************************************
403 //*************************************************************************
404 template <typename TIterator>
405 string_ext(TIterator first, TIterator last, value_type* buffer, size_type buffer_size, typename etl::enable_if<!etl::is_integral<TIterator>::value, int>::type = 0)
406 : istring(buffer, buffer_size - 1U)
407 {
408 this->assign(first, last);
409 }
410
411#if ETL_HAS_INITIALIZER_LIST
412 //*************************************************************************
414 //*************************************************************************
415 string_ext(std::initializer_list<value_type> init, value_type* buffer, size_type buffer_size)
416 : istring(buffer, buffer_size - 1U)
417 {
418 this->assign(init.begin(), init.end());
419 }
420#endif
421
422 //*************************************************************************
424 //*************************************************************************
426 {
427 if (&rhs != this)
428 {
429 this->assign(rhs);
430 }
431
432 return *this;
433 }
434
435 //*************************************************************************
437 //*************************************************************************
439 {
440 if (&rhs != this)
441 {
442 this->assign(rhs);
443 }
444
445 return *this;
446 }
447
448 //*************************************************************************
450 //*************************************************************************
451 string_ext& operator = (const value_type* text)
452 {
453 this->assign(text);
454
455 return *this;
456 }
457
458 //*************************************************************************
460 //*************************************************************************
462 {
463 this->assign(view);
464
465 return *this;
466 }
467
468 //*************************************************************************
470 //*************************************************************************
471#if ETL_HAS_ISTRING_REPAIR
472 virtual void repair() ETL_OVERRIDE
473#else
474 void repair()
475#endif
476 {
477 }
478
479 private:
480
481 //*************************************************************************
483 //*************************************************************************
484 string_ext(const string_ext& other) ETL_DELETE;
485 };
486
487 //*************************************************************************
489 //*************************************************************************
490#if ETL_USING_8BIT_TYPES
491 template <>
492 struct hash<etl::istring>
493 {
494 size_t operator()(const etl::istring& text) const
495 {
496 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
497 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
498 }
499 };
500
501 template <size_t SIZE>
502 struct hash<etl::string<SIZE> >
503 {
504 size_t operator()(const etl::string<SIZE>& text) const
505 {
506 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
507 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
508 }
509 };
510
511 template <>
512 struct hash<etl::string_ext>
513 {
514 size_t operator()(const etl::string_ext& text) const
515 {
516 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
517 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
518 }
519 };
520#endif
521
522 //***************************************************************************
524 //***************************************************************************
525 template<size_t Array_Size>
526 etl::string<Array_Size - 1U> make_string(const char(&text)[Array_Size])
527 {
528 return etl::string<Array_Size - 1U>(text, etl::strlen(text, Array_Size - 1));
529 }
530
531 //***************************************************************************
533 //***************************************************************************
534 template<size_t MAX_SIZE, size_t SIZE>
536 {
537 return etl::string<MAX_SIZE>(text, etl::strlen(text, SIZE));
538 }
539}
540
541#include "private/minmax_pop.h"
542
543#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 string.h:290
string_ext(const etl::istring &other, value_type *buffer, size_type buffer_size, size_type position, size_type length=npos)
Definition string.h:336
string_ext(value_type *buffer, size_type buffer_size)
Constructor.
Definition string.h:302
string_ext(const etl::string_ext &other, value_type *buffer, size_type buffer_size)
Definition string.h:312
string_ext(const etl::string_view &view, value_type *buffer, size_type buffer_size)
Definition string.h:392
string_ext(const char *text, char *buffer, size_type buffer_size)
Definition string.h:349
string_ext(size_type count, value_type c, value_type *buffer, size_type buffer_size)
Definition string.h:381
string_ext(const etl::istring &other, value_type *buffer, size_type buffer_size)
Definition string.h:323
void repair()
Fix the internal pointers after a low level memory copy.
Definition string.h:474
string_ext(const value_type *text, size_type count, value_type *buffer, size_type buffer_size)
Definition string.h:369
string_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 string.h:405
string_ext & operator=(const string_ext &rhs)
Assignment operator.
Definition string.h:425
Definition basic_string.h:115
Definition string.h:68
etl::string< MAX_SIZE_ > substr(size_type position=0, size_type length_=npos) const
Definition string.h:202
void repair()
Fix the internal pointers after a low level memory copy.
Definition string.h:271
string()
Constructor.
Definition string.h:82
string(TIterator first, TIterator last, typename etl::enable_if<!etl::is_integral< TIterator >::value, int >::type=0)
Definition string.h:167
string(const etl::string_view &view)
Definition string.h:190
string(const etl::string< MAX_SIZE_ > &other)
Definition string.h:92
string(const etl::istring &other)
Definition string.h:103
string & operator=(const string &rhs)
Assignment operator.
Definition string.h:221
string(const etl::istring &other, size_t position, size_t length=npos)
Definition string.h:116
string(const value_type *text, size_t count)
Definition string.h:141
ETL_EXPLICIT_STRING_FROM_CHAR string(const value_type *text)
Definition string.h:129
string(size_type count, value_type c)
Definition string.h:153
#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