Changeset 92540 in webkit
- Timestamp:
- Aug 5, 2011 7:17:55 PM (13 years ago)
- Location:
- trunk/Source/JavaScriptCore
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/ChangeLog
r92536 r92540 1 2011-08-05 Gavin Barraclough <barraclough@apple.com> 2 3 String replace with the empty string means string removal 4 https://bugs.webkit.org/show_bug.cgi?id=65799 5 6 Reviewed by Sam Weinig. 7 8 Optimization for String.prototype.replace([RegExp], ""), this improves v8-regexp by ~3%. 9 10 * runtime/StringPrototype.cpp: 11 (JSC::jsSpliceSubstrings): 12 (JSC::stringProtoFuncReplace): 13 1 14 2011-08-05 Noel Gordon <noel.gordon@gmail.com> 2 15 -
trunk/Source/JavaScriptCore/runtime/StringPrototype.cpp
r91284 r92540 249 249 }; 250 250 251 static ALWAYS_INLINE JSValue jsSpliceSubstrings(ExecState* exec, JSString* sourceVal, const UString& source, const StringRange* substringRanges, int rangeCount) 252 { 253 if (rangeCount == 1) { 254 int sourceSize = source.length(); 255 int position = substringRanges[0].position; 256 int length = substringRanges[0].length; 257 if (position <= 0 && length >= sourceSize) 258 return sourceVal; 259 // We could call UString::substr, but this would result in redundant checks 260 return jsString(exec, StringImpl::create(source.impl(), max(0, position), min(sourceSize, length))); 261 } 262 263 int totalLength = 0; 264 for (int i = 0; i < rangeCount; i++) 265 totalLength += substringRanges[i].length; 266 267 if (!totalLength) 268 return jsString(exec, ""); 269 270 UChar* buffer; 271 RefPtr<StringImpl> impl = StringImpl::tryCreateUninitialized(totalLength, buffer); 272 if (!impl) 273 return throwOutOfMemoryError(exec); 274 275 int bufferPos = 0; 276 for (int i = 0; i < rangeCount; i++) { 277 if (int srcLen = substringRanges[i].length) { 278 StringImpl::copyChars(buffer + bufferPos, source.characters() + substringRanges[i].position, srcLen); 279 bufferPos += srcLen; 280 } 281 } 282 283 return jsString(exec, impl.release()); 284 } 285 251 286 static ALWAYS_INLINE JSValue jsSpliceSubstringsWithSeparators(ExecState* exec, JSString* sourceVal, const UString& source, const StringRange* substringRanges, int rangeCount, const UString* separators, int separatorCount) 252 287 { … … 267 302 totalLength += separators[i].length(); 268 303 269 if ( totalLength == 0)304 if (!totalLength) 270 305 return jsString(exec, ""); 271 306 272 307 UChar* buffer; 273 PassRefPtr<StringImpl> impl = StringImpl::tryCreateUninitialized(totalLength, buffer);308 RefPtr<StringImpl> impl = StringImpl::tryCreateUninitialized(totalLength, buffer); 274 309 if (!impl) 275 310 return throwOutOfMemoryError(exec); … … 292 327 } 293 328 294 return jsString(exec, impl );329 return jsString(exec, impl.release()); 295 330 } 296 331 … … 320 355 321 356 RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor(); 357 358 // Optimization for substring removal (replace with empty). 359 if (global && callType == CallTypeNone && !replacementString.length()) { 360 int lastIndex = 0; 361 unsigned startPosition = 0; 362 363 Vector<StringRange, 16> sourceRanges; 364 365 while (true) { 366 int matchIndex; 367 int matchLen = 0; 368 int* ovector; 369 regExpConstructor->performMatch(*globalData, reg, source, startPosition, matchIndex, matchLen, &ovector); 370 if (matchIndex < 0) 371 break; 372 373 if (lastIndex < matchIndex) 374 sourceRanges.append(StringRange(lastIndex, matchIndex - lastIndex)); 375 376 lastIndex = matchIndex + matchLen; 377 startPosition = lastIndex; 378 379 // special case of empty match 380 if (!matchLen) { 381 startPosition++; 382 if (startPosition > sourceLen) 383 break; 384 } 385 } 386 387 if (!lastIndex) 388 return JSValue::encode(sourceVal); 389 390 if (static_cast<unsigned>(lastIndex) < sourceLen) 391 sourceRanges.append(StringRange(lastIndex, sourceLen - lastIndex)); 392 393 return JSValue::encode(jsSpliceSubstrings(exec, sourceVal, source, sourceRanges.data(), sourceRanges.size())); 394 } 322 395 323 396 int lastIndex = 0; … … 373 446 374 447 // special case of empty match 375 if ( matchLen == 0) {448 if (!matchLen) { 376 449 startPosition++; 377 450 if (startPosition > sourceLen) … … 426 499 427 500 // special case of empty match 428 if ( matchLen == 0) {501 if (!matchLen) { 429 502 startPosition++; 430 503 if (startPosition > sourceLen)
Note: See TracChangeset
for help on using the changeset viewer.