From f096a602cec4fe563dbb6c769789d6940c51b538 Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Thu, 5 Apr 2018 16:11:43 +0200 Subject: [PATCH] Add support for std::string::assign() --- tstl/include/string.hpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) 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());