Skip to content

Commit

Permalink
fix StringIndexOf
Browse files Browse the repository at this point in the history
  • Loading branch information
romainmenke authored and JakeChampion committed Dec 12, 2020
1 parent eb63a67 commit e4affbd
Showing 1 changed file with 32 additions and 37 deletions.
69 changes: 32 additions & 37 deletions polyfills/_ESAbstract/StringIndexOf/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
/* global */
(function () {

// 6.1.4.1 StringIndexOf ( string, searchValue, fromIndex )
return function StringIndexOf(string, searchValue, fromIndex) {
// eslint-disable-line no-unused-vars
// 1. Assert: Type(string) is String.
// 2. Assert: Type(searchValue) is String.
// 3. Assert: ! IsNonNegativeInteger(fromIndex) is true.
// 4. Let len be the length of string.
var len = string.length;
// 5. If searchValue is the empty String and fromIndex ≤ len, return fromIndex.
if (searchValue === "" && fromIndex <= len) {
return fromIndex;
}
// 6. Let searchLen be the length of searchValue.
var searchLen = searchValue.length;
// 7. If there exists any integer k such that fromIndex ≤ k ≤ len - searchLen
// and for all nonnegative integers j less than searchLen, the code unit at
// index k + j within string is the same as the code unit at index j within searchValue,
// let pos be the smallest (closest to -∞) such integer. Otherwise, let pos be -1.
var k = fromIndex;
var pos = -1;
while (k + searchLen <= len) {
var match = true;
for (var j = 0; j < searchLen; j += 1) {
if (string[j] !== searchValue[k + j]) {
match = false;
break;
}
}
if (match) {
pos = k;
function StringIndexOf(string, searchValue, fromIndex) { // eslint-disable-line no-unused-vars
// 1. Assert: Type(string) is String.
// 2. Assert: Type(searchValue) is String.
// 3. Assert: ! IsNonNegativeInteger(fromIndex) is true.
// 4. Let len be the length of string.
var len = string.length;
// 5. If searchValue is the empty String and fromIndex ≤ len, return fromIndex.
if (searchValue === "" && fromIndex <= len) {
return fromIndex;
}
// 6. Let searchLen be the length of searchValue.
var searchLen = searchValue.length;
// 7. If there exists any integer k such that fromIndex ≤ k ≤ len - searchLen
// and for all nonnegative integers j less than searchLen, the code unit at
// index k + j within string is the same as the code unit at index j within searchValue,
// let pos be the smallest (closest to -∞) such integer. Otherwise, let pos be -1.
var k = fromIndex;
var pos = -1;
while (k + searchLen <= len) {
var match = true;
for (var j = 0; j < searchLen; j += 1) {
if (string[j] !== searchValue[k + j]) {
match = false;
break;
}
k += 1;
}
// 8. Return pos.
return pos;
};
})();
if (match) {
pos = k;
break;
}
k += 1;
}
// 8. Return pos.
return pos;
}

0 comments on commit e4affbd

Please sign in to comment.