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 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;
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->assign(other);
96 }
97
98 //*************************************************************************
101 //*************************************************************************
103 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
104 {
105 this->assign(other);
106 }
107
108 //*************************************************************************
113 //*************************************************************************
114 string(const etl::istring& other, size_t position, size_t length = npos)
115 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
116 {
117 ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
118
119 this->assign(other, position, length);
120 }
121
122 //*************************************************************************
125 //*************************************************************************
126 ETL_EXPLICIT_STRING_FROM_CHAR string(const value_type* text)
127 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
128 {
129 this->assign(text, text + etl::char_traits<value_type>::length(text));
130 }
131
132 //*************************************************************************
136 //*************************************************************************
137 string(const value_type* text, size_t count)
138 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
139 {
140 this->assign(text, text + count);
141 }
142
143 //*************************************************************************
147 //*************************************************************************
148 string(size_type count, value_type c)
149 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
150 {
151 this->initialise();
152 this->resize(count, c);
153 }
154
155 //*************************************************************************
160 //*************************************************************************
161 template <typename TIterator>
163 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
164 {
165 this->assign(first, last);
166 }
167
168#if ETL_HAS_INITIALIZER_LIST
169 //*************************************************************************
171 //*************************************************************************
172 string(std::initializer_list<value_type> init)
173 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
174 {
175 this->assign(init.begin(), init.end());
176 }
177#endif
178
179 //*************************************************************************
182 //*************************************************************************
183 explicit string(const etl::string_view& view)
184 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
185 {
186 this->assign(view.begin(), view.end());
187 }
188
189 //*************************************************************************
193 //*************************************************************************
195 {
197
198 if (position != this->size())
199 {
201
202 length_ = etl::min(length_, this->size() - position);
203
204 new_string.assign(buffer + position, buffer + position + length_);
205 }
206
207 return new_string;
208 }
209
210 //*************************************************************************
212 //*************************************************************************
213 string& operator = (const string& rhs)
214 {
215 if (&rhs != this)
216 {
217 this->assign(rhs);
218 }
219
220 return *this;
221 }
222
223
224 //*************************************************************************
226 //*************************************************************************
227 string& operator = (const istring& rhs)
228 {
229 if (&rhs != this)
230 {
231 this->assign(rhs);
232 }
233
234 return *this;
235 }
236
237 //*************************************************************************
239 //*************************************************************************
240 string& operator = (const value_type* text)
241 {
242 this->assign(text);
243
244 return *this;
245 }
246
247 //*************************************************************************
249 //*************************************************************************
251 {
252 this->assign(view);
253
254 return *this;
255 }
256
257 //*************************************************************************
259 //*************************************************************************
260#if ETL_HAS_ISTRING_REPAIR
261 virtual void repair() ETL_OVERRIDE
262#else
263 void repair()
264#endif
265 {
267 }
268
269 private:
270
271 value_type buffer[MAX_SIZE + 1];
272 };
273
274 template <size_t MAX_SIZE_>
275 ETL_CONSTANT typename string<MAX_SIZE_>::size_type string<MAX_SIZE_>::MAX_SIZE;
276
277 //***************************************************************************
280 //***************************************************************************
281 class string_ext : public istring
282 {
283 public:
284
285 typedef istring base_type;
286 typedef istring interface_type;
287
288 typedef istring::value_type value_type;
290
291 //*************************************************************************
293 //*************************************************************************
294 string_ext(value_type* buffer, size_type buffer_size)
295 : istring(buffer, buffer_size - 1U)
296 {
297 this->initialise();
298 }
299
300 //*************************************************************************
303 //*************************************************************************
304 string_ext(const etl::string_ext& other, value_type* buffer, size_type buffer_size)
305 : istring(buffer, buffer_size - 1U)
306 {
307 this->assign(other);
308 }
309
310 //*************************************************************************
313 //*************************************************************************
314 string_ext(const etl::istring& other, value_type* buffer, size_type buffer_size)
315 : istring(buffer, buffer_size - 1U)
316 {
317 this->assign(other);
318 }
319
320 //*************************************************************************
325 //*************************************************************************
326 string_ext(const etl::istring& other, value_type* buffer, size_type buffer_size, size_type position, size_type length = npos)
327 : istring(buffer, buffer_size - 1U)
328 {
329 ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
330
331 this->assign(other, position, length);
332 }
333
334 //*************************************************************************
337 //*************************************************************************
338 string_ext(const char* text, char* buffer, size_type buffer_size)
339 : istring(buffer, buffer_size - 1U)
340 {
341 // Is the initial text at the same address as the buffer?
342 if (text == buffer)
343 {
344 this->current_size = etl::strlen(buffer);
345 }
346 else
347 {
348 this->assign(text, text + etl::strlen(text));
349 }
350 }
351
352 //*************************************************************************
356 //*************************************************************************
357 string_ext(const value_type* text, size_type count, value_type* buffer, size_type buffer_size)
358 : istring(buffer, buffer_size - 1U)
359 {
360 this->assign(text, text + count);
361 }
362
363 //*************************************************************************
367 //*************************************************************************
368 string_ext(size_type count, value_type c, value_type* buffer, size_type buffer_size)
369 : istring(buffer, buffer_size - 1U)
370 {
371 this->initialise();
372 this->resize(count, c);
373 }
374
375 //*************************************************************************
378 //*************************************************************************
379 explicit string_ext(const etl::string_view& view, value_type* buffer, size_type buffer_size)
380 : istring(buffer, buffer_size - 1U)
381 {
382 this->assign(view.begin(), view.end());
383 }
384
385 //*************************************************************************
390 //*************************************************************************
391 template <typename TIterator>
392 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)
393 : istring(buffer, buffer_size - 1U)
394 {
395 this->assign(first, last);
396 }
397
398#if ETL_HAS_INITIALIZER_LIST
399 //*************************************************************************
401 //*************************************************************************
402 string_ext(std::initializer_list<value_type> init, value_type* buffer, size_type buffer_size)
403 : istring(buffer, buffer_size - 1U)
404 {
405 this->assign(init.begin(), init.end());
406 }
407#endif
408
409 //*************************************************************************
411 //*************************************************************************
413 {
414 if (&rhs != this)
415 {
416 this->assign(rhs);
417 }
418
419 return *this;
420 }
421
422 //*************************************************************************
424 //*************************************************************************
426 {
427 if (&rhs != this)
428 {
429 this->assign(rhs);
430 }
431
432 return *this;
433 }
434
435 //*************************************************************************
437 //*************************************************************************
438 string_ext& operator = (const value_type* text)
439 {
440 this->assign(text);
441
442 return *this;
443 }
444
445 //*************************************************************************
447 //*************************************************************************
449 {
450 this->assign(view);
451
452 return *this;
453 }
454
455 //*************************************************************************
457 //*************************************************************************
458#if ETL_HAS_ISTRING_REPAIR
459 virtual void repair() ETL_OVERRIDE
460#else
461 void repair()
462#endif
463 {
464 }
465
466 private:
467
468 //*************************************************************************
470 //*************************************************************************
471 string_ext(const string_ext& other) ETL_DELETE;
472 };
473
474 //*************************************************************************
476 //*************************************************************************
477#if ETL_USING_8BIT_TYPES
478 template <>
479 struct hash<etl::istring>
480 {
481 size_t operator()(const etl::istring& text) const
482 {
483 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
484 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
485 }
486 };
487
488 template <size_t SIZE>
489 struct hash<etl::string<SIZE> >
490 {
491 size_t operator()(const etl::string<SIZE>& text) const
492 {
493 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
494 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
495 }
496 };
497
498 template <>
499 struct hash<etl::string_ext>
500 {
501 size_t operator()(const etl::string_ext& text) const
502 {
503 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
504 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
505 }
506 };
507#endif
508
509 //***************************************************************************
511 //***************************************************************************
512 template<size_t Array_Size>
513 etl::string<Array_Size - 1U> make_string(const char(&text)[Array_Size])
514 {
515 return etl::string<Array_Size - 1U>(text, etl::strlen(text, Array_Size - 1));
516 }
517
518 //***************************************************************************
520 //***************************************************************************
521 template<size_t MAX_SIZE, size_t SIZE>
523 {
524 return etl::string<MAX_SIZE>(text, etl::strlen(text, SIZE));
525 }
526}
527
528#include "private/minmax_pop.h"
529
530#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 string.h:282
string_ext(const etl::istring &other, value_type *buffer, size_type buffer_size, size_type position, size_type length=npos)
Definition string.h:326
string_ext(value_type *buffer, size_type buffer_size)
Constructor.
Definition string.h:294
string_ext(const etl::string_ext &other, value_type *buffer, size_type buffer_size)
Definition string.h:304
string_ext(const etl::string_view &view, value_type *buffer, size_type buffer_size)
Definition string.h:379
string_ext(const char *text, char *buffer, size_type buffer_size)
Definition string.h:338
string_ext(size_type count, value_type c, value_type *buffer, size_type buffer_size)
Definition string.h:368
string_ext(const etl::istring &other, value_type *buffer, size_type buffer_size)
Definition string.h:314
void repair()
Fix the internal pointers after a low level memory copy.
Definition string.h:461
string_ext(const value_type *text, size_type count, value_type *buffer, size_type buffer_size)
Definition string.h:357
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:392
string_ext & operator=(const string_ext &rhs)
Assignment operator.
Definition string.h:412
Definition basic_string.h:109
Definition string.h:68
etl::string< MAX_SIZE_ > substr(size_type position=0, size_type length_=npos) const
Definition string.h:194
void repair()
Fix the internal pointers after a low level memory copy.
Definition string.h:263
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:162
string(const etl::string_view &view)
Definition string.h:183
string(const etl::string< MAX_SIZE_ > &other)
Definition string.h:92
string(const etl::istring &other)
Definition string.h:102
string & operator=(const string &rhs)
Assignment operator.
Definition string.h:213
string(const etl::istring &other, size_t position, size_t length=npos)
Definition string.h:114
string(const value_type *text, size_t count)
Definition string.h:137
ETL_EXPLICIT_STRING_FROM_CHAR string(const value_type *text)
Definition string.h:126
string(size_type count, value_type c)
Definition string.h:148
#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