alignRuneStart backs up byte index idx to the nearest UTF-8 rune start byte. idx <= 0 or idx >= len(s) is returned unchanged. Motivation: Google's reference JS diff-match-patch uses string.substring (UTF-16 code unit) for all slicing — surrogate-pair-middle splits are well-defined. This Go port sli
(s string, idx int)
| 35 | // slice point but guarantees the resulting string is always valid UTF-8 and |
| 36 | // no MatchMain / DiffMain pass sees a continuation-byte-prefixed string. |
| 37 | func alignRuneStart(s string, idx int) int { |
| 38 | if idx <= 0 || idx >= len(s) { |
| 39 | return idx |
| 40 | } |
| 41 | for idx > 0 && !utf8.RuneStart(s[idx]) { |
| 42 | idx-- |
| 43 | } |
| 44 | return idx |
| 45 | } |
| 46 | |
| 47 | // alignRuneEnd moves byte index idx forward to the next UTF-8 rune start. |
| 48 | // idx <= 0 or idx >= len(s) is returned unchanged. |
no outgoing calls