Use a branchless check for unicode range

This commit is contained in:
Kovid Goyal
2025-04-01 12:32:17 +05:30
parent 6ecd78d9db
commit d4d2ae969e
4 changed files with 35 additions and 13 deletions

View File

@@ -7,6 +7,19 @@ import (
var _ = fmt.Print
func ensure_char_in_range(value uint32) uint32 {
// Branchless: if (value > MAX_UNICODE) value = 0
diff := int64(value) - UNICODE_LIMIT
// The right shift gives all ones for negative diff and all zeros for positive diff
mask := uint32(diff >> 63)
return value & mask
}
func CharPropsFor(ch rune) CharProps {
q := ensure_char_in_range(uint32(ch))
return charprops_for(q)
}
func IteratorOverGraphemes(text string) iter.Seq[string] {
var s GraphemeSegmentationResult
start_pos := 0
@@ -39,7 +52,7 @@ func (s *GraphemeSegmentationResult) Reset() {
func (s GraphemeSegmentationResult) Step(ch CharProps) GraphemeSegmentationResult {
key := grapheme_segmentation_key(s, ch)
return GraphemeSegmentationResultFor(key)
return graphemesegmentationresult_for(key)
}
func Runewidth(code rune) int {