Reduce use of String::characters
authordarin@apple.com <darin@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 12 Jan 2014 23:23:44 +0000 (23:23 +0000)
committerdarin@apple.com <darin@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 12 Jan 2014 23:23:44 +0000 (23:23 +0000)
commitd1e76610ed9bfca2460074349da557d7e6c0c427
treef408c8cd7e75c992924c61471528ed6c13f195e3
parent9f0f2c119ee7c37e6ffce19ecfab655d4aac3142
Reduce use of String::characters
https://bugs.webkit.org/show_bug.cgi?id=126854

Reviewed by Sam Weinig.

Source/JavaScriptCore:

* API/JSValueRef.cpp:
(JSValueMakeFromJSONString): Use characters16 instead of characters for 16-bit case.
Had to remove length check because an empty string could be either 8 bit or 16 bit.
Don't need a null string check before calling is8Bit because JSStringRef can't hold
a null string.

* bindings/ScriptValue.cpp:
(Deprecated::jsToInspectorValue): Use the existing string here instead of creating
a new one by calling characters and length on the old string. I think this may be
left over from when string types were not the same in JavaScriptCore and WebCore.
Also rewrite the property names loop to use modern for syntax and fewer locals.

* inspector/InspectorValues.cpp:
(Inspector::escapeChar): Changed to use appendLiteral instead of hard-coding string
lengths. Moved handling of "<" and ">" in here instead of at the call site.
(Inspector::doubleQuoteString): Simplify the code so there is no use of characters
and length. This is still an inefficient way of doing this job and could use a rethink.

* runtime/DatePrototype.cpp:
(JSC::formatLocaleDate): Use RetainPtr, createCFString, and the conversion from
CFStringRef to WTF::String to remove a lot of unneeded code.

* runtime/Identifier.h: Removed unneeded Identifier::characters function.

* runtime/JSStringBuilder.h:
(JSC::JSStringBuilder::append): Use characters16 instead of characters function here,
since we have already checked is8Bit above.

Source/WebCore:

* bindings/objc/WebScriptObject.mm:
(+[WebScriptObject _convertValueToObjcValue:JSC::originRootObject:rootObject:]):
Get rid of unneeded code to turn a WTF::String into an NSString, since that's
built into the WTF::String class.

* editing/CompositeEditCommand.cpp:
(WebCore::containsOnlyWhitespace): Use WTF::String's [] operator instead of
an explicit call to the characters function. Small performance cost.
* editing/TypingCommand.cpp:
(WebCore::TypingCommand::insertText): Ditto.

* editing/VisibleUnits.cpp:
(WebCore::startOfParagraph): Use reverseFind instead of writing our own loop.
(WebCore::endOfParagraph): Use find instead of writing our own loop.

* html/parser/HTMLParserIdioms.cpp:
(WebCore::stripLeadingAndTrailingHTMLSpaces): Use characters16 instead of
characters since we have already checked is8Bit.
(WebCore::parseHTMLNonNegativeInteger): Ditto. Replace the length check with
a check of isNull since a zero length string could be 8 bit, but it's not
safe to call is8Bit on a null WTF::String. (That should probably be fixed.)

* inspector/ContentSearchUtils.cpp:
(WebCore::ContentSearchUtils::createSearchRegexSource): Use StringBuilder
instead of String in code that builds up a string one character at a time.
The old way was ridiculously slow because it allocated a new string for every
character; the new way still isn't all that efficient, but it's better. Also
changed to use strchr instead of WTF::String::find for special characters.

* inspector/InspectorStyleSheet.cpp:
(WebCore::InspectorStyle::newLineAndWhitespaceDelimiters): Use WTF::String's
[] operator instead of an explicit call to the characters function.
* inspector/InspectorStyleTextEditor.cpp:
(WebCore::InspectorStyleTextEditor::insertProperty): Ditto.
(WebCore::InspectorStyleTextEditor::internalReplaceProperty): Ditto.
* platform/Length.cpp:
(WebCore::newCoordsArray): Ditto.

* platform/LinkHash.cpp:
(WebCore::visitedLinkHash): Use characters16 instead of characters since
we have already checked is8Bit. Replace the length check with a check of
isNull (as above in parseHTMLNonNegativeInteger).

* platform/graphics/Color.cpp:
(WebCore::Color::parseHexColor): Use characters16 since we already checked is8Bit.
(WebCore::Color::Color): Ditto.

* platform/graphics/TextRun.h:
(WebCore::TextRun::TextRun): Use characters16 instead of characters since
we have already checked is8Bit. Replace the length check with a check of
isNull (as above in parseHTMLNonNegativeInteger).

* platform/text/TextEncodingRegistry.cpp:
(WebCore::atomicCanonicalTextEncodingName): Use characters16 instead of
characters since we already checked is8Bit. Also use isEmpty instead of !length.

* rendering/RenderBlock.cpp:
(WebCore::RenderBlock::constructTextRun): Use characters16 instead of characters
since we have already checked is8Bit. Replace the length check with a check of
isNull (as above in parseHTMLNonNegativeInteger).

* rendering/RenderCombineText.cpp:
(WebCore::RenderCombineText::width): Check for zero length instead of checking
for null characters.

* svg/SVGFontElement.cpp:
(WebCore::SVGFontElement::registerLigaturesInGlyphCache): Rewrite ligatures
for loop and give local variables a better name. Construct the substring with
the substring function. Still a really inefficient approach -- allocating a new
string for every character is absurdly expensive.

* xml/XPathFunctions.cpp:
(WebCore::XPath::FunId::evaluate): Use String instead of StringBuilder. Still
not all that efficient, but better than before. Use substring to construct the
substring.

* xml/XPathNodeSet.h: Added begin and end functions so we can use a C++11 for
loop with this class.

Source/WTF:

* wtf/text/StringImpl.cpp:
(WTF::StringImpl::replace): Use characters16 here since is8Bit was already checked.
* wtf/text/WTFString.h:
(WTF::String::isAllSpecialCharacters): Use characters16 here since is8Bit was
already checked. Also renamed "len" to "length".

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@161840 268f45cc-cd09-0410-ab3c-d52691b4dbfc
29 files changed:
Source/JavaScriptCore/API/JSValueRef.cpp
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/bindings/ScriptValue.cpp
Source/JavaScriptCore/inspector/InspectorValues.cpp
Source/JavaScriptCore/runtime/DatePrototype.cpp
Source/JavaScriptCore/runtime/Identifier.h
Source/JavaScriptCore/runtime/JSStringBuilder.h
Source/WTF/ChangeLog
Source/WTF/wtf/text/StringImpl.cpp
Source/WTF/wtf/text/WTFString.h
Source/WebCore/ChangeLog
Source/WebCore/bindings/objc/WebScriptObject.mm
Source/WebCore/editing/CompositeEditCommand.cpp
Source/WebCore/editing/TypingCommand.cpp
Source/WebCore/editing/VisibleUnits.cpp
Source/WebCore/html/parser/HTMLParserIdioms.cpp
Source/WebCore/inspector/ContentSearchUtils.cpp
Source/WebCore/inspector/InspectorStyleSheet.cpp
Source/WebCore/inspector/InspectorStyleTextEditor.cpp
Source/WebCore/platform/Length.cpp
Source/WebCore/platform/LinkHash.cpp
Source/WebCore/platform/graphics/Color.cpp
Source/WebCore/platform/graphics/TextRun.h
Source/WebCore/platform/text/TextEncodingRegistry.cpp
Source/WebCore/rendering/RenderBlock.cpp
Source/WebCore/rendering/RenderCombineText.cpp
Source/WebCore/svg/SVGFontElement.cpp
Source/WebCore/xml/XPathFunctions.cpp
Source/WebCore/xml/XPathNodeSet.h