-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.go
113 lines (108 loc) · 1.94 KB
/
tag.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package nbt
import (
"math"
"reflect"
)
const (
tagEnd byte = iota
tagByte
tagInt16
tagInt32
tagInt64
tagFloat32
tagFloat64
tagByteArray
tagString
tagSlice
tagStruct
tagInt32Array
tagInt64Array
)
// tagName returns the name of a tag. The function panics if the tag does not exist.
func tagName(tag byte) string {
switch tag {
case tagEnd:
return "TAG_End"
case tagByte:
return "TAG_Byte"
case tagInt16:
return "TAG_Short"
case tagInt32:
return "TAG_Int"
case tagInt64:
return "TAG_Long"
case tagFloat32:
return "TAG_Float"
case tagFloat64:
return "TAG_Double"
case tagByteArray:
return "TAG_ByteArray"
case tagString:
return "TAG_String"
case tagSlice:
return "TAG_List"
case tagStruct:
return "TAG_Compound"
case tagInt32Array:
return "TAG_IntArray"
case tagInt64Array:
return "TAG_LongArray"
default:
panic("unknown tag")
}
}
// tagExists checks if a tag exists.
func tagExists(tag byte) bool {
switch tag {
case tagEnd,
tagByte,
tagInt16,
tagInt32,
tagInt64,
tagFloat32,
tagFloat64,
tagByteArray,
tagString,
tagSlice,
tagStruct,
tagInt32Array,
tagInt64Array:
return true
default:
return false
}
}
// tagFromType matches a reflect.Type with a tag type that can hold its value. If none is found, math.MaxUint8
// is returned.
func tagFromType(p reflect.Type) byte {
switch p.Kind() {
case reflect.Uint8, reflect.Bool:
return tagByte
case reflect.Int16:
return tagInt16
case reflect.Int32:
return tagInt32
case reflect.Int64:
return tagInt64
case reflect.Float32:
return tagFloat32
case reflect.Float64:
return tagFloat64
case reflect.Array:
switch p.Elem().Kind() {
case reflect.Uint8:
return tagByteArray
case reflect.Int32:
return tagInt32Array
case reflect.Int64:
return tagInt64Array
}
case reflect.String:
return tagString
case reflect.Slice:
return tagSlice
case reflect.Struct, reflect.Map:
return tagStruct
}
return math.MaxUint8
}