Changeset 161518 in webkit


Ignore:
Timestamp:
Jan 8, 2014 1:19:21 PM (10 years ago)
Author:
andersca@apple.com
Message:

Add WTF::StringView and use it for grammar checking
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):

Location:
trunk/Source
Files:
1 added
19 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r161451 r161518  
     12014-01-08  Anders Carlsson  <andersca@apple.com>
     2
     3        Add WTF::StringView and use it for grammar checking
     4        https://bugs.webkit.org/show_bug.cgi?id=126644
     5
     6        Reviewed by Antti Koivisto.
     7
     8        Add a new StringView class which is an immutable reference to either an 8-bit or a 16-bit string.
     9
     10        This makes it easier to pass substrings around without having to either copy string data
     11        or upconvert and use UChar pointers.
     12
     13        The StringView class is very loosely based on the std::string_view class which is proposed for a future version of C++,
     14        see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3609.html for more details.
     15
     16        * GNUmakefile.list.am:
     17        * WTF.vcxproj/WTF.vcxproj:
     18        * WTF.vcxproj/WTF.vcxproj.filters:
     19        * WTF.xcodeproj/project.pbxproj:
     20        * wtf/CMakeLists.txt:
     21        * wtf/Forward.h:
     22        * wtf/text/StringView.h: Added.
     23        (WTF::StringView::StringView):
     24        (WTF::StringView::empty):
     25        (WTF::StringView::characters8):
     26        (WTF::StringView::characters16):
     27        (WTF::StringView::isEmpty):
     28        (WTF::StringView::length):
     29        (WTF::StringView::is8Bit):
     30        (WTF::StringView::substring):
     31        (WTF::StringView::toString):
     32        (WTF::StringView::toStringWithoutCopying):
     33        (WTF::StringView::encodedCharacters):
     34
    1352014-01-07  David Farler  <dfarler@apple.com>
    236
  • trunk/Source/WTF/GNUmakefile.list.am

    r161173 r161518  
    158158    Source/WTF/wtf/StringExtras.h \
    159159    Source/WTF/wtf/StringHashDumpContext.h \
     160    Source/WTF/wtf/StringHasher.h \
    160161    Source/WTF/wtf/StringPrintStream.cpp \
    161162    Source/WTF/wtf/StringPrintStream.h \
    162     Source/WTF/wtf/StringHasher.h \
    163163    Source/WTF/wtf/TCPackedCache.h \
    164164    Source/WTF/wtf/TCPageMap.h \
     
    242242    Source/WTF/wtf/text/StringOperators.h \
    243243    Source/WTF/wtf/text/StringStatics.cpp \
     244    Source/WTF/wtf/text/StringView.h \
    244245    Source/WTF/wtf/text/TextPosition.h \
    245246    Source/WTF/wtf/text/WTFString.cpp \
  • trunk/Source/WTF/WTF.vcxproj/WTF.vcxproj

    r161173 r161518  
    292292    <ClInclude Include="..\wtf\text\StringImpl.h" />
    293293    <ClInclude Include="..\wtf\text\StringOperators.h" />
     294    <ClInclude Include="..\wtf\text\StringView.h" />
    294295    <ClInclude Include="..\wtf\text\WTFString.h" />
    295296    <ClInclude Include="..\wtf\Threading.h" />
  • trunk/Source/WTF/WTF.vcxproj/WTF.vcxproj.filters

    r161173 r161518  
    318318    <ClInclude Include="..\wtf\text\StringOperators.h">
    319319      <Filter>text</Filter>
     320    </ClInclude>
     321    <ClInclude Include="..\wtf\text\StringView.h">
     322      <Filter>wtf</Filter>
    320323    </ClInclude>
    321324    <ClInclude Include="..\wtf\text\WTFString.h">
  • trunk/Source/WTF/WTF.xcodeproj/project.pbxproj

    r161173 r161518  
    5656                1A233C7D17DAA6E300A93ACF /* MallocPtr.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A233C7C17DAA6E300A93ACF /* MallocPtr.h */; };
    5757                1A6BB769162F300500DD16DB /* StreamBuffer.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A6BB768162F300500DD16DB /* StreamBuffer.h */; };
     58                1A6EB1E0187D0BD30030126F /* StringView.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A6EB1DF187D0BD30030126F /* StringView.h */; };
    5859                1FA47C8A152502DA00568D1B /* WebCoreThread.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1FA47C88152502DA00568D1B /* WebCoreThread.cpp */; };
    5960                1FA47C8B152502DA00568D1B /* WebCoreThread.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FA47C89152502DA00568D1B /* WebCoreThread.h */; };
     
    323324                1A3F6BE6174ADA2100B2EEA7 /* NeverDestroyed.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NeverDestroyed.h; sourceTree = "<group>"; };
    324325                1A6BB768162F300500DD16DB /* StreamBuffer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StreamBuffer.h; sourceTree = "<group>"; };
     326                1A6EB1DF187D0BD30030126F /* StringView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringView.h; sourceTree = "<group>"; };
    325327                1FA47C88152502DA00568D1B /* WebCoreThread.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebCoreThread.cpp; sourceTree = "<group>"; };
    326328                1FA47C89152502DA00568D1B /* WebCoreThread.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebCoreThread.h; sourceTree = "<group>"; };
     
    906908                                A8A4732D151A825B004123FF /* WTFString.cpp */,
    907909                                A8A4732E151A825B004123FF /* WTFString.h */,
     910                                1A6EB1DF187D0BD30030126F /* StringView.h */,
    908911                        );
    909912                        path = text;
     
    11051108                                A8A47430151A825B004123FF /* TCSpinLock.h in Headers */,
    11061109                                A8A47432151A825B004123FF /* TCSystemAlloc.h in Headers */,
     1110                                1A6EB1E0187D0BD30030126F /* StringView.h in Headers */,
    11071111                                A8A47433151A825B004123FF /* TemporaryChange.h in Headers */,
    11081112                                A8A47444151A825B004123FF /* TextPosition.h in Headers */,
  • trunk/Source/WTF/wtf/CMakeLists.txt

    r161173 r161518  
    135135    text/StringHash.h
    136136    text/StringImpl.h
     137    text/StringView.h
    137138    text/WTFString.h
    138139
  • trunk/Source/WTF/wtf/Forward.h

    r161173 r161518  
    4848class StringBuilder;
    4949class StringImpl;
     50class StringView;
    5051
    5152}
     
    7071using WTF::StringBuilder;
    7172using WTF::StringImpl;
     73using WTF::StringView;
    7274using WTF::Vector;
    7375
  • trunk/Source/WebCore/ChangeLog

    r161506 r161518  
     12014-01-08  Anders Carlsson  <andersca@apple.com>
     2
     3        Add WTF::StringView and use it for grammar checking
     4        https://bugs.webkit.org/show_bug.cgi?id=126644
     5
     6        Reviewed by Antti Koivisto.
     7
     8        Use a StringView in TextCheckerClient::checkTextOfParagraph to avoid upconverting strings unnecessarily.
     9
     10        * editing/TextCheckingHelper.cpp:
     11        (WebCore::checkTextOfParagraph):
     12        Pass a StringView to TextCheckerClient::checkTextOfParagraph.
     13       
     14        * loader/EmptyClients.h:
     15        Update for TextCheckerClient changes.
     16
     17        * platform/text/TextCheckerClient.h:
     18        Change TextCheckerClient::checkTextOfParagraph to take a StringView.
     19       
    1202014-01-08  Eric Carlson  <eric.carlson@apple.com>
    221
  • trunk/Source/WebCore/editing/TextCheckingHelper.cpp

    r158350 r161518  
    3737#include "VisiblePosition.h"
    3838#include "VisibleUnits.h"
     39#include <wtf/text/StringView.h>
    3940
    4041namespace WebCore {
     
    640641{
    641642#if USE(UNIFIED_TEXT_CHECKING)
    642     client->checkTextOfParagraph(text, length, checkingTypes, results);
     643    results = client->checkTextOfParagraph(StringView(text, length), checkingTypes);
    643644#else
    644645    Vector<TextCheckingResult> spellingResult;
  • trunk/Source/WebCore/loader/EmptyClients.h

    r161342 r161518  
    4343#include "Page.h"
    4444#include "ResourceError.h"
     45#include <wtf/text/StringView.h>
    4546
    4647/*
     
    426427
    427428#if USE(UNIFIED_TEXT_CHECKING)
    428     virtual void checkTextOfParagraph(const UChar*, int, TextCheckingTypeMask, Vector<TextCheckingResult>&) OVERRIDE { };
     429    virtual Vector<TextCheckingResult> checkTextOfParagraph(StringView, TextCheckingTypeMask) OVERRIDE { return Vector<TextCheckingResult>(); }
    429430#endif
    430431
  • trunk/Source/WebCore/platform/text/TextCheckerClient.h

    r119192 r161518  
    5050
    5151#if USE(UNIFIED_TEXT_CHECKING)
    52     virtual void checkTextOfParagraph(const UChar* text, int length, TextCheckingTypeMask checkingTypes, Vector<TextCheckingResult>& results) = 0;
     52    virtual Vector<TextCheckingResult> checkTextOfParagraph(StringView, TextCheckingTypeMask checkingTypes) = 0;
    5353#endif
    5454
  • trunk/Source/WebKit/efl/ChangeLog

    r161331 r161518  
     12014-01-08  Anders Carlsson  <andersca@apple.com>
     2
     3        Add WTF::StringView and use it for grammar checking
     4        https://bugs.webkit.org/show_bug.cgi?id=126644
     5
     6        Reviewed by Antti Koivisto.
     7
     8        Update to match WebCore changes.
     9
     10        * WebCoreSupport/EditorClientEfl.h:
     11        (WebCore::EditorClientEfl::checkTextOfParagraph):
     12
    1132014-01-05  Jinwoo Song  <jinwoo7.song@samsung.com>
    214
  • trunk/Source/WebKit/efl/WebCoreSupport/EditorClientEfl.h

    r159856 r161518  
    4141#include <wtf/Deque.h>
    4242#include <wtf/Forward.h>
     43#include <wtf/text/StringView.h>
    4344
    4445struct Ewk_Should_Insert_Node_Event {
     
    148149    virtual void requestCheckingOfString(WTF::PassRefPtr<WebCore::TextCheckingRequest>) { }
    149150#if USE(UNIFIED_TEXT_CHECKING)
    150     virtual void checkTextOfParagraph(const UChar*, int, TextCheckingTypeMask, Vector<TextCheckingResult>&) { }
     151    virtual Vector<TextCheckingResult> checkTextOfParagraph(StringView, TextCheckingTypeMask checkingTypes) { return Vector<TextCheckingResult>(); }
    151152#endif
    152153    virtual TextCheckerClient* textChecker() { return this; }
  • trunk/Source/WebKit/mac/ChangeLog

    r161479 r161518  
     12014-01-08  Anders Carlsson  <andersca@apple.com>
     2
     3        Add WTF::StringView and use it for grammar checking
     4        https://bugs.webkit.org/show_bug.cgi?id=126644
     5
     6        Reviewed by Antti Koivisto.
     7
     8        * WebCoreSupport/WebEditorClient.h:
     9        * WebCoreSupport/WebEditorClient.mm:
     10        (WebEditorClient::checkTextOfParagraph):
     11        Update to match the new signature.
     12
     13        (nsStringWithoutCopying):
     14        New function that creates an NSString that references StringView data
     15        without copying the data. (If this function proves to be useful elsewhere, it could
     16        potentially be added to StringView).
     17
    1182014-01-02  Andy Estes  <aestes@apple.com>
    219
  • trunk/Source/WebKit/mac/WebCoreSupport/WebEditorClient.h

    r161185 r161518  
    160160    virtual WTF::String getAutoCorrectSuggestionForMisspelledWord(const WTF::String&) OVERRIDE;
    161161    virtual void checkGrammarOfString(const UChar*, int length, WTF::Vector<WebCore::GrammarDetail>&, int* badGrammarLocation, int* badGrammarLength) OVERRIDE;
    162     virtual void checkTextOfParagraph(const UChar* text, int length, WebCore::TextCheckingTypeMask checkingTypes, WTF::Vector<WebCore::TextCheckingResult>& results) OVERRIDE;
     162    virtual Vector<WebCore::TextCheckingResult> checkTextOfParagraph(StringView, WebCore::TextCheckingTypeMask checkingTypes) OVERRIDE;
    163163    virtual void updateSpellingUIWithGrammarString(const WTF::String&, const WebCore::GrammarDetail&) OVERRIDE;
    164164    virtual void updateSpellingUIWithMisspelledWord(const WTF::String&) OVERRIDE;
     
    174174    virtual void checkGrammarOfString(const UChar*, int length, WTF::Vector<WebCore::GrammarDetail>&, int* badGrammarLocation, int* badGrammarLength) OVERRIDE { }
    175175    // Note: checkTextOfParagraph() is implemented.
    176     virtual void checkTextOfParagraph(const UChar* text, int length, WebCore::TextCheckingTypeMask checkingTypes, WTF::Vector<WebCore::TextCheckingResult>& results) OVERRIDE;
     176    virtual Vector<WebCore::TextCheckingResult> checkTextOfParagraph(StringView, WebCore::TextCheckingTypeMask checkingTypes) OVERRIDE;
    177177    virtual void updateSpellingUIWithGrammarString(const WTF::String&, const WebCore::GrammarDetail&) OVERRIDE { }
    178178    virtual void updateSpellingUIWithMisspelledWord(const WTF::String&) OVERRIDE { }
  • trunk/Source/WebKit/mac/WebCoreSupport/WebEditorClient.mm

    r161185 r161518  
    7272#import <wtf/PassRefPtr.h>
    7373#import <wtf/RunLoop.h>
     74#import <wtf/text/StringView.h>
    7475#import <wtf/text/WTFString.h>
    7576
     
    875876}
    876877
    877 void WebEditorClient::checkTextOfParagraph(const UChar* text, int length, TextCheckingTypeMask checkingTypes, Vector<TextCheckingResult>& results)
     878void WebEditorClient::checkTextOfParagraph(StringView string, TextCheckingTypeMask checkingTypes)
    878879{
    879880    ASSERT(checkingTypes & NSTextCheckingTypeSpelling);
    880     NSString *textString = [[NSString alloc] initWithCharactersNoCopy:const_cast<UChar*>(text) length:length freeWhenDone:NO];
    881     NSArray *incomingResults = [[m_webView _UIKitDelegateForwarder] checkSpellingOfString:textString];
    882     [textString release];
     881
     882    NSArray *incomingResults = [[m_webView _UIKitDelegateForwarder] checkSpellingOfString:nsStringWithoutCopying(string).get()];
     883
     884    Vector<TextCheckingResult> results;
     885
    883886    for (NSValue *incomingResult in incomingResults) {
    884887        NSRange resultRange = [incomingResult rangeValue];
     
    890893        results.append(result);
    891894    }
     895
     896    return results;
    892897}
    893898#endif // PLATFORM(IOS)
     899
     900static RetainPtr<NSString> nsStringWithoutCopying(StringView stringView)
     901{
     902    if (stringView.is8Bit())
     903        return adoptNS([[NSString alloc] initWithBytesNoCopy:const_cast<LChar*>(stringView.characters8()) length:stringView.length() encoding:NSISOLatin1StringEncoding freeWhenDone:NO]);
     904
     905    return adoptNS([[NSString alloc] initWithCharactersNoCopy:const_cast<unichar*>(stringView.characters16()) length:stringView.length() freeWhenDone:NO]);
     906}
    894907
    895908#if !PLATFORM(IOS)
     
    10431056}
    10441057
    1045 void WebEditorClient::checkTextOfParagraph(const UChar* text, int length, TextCheckingTypeMask checkingTypes, Vector<TextCheckingResult>& results)
    1046 {
    1047     NSString *textString = [[NSString alloc] initWithCharactersNoCopy:const_cast<UChar*>(text) length:length freeWhenDone:NO];
    1048     NSArray *incomingResults = [[NSSpellChecker sharedSpellChecker] checkString:textString range:NSMakeRange(0, [textString length]) types:(checkingTypes|NSTextCheckingTypeOrthography) options:nil inSpellDocumentWithTag:spellCheckerDocumentTag() orthography:NULL wordCount:NULL];
    1049     [textString release];
    1050     results = core(incomingResults, checkingTypes);
     1058Vector<TextCheckingResult> WebEditorClient::checkTextOfParagraph(StringView string, TextCheckingTypeMask checkingTypes)
     1059{
     1060    auto textString = nsStringWithoutCopying(string);
     1061
     1062    NSArray *incomingResults = [[NSSpellChecker sharedSpellChecker] checkString:textString.get() range:NSMakeRange(0, [textString length]) types:(checkingTypes|NSTextCheckingTypeOrthography) options:nil inSpellDocumentWithTag:spellCheckerDocumentTag() orthography:NULL wordCount:NULL];
     1063
     1064    return core(incomingResults, checkingTypes);
    10511065}
    10521066
  • trunk/Source/WebKit2/ChangeLog

    r161497 r161518  
     12014-01-08  Anders Carlsson  <andersca@apple.com>
     2
     3        Add WTF::StringView and use it for grammar checking
     4        https://bugs.webkit.org/show_bug.cgi?id=126644
     5
     6        Reviewed by Antti Koivisto.
     7
     8        Use a StringView and remove a FIXME.
     9
     10        * WebProcess/WebCoreSupport/WebEditorClient.cpp:
     11        (WebKit::WebEditorClient::checkTextOfParagraph):
     12        * WebProcess/WebCoreSupport/WebEditorClient.h:
     13
    1142014-01-08  Zan Dobersek  <zdobersek@igalia.com>
    215
  • trunk/Source/WebKit2/WebProcess/WebCoreSupport/WebEditorClient.cpp

    r161045 r161518  
    5252#include <WebCore/UserTypingGestureIndicator.h>
    5353#include <wtf/NeverDestroyed.h>
     54#include <wtf/text/StringView.h>
    5455
    5556using namespace WebCore;
     
    443444
    444445#if USE(UNIFIED_TEXT_CHECKING)
    445 void WebEditorClient::checkTextOfParagraph(const UChar* text, int length, WebCore::TextCheckingTypeMask checkingTypes, Vector<TextCheckingResult>& results)
    446 {
    447     // FIXME: It would be nice if we wouldn't have to copy the text here.
    448     m_page->sendSync(Messages::WebPageProxy::CheckTextOfParagraph(String(text, length), checkingTypes), Messages::WebPageProxy::CheckTextOfParagraph::Reply(results));
     446Vector<TextCheckingResult> WebEditorClient::checkTextOfParagraph(StringView stringView, WebCore::TextCheckingTypeMask checkingTypes)
     447{
     448    Vector<TextCheckingResult> results;
     449
     450    m_page->sendSync(Messages::WebPageProxy::CheckTextOfParagraph(stringView.toStringWithoutCopying(), checkingTypes), Messages::WebPageProxy::CheckTextOfParagraph::Reply(results));
     451
     452    return results;
    449453}
    450454#endif
  • trunk/Source/WebKit2/WebProcess/WebCoreSupport/WebEditorClient.h

    r159856 r161518  
    140140    virtual void checkGrammarOfString(const UChar*, int length, Vector<WebCore::GrammarDetail>&, int* badGrammarLocation, int* badGrammarLength) OVERRIDE;
    141141#if USE(UNIFIED_TEXT_CHECKING)
    142     virtual void checkTextOfParagraph(const UChar* text, int length, WebCore::TextCheckingTypeMask checkingTypes, Vector<WebCore::TextCheckingResult>& results) OVERRIDE;
     142    virtual Vector<WebCore::TextCheckingResult> checkTextOfParagraph(StringView, WebCore::TextCheckingTypeMask checkingTypes) OVERRIDE;
    143143#endif
    144144    virtual void updateSpellingUIWithGrammarString(const String&, const WebCore::GrammarDetail&) OVERRIDE;
Note: See TracChangeset for help on using the changeset viewer.