+
+// This function construsts a substring out of a rope without flattening by reusing the existing fibers.
+// This can reduce memory usage substantially. Since traversing ropes is slow the function will revert
+// back to flattening if the rope turns out to be long.
+JSString* JSString::substringFromRope(ExecState* exec, unsigned substringStart, unsigned substringLength)
+{
+ ASSERT(isRope());
+
+ JSGlobalData* globalData = &exec->globalData();
+
+ UString substringFibers[3];
+
+ unsigned fiberCount = 0;
+ unsigned substringFiberCount = 0;
+ unsigned substringEnd = substringStart + substringLength;
+ unsigned fiberEnd = 0;
+
+ RopeIterator end;
+ for (RopeIterator it(m_other.m_fibers, m_fiberCount); it != end; ++it) {
+ ++fiberCount;
+ UStringImpl* fiberString = *it;
+ unsigned fiberStart = fiberEnd;
+ fiberEnd = fiberStart + fiberString->length();
+ if (fiberEnd <= substringStart)
+ continue;
+ unsigned copyStart = std::max(substringStart, fiberStart);
+ unsigned copyEnd = std::min(substringEnd, fiberEnd);
+ if (copyStart == fiberStart && copyEnd == fiberEnd)
+ substringFibers[substringFiberCount++] = UString(fiberString);
+ else
+ substringFibers[substringFiberCount++] = UString(UStringImpl::create(fiberString, copyStart - fiberStart, copyEnd - copyStart));
+ if (fiberEnd >= substringEnd)
+ break;
+ if (fiberCount > resolveRopeForSubstringCutoff || substringFiberCount >= 3) {
+ // This turned out to be a really inefficient rope. Just flatten it.
+ resolveRope(exec);
+ return jsSubstring(&exec->globalData(), m_value, substringStart, substringLength);
+ }
+ }
+ ASSERT(substringFiberCount && substringFiberCount <= 3);
+
+ if (substringLength == 1) {
+ ASSERT(substringFiberCount == 1);
+ UChar c = substringFibers[0].data()[0];
+ if (c <= 0xFF)
+ return globalData->smallStrings.singleCharacterString(globalData, c);
+ }
+ if (substringFiberCount == 1)
+ return new (globalData) JSString(globalData, substringFibers[0]);
+ if (substringFiberCount == 2)
+ return new (globalData) JSString(globalData, substringFibers[0], substringFibers[1]);
+ return new (globalData) JSString(globalData, substringFibers[0], substringFibers[1], substringFibers[2]);
+}
+
+JSValue JSString::replaceCharacter(ExecState* exec, UChar character, const UString& replacement)
+{
+ if (!isRope()) {
+ unsigned matchPosition = m_value.find(character);
+ if (matchPosition == UString::NotFound)
+ return JSValue(this);
+ return jsString(exec, m_value.substr(0, matchPosition), replacement, m_value.substr(matchPosition + 1));
+ }
+
+ RopeIterator end;
+
+ // Count total fibers and find matching string.
+ size_t fiberCount = 0;
+ UStringImpl* matchString = 0;
+ int matchPosition = -1;
+ for (RopeIterator it(m_other.m_fibers, m_fiberCount); it != end; ++it) {
+ ++fiberCount;
+ if (matchString)
+ continue;
+
+ UStringImpl* string = *it;
+ matchPosition = string->find(character);
+ if (matchPosition == -1)
+ continue;
+ matchString = string;
+ }
+
+ if (!matchString)
+ return this;
+
+ RopeBuilder builder(replacement.size() ? fiberCount + 2 : fiberCount + 1);
+ if (UNLIKELY(builder.isOutOfMemory()))
+ return throwOutOfMemoryError(exec);
+
+ for (RopeIterator it(m_other.m_fibers, m_fiberCount); it != end; ++it) {
+ UStringImpl* string = *it;
+ if (string != matchString) {
+ builder.append(UString(string));
+ continue;
+ }
+
+ builder.append(UString(string).substr(0, matchPosition));
+ if (replacement.size())
+ builder.append(replacement);
+ builder.append(UString(string).substr(matchPosition + 1));
+ matchString = 0;
+ }
+
+ JSGlobalData* globalData = &exec->globalData();
+ return JSValue(new (globalData) JSString(globalData, builder.release()));
+}
+
+JSString* JSString::getIndexSlowCase(ExecState* exec, unsigned i)
+{
+ ASSERT(isRope());
+ resolveRope(exec);
+ // Return a safe no-value result, this should never be used, since the excetion will be thrown.
+ if (exec->exception())
+ return jsString(exec, "");
+ ASSERT(!isRope());
+ ASSERT(i < m_value.size());
+ return jsSingleCharacterSubstring(exec, m_value, i);
+}