Add support for reserve()

This commit is contained in:
Baptiste Wicht 2014-03-01 14:24:08 +01:00
parent 54a335db26
commit c942abdb17

View File

@ -123,6 +123,10 @@ public:
_data[--_size] = '\0'; _data[--_size] = '\0';
} }
void reserve(size_t new_capacity){
ensure_capacity(new_capacity);
}
basic_string operator+(CharT c) const { basic_string operator+(CharT c) const {
basic_string copy(*this); basic_string copy(*this);
@ -132,17 +136,7 @@ public:
} }
basic_string& operator+=(CharT c){ basic_string& operator+=(CharT c){
if(!_data || _capacity <= _size + 1){ ensure_capacity(_size + 2);
_capacity = _capacity ? _capacity * 2 : 1;
auto new_data = new CharT[_capacity];
if(_data){
std::copy_n(new_data, _data, _size);
delete[] _data;
}
_data = new_data;
}
_data[_size] = c; _data[_size] = c;
_data[++_size] = '\0'; _data[++_size] = '\0';
@ -150,13 +144,12 @@ public:
return *this; return *this;
} }
basic_string& operator+=(const char* rhs){ void ensure_capacity(size_t new_capacity){
auto len = str_len(rhs); if(new_capacity > 0 && (!_data || _capacity < new_capacity)){
if(!_data || _capacity <= _size + len){
_capacity = _capacity ? _capacity * 2 : 1; _capacity = _capacity ? _capacity * 2 : 1;
if(_capacity < _size + len){ if(_capacity < new_capacity){
_capacity = _size + len + 1; _capacity = new_capacity;
} }
auto new_data = new CharT[_capacity]; auto new_data = new CharT[_capacity];
@ -168,6 +161,12 @@ public:
_data = new_data; _data = new_data;
} }
}
basic_string& operator+=(const char* rhs){
auto len = str_len(rhs);
ensure_capacity(_size + len + 1);
std::copy_n(_data + _size, rhs, len); std::copy_n(_data + _size, rhs, len);
@ -179,22 +178,7 @@ public:
} }
basic_string& operator+=(const basic_string& rhs){ basic_string& operator+=(const basic_string& rhs){
if(!_data || _capacity <= _size + rhs.size()){ ensure_capacity(_size + rhs.size() + 1);
_capacity = _capacity ? _capacity * 2 : 1;
if(_capacity < _size + rhs.size()){
_capacity = _size + rhs.size() + 1;
}
auto new_data = new CharT[_capacity];
if(_data){
std::copy_n(new_data, _data, _size);
delete[] _data;
}
_data = new_data;
}
std::copy_n(_data + _size, rhs.c_str(), rhs.size()); std::copy_n(_data + _size, rhs.c_str(), rhs.size());