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

Unified Diff: src/builtins/builtins-string.cc

Issue 2406873002: [builtins] Move StringEndsWith to a C++ builtin. (Closed)
Patch Set: Use a FlatStringReader instead" 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/builtins/builtins.h ('k') | src/js/string.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/builtins/builtins-string.cc
diff --git a/src/builtins/builtins-string.cc b/src/builtins/builtins-string.cc
index 9fcd62336146768bf0bb62df0ebda5f3816e2025..a195eb722358f4f5cee9eb0ef27fa015cbe8b71d 100644
--- a/src/builtins/builtins-string.cc
+++ b/src/builtins/builtins-string.cc
@@ -781,6 +781,56 @@ void Builtins::Generate_StringPrototypeCharCodeAt(
assembler->Return(result);
}
+// ES6 section 21.1.3.6
+// String.prototype.endsWith ( searchString [ , endPosition ] )
+BUILTIN(StringPrototypeEndsWith) {
+ HandleScope handle_scope(isolate);
+ TO_THIS_STRING(str, "String.prototype.endsWith");
+
+ // Check if the search string is a regExp and fail if it is.
+ Handle<Object> search = args.atOrUndefined(isolate, 1);
+ Maybe<bool> is_reg_exp = Object::IsRegExp(isolate, search);
+ if (is_reg_exp.IsNothing()) {
+ DCHECK(isolate->has_pending_exception());
+ return isolate->heap()->exception();
+ }
+ if (is_reg_exp.FromJust()) {
+ THROW_NEW_ERROR_RETURN_FAILURE(
+ isolate, NewTypeError(MessageTemplate::kFirstArgumentNotRegExp,
+ isolate->factory()->NewStringFromStaticChars(
+ "String.prototype.endsWith")));
+ }
+ Handle<String> search_string;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, search_string,
+ Object::ToString(isolate, search));
+
+ Handle<Object> position = args.atOrUndefined(isolate, 2);
+ int end;
+
+ if (position->IsUndefined(isolate)) {
+ end = str->length();
+ } else {
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, position,
+ Object::ToInteger(isolate, position));
+ double index = std::max(position->Number(), 0.0);
+ index = std::min(index, static_cast<double>(str->length()));
+ end = static_cast<uint32_t>(index);
+ }
+
+ int start = end - search_string->length();
+ if (start < 0) return *isolate->factory()->false_value();
+
+ FlatStringReader str_reader(isolate, String::Flatten(str));
+ FlatStringReader search_reader(isolate, String::Flatten(search_string));
+
+ for (int i = 0; i < search_string->length(); i++) {
+ if (str_reader.Get(start + i) != search_reader.Get(i)) {
+ return *isolate->factory()->false_value();
+ }
+ }
+ return *isolate->factory()->true_value();
+}
+
// ES6 section 21.1.3.7
// String.prototype.includes ( searchString [ , position ] )
BUILTIN(StringPrototypeIncludes) {
« no previous file with comments | « src/builtins/builtins.h ('k') | src/js/string.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698