mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Add String::operator==(const char*).
Without this function, comparing a String to a const char* will instantiate a temporary String which is obviously not great. Also add some missing null checks to StringView::operator==(const char*).
This commit is contained in:
parent
8b1154f5f2
commit
6a51093ab1
Notes:
sideshowbarker
2024-07-19 13:40:15 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/6a51093ab14
2 changed files with 19 additions and 0 deletions
|
@ -133,6 +133,20 @@ public:
|
|||
bool operator!=(const String& other) const { return !(*this == other); }
|
||||
bool operator<(const String&) const;
|
||||
|
||||
bool operator==(const char* cstring) const
|
||||
{
|
||||
if (is_null())
|
||||
return !cstring;
|
||||
if (!cstring)
|
||||
return false;
|
||||
return !strcmp(characters(), cstring);
|
||||
}
|
||||
|
||||
bool operator!=(const char* cstring) const
|
||||
{
|
||||
return !(*this == cstring);
|
||||
}
|
||||
|
||||
String isolated_copy() const;
|
||||
|
||||
static String empty();
|
||||
|
|
|
@ -29,6 +29,7 @@ public:
|
|||
}
|
||||
StringView(const AK::String& string);
|
||||
|
||||
bool is_null() const { return !m_characters; }
|
||||
bool is_empty() const { return m_length == 0; }
|
||||
const char* characters() const { return m_characters; }
|
||||
int length() const { return m_length; }
|
||||
|
@ -40,6 +41,10 @@ public:
|
|||
|
||||
bool operator==(const char* cstring) const
|
||||
{
|
||||
if (is_null())
|
||||
return !cstring;
|
||||
if (!cstring)
|
||||
return false;
|
||||
int other_length = strlen(cstring);
|
||||
if (m_length != other_length)
|
||||
return false;
|
||||
|
|
Loading…
Reference in a new issue