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;
75
76 static ETL_CONSTANT size_t MAX_SIZE = MAX_SIZE_;
77
78 //*************************************************************************
80 //*************************************************************************
82 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
83 {
84 this->initialise();
85 }
86
87 //*************************************************************************
90 //*************************************************************************
92 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
93 {
94 this->assign(other);
95 }
96
97 //*************************************************************************
100 //*************************************************************************
102 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
103 {
104 this->assign(other);
105 }
106
107 //*************************************************************************
112 //*************************************************************************
113 string(const etl::istring& other, size_t position, size_t length = npos)
114 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
115 {
116 ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
117
118 this->assign(other, position, length);
119 }
120
121 //*************************************************************************
124 //*************************************************************************
125 ETL_EXPLICIT_STRING_FROM_CHAR string(const value_type* text)
126 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
127 {
128 this->assign(text, text + etl::char_traits<value_type>::length(text));
129 }
130
131 //*************************************************************************
135 //*************************************************************************
136 string(const value_type* text, size_t count)
137 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
138 {
139 this->assign(text, text + count);
140 }
141
142 //*************************************************************************
146 //*************************************************************************
147 string(size_type count, value_type c)
148 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
149 {
150 this->initialise();
151 this->resize(count, c);
152 }
153
154 //*************************************************************************
159 //*************************************************************************
160 template <typename TIterator>
162 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
163 {
164 this->assign(first, last);
165 }
166
167#if ETL_HAS_INITIALIZER_LIST
168 //*************************************************************************
170 //*************************************************************************
171 string(std::initializer_list<value_type> init)
172 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
173 {
174 this->assign(init.begin(), init.end());
175 }
176#endif
177
178 //*************************************************************************
181 //*************************************************************************
182 explicit string(const etl::string_view& view)
183 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
184 {
185 this->assign(view.begin(), view.end());
186 }
187
188 //*************************************************************************
192 //*************************************************************************
194 {
196
197 if (position != this->size())
198 {
200
201 length_ = etl::min(length_, this->size() - position);
202
203 new_string.assign(buffer + position, buffer + position + length_);
204 }
205
206 return new_string;
207 }
208
209 //*************************************************************************
211 //*************************************************************************
212 string& operator = (const string& rhs)
213 {
214 if (&rhs != this)
215 {
216 this->assign(rhs);
217 }
218
219 return *this;
220 }
221
222
223 //*************************************************************************
225 //*************************************************************************
226 string& operator = (const istring& rhs)
227 {
228 if (&rhs != this)
229 {
230 this->assign(rhs);
231 }
232
233 return *this;
234 }
235
236 //*************************************************************************
238 //*************************************************************************
239 string& operator = (const value_type* text)
240 {
241 this->assign(text);
242
243 return *this;
244 }
245
246 //*************************************************************************
248 //*************************************************************************
249#if ETL_HAS_ISTRING_REPAIR
250 virtual void repair() ETL_OVERRIDE
251#else
252 void repair()
253#endif
254 {
256 }
257
258 private:
259
260 value_type buffer[MAX_SIZE + 1];
261 };
262
263 template <size_t MAX_SIZE_>
264 ETL_CONSTANT size_t string<MAX_SIZE_>::MAX_SIZE;
265
266 //***************************************************************************
269 //***************************************************************************
270 class string_ext : public istring
271 {
272 public:
273
274 typedef istring base_type;
275 typedef istring interface_type;
276
277 typedef istring::value_type value_type;
279
280 //*************************************************************************
282 //*************************************************************************
283 string_ext(value_type* buffer, size_type buffer_size)
284 : istring(buffer, buffer_size - 1U)
285 {
286 this->initialise();
287 }
288
289 //*************************************************************************
292 //*************************************************************************
293 string_ext(const etl::string_ext& other, value_type* buffer, size_type buffer_size)
294 : istring(buffer, buffer_size - 1U)
295 {
296 this->assign(other);
297 }
298
299 //*************************************************************************
302 //*************************************************************************
303 string_ext(const etl::istring& other, value_type* buffer, size_type buffer_size)
304 : istring(buffer, buffer_size - 1U)
305 {
306 this->assign(other);
307 }
308
309 //*************************************************************************
314 //*************************************************************************
315 string_ext(const etl::istring& other, value_type* buffer, size_type buffer_size, size_type position, size_type length = npos)
316 : istring(buffer, buffer_size - 1U)
317 {
318 ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
319
320 this->assign(other, position, length);
321 }
322
323 //*************************************************************************
326 //*************************************************************************
327 string_ext(const char* text, char* buffer, size_type buffer_size)
328 : istring(buffer, buffer_size - 1U)
329 {
330 // Is the initial text at the same address as the buffer?
331 if (text == buffer)
332 {
333 this->current_size = etl::strlen(buffer);
334 }
335 else
336 {
337 this->assign(text, text + etl::strlen(text));
338 }
339 }
340
341 //*************************************************************************
345 //*************************************************************************
346 string_ext(const value_type* text, size_type count, value_type* buffer, size_type buffer_size)
347 : istring(buffer, buffer_size - 1U)
348 {
349 this->assign(text, text + count);
350 }
351
352 //*************************************************************************
356 //*************************************************************************
357 string_ext(size_type count, value_type c, value_type* buffer, size_type buffer_size)
358 : istring(buffer, buffer_size - 1U)
359 {
360 this->initialise();
361 this->resize(count, c);
362 }
363
364 //*************************************************************************
369 //*************************************************************************
370 template <typename TIterator>
371 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)
372 : istring(buffer, buffer_size - 1U)
373 {
374 this->assign(first, last);
375 }
376
377#if ETL_HAS_INITIALIZER_LIST
378 //*************************************************************************
380 //*************************************************************************
381 string_ext(std::initializer_list<value_type> init, value_type* buffer, size_type buffer_size)
382 : istring(buffer, buffer_size - 1U)
383 {
384 this->assign(init.begin(), init.end());
385 }
386#endif
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 //*************************************************************************
400 //*************************************************************************
402 {
403 if (&rhs != this)
404 {
405 this->assign(rhs);
406 }
407
408 return *this;
409 }
410
411
412 //*************************************************************************
414 //*************************************************************************
416 {
417 if (&rhs != this)
418 {
419 this->assign(rhs);
420 }
421
422 return *this;
423 }
424
425 //*************************************************************************
427 //*************************************************************************
428 string_ext& operator = (const value_type* text)
429 {
430 this->assign(text);
431
432 return *this;
433 }
434
435 //*************************************************************************
437 //*************************************************************************
438#if ETL_HAS_ISTRING_REPAIR
439 virtual void repair() ETL_OVERRIDE
440#else
441 void repair()
442#endif
443 {
444 }
445
446 private:
447
448 //*************************************************************************
450 //*************************************************************************
451 string_ext(const string_ext& other) ETL_DELETE;
452 };
453
454 //*************************************************************************
456 //*************************************************************************
457#if ETL_USING_8BIT_TYPES
458 template <>
459 struct hash<etl::istring>
460 {
461 size_t operator()(const etl::istring& text) const
462 {
463 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
464 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
465 }
466 };
467
468 template <size_t SIZE>
469 struct hash<etl::string<SIZE> >
470 {
471 size_t operator()(const etl::string<SIZE>& text) const
472 {
473 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
474 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
475 }
476 };
477
478 template <>
479 struct hash<etl::string_ext>
480 {
481 size_t operator()(const etl::string_ext& 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#endif
488
489 //***************************************************************************
491 //***************************************************************************
492 template<size_t Array_Size>
493 etl::string<Array_Size - 1U> make_string(const char(&text)[Array_Size])
494 {
495 return etl::string<Array_Size - 1U>(text, etl::strlen(text, Array_Size - 1));
496 }
497
498 //***************************************************************************
500 //***************************************************************************
501 template<size_t MAX_SIZE, size_t SIZE>
503 {
504 return etl::string<MAX_SIZE>(text, etl::strlen(text, SIZE));
505 }
506}
507
508#include "private/minmax_pop.h"
509
510#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:636
pointer data()
Definition basic_string.h:599
void initialise()
Initialise the string.
Definition basic_string.h:2299
void repair_buffer(T *p_buffer_)
Fix the internal pointers after a low level memory copy.
Definition basic_string.h:2312
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 string.h:271
string_ext(const etl::istring &other, value_type *buffer, size_type buffer_size, size_type position, size_type length=npos)
Definition string.h:315
string_ext(value_type *buffer, size_type buffer_size)
Constructor.
Definition string.h:283
string_ext(const etl::string_ext &other, value_type *buffer, size_type buffer_size)
Definition string.h:293
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:327
string_ext(size_type count, value_type c, value_type *buffer, size_type buffer_size)
Definition string.h:357
string_ext(const etl::istring &other, value_type *buffer, size_type buffer_size)
Definition string.h:303
void repair()
Fix the internal pointers after a low level memory copy.
Definition string.h:441
string_ext(const value_type *text, size_type count, value_type *buffer, size_type buffer_size)
Definition string.h:346
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:371
string_ext & operator=(const string_ext &rhs)
Assignment operator.
Definition string.h:401
Definition basic_string.h:98
Definition string.h:68
etl::string< MAX_SIZE_ > substr(size_type position=0, size_type length_=npos) const
Definition string.h:193
void repair()
Fix the internal pointers after a low level memory copy.
Definition string.h:252
string()
Constructor.
Definition string.h:81
string(TIterator first, TIterator last, typename etl::enable_if<!etl::is_integral< TIterator >::value, int >::type=0)
Definition string.h:161
string(const etl::string_view &view)
Definition string.h:182
string(const etl::string< MAX_SIZE_ > &other)
Definition string.h:91
string(const etl::istring &other)
Definition string.h:101
string & operator=(const string &rhs)
Assignment operator.
Definition string.h:212
string(const etl::istring &other, size_t position, size_t length=npos)
Definition string.h:113
string(const value_type *text, size_t count)
Definition string.h:136
ETL_EXPLICIT_STRING_FROM_CHAR string(const value_type *text)
Definition string.h:125
string(size_type count, value_type c)
Definition string.h:147
#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:493
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:502
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