| 108 | } |
| 109 | |
| 110 | func stringByteOffsets(s string) []int { |
| 111 | var byteOffsets []int |
| 112 | runeIndex := 0 |
| 113 | for strIdx, ch := range s { |
| 114 | if byteOffsets != nil { |
| 115 | byteOffsets[runeIndex] = strIdx |
| 116 | } |
| 117 | runeLen := utf8.RuneLen(ch) |
| 118 | if ch == utf8.RuneError { |
| 119 | _, runeLen = utf8.DecodeRuneInString(s[strIdx:]) |
| 120 | } |
| 121 | if byteOffsets == nil && (strIdx != runeIndex || runeLen != 1) { |
| 122 | byteOffsets = make([]int, len(s)+1) |
| 123 | for i := 0; i < runeIndex; i++ { |
| 124 | byteOffsets[i] = i |
| 125 | } |
| 126 | byteOffsets[runeIndex] = strIdx |
| 127 | } |
| 128 | runeIndex++ |
| 129 | } |
| 130 | if byteOffsets != nil { |
| 131 | byteOffsets[runeIndex] = len(s) |
| 132 | return byteOffsets[:runeIndex+1] |
| 133 | } |
| 134 | return nil |
| 135 | } |
| 136 | |
| 137 | func runeByteOffsets(runes []rune) []int { |
| 138 | var byteOffsets []int |