| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "bytes" |
| 6 | "encoding/json" |
| 7 | "flag" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "net/http" |
| 11 | "os" |
| 12 | "strconv" |
| 13 | "strings" |
| 14 | ) |
| 15 | |
| 16 | type caseFolding struct { |
| 17 | Class byte |
| 18 | From rune |
| 19 | To []rune |
| 20 | } |
| 21 | |
| 22 | func unicodeCaseFoldingMapSubCommand(args []string) { |
| 23 | cmdName := "unicode-case-folding-map" |
| 24 | cmd := flag.NewFlagSet(cmdName, flag.ExitOnError) |
| 25 | cmd.Usage = func() { |
| 26 | _, _ = fmt.Fprintf(cmd.Output(), `Usage of %s: |
| 27 | |
| 28 | Generate input JSON data for emb-structs subcommand from unicode.org |
| 29 | |
| 30 | `, cmdName) |
| 31 | cmd.PrintDefaults() |
| 32 | } |
| 33 | |
| 34 | outputPath := cmd.String("o", "", "output file path(required)") |
| 35 | unicodeVersion := cmd.String("u", "15.0.0", "unicode version") |
| 36 | |
| 37 | if err := cmd.Parse(args); err != nil || |
| 38 | len(*outputPath) == 0 { |
| 39 | usage(cmd.Usage, err) |
| 40 | } |
| 41 | |
| 42 | url := "http://www.unicode.org/Public/" + *unicodeVersion + "/ucd/CaseFolding.txt" |
| 43 | |
| 44 | resp, err := http.Get(url) |
| 45 | if err != nil { |
| 46 | fmt.Printf("Failed to get CaseFolding.txt: %v\n", err) |
| 47 | os.Exit(1) |
| 48 | } |
| 49 | defer resp.Body.Close() |
| 50 | |
| 51 | bs, err := io.ReadAll(resp.Body) |
| 52 | if err != nil { |
| 53 | fmt.Printf("Failed to get CaseFolding.txt: %v\n", err) |
| 54 | os.Exit(1) |
| 55 | } |
| 56 | |
| 57 | buf := bytes.NewBuffer(bs) |
| 58 | scanner := bufio.NewScanner(buf) |
| 59 | |
| 60 | embstructmap := make(map[string]any) |
| 61 | embstructmap["prefix"] = "unicodeCaseFolding" |
| 62 | embstructmap["types"] = map[string]any{ |
| 63 | "From": "rune", |
| 64 | "To": "[]rune", |
| 65 | } |
| 66 | var data []any |
| 67 | |
| 68 | for scanner.Scan() { |
| 69 | line := scanner.Text() |
| 70 | if strings.HasPrefix(line, "#") || len(strings.TrimSpace(line)) == 0 { |
| 71 | continue |
| 72 | } |
| 73 | line = strings.Split(line, "#")[0] |
| 74 | parts := strings.Split(line, ";") |
| 75 | for i, p := range parts { |
| 76 | parts[i] = strings.TrimSpace(p) |
| 77 | } |
| 78 | cf := caseFolding{} |
| 79 | v, _ := strconv.ParseInt(parts[0], 16, 32) |
| 80 | cf.From = rune(int32(v)) |
| 81 | cf.Class = parts[1][0] |
| 82 | for _, v := range strings.Split(parts[2], " ") { |
| 83 | c, _ := strconv.ParseInt(v, 16, 32) |
| 84 | cf.To = append(cf.To, rune(int32(c))) |
| 85 | } |
| 86 | if cf.Class != 'C' && cf.Class != 'F' { |
| 87 | continue |
| 88 | } |
| 89 | var tos []string |
| 90 | for _, v := range cf.To { |
| 91 | tos = append(tos, fmt.Sprintf("%d", v)) |
| 92 | } |
| 93 | data = append(data, map[string]any{ |
| 94 | "From": fmt.Sprintf("0x%x", cf.From), |
| 95 | "To": tos, |
| 96 | }) |
| 97 | } |
| 98 | embstructmap["data"] = data |
| 99 | jsonData, err := json.MarshalIndent(embstructmap, "", " ") |
| 100 | if err != nil { |
| 101 | panic(err) |
| 102 | } |
| 103 | err = os.WriteFile(*outputPath, jsonData, 0644) |
| 104 | if err != nil { |
| 105 | panic(err) |
| 106 | } |
| 107 | |
| 108 | } |