// SPDX-License-Identifier: MIT //go:build ignore // Command fold reads the "XXXX YYYY" lines printed by `runner.dll --fold` // (see gen/runner) on stdin and writes fold_tables.go: the case folding of // .NET's StringComparison.OrdinalIgnoreCase, code point by code point. package main import ( "bufio" "flag" "fmt" "go/format" "os" "strconv" "strings" ) func main() { out := flag.String("o", "", "output file (default stdout)") flag.Parse() var b strings.Builder b.WriteString("// SPDX-License-Identifier: MIT\n\n// Code generated by gen/fold.go from `runner.dll --fold` (gen/runner); DO NOT EDIT.\n\npackage expr\n\n") b.WriteString("// ordinalUpper maps a code point to the one that .NET's OrdinalIgnoreCase comparison treats\n// it as, sorted by the first element. Code points that are not listed stand for themselves.\n") b.WriteString("var ordinalUpper = [...][2]rune{\n") sc := bufio.NewScanner(os.Stdin) n := 0 for sc.Scan() { f := strings.Fields(sc.Text()) if len(f) != 2 { continue } from, err1 := strconv.ParseInt(f[0], 16, 32) to, err2 := strconv.ParseInt(f[1], 16, 32) if err1 != nil || err2 != nil { fmt.Fprintln(os.Stderr, "bad line:", sc.Text()) os.Exit(1) } fmt.Fprintf(&b, "\t{0x%04X, 0x%04X},\n", from, to) n++ } b.WriteString("}\n") src, err := format.Source([]byte(b.String())) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } if n == 0 { fmt.Fprintln(os.Stderr, "no input") os.Exit(1) } if *out == "" { os.Stdout.Write(src) return } if err := os.WriteFile(*out, src, 0o644); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } }