MCPcopy Create free account
hub / github.com/sergi/go-diff / runeToInt

Function runeToInt

diffmatchpatch/stringutil.go:161–190  ·  view source on GitHub ↗

Converts a rune generated by intToRune back to an integer

(r rune)

Source from the content-addressed store, hash-verified

159
160// Converts a rune generated by intToRune back to an integer
161func runeToInt(r rune) uint32 {
162 i := uint32(r)
163 if i < (1 << ONE_BYTE_BITS) {
164 return i
165 }
166
167 bytes := []byte{0, 0, 0, 0}
168
169 size := utf8.EncodeRune(bytes, r)
170
171 if size == 2 {
172 return uint32(bytes[0]&0b11111)<<6 | uint32(bytes[1]&0b111111)
173 }
174
175 if size == 3 {
176 result := uint32(bytes[0]&0b1111)<<12 | uint32(bytes[1]&0b111111)<<6 | uint32(bytes[2]&0b111111)
177 if result >= UNICODE_INVALID_RANGE_END {
178 return result - UNICODE_INVALID_RANGE_DELTA
179 }
180
181 return result
182 }
183
184 if size == 4 {
185 result := uint32(bytes[0]&0b111)<<18 | uint32(bytes[1]&0b111111)<<12 | uint32(bytes[2]&0b111111)<<6 | uint32(bytes[3]&0b111111)
186 return result - UNICODE_INVALID_RANGE_DELTA - 3
187 }
188
189 panic(fmt.Sprintf("Unexpected state decoding rune=%v size=%d", r, size))
190}

Callers 2

TestRuneToIntFunction · 0.85
DiffCharsToLinesMethod · 0.85

Calls

no outgoing calls

Tested by 1

TestRuneToIntFunction · 0.68