mirror of
https://github.com/SerenityOS/serenity
synced 2024-07-22 10:36:24 +00:00
AK: Implement Span::starts_with()
Useful for checking for contents at the start of a span.
This commit is contained in:
parent
aacbee8ed8
commit
1069979ddf
|
@ -176,6 +176,14 @@ public:
|
|||
return false;
|
||||
}
|
||||
|
||||
bool constexpr starts_with(Span<const T> other) const
|
||||
{
|
||||
if (size() < other.size())
|
||||
return false;
|
||||
|
||||
return TypedTransfer<T>::compare(data(), other.data(), other.size());
|
||||
}
|
||||
|
||||
ALWAYS_INLINE constexpr const T& at(size_t index) const
|
||||
{
|
||||
VERIFY(index < this->m_size);
|
||||
|
|
|
@ -123,3 +123,19 @@ TEST_CASE(span_from_c_string)
|
|||
const char* str = "Serenity";
|
||||
[[maybe_unused]] ReadonlyBytes bytes { str, strlen(str) };
|
||||
}
|
||||
|
||||
TEST_CASE(starts_with)
|
||||
{
|
||||
const char* str = "HeyFriends!";
|
||||
ReadonlyBytes bytes { str, strlen(str) };
|
||||
const char* str_hey = "Hey";
|
||||
ReadonlyBytes hey_bytes { str_hey, strlen(str_hey) };
|
||||
EXPECT(bytes.starts_with(hey_bytes));
|
||||
const char* str_nah = "Nah";
|
||||
ReadonlyBytes nah_bytes { str_nah, strlen(str_nah) };
|
||||
EXPECT(!bytes.starts_with(nah_bytes));
|
||||
|
||||
const u8 hey_array[3] = { 'H', 'e', 'y' };
|
||||
ReadonlyBytes hey_bytes_u8 { hey_array, 3 };
|
||||
EXPECT(bytes.starts_with(hey_bytes_u8));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue