From 71bf65e0fd9c38901ab36c4627ebf30996ce8290 Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Thu, 5 Apr 2018 16:34:06 +0200 Subject: [PATCH] Refactorings --- tstl/include/string.hpp | 39 ++++++--------------------------------- 1 file changed, 6 insertions(+), 33 deletions(-) diff --git a/tstl/include/string.hpp b/tstl/include/string.hpp index 85e728ce..9242da4a 100644 --- a/tstl/include/string.hpp +++ b/tstl/include/string.hpp @@ -535,20 +535,7 @@ public: 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]); - } - } + ensure_capacity(n + 1, false); std::copy(it, end, begin()); data_ptr()[size()] = '\0'; @@ -558,7 +545,7 @@ public: private: - void ensure_capacity(size_t new_capacity){ + void ensure_capacity(size_t new_capacity, bool preserve = true){ if(new_capacity > 0 && (capacity() < new_capacity)){ auto new_cap = capacity() * 2; @@ -568,7 +555,9 @@ private: auto new_data = new CharT[new_cap]; - std::copy_n(begin(), size() + 1, new_data); + if(preserve){ + std::copy_n(begin(), size() + 1, new_data); + } if(is_small()){ new (&storage.big) base_long(new_cap, new_data); @@ -601,27 +590,11 @@ private: return *this; } - //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()); - auto need = rhs.size() + 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]); - } - } + ensure_capacity(rhs.size() + 1, false); std::copy(rhs.begin(), rhs.end(), begin()); data_ptr()[size()] = '\0';