mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-23 00:38:10 +02:00
Remove bounds checking for unicode table access in Go
This commit is contained in:
20
tools/wcswidth/char-props-data.go
generated
20
tools/wcswidth/char-props-data.go
generated
@@ -1,5 +1,7 @@
|
||||
package wcswidth
|
||||
|
||||
import "unsafe"
|
||||
|
||||
type GraphemeBreakProperty uint8
|
||||
|
||||
const (
|
||||
@@ -384,6 +386,15 @@ var charprops_t3 = [109]CharProps{
|
||||
((0 & 0b1) << 0) | ((CharProps(ICB_Extend) & 0b11) << 1) | ((CharProps(GBP_Extend) & 0b1111) << 3) | ((0 & 0b1) << 7) | ((0 & 0b1) << 8) | ((1 & 0b1) << 9) | ((0 & 0b1) << 10) | ((1 & 0b1) << 11) | ((0 & 0b1) << 12) | ((0 & 0b1) << 13) | ((CharProps(UC_Cf) & 0b11111) << 14) | ((0 & 0b1) << 19) | ((4 & 0b111) << 20), // 108
|
||||
}
|
||||
|
||||
// Array accessor function that avoids bounds checking
|
||||
func CharPropsFor(x rune) CharProps {
|
||||
x = max(0, min(x, 1114111))
|
||||
t1 := uintptr(*(*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(&charprops_t1[0])) + uintptr(x>>charprops_shift)*1)))
|
||||
t1_shifted := (t1 << charprops_shift) + (uintptr(x) & charprops_mask)
|
||||
t2 := uintptr(*(*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(&charprops_t2[0])) + t1_shifted*1)))
|
||||
return *(*CharProps)(unsafe.Pointer(uintptr(unsafe.Pointer(&charprops_t3[0])) + t2*4))
|
||||
}
|
||||
|
||||
const graphemesegmentationresult_mask = 15
|
||||
const graphemesegmentationresult_shift = 4
|
||||
|
||||
@@ -1026,6 +1037,15 @@ var graphemesegmentationresult_t3 = [630]GraphemeSegmentationResult{
|
||||
((GraphemeSegmentationResult(GBP_ZWJ) & 0b1111) << 0) | ((1 & 0b1) << 4) | ((0 & 0b1) << 5) | ((1 & 0b1) << 6) | ((1 & 0b1) << 7) | ((1 & 0b1) << 8) | ((0 & 0b1) << 9), // 629
|
||||
}
|
||||
|
||||
// Array accessor function that avoids bounds checking
|
||||
func GraphemeSegmentationResultFor(x uint16) GraphemeSegmentationResult {
|
||||
|
||||
t1 := uintptr(*(*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(&graphemesegmentationresult_t1[0])) + uintptr(x>>graphemesegmentationresult_shift)*1)))
|
||||
t1_shifted := (t1 << graphemesegmentationresult_shift) + (uintptr(x) & graphemesegmentationresult_mask)
|
||||
t2 := uintptr(*(*uint16)(unsafe.Pointer(uintptr(unsafe.Pointer(&graphemesegmentationresult_t2[0])) + t1_shifted*2)))
|
||||
return *(*GraphemeSegmentationResult)(unsafe.Pointer(uintptr(unsafe.Pointer(&graphemesegmentationresult_t3[0])) + t2*2))
|
||||
}
|
||||
|
||||
func grapheme_segmentation_key(r GraphemeSegmentationResult, ch CharProps) uint16 {
|
||||
return (r.State() << 7) | ch.GraphemeSegmentationProperty()
|
||||
}
|
||||
|
||||
@@ -7,10 +7,6 @@ import (
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
func CharPropsFor(ch rune) CharProps {
|
||||
return charprops_t3[charprops_t2[(rune(charprops_t1[ch>>charprops_shift])<<charprops_shift)+(ch&charprops_mask)]]
|
||||
}
|
||||
|
||||
func IteratorOverGraphemes(text string) iter.Seq[string] {
|
||||
var s GraphemeSegmentationResult
|
||||
start_pos := 0
|
||||
@@ -43,11 +39,7 @@ func (s *GraphemeSegmentationResult) Reset() {
|
||||
|
||||
func (s GraphemeSegmentationResult) Step(ch CharProps) GraphemeSegmentationResult {
|
||||
key := grapheme_segmentation_key(s, ch)
|
||||
t1 := uint16(graphemesegmentationresult_t1[key>>graphemesegmentationresult_shift]) << graphemesegmentationresult_shift
|
||||
t2 := graphemesegmentationresult_t2[t1+key&graphemesegmentationresult_mask]
|
||||
ans := graphemesegmentationresult_t3[t2]
|
||||
// fmt.Printf("state: %d gsp: %d -> key: %d t1: %d -> add_to_cell: %d\n", s.State(), ch.GraphemeSegmentationProperty(), key, t1, ans.Add_to_current_cell())
|
||||
return ans
|
||||
return GraphemeSegmentationResultFor(key)
|
||||
}
|
||||
|
||||
func Runewidth(code rune) int {
|
||||
|
||||
Reference in New Issue
Block a user