diff options
Diffstat (limited to 'src/string.cc')
| -rw-r--r-- | src/string.cc | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/src/string.cc b/src/string.cc index 7be39715..a672433b 100644 --- a/src/string.cc +++ b/src/string.cc @@ -1,11 +1,17 @@ #include "string.hh" -#include <cstdio> +#include <cstring> #include "assert.hh" #include "unit_tests.hh" namespace Kakoune { +namespace +{ +// Avoid including all of <algorithm> just for this. +constexpr auto max(auto lhs, auto rhs) { return lhs > rhs ? lhs : rhs;} +constexpr auto min(auto lhs, auto rhs) { return lhs < rhs ? lhs : rhs;} +} String::Data::Data(const char* data, size_t size, size_t capacity) { @@ -14,7 +20,8 @@ String::Data::Data(const char* data, size_t size, size_t capacity) kak_assert(capacity <= Long::max_capacity); u.l.ptr = Alloc{}.allocate(capacity+1); u.l.size = size; - u.l.set_capacity(capacity); + u.l.capacity = (capacity & Long::max_capacity); + u.l.mode = Long::active_mask; if (data != nullptr) memcpy(u.l.ptr, data, size); @@ -66,8 +73,8 @@ void String::Data::reserve(size_t new_capacity) return; kak_assert(new_capacity <= Long::max_capacity); - new_capacity = std::max(new_capacity, // Do not upgrade new_capacity to be over limit. - std::min(current_capacity * 2, Long::max_capacity)); + new_capacity = max(new_capacity, // Do not upgrade new_capacity to be over limit. + min(current_capacity * 2, Long::max_capacity)); char* new_ptr = Alloc{}.allocate(new_capacity+1); if (copy) @@ -78,7 +85,8 @@ void String::Data::reserve(size_t new_capacity) u.l.size = size(); u.l.ptr = new_ptr; - u.l.set_capacity(new_capacity); + u.l.capacity = (new_capacity & Long::max_capacity); + u.l.mode = Long::active_mask; } template void String::Data::reserve<true>(size_t); |
