Skip to content

Commit

Permalink
Editorial: Introduce StringLastIndexOf (#3290)
Browse files Browse the repository at this point in the history
  • Loading branch information
gibson042 authored and ljharb committed Mar 18, 2024
1 parent 6c78dfc commit aaef979
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,7 @@ <h1>
_string_: a String,
_searchValue_: a String,
_fromIndex_: a non-negative integer,
): an integer
): a non-negative integer or -1
</h1>
<dl class="header">
</dl>
Expand All @@ -1188,6 +1188,30 @@ <h1>
<p>This algorithm always returns -1 if _fromIndex_ + the length of _searchValue_ > the length of _string_.</p>
</emu-note>
</emu-clause>

<emu-clause id="sec-stringlastindexof" type="abstract operation">
<h1>
StringLastIndexOf (
_string_: a String,
_searchValue_: a String,
_fromIndex_: a non-negative integer,
): a non-negative integer or -1
</h1>
<dl class="header">
</dl>
<emu-alg>
1. Let _len_ be the length of _string_.
1. Let _searchLen_ be the length of _searchValue_.
1. Assert: _fromIndex_ + _searchLen_ ≤ _len_.
1. For each integer _i_ such that 0 ≤ _i_ ≤ _fromIndex_, in descending order, do
1. Let _candidate_ be the substring of _string_ from _i_ to _i_ + _searchLen_.
1. If _candidate_ is _searchValue_, return _i_.
1. Return -1.
</emu-alg>
<emu-note>
<p>If _searchValue_ is the empty String, this algorithm returns _fromIndex_. The empty String is effectively found at every position within a string, including after the last code unit.</p>
</emu-note>
</emu-clause>
</emu-clause>

<emu-clause id="sec-ecmascript-language-types-symbol-type">
Expand Down Expand Up @@ -34646,11 +34670,7 @@ <h1>String.prototype.lastIndexOf ( _searchString_ [ , _position_ ] )</h1>
1. Let _len_ be the length of _S_.
1. Let _searchLen_ be the length of _searchStr_.
1. Let _start_ be the result of clamping _pos_ between 0 and _len_ - _searchLen_.
1. If _searchStr_ is the empty String, return 𝔽(_start_).
1. For each integer _i_ such that 0 ≤ _i_ ≤ _start_, in descending order, do
1. Let _candidate_ be the substring of _S_ from _i_ to _i_ + _searchLen_.
1. If _candidate_ is _searchStr_, return 𝔽(_i_).
1. Return *-1*<sub>𝔽</sub>.
1. Return 𝔽(StringLastIndexOf(_S_, _searchStr_, _start_)).
</emu-alg>
<emu-note>
<p>This method is intentionally generic; it does not require that its *this* value be a String object. Therefore, it can be transferred to other kinds of objects for use as a method.</p>
Expand Down

0 comments on commit aaef979

Please sign in to comment.