summaryrefslogtreecommitdiff
path: root/src/string.cc
diff options
context:
space:
mode:
authorBen Judd <bjudd61@gmail.com>2024-06-11 08:33:35 -0700
committerBen Judd <bjudd61@gmail.com>2024-06-11 08:33:35 -0700
commitfaf83b10e25fe237c9e780d09386420c98110a5e (patch)
treea44e9c30cb0ee12dd5d69983fadffe46a52edb00 /src/string.cc
parent9c185249a2943bb81fcdff47c0d9423d0cc8caa6 (diff)
Switch to bitfield.
Diffstat (limited to 'src/string.cc')
-rw-r--r--src/string.cc18
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);