|
| 1 | +// Copyright 2022 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "flag" |
| 10 | + "fmt" |
| 11 | + "go/format" |
| 12 | + "os" |
| 13 | + "sort" |
| 14 | + "text/template" |
| 15 | + "unicode" |
| 16 | + |
| 17 | + "code.gitea.io/gitea/modules/json" |
| 18 | + |
| 19 | + "golang.org/x/text/unicode/rangetable" |
| 20 | +) |
| 21 | + |
| 22 | +// ambiguous.json provides a one to one mapping of ambiguous characters to other characters |
| 23 | +// See https://github.com/hediet/vscode-unicode-data/blob/main/out/ambiguous.json |
| 24 | + |
| 25 | +type AmbiguousTable struct { |
| 26 | + Confusable []rune |
| 27 | + With []rune |
| 28 | + Locale string |
| 29 | + RangeTable *unicode.RangeTable |
| 30 | +} |
| 31 | + |
| 32 | +type RunePair struct { |
| 33 | + Confusable rune |
| 34 | + With rune |
| 35 | +} |
| 36 | + |
| 37 | +var verbose bool |
| 38 | + |
| 39 | +func main() { |
| 40 | + flag.Usage = func() { |
| 41 | + fmt.Fprintf(os.Stderr, `%s: Generate AmbiguousCharacter |
| 42 | +
|
| 43 | +Usage: %[1]s [-v] [-o output.go] ambiguous.json |
| 44 | +`, os.Args[0]) |
| 45 | + flag.PrintDefaults() |
| 46 | + } |
| 47 | + |
| 48 | + output := "" |
| 49 | + flag.BoolVar(&verbose, "v", false, "verbose output") |
| 50 | + flag.StringVar(&output, "o", "ambiguous_gen.go", "file to output to") |
| 51 | + flag.Parse() |
| 52 | + input := flag.Arg(0) |
| 53 | + if input == "" { |
| 54 | + input = "ambiguous.json" |
| 55 | + } |
| 56 | + |
| 57 | + bs, err := os.ReadFile(input) |
| 58 | + if err != nil { |
| 59 | + fatalf("Unable to read: %s Err: %v", input, err) |
| 60 | + } |
| 61 | + |
| 62 | + var unwrapped string |
| 63 | + if err := json.Unmarshal(bs, &unwrapped); err != nil { |
| 64 | + fatalf("Unable to unwrap content in: %s Err: %v", input, err) |
| 65 | + } |
| 66 | + |
| 67 | + fromJSON := map[string][]uint32{} |
| 68 | + if err := json.Unmarshal([]byte(unwrapped), &fromJSON); err != nil { |
| 69 | + fatalf("Unable to unmarshal content in: %s Err: %v", input, err) |
| 70 | + } |
| 71 | + |
| 72 | + tables := make([]*AmbiguousTable, 0, len(fromJSON)) |
| 73 | + for locale, chars := range fromJSON { |
| 74 | + table := &AmbiguousTable{Locale: locale} |
| 75 | + table.Confusable = make([]rune, 0, len(chars)/2) |
| 76 | + table.With = make([]rune, 0, len(chars)/2) |
| 77 | + pairs := make([]RunePair, len(chars)/2) |
| 78 | + for i := 0; i < len(chars); i += 2 { |
| 79 | + pairs[i/2].Confusable, pairs[i/2].With = rune(chars[i]), rune(chars[i+1]) |
| 80 | + } |
| 81 | + sort.Slice(pairs, func(i, j int) bool { |
| 82 | + return pairs[i].Confusable < pairs[j].Confusable |
| 83 | + }) |
| 84 | + for _, pair := range pairs { |
| 85 | + table.Confusable = append(table.Confusable, pair.Confusable) |
| 86 | + table.With = append(table.With, pair.With) |
| 87 | + } |
| 88 | + table.RangeTable = rangetable.New(table.Confusable...) |
| 89 | + tables = append(tables, table) |
| 90 | + } |
| 91 | + sort.Slice(tables, func(i, j int) bool { |
| 92 | + return tables[i].Locale < tables[j].Locale |
| 93 | + }) |
| 94 | + data := map[string]interface{}{ |
| 95 | + "Tables": tables, |
| 96 | + } |
| 97 | + |
| 98 | + if err := runTemplate(generatorTemplate, output, &data); err != nil { |
| 99 | + fatalf("Unable to run template: %v", err) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func runTemplate(t *template.Template, filename string, data interface{}) error { |
| 104 | + buf := bytes.NewBuffer(nil) |
| 105 | + if err := t.Execute(buf, data); err != nil { |
| 106 | + return fmt.Errorf("unable to execute template: %w", err) |
| 107 | + } |
| 108 | + bs, err := format.Source(buf.Bytes()) |
| 109 | + if err != nil { |
| 110 | + verbosef("Bad source:\n%s", buf.String()) |
| 111 | + return fmt.Errorf("unable to format source: %w", err) |
| 112 | + } |
| 113 | + file, err := os.Create(filename) |
| 114 | + if err != nil { |
| 115 | + return fmt.Errorf("failed to create file %s because %w", filename, err) |
| 116 | + } |
| 117 | + defer file.Close() |
| 118 | + _, err = file.Write(bs) |
| 119 | + if err != nil { |
| 120 | + return fmt.Errorf("unable to write generated source: %w", err) |
| 121 | + } |
| 122 | + return nil |
| 123 | +} |
| 124 | + |
| 125 | +var generatorTemplate = template.Must(template.New("ambiguousTemplate").Parse(`// This file is generated by modules/charset/ambiguous/generate.go DO NOT EDIT |
| 126 | +// Copyright 2022 The Gitea Authors. All rights reserved. |
| 127 | +// Use of this source code is governed by a MIT-style |
| 128 | +// license that can be found in the LICENSE file. |
| 129 | +
|
| 130 | +package charset |
| 131 | +
|
| 132 | +import "unicode" |
| 133 | +
|
| 134 | +// This file is generated from https://github.com/hediet/vscode-unicode-data/blob/main/out/ambiguous.json |
| 135 | +
|
| 136 | +// AmbiguousTable matches a confusable rune with its partner for the Locale |
| 137 | +type AmbiguousTable struct { |
| 138 | + Confusable []rune |
| 139 | + With []rune |
| 140 | + Locale string |
| 141 | + RangeTable *unicode.RangeTable |
| 142 | +} |
| 143 | +
|
| 144 | +// AmbiguousCharacters provides a map by locale name to the confusable characters in that locale |
| 145 | +var AmbiguousCharacters = map[string]*AmbiguousTable{ |
| 146 | + {{range .Tables}}{{printf "%q:" .Locale}} { |
| 147 | + Confusable: []rune{ {{range .Confusable}}{{.}},{{end}} }, |
| 148 | + With: []rune{ {{range .With}}{{.}},{{end}} }, |
| 149 | + Locale: {{printf "%q" .Locale}}, |
| 150 | + RangeTable: &unicode.RangeTable{ |
| 151 | + R16: []unicode.Range16{ |
| 152 | + {{range .RangeTable.R16 }} {Lo:{{.Lo}}, Hi:{{.Hi}}, Stride: {{.Stride}}}, |
| 153 | + {{end}} }, |
| 154 | + R32: []unicode.Range32{ |
| 155 | + {{range .RangeTable.R32}} {Lo:{{.Lo}}, Hi:{{.Hi}}, Stride: {{.Stride}}}, |
| 156 | + {{end}} }, |
| 157 | + LatinOffset: {{.RangeTable.LatinOffset}}, |
| 158 | + }, |
| 159 | + }, |
| 160 | + {{end}} |
| 161 | +} |
| 162 | +
|
| 163 | +`)) |
| 164 | + |
| 165 | +func logf(format string, args ...interface{}) { |
| 166 | + fmt.Fprintf(os.Stderr, format+"\n", args...) |
| 167 | +} |
| 168 | + |
| 169 | +func verbosef(format string, args ...interface{}) { |
| 170 | + if verbose { |
| 171 | + logf(format, args...) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func fatalf(format string, args ...interface{}) { |
| 176 | + logf("fatal: "+format+"\n", args...) |
| 177 | + os.Exit(1) |
| 178 | +} |
0 commit comments