Use SIMD to replace C0 control codes in Go code

This commit is contained in:
Kovid Goyal
2025-07-21 08:54:22 +05:30
parent 12c1b0cbdf
commit fd5876b94e
10 changed files with 74 additions and 47 deletions

View File

@@ -3,9 +3,9 @@ package highlight
import (
"errors"
"fmt"
"strings"
"github.com/alecthomas/chroma/v2"
"github.com/kovidgoyal/kitty/tools/utils"
)
var _ = fmt.Print
@@ -19,29 +19,6 @@ type StyleResolveData interface {
TextForPath(string) (string, error)
}
type SanitizeControlCodes struct {
r *strings.Replacer
}
func (s SanitizeControlCodes) Sanitize(x string) string { return s.r.Replace(x) }
func NewSanitizeControlCodes(replace_tab_by string) *SanitizeControlCodes {
repls := make([]string, 0, 2*(0x1f+2+(0x9f-0x80+1)))
for i := range 0x1f + 1 {
var repl string
switch i {
case '\n', ' ':
repl = string(rune(i))
case '\t':
repl = replace_tab_by
default:
repl = string(rune(0x2400 + i))
}
repls = append(repls, string(rune(i)), repl)
}
return &SanitizeControlCodes{r: strings.NewReplacer(repls...)}
}
type Highlighter interface {
HighlightFile(path string, srd StyleResolveData) (highlighted_string string, err error)
Sanitize(string) string
@@ -49,8 +26,7 @@ type Highlighter interface {
func NewHighlighter(sanitize func(string) string) Highlighter {
if sanitize == nil {
s := NewSanitizeControlCodes(" ")
sanitize = s.Sanitize
sanitize = func(text string) string { return utils.ReplaceControlCodes(text, " ", "\n") }
}
return &highlighter{sanitize: sanitize, tokens_map: make(map[string][]chroma.Token)}
}