From 61bd9da8e07211a9580db486f9d51b23eb3883cd Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 12 Jan 2023 23:57:07 +0200 Subject: [PATCH] BAN: Add [[nodiscard]] to BAN functions returning ErrorOr --- BAN/BAN/String.cpp | 89 ++++++++++++++++++++---------------- BAN/BAN/StringView.cpp | 4 +- BAN/include/BAN/Queue.h | 4 +- BAN/include/BAN/String.h | 21 +++++---- BAN/include/BAN/StringView.h | 6 ++- BAN/include/BAN/Vector.h | 10 ++-- kernel/kernel/Input.cpp | 12 ++--- 7 files changed, 80 insertions(+), 66 deletions(-) diff --git a/BAN/BAN/String.cpp b/BAN/BAN/String.cpp index 4f119981..2429e1c4 100644 --- a/BAN/BAN/String.cpp +++ b/BAN/BAN/String.cpp @@ -12,12 +12,12 @@ namespace BAN String::String() { - MUST(copy_impl("", 0)); + MUST(copy_impl(""_sv)); } String::String(const String& other) { - MUST(copy_impl(other.Data(), other.Size())); + MUST(copy_impl(other.SV())); } String::String(String&& other) @@ -25,16 +25,9 @@ namespace BAN move_impl(Move(other)); } - String::String(const StringView& other) + String::String(StringView other) { - MUST(copy_impl(other.Data(), other.Size())); - } - - String::String(const char* data, size_type len) - { - if (len == size_type(-1)) - len = strlen(data); - MUST(copy_impl(data, len)); + MUST(copy_impl(other)); } String::~String() @@ -44,7 +37,7 @@ namespace BAN String& String::operator=(const String& other) { - copy_impl(other.Data(), other.Size()); + MUST(copy_impl(other.SV())); return *this; } @@ -58,58 +51,75 @@ namespace BAN ErrorOr String::PushBack(char ch) { TRY(EnsureCapasity(m_size + 2)); - m_data[m_size] = ch; - m_data[m_size + 1] = '\0'; + m_data[m_size] = ch; m_size++; + m_data[m_size] = '\0'; return {}; } ErrorOr String::Insert(char ch, size_type index) { 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); - m_data[index] = ch; - m_data[m_size + 1] = '\0'; - m_size++; + m_data[index] = ch; + m_size += 1; + m_data[m_size] = '\0'; return {}; } - ErrorOr String::Append(const char* string) + ErrorOr String::Insert(StringView other, size_type index) { - size_t len = strlen(string); - TRY(EnsureCapasity(m_size + len + 1)); - memcpy(m_data + m_size, string, len); - m_data[m_size + len] = '\0'; - m_size += len; + dprintln("insert '{}' to '{}' at index {}", other, *this, index); + + ASSERT(index <= m_size); + TRY(EnsureCapasity(m_size + other.Size() + 1)); + 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 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 {}; } ErrorOr String::Append(const String& string) { - TRY(Append(string.Data())); + TRY(Append(string.SV())); return {}; } void String::PopBack() { ASSERT(m_size > 0); - m_data[m_size - 1] = '\0'; m_size--; + m_data[m_size] = '\0'; } void String::Remove(size_type index) { - ASSERT(index < m_size); - memmove(m_data + index, m_data + index + 1, m_size - index - 1); - m_data[m_size - 1] = '\0'; - m_size--; + Erase(index, 1); + } + + 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() { - m_data[0] = '\0'; m_size = 0; + m_data[0] = '\0'; } char String::operator[](size_type index) const @@ -140,9 +150,10 @@ namespace BAN bool String::operator==(const char* other) const { - if (memcmp(m_data, other, m_size)) - return false; - return other[m_size] == '\0'; + for (size_type i = 0; i <= m_size; i++) + if (m_data[i] != other[i]) + return false; + return true; } ErrorOr String::Resize(size_type size, char ch) @@ -210,12 +221,12 @@ namespace BAN return {}; } - ErrorOr String::copy_impl(const char* data, size_type len) + ErrorOr String::copy_impl(StringView other) { - TRY(EnsureCapasity(len + 1)); - memcpy(m_data, data, len); - m_data[len] = '\0'; - m_size = len; + TRY(EnsureCapasity(other.Size() + 1)); + memcpy(m_data, other.Data(), other.Size()); + m_size = other.Size(); + m_data[m_size] = '\0'; return {}; } diff --git a/BAN/BAN/StringView.cpp b/BAN/BAN/StringView.cpp index 95fa60c5..4b311aab 100644 --- a/BAN/BAN/StringView.cpp +++ b/BAN/BAN/StringView.cpp @@ -95,12 +95,12 @@ namespace BAN if (comp(m_data[i])) { if (allow_empties || start != i) - result.PushBack(this->Substring(start, i - start)); + TRY(result.PushBack(this->Substring(start, i - start))); start = i + 1; } } if (start != m_size) - result.PushBack(this->Substring(start)); + TRY(result.PushBack(this->Substring(start))); return result; } diff --git a/BAN/include/BAN/Queue.h b/BAN/include/BAN/Queue.h index 4ba475bc..39a20842 100644 --- a/BAN/include/BAN/Queue.h +++ b/BAN/include/BAN/Queue.h @@ -21,7 +21,7 @@ namespace BAN Queue() = default; ~Queue(); - ErrorOr Push(const T& value); + [[nodiscard]] ErrorOr Push(const T& value); void Pop(); bool Empty() const; @@ -31,7 +31,7 @@ namespace BAN T& Front(); private: - ErrorOr VerifyCapacity(size_type size); + [[nodiscard]] ErrorOr VerifyCapacity(size_type size); private: T* m_data = nullptr; diff --git a/BAN/include/BAN/String.h b/BAN/include/BAN/String.h index 1782dd9b..3ec3d6cf 100644 --- a/BAN/include/BAN/String.h +++ b/BAN/include/BAN/String.h @@ -16,8 +16,7 @@ namespace BAN String(); String(const String&); String(String&&); - String(const StringView&); - String(const char*, size_type = -1); + String(StringView); ~String(); template @@ -26,13 +25,15 @@ namespace BAN String& operator=(const String&); String& operator=(String&&); - ErrorOr PushBack(char); - ErrorOr Insert(char, size_type); - ErrorOr Append(const char*); - ErrorOr Append(const String&); + [[nodiscard]] ErrorOr PushBack(char); + [[nodiscard]] ErrorOr Insert(char, size_type); + [[nodiscard]] ErrorOr Insert(StringView, size_type); + [[nodiscard]] ErrorOr Append(StringView); + [[nodiscard]] ErrorOr Append(const String&); void PopBack(); void Remove(size_type); + void Erase(size_type, size_type); void Clear(); @@ -43,8 +44,8 @@ namespace BAN bool operator==(StringView) const; bool operator==(const char*) const; - ErrorOr Resize(size_type, char = '\0'); - ErrorOr Reserve(size_type); + [[nodiscard]] ErrorOr Resize(size_type, char = '\0'); + [[nodiscard]] ErrorOr Reserve(size_type); StringView SV() const; @@ -55,9 +56,9 @@ namespace BAN const char* Data() const; private: - ErrorOr EnsureCapasity(size_type); + [[nodiscard]] ErrorOr EnsureCapasity(size_type); - ErrorOr copy_impl(const char*, size_type); + [[nodiscard]] ErrorOr copy_impl(StringView); void move_impl(String&&); private: diff --git a/BAN/include/BAN/StringView.h b/BAN/include/BAN/StringView.h index 8904de5f..a786c2a7 100644 --- a/BAN/include/BAN/StringView.h +++ b/BAN/include/BAN/StringView.h @@ -24,8 +24,8 @@ namespace BAN StringView Substring(size_type, size_type = -1) const; - ErrorOr> Split(char, bool = false); - ErrorOr> Split(bool(*comp)(char), bool = false); + [[nodiscard]] ErrorOr> Split(char, bool = false); + [[nodiscard]] ErrorOr> Split(bool(*comp)(char), bool = false); 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 { diff --git a/BAN/include/BAN/Vector.h b/BAN/include/BAN/Vector.h index 3e2af8db..8b6ecef4 100644 --- a/BAN/include/BAN/Vector.h +++ b/BAN/include/BAN/Vector.h @@ -21,8 +21,8 @@ namespace BAN Vector(const Vector&); ~Vector(); - ErrorOr PushBack(const T&); - ErrorOr Insert(const T&, size_type); + [[nodiscard]] ErrorOr PushBack(const T&); + [[nodiscard]] ErrorOr Insert(const T&, size_type); void PopBack(); void Remove(size_type); @@ -37,15 +37,15 @@ namespace BAN const T& Front() const; T& Front(); - ErrorOr Resize(size_type); - ErrorOr Reserve(size_type); + [[nodiscard]] ErrorOr Resize(size_type); + [[nodiscard]] ErrorOr Reserve(size_type); bool Empty() const; size_type Size() const; size_type Capasity() const; private: - ErrorOr EnsureCapasity(size_type); + [[nodiscard]] ErrorOr EnsureCapasity(size_type); private: T* m_data = nullptr; diff --git a/kernel/kernel/Input.cpp b/kernel/kernel/Input.cpp index 2383dc23..aa83ac1c 100644 --- a/kernel/kernel/Input.cpp +++ b/kernel/kernel/Input.cpp @@ -288,9 +288,9 @@ namespace Input bool right = s_mouse_data_buffer[0] & (1 << 1); bool middle = s_mouse_data_buffer[0] & (1 << 2); - if (left) s_mouse_button_event_queue.Push({ .button = MouseButton::Left }); - if (right) s_mouse_button_event_queue.Push({ .button = MouseButton::Right }); - if (middle) s_mouse_button_event_queue.Push({ .button = MouseButton::Middle }); + if (left) MUST(s_mouse_button_event_queue.Push({ .button = MouseButton::Left })); + if (right) MUST(s_mouse_button_event_queue.Push({ .button = MouseButton::Right })); + if (middle) MUST(s_mouse_button_event_queue.Push({ .button = MouseButton::Middle })); } 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_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; @@ -487,12 +487,12 @@ namespace Input if (update_leds) { - s_command_queue.Push({ + MUST(s_command_queue.Push({ .target = TARGET_KEYBOARD, .command = I8042_KB_SET_LEDS, .data = s_led_states, .has_data = true, - }); + })); } uint8_t modifiers = 0;