Bug 238287

Summary: String's find() / reverseFind() / replace() should take in a StringView instead of a String
Product: WebKit Reporter: Chris Dumez <cdumez>
Component: Web Template FrameworkAssignee: Chris Dumez <cdumez>
Status: RESOLVED FIXED    
Severity: Normal CC: alecflett, beidson, benjamin, changseok, cmarcelo, darin, dino, eric.carlson, esprehn+autocc, ews-watchlist, galpeter, ggaren, glenn, gyuyoung.kim, hi, hta, jer.noble, joepeck, jsbell, keith_miller, kondapallykalyan, luiz, mark.lam, mifenton, msaboff, pangle, philipj, saam, sergio, tommyw, tzagallo, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
See Also: https://bugs.webkit.org/show_bug.cgi?id=238235
https://bugs.webkit.org/show_bug.cgi?id=238264
https://bugs.webkit.org/show_bug.cgi?id=238333
Attachments:
Description Flags
Patch
none
Patch
none
Patch
none
Patch
ews-feeder: commit-queue-
Patch
ews-feeder: commit-queue-
Patch
ews-feeder: commit-queue-
Patch
ews-feeder: commit-queue-
Patch
none
Patch
none
Patch none

Description Chris Dumez 2022-03-23 13:54:17 PDT
String's find() / reverseFind() / replace() should take in a StringView instead of a String, to avoid unnecessary String construction.
Comment 1 Chris Dumez 2022-03-23 13:58:48 PDT
Created attachment 455549 [details]
Patch
Comment 2 Chris Dumez 2022-03-23 14:17:30 PDT
Created attachment 455552 [details]
Patch
Comment 3 Chris Dumez 2022-03-23 15:58:55 PDT
Created attachment 455573 [details]
Patch
Comment 4 Chris Dumez 2022-03-23 19:27:12 PDT
Created attachment 455598 [details]
Patch
Comment 5 Chris Dumez 2022-03-23 21:09:39 PDT
Created attachment 455601 [details]
Patch
Comment 6 Chris Dumez 2022-03-23 21:47:01 PDT
Created attachment 455604 [details]
Patch
Comment 7 Chris Dumez 2022-03-23 22:15:39 PDT
Created attachment 455607 [details]
Patch
Comment 8 Chris Dumez 2022-03-23 23:03:40 PDT
Created attachment 455610 [details]
Patch
Comment 9 Darin Adler 2022-03-24 08:13:37 PDT
Comment on attachment 455610 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=455610&action=review

> Source/WTF/wtf/text/AtomString.h:124
> +    size_t findIgnoringASCIICase(StringView, unsigned startOffset) const;

Strange that we call it "start" in find and call it "startOffset" in findIgnoringASCIICase. Also strange that one uses a defaulted argument and the other uses overloading.

> Source/WTF/wtf/text/StringImpl.cpp:964
>      if (UNLIKELY(!matchString))

I typically like to write isNull() rather than depending on operator!, but I suppose both work?

> Source/WTF/wtf/text/StringImpl.h:436
> +    WTF_EXPORT_PRIVATE size_t find(StringView, unsigned index);

I find the mix of "index", "start", and "startOffset" slightly annoying.

> Source/WebCore/page/UserContentURLPattern.cpp:66
>      static NeverDestroyed<const String> schemeSeparator(MAKE_STATIC_STRING_IMPL("://"));

This is now never used as a String, so I suggest we just make it an ASCIILiteral or a const char* or an auto. No need for NeverDestroyed any more.

> Source/WebCore/platform/network/curl/CurlMultipartHandle.cpp:55
> +        auto splitPosistion = header.find(':');

Noticed the "posistion" typo here.

> Source/WebKit/NetworkProcess/soup/NetworkProcessSoup.cpp:58
> +    size_t cLocalePosition = languages.find("c"_s);

Even this can be changed so it doesn’t involve creating/destroying strings. No reason Vector::find can’t be changed to work with anything that can do == with the Vector elements, doesn’t need to actually be the same type as the element.

Not that you should do this right now.
Comment 10 Chris Dumez 2022-03-24 08:21:10 PDT
(In reply to Darin Adler from comment #9)
> Comment on attachment 455610 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=455610&action=review
> 
> > Source/WTF/wtf/text/AtomString.h:124
> > +    size_t findIgnoringASCIICase(StringView, unsigned startOffset) const;
> 
> Strange that we call it "start" in find and call it "startOffset" in
> findIgnoringASCIICase. Also strange that one uses a defaulted argument and
> the other uses overloading.
> 
> > Source/WTF/wtf/text/StringImpl.cpp:964
> >      if (UNLIKELY(!matchString))
> 
> I typically like to write isNull() rather than depending on operator!, but I
> suppose both work?

Yes, it is identical:
```
inline StringView::operator bool() const
{
    return !isNull();
}
```
Comment 11 Chris Dumez 2022-03-24 08:28:33 PDT
Created attachment 455641 [details]
Patch
Comment 12 Chris Dumez 2022-03-24 08:36:03 PDT
Created attachment 455642 [details]
Patch
Comment 13 Chris Dumez 2022-03-24 09:55:54 PDT
Comment on attachment 455642 [details]
Patch

Clearing flags on attachment: 455642

Committed r291800 (248828@trunk): <https://commits.webkit.org/248828@trunk>
Comment 14 Chris Dumez 2022-03-24 09:55:59 PDT
All reviewed patches have been landed.  Closing bug.
Comment 15 Radar WebKit Bug Importer 2022-03-24 09:56:18 PDT
<rdar://problem/90774749>