mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-15 12:23:15 +00:00
StringView: Add StringView::operator==(StringView)
Previously we'd implicitly convert the second StringView to a String when comparing two StringViews, which is obviously not what we wanted.
This commit is contained in:
parent
77737be7b3
commit
2349dc1a21
|
@ -83,6 +83,22 @@ public:
|
||||||
|
|
||||||
bool operator==(const String&) const;
|
bool operator==(const String&) const;
|
||||||
|
|
||||||
|
bool operator==(const StringView& other) const
|
||||||
|
{
|
||||||
|
if (is_null())
|
||||||
|
return other.is_null();
|
||||||
|
if (other.is_null())
|
||||||
|
return false;
|
||||||
|
if (length() != other.length())
|
||||||
|
return false;
|
||||||
|
return !memcmp(m_characters, other.m_characters, m_length);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(const StringView& other) const
|
||||||
|
{
|
||||||
|
return !(*this == other);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class String;
|
friend class String;
|
||||||
const StringImpl* m_impl { nullptr };
|
const StringImpl* m_impl { nullptr };
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
PROGRAMS = TestString TestQueue TestVector TestHashMap TestJSON TestWeakPtr TestNonnullRefPtr TestRefPtr TestFixedArray TestFileSystemPath TestURL
|
PROGRAMS = TestString TestQueue TestVector TestHashMap TestJSON TestWeakPtr TestNonnullRefPtr TestRefPtr TestFixedArray TestFileSystemPath TestURL TestStringView
|
||||||
|
|
||||||
CXXFLAGS = -std=c++17 -Wall -Wextra -ggdb3 -O2 -I../ -I../../
|
CXXFLAGS = -std=c++17 -Wall -Wextra -ggdb3 -O2 -I../ -I../../
|
||||||
|
|
||||||
|
@ -62,6 +62,8 @@ TestFileSystemPath: TestFileSystemPath.o $(SHARED_TEST_OBJS)
|
||||||
TestURL: TestURL.o $(SHARED_TEST_OBJS)
|
TestURL: TestURL.o $(SHARED_TEST_OBJS)
|
||||||
$(PRE_CXX) $(CXX) $(CXXFLAGS) -o $@ TestURL.o $(SHARED_TEST_OBJS)
|
$(PRE_CXX) $(CXX) $(CXXFLAGS) -o $@ TestURL.o $(SHARED_TEST_OBJS)
|
||||||
|
|
||||||
|
TestStringView: TestStringView.o $(SHARED_TEST_OBJS)
|
||||||
|
$(PRE_CXX) $(CXX) $(CXXFLAGS) -o $@ TestStringView.o $(SHARED_TEST_OBJS)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(SHARED_TEST_OBJS)
|
rm -f $(SHARED_TEST_OBJS)
|
||||||
|
|
36
AK/Tests/TestStringView.cpp
Normal file
36
AK/Tests/TestStringView.cpp
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
#include <AK/TestSuite.h>
|
||||||
|
|
||||||
|
#include <AK/AKString.h>
|
||||||
|
|
||||||
|
TEST_CASE(construct_empty)
|
||||||
|
{
|
||||||
|
EXPECT(StringView().is_null());
|
||||||
|
EXPECT(StringView().is_empty());
|
||||||
|
EXPECT(!StringView().characters_without_null_termination());
|
||||||
|
EXPECT_EQ(StringView().length(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE(view_literal)
|
||||||
|
{
|
||||||
|
const char* truth = "cats rule dogs drool";
|
||||||
|
StringView view(truth);
|
||||||
|
EXPECT_EQ(view.is_null(), false);
|
||||||
|
EXPECT_EQ(view.characters_without_null_termination(), truth);
|
||||||
|
EXPECT_EQ(view, view);
|
||||||
|
EXPECT_EQ(view, truth);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE(compare_views)
|
||||||
|
{
|
||||||
|
String foo1 = "foo";
|
||||||
|
String foo2 = "foo";
|
||||||
|
auto view1 = foo1.view();
|
||||||
|
auto view2 = foo2.view();
|
||||||
|
|
||||||
|
EXPECT_EQ(view1, view2);
|
||||||
|
EXPECT_EQ(view1, foo1);
|
||||||
|
EXPECT_EQ(view1, foo2);
|
||||||
|
EXPECT_EQ(view1, "foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_MAIN(StringView)
|
Loading…
Reference in a new issue