node:buffer: default lastIndexOf(value, encoding) to search from the end (#31507)
`Buffer.prototype.lastIndexOf(value, encoding)` — the documented 2-arg overload where `byteOffset` is omitted and the second positional argument is the encoding — searched from offset `0` instead of from the end, so it returned `-1` for substrings that are actually present (or the wrong index). Broken for every encoding passed as the 2nd arg (utf8/latin1/hex/utf16le/base64). `JSBuffer.cpp` hardcoded `byteOffsetD = 0` in the `byteOffsetValue.isString()` branch, which defeated the direction-aware default a few lines below (`if (isnan(byteOffsetD)) byteOffsetD = dir ? 0 : byteLength`). Setting it to `NaN` lets that default apply: `lastIndexOf` now searches backward from `byteLength` (matching Node), while forward `indexOf`/`includes` (which default to 0 anyway) are unchanged, as is the 3-arg `lastIndexOf(value, byteOffset, encoding)` form. Adds tests covering `lastIndexOf(value, encoding)` across utf8/latin1/hex/utf16le/base64. (This matches Node — Bun 1.3.14 had the same bug, so it's not a port regression.) --------- Co-authored-by: robobun <117481402+robobun@users.noreply.github.com>
A
Alistair Smith committed
843549b819756ce4d6e57560c1d97873a7db76ec
Parent: a740d91
Committed by GitHub <noreply@github.com>
on 5/28/2026, 9:34:17 PM