BAN: Add [[nodiscard]] to BAN functions returning ErrorOr

This commit is contained in:
Bananymous 2023-01-12 23:57:07 +02:00
parent 78da037dda
commit 61bd9da8e0
7 changed files with 80 additions and 66 deletions

View File

@ -12,12 +12,12 @@ namespace BAN
String::String() String::String()
{ {
MUST(copy_impl("", 0)); MUST(copy_impl(""_sv));
} }
String::String(const String& other) String::String(const String& other)
{ {
MUST(copy_impl(other.Data(), other.Size())); MUST(copy_impl(other.SV()));
} }
String::String(String&& other) String::String(String&& other)
@ -25,16 +25,9 @@ namespace BAN
move_impl(Move(other)); move_impl(Move(other));
} }
String::String(const StringView& other) String::String(StringView other)
{ {
MUST(copy_impl(other.Data(), other.Size())); MUST(copy_impl(other));
}
String::String(const char* data, size_type len)
{
if (len == size_type(-1))
len = strlen(data);
MUST(copy_impl(data, len));
} }
String::~String() String::~String()
@ -44,7 +37,7 @@ namespace BAN
String& String::operator=(const String& other) String& String::operator=(const String& other)
{ {
copy_impl(other.Data(), other.Size()); MUST(copy_impl(other.SV()));
return *this; return *this;
} }
@ -59,57 +52,74 @@ namespace BAN
{ {
TRY(EnsureCapasity(m_size + 2)); TRY(EnsureCapasity(m_size + 2));
m_data[m_size] = ch; m_data[m_size] = ch;
m_data[m_size + 1] = '\0';
m_size++; m_size++;
m_data[m_size] = '\0';
return {}; return {};
} }
ErrorOr<void> String::Insert(char ch, size_type index) ErrorOr<void> String::Insert(char ch, size_type index)
{ {
ASSERT(index <= m_size); ASSERT(index <= m_size);
TRY(EnsureCapasity(m_size + 2)); TRY(EnsureCapasity(m_size + 1 + 1));
memmove(m_data + index + 1, m_data + index, m_size - index); memmove(m_data + index + 1, m_data + index, m_size - index);
m_data[index] = ch; m_data[index] = ch;
m_data[m_size + 1] = '\0'; m_size += 1;
m_size++; m_data[m_size] = '\0';
return {}; return {};
} }
ErrorOr<void> String::Append(const char* string) ErrorOr<void> String::Insert(StringView other, size_type index)
{ {
size_t len = strlen(string); dprintln("insert '{}' to '{}' at index {}", other, *this, index);
TRY(EnsureCapasity(m_size + len + 1));
memcpy(m_data + m_size, string, len); ASSERT(index <= m_size);
m_data[m_size + len] = '\0'; TRY(EnsureCapasity(m_size + other.Size() + 1));
m_size += len; memmove(m_data + index + other.Size(), m_data + index, m_size - index);
memcpy(m_data + index, other.Data(), other.Size());
m_size += other.Size();
m_data[m_size] = '\0';
return {};
}
ErrorOr<void> String::Append(StringView other)
{
TRY(EnsureCapasity(m_size + other.Size() + 1));
memcpy(m_data + m_size, other.Data(), other.Size());
m_size += other.Size();
m_data[m_size] = '\0';
return {}; return {};
} }
ErrorOr<void> String::Append(const String& string) ErrorOr<void> String::Append(const String& string)
{ {
TRY(Append(string.Data())); TRY(Append(string.SV()));
return {}; return {};
} }
void String::PopBack() void String::PopBack()
{ {
ASSERT(m_size > 0); ASSERT(m_size > 0);
m_data[m_size - 1] = '\0';
m_size--; m_size--;
m_data[m_size] = '\0';
} }
void String::Remove(size_type index) void String::Remove(size_type index)
{ {
ASSERT(index < m_size); Erase(index, 1);
memmove(m_data + index, m_data + index + 1, m_size - index - 1); }
m_data[m_size - 1] = '\0';
m_size--; void String::Erase(size_type index, size_type count)
{
ASSERT(index + count <= m_size);
memmove(m_data + index, m_data + index + count, m_size - index - count);
m_size -= count;
m_data[m_size] = '\0';
} }
void String::Clear() void String::Clear()
{ {
m_data[0] = '\0';
m_size = 0; m_size = 0;
m_data[0] = '\0';
} }
char String::operator[](size_type index) const char String::operator[](size_type index) const
@ -140,9 +150,10 @@ namespace BAN
bool String::operator==(const char* other) const bool String::operator==(const char* other) const
{ {
if (memcmp(m_data, other, m_size)) for (size_type i = 0; i <= m_size; i++)
if (m_data[i] != other[i])
return false; return false;
return other[m_size] == '\0'; return true;
} }
ErrorOr<void> String::Resize(size_type size, char ch) ErrorOr<void> String::Resize(size_type size, char ch)
@ -210,12 +221,12 @@ namespace BAN
return {}; return {};
} }
ErrorOr<void> String::copy_impl(const char* data, size_type len) ErrorOr<void> String::copy_impl(StringView other)
{ {
TRY(EnsureCapasity(len + 1)); TRY(EnsureCapasity(other.Size() + 1));
memcpy(m_data, data, len); memcpy(m_data, other.Data(), other.Size());
m_data[len] = '\0'; m_size = other.Size();
m_size = len; m_data[m_size] = '\0';
return {}; return {};
} }

View File

@ -95,12 +95,12 @@ namespace BAN
if (comp(m_data[i])) if (comp(m_data[i]))
{ {
if (allow_empties || start != i) if (allow_empties || start != i)
result.PushBack(this->Substring(start, i - start)); TRY(result.PushBack(this->Substring(start, i - start)));
start = i + 1; start = i + 1;
} }
} }
if (start != m_size) if (start != m_size)
result.PushBack(this->Substring(start)); TRY(result.PushBack(this->Substring(start)));
return result; return result;
} }

View File

@ -21,7 +21,7 @@ namespace BAN
Queue() = default; Queue() = default;
~Queue(); ~Queue();
ErrorOr<void> Push(const T& value); [[nodiscard]] ErrorOr<void> Push(const T& value);
void Pop(); void Pop();
bool Empty() const; bool Empty() const;
@ -31,7 +31,7 @@ namespace BAN
T& Front(); T& Front();
private: private:
ErrorOr<void> VerifyCapacity(size_type size); [[nodiscard]] ErrorOr<void> VerifyCapacity(size_type size);
private: private:
T* m_data = nullptr; T* m_data = nullptr;

View File

@ -16,8 +16,7 @@ namespace BAN
String(); String();
String(const String&); String(const String&);
String(String&&); String(String&&);
String(const StringView&); String(StringView);
String(const char*, size_type = -1);
~String(); ~String();
template<typename... Args> template<typename... Args>
@ -26,13 +25,15 @@ namespace BAN
String& operator=(const String&); String& operator=(const String&);
String& operator=(String&&); String& operator=(String&&);
ErrorOr<void> PushBack(char); [[nodiscard]] ErrorOr<void> PushBack(char);
ErrorOr<void> Insert(char, size_type); [[nodiscard]] ErrorOr<void> Insert(char, size_type);
ErrorOr<void> Append(const char*); [[nodiscard]] ErrorOr<void> Insert(StringView, size_type);
ErrorOr<void> Append(const String&); [[nodiscard]] ErrorOr<void> Append(StringView);
[[nodiscard]] ErrorOr<void> Append(const String&);
void PopBack(); void PopBack();
void Remove(size_type); void Remove(size_type);
void Erase(size_type, size_type);
void Clear(); void Clear();
@ -43,8 +44,8 @@ namespace BAN
bool operator==(StringView) const; bool operator==(StringView) const;
bool operator==(const char*) const; bool operator==(const char*) const;
ErrorOr<void> Resize(size_type, char = '\0'); [[nodiscard]] ErrorOr<void> Resize(size_type, char = '\0');
ErrorOr<void> Reserve(size_type); [[nodiscard]] ErrorOr<void> Reserve(size_type);
StringView SV() const; StringView SV() const;
@ -55,9 +56,9 @@ namespace BAN
const char* Data() const; const char* Data() const;
private: private:
ErrorOr<void> EnsureCapasity(size_type); [[nodiscard]] ErrorOr<void> EnsureCapasity(size_type);
ErrorOr<void> copy_impl(const char*, size_type); [[nodiscard]] ErrorOr<void> copy_impl(StringView);
void move_impl(String&&); void move_impl(String&&);
private: private:

View File

@ -24,8 +24,8 @@ namespace BAN
StringView Substring(size_type, size_type = -1) const; StringView Substring(size_type, size_type = -1) const;
ErrorOr<Vector<StringView>> Split(char, bool = false); [[nodiscard]] ErrorOr<Vector<StringView>> Split(char, bool = false);
ErrorOr<Vector<StringView>> Split(bool(*comp)(char), bool = false); [[nodiscard]] ErrorOr<Vector<StringView>> Split(bool(*comp)(char), bool = false);
size_type Count(char) const; size_type Count(char) const;
@ -41,6 +41,8 @@ namespace BAN
} }
inline BAN::StringView operator""_sv(const char* str, BAN::StringView::size_type len) { return BAN::StringView(str, len); }
namespace BAN::Formatter namespace BAN::Formatter
{ {

View File

@ -21,8 +21,8 @@ namespace BAN
Vector(const Vector<T>&); Vector(const Vector<T>&);
~Vector(); ~Vector();
ErrorOr<void> PushBack(const T&); [[nodiscard]] ErrorOr<void> PushBack(const T&);
ErrorOr<void> Insert(const T&, size_type); [[nodiscard]] ErrorOr<void> Insert(const T&, size_type);
void PopBack(); void PopBack();
void Remove(size_type); void Remove(size_type);
@ -37,15 +37,15 @@ namespace BAN
const T& Front() const; const T& Front() const;
T& Front(); T& Front();
ErrorOr<void> Resize(size_type); [[nodiscard]] ErrorOr<void> Resize(size_type);
ErrorOr<void> Reserve(size_type); [[nodiscard]] ErrorOr<void> Reserve(size_type);
bool Empty() const; bool Empty() const;
size_type Size() const; size_type Size() const;
size_type Capasity() const; size_type Capasity() const;
private: private:
ErrorOr<void> EnsureCapasity(size_type); [[nodiscard]] ErrorOr<void> EnsureCapasity(size_type);
private: private:
T* m_data = nullptr; T* m_data = nullptr;

View File

@ -288,9 +288,9 @@ namespace Input
bool right = s_mouse_data_buffer[0] & (1 << 1); bool right = s_mouse_data_buffer[0] & (1 << 1);
bool middle = s_mouse_data_buffer[0] & (1 << 2); bool middle = s_mouse_data_buffer[0] & (1 << 2);
if (left) s_mouse_button_event_queue.Push({ .button = MouseButton::Left }); if (left) MUST(s_mouse_button_event_queue.Push({ .button = MouseButton::Left }));
if (right) s_mouse_button_event_queue.Push({ .button = MouseButton::Right }); if (right) MUST(s_mouse_button_event_queue.Push({ .button = MouseButton::Right }));
if (middle) s_mouse_button_event_queue.Push({ .button = MouseButton::Middle }); if (middle) MUST(s_mouse_button_event_queue.Push({ .button = MouseButton::Middle }));
} }
if (s_mouse_data_buffer[1] || s_mouse_data_buffer[2]) if (s_mouse_data_buffer[1] || s_mouse_data_buffer[2])
@ -298,7 +298,7 @@ namespace Input
int16_t rel_x = (int16_t)s_mouse_data_buffer[1] - ((s_mouse_data_buffer[0] << 4) & 0x100); int16_t rel_x = (int16_t)s_mouse_data_buffer[1] - ((s_mouse_data_buffer[0] << 4) & 0x100);
int16_t rel_y = (int16_t)s_mouse_data_buffer[2] - ((s_mouse_data_buffer[0] << 3) & 0x100); int16_t rel_y = (int16_t)s_mouse_data_buffer[2] - ((s_mouse_data_buffer[0] << 3) & 0x100);
s_mouse_move_event_queue.Push({ .dx = rel_x, .dy = rel_y }); MUST(s_mouse_move_event_queue.Push({ .dx = rel_x, .dy = rel_y }));
} }
s_mouse_data_buffer_index = 0; s_mouse_data_buffer_index = 0;
@ -487,12 +487,12 @@ namespace Input
if (update_leds) if (update_leds)
{ {
s_command_queue.Push({ MUST(s_command_queue.Push({
.target = TARGET_KEYBOARD, .target = TARGET_KEYBOARD,
.command = I8042_KB_SET_LEDS, .command = I8042_KB_SET_LEDS,
.data = s_led_states, .data = s_led_states,
.has_data = true, .has_data = true,
}); }));
} }
uint8_t modifiers = 0; uint8_t modifiers = 0;