<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[184867] trunk/Source/WTF</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/184867">184867</a></dd>
<dt>Author</dt> <dd>akling@apple.com</dd>
<dt>Date</dt> <dd>2015-05-26 11:57:00 -0700 (Tue, 26 May 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>StringView should have find(StringView, start).
&lt;https://webkit.org/b/145351&gt;

Reviewed by Darin Adler.

Move the class agnostic guts of StringImpl's find() implementation from StringImpl
to StringCommon.h and templatize the code into a findCommon() helper.

StringImpl::find() and StringView::find() now both call findCommon().

* wtf/text/StringCommon.h:
(WTF::findInner):
(WTF::find):
(WTF::findCommon):
* wtf/text/StringImpl.cpp:
(WTF::StringImpl::find):
(WTF::findInner): Deleted.
* wtf/text/StringImpl.h:
(WTF::find): Deleted.
* wtf/text/StringView.cpp:
(WTF::StringView::find):
* wtf/text/StringView.h:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWTFChangeLog">trunk/Source/WTF/ChangeLog</a></li>
<li><a href="#trunkSourceWTFwtftextStringCommonh">trunk/Source/WTF/wtf/text/StringCommon.h</a></li>
<li><a href="#trunkSourceWTFwtftextStringImplcpp">trunk/Source/WTF/wtf/text/StringImpl.cpp</a></li>
<li><a href="#trunkSourceWTFwtftextStringImplh">trunk/Source/WTF/wtf/text/StringImpl.h</a></li>
<li><a href="#trunkSourceWTFwtftextStringViewcpp">trunk/Source/WTF/wtf/text/StringView.cpp</a></li>
<li><a href="#trunkSourceWTFwtftextStringViewh">trunk/Source/WTF/wtf/text/StringView.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWTFChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/ChangeLog (184866 => 184867)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/ChangeLog        2015-05-26 18:56:42 UTC (rev 184866)
+++ trunk/Source/WTF/ChangeLog        2015-05-26 18:57:00 UTC (rev 184867)
</span><span class="lines">@@ -1,3 +1,29 @@
</span><ins>+2015-05-26  Andreas Kling  &lt;akling@apple.com&gt;
+
+        StringView should have find(StringView, start).
+        &lt;https://webkit.org/b/145351&gt;
+
+        Reviewed by Darin Adler.
+
+        Move the class agnostic guts of StringImpl's find() implementation from StringImpl
+        to StringCommon.h and templatize the code into a findCommon() helper.
+
+        StringImpl::find() and StringView::find() now both call findCommon().
+
+        * wtf/text/StringCommon.h:
+        (WTF::findInner):
+        (WTF::find):
+        (WTF::findCommon):
+        * wtf/text/StringImpl.cpp:
+        (WTF::StringImpl::find):
+        (WTF::findInner): Deleted.
+        * wtf/text/StringImpl.h:
+        (WTF::find): Deleted.
+        * wtf/text/StringView.cpp:
+        (WTF::StringView::find):
+        * wtf/text/StringView.h:
+
+
</ins><span class="cx"> 2015-05-26  Dan Bernstein  &lt;mitz@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         &lt;rdar://problem/21104551&gt; Update build settings
</span></span></pre></div>
<a id="trunkSourceWTFwtftextStringCommonh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/text/StringCommon.h (184866 => 184867)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/text/StringCommon.h        2015-05-26 18:56:42 UTC (rev 184866)
+++ trunk/Source/WTF/wtf/text/StringCommon.h        2015-05-26 18:57:00 UTC (rev 184867)
</span><span class="lines">@@ -461,6 +461,90 @@
</span><span class="cx">     return findIgnoringASCIICase(source.characters16(), stringToFind.characters16(), startOffset, searchLength, matchLength);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+template &lt;typename SearchCharacterType, typename MatchCharacterType&gt;
+ALWAYS_INLINE static size_t findInner(const SearchCharacterType* searchCharacters, const MatchCharacterType* matchCharacters, unsigned index, unsigned searchLength, unsigned matchLength)
+{
+    // Optimization: keep a running hash of the strings,
+    // only call equal() if the hashes match.
+
+    // delta is the number of additional times to test; delta == 0 means test only once.
+    unsigned delta = searchLength - matchLength;
+
+    unsigned searchHash = 0;
+    unsigned matchHash = 0;
+
+    for (unsigned i = 0; i &lt; matchLength; ++i) {
+        searchHash += searchCharacters[i];
+        matchHash += matchCharacters[i];
+    }
+
+    unsigned i = 0;
+    // keep looping until we match
+    while (searchHash != matchHash || !equal(searchCharacters + i, matchCharacters, matchLength)) {
+        if (i == delta)
+            return notFound;
+        searchHash += searchCharacters[i + matchLength];
+        searchHash -= searchCharacters[i];
+        ++i;
+    }
+    return index + i;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+template&lt;typename CharacterType&gt;
+inline size_t find(const CharacterType* characters, unsigned length, CharacterType matchCharacter, unsigned index = 0)
+{
+    while (index &lt; length) {
+        if (characters[index] == matchCharacter)
+            return index;
+        ++index;
+    }
+    return notFound;
+}
+
+ALWAYS_INLINE size_t find(const UChar* characters, unsigned length, LChar matchCharacter, unsigned index = 0)
+{
+    return find(characters, length, static_cast&lt;UChar&gt;(matchCharacter), index);
+}
+
+inline size_t find(const LChar* characters, unsigned length, UChar matchCharacter, unsigned index = 0)
+{
+    if (matchCharacter &amp; ~0xFF)
+        return notFound;
+    return find(characters, length, static_cast&lt;LChar&gt;(matchCharacter), index);
+}
+
+template&lt;typename StringClass&gt;
+size_t findCommon(const StringClass&amp; haystack, const StringClass&amp; needle, unsigned start)
+{
+    unsigned needleLength = needle.length();
+
+    if (needleLength == 1) {
+        if (haystack.is8Bit())
+            return WTF::find(haystack.characters8(), haystack.length(), needle[0], start);
+        return WTF::find(haystack.characters16(), haystack.length(), needle[0], start);
+    }
+
+    if (!needleLength)
+        return std::min(start, haystack.length());
+
+    if (start &gt; haystack.length())
+        return notFound;
+    unsigned searchLength = haystack.length() - start;
+    if (needleLength &gt; searchLength)
+        return notFound;
+
+    if (haystack.is8Bit()) {
+        if (needle.is8Bit())
+            return findInner(haystack.characters8() + start, needle.characters8(), start, searchLength, needleLength);
+        return findInner(haystack.characters8() + start, needle.characters16(), start, searchLength, needleLength);
+    }
+
+    if (needle.is8Bit())
+        return findInner(haystack.characters16() + start, needle.characters8(), start, searchLength, needleLength);
+
+    return findInner(haystack.characters16() + start, needle.characters16(), start, searchLength, needleLength);
+}
+
+}
+
</ins><span class="cx"> #endif // StringCommon_h
</span></span></pre></div>
<a id="trunkSourceWTFwtftextStringImplcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/text/StringImpl.cpp (184866 => 184867)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/text/StringImpl.cpp        2015-05-26 18:56:42 UTC (rev 184866)
+++ trunk/Source/WTF/wtf/text/StringImpl.cpp        2015-05-26 18:57:00 UTC (rev 184867)
</span><span class="lines">@@ -1092,35 +1092,6 @@
</span><span class="cx">     return index + i;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-template &lt;typename SearchCharacterType, typename MatchCharacterType&gt;
-ALWAYS_INLINE static size_t findInner(const SearchCharacterType* searchCharacters, const MatchCharacterType* matchCharacters, unsigned index, unsigned searchLength, unsigned matchLength)
-{
-    // Optimization: keep a running hash of the strings,
-    // only call equal() if the hashes match.
-
-    // delta is the number of additional times to test; delta == 0 means test only once.
-    unsigned delta = searchLength - matchLength;
-
-    unsigned searchHash = 0;
-    unsigned matchHash = 0;
-
-    for (unsigned i = 0; i &lt; matchLength; ++i) {
-        searchHash += searchCharacters[i];
-        matchHash += matchCharacters[i];
-    }
-
-    unsigned i = 0;
-    // keep looping until we match
-    while (searchHash != matchHash || !equal(searchCharacters + i, matchCharacters, matchLength)) {
-        if (i == delta)
-            return notFound;
-        searchHash += searchCharacters[i + matchLength];
-        searchHash -= searchCharacters[i];
-        ++i;
-    }
-    return index + i;        
-}
-
</del><span class="cx"> size_t StringImpl::find(StringImpl* matchString)
</span><span class="cx"> {
</span><span class="cx">     // Check for null string to match against
</span><span class="lines">@@ -1166,35 +1137,7 @@
</span><span class="cx">     if (UNLIKELY(!matchString))
</span><span class="cx">         return notFound;
</span><span class="cx"> 
</span><del>-    unsigned matchLength = matchString-&gt;length();
-
-    // Optimization 1: fast case for strings of length 1.
-    if (matchLength == 1) {
-        if (is8Bit())
-            return WTF::find(characters8(), length(), (*matchString)[0], index);
-        return WTF::find(characters16(), length(), (*matchString)[0], index);
-    }
-
-    if (UNLIKELY(!matchLength))
-        return std::min(index, length());
-
-    // Check index &amp; matchLength are in range.
-    if (index &gt; length())
-        return notFound;
-    unsigned searchLength = length() - index;
-    if (matchLength &gt; searchLength)
-        return notFound;
-
-    if (is8Bit()) {
-        if (matchString-&gt;is8Bit())
-            return findInner(characters8() + index, matchString-&gt;characters8(), index, searchLength, matchLength);
-        return findInner(characters8() + index, matchString-&gt;characters16(), index, searchLength, matchLength);
-    }
-
-    if (matchString-&gt;is8Bit())
-        return findInner(characters16() + index, matchString-&gt;characters8(), index, searchLength, matchLength);
-
-    return findInner(characters16() + index, matchString-&gt;characters16(), index, searchLength, matchLength);
</del><ins>+    return findCommon(*this, *matchString, index);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template &lt;typename SearchCharacterType, typename MatchCharacterType&gt;
</span></span></pre></div>
<a id="trunkSourceWTFwtftextStringImplh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/text/StringImpl.h (184866 => 184867)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/text/StringImpl.h        2015-05-26 18:56:42 UTC (rev 184866)
+++ trunk/Source/WTF/wtf/text/StringImpl.h        2015-05-26 18:57:00 UTC (rev 184867)
</span><span class="lines">@@ -33,6 +33,7 @@
</span><span class="cx"> #include &lt;wtf/StringHasher.h&gt;
</span><span class="cx"> #include &lt;wtf/Vector.h&gt;
</span><span class="cx"> #include &lt;wtf/text/ConversionMode.h&gt;
</span><ins>+#include &lt;wtf/text/StringCommon.h&gt;
</ins><span class="cx"> 
</span><span class="cx"> #if USE(CF)
</span><span class="cx"> typedef const struct __CFString * CFStringRef;
</span><span class="lines">@@ -983,29 +984,6 @@
</span><span class="cx">     return a ? equalIgnoringASCIICase(*a, b, charactersCount - 1) : false;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-template&lt;typename CharacterType&gt;
-inline size_t find(const CharacterType* characters, unsigned length, CharacterType matchCharacter, unsigned index = 0)
-{
-    while (index &lt; length) {
-        if (characters[index] == matchCharacter)
-            return index;
-        ++index;
-    }
-    return notFound;
-}
-
-ALWAYS_INLINE size_t find(const UChar* characters, unsigned length, LChar matchCharacter, unsigned index = 0)
-{
-    return find(characters, length, static_cast&lt;UChar&gt;(matchCharacter), index);
-}
-
-inline size_t find(const LChar* characters, unsigned length, UChar matchCharacter, unsigned index = 0)
-{
-    if (matchCharacter &amp; ~0xFF)
-        return notFound;
-    return find(characters, length, static_cast&lt;LChar&gt;(matchCharacter), index);
-}
-
</del><span class="cx"> inline size_t find(const LChar* characters, unsigned length, CharacterMatchFunctionPtr matchFunction, unsigned index = 0)
</span><span class="cx"> {
</span><span class="cx">     while (index &lt; length) {
</span></span></pre></div>
<a id="trunkSourceWTFwtftextStringViewcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/text/StringView.cpp (184866 => 184867)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/text/StringView.cpp        2015-05-26 18:56:42 UTC (rev 184866)
+++ trunk/Source/WTF/wtf/text/StringView.cpp        2015-05-26 18:57:00 UTC (rev 184867)
</span><span class="lines">@@ -96,6 +96,11 @@
</span><span class="cx">     return StringImpl::utf8ForCharacters(characters16(), length(), mode);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+size_t StringView::find(StringView matchString, unsigned start) const
+{
+    return findCommon(*this, matchString, start);
+}
+
</ins><span class="cx"> #if CHECK_STRINGVIEW_LIFETIME
</span><span class="cx"> 
</span><span class="cx"> // Manage reference count manually so UnderlyingString does not need to be defined in the header.
</span></span></pre></div>
<a id="trunkSourceWTFwtftextStringViewh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/text/StringView.h (184866 => 184867)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/text/StringView.h        2015-05-26 18:56:42 UTC (rev 184866)
+++ trunk/Source/WTF/wtf/text/StringView.h        2015-05-26 18:57:00 UTC (rev 184867)
</span><span class="lines">@@ -109,6 +109,9 @@
</span><span class="cx">     StringView substring(unsigned start, unsigned length = std::numeric_limits&lt;unsigned&gt;::max()) const;
</span><span class="cx"> 
</span><span class="cx">     size_t find(UChar, unsigned start = 0) const;
</span><ins>+
+    WTF_EXPORT_STRING_API size_t find(StringView, unsigned start) const;
+
</ins><span class="cx">     WTF_EXPORT_STRING_API size_t findIgnoringASCIICase(const StringView&amp;) const;
</span><span class="cx">     WTF_EXPORT_STRING_API size_t findIgnoringASCIICase(const StringView&amp;, unsigned startOffset) const;
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>