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

Side by Side Diff: src/runtime.cc

Issue 9231017: Recursion limit for one-char string replace and retire String::kMinNonFlatLength. (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') | src/string.js » ('J')
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 3219 matching lines...) Expand 10 before | Expand all | Expand 10 after
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, 3236 Handle<String> Runtime::StringReplaceOneCharWithString(Isolate* isolate,
3237 Handle<String> subject, 3237 Handle<String> subject,
3238 Handle<String> search, 3238 Handle<String> search,
3239 Handle<String> replace, 3239 Handle<String> replace,
3240 bool* found) { 3240 bool* found,
3241 int recursion_limit) {
3242 if (recursion_limit == 0) return Handle<String>::null();
3241 if (subject->IsConsString()) { 3243 if (subject->IsConsString()) {
3242 ConsString* cons = ConsString::cast(*subject); 3244 ConsString* cons = ConsString::cast(*subject);
3243 Handle<String> first = Handle<String>(cons->first()); 3245 Handle<String> first = Handle<String>(cons->first());
3244 Handle<String> second = Handle<String>(cons->second()); 3246 Handle<String> second = Handle<String>(cons->second());
3245 Handle<String> new_first = StringReplaceOneCharWithString(isolate, 3247 Handle<String> new_first =
3246 first, 3248 StringReplaceOneCharWithString(isolate,
3247 search, 3249 first,
3248 replace, 3250 search,
3249 found); 3251 replace,
3250 if (*found) { 3252 found,
3251 return isolate->factory()->NewConsString(new_first, second); 3253 recursion_limit - 1);
3252 } else { 3254 if (*found) return isolate->factory()->NewConsString(new_first, second);
3253 Handle<String> new_second = StringReplaceOneCharWithString(isolate, 3255 if (new_first.is_null()) return new_first;
3254 second, 3256
3255 search, 3257 Handle<String> new_second =
3256 replace, 3258 StringReplaceOneCharWithString(isolate,
3257 found); 3259 second,
3258 return isolate->factory()->NewConsString(first, new_second); 3260 search,
3259 } 3261 replace,
3262 found,
3263 recursion_limit - 1);
3264 if (*found) return isolate->factory()->NewConsString(first, new_second);
3265 if (new_second.is_null()) return new_second;
3266
3267 return subject;
3260 } else { 3268 } else {
3261 int index = StringMatch(isolate, subject, search, 0); 3269 int index = StringMatch(isolate, subject, search, 0);
3262 if (index == -1) return subject; 3270 if (index == -1) return subject;
3263 *found = true; 3271 *found = true;
3264 Handle<String> first = isolate->factory()->NewSubString(subject, 0, index); 3272 Handle<String> first = isolate->factory()->NewSubString(subject, 0, index);
3265 Handle<String> cons1 = isolate->factory()->NewConsString(first, replace); 3273 Handle<String> cons1 = isolate->factory()->NewConsString(first, replace);
3266 Handle<String> second = 3274 Handle<String> second =
3267 isolate->factory()->NewSubString(subject, index + 1, subject->length()); 3275 isolate->factory()->NewSubString(subject, index + 1, subject->length());
3268 return isolate->factory()->NewConsString(cons1, second); 3276 return isolate->factory()->NewConsString(cons1, second);
3269 } 3277 }
3270 } 3278 }
3271 3279
3272 3280
3273 RUNTIME_FUNCTION(MaybeObject*, Runtime_StringReplaceOneCharWithString) { 3281 RUNTIME_FUNCTION(MaybeObject*, Runtime_StringReplaceOneCharWithString) {
3274 ASSERT(args.length() == 3); 3282 ASSERT(args.length() == 3);
3275 HandleScope scope(isolate); 3283 HandleScope scope(isolate);
3276 CONVERT_ARG_CHECKED(String, subject, 0); 3284 CONVERT_ARG_CHECKED(String, subject, 0);
3277 CONVERT_ARG_CHECKED(String, search, 1); 3285 CONVERT_ARG_CHECKED(String, search, 1);
3278 CONVERT_ARG_CHECKED(String, replace, 2); 3286 CONVERT_ARG_CHECKED(String, replace, 2);
3287
3288 // If the cons string tree is too deep, we simply abort the recursion and
3289 // retry with a flattened subject string.
3290 const int kRecursionLimit = 0x1000;
Yang 2012/01/17 13:07:34 Recursion limit is chosen so that regexpdna still
3279 bool found = false; 3291 bool found = false;
3280 3292 for (int i = 0; i < 2; i++) {
Erik Corry 2012/01/17 13:50:24 I think this would be clearer without the loop, ju
Yang 2012/01/17 14:29:04 Done.
3281 return *(Runtime::StringReplaceOneCharWithString(isolate, 3293 Handle<String> result =
3282 subject, 3294 Runtime::StringReplaceOneCharWithString(isolate,
3283 search, 3295 subject,
3284 replace, 3296 search,
3285 &found)); 3297 replace,
3298 &found,
3299 kRecursionLimit);
3300 if (!result.is_null()) return *result;
Erik Corry 2012/01/17 13:51:21 Should you have a test for found == false here and
Yang 2012/01/17 14:29:04 Allocation is only ever done if found==true. In al
3301 subject = FlattenGetString(subject);
3302 }
3303 UNREACHABLE();
3304 return NULL;
3286 } 3305 }
3287 3306
3288 3307
3289 // Perform string match of pattern on subject, starting at start index. 3308 // Perform string match of pattern on subject, starting at start index.
3290 // Caller must ensure that 0 <= start_index <= sub->length(), 3309 // Caller must ensure that 0 <= start_index <= sub->length(),
3291 // and should check that pat->length() + start_index <= sub->length(). 3310 // and should check that pat->length() + start_index <= sub->length().
3292 int Runtime::StringMatch(Isolate* isolate, 3311 int Runtime::StringMatch(Isolate* isolate,
3293 Handle<String> sub, 3312 Handle<String> sub,
3294 Handle<String> pat, 3313 Handle<String> pat,
3295 int start_index) { 3314 int start_index) {
(...skipping 10309 matching lines...) Expand 10 before | Expand all | Expand 10 after
13605 } else { 13624 } else {
13606 // Handle last resort GC and make sure to allow future allocations 13625 // Handle last resort GC and make sure to allow future allocations
13607 // to grow the heap without causing GCs (if possible). 13626 // to grow the heap without causing GCs (if possible).
13608 isolate->counters()->gc_last_resort_from_js()->Increment(); 13627 isolate->counters()->gc_last_resort_from_js()->Increment();
13609 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags); 13628 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags);
13610 } 13629 }
13611 } 13630 }
13612 13631
13613 13632
13614 } } // namespace v8::internal 13633 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/runtime.h ('k') | src/string.js » ('j') | src/string.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698