-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathenc_int.go
148 lines (138 loc) · 3.11 KB
/
enc_int.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
package jx
var digits []uint32
func init() {
digits = make([]uint32, 1000)
for i := uint32(0); i < 1000; i++ {
digits[i] = (((i / 100) + '0') << 16) + ((((i / 10) % 10) + '0') << 8) + i%10 + '0'
if i < 10 {
digits[i] += 2 << 24
} else if i < 100 {
digits[i] += 1 << 24
}
}
}
func writeFirstBuf(space []byte, v uint32) []byte {
start := v >> 24
if start == 0 {
space = append(space, byte(v>>16), byte(v>>8))
} else if start == 1 {
space = append(space, byte(v>>8))
}
space = append(space, byte(v))
return space
}
func writeBuf(buf []byte, v uint32) []byte {
return append(buf, byte(v>>16), byte(v>>8), byte(v))
}
// Uint32 writes uint32 to stream.
func (e *Encoder) Uint32(val uint32) {
q1 := val / 1000
if q1 == 0 {
e.buf = writeFirstBuf(e.buf, digits[val])
return
}
r1 := val - q1*1000
q2 := q1 / 1000
if q2 == 0 {
e.buf = writeFirstBuf(e.buf, digits[q1])
e.buf = writeBuf(e.buf, digits[r1])
return
}
r2 := q1 - q2*1000
q3 := q2 / 1000
if q3 == 0 {
e.buf = writeFirstBuf(e.buf, digits[q2])
} else {
r3 := q2 - q3*1000
e.buf = append(e.buf, byte(q3+'0'))
e.buf = writeBuf(e.buf, digits[r3])
}
e.buf = writeBuf(e.buf, digits[r2])
e.buf = writeBuf(e.buf, digits[r1])
}
// Int32 writes int32 to stream.
func (e *Encoder) Int32(nval int32) {
var val uint32
if nval < 0 {
val = uint32(-nval)
e.buf = append(e.buf, '-')
} else {
val = uint32(nval)
}
e.Uint32(val)
}
// Uint64 writes uint64 to stream.
func (e *Encoder) Uint64(val uint64) {
q1 := val / 1000
if q1 == 0 {
e.buf = writeFirstBuf(e.buf, digits[val])
return
}
r1 := val - q1*1000
q2 := q1 / 1000
if q2 == 0 {
e.buf = writeFirstBuf(e.buf, digits[q1])
e.buf = writeBuf(e.buf, digits[r1])
return
}
r2 := q1 - q2*1000
q3 := q2 / 1000
if q3 == 0 {
e.buf = writeFirstBuf(e.buf, digits[q2])
e.buf = writeBuf(e.buf, digits[r2])
e.buf = writeBuf(e.buf, digits[r1])
return
}
r3 := q2 - q3*1000
q4 := q3 / 1000
if q4 == 0 {
e.buf = writeFirstBuf(e.buf, digits[q3])
e.buf = writeBuf(e.buf, digits[r3])
e.buf = writeBuf(e.buf, digits[r2])
e.buf = writeBuf(e.buf, digits[r1])
return
}
r4 := q3 - q4*1000
q5 := q4 / 1000
if q5 == 0 {
e.buf = writeFirstBuf(e.buf, digits[q4])
e.buf = writeBuf(e.buf, digits[r4])
e.buf = writeBuf(e.buf, digits[r3])
e.buf = writeBuf(e.buf, digits[r2])
e.buf = writeBuf(e.buf, digits[r1])
return
}
r5 := q4 - q5*1000
q6 := q5 / 1000
if q6 == 0 {
e.buf = writeFirstBuf(e.buf, digits[q5])
} else {
e.buf = writeFirstBuf(e.buf, digits[q6])
r6 := q5 - q6*1000
e.buf = writeBuf(e.buf, digits[r6])
}
e.buf = writeBuf(e.buf, digits[r5])
e.buf = writeBuf(e.buf, digits[r4])
e.buf = writeBuf(e.buf, digits[r3])
e.buf = writeBuf(e.buf, digits[r2])
e.buf = writeBuf(e.buf, digits[r1])
}
// Int64 writes int64 to stream
func (e *Encoder) Int64(nval int64) {
var val uint64
if nval < 0 {
val = uint64(-nval)
e.buf = append(e.buf, '-')
} else {
val = uint64(nval)
}
e.Uint64(val)
}
// Int writes int to stream.
func (e *Encoder) Int(val int) {
e.Int64(int64(val))
}
// Uint writes uint to stream.
func (e *Encoder) Uint(val uint) {
e.Uint64(uint64(val))
}