Add more CTL content

This commit is contained in:
Justine Tunney 2024-06-28 19:07:35 -07:00
parent 38921dc46b
commit 021c53ba32
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
56 changed files with 1747 additions and 298 deletions

View file

@ -26,7 +26,7 @@ namespace ctl {
void
string::destroy_big() noexcept
{
auto* b = big();
auto* b = &__b;
if (b->n) {
if (b->n >= b->c)
__builtin_trap();
@ -41,7 +41,14 @@ string::destroy_big() noexcept
void
string::init_big(const string& s) noexcept
{
init_big(string_view(s));
char* p2;
size_t size = s.size();
size_t need = size + 1;
size_t capacity = need;
if (!(p2 = (char*)malloc(capacity)))
__builtin_trap();
memcpy(p2, s.data(), need);
set_big_string(p2, size, capacity);
}
void
@ -103,7 +110,7 @@ string::reserve(size_t c2) noexcept
__builtin_trap();
memcpy(p2, data(), __::string_size);
} else {
if (!(p2 = (char*)realloc(big()->p, c2)))
if (!(p2 = (char*)realloc(__b.p, c2)))
__builtin_trap();
}
std::atomic_signal_fence(std::memory_order_seq_cst);
@ -120,7 +127,7 @@ string::resize(const size_t n2, const char ch) noexcept
if (n2 > size())
memset(data() + size(), ch, n2 - size());
if (isbig()) {
big()->p[big()->n = n2] = 0;
__b.p[__b.n = n2] = 0;
} else {
set_small_size(n2);
data()[size()] = 0;
@ -141,9 +148,9 @@ string::append(const char ch) noexcept
}
data()[size()] = ch;
if (isbig()) {
++big()->n;
++__b.n;
} else {
--small()->rem;
--__s.rem;
}
data()[size()] = 0;
}
@ -174,9 +181,9 @@ string::append(const char ch, const size_t size) noexcept
if (size)
memset(data() + this->size(), ch, size);
if (isbig()) {
big()->n += size;
__b.n += size;
} else {
small()->rem -= size;
__s.rem -= size;
}
data()[this->size()] = 0;
}
@ -188,9 +195,9 @@ string::append(const void* data, const size_t size) noexcept
if (size)
memcpy(this->data() + this->size(), data, size);
if (isbig()) {
big()->n += size;
__b.n += size;
} else {
small()->rem -= size;
__s.rem -= size;
}
this->data()[this->size()] = 0;
}
@ -201,9 +208,9 @@ string::pop_back() noexcept
if (!size())
__builtin_trap();
if (isbig()) {
--big()->n;
--__b.n;
} else {
++small()->rem;
++__s.rem;
}
data()[size()] = 0;
}
@ -322,7 +329,7 @@ string::replace(const size_t pos,
memmove(data() + pos + s.n, data() + last, extra);
memcpy(data() + pos, s.p, s.n);
if (isbig()) {
big()->p[big()->n = need] = 0;
__b.p[__b.n = need] = 0;
} else {
set_small_size(need);
data()[size()] = 0;
@ -346,9 +353,9 @@ string::insert(const size_t i, const string_view s) noexcept
memmove(data() + i + s.n, data() + i, extra);
memcpy(data() + i, s.p, s.n);
if (isbig()) {
big()->n += s.n;
__b.n += s.n;
} else {
small()->rem -= s.n;
__s.rem -= s.n;
}
data()[size()] = 0;
return *this;
@ -365,7 +372,7 @@ string::erase(const size_t pos, size_t count) noexcept
if (extra)
memmove(data() + pos, data() + pos + count, extra);
if (isbig()) {
big()->n = pos + extra;
__b.n = pos + extra;
} else {
set_small_size(pos + extra);
}