ladybird/AK/StringView.h
Lenny Maiorani 254e010c75 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.
2021-04-22 20:27:21 +02:00

234 lines
8.3 KiB
C++

/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Assertions.h>
#include <AK/Checked.h>
#include <AK/Forward.h>
#include <AK/Span.h>
#include <AK/StdLibExtras.h>
#include <AK/StringUtils.h>
#include <AK/Vector.h>
namespace AK {
class StringView {
public:
ALWAYS_INLINE constexpr StringView() = default;
ALWAYS_INLINE constexpr StringView(const char* characters, size_t length)
: m_characters(characters)
, m_length(length)
{
VERIFY(!Checked<uintptr_t>::addition_would_overflow((uintptr_t)characters, length));
}
ALWAYS_INLINE StringView(const unsigned char* characters, size_t length)
: m_characters((const char*)characters)
, m_length(length)
{
VERIFY(!Checked<uintptr_t>::addition_would_overflow((uintptr_t)characters, length));
}
ALWAYS_INLINE constexpr StringView(const char* cstring)
: m_characters(cstring)
, m_length(cstring ? __builtin_strlen(cstring) : 0)
{
}
ALWAYS_INLINE StringView(ReadonlyBytes bytes)
: m_characters(reinterpret_cast<const char*>(bytes.data()))
, m_length(bytes.size())
{
}
StringView(const ByteBuffer&);
StringView(const String&);
StringView(const FlyString&);
[[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]] constexpr size_t length() const { return m_length; }
[[nodiscard]] ReadonlyBytes bytes() const { return { m_characters, m_length }; }
constexpr const char& operator[](size_t index) const { return m_characters[index]; }
using ConstIterator = SimpleIterator<const StringView, const char>;
[[nodiscard]] constexpr ConstIterator begin() const { return ConstIterator::begin(*this); }
[[nodiscard]] constexpr ConstIterator end() const { return ConstIterator::end(*this); }
[[nodiscard]] unsigned hash() const;
[[nodiscard]] bool starts_with(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
[[nodiscard]] bool ends_with(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
[[nodiscard]] bool starts_with(char) const;
[[nodiscard]] bool ends_with(char) const;
[[nodiscard]] bool matches(const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;
[[nodiscard]] bool matches(const StringView& mask, Vector<MaskSpan>&, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;
[[nodiscard]] bool contains(char) const;
[[nodiscard]] bool contains(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
[[nodiscard]] bool equals_ignoring_case(const StringView& other) const;
[[nodiscard]] StringView trim_whitespace(TrimMode mode = TrimMode::Both) const { return StringUtils::trim_whitespace(*this, mode); }
Optional<size_t> find_first_of(char) const;
Optional<size_t> find_first_of(const StringView&) const;
Optional<size_t> find_last_of(char) const;
Optional<size_t> find_last_of(const StringView&) const;
Optional<size_t> find(const StringView&) const;
Optional<size_t> find(char c) 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;
template<typename UnaryPredicate>
[[nodiscard]] Vector<StringView> split_view_if(UnaryPredicate&& predicate, bool keep_empty = false) const
{
if (is_empty())
return {};
Vector<StringView> v;
size_t substart = 0;
for (size_t i = 0; i < length(); ++i) {
char ch = characters_without_null_termination()[i];
if (predicate(ch)) {
size_t sublen = i - substart;
if (sublen != 0 || keep_empty)
v.append(substring_view(substart, sublen));
substart = i + 1;
}
}
size_t taillen = length() - substart;
if (taillen != 0 || keep_empty)
v.append(substring_view(substart, taillen));
return v;
}
// Create a Vector of StringViews split by line endings. As of CommonMark
// 0.29, the spec defines a line ending as "a newline (U+000A), a carriage
// return (U+000D) not followed by a newline, or a carriage return and a
// following newline.".
[[nodiscard]] Vector<StringView> lines(bool consider_cr = true) const;
template<typename T = int>
Optional<T> to_int() const;
template<typename T = unsigned>
Optional<T> to_uint() const;
// Create a new substring view of this string view, starting either at the beginning of
// the given substring view, or after its end, and continuing until the end of this string
// view (that is, for the remaining part of its length). For example,
//
// StringView str { "foobar" };
// StringView substr = str.substring_view(1, 2); // "oo"
// StringView substr_from = str.substring_view_starting_from_substring(subst); // "oobar"
// StringView substr_after = str.substring_view_starting_after_substring(subst); // "bar"
//
// Note that this only works if the string view passed as an argument is indeed a substring
// view of this string view, such as one created by substring_view() and split_view(). It
// does not work for arbitrary strings; for example declaring substr in the example above as
//
// StringView substr { "oo" };
//
// would not work.
[[nodiscard]] StringView substring_view_starting_from_substring(const StringView& substring) const;
[[nodiscard]] StringView substring_view_starting_after_substring(const StringView& substring) const;
bool operator==(const char* cstring) const
{
if (is_null())
return !cstring;
if (!cstring)
return false;
// NOTE: `m_characters` is not guaranteed to be null-terminated, but `cstring` is.
const char* cp = cstring;
for (size_t i = 0; i < m_length; ++i) {
if (!*cp)
return false;
if (m_characters[i] != *(cp++))
return false;
}
return !*cp;
}
bool operator!=(const char* cstring) const
{
return !(*this == cstring);
}
bool operator==(const String&) const;
constexpr bool operator==(const StringView& other) const
{
if (is_null())
return other.is_null();
if (other.is_null())
return false;
if (length() != other.length())
return false;
return !__builtin_memcmp(m_characters, other.m_characters, m_length);
}
constexpr bool operator!=(const StringView& other) const
{
return !(*this == other);
}
bool operator<(const StringView& other) const
{
if (int c = __builtin_memcmp(m_characters, other.m_characters, min(m_length, other.m_length)))
return c < 0;
return m_length < other.m_length;
}
[[nodiscard]] String to_string() const;
[[nodiscard]] bool is_whitespace() const { return StringUtils::is_whitespace(*this); }
template<typename T, typename... Rest>
[[nodiscard]] bool is_one_of(const T& string, Rest... rest) const
{
if (*this == string)
return true;
return is_one_of(rest...);
}
private:
[[nodiscard]] bool is_one_of() const { return false; }
friend class String;
const char* m_characters { nullptr };
size_t m_length { 0 };
};
template<>
struct Traits<StringView> : public GenericTraits<String> {
static unsigned hash(const StringView& s) { return s.hash(); }
};
}
[[nodiscard]] ALWAYS_INLINE constexpr AK::StringView operator"" sv(const char* cstring, size_t length)
{
return AK::StringView(cstring, length);
}
using AK::StringView;