OLD | NEW |
---|---|
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2009 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 2348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2359 // Dispatch to different algorithms. | 2359 // Dispatch to different algorithms. |
2360 template <typename schar, typename pchar> | 2360 template <typename schar, typename pchar> |
2361 static int StringMatchStrategy(Vector<const schar> sub, | 2361 static int StringMatchStrategy(Vector<const schar> sub, |
2362 Vector<const pchar> pat, | 2362 Vector<const pchar> pat, |
2363 int start_index) { | 2363 int start_index) { |
2364 ASSERT(pat.length() > 1); | 2364 ASSERT(pat.length() > 1); |
2365 | 2365 |
2366 // We have an ASCII haystack and a non-ASCII needle. Check if there | 2366 // We have an ASCII haystack and a non-ASCII needle. Check if there |
2367 // really is a non-ASCII character in the needle and bail out if there | 2367 // really is a non-ASCII character in the needle and bail out if there |
2368 // is. | 2368 // is. |
2369 if (sizeof(pchar) > 1 && sizeof(schar) == 1) { | 2369 if (sizeof(schar) == 1 && sizeof(pchar) > 1) { |
2370 for (int i = 0; i < pat.length(); i++) { | 2370 for (int i = 0; i < pat.length(); i++) { |
2371 uc16 c = pat[i]; | 2371 uc16 c = pat[i]; |
2372 if (c > String::kMaxAsciiCharCode) { | 2372 if (c > String::kMaxAsciiCharCode) { |
2373 return -1; | 2373 return -1; |
2374 } | 2374 } |
2375 } | 2375 } |
2376 } | 2376 } |
2377 if (pat.length() < kBMMinPatternLength) { | 2377 if (pat.length() < kBMMinPatternLength) { |
2378 // We don't believe fancy searching can ever be more efficient. | 2378 // We don't believe fancy searching can ever be more efficient. |
2379 // The max shift of Boyer-Moore on a pattern of this length does | 2379 // The max shift of Boyer-Moore on a pattern of this length does |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2462 Object* index = args[2]; | 2462 Object* index = args[2]; |
2463 uint32_t start_index; | 2463 uint32_t start_index; |
2464 if (!Array::IndexFromObject(index, &start_index)) return Smi::FromInt(-1); | 2464 if (!Array::IndexFromObject(index, &start_index)) return Smi::FromInt(-1); |
2465 | 2465 |
2466 RUNTIME_ASSERT(start_index <= static_cast<uint32_t>(sub->length())); | 2466 RUNTIME_ASSERT(start_index <= static_cast<uint32_t>(sub->length())); |
2467 int position = Runtime::StringMatch(sub, pat, start_index); | 2467 int position = Runtime::StringMatch(sub, pat, start_index); |
2468 return Smi::FromInt(position); | 2468 return Smi::FromInt(position); |
2469 } | 2469 } |
2470 | 2470 |
2471 | 2471 |
2472 template <typename schar, typename pchar> | |
2473 static int StringMatchBackwards(Vector<const schar> sub, | |
2474 Vector<const pchar> pat, | |
2475 int idx) { | |
2476 ASSERT(pat.length() >= 1); | |
2477 ASSERT(idx + pat.length() <= sub.length()); | |
2478 | |
2479 if (sizeof(schar) == 1 && sizeof(pchar) > 1) { | |
2480 for (int i = 0; i < pat.length(); i++) { | |
2481 uc16 c = pat[i]; | |
2482 if (c > String::kMaxAsciiCharCode) { | |
2483 return -1; | |
2484 } | |
2485 } | |
2486 } | |
2487 | |
2488 pchar pattern_first_char = pat[0]; | |
2489 for (int i = idx; i >= 0; i--) { | |
2490 if (sub[i] != pattern_first_char) continue; | |
2491 int j = 1; | |
2492 while (j < pat.length()) { | |
2493 if (pat[j] != sub[i+j]) { | |
2494 break; | |
2495 } | |
2496 j++; | |
2497 } | |
2498 if (j == pat.length()) { | |
2499 return i; | |
2500 } | |
2501 } | |
2502 return -1; | |
2503 } | |
2504 | |
2472 static Object* Runtime_StringLastIndexOf(Arguments args) { | 2505 static Object* Runtime_StringLastIndexOf(Arguments args) { |
2473 NoHandleAllocation ha; | 2506 HandleScope scope; // create a new handle scope |
Mads Ager (chromium)
2010/02/22 11:30:04
Comment seems redundant here.
| |
2474 ASSERT(args.length() == 3); | 2507 ASSERT(args.length() == 3); |
2475 | 2508 |
2476 CONVERT_ARG_CHECKED(String, sub, 0); | 2509 CONVERT_ARG_CHECKED(String, sub, 0); |
2477 CONVERT_ARG_CHECKED(String, pat, 1); | 2510 CONVERT_ARG_CHECKED(String, pat, 1); |
2511 | |
2478 Object* index = args[2]; | 2512 Object* index = args[2]; |
2479 | |
2480 uint32_t start_index; | 2513 uint32_t start_index; |
2481 if (!Array::IndexFromObject(index, &start_index)) return Smi::FromInt(-1); | 2514 if (!Array::IndexFromObject(index, &start_index)) return Smi::FromInt(-1); |
2482 | 2515 |
2516 uint32_t pat_length = pat->length(); | |
2517 uint32_t sub_length = sub->length(); | |
2518 | |
2519 if (start_index + pat_length > sub_length) { | |
2520 start_index = sub_length - pat_length; | |
2521 } | |
2522 | |
2523 if (pat_length == 0) { | |
2524 return Smi::FromInt(start_index); | |
2525 } | |
2526 | |
2483 if (!sub->IsFlat()) { | 2527 if (!sub->IsFlat()) { |
2484 FlattenString(sub); | 2528 FlattenString(sub); |
2485 } | 2529 } |
2486 | 2530 |
2487 uint32_t pattern_length = pat->length(); | 2531 if (pat_length == 1) { |
2488 uint32_t sub_length = sub->length(); | |
2489 | |
2490 if (start_index + pattern_length > sub_length) { | |
2491 start_index = sub_length - pattern_length; | |
2492 } | |
2493 | |
2494 if (pattern_length == 1) { | |
2495 AssertNoAllocation no_heap_allocation; // ensure vectors stay valid | 2532 AssertNoAllocation no_heap_allocation; // ensure vectors stay valid |
2496 if (sub->IsAsciiRepresentation()) { | 2533 if (sub->IsAsciiRepresentation()) { |
2497 uc16 pchar = pat->Get(0); | 2534 uc16 pchar = pat->Get(0); |
2498 if (pchar > String::kMaxAsciiCharCode) { | 2535 if (pchar > String::kMaxAsciiCharCode) { |
2499 return Smi::FromInt(-1); | 2536 return Smi::FromInt(-1); |
2500 } | 2537 } |
2501 return Smi::FromInt(SingleCharLastIndexOf(sub->ToAsciiVector(), | 2538 return Smi::FromInt(SingleCharLastIndexOf(sub->ToAsciiVector(), |
2502 static_cast<char>(pat->Get(0)), | 2539 static_cast<char>(pat->Get(0)), |
2503 start_index)); | 2540 start_index)); |
2504 } else { | 2541 } else { |
2505 return Smi::FromInt(SingleCharLastIndexOf(sub->ToUC16Vector(), | 2542 return Smi::FromInt(SingleCharLastIndexOf(sub->ToUC16Vector(), |
2506 pat->Get(0), | 2543 pat->Get(0), |
2507 start_index)); | 2544 start_index)); |
2508 } | 2545 } |
2509 } | 2546 } |
2510 | 2547 |
2511 pat->TryFlattenIfNotFlat(); | 2548 if (!pat->IsFlat()) { |
2512 | 2549 FlattenString(pat); |
2513 for (int i = start_index; i >= 0; i--) { | |
2514 bool found = true; | |
2515 for (uint32_t j = 0; j < pattern_length; j++) { | |
2516 if (sub->Get(i + j) != pat->Get(j)) { | |
2517 found = false; | |
2518 break; | |
2519 } | |
2520 } | |
2521 if (found) return Smi::FromInt(i); | |
2522 } | 2550 } |
2523 | 2551 |
2524 return Smi::FromInt(-1); | 2552 AssertNoAllocation no_heap_allocation; // ensure vectors stay valid |
2553 | |
2554 int position = -1; | |
2555 | |
2556 if (pat->IsAsciiRepresentation()) { | |
2557 Vector<const char> pat_vector = pat->ToAsciiVector(); | |
2558 if (sub->IsAsciiRepresentation()) { | |
2559 position = StringMatchBackwards(sub->ToAsciiVector(), | |
2560 pat_vector, | |
2561 start_index); | |
2562 } else { | |
2563 position = StringMatchBackwards(sub->ToUC16Vector(), | |
2564 pat_vector, | |
2565 start_index); | |
2566 } | |
2567 } else { | |
2568 Vector<const uc16> pat_vector = pat->ToUC16Vector(); | |
2569 if (sub->IsAsciiRepresentation()) { | |
2570 position = StringMatchBackwards(sub->ToAsciiVector(), | |
2571 pat_vector, | |
2572 start_index); | |
2573 } else { | |
2574 position = StringMatchBackwards(sub->ToUC16Vector(), | |
2575 pat_vector, | |
2576 start_index); | |
2577 } | |
2578 } | |
2579 | |
2580 return Smi::FromInt(position); | |
2525 } | 2581 } |
2526 | 2582 |
2527 | 2583 |
2528 static Object* Runtime_StringLocaleCompare(Arguments args) { | 2584 static Object* Runtime_StringLocaleCompare(Arguments args) { |
2529 NoHandleAllocation ha; | 2585 NoHandleAllocation ha; |
2530 ASSERT(args.length() == 2); | 2586 ASSERT(args.length() == 2); |
2531 | 2587 |
2532 CONVERT_CHECKED(String, str1, args[0]); | 2588 CONVERT_CHECKED(String, str1, args[0]); |
2533 CONVERT_CHECKED(String, str2, args[1]); | 2589 CONVERT_CHECKED(String, str2, args[1]); |
2534 | 2590 |
(...skipping 5673 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
8208 } else { | 8264 } else { |
8209 // Handle last resort GC and make sure to allow future allocations | 8265 // Handle last resort GC and make sure to allow future allocations |
8210 // to grow the heap without causing GCs (if possible). | 8266 // to grow the heap without causing GCs (if possible). |
8211 Counters::gc_last_resort_from_js.Increment(); | 8267 Counters::gc_last_resort_from_js.Increment(); |
8212 Heap::CollectAllGarbage(false); | 8268 Heap::CollectAllGarbage(false); |
8213 } | 8269 } |
8214 } | 8270 } |
8215 | 8271 |
8216 | 8272 |
8217 } } // namespace v8::internal | 8273 } } // namespace v8::internal |
OLD | NEW |