forked from dotabuff/manta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_type.go
56 lines (47 loc) · 971 Bytes
/
field_type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package manta
import (
"fmt"
"regexp"
"strconv"
)
var fieldTypeRe = regexp.MustCompile(`([^\<\[\*]+)(\<\s(.*)\s\>)?(\*)?(\[(.*)\])?`) // (\<\s.*?\s\>)?([.*?])?`)
type fieldType struct {
baseType string
genericType *fieldType
pointer bool
count int
}
func newFieldType(name string) *fieldType {
ss := fieldTypeRe.FindStringSubmatch(name)
if len(ss) != 7 {
panic(fmt.Sprintf("bad regexp: %s -> %#v", name, ss))
}
x := &fieldType{
baseType: ss[1],
pointer: ss[4] == "*",
}
if ss[3] != "" {
x.genericType = newFieldType(ss[3])
}
if n, ok := itemCounts[ss[6]]; ok {
x.count = n
} else if n, _ := strconv.Atoi(ss[6]); n > 0 {
x.count = n
} else if ss[6] != "" {
x.count = 1024
}
return x
}
func (t *fieldType) String() string {
x := t.baseType
if t.genericType != nil {
x += "<" + t.genericType.String() + ">"
}
if t.pointer {
x += "*"
}
if t.count > 0 {
x += "[" + strconv.Itoa(t.count) + "]"
}
return x
}