mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 21:17:42 +00:00
AK: Add StringView::starts_with(char) & StringView::ends_with(char)
This is simply meant to be a more efficient implementation in the case that we only need to check a single character.
This commit is contained in:
parent
a406a8c7d2
commit
854f0b9e1a
3 changed files with 20 additions and 0 deletions
|
@ -105,6 +105,13 @@ Vector<StringView> StringView::lines(bool consider_cr) const
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool StringView::starts_with(char ch) const
|
||||||
|
{
|
||||||
|
if (is_empty())
|
||||||
|
return false;
|
||||||
|
return ch == characters_without_null_termination()[0];
|
||||||
|
}
|
||||||
|
|
||||||
bool StringView::starts_with(const StringView& str) const
|
bool StringView::starts_with(const StringView& str) const
|
||||||
{
|
{
|
||||||
if (str.is_empty())
|
if (str.is_empty())
|
||||||
|
@ -118,6 +125,13 @@ bool StringView::starts_with(const StringView& str) const
|
||||||
return !memcmp(characters_without_null_termination(), str.characters_without_null_termination(), str.length());
|
return !memcmp(characters_without_null_termination(), str.characters_without_null_termination(), str.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool StringView::ends_with(char ch) const
|
||||||
|
{
|
||||||
|
if (is_empty())
|
||||||
|
return false;
|
||||||
|
return ch == characters_without_null_termination()[length() - 1];
|
||||||
|
}
|
||||||
|
|
||||||
bool StringView::ends_with(const StringView& str) const
|
bool StringView::ends_with(const StringView& str) const
|
||||||
{
|
{
|
||||||
if (str.is_empty())
|
if (str.is_empty())
|
||||||
|
|
|
@ -63,6 +63,8 @@ public:
|
||||||
|
|
||||||
bool starts_with(const StringView&) const;
|
bool starts_with(const StringView&) const;
|
||||||
bool ends_with(const StringView&) const;
|
bool ends_with(const StringView&) const;
|
||||||
|
bool starts_with(char) const;
|
||||||
|
bool ends_with(char) const;
|
||||||
|
|
||||||
StringView substring_view(size_t start, size_t length) const;
|
StringView substring_view(size_t start, size_t length) const;
|
||||||
Vector<StringView> split_view(char, bool keep_empty = false) const;
|
Vector<StringView> split_view(char, bool keep_empty = false) const;
|
||||||
|
|
|
@ -63,6 +63,8 @@ TEST_CASE(starts_with)
|
||||||
{
|
{
|
||||||
String test_string = "ABCDEF";
|
String test_string = "ABCDEF";
|
||||||
StringView test_string_view = test_string.view();
|
StringView test_string_view = test_string.view();
|
||||||
|
EXPECT(test_string_view.starts_with('A'));
|
||||||
|
EXPECT(!test_string_view.starts_with('B'));
|
||||||
EXPECT(test_string_view.starts_with("AB"));
|
EXPECT(test_string_view.starts_with("AB"));
|
||||||
EXPECT(test_string_view.starts_with("ABCDEF"));
|
EXPECT(test_string_view.starts_with("ABCDEF"));
|
||||||
EXPECT(!test_string_view.starts_with("DEF"));
|
EXPECT(!test_string_view.starts_with("DEF"));
|
||||||
|
@ -73,6 +75,8 @@ TEST_CASE(ends_with)
|
||||||
String test_string = "ABCDEF";
|
String test_string = "ABCDEF";
|
||||||
StringView test_string_view = test_string.view();
|
StringView test_string_view = test_string.view();
|
||||||
EXPECT(test_string_view.ends_with("DEF"));
|
EXPECT(test_string_view.ends_with("DEF"));
|
||||||
|
EXPECT(test_string_view.ends_with('F'));
|
||||||
|
EXPECT(!test_string_view.ends_with('E'));
|
||||||
EXPECT(test_string_view.ends_with("ABCDEF"));
|
EXPECT(test_string_view.ends_with("ABCDEF"));
|
||||||
EXPECT(!test_string_view.ends_with("ABCDE"));
|
EXPECT(!test_string_view.ends_with("ABCDE"));
|
||||||
EXPECT(!test_string_view.ends_with("ABCDEFG"));
|
EXPECT(!test_string_view.ends_with("ABCDEFG"));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue