runeBoundary returns the largest index <= n that doesn't split a UTF-8 rune, so truncating at it never yields an invalid encoding.
(s string, n int)
| 93 | // runeBoundary returns the largest index <= n that doesn't split a UTF-8 rune, |
| 94 | // so truncating at it never yields an invalid encoding. |
| 95 | func runeBoundary(s string, n int) int { |
| 96 | for n > 0 && !utf8RuneStart(s[n]) { |
| 97 | n-- |
| 98 | } |
| 99 | return n |
| 100 | } |
| 101 | |
| 102 | func utf8RuneStart(b byte) bool { return b&0xC0 != 0x80 } |
| 103 |
no test coverage detected