Changeset 43104 in webkit for trunk/JavaScriptCore/runtime


Ignore:
Timestamp:
Apr 30, 2009, 4:36:38 PM (16 years ago)
Author:
[email protected]
Message:

2009-04-30 Maciej Stachowiak <[email protected]>

Reviewed by Gavin Barraclough.

  • Concatenate final three strings in simple replace case at one go

~0.2% SunSpider speedup

  • runtime/StringPrototype.cpp: (JSC::stringProtoFuncReplace): Use new replaceRange helper instead of taking substrings and concatenating three strings.
  • runtime/UString.cpp: (JSC::UString::replaceRange): New helper function.
  • runtime/UString.h:
Location:
trunk/JavaScriptCore/runtime
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/runtime/StringPrototype.cpp

    r43037 r43104  
    352352
    353353    int ovector[2] = { matchPos, matchPos + matchLen };
    354     return jsString(exec, source.substr(0, matchPos)
    355         + substituteBackreferences(replacementString, source, ovector, 0)
    356         + source.substr(matchPos + matchLen));
     354    return jsString(exec, source.replaceRange(matchPos, matchLen, substituteBackreferences(replacementString, source, ovector, 0)));
    357355}
    358356
  • trunk/JavaScriptCore/runtime/UString.cpp

    r43048 r43104  
    982982}
    983983
     984UString UString::replaceRange(int rangeStart, int rangeLength, const UString& replacement) const
     985{
     986    m_rep->checkConsistency();
     987
     988    int replacementLength = replacement.size();
     989    int totalLength = size() - rangeLength + replacementLength;
     990    if (totalLength == 0)
     991        return "";
     992
     993    UChar* buffer = allocChars(totalLength);
     994    if (!buffer)
     995        return null();
     996
     997    copyChars(buffer, data(), rangeStart);
     998    copyChars(buffer + rangeStart, replacement.data(), replacementLength);
     999    int rangeEnd = rangeStart + rangeLength;
     1000    copyChars(buffer + rangeStart + replacementLength, data() + rangeEnd, size() - rangeEnd);
     1001
     1002    return UString::Rep::create(buffer, totalLength);
     1003}
     1004
     1005
    9841006UString& UString::append(const UString &t)
    9851007{
  • trunk/JavaScriptCore/runtime/UString.h

    r43090 r43104  
    245245        UString spliceSubstringsWithSeparators(const Range* substringRanges, int rangeCount, const UString* separators, int separatorCount) const;
    246246
     247        UString replaceRange(int rangeStart, int RangeEnd, const UString& replacement) const;
     248
    247249        UString& append(const UString&);
    248250        UString& append(const char*);
Note: See TracChangeset for help on using the changeset viewer.