Ignore:
Timestamp:
Aug 16, 2010, 4:31:33 PM (15 years ago)
Author:
[email protected]
Message:

Bug 44080 - String find/reverseFind methods need tidying up
These methods have a couple of problems with their interface, and implementation.

Reviewed by Sam Weinig

These methods take and int index, and return an int - however this is problematic
since on 64-bit string indices may have a full 32-bit range. This spills out into
surrounding code, which unsafely casts string indices from unsigned to int. Code
checking the result of these methods check for a mix of "== -1", "< 0", and
"== notFound". Clean this up by changing these methods to take an unsigned
starting index, and return a size_t. with a failed match indicated by notFound.
reverseFind also has a special meaning for the starting index argument, in that a
negative index is interpreted as an offset back from the end of the string. Remove
this functionality, in the (1!) case where it is used we should just calculate the
offset by subtracting from the string's length.

The implementation has a few problems too. The code is not in webkit style, in
using assorted abbreviations in variable names, and implementations of similar
find methods with differing argument types were unnecessarily inconsistent. When
find is passed const char* data the string would be handled as latin1 (zero
extended to UTF-16) for all characters but the first; this is sign extended.
Case-insensitive find is broken for unicode strings; the hashing optimization is
not unicode safe, and could result in false negatives.

Unify UString find methods to match String.

JavaScriptCore:

(JSC::escapeQuotes):

  • bytecompiler/NodesCodegen.cpp:

(JSC::substitute):

  • runtime/JSString.cpp:

(JSC::JSString::replaceCharacter):

  • runtime/RegExp.cpp:

(JSC::RegExp::RegExp):

  • runtime/RegExpKey.h:

(JSC::RegExpKey::getFlagsValue):

  • runtime/StringPrototype.cpp:

(JSC::substituteBackreferencesSlow):
(JSC::substituteBackreferences):
(JSC::stringProtoFuncReplace):
(JSC::stringProtoFuncIndexOf):
(JSC::stringProtoFuncLastIndexOf):
(JSC::stringProtoFuncSplit):

  • runtime/UString.cpp:
  • runtime/UString.h:

(JSC::UString::find):
(JSC::UString::reverseFind):

  • wtf/text/AtomicString.h:

(WTF::AtomicString::find):

  • wtf/text/StringImpl.cpp:

(WTF::StringImpl::find):
(WTF::StringImpl::findCaseInsensitive):
(WTF::StringImpl::reverseFind):
(WTF::StringImpl::reverseFindCaseInsensitive):
(WTF::StringImpl::endsWith):
(WTF::StringImpl::replace):

  • wtf/text/StringImpl.h:

(WTF::StringImpl::startsWith):

  • wtf/text/WTFString.cpp:

(WTF::String::split):

  • wtf/text/WTFString.h:

(WTF::String::find):
(WTF::String::reverseFind):
(WTF::String::findCaseInsensitive):
(WTF::String::reverseFindCaseInsensitive):
(WTF::String::contains):
(WTF::find):
(WTF::reverseFind):

WebCore:

  • css/CSSSelector.cpp:

(WebCore::CSSSelector::RareData::parseNth):

  • css/CSSStyleDeclaration.cpp:

(WebCore::CSSStyleDeclaration::setProperty):

  • css/CSSStyleSelector.cpp:

(WebCore::CSSStyleSelector::SelectorChecker::checkOneSelector):

  • dom/Document.cpp:

(WebCore::Document::getImageMap):

  • editing/CompositeEditCommand.cpp:

(WebCore::CompositeEditCommand::inputText):

  • editing/InsertTextCommand.cpp:

(WebCore::InsertTextCommand::input):

  • editing/TextIterator.cpp:

(WebCore::TextIterator::handleTextBox):

  • editing/TypingCommand.cpp:

(WebCore::TypingCommand::insertText):

  • editing/markup.cpp:

(WebCore::fillContainerFromString):
(WebCore::createFragmentFromText):

  • html/File.cpp:

(WebCore::File::Init):

  • html/HTMLAnchorElement.cpp:

(WebCore::HTMLAnchorElement::setHost):

  • html/HTMLEmbedElement.cpp:

(WebCore::HTMLEmbedElement::parseMappedAttribute):

  • html/HTMLFormControlElement.cpp:

(WebCore::HTMLTextFormControlElement::isPlaceholderEmpty):

  • html/HTMLObjectElement.cpp:

(WebCore::HTMLObjectElement::parseMappedAttribute):

  • inspector/InspectorDOMAgent.cpp:

(WebCore::InspectorDOMAgent::performSearch):

  • loader/CrossOriginPreflightResultCache.cpp:

(WebCore::parseAccessControlAllowList):

  • loader/MainResourceLoader.cpp:

(WebCore::MainResourceLoader::substituteMIMETypeFromPluginDatabase):

  • loader/appcache/ApplicationCacheStorage.cpp:

(WebCore::parseHeader):
(WebCore::parseHeaders):

  • loader/icon/IconFetcher.cpp:

(WebCore::parseIconLink):

  • page/DOMWindow.cpp:

(WebCore::DOMWindow::parseModalDialogFeatures):

  • page/SecurityOrigin.cpp:

(WebCore::SecurityOrigin::createFromDatabaseIdentifier):

  • page/UserContentURLPattern.cpp:

(WebCore::UserContentURLPattern::parse):

  • page/XSSAuditor.cpp:

(WebCore::XSSAuditor::findInRequest):

  • platform/ContentType.cpp:

(WebCore::ContentType::parameter):
(WebCore::ContentType::type):

  • platform/KURL.cpp:

(WebCore::KURL::lastPathComponent):
(WebCore::KURL::setProtocol):
(WebCore::decodeURLEscapeSequences):
(WebCore::substituteBackslashes):
(WebCore::mimeTypeFromDataURL):

  • platform/Length.cpp:

(WebCore::newCoordsArray):
(WebCore::newLengthArray):

  • platform/LinkHash.cpp:

(WebCore::findSlashDotDotSlash):
(WebCore::findSlashSlash):
(WebCore::findSlashDotSlash):
(WebCore::cleanPath):

  • platform/MIMETypeRegistry.cpp:

(WebCore::MIMETypeRegistry::getMIMETypeForPath):

  • platform/SchemeRegistry.cpp:

(WebCore::SchemeRegistry::shouldTreatURLAsLocal):

  • platform/graphics/MediaPlayer.cpp:

(WebCore::MediaPlayer::load):

  • platform/mac/DragImageMac.mm:

(WebCore::createDragImageIconForCachedImage):

  • platform/network/CredentialStorage.cpp:

(WebCore::protectionSpaceMapKeyFromURL):
(WebCore::findDefaultProtectionSpaceForURL):

  • platform/network/HTTPParsers.cpp:

(WebCore::skipWhiteSpace):
(WebCore::skipToken):
(WebCore::parseHTTPRefresh):
(WebCore::filenameFromHTTPContentDisposition):
(WebCore::findCharsetInMediaType):
(WebCore::parseXSSProtectionHeader):
(WebCore::extractReasonPhraseFromHTTPStatusLine):

  • platform/network/ResourceResponseBase.cpp:

(WebCore::ResourceResponseBase::isAttachment):
(WebCore::parseCacheHeader):

  • rendering/RenderEmbeddedObject.cpp:

(WebCore::RenderEmbeddedObject::updateWidget):

  • storage/Entry.cpp:

(WebCore::Entry::Entry):

  • svg/SVGFont.cpp:

(WebCore::isCompatibleGlyph):

  • svg/SVGURIReference.cpp:

(WebCore::SVGURIReference::getTarget):

  • svg/animation/SVGSMILElement.cpp:

(WebCore::SVGSMILElement::parseClockValue):
(WebCore::SVGSMILElement::parseCondition):

  • xml/XPathFunctions.cpp:

(WebCore::XPath::FunSubstringBefore::evaluate):
(WebCore::XPath::FunSubstringAfter::evaluate):
(WebCore::XPath::FunTranslate::evaluate):
(WebCore::XPath::FunLang::evaluate):

  • xml/XPathParser.cpp:

(WebCore::XPath::Parser::expandQName):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/runtime/UString.h

    r65344 r65468  
    105105    static UString number(double);
    106106
    107 
     107    // Find a single character or string, also with match function & latin1 forms.
     108    size_t find(UChar c, unsigned start = 0) const
     109        { return m_impl ? m_impl->find(c, start) : notFound; }
     110    size_t find(const UString& str, unsigned start = 0) const
     111        { return m_impl ? m_impl->find(str.impl(), start) : notFound; }
     112    size_t find(const char* str, unsigned start = 0) const
     113        { return m_impl ? m_impl->find(str, start) : notFound; }
     114
     115    // Find the last instance of a single character or string.
     116    size_t reverseFind(UChar c, unsigned start = UINT_MAX) const
     117        { return m_impl ? m_impl->reverseFind(c, start) : notFound; }
     118    size_t reverseFind(const UString& str, unsigned start = UINT_MAX) const
     119        { return m_impl ? m_impl->reverseFind(str.impl(), start) : notFound; }
    108120
    109121    double toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const;
     
    115127    uint32_t toStrictUInt32(bool* ok = 0) const;
    116128
    117     static const unsigned NotFound = 0xFFFFFFFFu;
    118     unsigned find(const UString& f, unsigned pos = 0) const;
    119     unsigned find(UChar, unsigned pos = 0) const;
    120     unsigned rfind(const UString& f, unsigned pos) const;
    121     unsigned rfind(UChar, unsigned pos) const;
    122 
    123     UString substr(unsigned pos = 0, unsigned len = 0xFFFFFFFF) const;
     129    UString substr(unsigned pos = 0, unsigned len = UINT_MAX) const;
    124130
    125131private:
Note: See TracChangeset for help on using the changeset viewer.