diff --git a/tstl/include/string.hpp b/tstl/include/string.hpp index ff1e827c..85e728ce 100644 --- a/tstl/include/string.hpp +++ b/tstl/include/string.hpp @@ -312,6 +312,40 @@ public: ensure_capacity(new_capacity); } + basic_string& append(const basic_string& rhs){ + return base_append(rhs); + } + + basic_string& append(const char* rhs){ + std::string_view sv = rhs; + + return base_append(sv); + } + + template::value>> + basic_string& append(const T& rhs){ + std::string_view sv = rhs; + + return base_append(sv); + } + + template + basic_string& append(T it, T end){ + return base_append(it, end); + } + + basic_string& operator+=(const char* rhs){ + return append(rhs); + } + + basic_string& operator+=(const basic_string& rhs){ + return append(rhs); + } + + basic_string& operator+=(const sv_type& rhs){ + return append(rhs); + } + /*! * \brief Creates a string resulting of the concatenation of this string the given char */ @@ -337,32 +371,6 @@ public: return *this; } - basic_string& operator+=(const char* rhs){ - auto len = str_len(rhs); - - ensure_capacity(size() + len + 1); - - std::copy_n(rhs, len, begin() + size()); - - set_size(size() + len); - - (*this)[size()] = '\0'; - - return *this; - } - - basic_string& operator+=(const basic_string& rhs){ - ensure_capacity(size() + rhs.size() + 1); - - std::copy_n(rhs.begin(), rhs.size(), begin() + size()); - - set_size(size() + rhs.size()); - - (*this)[size()] = '\0'; - - return *this; - } - //Accessors /*! @@ -573,6 +581,26 @@ private: } } + template + basic_string& base_append(const T& rhs){ + return base_append(rhs.begin(), rhs.end()); + } + + template + basic_string& base_append(T it, T end){ + auto n = std::distance(it, end); + + ensure_capacity(size() + n + 1); + + std::copy(it, end, begin() + size()); + + set_size(size() + n); + + (*this)[size()] = '\0'; + + return *this; + } + //TODO Cleanup the duplication with ensure_capacity() //Make sure to avoid the unecessary copy