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

Side by Side Diff: src/objects.cc

Issue 2351643002: [builtins] Move StringIndexOf to a builtin. (Closed)
Patch Set: Fix signed vs unsigned comparison Created 4 years, 2 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
« no previous file with comments | « src/objects.h ('k') | src/runtime/runtime-strings.cc » ('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 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 11581 matching lines...) Expand 10 before | Expand all | Expand 10 after
11592 } 11592 }
11593 } 11593 }
11594 if (r < 0) { 11594 if (r < 0) {
11595 result = ComparisonResult::kLessThan; 11595 result = ComparisonResult::kLessThan;
11596 } else if (r > 0) { 11596 } else if (r > 0) {
11597 result = ComparisonResult::kGreaterThan; 11597 result = ComparisonResult::kGreaterThan;
11598 } 11598 }
11599 return result; 11599 return result;
11600 } 11600 }
11601 11601
11602 int String::IndexOf(Isolate* isolate, Handle<String> sub, Handle<String> pat, 11602 Object* String::IndexOf(Isolate* isolate, Handle<Object> receiver,
11603 int start_index) { 11603 Handle<Object> pattern, Handle<Object> position) {
11604 DCHECK(0 <= start_index); 11604 if (receiver->IsNull(isolate) || receiver->IsUndefined(isolate)) {
11605 DCHECK(start_index <= sub->length()); 11605 THROW_NEW_ERROR_RETURN_FAILURE(
11606 isolate, NewTypeError(MessageTemplate::kCalledOnNullOrUndefined,
11607 isolate->factory()->NewStringFromAsciiChecked(
11608 "String.prototype.lastIndexOf")));
11609 }
11610 Handle<String> sub;
11611 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, sub,
11612 Object::ToString(isolate, receiver));
11613 Handle<String> pat;
11614 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, pat,
11615 Object::ToString(isolate, pattern));
11616 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, position,
11617 Object::ToInteger(isolate, position));
11606 11618
11607 int pattern_length = pat->length(); 11619 double position_number = std::max(position->Number(), 0.0);
11608 if (pattern_length == 0) return start_index; 11620 position_number =
11621 std::min(position_number, static_cast<double>(sub->length()));
11622 uint32_t start_index = static_cast<uint32_t>(position_number);
11609 11623
11610 int subject_length = sub->length(); 11624 uint32_t pattern_length = pat->length();
11611 if (start_index + pattern_length > subject_length) return -1; 11625 if (pattern_length == 0) return Smi::FromInt(start_index);
11626
11627 uint32_t subject_length = sub->length();
11628 if (start_index + pattern_length > subject_length) return Smi::FromInt(-1);
11612 11629
11613 sub = String::Flatten(sub); 11630 sub = String::Flatten(sub);
11614 pat = String::Flatten(pat); 11631 pat = String::Flatten(pat);
11615 11632
11633 int first_index = -1;
11616 DisallowHeapAllocation no_gc; // ensure vectors stay valid 11634 DisallowHeapAllocation no_gc; // ensure vectors stay valid
11617 // Extract flattened substrings of cons strings before getting encoding. 11635 // Extract flattened substrings of cons strings before getting encoding.
11618 String::FlatContent seq_sub = sub->GetFlatContent(); 11636 String::FlatContent seq_sub = sub->GetFlatContent();
11619 String::FlatContent seq_pat = pat->GetFlatContent(); 11637 String::FlatContent seq_pat = pat->GetFlatContent();
11620 11638
11621 // dispatch on type of strings 11639 // dispatch on type of strings
11622 if (seq_pat.IsOneByte()) { 11640 if (seq_pat.IsOneByte()) {
11623 Vector<const uint8_t> pat_vector = seq_pat.ToOneByteVector(); 11641 Vector<const uint8_t> pat_vector = seq_pat.ToOneByteVector();
11624 if (seq_sub.IsOneByte()) { 11642 if (seq_sub.IsOneByte()) {
11625 return SearchString(isolate, seq_sub.ToOneByteVector(), pat_vector, 11643 first_index = SearchString(isolate, seq_sub.ToOneByteVector(), pat_vector,
11626 start_index); 11644 start_index);
11645 } else {
11646 first_index = SearchString(isolate, seq_sub.ToUC16Vector(), pat_vector,
11647 start_index);
11627 } 11648 }
11628 return SearchString(isolate, seq_sub.ToUC16Vector(), pat_vector, 11649 } else {
11629 start_index); 11650 Vector<const uc16> pat_vector = seq_pat.ToUC16Vector();
11651 if (seq_sub.IsOneByte()) {
11652 first_index = SearchString(isolate, seq_sub.ToOneByteVector(), pat_vector,
11653 start_index);
11654 } else {
11655 first_index = SearchString(isolate, seq_sub.ToUC16Vector(), pat_vector,
11656 start_index);
11657 }
11630 } 11658 }
11631 Vector<const uc16> pat_vector = seq_pat.ToUC16Vector(); 11659 return Smi::FromInt(first_index);
11632 if (seq_sub.IsOneByte()) {
11633 return SearchString(isolate, seq_sub.ToOneByteVector(), pat_vector,
11634 start_index);
11635 }
11636 return SearchString(isolate, seq_sub.ToUC16Vector(), pat_vector, start_index);
11637 } 11660 }
11638 11661
11639 namespace { // for String.Prototype.lastIndexOf 11662 namespace { // for String.Prototype.lastIndexOf
11640 11663
11641 template <typename schar, typename pchar> 11664 template <typename schar, typename pchar>
11642 int StringMatchBackwards(Vector<const schar> subject, 11665 int StringMatchBackwards(Vector<const schar> subject,
11643 Vector<const pchar> pattern, int idx) { 11666 Vector<const pchar> pattern, int idx) {
11644 int pattern_length = pattern.length(); 11667 int pattern_length = pattern.length();
11645 DCHECK(pattern_length >= 1); 11668 DCHECK(pattern_length >= 1);
11646 DCHECK(idx + pattern_length <= subject.length()); 11669 DCHECK(idx + pattern_length <= subject.length());
(...skipping 7930 matching lines...) Expand 10 before | Expand all | Expand 10 after
19577 for (PrototypeIterator iter(isolate, this, kStartAtReceiver, 19600 for (PrototypeIterator iter(isolate, this, kStartAtReceiver,
19578 PrototypeIterator::END_AT_NULL); 19601 PrototypeIterator::END_AT_NULL);
19579 !iter.IsAtEnd(); iter.AdvanceIgnoringProxies()) { 19602 !iter.IsAtEnd(); iter.AdvanceIgnoringProxies()) {
19580 if (iter.GetCurrent<Object>()->IsJSProxy()) return true; 19603 if (iter.GetCurrent<Object>()->IsJSProxy()) return true;
19581 } 19604 }
19582 return false; 19605 return false;
19583 } 19606 }
19584 19607
19585 } // namespace internal 19608 } // namespace internal
19586 } // namespace v8 19609 } // namespace v8
OLDNEW
« no previous file with comments | « src/objects.h ('k') | src/runtime/runtime-strings.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698