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

Unified Diff: src/string.js

Issue 20928002: Implemented String.prototype.endsWith as per ES6 draft 07-15-13, section 15.5.3.23 (Closed) Base URL: git://github.com/v8/v8.git@master
Patch Set: Created 7 years, 5 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 | « no previous file | test/mjsunit/string-endswith.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/string.js
diff --git a/src/string.js b/src/string.js
index cb82c166346fc7ba5491a1e9e5d28e0e9b5a5108..8a249e36ec2f70c285c96928cd7798681e14c35b 100644
--- a/src/string.js
+++ b/src/string.js
@@ -890,6 +890,39 @@ function HtmlEscape(str) {
.replace(/'/g, "'");
}
+// ES6 draft 07-15-13, section 15.5.3.23
+function StringEndsWith(searchString /* position */) { // length == 1
Michael Starzinger 2013/07/29 13:16:27 nit: Two white-spaces before comment at end of lin
+ if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
+ throw MakeTypeError("called_on_null_or_undefined",
+ ["String.prototype.endsWith"]);
+ }
+
+ var s = TO_STRING_INLINE(this);
+ var s_len = s.length;
+ var ss = TO_STRING_INLINE(searchString);
+ var ss_len = ss.length;
+
+ if (ss_len > 0) {
+ var pos = s_len;
+ if (%_ArgumentsLength() > 1) {
Michael Starzinger 2013/07/29 13:16:27 There is a missing check as to whether "position"
+ var arg = ToNumber(%_Arguments(1)); // position
+ if (!NUMBER_IS_NAN(arg)) {
Michael Starzinger 2013/07/29 13:16:27 In case ToNumber(position) yields NaN, then "pos"
+ pos = TO_INTEGER(arg);
+ }
+ }
+
+ var end = $Math.min($Math.max(pos, 0), s_len);
Michael Starzinger 2013/07/29 13:16:27 This calls potentially monkey-patched versions of
+ var start = end - ss_len;
+ if (start < 0) {
+ return false;
+ }
+
+ return %StringLastIndexOf(s, ss, start) === end - ss_len;
+ }
+
+ return true;
+}
+
// Compatibility support for KJS.
// Tested by mozilla/js/tests/js1_5/Regress/regress-276103.js.
@@ -1010,7 +1043,8 @@ function SetUpString() {
"small", StringSmall,
"strike", StringStrike,
"sub", StringSub,
- "sup", StringSup
+ "sup", StringSup,
+ "endsWith", StringEndsWith
));
}
« no previous file with comments | « no previous file | test/mjsunit/string-endswith.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698