Embedded Template Library  1.0
string.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 https://github.com/ETLCPP/etl
8 https://www.etlcpp.com
9 
10 Copyright(c) 2016 jwellbelove
11 
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files(the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions :
18 
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
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 
39 #include <ctype.h>
40 
41 #if ETL_CPP11_SUPPORTED && ETL_NOT_USING_STLPORT && ETL_USING_STL
42  #include <initializer_list>
43 #endif
44 
45 #include "private/minmax_push.h"
46 
47 namespace etl
48 {
49  typedef etl::ibasic_string<char> istring;
50 
51  //***************************************************************************
55  //***************************************************************************
56  template <const size_t MAX_SIZE_>
57  class string : public istring
58  {
59  public:
60 
61  typedef istring base_type;
62  typedef istring interface_type;
63 
64  typedef istring::value_type value_type;
65 
66  static const size_t MAX_SIZE = MAX_SIZE_;
67 
68  //*************************************************************************
70  //*************************************************************************
72  : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
73  {
74  this->initialise();
75  }
76 
77  //*************************************************************************
80  //*************************************************************************
82  : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
83  {
84  this->assign(other);
85  }
86 
87  //*************************************************************************
90  //*************************************************************************
91  string(const etl::istring& other)
92  : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
93  {
94  this->assign(other);
95  }
96 
97  //*************************************************************************
102  //*************************************************************************
103  string(const etl::istring& other, size_t position, size_t length = npos)
104  : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
105  {
106  ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
107 
108  this->assign(other, position, length);
109  }
110 
111  //*************************************************************************
114  //*************************************************************************
115  ETL_EXPLICIT_STRING_FROM_CHAR string(const value_type* text)
116  : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
117  {
118  this->assign(text, text + etl::char_traits<value_type>::length(text));
119  }
120 
121  //*************************************************************************
125  //*************************************************************************
126  string(const value_type* text, size_t count)
127  : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
128  {
129  this->assign(text, text + count);
130  }
131 
132  //*************************************************************************
136  //*************************************************************************
137  string(size_type count, value_type c)
138  : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
139  {
140  this->initialise();
141  this->resize(count, c);
142  }
143 
144  //*************************************************************************
149  //*************************************************************************
150  template <typename TIterator>
151  string(TIterator first, TIterator last)
152  : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
153  {
154  this->assign(first, last);
155  }
156 
157 #if ETL_CPP11_SUPPORTED && ETL_NOT_USING_STLPORT && ETL_USING_STL
158  //*************************************************************************
160  //*************************************************************************
161  string(std::initializer_list<value_type> init)
162  : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
163  {
164  this->assign(init.begin(), init.end());
165  }
166 #endif
167 
168  //*************************************************************************
171  //*************************************************************************
172  explicit string(const etl::string_view& view)
173  : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
174  {
175  this->assign(view.begin(), view.end());
176  }
177 
178  //*************************************************************************
182  //*************************************************************************
183  etl::string<MAX_SIZE_> substr(size_type position = 0, size_type length_ = npos) const
184  {
185  etl::string<MAX_SIZE_> new_string;
186 
187  if (position != this->size())
188  {
189  ETL_ASSERT(position < this->size(), ETL_ERROR(string_out_of_bounds));
190 
191  length_ = etl::min(length_, this->size() - position);
192 
193  new_string.assign(buffer + position, buffer + position + length_);
194  }
195 
196  return new_string;
197  }
198 
199  //*************************************************************************
201  //*************************************************************************
202  string& operator = (const string& rhs)
203  {
204  if (&rhs != this)
205  {
206  this->assign(rhs);
207  }
208 
209  return *this;
210  }
211 
212 
213  //*************************************************************************
215  //*************************************************************************
216  string& operator = (const istring& rhs)
217  {
218  if (&rhs != this)
219  {
220  this->assign(rhs);
221  }
222 
223  return *this;
224  }
225 
226  //*************************************************************************
228  //*************************************************************************
229  string& operator = (const value_type* text)
230  {
231  this->assign(text);
232 
233  return *this;
234  }
235 
236  //*************************************************************************
238  //*************************************************************************
239  void repair()
240 #ifdef ETL_ISTRING_REPAIR_ENABLE
241  ETL_OVERRIDE
242 #endif
243  {
245  }
246 
247  private:
248 
249  value_type buffer[MAX_SIZE + 1];
250  };
251 
252  //***************************************************************************
255  //***************************************************************************
256  class string_ext : public istring
257  {
258  public:
259 
260  typedef istring base_type;
261  typedef istring interface_type;
262 
263  typedef istring::value_type value_type;
264  typedef istring::size_type size_type;
265 
266  //*************************************************************************
268  //*************************************************************************
269  string_ext(value_type* buffer, size_type buffer_size)
270  : istring(buffer, buffer_size - 1U)
271  {
272  this->initialise();
273  }
274 
275  //*************************************************************************
278  //*************************************************************************
279  string_ext(const etl::string_ext& other, value_type* buffer, size_type buffer_size)
280  : istring(buffer, buffer_size - 1U)
281  {
282  this->assign(other);
283  }
284 
285  //*************************************************************************
288  //*************************************************************************
289  string_ext(const etl::istring& other, value_type* buffer, size_type buffer_size)
290  : istring(buffer, buffer_size - 1U)
291  {
292  this->assign(other);
293  }
294 
295  //*************************************************************************
300  //*************************************************************************
301  string_ext(const etl::istring& other, value_type* buffer, size_type buffer_size, size_type position, size_type length = npos)
302  : istring(buffer, buffer_size - 1U)
303  {
304  ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
305 
306  this->assign(other, position, length);
307  }
308 
309  //*************************************************************************
312  //*************************************************************************
313  string_ext(const char* text, char* buffer, size_type buffer_size)
314  : istring(buffer, buffer_size - 1U)
315  {
316  // Is the initial text at the same address as the buffer?
317  if (text == buffer)
318  {
319  this->current_size = etl::strlen(buffer);
320  }
321  else
322  {
323  this->assign(text, text + etl::strlen(text));
324  }
325  }
326 
327  //*************************************************************************
331  //*************************************************************************
332  string_ext(const value_type* text, size_type count, value_type* buffer, size_type buffer_size)
333  : istring(buffer, buffer_size - 1U)
334  {
335  this->assign(text, text + count);
336  }
337 
338  //*************************************************************************
342  //*************************************************************************
343  string_ext(size_type count, value_type c, value_type* buffer, size_type buffer_size)
344  : istring(buffer, buffer_size - 1U)
345  {
346  this->initialise();
347  this->resize(count, c);
348  }
349 
350  //*************************************************************************
355  //*************************************************************************
356  template <typename TIterator>
357  string_ext(TIterator first, TIterator last, value_type* buffer, size_type buffer_size)
358  : istring(buffer, buffer_size - 1U)
359  {
360  this->assign(first, last);
361  }
362 
363 #if ETL_CPP11_SUPPORTED && ETL_NOT_USING_STLPORT && ETL_USING_STL
364  //*************************************************************************
366  //*************************************************************************
367  string_ext(std::initializer_list<value_type> init, value_type* buffer, size_type buffer_size)
368  : istring(buffer, buffer_size - 1U)
369  {
370  this->assign(init.begin(), init.end());
371  }
372 #endif
373 
374  //*************************************************************************
377  //*************************************************************************
378  explicit string_ext(const etl::string_view& view, value_type* buffer, size_type buffer_size)
379  : istring(buffer, buffer_size - 1U)
380  {
381  this->assign(view.begin(), view.end());
382  }
383 
384  //*************************************************************************
386  //*************************************************************************
388  {
389  if (&rhs != this)
390  {
391  this->assign(rhs);
392  }
393 
394  return *this;
395  }
396 
397 
398  //*************************************************************************
400  //*************************************************************************
402  {
403  if (&rhs != this)
404  {
405  this->assign(rhs);
406  }
407 
408  return *this;
409  }
410 
411  //*************************************************************************
413  //*************************************************************************
414  string_ext& operator = (const value_type* text)
415  {
416  this->assign(text);
417 
418  return *this;
419  }
420 
421  //*************************************************************************
423  //*************************************************************************
424  void repair()
425 #ifdef ETL_ISTRING_REPAIR_ENABLE
426  ETL_OVERRIDE
427 #endif
428  {
429  }
430 
431  private:
432 
433  //*************************************************************************
435  //*************************************************************************
436  string_ext(const string_ext& other) ETL_DELETE;
437  };
438 
439  //*************************************************************************
441  //*************************************************************************
442 #if ETL_8BIT_SUPPORT
443  template <>
444  struct hash<etl::istring>
445  {
446  size_t operator()(const etl::istring& text) const
447  {
448  return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(&text[0]),
449  reinterpret_cast<const uint8_t*>(&text[text.size()]));
450  }
451  };
452 
453  template <const size_t SIZE>
454  struct hash<etl::string<SIZE> >
455  {
456  size_t operator()(const etl::string<SIZE>& text) const
457  {
458  return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(&text[0]),
459  reinterpret_cast<const uint8_t*>(&text[text.size()]));
460  }
461  };
462 
463  template <>
464  struct hash<etl::string_ext>
465  {
466  size_t operator()(const etl::string_ext& text) const
467  {
468  return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(&text[0]),
469  reinterpret_cast<const uint8_t*>(&text[text.size()]));
470  }
471  };
472 #endif
473 
474  //***************************************************************************
476  //***************************************************************************
477  template<size_t ARRAY_SIZE>
478  etl::string<ARRAY_SIZE - 1> make_string(const char(&text)[ARRAY_SIZE])
479  {
480  return etl::string<ARRAY_SIZE - 1>(text, ARRAY_SIZE - 1);
481  }
482 
483  //***************************************************************************
485  //***************************************************************************
486  template<const size_t MAX_SIZE, const size_t SIZE>
488  {
489  return etl::string<MAX_SIZE>(text, SIZE - 1);
490  }
491 }
492 
493 #include "private/minmax_pop.h"
494 
495 #endif
String view.
Definition: string_view.h:104
const_iterator begin() const
Returns a const iterator to the beginning of the array.
Definition: string_view.h:201
const_iterator end() const
Returns a const iterator to the end of the array.
Definition: string_view.h:217
Definition: basic_string.h:321
void resize(size_type new_size)
Definition: basic_string.h:451
void assign(const etl::ibasic_string< T > &other)
Definition: basic_string.h:590
void initialise()
Initialise the string.
Definition: basic_string.h:2201
void repair_buffer(T *p_buffer_)
Fix the internal pointers after a low level memory copy.
Definition: basic_string.h:2214
size_type length() const
Definition: basic_string.h:177
size_type current_size
The current number of elements in the string.
Definition: basic_string.h:306
size_type size() const
Definition: basic_string.h:168
Definition: string.h:257
string_ext(const etl::istring &other, value_type *buffer, size_type buffer_size, size_type position, size_type length=npos)
Definition: string.h:301
string_ext(value_type *buffer, size_type buffer_size)
Constructor.
Definition: string.h:269
string_ext(const etl::string_ext &other, value_type *buffer, size_type buffer_size)
Definition: string.h:279
string_ext(const etl::string_view &view, value_type *buffer, size_type buffer_size)
Definition: string.h:378
string_ext(const char *text, char *buffer, size_type buffer_size)
Definition: string.h:313
string_ext(size_type count, value_type c, value_type *buffer, size_type buffer_size)
Definition: string.h:343
string_ext(const etl::istring &other, value_type *buffer, size_type buffer_size)
Definition: string.h:289
string_ext & operator=(const string_ext &rhs)
Assignment operator.
Definition: string.h:387
void repair()
Fix the internal pointers after a low level memory copy.
Definition: string.h:424
string_ext(const value_type *text, size_type count, value_type *buffer, size_type buffer_size)
Definition: string.h:332
string_ext(TIterator first, TIterator last, value_type *buffer, size_type buffer_size)
Definition: string.h:357
Definition: basic_string.h:108
Definition: string.h:58
string & operator=(const string &rhs)
Assignment operator.
Definition: string.h:202
void repair()
Fix the internal pointers after a low level memory copy.
Definition: string.h:239
string()
Constructor.
Definition: string.h:71
etl::string< MAX_SIZE_ > substr(size_type position=0, size_type length_=npos) const
Definition: string.h:183
string(const etl::string_view &view)
Definition: string.h:172
string(TIterator first, TIterator last)
Definition: string.h:151
string(const etl::string< MAX_SIZE_ > &other)
Definition: string.h:81
string(const etl::istring &other)
Definition: string.h:91
string(const etl::istring &other, size_t position, size_t length=npos)
Definition: string.h:103
string(const value_type *text, size_t count)
Definition: string.h:126
string(size_type count, value_type c)
Definition: string.h:137
#define ETL_ASSERT(b, e)
Definition: error_handler.h:290
Definition: absolute.h:37
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:487
size_t strlen(const T *t)
Alternative strlen for all character types.
Definition: char_traits.h:247
etl::string< ARRAY_SIZE - 1 > make_string(const char(&text)[ARRAY_SIZE])
Hash function.
Definition: string.h:478
Character traits for any character type.
Definition: char_traits.h:97