Tests pass

This commit is contained in:
Jōshin 2024-06-06 07:35:19 -07:00
parent 23d489c041
commit 5f4acebca4
No known key found for this signature in database
3 changed files with 22 additions and 11 deletions

View file

@ -47,27 +47,27 @@ string::~string() /* noexcept */
} }
} }
string::string(const char* s) noexcept string::string(const char* s) noexcept : string()
{ {
append(s, strlen(s)); append(s, strlen(s));
} }
string::string(const string& s) noexcept string::string(const string& s) noexcept : string()
{ {
append(s.data(), s.size()); append(s.data(), s.size());
} }
string::string(const string_view s) noexcept string::string(const string_view s) noexcept : string()
{ {
append(s.p, s.n); append(s.p, s.n);
} }
string::string(size_t size, char ch) noexcept string::string(size_t size, char ch) noexcept : string()
{ {
resize(size, ch); resize(size, ch);
} }
string::string(const char* s, size_t size) noexcept string::string(const char* s, size_t size) noexcept : string()
{ {
append(s, size); append(s, size);
} }
@ -75,6 +75,8 @@ string::string(const char* s, size_t size) noexcept
const char* const char*
string::c_str() const noexcept string::c_str() const noexcept
{ {
if (!size())
return "";
if (size() >= capacity()) if (size() >= capacity())
__builtin_trap(); __builtin_trap();
if (data()[size()]) if (data()[size()])
@ -86,8 +88,9 @@ void
string::reserve(size_t c2) noexcept string::reserve(size_t c2) noexcept
{ {
char* p2; char* p2;
if (c2 < size()) size_t n = size();
c2 = size(); if (c2 < n)
c2 = n;
if (ckd_add(&c2, c2, 15)) if (ckd_add(&c2, c2, 15))
__builtin_trap(); __builtin_trap();
c2 &= -16; c2 &= -16;
@ -101,7 +104,6 @@ string::reserve(size_t c2) noexcept
if (!(p2 = (char *)realloc(big()->p, c2))) if (!(p2 = (char *)realloc(big()->p, c2)))
__builtin_trap(); __builtin_trap();
} }
size_t n = size();
std::atomic_signal_fence(std::memory_order_seq_cst); std::atomic_signal_fence(std::memory_order_seq_cst);
set_big_capacity(c2); set_big_capacity(c2);
big()->n = n; big()->n = n;

View file

@ -84,7 +84,9 @@ class string
string() noexcept string() noexcept
{ {
set_small_size(0); set_small_size(0);
small()->buf[0] = 0; #if 0
s.small()->buf[0] = 0;
#endif
} }
void swap(string& s) noexcept void swap(string& s) noexcept
@ -100,9 +102,9 @@ class string
{ {
__builtin_memcpy(blob, __builtin_launder(s.blob), sizeof(blob)); __builtin_memcpy(blob, __builtin_launder(s.blob), sizeof(blob));
s.set_small_size(0); s.set_small_size(0);
/* shouldn't be necessary, but the spec says s should be left in a valid #if 0
state and our c_str() depends on this */
s.small()->buf[0] = 0; s.small()->buf[0] = 0;
#endif
} }
void clear() noexcept void clear() noexcept
@ -304,6 +306,7 @@ class string
if (c2 > __::big_mask) if (c2 > __::big_mask)
__builtin_trap(); __builtin_trap();
*(__builtin_launder(blob) + __::sso_max) = 0x80; *(__builtin_launder(blob) + __::sso_max) = 0x80;
big()->c &= ~__::big_mask;
big()->c |= c2; big()->c |= c2;
} }

View file

@ -308,6 +308,12 @@ main()
s.append(" world"); s.append(" world");
if (s != "hello world") if (s != "hello world")
return 63; return 63;
for (int i = 0; i < 4; ++i) {
s.append(" world");
}
if (s != "hello world world world world world") {
return 64;
}
} }
// { // {