1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-08 05:27:14 +09:00

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.
This commit is contained in:
Timothy Flynn 2025-05-10 16:30:36 -04:00 committed by Tim Flynn
parent dceed08058
commit 95441dabd5
Notes: github-actions[bot] 2025-05-11 01:20:56 +00:00
3 changed files with 41 additions and 63 deletions

View file

@ -7,6 +7,7 @@
*/
#include <AK/Error.h>
#ifdef AK_OS_WINDOWS
# include <AK/ByteString.h>
# include <AK/HashMap.h>

View file

@ -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<size_t N>
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<OneOf<ByteString, String, FlyString> 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<T>, "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<size_t N>
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>();
}
T& value() { return m_value_or_error.template get<T>(); }
T const& value() const { return m_value_or_error.template get<T>(); }
ErrorType& error() { return m_value_or_error.template get<ErrorType>(); }

View file

@ -57,10 +57,7 @@ public:
template<OneOf<String, FlyString, ByteString, ByteBuffer> 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<size_t> find(char needle, size_t start = 0) const
{
return StringUtils::find(*this, needle, start);
}
[[nodiscard]] Optional<size_t> find(char needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); }
[[nodiscard]] Optional<size_t> find(StringView needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); }
[[nodiscard]] Optional<size_t> find_last(char needle) const { return StringUtils::find_last(*this, needle); }
[[nodiscard]] Optional<size_t> 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);