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

Unified Diff: src/objects.cc

Issue 2351643002: [builtins] Move StringIndexOf to a builtin. (Closed)
Patch Set: Fix signed vs unsigned comparison Created 4 years, 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/objects.h ('k') | src/runtime/runtime-strings.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/objects.cc
diff --git a/src/objects.cc b/src/objects.cc
index eb0cb4a52ebdefbf3d1b83c8f0027933efb6b3f0..319fc7ba7375e23d31ce697405b92bdf7c73bed9 100644
--- a/src/objects.cc
+++ b/src/objects.cc
@@ -11599,20 +11599,38 @@ ComparisonResult String::Compare(Handle<String> x, Handle<String> y) {
return result;
}
-int String::IndexOf(Isolate* isolate, Handle<String> sub, Handle<String> pat,
- int start_index) {
- DCHECK(0 <= start_index);
- DCHECK(start_index <= sub->length());
+Object* String::IndexOf(Isolate* isolate, Handle<Object> receiver,
+ Handle<Object> pattern, Handle<Object> position) {
+ if (receiver->IsNull(isolate) || receiver->IsUndefined(isolate)) {
+ THROW_NEW_ERROR_RETURN_FAILURE(
+ isolate, NewTypeError(MessageTemplate::kCalledOnNullOrUndefined,
+ isolate->factory()->NewStringFromAsciiChecked(
+ "String.prototype.lastIndexOf")));
+ }
+ Handle<String> sub;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, sub,
+ Object::ToString(isolate, receiver));
+ Handle<String> pat;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, pat,
+ Object::ToString(isolate, pattern));
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, position,
+ Object::ToInteger(isolate, position));
- int pattern_length = pat->length();
- if (pattern_length == 0) return start_index;
+ double position_number = std::max(position->Number(), 0.0);
+ position_number =
+ std::min(position_number, static_cast<double>(sub->length()));
+ uint32_t start_index = static_cast<uint32_t>(position_number);
- int subject_length = sub->length();
- if (start_index + pattern_length > subject_length) return -1;
+ uint32_t pattern_length = pat->length();
+ if (pattern_length == 0) return Smi::FromInt(start_index);
+
+ uint32_t subject_length = sub->length();
+ if (start_index + pattern_length > subject_length) return Smi::FromInt(-1);
sub = String::Flatten(sub);
pat = String::Flatten(pat);
+ int first_index = -1;
DisallowHeapAllocation no_gc; // ensure vectors stay valid
// Extract flattened substrings of cons strings before getting encoding.
String::FlatContent seq_sub = sub->GetFlatContent();
@@ -11622,18 +11640,23 @@ int String::IndexOf(Isolate* isolate, Handle<String> sub, Handle<String> pat,
if (seq_pat.IsOneByte()) {
Vector<const uint8_t> pat_vector = seq_pat.ToOneByteVector();
if (seq_sub.IsOneByte()) {
- return SearchString(isolate, seq_sub.ToOneByteVector(), pat_vector,
- start_index);
+ first_index = SearchString(isolate, seq_sub.ToOneByteVector(), pat_vector,
+ start_index);
+ } else {
+ first_index = SearchString(isolate, seq_sub.ToUC16Vector(), pat_vector,
+ start_index);
+ }
+ } else {
+ Vector<const uc16> pat_vector = seq_pat.ToUC16Vector();
+ if (seq_sub.IsOneByte()) {
+ first_index = SearchString(isolate, seq_sub.ToOneByteVector(), pat_vector,
+ start_index);
+ } else {
+ first_index = SearchString(isolate, seq_sub.ToUC16Vector(), pat_vector,
+ start_index);
}
- return SearchString(isolate, seq_sub.ToUC16Vector(), pat_vector,
- start_index);
- }
- Vector<const uc16> pat_vector = seq_pat.ToUC16Vector();
- if (seq_sub.IsOneByte()) {
- return SearchString(isolate, seq_sub.ToOneByteVector(), pat_vector,
- start_index);
}
- return SearchString(isolate, seq_sub.ToUC16Vector(), pat_vector, start_index);
+ return Smi::FromInt(first_index);
}
namespace { // for String.Prototype.lastIndexOf
« 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