Stefan Scholz / ETL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers wstring.h Source File

wstring.h

Go to the documentation of this file.
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_WSTRING__
00032 #define __ETL_WSTRING__
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<wchar_t> iwstring;
00045 
00046   //***************************************************************************
00047   /// A wstring implementation that uses a fixed size buffer.
00048   ///\tparam MAX_SIZE_ The maximum number of elements that can be stored.
00049   ///\ingroup wstring
00050   //***************************************************************************
00051   template <const size_t MAX_SIZE_>
00052   class wstring : public iwstring
00053   {
00054   public:
00055 
00056     typedef iwstring::value_type value_type;
00057 
00058     static const size_t MAX_SIZE = MAX_SIZE_;
00059 
00060     //*************************************************************************
00061     /// Constructor.
00062     //*************************************************************************
00063     wstring()
00064       : iwstring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
00065     {
00066       iwstring::initialise();
00067     }
00068 
00069     //*************************************************************************
00070     /// Copy constructor.
00071     ///\param other The other string.
00072     //*************************************************************************
00073     wstring(const etl::wstring<MAX_SIZE_>& other)
00074       : iwstring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
00075     {
00076       wstring::initialise();
00077       wstring::assign(other.begin(), other.end());
00078     }
00079 
00080 
00081     //*************************************************************************
00082     /// From other string, position, length.
00083     ///\param other The other string.
00084     ///\param position The position of the first character.
00085     ///\param length   The number of characters. Default = npos.
00086     //*************************************************************************
00087     wstring(const etl::wstring<MAX_SIZE_>& other, size_t position, size_t length_ = npos)
00088       : iwstring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
00089     {
00090       ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
00091 
00092       // Set the length to the exact amount.
00093       length_ = (length_ > MAX_SIZE_) ? MAX_SIZE_ : length_;
00094 
00095       iwstring::initialise();
00096       iwstring::assign(other.begin() + position, other.begin() + position + length_);
00097     }
00098 
00099     //*************************************************************************
00100     /// Constructor, from null terminated text.
00101     ///\param text The initial text of the wstring.
00102     //*************************************************************************
00103     wstring(const value_type* text)
00104       : iwstring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
00105     {
00106       iwstring::initialise();
00107       iwstring::assign(text, text + etl::char_traits<value_type>::length(text));
00108     }
00109 
00110     //*************************************************************************
00111     /// Constructor, from null terminated text and count.
00112     ///\param text  The initial text of the wstring.
00113     ///\param count The number of characters to copy.
00114     //*************************************************************************
00115     wstring(const value_type* text, size_t count)
00116       : iwstring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
00117     {
00118       iwstring::initialise();
00119       iwstring::assign(text, text + count);
00120     }
00121 
00122     //*************************************************************************
00123     /// Constructor, from initial size and value.
00124     ///\param initialSize  The initial size of the wstring.
00125     ///\param value        The value to fill the wstring with.
00126     //*************************************************************************
00127     wstring(size_t count, value_type c)
00128       : iwstring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
00129     {
00130       iwstring::initialise();
00131       iwstring::resize(count, c);
00132     }
00133 
00134     //*************************************************************************
00135     /// Constructor, from an iterator range.
00136     ///\tparam TIterator The iterator type.
00137     ///\param first The iterator to the first element.
00138     ///\param last  The iterator to the last element + 1.
00139     //*************************************************************************
00140     template <typename TIterator>
00141     wstring(TIterator first, TIterator last)
00142       : iwstring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
00143     {
00144       iwstring::assign(first, last);
00145     }
00146 
00147     //*************************************************************************
00148     /// Returns a sub-string.
00149     ///\param position The position of the first character. Default = 0.
00150     ///\param length   The number of characters. Default = npos.
00151     //*************************************************************************
00152     etl::wstring<MAX_SIZE_> substr(size_t position = 0, size_t length_ = npos) const
00153     {
00154       etl::wstring<MAX_SIZE_> new_string;
00155 
00156       if (position != size())
00157       {
00158         ETL_ASSERT(position < size(), ETL_ERROR(string_out_of_bounds));
00159 
00160         length_ = std::min(length_, size() - position);
00161 
00162         new_string.assign(buffer + position, buffer + position + length_);
00163       }
00164 
00165       return new_string;
00166     }
00167 
00168     //*************************************************************************
00169     /// Assignment operator.
00170     //*************************************************************************
00171     wstring& operator = (const wstring& rhs)
00172     {
00173       if (&rhs != this)
00174       {
00175         iwstring::assign(rhs.cbegin(), rhs.cend());
00176       }
00177 
00178       return *this;
00179     }
00180 
00181     //*************************************************************************
00182     /// Fix the internal pointers after a low level memory copy.
00183     //*************************************************************************
00184     void repair()
00185     {
00186       etl::iwstring::repair(buffer);
00187     }
00188 
00189   private:
00190 
00191     value_type buffer[MAX_SIZE + 1];
00192   };
00193 
00194   //*************************************************************************
00195   /// Hash function.
00196   //*************************************************************************
00197 #if ETL_8BIT_SUPPORT
00198   template <>
00199   struct hash<etl::iwstring>
00200   {
00201     size_t operator()(const etl::iwstring& text) const
00202     {
00203       return etl::__private_hash__::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(&text[0]),
00204                                                          reinterpret_cast<const uint8_t*>(&text[text.size()]));
00205     }
00206   };
00207 
00208   template <const size_t SIZE>
00209   struct hash<etl::wstring<SIZE> >
00210   {
00211     size_t operator()(const etl::wstring<SIZE>& text) const
00212     {
00213       return etl::__private_hash__::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(&text[0]),
00214                                                          reinterpret_cast<const uint8_t*>(&text[text.size()]));
00215     }
00216   };
00217 #endif
00218 }
00219 
00220 #if defined(ETL_COMPILER_MICROSOFT)
00221   #define min(a,b) (((a) < (b)) ? (a) : (b))
00222 #endif
00223 
00224 #endif
00225