Add a String::resize method
This commit is contained in:
parent
f2ba54b2d4
commit
b8908f2dc6
|
@ -114,6 +114,21 @@ void String::Data::release()
|
||||||
Alloc{}.deallocate(l.ptr, l.capacity+1);
|
Alloc{}.deallocate(l.ptr, l.capacity+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void String::resize(ByteCount size, char c)
|
||||||
|
{
|
||||||
|
const size_t target_size = (size_t)size;
|
||||||
|
const size_t current_size = m_data.size();
|
||||||
|
if (target_size < current_size)
|
||||||
|
m_data.set_size(target_size);
|
||||||
|
else if (target_size > current_size)
|
||||||
|
{
|
||||||
|
m_data.reserve(target_size);
|
||||||
|
m_data.set_size(target_size);
|
||||||
|
for (auto i = current_size; i < target_size; ++i)
|
||||||
|
m_data.data()[i] = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void String::Data::set_size(size_t size)
|
void String::Data::set_size(size_t size)
|
||||||
{
|
{
|
||||||
if (is_long())
|
if (is_long())
|
||||||
|
|
|
@ -125,6 +125,7 @@ public:
|
||||||
void push_back(char c) { m_data.append(&c, 1); }
|
void push_back(char c) { m_data.append(&c, 1); }
|
||||||
void force_size(ByteCount size) { m_data.force_size((size_t)size); }
|
void force_size(ByteCount size) { m_data.force_size((size_t)size); }
|
||||||
void reserve(ByteCount size) { m_data.reserve((size_t)size); }
|
void reserve(ByteCount size) { m_data.reserve((size_t)size); }
|
||||||
|
void resize(ByteCount size, char c);
|
||||||
|
|
||||||
static const String ms_empty;
|
static const String ms_empty;
|
||||||
|
|
||||||
|
@ -165,6 +166,7 @@ public:
|
||||||
|
|
||||||
template<bool copy = true>
|
template<bool copy = true>
|
||||||
void reserve(size_t new_capacity);
|
void reserve(size_t new_capacity);
|
||||||
|
void set_size(size_t size);
|
||||||
void force_size(size_t new_size);
|
void force_size(size_t new_size);
|
||||||
void append(const char* str, size_t len);
|
void append(const char* str, size_t len);
|
||||||
void clear();
|
void clear();
|
||||||
|
@ -172,7 +174,6 @@ public:
|
||||||
private:
|
private:
|
||||||
void release();
|
void release();
|
||||||
void set_empty() { s.size = 1; }
|
void set_empty() { s.size = 1; }
|
||||||
void set_size(size_t size);
|
|
||||||
void set_short(const char* data, size_t size);
|
void set_short(const char* data, size_t size);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user