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

Unified Diff: src/js/string.js

Issue 2109583002: Fix order of conversions in String.prototype.substr. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Get s.length only once. Created 4 years, 6 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/substr.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/js/string.js
diff --git a/src/js/string.js b/src/js/string.js
index d2eaa3280981fab97df5a9e01d7f11e417887aa2..b3bc3933cda29b04a3253f74d6949c85419f7231 100644
--- a/src/js/string.js
+++ b/src/js/string.js
@@ -447,43 +447,19 @@ function StringSubstring(start, end) {
}
-// ES6 draft, revision 26 (2014-07-18), section B.2.3.1
-function StringSubstr(start, n) {
+// ecma262/#sec-string.prototype.substr
+function StringSubstr(start, length) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.substr");
-
var s = TO_STRING(this);
- var len;
-
- // Correct n: If not given, set to string length; if explicitly
- // set to undefined, zero, or negative, returns empty string.
- if (IS_UNDEFINED(n)) {
- len = s.length;
- } else {
- len = TO_INTEGER(n);
- if (len <= 0) return '';
- }
-
- // Correct start: If not given (or undefined), set to zero; otherwise
- // convert to integer and handle negative case.
- if (IS_UNDEFINED(start)) {
- start = 0;
- } else {
- start = TO_INTEGER(start);
- // If positive, and greater than or equal to the string length,
- // return empty string.
- if (start >= s.length) return '';
- // If negative and absolute value is larger than the string length,
- // use zero.
- if (start < 0) {
- start += s.length;
- if (start < 0) start = 0;
- }
- }
+ var size = s.length;
+ start = TO_INTEGER(start);
+ length = IS_UNDEFINED(length) ? size : TO_INTEGER(length);
- var end = start + len;
- if (end > s.length) end = s.length;
+ if (start < 0) start = MaxSimple(size + start, 0);
+ length = MinSimple(MaxSimple(length, 0), size - start);
- return %_SubString(s, start, end);
+ if (length <= 0) return '';
+ return %_SubString(s, start, start + length);
}
« no previous file with comments | « no previous file | test/mjsunit/substr.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698