Stefan Scholz / ETL
Committer:
bobbery
Date:
Fri Mar 16 16:34:18 2018 +0000
Revision:
0:b47c2a7920c2
Works after using gcc_generic undef CAPACITY and replacing nullptr by std::nullptr

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bobbery 0:b47c2a7920c2 1 ///\file
bobbery 0:b47c2a7920c2 2
bobbery 0:b47c2a7920c2 3 /******************************************************************************
bobbery 0:b47c2a7920c2 4 The MIT License(MIT)
bobbery 0:b47c2a7920c2 5
bobbery 0:b47c2a7920c2 6 Embedded Template Library.
bobbery 0:b47c2a7920c2 7 https://github.com/ETLCPP/etl
bobbery 0:b47c2a7920c2 8 http://www.etlcpp.com
bobbery 0:b47c2a7920c2 9
bobbery 0:b47c2a7920c2 10 Copyright(c) 2016 jwellbelove
bobbery 0:b47c2a7920c2 11
bobbery 0:b47c2a7920c2 12 Permission is hereby granted, free of charge, to any person obtaining a copy
bobbery 0:b47c2a7920c2 13 of this software and associated documentation files(the "Software"), to deal
bobbery 0:b47c2a7920c2 14 in the Software without restriction, including without limitation the rights
bobbery 0:b47c2a7920c2 15 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
bobbery 0:b47c2a7920c2 16 copies of the Software, and to permit persons to whom the Software is
bobbery 0:b47c2a7920c2 17 furnished to do so, subject to the following conditions :
bobbery 0:b47c2a7920c2 18
bobbery 0:b47c2a7920c2 19 The above copyright notice and this permission notice shall be included in all
bobbery 0:b47c2a7920c2 20 copies or substantial portions of the Software.
bobbery 0:b47c2a7920c2 21
bobbery 0:b47c2a7920c2 22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
bobbery 0:b47c2a7920c2 23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
bobbery 0:b47c2a7920c2 24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
bobbery 0:b47c2a7920c2 25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
bobbery 0:b47c2a7920c2 26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
bobbery 0:b47c2a7920c2 27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
bobbery 0:b47c2a7920c2 28 SOFTWARE.
bobbery 0:b47c2a7920c2 29 ******************************************************************************/
bobbery 0:b47c2a7920c2 30
bobbery 0:b47c2a7920c2 31 #ifndef __ETL_STRING__
bobbery 0:b47c2a7920c2 32 #define __ETL_STRING__
bobbery 0:b47c2a7920c2 33
bobbery 0:b47c2a7920c2 34 #include "platform.h"
bobbery 0:b47c2a7920c2 35 #include "basic_string.h"
bobbery 0:b47c2a7920c2 36 #include "hash.h"
bobbery 0:b47c2a7920c2 37
bobbery 0:b47c2a7920c2 38 #if defined(ETL_COMPILER_MICROSOFT)
bobbery 0:b47c2a7920c2 39 #undef min
bobbery 0:b47c2a7920c2 40 #endif
bobbery 0:b47c2a7920c2 41
bobbery 0:b47c2a7920c2 42 namespace etl
bobbery 0:b47c2a7920c2 43 {
bobbery 0:b47c2a7920c2 44 typedef etl::ibasic_string<char> istring;
bobbery 0:b47c2a7920c2 45
bobbery 0:b47c2a7920c2 46 //***************************************************************************
bobbery 0:b47c2a7920c2 47 /// A string implementation that uses a fixed size buffer.
bobbery 0:b47c2a7920c2 48 ///\tparam MAX_SIZE_ The maximum number of elements that can be stored.
bobbery 0:b47c2a7920c2 49 ///\ingroup string
bobbery 0:b47c2a7920c2 50 //***************************************************************************
bobbery 0:b47c2a7920c2 51 template <const size_t MAX_SIZE_>
bobbery 0:b47c2a7920c2 52 class string : public istring
bobbery 0:b47c2a7920c2 53 {
bobbery 0:b47c2a7920c2 54 public:
bobbery 0:b47c2a7920c2 55
bobbery 0:b47c2a7920c2 56 typedef istring::value_type value_type;
bobbery 0:b47c2a7920c2 57
bobbery 0:b47c2a7920c2 58 static const size_t MAX_SIZE = MAX_SIZE_;
bobbery 0:b47c2a7920c2 59
bobbery 0:b47c2a7920c2 60 //*************************************************************************
bobbery 0:b47c2a7920c2 61 /// Constructor.
bobbery 0:b47c2a7920c2 62 //*************************************************************************
bobbery 0:b47c2a7920c2 63 string()
bobbery 0:b47c2a7920c2 64 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
bobbery 0:b47c2a7920c2 65 {
bobbery 0:b47c2a7920c2 66 istring::initialise();
bobbery 0:b47c2a7920c2 67 }
bobbery 0:b47c2a7920c2 68
bobbery 0:b47c2a7920c2 69 //*************************************************************************
bobbery 0:b47c2a7920c2 70 /// Copy constructor.
bobbery 0:b47c2a7920c2 71 ///\param other The other string.
bobbery 0:b47c2a7920c2 72 //*************************************************************************
bobbery 0:b47c2a7920c2 73 string(const etl::string<MAX_SIZE_>& other)
bobbery 0:b47c2a7920c2 74 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
bobbery 0:b47c2a7920c2 75 {
bobbery 0:b47c2a7920c2 76 istring::assign(other.begin(), other.end());
bobbery 0:b47c2a7920c2 77 }
bobbery 0:b47c2a7920c2 78
bobbery 0:b47c2a7920c2 79 //*************************************************************************
bobbery 0:b47c2a7920c2 80 /// From other string, position, length.
bobbery 0:b47c2a7920c2 81 ///\param other The other string.
bobbery 0:b47c2a7920c2 82 ///\param position The position of the first character.
bobbery 0:b47c2a7920c2 83 ///\param length The number of characters. Default = npos.
bobbery 0:b47c2a7920c2 84 //*************************************************************************
bobbery 0:b47c2a7920c2 85 string(const etl::string<MAX_SIZE_>& other, size_t position, size_t length_ = npos)
bobbery 0:b47c2a7920c2 86 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
bobbery 0:b47c2a7920c2 87 {
bobbery 0:b47c2a7920c2 88 ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
bobbery 0:b47c2a7920c2 89
bobbery 0:b47c2a7920c2 90 // Set the length to the exact amount.
bobbery 0:b47c2a7920c2 91 length_ = (length_ > MAX_SIZE_) ? MAX_SIZE_ : length_;
bobbery 0:b47c2a7920c2 92
bobbery 0:b47c2a7920c2 93 istring::assign(other.begin() + position, other.begin() + position + length_);
bobbery 0:b47c2a7920c2 94 }
bobbery 0:b47c2a7920c2 95
bobbery 0:b47c2a7920c2 96 //*************************************************************************
bobbery 0:b47c2a7920c2 97 /// Constructor, from null terminated text.
bobbery 0:b47c2a7920c2 98 ///\param text The initial text of the string.
bobbery 0:b47c2a7920c2 99 //*************************************************************************
bobbery 0:b47c2a7920c2 100 string(const value_type* text)
bobbery 0:b47c2a7920c2 101 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
bobbery 0:b47c2a7920c2 102 {
bobbery 0:b47c2a7920c2 103 istring::assign(text, text + etl::char_traits<value_type>::length(text));
bobbery 0:b47c2a7920c2 104 }
bobbery 0:b47c2a7920c2 105
bobbery 0:b47c2a7920c2 106 //*************************************************************************
bobbery 0:b47c2a7920c2 107 /// Constructor, from null terminated text and count.
bobbery 0:b47c2a7920c2 108 ///\param text The initial text of the string.
bobbery 0:b47c2a7920c2 109 ///\param count The number of characters to copy.
bobbery 0:b47c2a7920c2 110 //*************************************************************************
bobbery 0:b47c2a7920c2 111 string(const value_type* text, size_t count)
bobbery 0:b47c2a7920c2 112 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
bobbery 0:b47c2a7920c2 113 {
bobbery 0:b47c2a7920c2 114 istring::assign(text, text + count);
bobbery 0:b47c2a7920c2 115 }
bobbery 0:b47c2a7920c2 116
bobbery 0:b47c2a7920c2 117 //*************************************************************************
bobbery 0:b47c2a7920c2 118 /// Constructor, from initial size and value.
bobbery 0:b47c2a7920c2 119 ///\param initialSize The initial size of the string.
bobbery 0:b47c2a7920c2 120 ///\param value The value to fill the string with.
bobbery 0:b47c2a7920c2 121 //*************************************************************************
bobbery 0:b47c2a7920c2 122 string(size_t count, value_type c)
bobbery 0:b47c2a7920c2 123 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
bobbery 0:b47c2a7920c2 124 {
bobbery 0:b47c2a7920c2 125 istring::initialise();
bobbery 0:b47c2a7920c2 126 istring::resize(count, c);
bobbery 0:b47c2a7920c2 127 }
bobbery 0:b47c2a7920c2 128
bobbery 0:b47c2a7920c2 129 //*************************************************************************
bobbery 0:b47c2a7920c2 130 /// Constructor, from an iterator range.
bobbery 0:b47c2a7920c2 131 ///\tparam TIterator The iterator type.
bobbery 0:b47c2a7920c2 132 ///\param first The iterator to the first element.
bobbery 0:b47c2a7920c2 133 ///\param last The iterator to the last element + 1.
bobbery 0:b47c2a7920c2 134 //*************************************************************************
bobbery 0:b47c2a7920c2 135 template <typename TIterator>
bobbery 0:b47c2a7920c2 136 string(TIterator first, TIterator last)
bobbery 0:b47c2a7920c2 137 : istring(reinterpret_cast<value_type*>(&buffer), MAX_SIZE)
bobbery 0:b47c2a7920c2 138 {
bobbery 0:b47c2a7920c2 139 istring::assign(first, last);
bobbery 0:b47c2a7920c2 140 }
bobbery 0:b47c2a7920c2 141
bobbery 0:b47c2a7920c2 142 //*************************************************************************
bobbery 0:b47c2a7920c2 143 /// Returns a sub-string.
bobbery 0:b47c2a7920c2 144 ///\param position The position of the first character. Default = 0.
bobbery 0:b47c2a7920c2 145 ///\param length The number of characters. Default = npos.
bobbery 0:b47c2a7920c2 146 //*************************************************************************
bobbery 0:b47c2a7920c2 147 etl::string<MAX_SIZE_> substr(size_t position = 0, size_t length_ = npos) const
bobbery 0:b47c2a7920c2 148 {
bobbery 0:b47c2a7920c2 149 etl::string<MAX_SIZE_> new_string;
bobbery 0:b47c2a7920c2 150
bobbery 0:b47c2a7920c2 151 if (position != this->size())
bobbery 0:b47c2a7920c2 152 {
bobbery 0:b47c2a7920c2 153 ETL_ASSERT(position < this->size(), ETL_ERROR(string_out_of_bounds));
bobbery 0:b47c2a7920c2 154
bobbery 0:b47c2a7920c2 155 length_ = std::min(length_, this->size() - position);
bobbery 0:b47c2a7920c2 156
bobbery 0:b47c2a7920c2 157 new_string.assign(buffer + position, buffer + position + length_);
bobbery 0:b47c2a7920c2 158 }
bobbery 0:b47c2a7920c2 159
bobbery 0:b47c2a7920c2 160 return new_string;
bobbery 0:b47c2a7920c2 161 }
bobbery 0:b47c2a7920c2 162
bobbery 0:b47c2a7920c2 163 //*************************************************************************
bobbery 0:b47c2a7920c2 164 /// Assignment operator.
bobbery 0:b47c2a7920c2 165 //*************************************************************************
bobbery 0:b47c2a7920c2 166 string& operator = (const string& rhs)
bobbery 0:b47c2a7920c2 167 {
bobbery 0:b47c2a7920c2 168 if (&rhs != this)
bobbery 0:b47c2a7920c2 169 {
bobbery 0:b47c2a7920c2 170 istring::assign(rhs.cbegin(), rhs.cend());
bobbery 0:b47c2a7920c2 171 }
bobbery 0:b47c2a7920c2 172
bobbery 0:b47c2a7920c2 173 return *this;
bobbery 0:b47c2a7920c2 174 }
bobbery 0:b47c2a7920c2 175
bobbery 0:b47c2a7920c2 176 //*************************************************************************
bobbery 0:b47c2a7920c2 177 /// Fix the internal pointers after a low level memory copy.
bobbery 0:b47c2a7920c2 178 //*************************************************************************
bobbery 0:b47c2a7920c2 179 void repair()
bobbery 0:b47c2a7920c2 180 {
bobbery 0:b47c2a7920c2 181 etl::istring::repair(buffer);
bobbery 0:b47c2a7920c2 182 }
bobbery 0:b47c2a7920c2 183
bobbery 0:b47c2a7920c2 184 private:
bobbery 0:b47c2a7920c2 185
bobbery 0:b47c2a7920c2 186 value_type buffer[MAX_SIZE + 1];
bobbery 0:b47c2a7920c2 187 };
bobbery 0:b47c2a7920c2 188
bobbery 0:b47c2a7920c2 189 //*************************************************************************
bobbery 0:b47c2a7920c2 190 /// Hash function.
bobbery 0:b47c2a7920c2 191 //*************************************************************************
bobbery 0:b47c2a7920c2 192 #if ETL_8BIT_SUPPORT
bobbery 0:b47c2a7920c2 193 template <>
bobbery 0:b47c2a7920c2 194 struct hash<etl::istring>
bobbery 0:b47c2a7920c2 195 {
bobbery 0:b47c2a7920c2 196 size_t operator()(const etl::istring& text) const
bobbery 0:b47c2a7920c2 197 {
bobbery 0:b47c2a7920c2 198 return etl::__private_hash__::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(&text[0]),
bobbery 0:b47c2a7920c2 199 reinterpret_cast<const uint8_t*>(&text[text.size()]));
bobbery 0:b47c2a7920c2 200 }
bobbery 0:b47c2a7920c2 201 };
bobbery 0:b47c2a7920c2 202
bobbery 0:b47c2a7920c2 203 template <const size_t SIZE>
bobbery 0:b47c2a7920c2 204 struct hash<etl::string<SIZE> >
bobbery 0:b47c2a7920c2 205 {
bobbery 0:b47c2a7920c2 206 size_t operator()(const etl::string<SIZE>& text) const
bobbery 0:b47c2a7920c2 207 {
bobbery 0:b47c2a7920c2 208 return etl::__private_hash__::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(&text[0]),
bobbery 0:b47c2a7920c2 209 reinterpret_cast<const uint8_t*>(&text[text.size()]));
bobbery 0:b47c2a7920c2 210 }
bobbery 0:b47c2a7920c2 211 };
bobbery 0:b47c2a7920c2 212 #endif
bobbery 0:b47c2a7920c2 213 }
bobbery 0:b47c2a7920c2 214
bobbery 0:b47c2a7920c2 215 #if defined(ETL_COMPILER_MICROSOFT)
bobbery 0:b47c2a7920c2 216 #define min(a,b) (((a) < (b)) ? (a) : (b))
bobbery 0:b47c2a7920c2 217 #endif
bobbery 0:b47c2a7920c2 218
bobbery 0:b47c2a7920c2 219 #endif
bobbery 0:b47c2a7920c2 220