Skip to content

Commit

Permalink
Editorial: Revert unintentional normative change for String.prototype…
Browse files Browse the repository at this point in the history
….substr (tc39#2844)

`"a".substr(0, Infinity)` should return `"a"`, but tc39#2007 incorrectly changed the result to be `""`.

If we change `substr`'s algorithm to be more similar to
`String.prototype.substring` and `String.prototype.slice`, we can easily
fix this issue:

1. `min(intStart, size)` guarantees that `intStart` is now in `[0, size]`.
2. Clamping `intLength` guarantees it's also in `[0, size]`.

With these two changes, `intEnd = min(intStart + intLength, size)` is
now in range `[intStart, size]`, so we no longer have to check for
`intStart ≥ intEnd`, but instead can directly perform the `substring`
operation.
  • Loading branch information
anba authored and ljharb committed Sep 21, 2022
1 parent e72a6b2 commit 262e214
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -47505,10 +47505,10 @@ <h1>String.prototype.substr ( _start_, _length_ )</h1>
1. Let _intStart_ be ? ToIntegerOrInfinity(_start_).
1. If _intStart_ is -&infin;, set _intStart_ to 0.
1. Else if _intStart_ &lt; 0, set _intStart_ to max(_size_ + _intStart_, 0).
1. Else, set _intStart_ to min(_intStart_, _size_).
1. If _length_ is *undefined*, let _intLength_ be _size_; otherwise let _intLength_ be ? ToIntegerOrInfinity(_length_).
1. If _intStart_ is +&infin;, _intLength_ &le; 0, or _intLength_ is +&infin;, return the empty String.
1. Set _intLength_ to the result of clamping _intLength_ between 0 and _size_.
1. Let _intEnd_ be min(_intStart_ + _intLength_, _size_).
1. If _intStart_ &ge; _intEnd_, return the empty String.
1. Return the substring of _S_ from _intStart_ to _intEnd_.
</emu-alg>
<emu-note>
Expand Down

0 comments on commit 262e214

Please sign in to comment.