Stefan Scholz / ETL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cstring.h Source File

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