From b8908f2dc6113fb38571a98be777128a0a2abe79 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 19 Jun 2016 17:01:27 +0100 Subject: Add a String::resize method --- src/string.cc | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/string.cc') diff --git a/src/string.cc b/src/string.cc index 42f3d7ce..638eabdb 100644 --- a/src/string.cc +++ b/src/string.cc @@ -114,6 +114,21 @@ void String::Data::release() 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) { if (is_long()) -- cgit v1.2.3