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()
{
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<void> 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<void> 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<void> String::Append(const char* string)
ErrorOr<void> 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<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 {};
}
ErrorOr<void> 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<void> String::Resize(size_type size, char ch)
@ -210,12 +221,12 @@ namespace BAN
return {};
}
ErrorOr<void> String::copy_impl(const char* data, size_type len)
ErrorOr<void> 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 {};
}

View File

@ -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;
}

View File

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

View File

@ -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<typename... Args>
@ -26,13 +25,15 @@ namespace BAN
String& operator=(const String&);
String& operator=(String&&);
ErrorOr<void> PushBack(char);
ErrorOr<void> Insert(char, size_type);
ErrorOr<void> Append(const char*);
ErrorOr<void> Append(const String&);
[[nodiscard]] ErrorOr<void> PushBack(char);
[[nodiscard]] ErrorOr<void> Insert(char, size_type);
[[nodiscard]] ErrorOr<void> Insert(StringView, size_type);
[[nodiscard]] ErrorOr<void> Append(StringView);
[[nodiscard]] ErrorOr<void> 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<void> Resize(size_type, char = '\0');
ErrorOr<void> Reserve(size_type);
[[nodiscard]] ErrorOr<void> Resize(size_type, char = '\0');
[[nodiscard]] ErrorOr<void> Reserve(size_type);
StringView SV() const;
@ -55,9 +56,9 @@ namespace BAN
const char* Data() const;
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&&);
private:

View File

@ -24,8 +24,8 @@ namespace BAN
StringView Substring(size_type, size_type = -1) const;
ErrorOr<Vector<StringView>> Split(char, bool = false);
ErrorOr<Vector<StringView>> Split(bool(*comp)(char), bool = false);
[[nodiscard]] ErrorOr<Vector<StringView>> Split(char, bool = false);
[[nodiscard]] ErrorOr<Vector<StringView>> 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
{

View File

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

View File

@ -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;