mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-22 17:24:48 -05:00
AK: Add String starts_with(char) & 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
0e3a9d8e9d
commit
9920d17342
Notes:
sideshowbarker
2024-07-19 09:19:24 +09:00
Author: https://github.com/shannonbooth Commit: https://github.com/SerenityOS/serenity/commit/9920d17342b Pull-request: https://github.com/SerenityOS/serenity/pull/1222
3 changed files with 19 additions and 0 deletions
|
@ -295,6 +295,13 @@ bool String::starts_with(const StringView& str) const
|
||||||
return !memcmp(characters(), str.characters_without_null_termination(), str.length());
|
return !memcmp(characters(), str.characters_without_null_termination(), str.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool String::starts_with(char ch) const
|
||||||
|
{
|
||||||
|
if (is_empty())
|
||||||
|
return false;
|
||||||
|
return characters()[0] == ch;
|
||||||
|
}
|
||||||
|
|
||||||
bool String::ends_with(const StringView& str) const
|
bool String::ends_with(const StringView& str) const
|
||||||
{
|
{
|
||||||
if (str.is_empty())
|
if (str.is_empty())
|
||||||
|
@ -306,6 +313,12 @@ bool String::ends_with(const StringView& str) const
|
||||||
return !memcmp(characters() + (length() - str.length()), str.characters_without_null_termination(), str.length());
|
return !memcmp(characters() + (length() - str.length()), str.characters_without_null_termination(), str.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool String::ends_with(char ch) const
|
||||||
|
{
|
||||||
|
if (is_empty())
|
||||||
|
return false;
|
||||||
|
return characters()[length() - 1] == ch;
|
||||||
|
}
|
||||||
String String::repeated(char ch, size_t count)
|
String String::repeated(char ch, size_t count)
|
||||||
{
|
{
|
||||||
if (!count)
|
if (!count)
|
||||||
|
|
|
@ -157,6 +157,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;
|
||||||
|
|
||||||
bool operator==(const String&) const;
|
bool operator==(const String&) const;
|
||||||
bool operator!=(const String& other) const { return !(*this == other); }
|
bool operator!=(const String& other) const { return !(*this == other); }
|
||||||
|
|
|
@ -80,6 +80,8 @@ TEST_CASE(starts_with)
|
||||||
{
|
{
|
||||||
String test_string = "ABCDEF";
|
String test_string = "ABCDEF";
|
||||||
EXPECT(test_string.starts_with("AB"));
|
EXPECT(test_string.starts_with("AB"));
|
||||||
|
EXPECT(test_string.starts_with('A'));
|
||||||
|
EXPECT(!test_string.starts_with('B'));
|
||||||
EXPECT(test_string.starts_with("ABCDEF"));
|
EXPECT(test_string.starts_with("ABCDEF"));
|
||||||
EXPECT(!test_string.starts_with("DEF"));
|
EXPECT(!test_string.starts_with("DEF"));
|
||||||
}
|
}
|
||||||
|
@ -88,6 +90,8 @@ TEST_CASE(ends_with)
|
||||||
{
|
{
|
||||||
String test_string = "ABCDEF";
|
String test_string = "ABCDEF";
|
||||||
EXPECT(test_string.ends_with("EF"));
|
EXPECT(test_string.ends_with("EF"));
|
||||||
|
EXPECT(test_string.ends_with('F'));
|
||||||
|
EXPECT(!test_string.ends_with('E'));
|
||||||
EXPECT(test_string.ends_with("ABCDEF"));
|
EXPECT(test_string.ends_with("ABCDEF"));
|
||||||
EXPECT(!test_string.ends_with("ABC"));
|
EXPECT(!test_string.ends_with("ABC"));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue