Bug 1431261 - Add nsTDependentString copy constructor. r=dbaron

This adds a copy constructor that uses Rebind to preserve the reference to
static data rather allocate a new shared buffer.

--HG--
extra : rebase_source : db04def1137a9be646b8e09dfee780c9163a585c
This commit is contained in:
Eric Rahm 2018-01-18 14:16:19 -08:00
Родитель 2739f28b3d
Коммит 5497e40f06
2 изменённых файлов: 60 добавлений и 3 удалений

Просмотреть файл

@ -105,11 +105,21 @@ public:
{
}
// XXX are you sure??
// auto-generated copy-constructor OK
// auto-generated copy-assignment operator OK
// auto-generated destructor OK
nsTDependentString(self_type&& aStr)
: string_type()
{
Rebind(aStr, /* aStartPos = */ 0);
aStr.SetToEmptyBuffer();
}
explicit
nsTDependentString(const self_type& aStr)
: string_type()
{
Rebind(aStr, /* aStartPos = */ 0);
}
/**
* allow this class to be bound to a different string...
@ -128,6 +138,7 @@ private:
// NOT USED
nsTDependentString(const substring_tuple_type&) = delete;
self_type& operator=(const self_type& aStr) = delete;
};
extern template class nsTDependentString<char>;

Просмотреть файл

@ -68,6 +68,52 @@ TEST(Strings, IsChar)
#endif
}
TEST(Strings, DependentStrings)
{
// A few tests that make sure copying nsTDependentStrings behaves properly.
using DataFlags = mozilla::detail::StringDataFlags;
{
// Test copy ctor.
nsDependentCString tmp("foo");
auto data = tmp.Data();
nsDependentCString foo(tmp);
// Neither string should be using a shared buffer.
EXPECT_FALSE(tmp.GetDataFlags() & DataFlags::SHARED);
EXPECT_FALSE(foo.GetDataFlags() & DataFlags::SHARED);
// Both strings should be pointing to the original buffer.
EXPECT_EQ(data, tmp.Data());
EXPECT_EQ(data, foo.Data());
}
{
// Test move ctor.
nsDependentCString tmp("foo");
auto data = tmp.Data();
nsDependentCString foo(mozilla::Move(tmp));
// Neither string should be using a shared buffer.
EXPECT_FALSE(tmp.GetDataFlags() & DataFlags::SHARED);
EXPECT_FALSE(foo.GetDataFlags() & DataFlags::SHARED);
// First string should be reset, the second should be pointing to the
// original buffer.
EXPECT_NE(data, tmp.Data());
EXPECT_EQ(data, foo.Data());
EXPECT_TRUE(tmp.IsEmpty());
}
{
// Test copying to a nsCString.
nsDependentCString tmp("foo");
auto data = tmp.Data();
nsCString foo(tmp);
// Original string should not be shared, copy should be shared.
EXPECT_FALSE(tmp.GetDataFlags() & DataFlags::SHARED);
EXPECT_TRUE(foo.GetDataFlags() & DataFlags::SHARED);
// First string should remain the same, the second should be pointing to
// a new buffer.
EXPECT_EQ(data, tmp.Data());
EXPECT_NE(data, foo.Data());
}
}
TEST(Strings, assign)
{
nsCString result;