| 388 | } |
| 389 | |
| 390 | func newStringByteMapper(s string) *stringByteMapper { |
| 391 | var mapper *stringByteMapper |
| 392 | runeIndex := 0 |
| 393 | delta := 0 |
| 394 | for strIdx, ch := range s { |
| 395 | runeLen := utf8.RuneLen(ch) |
| 396 | if ch == utf8.RuneError { |
| 397 | _, runeLen = utf8.DecodeRuneInString(s[strIdx:]) |
| 398 | } |
| 399 | if runeLen != 1 { |
| 400 | if mapper == nil { |
| 401 | mapper = &stringByteMapper{} |
| 402 | } |
| 403 | delta += runeLen - 1 |
| 404 | mapper.runeIndexes = append(mapper.runeIndexes, runeIndex+1) |
| 405 | mapper.deltas = append(mapper.deltas, delta) |
| 406 | } |
| 407 | runeIndex++ |
| 408 | } |
| 409 | return mapper |
| 410 | } |
| 411 | |
| 412 | func (m *stringByteMapper) byteIndex(runeIndex int) int { |
| 413 | i := sort.Search(len(m.runeIndexes), func(i int) bool { |