1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:17:44 +00:00

AK/GenericLexer: constexpr where possible

Problem:
- Much of the `GenericLexer` can be `constexpr`, but is not.

Solution:
- Make it `constexpr` and de-duplicate code.
- Extend some of `StringView` with `constexpr` to support.
- Add tests to ensure `constexpr` behavior.

Note:
- Construction of `StringView` from pointer and length is not
  `constexpr`-compatible at the moment because the VERIFY cannot be,
  yet.
This commit is contained in:
Lenny Maiorani 2021-04-21 21:19:39 -06:00 committed by Linus Groh
parent c2280a907d
commit 254e010c75
6 changed files with 303 additions and 188 deletions

View file

@ -46,15 +46,15 @@ public:
StringView(const String&);
StringView(const FlyString&);
[[nodiscard]] bool is_null() const { return !m_characters; }
[[nodiscard]] bool is_empty() const { return m_length == 0; }
[[nodiscard]] constexpr bool is_null() const { return !m_characters; }
[[nodiscard]] constexpr bool is_empty() const { return m_length == 0; }
[[nodiscard]] const char* characters_without_null_termination() const { return m_characters; }
[[nodiscard]] size_t length() const { return m_length; }
[[nodiscard]] constexpr size_t length() const { return m_length; }
[[nodiscard]] ReadonlyBytes bytes() const { return { m_characters, m_length }; }
const char& operator[](size_t index) const { return m_characters[index]; }
constexpr const char& operator[](size_t index) const { return m_characters[index]; }
using ConstIterator = SimpleIterator<const StringView, const char>;
@ -84,8 +84,17 @@ public:
Optional<size_t> find(const StringView&) const;
Optional<size_t> find(char c) const;
[[nodiscard]] StringView substring_view(size_t start, size_t length) const;
[[nodiscard]] StringView substring_view(size_t start) const;
[[nodiscard]] constexpr StringView substring_view(size_t start, size_t length) const
{
VERIFY(start + length <= m_length);
return { m_characters + start, length };
}
[[nodiscard]] constexpr StringView substring_view(size_t start) const
{
return substring_view(start, length() - start);
}
[[nodiscard]] Vector<StringView> split_view(char, bool keep_empty = false) const;
[[nodiscard]] Vector<StringView> split_view(const StringView&, bool keep_empty = false) const;
@ -166,7 +175,7 @@ public:
bool operator==(const String&) const;
bool operator==(const StringView& other) const
constexpr bool operator==(const StringView& other) const
{
if (is_null())
return other.is_null();
@ -177,7 +186,7 @@ public:
return !__builtin_memcmp(m_characters, other.m_characters, m_length);
}
bool operator!=(const StringView& other) const
constexpr bool operator!=(const StringView& other) const
{
return !(*this == other);
}