Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: src/runtime.cc

Issue 9213002: Fast path for string.replace that replaces a single character by a string. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/runtime.h ('k') | src/string.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 3215 matching lines...) Expand 10 before | Expand all | Expand 10 after
3226 } 3226 }
3227 3227
3228 return StringReplaceRegExpWithString(isolate, 3228 return StringReplaceRegExpWithString(isolate,
3229 subject, 3229 subject,
3230 regexp, 3230 regexp,
3231 replacement, 3231 replacement,
3232 last_match_info); 3232 last_match_info);
3233 } 3233 }
3234 3234
3235 3235
3236 Handle<String> Runtime::StringReplaceOneCharWithString(Isolate* isolate,
3237 Handle<String> subject,
3238 Handle<String> search,
3239 Handle<String> replace,
3240 bool* found) {
3241 if (subject->IsConsString()) {
3242 ConsString* cons = ConsString::cast(*subject);
3243 Handle<String> first = Handle<String>(cons->first());
3244 Handle<String> second = Handle<String>(cons->second());
3245 Handle<String> new_first = StringReplaceOneCharWithString(isolate,
3246 first,
3247 search,
3248 replace,
3249 found);
3250 if (*found) {
3251 return isolate->factory()->NewConsString(new_first, second);
3252 } else {
3253 Handle<String> new_second = StringReplaceOneCharWithString(isolate,
3254 second,
3255 search,
3256 replace,
3257 found);
3258 return isolate->factory()->NewConsString(first, new_second);
3259 }
3260 } else {
3261 int index = StringMatch(isolate, subject, search, 0);
3262 if (index == -1) return subject;
3263 *found = true;
3264 Handle<String> first = isolate->factory()->NewSubString(subject, 0, index);
3265 Handle<String> cons1 = isolate->factory()->NewConsString(first, replace);
3266 Handle<String> second =
3267 isolate->factory()->NewSubString(subject, index + 1, subject->length());
3268 return isolate->factory()->NewConsString(cons1, second);
3269 }
3270 }
3271
3272
3273 RUNTIME_FUNCTION(MaybeObject*, Runtime_StringReplaceOneCharWithString) {
3274 ASSERT(args.length() == 3);
3275 HandleScope scope(isolate);
3276 CONVERT_ARG_CHECKED(String, subject, 0);
3277 CONVERT_ARG_CHECKED(String, search, 1);
3278 CONVERT_ARG_CHECKED(String, replace, 2);
3279 bool found = false;
3280
3281 return *(Runtime::StringReplaceOneCharWithString(isolate,
3282 subject,
3283 search,
3284 replace,
3285 &found));
3286 }
3287
3288
3236 // Perform string match of pattern on subject, starting at start index. 3289 // Perform string match of pattern on subject, starting at start index.
3237 // Caller must ensure that 0 <= start_index <= sub->length(), 3290 // Caller must ensure that 0 <= start_index <= sub->length(),
3238 // and should check that pat->length() + start_index <= sub->length(). 3291 // and should check that pat->length() + start_index <= sub->length().
3239 int Runtime::StringMatch(Isolate* isolate, 3292 int Runtime::StringMatch(Isolate* isolate,
3240 Handle<String> sub, 3293 Handle<String> sub,
3241 Handle<String> pat, 3294 Handle<String> pat,
3242 int start_index) { 3295 int start_index) {
3243 ASSERT(0 <= start_index); 3296 ASSERT(0 <= start_index);
3244 ASSERT(start_index <= sub->length()); 3297 ASSERT(start_index <= sub->length());
3245 3298
(...skipping 10301 matching lines...) Expand 10 before | Expand all | Expand 10 after
13547 } else { 13600 } else {
13548 // Handle last resort GC and make sure to allow future allocations 13601 // Handle last resort GC and make sure to allow future allocations
13549 // to grow the heap without causing GCs (if possible). 13602 // to grow the heap without causing GCs (if possible).
13550 isolate->counters()->gc_last_resort_from_js()->Increment(); 13603 isolate->counters()->gc_last_resort_from_js()->Increment();
13551 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags); 13604 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags);
13552 } 13605 }
13553 } 13606 }
13554 13607
13555 13608
13556 } } // namespace v8::internal 13609 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/runtime.h ('k') | src/string.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698