2012-05-29 07:19:50 +02:00
|
|
|
#include "string.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
|
2015-01-08 20:31:28 +01:00
|
|
|
#include <cstdio>
|
2017-10-09 16:12:42 +02:00
|
|
|
#include <cstring>
|
2015-01-08 20:31:28 +01:00
|
|
|
|
2012-05-29 07:19:50 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2016-02-05 10:27:22 +01:00
|
|
|
String::Data::Data(const char* data, size_t size, size_t capacity)
|
|
|
|
{
|
|
|
|
if (capacity > Short::capacity)
|
|
|
|
{
|
|
|
|
if (capacity & 1)
|
|
|
|
++capacity;
|
|
|
|
|
2016-09-28 20:03:26 +02:00
|
|
|
kak_assert(capacity < Long::max_capacity);
|
2021-07-20 11:53:06 +02:00
|
|
|
u.l.ptr = Alloc{}.allocate(capacity+1);
|
|
|
|
u.l.size = size;
|
|
|
|
u.l.capacity = capacity;
|
2016-02-05 10:27:22 +01:00
|
|
|
|
2017-06-26 12:27:18 +02:00
|
|
|
if (data != nullptr)
|
2021-07-20 11:53:06 +02:00
|
|
|
memcpy(u.l.ptr, data, size);
|
|
|
|
u.l.ptr[size] = 0;
|
2016-02-05 10:27:22 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
set_short(data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
String::Data& String::Data::operator=(const Data& other)
|
|
|
|
{
|
2017-08-23 13:13:42 +02:00
|
|
|
if (&other == this)
|
|
|
|
return *this;
|
|
|
|
|
2016-02-05 10:27:22 +01:00
|
|
|
const size_t new_size = other.size();
|
|
|
|
reserve<false>(new_size);
|
|
|
|
memcpy(data(), other.data(), new_size+1);
|
|
|
|
set_size(new_size);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
String::Data& String::Data::operator=(Data&& other) noexcept
|
|
|
|
{
|
2017-08-23 13:13:42 +02:00
|
|
|
if (&other == this)
|
|
|
|
return *this;
|
|
|
|
|
2016-11-23 02:09:09 +01:00
|
|
|
release();
|
|
|
|
|
2016-02-05 10:27:22 +01:00
|
|
|
if (other.is_long())
|
|
|
|
{
|
2021-07-20 11:53:06 +02:00
|
|
|
u.l = other.u.l;
|
2016-02-05 10:27:22 +01:00
|
|
|
other.set_empty();
|
|
|
|
}
|
|
|
|
else
|
2021-07-20 11:53:06 +02:00
|
|
|
u.s = other.u.s;
|
2016-02-05 10:27:22 +01:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<bool copy>
|
|
|
|
void String::Data::reserve(size_t new_capacity)
|
|
|
|
{
|
2019-03-19 12:00:57 +01:00
|
|
|
if (capacity() != 0 and new_capacity <= capacity())
|
2016-02-05 10:27:22 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (is_long())
|
2021-07-20 11:53:06 +02:00
|
|
|
new_capacity = std::max(u.l.capacity * 2, new_capacity);
|
2016-02-05 10:27:22 +01:00
|
|
|
|
2016-09-28 20:03:26 +02:00
|
|
|
if (new_capacity & 1)
|
|
|
|
++new_capacity;
|
|
|
|
|
|
|
|
kak_assert(new_capacity < Long::max_capacity);
|
2016-02-05 10:27:22 +01:00
|
|
|
char* new_ptr = Alloc{}.allocate(new_capacity+1);
|
|
|
|
if (copy)
|
|
|
|
{
|
|
|
|
memcpy(new_ptr, data(), size()+1);
|
2021-07-20 11:53:06 +02:00
|
|
|
u.l.size = size();
|
2016-02-05 10:27:22 +01:00
|
|
|
}
|
|
|
|
release();
|
|
|
|
|
2021-07-20 11:53:06 +02:00
|
|
|
u.l.ptr = new_ptr;
|
|
|
|
u.l.capacity = new_capacity;
|
2016-02-05 10:27:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template void String::Data::reserve<true>(size_t);
|
|
|
|
template void String::Data::reserve<false>(size_t);
|
|
|
|
|
|
|
|
void String::Data::force_size(size_t new_size)
|
|
|
|
{
|
|
|
|
reserve<false>(new_size);
|
|
|
|
set_size(new_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void String::Data::append(const char* str, size_t len)
|
|
|
|
{
|
2017-06-26 12:27:18 +02:00
|
|
|
if (len == 0)
|
|
|
|
return;
|
|
|
|
|
2016-02-05 10:27:22 +01:00
|
|
|
const size_t new_size = size() + len;
|
|
|
|
reserve(new_size);
|
|
|
|
|
|
|
|
memcpy(data() + size(), str, len);
|
|
|
|
set_size(new_size);
|
|
|
|
data()[new_size] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void String::Data::clear()
|
|
|
|
{
|
|
|
|
release();
|
|
|
|
set_empty();
|
|
|
|
}
|
|
|
|
|
2016-06-19 18:01:27 +02:00
|
|
|
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;
|
|
|
|
}
|
2019-11-16 23:50:18 +01:00
|
|
|
data()[target_size] = 0;
|
2016-06-19 18:01:27 +02:00
|
|
|
}
|
|
|
|
|
2016-02-05 10:27:22 +01:00
|
|
|
void String::Data::set_size(size_t size)
|
|
|
|
{
|
|
|
|
if (is_long())
|
2021-07-20 11:53:06 +02:00
|
|
|
u.l.size = size;
|
2016-02-05 10:27:22 +01:00
|
|
|
else
|
2021-07-20 11:53:06 +02:00
|
|
|
u.s.size = (size << 1) | 1;
|
2016-02-05 10:27:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void String::Data::set_short(const char* data, size_t size)
|
|
|
|
{
|
2021-07-20 11:53:06 +02:00
|
|
|
u.s.size = (size << 1) | 1;
|
2017-06-26 12:27:18 +02:00
|
|
|
if (data != nullptr)
|
2021-07-20 11:53:06 +02:00
|
|
|
memcpy(u.s.string, data, size);
|
|
|
|
u.s.string[size] = 0;
|
2016-02-05 10:27:22 +01:00
|
|
|
}
|
|
|
|
|
2015-11-25 22:07:41 +01:00
|
|
|
const String String::ms_empty;
|
|
|
|
|
2012-05-29 07:19:50 +02:00
|
|
|
}
|