From 95441dabd552cbb10748c499055ead5504e57bde Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 10 May 2025 16:30:36 -0400 Subject: [PATCH] AK: Define some simple getters in a single line This matches our coding style everywhere else. clang-format used to have trouble with these, but it doesn't any longer. --- AK/Error.cpp | 1 + AK/Error.h | 89 ++++++++++++++++++++----------------------------- AK/StringView.h | 14 ++------ 3 files changed, 41 insertions(+), 63 deletions(-) diff --git a/AK/Error.cpp b/AK/Error.cpp index bd70c169ce..3d87839eed 100644 --- a/AK/Error.cpp +++ b/AK/Error.cpp @@ -7,6 +7,7 @@ */ #include + #ifdef AK_OS_WINDOWS # include # include diff --git a/AK/Error.h b/AK/Error.h index 6db779c5a4..9f4679b5b8 100644 --- a/AK/Error.h +++ b/AK/Error.h @@ -15,9 +15,6 @@ namespace AK { class [[nodiscard]] Error { public: - ALWAYS_INLINE Error(Error&&) = default; - ALWAYS_INLINE Error& operator=(Error&&) = default; - enum class Kind : u8 { Errno, Syscall, @@ -31,42 +28,50 @@ public: return Error(code); } -#ifdef AK_OS_WINDOWS - static Error from_windows_error(u32 windows_error); - static Error from_windows_error(); -#endif - static Error from_syscall(StringView syscall_name, int code) { return Error(syscall_name, code); } - static Error from_string_view(StringView string_literal) { return Error(string_literal); } + + // Prefer `from_string_literal` when directly typing out an error message: + // + // return Error::from_string_literal("Class: Some failure"); + // + // If you need to return a static string based on a dynamic condition (like picking an error from an array), then + // prefer `from_string_view` instead. + template + ALWAYS_INLINE static Error from_string_literal(char const (&string_literal)[N]) + { + return from_string_view(StringView { string_literal, N - 1 }); + } + + static Error from_string_view(StringView string_literal) + { + return Error(string_literal); + } template T> static Error from_string_view(T) { - // `Error::from_string_view(ByteString::formatted(...))` is a somewhat common mistake, which leads to a UAF situation. - // If your string outlives this error and _isn't_ a temporary being passed to this function, explicitly call .view() on it to resolve to the StringView overload. + // `Error::from_string_view(ByteString::formatted(...))` is a somewhat common mistake, which leads to a UAF + // situation. If your string outlives this error and _isn't_ a temporary being passed to this function, + // explicitly call .view() on it to resolve to the StringView overload. static_assert(DependentFalse, "Error::from_string_view(String) is almost always a use-after-free"); VERIFY_NOT_REACHED(); } +#ifdef AK_OS_WINDOWS + static Error from_windows_error(u32 windows_error); + static Error from_windows_error(); +#endif + static Error copy(Error const& error) { return Error(error); } - // NOTE: Prefer `from_string_literal` when directly typing out an error message: - // - // return Error::from_string_literal("Class: Some failure"); - // - // If you need to return a static string based on a dynamic condition (like - // picking an error from an array), then prefer `from_string_view` instead. - template - ALWAYS_INLINE static Error from_string_literal(char const (&string_literal)[N]) - { - return from_string_view(StringView { string_literal, N - 1 }); - } + ALWAYS_INLINE Error(Error&&) = default; + ALWAYS_INLINE Error& operator=(Error&&) = default; bool operator==(Error const& other) const { @@ -74,30 +79,15 @@ public: } int code() const { return m_code; } - bool is_errno() const - { - return m_kind == Kind::Errno || m_kind == Kind::Syscall; - } - bool is_syscall() const - { - return m_kind == Kind::Syscall; - } - bool is_windows_error() const - { - return m_kind == Kind::Windows; - } - bool is_string_literal() const - { - return m_kind == Kind::StringLiteral; - } - StringView string_literal() const - { - return m_string_literal; - } - Kind kind() const - { - return m_kind; - } + bool is_errno() const { return m_kind == Kind::Errno || m_kind == Kind::Syscall; } + bool is_syscall() const { return m_kind == Kind::Syscall; } + + bool is_windows_error() const { return m_kind == Kind::Windows; } + + bool is_string_literal() const { return m_kind == Kind::StringLiteral; } + StringView string_literal() const { return m_string_literal; } + + Kind kind() const { return m_kind; } protected: Error(int code, Kind kind = Kind::Errno) @@ -124,9 +114,7 @@ private: Error& operator=(Error const&) = default; StringView m_string_literal; - int m_code { 0 }; - Kind m_kind {}; }; @@ -166,10 +154,7 @@ public: { } - T& value() - { - return m_value_or_error.template get(); - } + T& value() { return m_value_or_error.template get(); } T const& value() const { return m_value_or_error.template get(); } ErrorType& error() { return m_value_or_error.template get(); } diff --git a/AK/StringView.h b/AK/StringView.h index 6c389d6434..de3c854a2b 100644 --- a/AK/StringView.h +++ b/AK/StringView.h @@ -57,10 +57,7 @@ public: template StringType> StringView& operator=(StringType&&) = delete; - [[nodiscard]] constexpr bool is_null() const - { - return m_characters == nullptr; - } + [[nodiscard]] constexpr bool is_null() const { return m_characters == nullptr; } [[nodiscard]] constexpr bool is_empty() const { return m_length == 0; } [[nodiscard]] constexpr char const* characters_without_null_termination() const { return m_characters; } @@ -106,10 +103,7 @@ public: [[nodiscard]] String to_ascii_uppercase_string() const; [[nodiscard]] String to_ascii_titlecase_string() const; - [[nodiscard]] Optional find(char needle, size_t start = 0) const - { - return StringUtils::find(*this, needle, start); - } + [[nodiscard]] Optional find(char needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); } [[nodiscard]] Optional find(StringView needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); } [[nodiscard]] Optional find_last(char needle) const { return StringUtils::find_last(*this, needle); } [[nodiscard]] Optional find_last(StringView needle) const { return StringUtils::find_last(*this, needle); } @@ -311,11 +305,8 @@ public: } constexpr bool operator<(StringView other) const { return compare(other) < 0; } - constexpr bool operator<=(StringView other) const { return compare(other) <= 0; } - constexpr bool operator>(StringView other) const { return compare(other) > 0; } - constexpr bool operator>=(StringView other) const { return compare(other) >= 0; } [[nodiscard]] ByteString to_byte_string() const; @@ -326,6 +317,7 @@ public: } [[nodiscard]] ByteString replace(StringView needle, StringView replacement, ReplaceMode) const; + [[nodiscard]] size_t count(StringView needle) const { return StringUtils::count(*this, needle);