optimize cp473 converter

This commit is contained in:
Robert Janetzko 2022-04-20 10:46:42 +00:00
parent 5f461c4f0f
commit f038523b46
6 changed files with 287 additions and 274 deletions

View file

@ -173,7 +173,7 @@ func parse{{ $obj.Name }}{{ if $plus }}Plus{{ end }}(p *util.XMLParser{{ if $plu
if err != nil {
return nil, err
}
switch data {
switch string(data) {
{{- range $sub := ($obj.ActiveSubTypes $plus) }}
case "{{ $sub.Case }}":
{{- if eq 1 (len $sub.Options) }}
@ -356,20 +356,20 @@ func (f Field) StartAction(obj Object, plus bool) string {
if f.Type == "int" {
return fmt.Sprintf("%sobj.%s = num(data)", s, n)
} else if f.Type == "string" {
return fmt.Sprintf("%sobj.%s = string(data)", s, n)
return fmt.Sprintf("%sobj.%s = txt(data)", s, n)
} else if f.Type == "bool" {
s := "_, err := p.Value()\nif err != nil { return nil, err }\n"
return fmt.Sprintf("%sobj.%s = true", s, n)
} else if f.Type == "enum" {
return fmt.Sprintf("%sobj.%s = parse%s%s(string(data))", s, n, obj.Name, n)
return fmt.Sprintf("%sobj.%s = parse%s%s(txt(data))", s, n, obj.Name, n)
}
} else {
if f.Type == "int" {
return fmt.Sprintf("%sobj.%s = append(obj.%s, num(data))", s, n, n)
} else if f.Type == "string" {
return fmt.Sprintf("%sobj.%s = append(obj.%s, string(data))", s, n, n)
return fmt.Sprintf("%sobj.%s = append(obj.%s, txt(data))", s, n, n)
} else if f.Type == "enum" {
return fmt.Sprintf("%sobj.%s = append(obj.%s, parse%s%s(string(data)))", s, n, n, obj.Name, n)
return fmt.Sprintf("%sobj.%s = append(obj.%s, parse%s%s(txt(data)))", s, n, n, obj.Name, n)
}
}
}

View file

@ -29,6 +29,7 @@ var static embed.FS
func main() {
f := flag.String("f", "", "open a file")
p := flag.Bool("p", false, "start profiling")
flag.Parse()
router := mux.NewRouter().StrictSlash(true)
@ -116,10 +117,12 @@ func main() {
t := templates.New(functions)
if len(*f) > 0 {
defer profile.Start(profile.ProfilePath(".")).Stop()
go func() {
http.ListenAndServe(":8081", nil)
}()
if *p {
defer profile.Start(profile.ProfilePath(".")).Stop()
go func() {
http.ListenAndServe(":8081", nil)
}()
}
w, err := model.Parse(*f)
if err != nil {

File diff suppressed because it is too large Load diff

View file

@ -55,8 +55,7 @@ func NewLegendsParser(file string) (*util.XMLParser, *os.File, *pb.ProgressBar,
fmt.Println("Successfully Opened", file)
converter := util.NewConvertReader(xmlFile)
barReader := bar.NewProxyReader(converter)
barReader := bar.NewProxyReader(xmlFile)
d := util.NewXMLParser(bufio.NewReader(barReader))
return d, xmlFile, bar, err
@ -198,11 +197,11 @@ func parseId(p *util.XMLParser) (int, error) {
switch t {
case util.StartElement:
if n == "id" {
s, err := p.Value()
d, err := p.Value()
if err != nil {
return -1, err
}
return strconv.Atoi(s)
return strconv.Atoi(string(d))
} else {
p.Skip()
}
@ -210,11 +209,15 @@ func parseId(p *util.XMLParser) (int, error) {
}
}
func num(s string) int {
v, _ := strconv.Atoi(s)
func num(b []byte) int {
v, _ := strconv.Atoi(string(b))
return v
}
func txt(b []byte) string {
return util.ConvertCp473(b)
}
var sameFields map[string]map[string]map[string]bool
func exportSameFields() map[string]map[string]string {

View file

@ -26,3 +26,10 @@ func (c *ConvertReader) Read(b []byte) (n int, err error) {
}
return n, err
}
func ConvertCp473(b []byte) string {
for i := range b {
b[i] = cp437[b[i]]
}
return string(b)
}

View file

@ -88,10 +88,10 @@ func (x *XMLParser) Token() (TokenType, string, error) {
}
}
func (x *XMLParser) Value() (string, error) {
func (x *XMLParser) Value() ([]byte, error) {
if x.selfClose {
x.selfClose = false
return "", nil
return nil, nil
}
x.scratch.reset()
@ -101,12 +101,12 @@ func (x *XMLParser) Value() (string, error) {
for {
b, err := x.reader.ReadByte()
if err != nil {
return "", err
return nil, err
}
if b == '<' {
f = true
} else if f && b == '>' {
return string(x.scratch.bytes()), nil
return x.scratch.bytes(), nil
} else if !f {
x.scratch.add(b)