OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/objects.h" | 5 #include "src/objects.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 #include <iomanip> | 8 #include <iomanip> |
9 #include <memory> | 9 #include <memory> |
10 #include <sstream> | 10 #include <sstream> |
(...skipping 11579 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
11590 } | 11590 } |
11591 } | 11591 } |
11592 if (r < 0) { | 11592 if (r < 0) { |
11593 result = ComparisonResult::kLessThan; | 11593 result = ComparisonResult::kLessThan; |
11594 } else if (r > 0) { | 11594 } else if (r > 0) { |
11595 result = ComparisonResult::kGreaterThan; | 11595 result = ComparisonResult::kGreaterThan; |
11596 } | 11596 } |
11597 return result; | 11597 return result; |
11598 } | 11598 } |
11599 | 11599 |
11600 int String::IndexOf(Isolate* isolate, Handle<String> sub, Handle<String> pat, | 11600 Object* String::IndexOf(Isolate* isolate, Handle<Object> receiver, |
11601 int start_index) { | 11601 Handle<Object> search, Handle<Object> position) { |
| 11602 if (receiver->IsNull(isolate) || receiver->IsUndefined(isolate)) { |
| 11603 THROW_NEW_ERROR_RETURN_FAILURE( |
| 11604 isolate, NewTypeError(MessageTemplate::kCalledOnNullOrUndefined, |
| 11605 isolate->factory()->NewStringFromAsciiChecked( |
| 11606 "String.prototype.indexOf"))); |
| 11607 } |
| 11608 Handle<String> receiver_string; |
| 11609 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, receiver_string, |
| 11610 Object::ToString(isolate, receiver)); |
| 11611 |
| 11612 Handle<String> search_string; |
| 11613 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, search_string, |
| 11614 Object::ToString(isolate, search)); |
| 11615 |
| 11616 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, position, |
| 11617 Object::ToInteger(isolate, position)); |
| 11618 |
| 11619 double index = std::max(position->Number(), 0.0); |
| 11620 index = std::min(index, static_cast<double>(receiver_string->length())); |
| 11621 |
| 11622 return Smi::FromInt(String::IndexOf(isolate, receiver_string, search_string, |
| 11623 static_cast<uint32_t>(index))); |
| 11624 } |
| 11625 |
| 11626 namespace { |
| 11627 |
| 11628 template <typename T> |
| 11629 int SearchString(Isolate* isolate, String::FlatContent receiver_content, |
| 11630 Vector<T> pat_vector, int start_index) { |
| 11631 if (receiver_content.IsOneByte()) { |
| 11632 return SearchString(isolate, receiver_content.ToOneByteVector(), pat_vector, |
| 11633 start_index); |
| 11634 } |
| 11635 return SearchString(isolate, receiver_content.ToUC16Vector(), pat_vector, |
| 11636 start_index); |
| 11637 } |
| 11638 |
| 11639 } // namespace |
| 11640 |
| 11641 int String::IndexOf(Isolate* isolate, Handle<String> receiver, |
| 11642 Handle<String> search, int start_index) { |
11602 DCHECK(0 <= start_index); | 11643 DCHECK(0 <= start_index); |
11603 DCHECK(start_index <= sub->length()); | 11644 DCHECK(start_index <= receiver->length()); |
11604 | 11645 |
11605 int pattern_length = pat->length(); | 11646 uint32_t search_length = search->length(); |
11606 if (pattern_length == 0) return start_index; | 11647 if (search_length == 0) return start_index; |
11607 | 11648 |
11608 int subject_length = sub->length(); | 11649 uint32_t receiver_length = receiver->length(); |
11609 if (start_index + pattern_length > subject_length) return -1; | 11650 if (start_index + search_length > receiver_length) return -1; |
11610 | 11651 |
11611 sub = String::Flatten(sub); | 11652 receiver = String::Flatten(receiver); |
11612 pat = String::Flatten(pat); | 11653 search = String::Flatten(search); |
11613 | 11654 |
11614 DisallowHeapAllocation no_gc; // ensure vectors stay valid | 11655 DisallowHeapAllocation no_gc; // ensure vectors stay valid |
11615 // Extract flattened substrings of cons strings before getting encoding. | 11656 // Extract flattened substrings of cons strings before getting encoding. |
11616 String::FlatContent seq_sub = sub->GetFlatContent(); | 11657 String::FlatContent receiver_content = receiver->GetFlatContent(); |
11617 String::FlatContent seq_pat = pat->GetFlatContent(); | 11658 String::FlatContent search_content = search->GetFlatContent(); |
11618 | 11659 |
11619 // dispatch on type of strings | 11660 // dispatch on type of strings |
11620 if (seq_pat.IsOneByte()) { | 11661 if (search_content.IsOneByte()) { |
11621 Vector<const uint8_t> pat_vector = seq_pat.ToOneByteVector(); | 11662 Vector<const uint8_t> pat_vector = search_content.ToOneByteVector(); |
11622 if (seq_sub.IsOneByte()) { | 11663 return SearchString<const uint8_t>(isolate, receiver_content, pat_vector, |
11623 return SearchString(isolate, seq_sub.ToOneByteVector(), pat_vector, | 11664 start_index); |
11624 start_index); | |
11625 } | |
11626 return SearchString(isolate, seq_sub.ToUC16Vector(), pat_vector, | |
11627 start_index); | |
11628 } | 11665 } |
11629 Vector<const uc16> pat_vector = seq_pat.ToUC16Vector(); | 11666 Vector<const uc16> pat_vector = search_content.ToUC16Vector(); |
11630 if (seq_sub.IsOneByte()) { | 11667 return SearchString<const uc16>(isolate, receiver_content, pat_vector, |
11631 return SearchString(isolate, seq_sub.ToOneByteVector(), pat_vector, | 11668 start_index); |
11632 start_index); | |
11633 } | |
11634 return SearchString(isolate, seq_sub.ToUC16Vector(), pat_vector, start_index); | |
11635 } | 11669 } |
11636 | 11670 |
11637 namespace { // for String.Prototype.lastIndexOf | 11671 namespace { // for String.Prototype.lastIndexOf |
11638 | 11672 |
11639 template <typename schar, typename pchar> | 11673 template <typename schar, typename pchar> |
11640 int StringMatchBackwards(Vector<const schar> subject, | 11674 int StringMatchBackwards(Vector<const schar> subject, |
11641 Vector<const pchar> pattern, int idx) { | 11675 Vector<const pchar> pattern, int idx) { |
11642 int pattern_length = pattern.length(); | 11676 int pattern_length = pattern.length(); |
11643 DCHECK(pattern_length >= 1); | 11677 DCHECK(pattern_length >= 1); |
11644 DCHECK(idx + pattern_length <= subject.length()); | 11678 DCHECK(idx + pattern_length <= subject.length()); |
(...skipping 8288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
19933 } | 19967 } |
19934 | 19968 |
19935 // Evaluation of module body. | 19969 // Evaluation of module body. |
19936 Handle<JSFunction> resume( | 19970 Handle<JSFunction> resume( |
19937 isolate->native_context()->generator_next_internal(), isolate); | 19971 isolate->native_context()->generator_next_internal(), isolate); |
19938 return Execution::Call(isolate, resume, generator, 0, nullptr); | 19972 return Execution::Call(isolate, resume, generator, 0, nullptr); |
19939 } | 19973 } |
19940 | 19974 |
19941 } // namespace internal | 19975 } // namespace internal |
19942 } // namespace v8 | 19976 } // namespace v8 |
OLD | NEW |