Title: [236443] trunk
Revision
236443
Author
jer.no...@apple.com
Date
2018-09-24 16:48:51 -0700 (Mon, 24 Sep 2018)

Log Message

SharedBuffer should have an equality test
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):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (236442 => 236443)


--- trunk/Source/WebCore/ChangeLog	2018-09-24 23:38:31 UTC (rev 236442)
+++ trunk/Source/WebCore/ChangeLog	2018-09-24 23:48:51 UTC (rev 236443)
@@ -1,3 +1,17 @@
+2018-09-24  Jer Noble  <jer.no...@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  <rn...@webkit.org>
 
         imported/w3c/web-platform-tests/shadow-dom/slotchange.html is a flaky failure

Modified: trunk/Source/WebCore/platform/SharedBuffer.cpp (236442 => 236443)


--- trunk/Source/WebCore/platform/SharedBuffer.cpp	2018-09-24 23:38:31 UTC (rev 236442)
+++ trunk/Source/WebCore/platform/SharedBuffer.cpp	2018-09-24 23:48:51 UTC (rev 236443)
@@ -222,6 +222,55 @@
 }
 #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(

Modified: trunk/Source/WebCore/platform/SharedBuffer.h (236442 => 236443)


--- trunk/Source/WebCore/platform/SharedBuffer.h	2018-09-24 23:38:31 UTC (rev 236442)
+++ trunk/Source/WebCore/platform/SharedBuffer.h	2018-09-24 23:48:51 UTC (rev 236443)
@@ -173,6 +173,9 @@
 
     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 @@
 #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);

Modified: trunk/Tools/ChangeLog (236442 => 236443)


--- trunk/Tools/ChangeLog	2018-09-24 23:38:31 UTC (rev 236442)
+++ trunk/Tools/ChangeLog	2018-09-24 23:48:51 UTC (rev 236443)
@@ -1,3 +1,13 @@
+2018-09-24  Jer Noble  <jer.no...@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  <achristen...@webkit.org>
 
         Prepare to replace WKBundleFileHandleCreateWithPath with a version that takes a WKBundlePageRef

Modified: trunk/Tools/TestWebKitAPI/Tests/WebCore/SharedBuffer.cpp (236442 => 236443)


--- trunk/Tools/TestWebKitAPI/Tests/WebCore/SharedBuffer.cpp	2018-09-24 23:38:31 UTC (rev 236442)
+++ trunk/Tools/TestWebKitAPI/Tests/WebCore/SharedBuffer.cpp	2018-09-24 23:48:51 UTC (rev 236443)
@@ -198,4 +198,23 @@
     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'}}));
 }
+
+}
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to