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

Unified Diff: src/string.js

Issue 113838: Fix for issue 351 - lastIndexOf. (Closed)
Patch Set: Added more tests, as requested. Created 11 years, 7 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/regress/regress-351.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 0bcabc943b0f5b09bcf81685bfb9b093b333263b..df1f393e0187821ea0acd361b2cfcf338cac08a3 100644
--- a/src/string.js
+++ b/src/string.js
@@ -120,20 +120,26 @@ function StringIndexOf(searchString /* position */) { // length == 1
// ECMA-262 section 15.5.4.8
function StringLastIndexOf(searchString /* position */) { // length == 1
var sub = ToString(this);
+ var subLength = sub.length;
var pat = ToString(searchString);
- var index = (%_ArgumentsLength() > 1)
- ? ToNumber(%_Arguments(1) /* position */)
- : $NaN;
- var firstIndex;
- if ($isNaN(index)) {
- firstIndex = sub.length - pat.length;
- } else {
- firstIndex = TO_INTEGER(index);
- if (firstIndex + pat.length > sub.length) {
- firstIndex = sub.length - pat.length;
+ var patLength = pat.length;
+ var index = subLength - patLength;
+ if (%_ArgumentsLength() > 1) {
+ var position = ToNumber(%_Arguments(1));
+ if (!$isNaN(position)) {
+ position = TO_INTEGER(position);
+ if (position < 0) {
+ position = 0;
+ }
+ if (position + patLength < subLength) {
+ index = position
+ }
}
}
- return %StringLastIndexOf(sub, pat, firstIndex);
+ if (index < 0) {
+ return -1;
+ }
+ return %StringLastIndexOf(sub, pat, index);
}
« no previous file with comments | « no previous file | test/mjsunit/regress/regress-351.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698