Title: [234122] trunk
Revision
234122
Author
[email protected]
Date
2018-07-23 17:01:49 -0700 (Mon, 23 Jul 2018)

Log Message

WTF::StringView::split should have an allowEmptyEntries flag
https://bugs.webkit.org/show_bug.cgi?id=187864

Reviewed by Konstantin Tokarev.

Source/WTF:

* wtf/text/StringView.cpp:
(WTF::StringView::SplitResult::Iterator::findNextSubstring):
(WTF::StringView::SplitResult::Iterator::operator++):
* wtf/text/StringView.h:
(WTF::StringView::split const):
(WTF::StringView::SplitResult::SplitResult):
(WTF::StringView::SplitResult::Iterator::operator== const):
We can't mimic String::split completely, because this one's iterator-based --
achieve desired behavior by adding m_allowEmptyEntries to SplitResult and m_isDone to its Iterator.
(The latter keeps us from hitting begin() == end() prematurely on a final empty entry.)

Tools:

* TestWebKitAPI/Tests/WTF/StringView.cpp: Add test.

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (234121 => 234122)


--- trunk/Source/WTF/ChangeLog	2018-07-23 23:44:59 UTC (rev 234121)
+++ trunk/Source/WTF/ChangeLog	2018-07-24 00:01:49 UTC (rev 234122)
@@ -1,3 +1,21 @@
+2018-07-23  Ross Kirsling  <[email protected]>
+
+        WTF::StringView::split should have an allowEmptyEntries flag
+        https://bugs.webkit.org/show_bug.cgi?id=187864
+
+        Reviewed by Konstantin Tokarev.
+
+        * wtf/text/StringView.cpp:
+        (WTF::StringView::SplitResult::Iterator::findNextSubstring):
+        (WTF::StringView::SplitResult::Iterator::operator++):
+        * wtf/text/StringView.h:
+        (WTF::StringView::split const):
+        (WTF::StringView::SplitResult::SplitResult):
+        (WTF::StringView::SplitResult::Iterator::operator== const):
+        We can't mimic String::split completely, because this one's iterator-based --
+        achieve desired behavior by adding m_allowEmptyEntries to SplitResult and m_isDone to its Iterator.
+        (The latter keeps us from hitting begin() == end() prematurely on a final empty entry.)
+
 2018-07-23  Ben Richards  <[email protected]>
 
         We should cache the compiled sandbox profile in a data vault

Modified: trunk/Source/WTF/wtf/text/StringView.cpp (234121 => 234122)


--- trunk/Source/WTF/wtf/text/StringView.cpp	2018-07-23 23:44:59 UTC (rev 234121)
+++ trunk/Source/WTF/wtf/text/StringView.cpp	2018-07-24 00:01:49 UTC (rev 234122)
@@ -105,22 +105,25 @@
 void StringView::SplitResult::Iterator::findNextSubstring()
 {
     for (size_t separatorPosition; (separatorPosition = m_result.m_string.find(m_result.m_separator, m_position)) != notFound; ++m_position) {
-        if (separatorPosition > m_position) {
+        if (m_result.m_allowEmptyEntries || separatorPosition > m_position) {
             m_length = separatorPosition - m_position;
             return;
         }
     }
     m_length = m_result.m_string.length() - m_position;
+    if (!m_length && !m_result.m_allowEmptyEntries)
+        m_isDone = true;
 }
 
 auto StringView::SplitResult::Iterator::operator++() -> Iterator&
 {
-    ASSERT(m_position < m_result.m_string.length());
+    ASSERT(m_position <= m_result.m_string.length() && !m_isDone);
     m_position += m_length;
     if (m_position < m_result.m_string.length()) {
         ++m_position;
         findNextSubstring();
-    }
+    } else if (!m_isDone)
+        m_isDone = true;
     return *this;
 }
 

Modified: trunk/Source/WTF/wtf/text/StringView.h (234121 => 234122)


--- trunk/Source/WTF/wtf/text/StringView.h	2018-07-23 23:44:59 UTC (rev 234121)
+++ trunk/Source/WTF/wtf/text/StringView.h	2018-07-24 00:01:49 UTC (rev 234122)
@@ -124,8 +124,11 @@
     template<typename MatchedCharacterPredicate>
     StringView stripLeadingAndTrailingMatchedCharacters(const MatchedCharacterPredicate&);
 
+    enum AllowEmptyEntriesTag { AllowEmptyEntries };
+
     class SplitResult;
     SplitResult split(UChar) const;
+    SplitResult split(UChar, AllowEmptyEntriesTag) const;
 
     size_t find(UChar, unsigned start = 0) const;
     size_t find(CodeUnitMatchFunction, unsigned start = 0) const;
@@ -635,7 +638,7 @@
 
 class StringView::SplitResult {
 public:
-    explicit SplitResult(StringView, UChar separator);
+    SplitResult(StringView, UChar separator, bool allowEmptyEntries);
 
     class Iterator;
     Iterator begin() const;
@@ -644,6 +647,7 @@
 private:
     StringView m_string;
     UChar m_separator;
+    bool m_allowEmptyEntries;
 };
 
 class StringView::GraphemeClusters {
@@ -703,6 +707,7 @@
     const SplitResult& m_result;
     unsigned m_position { 0 };
     unsigned m_length;
+    bool m_isDone;
 };
 
 class StringView::GraphemeClusters::Iterator {
@@ -897,12 +902,18 @@
 
 inline auto StringView::split(UChar separator) const -> SplitResult
 {
-    return SplitResult { *this, separator };
+    return SplitResult { *this, separator, false };
 }
 
-inline StringView::SplitResult::SplitResult(StringView stringView, UChar separator)
+inline auto StringView::split(UChar separator, AllowEmptyEntriesTag) const -> SplitResult
+{
+    return SplitResult { *this, separator, true };
+}
+
+inline StringView::SplitResult::SplitResult(StringView stringView, UChar separator, bool allowEmptyEntries)
     : m_string { stringView }
     , m_separator { separator }
+    , m_allowEmptyEntries { allowEmptyEntries }
 {
 }
 
@@ -918,6 +929,7 @@
 
 inline StringView::SplitResult::Iterator::Iterator(const SplitResult& result)
     : m_result { result }
+    , m_isDone { result.m_string.isEmpty() && !result.m_allowEmptyEntries }
 {
     findNextSubstring();
 }
@@ -925,12 +937,13 @@
 inline StringView::SplitResult::Iterator::Iterator(const SplitResult& result, PositionTag)
     : m_result { result }
     , m_position { result.m_string.length() }
+    , m_isDone { true }
 {
 }
 
 inline StringView StringView::SplitResult::Iterator::operator*() const
 {
-    ASSERT(m_position < m_result.m_string.length());
+    ASSERT(m_position <= m_result.m_string.length() && !m_isDone);
     return m_result.m_string.substring(m_position, m_length);
 }
 
@@ -937,7 +950,7 @@
 inline bool StringView::SplitResult::Iterator::operator==(const Iterator& other) const
 {
     ASSERT(&m_result == &other.m_result);
-    return m_position == other.m_position;
+    return m_position == other.m_position && m_isDone == other.m_isDone;
 }
 
 inline bool StringView::SplitResult::Iterator::operator!=(const Iterator& other) const

Modified: trunk/Tools/ChangeLog (234121 => 234122)


--- trunk/Tools/ChangeLog	2018-07-23 23:44:59 UTC (rev 234121)
+++ trunk/Tools/ChangeLog	2018-07-24 00:01:49 UTC (rev 234122)
@@ -1,3 +1,12 @@
+2018-07-23  Ross Kirsling  <[email protected]>
+
+        WTF::StringView::split should have an allowEmptyEntries flag
+        https://bugs.webkit.org/show_bug.cgi?id=187864
+
+        Reviewed by Konstantin Tokarev.
+
+        * TestWebKitAPI/Tests/WTF/StringView.cpp: Add test.
+
 2018-07-23  Ben Richards  <[email protected]>
 
         We should cache the compiled sandbox profile in a data vault

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp (234121 => 234122)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp	2018-07-23 23:44:59 UTC (rev 234121)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp	2018-07-24 00:01:49 UTC (rev 234122)
@@ -289,7 +289,7 @@
 TEST(WTF, StringViewSplitWithConsecutiveSeparators)
 {
     String referenceHolder;
-    StringView a = stringViewFromUTF8(referenceHolder, "This     is  a       sentence.");
+    StringView a = stringViewFromUTF8(referenceHolder, " This     is  a       sentence. ");
 
     Vector<String> actual = vectorFromSplitResult(a.split(' '));
     Vector<String> expected({ "This", "is", "a", "sentence." });
@@ -296,6 +296,12 @@
     ASSERT_EQ(expected.size(), actual.size());
     for (size_t i = 0; i < actual.size(); ++i)
         EXPECT_STREQ(expected[i].utf8().data(), actual[i].utf8().data()) << "Vectors differ at index " << i;
+
+    actual = vectorFromSplitResult(a.split(' ', StringView::AllowEmptyEntries));
+    expected = { "", "This", "", "", "", "", "is", "", "a", "", "", "", "", "", "", "sentence.", "" };
+    ASSERT_EQ(expected.size(), actual.size());
+    for (size_t i = 0; i < actual.size(); ++i)
+        EXPECT_STREQ(expected[i].utf8().data(), actual[i].utf8().data()) << "Vectors differ at index " << i;
 }
 
 TEST(WTF, StringViewEqualBasic)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to