Add WTF::StringView and use it for grammar checking
authorandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 8 Jan 2014 21:19:21 +0000 (21:19 +0000)
committerandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 8 Jan 2014 21:19:21 +0000 (21:19 +0000)
https://bugs.webkit.org/show_bug.cgi?id=126644

Reviewed by Antti Koivisto.

Source/WebCore:

Use a StringView in TextCheckerClient::checkTextOfParagraph to avoid upconverting strings unnecessarily.

* editing/TextCheckingHelper.cpp:
(WebCore::checkTextOfParagraph):
Pass a StringView to TextCheckerClient::checkTextOfParagraph.

* loader/EmptyClients.h:
Update for TextCheckerClient changes.

* platform/text/TextCheckerClient.h:
Change TextCheckerClient::checkTextOfParagraph to take a StringView.

Source/WebKit/efl:

Update to match WebCore changes.

* WebCoreSupport/EditorClientEfl.h:
(WebCore::EditorClientEfl::checkTextOfParagraph):

Source/WebKit/mac:

* WebCoreSupport/WebEditorClient.h:
* WebCoreSupport/WebEditorClient.mm:
(WebEditorClient::checkTextOfParagraph):
Update to match the new signature.

(nsStringWithoutCopying):
New function that creates an NSString that references StringView data
without copying the data. (If this function proves to be useful elsewhere, it could
potentially be added to StringView).

Source/WebKit2:

Use a StringView and remove a FIXME.

* WebProcess/WebCoreSupport/WebEditorClient.cpp:
(WebKit::WebEditorClient::checkTextOfParagraph):
* WebProcess/WebCoreSupport/WebEditorClient.h:

Source/WTF:

Add a new StringView class which is an immutable reference to either an 8-bit or a 16-bit string.

This makes it easier to pass substrings around without having to either copy string data
or upconvert and use UChar pointers.

The StringView class is very loosely based on the std::string_view class which is proposed for a future version of C++,
see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3609.html for more details.

* GNUmakefile.list.am:
* WTF.vcxproj/WTF.vcxproj:
* WTF.vcxproj/WTF.vcxproj.filters:
* WTF.xcodeproj/project.pbxproj:
* wtf/CMakeLists.txt:
* wtf/Forward.h:
* wtf/text/StringView.h: Added.
(WTF::StringView::StringView):
(WTF::StringView::empty):
(WTF::StringView::characters8):
(WTF::StringView::characters16):
(WTF::StringView::isEmpty):
(WTF::StringView::length):
(WTF::StringView::is8Bit):
(WTF::StringView::substring):
(WTF::StringView::toString):
(WTF::StringView::toStringWithoutCopying):
(WTF::StringView::encodedCharacters):

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

20 files changed:
Source/WTF/ChangeLog
Source/WTF/GNUmakefile.list.am
Source/WTF/WTF.vcxproj/WTF.vcxproj
Source/WTF/WTF.vcxproj/WTF.vcxproj.filters
Source/WTF/WTF.xcodeproj/project.pbxproj
Source/WTF/wtf/CMakeLists.txt
Source/WTF/wtf/Forward.h
Source/WTF/wtf/text/StringView.h [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/editing/TextCheckingHelper.cpp
Source/WebCore/loader/EmptyClients.h
Source/WebCore/platform/text/TextCheckerClient.h
Source/WebKit/efl/ChangeLog
Source/WebKit/efl/WebCoreSupport/EditorClientEfl.h
Source/WebKit/mac/ChangeLog
Source/WebKit/mac/WebCoreSupport/WebEditorClient.h
Source/WebKit/mac/WebCoreSupport/WebEditorClient.mm
Source/WebKit2/ChangeLog
Source/WebKit2/WebProcess/WebCoreSupport/WebEditorClient.cpp
Source/WebKit2/WebProcess/WebCoreSupport/WebEditorClient.h

index 7ed0a21a4f187c299b9263f8db0016de5df3e6ec..6294c28795e0ae3b3ca22399f3a6b23a158ec5f7 100644 (file)
@@ -1,3 +1,37 @@
+2014-01-08  Anders Carlsson  <andersca@apple.com>
+
+        Add WTF::StringView and use it for grammar checking
+        https://bugs.webkit.org/show_bug.cgi?id=126644
+
+        Reviewed by Antti Koivisto.
+
+        Add a new StringView class which is an immutable reference to either an 8-bit or a 16-bit string.
+
+        This makes it easier to pass substrings around without having to either copy string data
+        or upconvert and use UChar pointers.
+
+        The StringView class is very loosely based on the std::string_view class which is proposed for a future version of C++,
+        see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3609.html for more details.
+
+        * GNUmakefile.list.am:
+        * WTF.vcxproj/WTF.vcxproj:
+        * WTF.vcxproj/WTF.vcxproj.filters:
+        * WTF.xcodeproj/project.pbxproj:
+        * wtf/CMakeLists.txt:
+        * wtf/Forward.h:
+        * wtf/text/StringView.h: Added.
+        (WTF::StringView::StringView):
+        (WTF::StringView::empty):
+        (WTF::StringView::characters8):
+        (WTF::StringView::characters16):
+        (WTF::StringView::isEmpty):
+        (WTF::StringView::length):
+        (WTF::StringView::is8Bit):
+        (WTF::StringView::substring):
+        (WTF::StringView::toString):
+        (WTF::StringView::toStringWithoutCopying):
+        (WTF::StringView::encodedCharacters):
+
 2014-01-07  David Farler  <dfarler@apple.com>
 
         [ASAN] Building with trunk clang corrupts sandbox profile with comment from Compiler.h
index 767e291ea1fe0ccbf88ee02306a645abec9869c3..390035638bfc554fe4acbd171f41b5cd3b3ad8b2 100644 (file)
@@ -157,9 +157,9 @@ wtf_sources += \
     Source/WTF/wtf/StreamBuffer.h \
     Source/WTF/wtf/StringExtras.h \
     Source/WTF/wtf/StringHashDumpContext.h \
+    Source/WTF/wtf/StringHasher.h \
     Source/WTF/wtf/StringPrintStream.cpp \
     Source/WTF/wtf/StringPrintStream.h \
-    Source/WTF/wtf/StringHasher.h \
     Source/WTF/wtf/TCPackedCache.h \
     Source/WTF/wtf/TCPageMap.h \
     Source/WTF/wtf/TCSpinLock.h \
@@ -241,6 +241,7 @@ wtf_sources += \
     Source/WTF/wtf/text/StringImpl.h \
     Source/WTF/wtf/text/StringOperators.h \
     Source/WTF/wtf/text/StringStatics.cpp \
+    Source/WTF/wtf/text/StringView.h \
     Source/WTF/wtf/text/TextPosition.h \
     Source/WTF/wtf/text/WTFString.cpp \
     Source/WTF/wtf/text/WTFString.h \
index 699e77488c8522e6ca5ccbe7512188843b9e8b99..e939aa4e5df87bf887087b7cb42bf94866d6aad0 100644 (file)
     <ClInclude Include="..\wtf\text\StringHash.h" />
     <ClInclude Include="..\wtf\text\StringImpl.h" />
     <ClInclude Include="..\wtf\text\StringOperators.h" />
+    <ClInclude Include="..\wtf\text\StringView.h" />
     <ClInclude Include="..\wtf\text\WTFString.h" />
     <ClInclude Include="..\wtf\Threading.h" />
     <ClInclude Include="..\wtf\ThreadingPrimitives.h" />
index 7b7f1d47a9f5429087fc4d813604ac1270677dd0..775d3133164043f896cd0a87e2702397695f082f 100644 (file)
     <ClInclude Include="..\wtf\text\StringOperators.h">
       <Filter>text</Filter>
     </ClInclude>
+    <ClInclude Include="..\wtf\text\StringView.h">
+      <Filter>wtf</Filter>
+    </ClInclude>
     <ClInclude Include="..\wtf\text\WTFString.h">
       <Filter>text</Filter>
     </ClInclude>
index 3be94c7a09642e1af54767ecba506f8a541f7aaa..2ad559446ffa570e7f2b6801c5d9dea523216782 100644 (file)
@@ -55,6 +55,7 @@
                1A1D8B9E1731879800141DA4 /* FunctionDispatcher.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A1D8B9D1731879800141DA4 /* FunctionDispatcher.cpp */; };
                1A233C7D17DAA6E300A93ACF /* MallocPtr.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A233C7C17DAA6E300A93ACF /* MallocPtr.h */; };
                1A6BB769162F300500DD16DB /* StreamBuffer.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A6BB768162F300500DD16DB /* StreamBuffer.h */; };
+               1A6EB1E0187D0BD30030126F /* StringView.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A6EB1DF187D0BD30030126F /* StringView.h */; };
                1FA47C8A152502DA00568D1B /* WebCoreThread.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1FA47C88152502DA00568D1B /* WebCoreThread.cpp */; };
                1FA47C8B152502DA00568D1B /* WebCoreThread.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FA47C89152502DA00568D1B /* WebCoreThread.h */; };
                26147B0A15DDCCDC00DDB907 /* IntegerToStringConversion.h in Headers */ = {isa = PBXBuildFile; fileRef = 26147B0815DDCCDC00DDB907 /* IntegerToStringConversion.h */; };
                1A233C7C17DAA6E300A93ACF /* MallocPtr.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MallocPtr.h; sourceTree = "<group>"; };
                1A3F6BE6174ADA2100B2EEA7 /* NeverDestroyed.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NeverDestroyed.h; sourceTree = "<group>"; };
                1A6BB768162F300500DD16DB /* StreamBuffer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StreamBuffer.h; sourceTree = "<group>"; };
+               1A6EB1DF187D0BD30030126F /* StringView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringView.h; sourceTree = "<group>"; };
                1FA47C88152502DA00568D1B /* WebCoreThread.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebCoreThread.cpp; sourceTree = "<group>"; };
                1FA47C89152502DA00568D1B /* WebCoreThread.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebCoreThread.h; sourceTree = "<group>"; };
                26147B0815DDCCDC00DDB907 /* IntegerToStringConversion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IntegerToStringConversion.h; sourceTree = "<group>"; };
                                A8A4732C151A825B004123FF /* TextPosition.h */,
                                A8A4732D151A825B004123FF /* WTFString.cpp */,
                                A8A4732E151A825B004123FF /* WTFString.h */,
+                               1A6EB1DF187D0BD30030126F /* StringView.h */,
                        );
                        path = text;
                        sourceTree = "<group>";
                                A8A4742F151A825B004123FF /* TCPageMap.h in Headers */,
                                A8A47430151A825B004123FF /* TCSpinLock.h in Headers */,
                                A8A47432151A825B004123FF /* TCSystemAlloc.h in Headers */,
+                               1A6EB1E0187D0BD30030126F /* StringView.h in Headers */,
                                A8A47433151A825B004123FF /* TemporaryChange.h in Headers */,
                                A8A47444151A825B004123FF /* TextPosition.h in Headers */,
                                A8A47447151A825B004123FF /* ThreadFunctionInvocation.h in Headers */,
index 3723c75eefdba5ae892f8e49ae7a9fd34f88383a..b002d5c507e30f831e10310c8d0062fc7c7d3ff4 100644 (file)
@@ -134,6 +134,7 @@ set(WTF_HEADERS
     text/StringBuffer.h
     text/StringHash.h
     text/StringImpl.h
+    text/StringView.h
     text/WTFString.h
 
     threads/BinarySemaphore.h
index f1c223ff91273c45ebf3eeebeacd3b7868899421..167cafb2008841d2d21092520b03a7672388c4a5 100644 (file)
@@ -47,6 +47,7 @@ class PrintStream;
 class String;
 class StringBuilder;
 class StringImpl;
+class StringView;
 
 }
 
@@ -69,6 +70,7 @@ using WTF::String;
 using WTF::StringBuffer;
 using WTF::StringBuilder;
 using WTF::StringImpl;
+using WTF::StringView;
 using WTF::Vector;
 
 #endif // WTF_Forward_h
diff --git a/Source/WTF/wtf/text/StringView.h b/Source/WTF/wtf/text/StringView.h
new file mode 100644 (file)
index 0000000..4c854e1
--- /dev/null
@@ -0,0 +1,148 @@
+/*
+ * Copyright (C) 2014 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef StringView_h
+#define StringView_h
+
+#include <wtf/text/WTFString.h>
+
+namespace WTF {
+
+// StringView is a non-owning reference to a string, similar to the proposed std::string_view.
+// Whether the string is 8-bit or 16-bit is encoded in the upper bit of the length member.
+// This means that strings longer than 2 Gigabytes can not be represented. If that turns out to be
+// a problem we can investigate alternative solutions.
+
+class StringView {
+public:
+    StringView(const LChar *characters, unsigned length)
+    {
+        initialize(characters, length);
+    }
+
+    StringView(const UChar *characters, unsigned length)
+    {
+        initialize(characters, length);
+    }
+
+    StringView(const String& string)
+        : m_characters(nullptr)
+        , m_length(0)
+    {
+        if (!string.impl())
+            return;
+        
+        if (string.is8Bit())
+            initialize(string.characters8(), string.length());
+        else
+            initialize(string.characters16(), string.length());
+    }
+
+    static StringView empty()
+    {
+        return StringView(reinterpret_cast<const LChar*>(""), 0);
+    }
+
+    const LChar* characters8() const
+    {
+        ASSERT(is8Bit());
+
+        return static_cast<const LChar*>(m_characters);
+    }
+
+    const UChar* characters16() const
+    {
+        ASSERT(!is8Bit());
+
+        return static_cast<const UChar*>(m_characters);
+    }
+
+    bool isEmpty() const { return !length(); }
+    unsigned length() const { return m_length & ~is16BitStringFlag; }
+
+    bool is8Bit() const { return !(m_length & is16BitStringFlag); }
+
+    StringView substring(unsigned start, unsigned length = std::numeric_limits<unsigned>::max()) const
+    {
+        if (start >= this->length())
+            return empty();
+        unsigned maxLength = this->length() - start;
+
+        if (length >= maxLength) {
+            if (!start)
+                return *this;
+            length = maxLength;
+        }
+
+        if (is8Bit())
+            return StringView(characters8() + start, length);
+
+        return StringView(characters16() + start, length);
+    }
+
+    String toString() const
+    {
+        if (is8Bit())
+            return String(characters8(), length());
+
+        return String(characters16(), length());
+    }
+
+    String toStringWithoutCopying() const
+    {
+        if (is8Bit())
+            return StringImpl::createWithoutCopying(characters8(), length());
+
+        return StringImpl::createWithoutCopying(characters16(), length());
+    }
+
+private:
+    void initialize(const LChar *characters, unsigned length)
+    {
+        ASSERT(!(length & is16BitStringFlag));
+        
+        m_characters = characters;
+        m_length = length;
+    }
+
+    void initialize(const UChar *characters, unsigned length)
+    {
+        ASSERT(!(length & is16BitStringFlag));
+        
+        m_characters = characters;
+        m_length = is16BitStringFlag | length;
+    }
+
+    static const unsigned is16BitStringFlag = 1u << 31;
+
+    const void* m_characters;
+    unsigned m_length;
+};
+
+} // namespace WTF
+
+using WTF::StringView;
+
+#endif // StringView_h
index 2b8c992e60af6619592f0b91b4bd80c35fe7df53..d1fb6e8bdd55481bd4dfd1a474fda7a05c55aa09 100644 (file)
@@ -1,3 +1,22 @@
+2014-01-08  Anders Carlsson  <andersca@apple.com>
+
+        Add WTF::StringView and use it for grammar checking
+        https://bugs.webkit.org/show_bug.cgi?id=126644
+
+        Reviewed by Antti Koivisto.
+
+        Use a StringView in TextCheckerClient::checkTextOfParagraph to avoid upconverting strings unnecessarily.
+
+        * editing/TextCheckingHelper.cpp:
+        (WebCore::checkTextOfParagraph):
+        Pass a StringView to TextCheckerClient::checkTextOfParagraph.
+        
+        * loader/EmptyClients.h:
+        Update for TextCheckerClient changes.
+
+        * platform/text/TextCheckerClient.h:
+        Change TextCheckerClient::checkTextOfParagraph to take a StringView.
+        
 2014-01-08  Eric Carlson  <eric.carlson@apple.com>
 
         Unreviewed Windows build fix attempt after r161481.
index 22cb98bea6101b839c5542d1215f6d3ead189746..e1ce1460e09bb03c91d86b7dd7a54e7fe15c4e06 100644 (file)
@@ -36,6 +36,7 @@
 #include "TextIterator.h"
 #include "VisiblePosition.h"
 #include "VisibleUnits.h"
+#include <wtf/text/StringView.h>
 
 namespace WebCore {
 
@@ -639,7 +640,7 @@ void checkTextOfParagraph(TextCheckerClient* client, const UChar* text, int leng
                           TextCheckingTypeMask checkingTypes, Vector<TextCheckingResult>& results)
 {
 #if USE(UNIFIED_TEXT_CHECKING)
-    client->checkTextOfParagraph(text, length, checkingTypes, results);
+    results = client->checkTextOfParagraph(StringView(text, length), checkingTypes);
 #else
     Vector<TextCheckingResult> spellingResult;
     if (checkingTypes & TextCheckingTypeSpelling)
index da63cf64fd6e56467e9882e9fe7b30aec69d6d36..a274f6aaba308b54e02f9b5d92ab780f1d6dc39c 100644 (file)
@@ -42,6 +42,7 @@
 #include "InspectorClient.h"
 #include "Page.h"
 #include "ResourceError.h"
+#include <wtf/text/StringView.h>
 
 /*
  This file holds empty Client stubs for use by WebCore.
@@ -425,7 +426,7 @@ public:
     virtual void checkGrammarOfString(const UChar*, int, Vector<GrammarDetail>&, int*, int*) OVERRIDE { }
 
 #if USE(UNIFIED_TEXT_CHECKING)
-    virtual void checkTextOfParagraph(const UChar*, int, TextCheckingTypeMask, Vector<TextCheckingResult>&) OVERRIDE { };
+    virtual Vector<TextCheckingResult> checkTextOfParagraph(StringView, TextCheckingTypeMask) OVERRIDE { return Vector<TextCheckingResult>(); }
 #endif
 
     virtual void getGuessesForWord(const String&, const String&, Vector<String>&) OVERRIDE { }
index 803bfb88b8f45342c96e30fb790c4e568d608cbe..054fb11ecf89e681d5e8ee2aefeb6cae5c28ef9f 100644 (file)
@@ -49,7 +49,7 @@ public:
     virtual void checkGrammarOfString(const UChar*, int length, Vector<GrammarDetail>&, int* badGrammarLocation, int* badGrammarLength) = 0;
 
 #if USE(UNIFIED_TEXT_CHECKING)
-    virtual void checkTextOfParagraph(const UChar* text, int length, TextCheckingTypeMask checkingTypes, Vector<TextCheckingResult>& results) = 0;
+    virtual Vector<TextCheckingResult> checkTextOfParagraph(StringView, TextCheckingTypeMask checkingTypes) = 0;
 #endif
 
     // For spellcheckers that support multiple languages, it's often important to be able to identify the language in order to
index a24461e7ecef8b81e9aecf735735abe513699698..f88fdeb56984a424c932d81b6e3c834fb6c8bf90 100644 (file)
@@ -1,3 +1,15 @@
+2014-01-08  Anders Carlsson  <andersca@apple.com>
+
+        Add WTF::StringView and use it for grammar checking
+        https://bugs.webkit.org/show_bug.cgi?id=126644
+
+        Reviewed by Antti Koivisto.
+
+        Update to match WebCore changes.
+
+        * WebCoreSupport/EditorClientEfl.h:
+        (WebCore::EditorClientEfl::checkTextOfParagraph):
+
 2014-01-05  Jinwoo Song  <jinwoo7.song@samsung.com>
 
         [EFL] Fix failing WebKit1 API test of ewk_view_setting_tiled_backing_store
index 267d6594c7170b8cd3eb79e670d7e78be40df6b7..64ea24a61ef90611209da20573972942b8e62841 100644 (file)
@@ -40,6 +40,7 @@
 #include "TextCheckerClient.h"
 #include <wtf/Deque.h>
 #include <wtf/Forward.h>
+#include <wtf/text/StringView.h>
 
 struct Ewk_Should_Insert_Node_Event {
     WebCore::Node* node;
@@ -147,7 +148,7 @@ public:
     virtual void setInputMethodState(bool enabled);
     virtual void requestCheckingOfString(WTF::PassRefPtr<WebCore::TextCheckingRequest>) { }
 #if USE(UNIFIED_TEXT_CHECKING)
-    virtual void checkTextOfParagraph(const UChar*, int, TextCheckingTypeMask, Vector<TextCheckingResult>&) { }
+    virtual Vector<TextCheckingResult> checkTextOfParagraph(StringView, TextCheckingTypeMask checkingTypes) { return Vector<TextCheckingResult>(); }
 #endif
     virtual TextCheckerClient* textChecker() { return this; }
 
index e438c8543c5c6b533b219115e045535acf0a651f..4c13bc742a0b8939966bd100565b6980551a8ea1 100644 (file)
@@ -1,3 +1,20 @@
+2014-01-08  Anders Carlsson  <andersca@apple.com>
+
+        Add WTF::StringView and use it for grammar checking
+        https://bugs.webkit.org/show_bug.cgi?id=126644
+
+        Reviewed by Antti Koivisto.
+
+        * WebCoreSupport/WebEditorClient.h:
+        * WebCoreSupport/WebEditorClient.mm:
+        (WebEditorClient::checkTextOfParagraph):
+        Update to match the new signature.
+
+        (nsStringWithoutCopying):
+        New function that creates an NSString that references StringView data
+        without copying the data. (If this function proves to be useful elsewhere, it could
+        potentially be added to StringView).
+
 2014-01-02  Andy Estes  <aestes@apple.com>
 
         [iOS] Clean up some exported headers
index c7305909741e8c5c4f3f579901bdbc63b8cbba6d..fbef8b0a7d64751e9b5b5b070dccbdb273926fd6 100644 (file)
@@ -159,7 +159,7 @@ public:
     virtual void checkSpellingOfString(const UChar*, int length, int* misspellingLocation, int* misspellingLength) OVERRIDE;
     virtual WTF::String getAutoCorrectSuggestionForMisspelledWord(const WTF::String&) OVERRIDE;
     virtual void checkGrammarOfString(const UChar*, int length, WTF::Vector<WebCore::GrammarDetail>&, int* badGrammarLocation, int* badGrammarLength) OVERRIDE;
-    virtual void checkTextOfParagraph(const UChar* text, int length, WebCore::TextCheckingTypeMask checkingTypes, WTF::Vector<WebCore::TextCheckingResult>& results) OVERRIDE;
+    virtual Vector<WebCore::TextCheckingResult> checkTextOfParagraph(StringView, WebCore::TextCheckingTypeMask checkingTypes) OVERRIDE;
     virtual void updateSpellingUIWithGrammarString(const WTF::String&, const WebCore::GrammarDetail&) OVERRIDE;
     virtual void updateSpellingUIWithMisspelledWord(const WTF::String&) OVERRIDE;
     virtual void showSpellingUI(bool show) OVERRIDE;
@@ -173,7 +173,7 @@ public:
     virtual WTF::String getAutoCorrectSuggestionForMisspelledWord(const WTF::String&) OVERRIDE { return ""; }
     virtual void checkGrammarOfString(const UChar*, int length, WTF::Vector<WebCore::GrammarDetail>&, int* badGrammarLocation, int* badGrammarLength) OVERRIDE { }
     // Note: checkTextOfParagraph() is implemented. 
-    virtual void checkTextOfParagraph(const UChar* text, int length, WebCore::TextCheckingTypeMask checkingTypes, WTF::Vector<WebCore::TextCheckingResult>& results) OVERRIDE;
+    virtual Vector<WebCore::TextCheckingResult> checkTextOfParagraph(StringView, WebCore::TextCheckingTypeMask checkingTypes) OVERRIDE;
     virtual void updateSpellingUIWithGrammarString(const WTF::String&, const WebCore::GrammarDetail&) OVERRIDE { }
     virtual void updateSpellingUIWithMisspelledWord(const WTF::String&) OVERRIDE { }
     virtual void showSpellingUI(bool show) OVERRIDE { }
index 1cdd3e6fd83e9e916aa659ae0914985bbd55d443..0c968923f2f0a47ea720edfcc1769a6326c95122 100644 (file)
@@ -71,6 +71,7 @@
 #import <wtf/MainThread.h>
 #import <wtf/PassRefPtr.h>
 #import <wtf/RunLoop.h>
+#import <wtf/text/StringView.h>
 #import <wtf/text/WTFString.h>
 
 #if PLATFORM(IOS)
@@ -874,12 +875,14 @@ int WebEditorClient::pasteboardChangeCount()
     return 0;
 }
 
-void WebEditorClient::checkTextOfParagraph(const UChar* text, int length, TextCheckingTypeMask checkingTypes, Vector<TextCheckingResult>& results)
+void WebEditorClient::checkTextOfParagraph(StringView string, TextCheckingTypeMask checkingTypes)
 {
     ASSERT(checkingTypes & NSTextCheckingTypeSpelling);
-    NSString *textString = [[NSString alloc] initWithCharactersNoCopy:const_cast<UChar*>(text) length:length freeWhenDone:NO];
-    NSArray *incomingResults = [[m_webView _UIKitDelegateForwarder] checkSpellingOfString:textString];
-    [textString release];
+
+    NSArray *incomingResults = [[m_webView _UIKitDelegateForwarder] checkSpellingOfString:nsStringWithoutCopying(string).get()];
+
+    Vector<TextCheckingResult> results;
+
     for (NSValue *incomingResult in incomingResults) {
         NSRange resultRange = [incomingResult rangeValue];
         ASSERT(resultRange.location != NSNotFound && resultRange.length > 0);
@@ -889,9 +892,19 @@ void WebEditorClient::checkTextOfParagraph(const UChar* text, int length, TextCh
         result.length = resultRange.length;
         results.append(result);
     }
+
+    return results;
 }
 #endif // PLATFORM(IOS)
 
+static RetainPtr<NSString> nsStringWithoutCopying(StringView stringView)
+{
+    if (stringView.is8Bit())
+        return adoptNS([[NSString alloc] initWithBytesNoCopy:const_cast<LChar*>(stringView.characters8()) length:stringView.length() encoding:NSISOLatin1StringEncoding freeWhenDone:NO]);
+
+    return adoptNS([[NSString alloc] initWithCharactersNoCopy:const_cast<unichar*>(stringView.characters16()) length:stringView.length() freeWhenDone:NO]);
+}
+
 #if !PLATFORM(IOS)
 bool WebEditorClient::shouldEraseMarkersAfterChangeSelection(TextCheckingType type) const
 {
@@ -1042,12 +1055,13 @@ static Vector<TextCheckingResult> core(NSArray *incomingResults, TextCheckingTyp
     return results;
 }
 
-void WebEditorClient::checkTextOfParagraph(const UChar* text, int length, TextCheckingTypeMask checkingTypes, Vector<TextCheckingResult>& results)
+Vector<TextCheckingResult> WebEditorClient::checkTextOfParagraph(StringView string, TextCheckingTypeMask checkingTypes)
 {
-    NSString *textString = [[NSString alloc] initWithCharactersNoCopy:const_cast<UChar*>(text) length:length freeWhenDone:NO];
-    NSArray *incomingResults = [[NSSpellChecker sharedSpellChecker] checkString:textString range:NSMakeRange(0, [textString length]) types:(checkingTypes|NSTextCheckingTypeOrthography) options:nil inSpellDocumentWithTag:spellCheckerDocumentTag() orthography:NULL wordCount:NULL];
-    [textString release];
-    results = core(incomingResults, checkingTypes);
+    auto textString = nsStringWithoutCopying(string);
+
+    NSArray *incomingResults = [[NSSpellChecker sharedSpellChecker] checkString:textString.get() range:NSMakeRange(0, [textString length]) types:(checkingTypes|NSTextCheckingTypeOrthography) options:nil inSpellDocumentWithTag:spellCheckerDocumentTag() orthography:NULL wordCount:NULL];
+
+    return core(incomingResults, checkingTypes);
 }
 
 void WebEditorClient::updateSpellingUIWithGrammarString(const String& badGrammarPhrase, const GrammarDetail& grammarDetail)
index cb42d35131233b7aab01bcb22a01fee821207c5a..ca35c89072ab0a3f7f94c5e2f06719adc9aca307 100644 (file)
@@ -1,3 +1,16 @@
+2014-01-08  Anders Carlsson  <andersca@apple.com>
+
+        Add WTF::StringView and use it for grammar checking
+        https://bugs.webkit.org/show_bug.cgi?id=126644
+
+        Reviewed by Antti Koivisto.
+
+        Use a StringView and remove a FIXME.
+
+        * WebProcess/WebCoreSupport/WebEditorClient.cpp:
+        (WebKit::WebEditorClient::checkTextOfParagraph):
+        * WebProcess/WebCoreSupport/WebEditorClient.h:
+
 2014-01-08  Zan Dobersek  <zdobersek@igalia.com>
 
         [Automake] Scripts for generated build targets do not necessarily produce their output
index f98f64a0d382bd9b5542188b3a9c686d6c6e2246..9059f1e45da7fbc7ffc01af467ca8d1815091b78 100644 (file)
@@ -51,6 +51,7 @@
 #include <WebCore/UndoStep.h>
 #include <WebCore/UserTypingGestureIndicator.h>
 #include <wtf/NeverDestroyed.h>
+#include <wtf/text/StringView.h>
 
 using namespace WebCore;
 using namespace HTMLNames;
@@ -442,10 +443,13 @@ void WebEditorClient::checkGrammarOfString(const UChar* text, int length, Vector
 }
 
 #if USE(UNIFIED_TEXT_CHECKING)
-void WebEditorClient::checkTextOfParagraph(const UChar* text, int length, WebCore::TextCheckingTypeMask checkingTypes, Vector<TextCheckingResult>& results)
+Vector<TextCheckingResult> WebEditorClient::checkTextOfParagraph(StringView stringView, WebCore::TextCheckingTypeMask checkingTypes)
 {
-    // FIXME: It would be nice if we wouldn't have to copy the text here.
-    m_page->sendSync(Messages::WebPageProxy::CheckTextOfParagraph(String(text, length), checkingTypes), Messages::WebPageProxy::CheckTextOfParagraph::Reply(results));
+    Vector<TextCheckingResult> results;
+
+    m_page->sendSync(Messages::WebPageProxy::CheckTextOfParagraph(stringView.toStringWithoutCopying(), checkingTypes), Messages::WebPageProxy::CheckTextOfParagraph::Reply(results));
+
+    return results;
 }
 #endif
 
index 3dae6f3afb197e7e1935a428955aa21ec0a29380..b1b28d7654e78f56fae730d4a462de88da8bfc71 100644 (file)
@@ -139,7 +139,7 @@ private:
     virtual String getAutoCorrectSuggestionForMisspelledWord(const String& misspelledWord) OVERRIDE;
     virtual void checkGrammarOfString(const UChar*, int length, Vector<WebCore::GrammarDetail>&, int* badGrammarLocation, int* badGrammarLength) OVERRIDE;
 #if USE(UNIFIED_TEXT_CHECKING)
-    virtual void checkTextOfParagraph(const UChar* text, int length, WebCore::TextCheckingTypeMask checkingTypes, Vector<WebCore::TextCheckingResult>& results) OVERRIDE;
+    virtual Vector<WebCore::TextCheckingResult> checkTextOfParagraph(StringView, WebCore::TextCheckingTypeMask checkingTypes) OVERRIDE;
 #endif
     virtual void updateSpellingUIWithGrammarString(const String&, const WebCore::GrammarDetail&) OVERRIDE;
     virtual void updateSpellingUIWithMisspelledWord(const String&) OVERRIDE;