diff --git a/tstl/include/string.hpp b/tstl/include/string.hpp index c08702a0..ff1e827c 100644 --- a/tstl/include/string.hpp +++ b/tstl/include/string.hpp @@ -521,6 +521,33 @@ public: return base_assign(sv); } + template + basic_string& assign(It it, It end){ + auto n = std::distance(it, end); + + set_size(n); + + auto need = n + 1; + + if(capacity() < need){ + auto capacity = need; + + if(is_small()){ + new (&storage.big) base_long(capacity, new CharT[capacity]); + + set_small(false); + } else { + storage.big.capacity = capacity; + storage.big.data.reset(new CharT[capacity]); + } + } + + std::copy(it, end, begin()); + data_ptr()[size()] = '\0'; + + return *this; + } + private: void ensure_capacity(size_t new_capacity){ @@ -546,6 +573,9 @@ private: } } + //TODO Cleanup the duplication with ensure_capacity() + //Make sure to avoid the unecessary copy + template basic_string& base_assign(const T& rhs){ set_size(rhs.size());