Debian Codegraph @

internal/gen/trie.go

raw symbols SCIP
1 // Package trie handles the generation of compressed tries for character properties
2 package main
3
4 import (
5 "bytes"
6 "fmt"
7 "go/format"
8 "io"
9 "os"
10 "unicode"
11
12 "github.com/clipperhouse/displaywidth/internal/gen/triegen"
13 )
14
15 // GenerateTrie creates a compressed trie from Unicode data using triegen
16 func GenerateTrie(data *UnicodeData) (*triegen.Trie, error) {
17 trie := triegen.NewTrie("stringWidth")
18
19 // Insert all characters with non-default properties
20 inserted := 0
21 for r := rune(0); r <= unicode.MaxRune; r++ {
22 // Skip surrogate characters (U+D800-U+DFFF) and other invalid characters
23 if r >= 0xD800 && r <= 0xDFFF {
24 continue
25 }
26
27 // Skip characters that would create invalid UTF-8
28 if r == unicode.ReplacementChar {
29 continue
30 }
31
32 props := buildPropertyBitmap(r, data)
33
34 // Only insert characters with non-default properties
35 if props != 0 {
36 trie.Insert(r, uint64(props))
37 inserted++
38 }
39 }
40
41 fmt.Printf("Inserted %d characters with non-default properties\n", inserted)
42 return trie, nil
43 }
44
45 // WriteTrieGo generates the Go code for the trie using triegen
46 func WriteTrieGo(trie *triegen.Trie, outputPath string) error {
47 buf := &bytes.Buffer{}
48
49 // Write package header
50 fmt.Fprintf(buf, "// Code generated by internal/gen/main.go. DO NOT EDIT.\n\n")
51 fmt.Fprintf(buf, "package displaywidth\n\n")
52
53 // Write property definitions
54 writeProperties(buf)
55
56 // Generate the trie using triegen (it will use uint8/uint16/etc directly)
57 size, err := trie.Gen(buf)
58 if err != nil {
59 return fmt.Errorf("failed to generate trie: %v", err)
60 }
61
62 b := buf.Bytes()
63 typename := "stringWidthTrie"
64
65 typeDefSig := `type ` + typename + ` struct`
66 noTypeDef := `// ` + typeDefSig
67 b = bytes.ReplaceAll(b, []byte(typeDefSig), []byte(noTypeDef))
68
69 lookupSig := `(t *` + typename + `) lookup(s []byte)`
70 genericLookupSig := `lookup[T ~string | []byte](s T)`
71 b = bytes.ReplaceAll(b, []byte(lookupSig), []byte(genericLookupSig))
72
73 lookupValueSig := `(t *` + typename + `) lookupValue`
74 genericLookupValueSig := `lookupValue`
75 b = bytes.ReplaceAll(b, []byte(lookupValueSig), []byte(genericLookupValueSig))
76
77 lookupCallSig := `t.lookupValue(`
78 genericLookupCallSig := `lookupValue(`
79 b = bytes.ReplaceAll(b, []byte(lookupCallSig), []byte(genericLookupCallSig))
80
81 formatted, err := format.Source(b)
82 if err != nil {
83 return err
84 }
85
86 dst, err := os.Create(outputPath)
87 if err != nil {
88 return err
89 }
90 defer dst.Close()
91
92 _, err = dst.Write(formatted)
93 if err != nil {
94 return err
95 }
96
97 fmt.Printf("Generated trie with size %d bytes\n", size)
98 return nil
99 }
100
101 // writeProperties writes the character properties definitions to the buffer.
102 // It uses PropertyDefinitions from unicode.go as the single source of truth.
103 func writeProperties(w io.Writer) {
104 fmt.Fprintf(w, "// property is an enum representing the properties of a character\n")
105 fmt.Fprintf(w, "type property uint8\n\n")
106 fmt.Fprintf(w, "const (\n")
107
108 for i, prop := range PropertyDefinitions {
109 fmt.Fprintf(w, "// %s\n", prop.Comment)
110
111 constName := "_" + prop.Name
112 if i == 0 {
113 fmt.Fprintf(w, "%s property = iota + 1\n", constName)
114 } else {
115 fmt.Fprintf(w, "%s\n", constName)
116 }
117 }
118
119 fmt.Fprintf(w, ")\n\n")
120 }