Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
u16string.h
00001 ///\file 00002 00003 /****************************************************************************** 00004 The MIT License(MIT) 00005 00006 Embedded Template Library. 00007 https://github.com/ETLCPP/etl 00008 http://www.etlcpp.com 00009 00010 Copyright(c) 2016 jwellbelove 00011 00012 Permission is hereby granted, free of charge, to any person obtaining a copy 00013 of this software and associated documentation files(the "Software"), to deal 00014 in the Software without restriction, including without limitation the rights 00015 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell 00016 copies of the Software, and to permit persons to whom the Software is 00017 furnished to do so, subject to the following conditions : 00018 00019 The above copyright notice and this permission notice shall be included in all 00020 copies or substantial portions of the Software. 00021 00022 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00023 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00024 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE 00025 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00026 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00027 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00028 SOFTWARE. 00029 ******************************************************************************/ 00030 00031 #ifndef __ETL_U16STRING__ 00032 #define __ETL_U16STRING__ 00033 00034 #include "platform.h " 00035 #include "basic_string.h " 00036 #include "hash.h " 00037 00038 #if defined(ETL_COMPILER_MICROSOFT) 00039 #undef min 00040 #endif 00041 00042 namespace etl 00043 { 00044 typedef ibasic_string<char16_t> iu16string; 00045 00046 //*************************************************************************** 00047 /// A u16string implementation that uses a fixed size buffer. 00048 ///\tparam MAX_SIZE_ The maximum number of elements that can be stored. 00049 ///\ingroup u16string 00050 //*************************************************************************** 00051 template <const size_t MAX_SIZE_> 00052 class u16string : public iu16string 00053 { 00054 public: 00055 00056 typedef iu16string::value_type value_type; 00057 00058 static const size_t MAX_SIZE = MAX_SIZE_; 00059 00060 //************************************************************************* 00061 /// Constructor. 00062 //************************************************************************* 00063 u16string() 00064 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE) 00065 { 00066 iu16string::initialise(); 00067 } 00068 00069 //************************************************************************* 00070 /// Copy constructor. 00071 ///\param other The other string. 00072 //************************************************************************* 00073 u16string(const etl::u16string<MAX_SIZE_>& other) 00074 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE) 00075 { 00076 iu16string::initialise(); 00077 iu16string::assign(other.begin(), other.end()); 00078 } 00079 00080 //************************************************************************* 00081 /// From other string, position, length. 00082 ///\param other The other string. 00083 ///\param position The position of the first character. 00084 ///\param length The number of characters. Default = npos. 00085 //************************************************************************* 00086 u16string(const etl::u16string<MAX_SIZE_>& other, size_t position, size_t length_ = npos) 00087 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE) 00088 { 00089 ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds)); 00090 00091 // Set the length to the exact amount. 00092 length_ = (length_ > MAX_SIZE_) ? MAX_SIZE_ : length_; 00093 00094 iu16string::initialise(); 00095 iu16string::assign(other.begin() + position, other.begin() + position + length_); 00096 } 00097 00098 //************************************************************************* 00099 /// Constructor, from null terminated text. 00100 ///\param text The initial text of the u16string. 00101 //************************************************************************* 00102 u16string(const value_type* text) 00103 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE) 00104 { 00105 iu16string::initialise(); 00106 iu16string::assign(text, text + etl::char_traits<value_type>::length(text)); 00107 } 00108 00109 //************************************************************************* 00110 /// Constructor, from null terminated text and count. 00111 ///\param text The initial text of the u16string. 00112 ///\param count The number of characters to copy. 00113 //************************************************************************* 00114 u16string(const value_type* text, size_t count) 00115 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE) 00116 { 00117 iu16string::initialise(); 00118 iu16string::assign(text, text + count); 00119 } 00120 00121 //************************************************************************* 00122 /// Constructor, from initial size and value. 00123 ///\param initialSize The initial size of the u16string. 00124 ///\param value The value to fill the u16string with. 00125 //************************************************************************* 00126 u16string(size_t count, value_type c) 00127 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE) 00128 { 00129 iu16string::initialise(); 00130 iu16string::resize(count, c); 00131 } 00132 00133 //************************************************************************* 00134 /// Constructor, from an iterator range. 00135 ///\tparam TIterator The iterator type. 00136 ///\param first The iterator to the first element. 00137 ///\param last The iterator to the last element + 1. 00138 //************************************************************************* 00139 template <typename TIterator> 00140 u16string(TIterator first, TIterator last) 00141 : iu16string(reinterpret_cast<value_type*>(&buffer), MAX_SIZE) 00142 { 00143 iu16string::assign(first, last); 00144 } 00145 00146 //************************************************************************* 00147 /// Returns a sub-string. 00148 ///\param position The position of the first character. Default = 0. 00149 ///\param length The number of characters. Default = npos. 00150 //************************************************************************* 00151 etl::u16string<MAX_SIZE_> substr(size_t position = 0, size_t length_ = npos) const 00152 { 00153 etl::u16string<MAX_SIZE_> new_string; 00154 00155 if (position != size()) 00156 { 00157 ETL_ASSERT(position < size(), ETL_ERROR(string_out_of_bounds)); 00158 00159 length_ = std::min(length_, size() - position); 00160 00161 new_string.assign(buffer + position, buffer + position + length_); 00162 } 00163 00164 return new_string; 00165 } 00166 00167 //************************************************************************* 00168 /// Assignment operator. 00169 //************************************************************************* 00170 u16string& operator = (const u16string& rhs) 00171 { 00172 if (&rhs != this) 00173 { 00174 iu16string::assign(rhs.cbegin(), rhs.cend()); 00175 } 00176 00177 return *this; 00178 } 00179 00180 //************************************************************************* 00181 /// Fix the internal pointers after a low level memory copy. 00182 //************************************************************************* 00183 void repair() 00184 { 00185 etl::iu16string::repair(buffer); 00186 } 00187 00188 private: 00189 00190 value_type buffer[MAX_SIZE + 1]; 00191 }; 00192 00193 //************************************************************************* 00194 /// Hash function. 00195 //************************************************************************* 00196 #if ETL_8BIT_SUPPORT 00197 template <> 00198 struct hash<etl::iu16string> 00199 { 00200 size_t operator()(const etl::iu16string& text) const 00201 { 00202 return etl::__private_hash__::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(&text[0]), 00203 reinterpret_cast<const uint8_t*>(&text[text.size()])); 00204 } 00205 }; 00206 00207 template <const size_t SIZE> 00208 struct hash<etl::u16string<SIZE> > 00209 { 00210 size_t operator()(const etl::u16string<SIZE>& text) const 00211 { 00212 return etl::__private_hash__::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(&text[0]), 00213 reinterpret_cast<const uint8_t*>(&text[text.size()])); 00214 } 00215 }; 00216 #endif 00217 } 00218 00219 #if defined(ETL_COMPILER_MICROSOFT) 00220 #define min(a,b) (((a) < (b)) ? (a) : (b)) 00221 #endif 00222 00223 #endif 00224
Generated on Tue Jul 12 2022 14:05:45 by
