SharedBuffer should have an equality test
authorjer.noble@apple.com <jer.noble@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 24 Sep 2018 23:48:51 +0000 (23:48 +0000)
committerjer.noble@apple.com <jer.noble@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 24 Sep 2018 23:48:51 +0000 (23:48 +0000)
https://bugs.webkit.org/show_bug.cgi?id=189919

Reviewed by Alex Christensen.

Source/WebCore:

Test: TestWebKitAPI SharedBuffer.isEqualTo.

* platform/SharedBuffer.cpp:
* platform/SharedBuffer.h:
(WebCore::operator==):
(WebCore::operator!=):

Tools:

* TestWebKitAPI/Tests/WebCore/SharedBuffer.cpp:
(TestWebKitAPI::TEST_F):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@236443 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/SharedBuffer.cpp
Source/WebCore/platform/SharedBuffer.h
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebCore/SharedBuffer.cpp

index c1e5321..d21cbe9 100644 (file)
@@ -1,3 +1,17 @@
+2018-09-24  Jer Noble  <jer.noble@apple.com>
+
+        SharedBuffer should have an equality test
+        https://bugs.webkit.org/show_bug.cgi?id=189919
+
+        Reviewed by Alex Christensen.
+
+        Test: TestWebKitAPI SharedBuffer.isEqualTo.
+
+        * platform/SharedBuffer.cpp:
+        * platform/SharedBuffer.h:
+        (WebCore::operator==):
+        (WebCore::operator!=):
+
 2018-09-24  Ryosuke Niwa  <rniwa@webkit.org>
 
         imported/w3c/web-platform-tests/shadow-dom/slotchange.html is a flaky failure
index e2fdb77..db0af3e 100644 (file)
@@ -222,6 +222,55 @@ void SharedBuffer::hintMemoryNotNeededSoon() const
 }
 #endif
 
+bool SharedBuffer::operator==(const SharedBuffer& other) const
+{
+    if (this == &other)
+        return true;
+
+    if (m_size != other.m_size)
+        return false;
+
+    auto thisIterator = begin();
+    size_t thisOffset = 0;
+    auto otherIterator = other.begin();
+    size_t otherOffset = 0;
+
+    while (thisIterator != end() && otherIterator != other.end()) {
+        auto& thisSegment = thisIterator->segment.get();
+        auto& otherSegment = otherIterator->segment.get();
+
+        if (&thisSegment == &otherSegment && !thisOffset && !otherOffset) {
+            ++thisIterator;
+            ++otherIterator;
+            continue;
+        }
+
+        ASSERT(thisOffset < thisSegment.size());
+        ASSERT(otherOffset < otherSegment.size());
+
+        size_t thisRemaining = thisSegment.size() - thisOffset;
+        size_t otherRemaining = otherSegment.size() - otherOffset;
+        size_t remaining = std::min(thisRemaining, otherRemaining);
+
+        if (memcmp(thisSegment.data() + thisOffset, otherSegment.data() + otherOffset, remaining))
+            return false;
+
+        thisOffset += remaining;
+        otherOffset += remaining;
+
+        if (thisOffset == thisSegment.size()) {
+            ++thisIterator;
+            thisOffset = 0;
+        }
+
+        if (otherOffset == otherSegment.size()) {
+            ++otherIterator;
+            otherOffset = 0;
+        }
+    }
+    return true;
+}
+
 size_t SharedBuffer::DataSegment::size() const
 {
     auto visitor = WTF::makeVisitor(
index 3bcd825..e2aae2f 100644 (file)
@@ -173,6 +173,9 @@ public:
 
     void hintMemoryNotNeededSoon() const;
 
+    bool operator==(const SharedBuffer&) const;
+    bool operator!=(const SharedBuffer& other) const { return !operator==(other); }
+
 private:
     explicit SharedBuffer() = default;
     explicit SharedBuffer(const char*, size_t);
@@ -202,6 +205,16 @@ private:
 #endif
 };
 
+inline bool operator==(const Ref<SharedBuffer>& left, const SharedBuffer& right)
+{
+    return left.get() == right;
+}
+
+inline bool operator!=(const Ref<SharedBuffer>& left, const SharedBuffer& right)
+{
+    return left.get() != right;
+}
+
 class WEBCORE_EXPORT SharedBufferDataView {
 public:
     SharedBufferDataView(Ref<SharedBuffer::DataSegment>&&, size_t);
index 39bfc2c..93f9733 100644 (file)
@@ -1,3 +1,13 @@
+2018-09-24  Jer Noble  <jer.noble@apple.com>
+
+        SharedBuffer should have an equality test
+        https://bugs.webkit.org/show_bug.cgi?id=189919
+
+        Reviewed by Alex Christensen.
+
+        * TestWebKitAPI/Tests/WebCore/SharedBuffer.cpp:
+        (TestWebKitAPI::TEST_F):
+
 2018-09-24  Alex Christensen  <achristensen@webkit.org>
 
         Prepare to replace WKBundleFileHandleCreateWithPath with a version that takes a WKBundlePageRef
index 8d8ccab..eb92d50 100644 (file)
@@ -198,4 +198,23 @@ TEST_F(SharedBufferTest, getSomeData)
     checkBuffer(l.data(), l.size(), "l");
 }
 
+TEST_F(SharedBufferTest, isEqualTo)
+{
+    auto makeBuffer = [] (Vector<Vector<char>>&& contents) {
+        auto buffer = SharedBuffer::create();
+        for (auto& content : contents)
+            buffer->append(WTFMove(content));
+        return buffer;
+    };
+    auto buffer1 = makeBuffer({{'a', 'b', 'c', 'd'}});
+    EXPECT_EQ(buffer1, buffer1);
+
+    buffer1->append(Vector<char>({'a', 'b', 'c', 'd'}));
+    EXPECT_EQ(buffer1, makeBuffer({{'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'}}));
+    EXPECT_EQ(makeBuffer({{'a'}, {'b', 'c'}, {'d'}}), makeBuffer({{'a', 'b'}, {'c', 'd'}}));
+    EXPECT_NE(makeBuffer({{'a', 'b'}}), makeBuffer({{'a', 'b', 'c'}}));
+    EXPECT_NE(makeBuffer({{'a', 'b'}}), makeBuffer({{'b', 'c'}}));
+    EXPECT_NE(makeBuffer({{'a'}, {'b'}}), makeBuffer({{'a'}, {'a'}}));
+}
+
 }