Ignore:
Timestamp:
Feb 8, 2010, 11:40:57 PM (15 years ago)
Author:
[email protected]
Message:

Make String.replace throw an exception on out-of-memory, rather than
returning a null (err, empty-ish) string. Move String::replaceRange
and String::spliceSubstringsWithSeparators out to StringPrototype -
these were fairly specific use anyway, and we can better integrate
throwing the JS expcetion this way.

Reviewed by Oliver Hunt

Also removes redundant assignment operator from UString.

(JSC::StringRange::StringRange):
(JSC::jsSpliceSubstringsWithSeparators):
(JSC::jsReplaceRange):
(JSC::stringProtoFuncReplace):

  • runtime/UString.cpp:
  • runtime/UString.h:
File:
1 edited

Legend:

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

    r54464 r54531  
    312312}
    313313
    314 UString UString::spliceSubstringsWithSeparators(const Range* substringRanges, int rangeCount, const UString* separators, int separatorCount) const
    315 {
    316     m_rep->checkConsistency();
    317 
    318     if (rangeCount == 1 && separatorCount == 0) {
    319         int thisSize = size();
    320         int position = substringRanges[0].position;
    321         int length = substringRanges[0].length;
    322         if (position <= 0 && length >= thisSize)
    323             return *this;
    324         return UString::Rep::create(m_rep, max(0, position), min(thisSize, length));
    325     }
    326 
    327     int totalLength = 0;
    328     for (int i = 0; i < rangeCount; i++)
    329         totalLength += substringRanges[i].length;
    330     for (int i = 0; i < separatorCount; i++)
    331         totalLength += separators[i].size();
    332 
    333     if (totalLength == 0)
    334         return "";
    335 
    336     UChar* buffer;
    337     PassRefPtr<Rep> rep = Rep::tryCreateUninitialized(totalLength, buffer);
    338     if (!rep)
    339         return null();
    340 
    341     int maxCount = max(rangeCount, separatorCount);
    342     int bufferPos = 0;
    343     for (int i = 0; i < maxCount; i++) {
    344         if (i < rangeCount) {
    345             UStringImpl::copyChars(buffer + bufferPos, data() + substringRanges[i].position, substringRanges[i].length);
    346             bufferPos += substringRanges[i].length;
    347         }
    348         if (i < separatorCount) {
    349             UStringImpl::copyChars(buffer + bufferPos, separators[i].data(), separators[i].size());
    350             bufferPos += separators[i].size();
    351         }
    352     }
    353 
    354     return rep;
    355 }
    356 
    357 UString UString::replaceRange(int rangeStart, int rangeLength, const UString& replacement) const
    358 {
    359     m_rep->checkConsistency();
    360 
    361     int replacementLength = replacement.size();
    362     int totalLength = size() - rangeLength + replacementLength;
    363     if (totalLength == 0)
    364         return "";
    365 
    366     UChar* buffer;
    367     PassRefPtr<Rep> rep = Rep::tryCreateUninitialized(totalLength, buffer);
    368     if (!rep)
    369         return null();
    370 
    371     UStringImpl::copyChars(buffer, data(), rangeStart);
    372     UStringImpl::copyChars(buffer + rangeStart, replacement.data(), replacementLength);
    373     int rangeEnd = rangeStart + rangeLength;
    374     UStringImpl::copyChars(buffer + rangeStart + replacementLength, data() + rangeEnd, size() - rangeEnd);
    375 
    376     return rep;
    377 }
    378 
    379314bool UString::getCString(CStringBuffer& buffer) const
    380315{
     
    420355
    421356    return asciiBuffer;
    422 }
    423 
    424 UString& UString::operator=(const char* c)
    425 {
    426     if (!c) {
    427         m_rep = s_nullRep;
    428         return *this;
    429     }
    430 
    431     if (!c[0]) {
    432         m_rep = &Rep::empty();
    433         return *this;
    434     }
    435 
    436     int l = static_cast<int>(strlen(c));
    437     UChar* d = 0;
    438     m_rep = Rep::tryCreateUninitialized(l, d);
    439     if (m_rep) {
    440         for (int i = 0; i < l; i++)
    441             d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend
    442     } else
    443         m_rep = s_nullRep;;
    444 
    445     return *this;
    446357}
    447358
Note: See TracChangeset for help on using the changeset viewer.