forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamImplNumber.java
More file actions
213 lines (195 loc) · 6.92 KB
/
StreamImplNumber.java
File metadata and controls
213 lines (195 loc) · 6.92 KB
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
package com.jsoniter.output;
import java.io.IOException;
class StreamImplNumber {
private final static byte[] DigitTens = {
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
'5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
'6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
'7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
'8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
'9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
};
private final static byte[] DigitOnes = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
};
/**
* All possible chars for representing a number as a String
*/
private final static byte[] digits = {
'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b',
'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z'
};
private static final byte[] INT_MIN = "-2147483648".getBytes();
private static final byte[] LONG_MIN = "-9223372036854775808".getBytes();
public static final void writeInt(JsonStream stream, int val) throws IOException {
if (val == Integer.MIN_VALUE) {
stream.write(INT_MIN);
return;
}
if (val < 0) {
stream.write('-');
val = -val;
}
if (stream.buf.length - stream.count < 10) {
stream.flushBuffer();
}
int charPos = stream.count + stringSize(val);
stream.count = charPos;
int q, r;
// Generate two digits per iteration
while (val >= 65536) {
q = val / 100;
// really: r = i - (q * 100);
r = val - ((q << 6) + (q << 5) + (q << 2));
val = q;
stream.buf[--charPos] = DigitOnes[r];
stream.buf[--charPos] = DigitTens[r];
}
// Fall thru to fast mode for smaller numbers
// assert(i <= 65536, i);
for (; ; ) {
q = (val * 52429) >>> (16 + 3);
r = val - ((q << 3) + (q << 1)); // r = i-(q*10) ...
stream.buf[--charPos] = digits[r];
val = q;
if (val == 0) break;
}
}
private final static int[] sizeTable = {9, 99, 999, 9999, 99999, 999999, 9999999,
99999999, 999999999, Integer.MAX_VALUE};
// Requires positive x
private static int stringSize(int x) {
for (int i = 0; ; i++)
if (x <= sizeTable[i])
return i + 1;
}
public static final void writeLong(JsonStream stream, long val) throws IOException {
if (val == Long.MIN_VALUE) {
stream.write(LONG_MIN);
return;
}
if (val < 0) {
stream.write('-');
val = -val;
}
if (stream.buf.length - stream.count < 20) {
stream.flushBuffer();
}
long q;
int r;
int charPos = stream.count + stringSize(val);
stream.count = charPos;
char sign = 0;
// Get 2 digits/iteration using longs until quotient fits into an int
while (val > Integer.MAX_VALUE) {
q = val / 100;
// really: r = i - (q * 100);
r = (int)(val - ((q << 6) + (q << 5) + (q << 2)));
val = q;
stream.buf[--charPos] = DigitOnes[r];
stream.buf[--charPos] = DigitTens[r];
}
// Get 2 digits/iteration using ints
int q2;
int i2 = (int)val;
while (i2 >= 65536) {
q2 = i2 / 100;
// really: r = i2 - (q * 100);
r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2));
i2 = q2;
stream.buf[--charPos] = DigitOnes[r];
stream.buf[--charPos] = DigitTens[r];
}
// Fall thru to fast mode for smaller numbers
// assert(i2 <= 65536, i2);
for (;;) {
q2 = (i2 * 52429) >>> (16+3);
r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ...
stream.buf[--charPos] = digits[r];
i2 = q2;
if (i2 == 0) break;
}
}
private static int stringSize(long x) {
long p = 10;
for (int i=1; i<19; i++) {
if (x < p)
return i;
p = 10*p;
}
return 19;
}
private static final int POW10[] = {1, 10, 100, 1000, 10000, 100000, 1000000};
private static final long MAX_DOUBLE_TO_WRITE = Long.MAX_VALUE / 1000000 - 1;
public static final void writeFloat(JsonStream stream, float val) throws IOException {
if (val < 0) {
stream.write('-');
val = -val;
}
int precision = 6;
int exp = POW10[precision];
long lval = (long)(val * exp + 0.5);
stream.writeVal(lval / exp);
long fval = lval % exp;
if (fval == 0) {
return;
}
stream.write('.');
if (stream.buf.length - stream.count < 10) {
stream.flushBuffer();
}
for (int p = precision - 1; p > 0 && fval < POW10[p]; p--) {
stream.write('0');
}
stream.writeVal(fval);
while(stream.buf[stream.count-1] == '0') {
stream.count--;
}
}
public static final void writeDouble(JsonStream stream, double val) throws IOException {
if (val < 0) {
val = -val;
stream.write('-');
}
if (val > MAX_DOUBLE_TO_WRITE) {
stream.writeRaw(Double.toString(val));
return;
}
int precision = 6;
int exp = POW10[precision];
long lval = (long)(val * exp + 0.5);
stream.writeVal(lval / exp);
long fval = lval % exp;
if (fval == 0) {
return;
}
stream.write('.');
if (stream.buf.length - stream.count < 10) {
stream.flushBuffer();
}
for (int p = precision - 1; p > 0 && fval < POW10[p]; p--) {
stream.write('0');
}
stream.writeVal(fval);
while(stream.buf[stream.count-1] == '0') {
stream.count--;
}
}
}