-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
398 lines (332 loc) · 7.1 KB
/
util.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package base
import "github.com/kazu/loncha"
const (
SizeOfbool = 1
SizeOfint8 = 1
SizeOfint16 = 2
SizeOfuint16 = 2
SizeOfint32 = 4
SizeOfuint32 = 4
SizeOfint64 = 8
SizeOfuint64 = 8
SizeOffloat32 = 4
SizeOffloat64 = 8
SizeOfuint8 = 1
SizeOfbyte = 1
)
const (
TypeBool = iota
TypeByte
TypeInt8
TypeInt16
TypeInt32
TypeInt64
TypeUint8
TypeUint16
TypeUint32
TypeUint64
TypeFloat32
TypeFloat64
)
const (
FieldTypeNone = 1 << iota
FieldTypeStruct
FieldTypeTable
FieldTypeUnion
FieldTypeSlice
FieldTypeBasic1
FieldTypeBasic2
FieldTypeBasic4
FieldTypeBasic8
)
type Bool bool
type Byte byte
type Int8 int8
type Int16 int16
type Int32 int32
type Int64 int64
type Uint8 uint8
type Uint16 uint16
type Uint32 uint32
type Uint64 uint64
type Float32 float32
type Float64 float64
var NameToType map[string]int = map[string]int{
"bool": TypeBool,
"byte": TypeByte,
"int8": TypeInt8,
"int16": TypeInt16,
"int32": TypeInt32,
"int64": TypeInt64,
"unt8": TypeUint8,
"uint16": TypeUint16,
"uint32": TypeUint32,
"uint64": TypeUint64,
"float32": TypeFloat32,
"float64": TypeFloat64,
"Bool": TypeBool,
"Byte": TypeByte,
"Int8": TypeInt8,
"Int16": TypeInt16,
"Int32": TypeInt32,
"Int64": TypeInt64,
"Unt8": TypeUint8,
"Uint16": TypeUint16,
"Uint32": TypeUint32,
"Uint64": TypeUint64,
"Float32": TypeFloat32,
"Float64": TypeFloat64,
}
var TypeToGroup map[int]int = map[int]int{
TypeBool: FieldTypeBasic1,
TypeByte: FieldTypeBasic1,
TypeInt8: FieldTypeBasic1,
TypeInt16: FieldTypeBasic2,
TypeInt32: FieldTypeBasic4,
TypeInt64: FieldTypeBasic8,
TypeUint8: FieldTypeBasic1,
TypeUint16: FieldTypeBasic2,
TypeUint32: FieldTypeBasic4,
TypeUint64: FieldTypeBasic8,
TypeFloat32: FieldTypeBasic4,
TypeFloat64: FieldTypeBasic8,
}
var TypeToSize map[int]int = map[int]int{
TypeBool: 1,
TypeByte: 1,
TypeInt8: 1,
TypeInt16: 2,
TypeInt32: 4,
TypeInt64: 8,
TypeUint8: 1,
TypeUint16: 2,
TypeUint32: 4,
TypeUint64: 8,
TypeFloat32: 4,
TypeFloat64: 8,
}
var IsStructName map[string]bool = map[string]bool{}
func SetNameIsStrunct(name string, enable bool) bool {
if enable {
IsStructName[name] = true
return true
}
return false
}
func NameToTypeEnum(s string) int {
if v, ok := NameToType[s]; ok {
return v
}
return -1
}
func HasNameType(s string) bool {
_, ok := NameToType[s]
return ok
}
func AtoiNoErr(i int, e error) int {
if e != nil {
Log(LOG_WARN, func() LogArgs {
return F("AtoiNoErr(%d, e=%v) has error\n", i, e)
})
return 0
}
return i
}
const FieldTypeBasic = FieldTypeBasic1 | FieldTypeBasic2 | FieldTypeBasic4 | FieldTypeBasic8
func IsFieldStruct(i int) bool {
return IsMatchBit(i, FieldTypeStruct)
}
func IsFieldUnion(i int) bool {
return IsMatchBit(i, FieldTypeUnion)
}
func IsFieldBytes(i int) bool {
return IsMatchBit(i, FieldTypeSlice) && IsMatchBit(i, FieldTypeBasic1)
}
func IsFieldSlice(i int) bool {
return IsMatchBit(i, FieldTypeSlice)
}
func IsFieldTable(i int) bool {
return IsMatchBit(i, FieldTypeTable)
}
func IsFieldBasicType(i int) bool {
return IsMatchBit(i, FieldTypeBasic)
}
var UnionAlias map[string][]string = map[string][]string{}
func SetAlias(union, alias string) bool {
if UnionAlias[union] == nil {
UnionAlias[union] = []string{}
}
UnionAlias[union] = append(UnionAlias[union], alias)
return true
}
func IsUnionName(s string) bool {
_, ok := UnionAlias[s]
return ok
}
func ToBool(s string) bool {
return s == "true" || s == "True"
}
var All_IdxToType map[string](map[int]int) = map[string](map[int]int){}
var All_IdxToTypeGroup map[string](map[int]int) = map[string](map[int]int){}
var All_IdxToName map[string](map[int]string) = map[string](map[int]string){}
var All_NameToIdx map[string](map[string]int) = map[string](map[string]int){}
var IdxNames []string = []string{}
var Name2Idx map[string]int = map[string]int{}
func AddName(s string) {
if _, ok := Name2Idx[s]; ok {
return
}
IdxNames = append(IdxNames, s)
idx, err := loncha.IndexOf(IdxNames, func(i int) bool { return IdxNames[i] == s })
if err != nil {
Log(LOG_WARN, func() LogArgs {
return F("IdxNames: fail to add %s ", s)
})
return
}
Name2Idx[s] = idx
return
}
func Symbol(s string) int {
v, ok := Name2Idx[s]
if !ok {
return -1
}
return v
}
func SymbolToString(i int) string {
if i >= len(IdxNames) {
return ""
}
return IdxNames[i]
}
func NodeFieldSynmbol(node, field string) (uint32, error) {
nidx := uint32(Symbol(node))
fidx := uint32(Symbol(field))
if nidx < 0 || fidx < 0 {
return 0, ERR_INVALID_INDEX
}
nidx = nidx << 16
nidx += fidx
return nidx, nil
}
func SymbolToNames(idx uint32) (node, field string, e error) {
nidx := idx >> 16
fidx := idx - ((idx >> 16) << 16)
node = SymbolToString(int(nidx))
if node == "" {
e = ERR_INVALID_INDEX
return
}
field = SymbolToString(int(fidx))
if field == "" {
e = ERR_INVALID_INDEX
return
}
return
}
func IsSameTable(i, j uint32) bool {
return (i >> 16) == (j >> 16)
}
func IsSameField(i, j uint32) bool {
return (i - ((i >> 16) << 16)) == (j - ((j >> 16) << 16))
}
func SetNameToIdx(name string, v map[string]int) {
All_NameToIdx[name] = v
}
func SetIdxToName(name string, v map[int]string) {
All_IdxToName[name] = v
}
func SetIdxToType(name string, v map[int]int) {
All_IdxToType[name] = v
}
func SetdxToTypeGroup(name string, v map[int]int) {
All_IdxToTypeGroup[name] = v
}
func GetTypeGroup(s string) (result int) {
result = 0
if enum, ok := NameToType[s]; ok {
return TypeToGroup[enum]
}
if _, ok := UnionAlias[s]; ok {
return FieldTypeUnion
}
if len(s) >= 6 && s[0:6] == "[]byte" {
return FieldTypeSlice | FieldTypeBasic1
}
if len(s) >= 2 && s[0:2] == "[]" {
result |= FieldTypeSlice
}
if enum, ok := NameToType[s[2:]]; ok {
result |= TypeToGroup[enum]
return
}
if IsStructName[s] {
result |= FieldTypeStruct
} else {
result |= FieldTypeTable
}
return
}
type ReqestNameField struct {
Names []string
fNum int
}
var requestNameFields []ReqestNameField = []ReqestNameField{}
func RequestSettingNameFields(nName, fName, fType string, fNum int) bool {
requestNameFields = append(requestNameFields,
ReqestNameField{
Names: []string{nName, fName, fType},
fNum: fNum,
})
return true
}
func ApplyRequestNameFields() {
if len(requestNameFields) == 0 {
return
}
loncha.Delete(&requestNameFields, func(i int) bool {
req := requestNameFields[i]
grp := GetTypeGroup(req.Names[2])
idxs := All_IdxToTypeGroup[req.Names[0]]
idxs[req.fNum] = grp
return true
})
return
}
// RootName undocumented
var RootName string = ""
// SetRootName undocumented
func SetRootName(s string) {
if len(RootName) == 0 {
RootName = s
}
}
// MaxInt ... return maxinum value from values
func MaxInt(vals ...int) (r int) {
r = vals[0]
for _, v := range vals {
if v > r {
r = v
}
}
return
}
// MinInt ... return minimum value from values
func MinInt(vals ...int) (r int) {
r = vals[0]
for _, v := range vals {
if v < r {
r = v
}
}
return
}
// SumInts ... sum of []int
func SumInts(ints []int) int {
sum := 0
for _, v := range ints {
sum += v
}
return sum
}