ecma262 icon indicating copy to clipboard operation
ecma262 copied to clipboard

Editorial: Revert unintentional normative change for String.prototype.substr

Open anba opened this issue 3 years ago • 0 comments

"a".substr(0, Infinity) should return "a", but #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.

anba avatar Aug 04 '22 07:08 anba