Embedded Template Library  1.0
u16string.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_U16STRING_INCLUDED
32 #define ETL_U16STRING_INCLUDED
33 
34 #include "platform.h"
35 #include "basic_string.h"
36 #include "string_view.h"
37 #include "hash.h"
38 
39 #if ETL_CPP11_SUPPORTED && ETL_NOT_USING_STLPORT && ETL_USING_STL
40  #include <initializer_list>
41 #endif
42 
43 #include "private/minmax_push.h"
44 
45 namespace etl
46 {
47  typedef ibasic_string<char16_t> iu16string;
48 
49  //***************************************************************************
53  //***************************************************************************
54  template <const size_t MAX_SIZE_>
55  class u16string : public iu16string
56  {
57  public:
58 
59  typedef iu16string base_type;
60  typedef iu16string interface_type;
61 
62  typedef iu16string::value_type value_type;
63 
64  static const size_t MAX_SIZE = MAX_SIZE_;
65 
66  //*************************************************************************
68  //*************************************************************************
70  : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
71  {
72  this->initialise();
73  }
74 
75  //*************************************************************************
78  //*************************************************************************
80  : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
81  {
82  this->assign(other);
83  }
84 
85  //*************************************************************************
88  //*************************************************************************
89  u16string(const etl::iu16string& other)
90  : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
91  {
92  this->assign(other);
93  }
94 
95  //*************************************************************************
100  //*************************************************************************
101  u16string(const etl::iu16string& other, size_type position, size_type length = npos)
102  : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
103  {
104  ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
105 
106  this->assign(other, position, length);
107  }
108 
109  //*************************************************************************
112  //*************************************************************************
113  ETL_EXPLICIT_STRING_FROM_CHAR u16string(const value_type* text)
114  : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
115  {
116  this->assign(text, text + etl::char_traits<value_type>::length(text));
117  }
118 
119  //*************************************************************************
123  //*************************************************************************
124  u16string(const value_type* text, size_type count)
125  : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
126  {
127  this->assign(text, text + count);
128  }
129 
130  //*************************************************************************
134  //*************************************************************************
135  u16string(size_type count, value_type c)
136  : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
137  {
138  this->initialise();
139  this->resize(count, c);
140  }
141 
142  //*************************************************************************
147  //*************************************************************************
148  template <typename TIterator>
149  u16string(TIterator first, TIterator last)
150  : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
151  {
152  this->assign(first, last);
153  }
154 
155 #if ETL_CPP11_SUPPORTED && ETL_NOT_USING_STLPORT && ETL_USING_STL
156  //*************************************************************************
158  //*************************************************************************
159  u16string(std::initializer_list<value_type> init)
160  : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
161  {
162  this->assign(init.begin(), init.end());
163  }
164 #endif
165 
166  //*************************************************************************
169  //*************************************************************************
170  explicit u16string(const etl::u16string_view& view)
171  : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
172  {
173  this->assign(view.begin(), view.end());
174  }
175 
176  //*************************************************************************
180  //*************************************************************************
181  etl::u16string<MAX_SIZE_> substr(size_type position = 0, size_type length_ = npos) const
182  {
183  etl::u16string<MAX_SIZE_> new_string;
184 
185  if (position != size())
186  {
187  ETL_ASSERT(position < size(), ETL_ERROR(string_out_of_bounds));
188 
189  length_ = etl::min(length_, size() - position);
190 
191  new_string.assign(buffer + position, buffer + position + length_);
192  }
193 
194  return new_string;
195  }
196 
197  //*************************************************************************
199  //*************************************************************************
201  {
202  if (&rhs != this)
203  {
204  this->assign(rhs);
205  }
206 
207  return *this;
208  }
209 
210  //*************************************************************************
212  //*************************************************************************
213  u16string& operator = (const value_type* text)
214  {
215  this->assign(text);
216 
217  return *this;
218  }
219 
220  //*************************************************************************
222  //*************************************************************************
223  void repair()
224 #ifdef ETL_ISTRING_REPAIR_ENABLE
225  ETL_OVERRIDE
226 #endif
227  {
229  }
230 
231  private:
232 
233  value_type buffer[MAX_SIZE + 1];
234  };
235 
236  //***************************************************************************
239  //***************************************************************************
240  class u16string_ext : public iu16string
241  {
242  public:
243 
244  typedef iu16string base_type;
245  typedef iu16string interface_type;
246 
247  typedef iu16string::value_type value_type;
248 
249  //*************************************************************************
251  //*************************************************************************
252  u16string_ext(value_type* buffer, size_type buffer_size)
253  : iu16string(buffer, buffer_size - 1U)
254  {
255  this->initialise();
256  }
257 
258  //*************************************************************************
261  //*************************************************************************
262  u16string_ext(const etl::u16string_ext& other, value_type* buffer, size_type buffer_size)
263  : iu16string(buffer, buffer_size - 1U)
264  {
265  this->assign(other);
266  }
267 
268  //*************************************************************************
271  //*************************************************************************
272  u16string_ext(const etl::iu16string& other, value_type* buffer, size_type buffer_size)
273  : iu16string(buffer, buffer_size - 1U)
274  {
275  this->assign(other);
276  }
277 
278  //*************************************************************************
283  //*************************************************************************
284  u16string_ext(const etl::iu16string& other, value_type* buffer, size_type buffer_size, size_type position, size_type length = npos)
285  : iu16string(buffer, buffer_size - 1U)
286  {
287  ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
288 
289  this->assign(other, position, length);
290  }
291 
292  //*************************************************************************
295  //*************************************************************************
296  u16string_ext(const value_type* text, value_type* buffer, size_type buffer_size)
297  : iu16string(buffer, buffer_size - 1U)
298  {
299  // Is the initial text at the same address as the buffer?
300  if (text == buffer)
301  {
302  this->current_size = etl::strlen(buffer);
303  }
304  else
305  {
306  this->assign(text, text + etl::strlen(text));
307  }
308  }
309 
310  //*************************************************************************
314  //*************************************************************************
315  u16string_ext(const value_type* text, size_type count, value_type* buffer, size_type buffer_size)
316  : iu16string(buffer, buffer_size - 1U)
317  {
318  this->assign(text, text + count);
319  }
320 
321  //*************************************************************************
325  //*************************************************************************
326  u16string_ext(size_type count, value_type c, value_type* buffer, size_type buffer_size)
327  : iu16string(buffer, buffer_size - 1U)
328  {
329  this->initialise();
330  this->resize(count, c);
331  }
332 
333  //*************************************************************************
338  //*************************************************************************
339  template <typename TIterator>
340  u16string_ext(TIterator first, TIterator last, value_type* buffer, size_type buffer_size)
341  : iu16string(buffer, buffer_size - 1U)
342  {
343  this->assign(first, last);
344  }
345 
346 #if ETL_CPP11_SUPPORTED && ETL_NOT_USING_STLPORT && ETL_USING_STL
347  //*************************************************************************
349  //*************************************************************************
350  u16string_ext(std::initializer_list<value_type> init, value_type* buffer, size_type buffer_size)
351  : iu16string(buffer, buffer_size - 1U)
352  {
353  this->assign(init.begin(), init.end());
354  }
355 #endif
356 
357  //*************************************************************************
360  //*************************************************************************
361  explicit u16string_ext(const etl::u16string_view& view, value_type* buffer, size_type buffer_size)
362  : iu16string(buffer, buffer_size - 1U)
363  {
364  this->assign(view.begin(), view.end());
365  }
366 
367  //*************************************************************************
369  //*************************************************************************
371  {
372  if (&rhs != this)
373  {
374  this->assign(rhs);
375  }
376 
377  return *this;
378  }
379 
380 
381  //*************************************************************************
383  //*************************************************************************
385  {
386  if (&rhs != this)
387  {
388  this->assign(rhs);
389  }
390 
391  return *this;
392  }
393 
394  //*************************************************************************
396  //*************************************************************************
397  u16string_ext& operator = (const value_type* text)
398  {
399  this->assign(text);
400 
401  return *this;
402  }
403 
404  //*************************************************************************
406  //*************************************************************************
407  void repair()
408 #ifdef ETL_ISTRING_REPAIR_ENABLE
409  ETL_OVERRIDE
410 #endif
411  {
412  }
413 
414  private:
415 
416  //*************************************************************************
418  //*************************************************************************
419  u16string_ext(const u16string_ext& other) ETL_DELETE;
420  };
421 
422  //*************************************************************************
424  //*************************************************************************
425 #if ETL_8BIT_SUPPORT
426  template <>
427  struct hash<etl::iu16string>
428  {
429  size_t operator()(const etl::iu16string& text) const
430  {
431  return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(&text[0]),
432  reinterpret_cast<const uint8_t*>(&text[text.size()]));
433  }
434  };
435 
436  template <const size_t SIZE>
437  struct hash<etl::u16string<SIZE> >
438  {
439  size_t operator()(const etl::u16string<SIZE>& text) const
440  {
441  return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(&text[0]),
442  reinterpret_cast<const uint8_t*>(&text[text.size()]));
443  }
444  };
445 
446  template <>
447  struct hash<etl::u16string_ext >
448  {
449  size_t operator()(const etl::u16string_ext& text) const
450  {
451  return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(&text[0]),
452  reinterpret_cast<const uint8_t*>(&text[text.size()]));
453  }
454  };
455 #endif
456 
457  //***************************************************************************
459  //***************************************************************************
460  template<size_t ARRAY_SIZE>
461  etl::u16string<ARRAY_SIZE == 1 ? 1 : ARRAY_SIZE - 1> make_string(const char16_t(&text)[ARRAY_SIZE])
462  {
463  return etl::u16string<ARRAY_SIZE == 1 ? 1 : ARRAY_SIZE - 1>(text, ARRAY_SIZE - 1);
464  }
465 
466  //***************************************************************************
468  //***************************************************************************
469  template<const size_t MAX_SIZE, const size_t SIZE>
471  {
472  return etl::u16string<MAX_SIZE>(text, SIZE - 1);
473  }
474 }
475 
476 #include "private/minmax_pop.h"
477 
478 #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: basic_string.h:108
Definition: u16string.h:241
u16string_ext(size_type count, value_type c, value_type *buffer, size_type buffer_size)
Definition: u16string.h:326
u16string_ext(value_type *buffer, size_type buffer_size)
Constructor.
Definition: u16string.h:252
u16string_ext(const etl::u16string_view &view, value_type *buffer, size_type buffer_size)
Definition: u16string.h:361
u16string_ext(const value_type *text, size_type count, value_type *buffer, size_type buffer_size)
Definition: u16string.h:315
u16string_ext(const etl::iu16string &other, value_type *buffer, size_type buffer_size, size_type position, size_type length=npos)
Definition: u16string.h:284
u16string_ext(const etl::iu16string &other, value_type *buffer, size_type buffer_size)
Definition: u16string.h:272
u16string_ext(TIterator first, TIterator last, value_type *buffer, size_type buffer_size)
Definition: u16string.h:340
void repair()
Fix the internal pointers after a low level memory copy.
Definition: u16string.h:407
u16string_ext(const value_type *text, value_type *buffer, size_type buffer_size)
Definition: u16string.h:296
u16string_ext(const etl::u16string_ext &other, value_type *buffer, size_type buffer_size)
Definition: u16string.h:262
u16string_ext & operator=(const u16string_ext &rhs)
Assignment operator.
Definition: u16string.h:370
Definition: u16string.h:56
u16string(const etl::iu16string &other)
Definition: u16string.h:89
etl::u16string< MAX_SIZE_ > substr(size_type position=0, size_type length_=npos) const
Definition: u16string.h:181
u16string(const etl::iu16string &other, size_type position, size_type length=npos)
Definition: u16string.h:101
u16string(size_type count, value_type c)
Definition: u16string.h:135
u16string & operator=(const u16string &rhs)
Assignment operator.
Definition: u16string.h:200
u16string(const etl::u16string_view &view)
Definition: u16string.h:170
u16string(const etl::u16string< MAX_SIZE_ > &other)
Definition: u16string.h:79
u16string()
Constructor.
Definition: u16string.h:69
u16string(const value_type *text, size_type count)
Definition: u16string.h:124
void repair()
Fix the internal pointers after a low level memory copy.
Definition: u16string.h:223
u16string(TIterator first, TIterator last)
Definition: u16string.h:149
#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