string.cc (4962B)
1 #include "string.hh" 2 3 #include <cstring> 4 #include "assert.hh" 5 #include "unit_tests.hh" 6 7 namespace Kakoune 8 { 9 namespace 10 { 11 // Avoid including all of <algorithm> just for this. 12 constexpr auto max(auto lhs, auto rhs) { return lhs > rhs ? lhs : rhs;} 13 constexpr auto min(auto lhs, auto rhs) { return lhs < rhs ? lhs : rhs;} 14 } 15 16 String::Data::Data(const char* data, size_t size, size_t capacity) 17 { 18 if (capacity > Short::capacity) 19 { 20 kak_assert(capacity <= Long::max_capacity); 21 u.l.ptr = Alloc{}.allocate(capacity+1); 22 u.l.size = size; 23 u.l.capacity = (capacity & Long::max_capacity); 24 u.l.mode = Long::active_mask; 25 26 if (data != nullptr) 27 memcpy(u.l.ptr, data, size); 28 u.l.ptr[size] = 0; 29 } 30 else 31 set_short(data, size); 32 } 33 34 String::Data& String::Data::operator=(const Data& other) 35 { 36 if (&other == this) 37 return *this; 38 39 const size_t new_size = other.size(); 40 reserve<false>(new_size); 41 memcpy(data(), other.data(), new_size+1); 42 set_size(new_size); 43 44 return *this; 45 } 46 47 String::Data& String::Data::operator=(Data&& other) noexcept 48 { 49 if (&other == this) 50 return *this; 51 52 release(); 53 54 if (other.is_long()) 55 { 56 u.l = other.u.l; 57 other.set_empty(); 58 } 59 else 60 u.s = other.u.s; 61 62 return *this; 63 } 64 65 template<bool copy> 66 void String::Data::reserve(size_t new_capacity) 67 { 68 auto const current_capacity = capacity(); 69 if (current_capacity != 0 and new_capacity <= current_capacity) 70 return; 71 72 if (not is_long() and new_capacity <= Short::capacity) 73 return; 74 75 kak_assert(new_capacity <= Long::max_capacity); 76 new_capacity = max(new_capacity, // Do not upgrade new_capacity to be over limit. 77 min(current_capacity * 2, Long::max_capacity)); 78 79 char* new_ptr = Alloc{}.allocate(new_capacity+1); 80 if (copy) 81 { 82 memcpy(new_ptr, data(), size()+1); 83 } 84 release(); 85 86 u.l.size = size(); 87 u.l.ptr = new_ptr; 88 u.l.capacity = (new_capacity & Long::max_capacity); 89 u.l.mode = Long::active_mask; 90 } 91 92 template void String::Data::reserve<true>(size_t); 93 template void String::Data::reserve<false>(size_t); 94 95 void String::Data::force_size(size_t new_size) 96 { 97 reserve<false>(new_size); 98 set_size(new_size); 99 data()[new_size] = 0; 100 } 101 102 void String::Data::append(const char* str, size_t len) 103 { 104 if (len == 0) 105 return; 106 107 const size_t new_size = size() + len; 108 reserve(new_size); 109 110 memcpy(data() + size(), str, len); 111 set_size(new_size); 112 data()[new_size] = 0; 113 } 114 115 void String::Data::clear() 116 { 117 release(); 118 set_empty(); 119 } 120 121 String::String(Codepoint cp, CharCount count) 122 { 123 reserve(utf8::codepoint_size(cp) * (int)count); 124 while (count-- > 0) 125 utf8::dump(std::back_inserter(*this), cp); 126 } 127 128 String::String(Codepoint cp, ColumnCount count) 129 { 130 int cp_count = (int)(count / max(codepoint_width(cp), 1_col)); 131 reserve(utf8::codepoint_size(cp) * cp_count); 132 while (cp_count-- > 0) 133 utf8::dump(std::back_inserter(*this), cp); 134 } 135 136 void String::resize(ByteCount size, char c) 137 { 138 const size_t target_size = (size_t)size; 139 const size_t current_size = m_data.size(); 140 if (target_size < current_size) 141 m_data.set_size(target_size); 142 else if (target_size > current_size) 143 { 144 m_data.reserve(target_size); 145 m_data.set_size(target_size); 146 for (auto i = current_size; i < target_size; ++i) 147 m_data.data()[i] = c; 148 } 149 data()[target_size] = 0; 150 } 151 152 void String::Data::set_size(size_t size) 153 { 154 if (is_long()) 155 u.l.size = size; 156 else 157 u.s.remaining_size = Short::capacity - size; 158 } 159 160 void String::Data::set_short(const char* data, size_t size) 161 { 162 kak_assert(size <= Short::capacity); 163 u.s.remaining_size = Short::capacity - size; 164 if (data != nullptr) 165 memcpy(u.s.string, data, size); 166 if (size != Short::capacity) // in this case, remaining_size is the null terminator 167 u.s.string[size] = 0; 168 } 169 170 UnitTest test_data{[]{ 171 using Data = String::Data; 172 { // Basic data usage. 173 Data data; 174 kak_assert(data.size() == 0); 175 kak_assert(not data.is_long()); 176 kak_assert(data.capacity() == (sizeof(char *) == 8? 23: 11)); 177 178 // Should be SSO-ed. 179 data.append("test", 4); 180 kak_assert(data.size() == 4); 181 kak_assert(data.capacity() == (sizeof(char *) == 8? 23: 11)); 182 kak_assert(not data.is_long()); 183 kak_assert(data.data() == StringView("test")); 184 } 185 { 186 char large_buf[2048]; 187 memset(large_buf, 'x', 2048); 188 Data data(large_buf, 2048); 189 kak_assert(data.size() == 2048); 190 kak_assert(data.capacity() >= 2048); 191 192 data.clear(); 193 kak_assert(data.size() == 0); 194 kak_assert(not data.is_long()); 195 kak_assert(data.capacity() == (sizeof(char *) == 8? 23: 11)); 196 } 197 }}; 198 199 const String String::ms_empty; 200 201 }