mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-22 17:24:48 -05:00
AK: Add StringView::ends_with function
This commit is contained in:
parent
27df4eb43d
commit
24bc674d94
Notes:
sideshowbarker
2024-07-19 10:33:37 +09:00
Author: https://github.com/shannonbooth Commit: https://github.com/SerenityOS/serenity/commit/24bc674d94c Pull-request: https://github.com/SerenityOS/serenity/pull/955
3 changed files with 22 additions and 0 deletions
|
@ -92,6 +92,17 @@ bool StringView::starts_with(const StringView& str) const
|
|||
return !memcmp(characters_without_null_termination(), str.characters_without_null_termination(), str.length());
|
||||
}
|
||||
|
||||
bool StringView::ends_with(const StringView& str) const
|
||||
{
|
||||
if (str.is_empty())
|
||||
return true;
|
||||
if (is_empty())
|
||||
return false;
|
||||
if (str.length() > length())
|
||||
return false;
|
||||
return !memcmp(characters_without_null_termination() + length() - str.length(), str.characters_without_null_termination(), str.length());
|
||||
}
|
||||
|
||||
StringView StringView::substring_view(size_t start, size_t length) const
|
||||
{
|
||||
if (!length)
|
||||
|
|
|
@ -42,6 +42,7 @@ public:
|
|||
unsigned hash() const;
|
||||
|
||||
bool starts_with(const StringView&) const;
|
||||
bool ends_with(const StringView&) const;
|
||||
|
||||
StringView substring_view(size_t start, size_t length) const;
|
||||
Vector<StringView> split_view(char, bool keep_empty = false) const;
|
||||
|
|
|
@ -42,6 +42,16 @@ TEST_CASE(starts_with)
|
|||
EXPECT(!test_string_view.starts_with("DEF"));
|
||||
}
|
||||
|
||||
TEST_CASE(ends_with)
|
||||
{
|
||||
String test_string = "ABCDEF";
|
||||
StringView test_string_view = test_string.view();
|
||||
EXPECT(test_string_view.ends_with("DEF"));
|
||||
EXPECT(test_string_view.ends_with("ABCDEF"));
|
||||
EXPECT(!test_string_view.ends_with("ABCDE"));
|
||||
EXPECT(!test_string_view.ends_with("ABCDEFG"));
|
||||
}
|
||||
|
||||
TEST_CASE(lines)
|
||||
{
|
||||
String test_string = "a\nb\r\nc\rd";
|
||||
|
|
Loading…
Reference in a new issue