+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);
+}
+