// writeKeys writes keys to a special index used by the display package. // tags are assumed to be sorted by length. func writeKeys(w *gen.CodeWriter, name string, keys []string) { w.Size += int(3 * reflect.TypeOf("").Size()) w.WriteComment("Number of keys: %d", len(keys)) fmt.Fprintf(w, "var (\n\t%sIndex = tagIndex{\n", name) for i := 2; i <= 4; i++ { sub := []string{} for _, t := range keys { if len(t) != i { break } sub = append(sub, t) } s := strings.Join(sub, "") w.WriteString(s) fmt.Fprintf(w, ",\n") keys = keys[len(sub):] } fmt.Fprintln(w, "\t}") if len(keys) > 0 { w.Size += int(reflect.TypeOf([]string{}).Size()) fmt.Fprintf(w, "\t%sTagsLong = ", name) w.WriteSlice(keys) } fmt.Fprintln(w, ")\n") }
// WriteTable writes an entry for a single Namer. func (g *group) writeTable(w *gen.CodeWriter, name string) { start := w.Size writeKeys(w, name, g.toTags) w.Size += len(g.headers) * int(reflect.ValueOf(g.headers[0]).Type().Size()) fmt.Fprintf(w, "var %sHeaders = [%d]header{\n", name, len(g.headers)) title := strings.Title(name) for _, h := range g.headers { h.writeEntry(w, title) } fmt.Fprintln(w, "}\n") for _, h := range g.headers { h.writeSingle(w, title) } n := w.Size - start fmt.Fprintf(w, "// Total size for %s: %d bytes (%d KB)\n\n", name, n, n/1000) }
func genNumSystem(w *gen.CodeWriter, data *cldr.CLDR) { numSysData := []systemData{ {digitSize: 1, zero: [4]byte{'0'}}, } for _, ns := range data.Supplemental().NumberingSystems.NumberingSystem { if len(ns.Digits) == 0 { continue } switch ns.Id { case "latn": // hard-wired continue case "hanidec": // non-consecutive digits: treat as "algorithmic" continue } zero, sz := utf8.DecodeRuneInString(ns.Digits) if ns.Digits[sz-1]+9 > 0xBF { // 1011 1111: highest continuation byte log.Fatalf("Last byte of zero value overflows for %s", ns.Id) } i := rune(0) for _, r := range ns.Digits { // Verify that we can do simple math on the UTF-8 byte sequence // of zero to get the digit. if zero+i != r { // Runes not consecutive. log.Fatalf("Digit %d of %s (%U) is not offset correctly from zero value", i, ns.Id, r) } i++ } var x [utf8.UTFMax]byte utf8.EncodeRune(x[:], zero) id := system(len(numSysData)) systemMap[ns.Id] = id numSysData = append(numSysData, systemData{ id: id, digitSize: byte(sz), zero: x, }) } w.WriteVar("numSysData", numSysData) algoID := system(len(numSysData)) fmt.Fprintln(w, "const (") for _, ns := range data.Supplemental().NumberingSystems.NumberingSystem { id, ok := systemMap[ns.Id] if !ok { id = algoID systemMap[ns.Id] = id algoID++ } fmt.Fprintf(w, "num%s = %#x\n", strings.Title(ns.Id), id) } fmt.Fprintln(w, "numNumberSystems") fmt.Fprintln(w, ")") fmt.Fprintln(w, "var systemMap = map[string]system{") for _, ns := range data.Supplemental().NumberingSystems.NumberingSystem { fmt.Fprintf(w, "%q: num%s,\n", ns.Id, strings.Title(ns.Id)) w.Size += len(ns.Id) + 16 + 1 // very coarse approximation } fmt.Fprintln(w, "}") }