The search that starts before its offset
A UTF-16 search returns a match before the byte offset where it was asked to start.
What’s going wrong
Input is an array of at most 100 {text, needle, offset} rows. Strings contain at most 40 BMP characters excluding surrogate code units; every character occupies two UTF-16LE bytes. offset is an integer in -200..200 measured in BYTES. Negative offsets count from the text byte length and clamp to zero; positive offsets clamp to the byte length. For a nonempty needle, return the first byte index at or after the normalized offset whose complete code-unit-aligned text equals needle, or -1. Search only positions aligned to the start of the text, never across code units. An empty needle returns the normalized offset exactly, even if odd. Return an array of numbers.
Expected behavior
Round an aligned search start without moving backward.
Reproduction
[{"text":"aba","needle":"a","offset":1}][4]
[{"text":"aba","needle":"a","offset":2}][4]
[{"text":"aba","needle":"a","offset":0}][0]
Before you begin
- JavaScript strings and integer arithmetic
- Byte offsets versus UTF-16 code units
Need a nudge?
Reveal one investigation hint at a time. Try each clue before opening the next.
Read the report, then sign in to start your investigation.