mirror of
https://github.com/wichtounet/thor-os.git
synced 2025-09-17 16:51:33 -04:00
Better assign/append support for std::string
This commit is contained in:
parent
f096a602ce
commit
9dd92aa5a5
@ -312,6 +312,40 @@ public:
|
|||||||
ensure_capacity(new_capacity);
|
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<typename T, typename = std::enable_if_t<is_sv<T>::value>>
|
||||||
|
basic_string& append(const T& rhs){
|
||||||
|
std::string_view sv = rhs;
|
||||||
|
|
||||||
|
return base_append(sv);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
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
|
* \brief Creates a string resulting of the concatenation of this string the given char
|
||||||
*/
|
*/
|
||||||
@ -337,32 +371,6 @@ public:
|
|||||||
return *this;
|
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
|
//Accessors
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -573,6 +581,26 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
basic_string& base_append(const T& rhs){
|
||||||
|
return base_append(rhs.begin(), rhs.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
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()
|
//TODO Cleanup the duplication with ensure_capacity()
|
||||||
//Make sure to avoid the unecessary copy
|
//Make sure to avoid the unecessary copy
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user