-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb63a67
commit e4affbd
Showing
1 changed file
with
32 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |