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
285 lines (265 loc) · 9.63 KB
/
StreamImplNumber.java
File metadata and controls
285 lines (265 loc) · 9.63 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
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
/*
this implementations contains significant code from https://github.com/ngs-doo/dsl-json/blob/master/LICENSE
Copyright (c) 2015, Nova Generacija Softvera d.o.o.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of Nova Generacija Softvera d.o.o. nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jsoniter.output;
import java.io.IOException;
class StreamImplNumber {
private final static int[] DIGITS = new int[1000];
static {
for (int i = 0; i < 1000; i++) {
DIGITS[i] = (i < 10 ? (2 << 24) : i < 100 ? (1 << 24) : 0)
+ (((i / 100) + '0') << 16)
+ ((((i / 10) % 10) + '0') << 8)
+ i % 10 + '0';
}
}
private static final byte[] MIN_INT = "-2147483648".getBytes();
public static final void writeInt(final JsonStream stream, int value) throws IOException {
stream.ensure(12);
byte[] buf = stream.buf;
int pos = stream.count;
if (value < 0) {
if (value == Integer.MIN_VALUE) {
System.arraycopy(MIN_INT, 0, buf, pos, MIN_INT.length);
stream.count = pos + MIN_INT.length;
return;
}
value = -value;
buf[pos++] = '-';
}
final int q1 = value / 1000;
if (q1 == 0) {
pos += writeFirstBuf(buf, DIGITS[value], pos);
stream.count = pos;
return;
}
final int r1 = value - q1 * 1000;
final int q2 = q1 / 1000;
if (q2 == 0) {
final int v1 = DIGITS[r1];
final int v2 = DIGITS[q1];
int off = writeFirstBuf(buf, v2, pos);
writeBuf(buf, v1, pos + off);
stream.count = pos + 3 + off;
return;
}
final int r2 = q1 - q2 * 1000;
final long q3 = q2 / 1000;
final int v1 = DIGITS[r1];
final int v2 = DIGITS[r2];
if (q3 == 0) {
pos += writeFirstBuf(buf, DIGITS[q2], pos);
} else {
final int r3 = (int) (q2 - q3 * 1000);
buf[pos++] = (byte) (q3 + '0');
writeBuf(buf, DIGITS[r3], pos);
pos += 3;
}
writeBuf(buf, v2, pos);
writeBuf(buf, v1, pos + 3);
stream.count = pos + 6;
}
private static int writeFirstBuf(final byte[] buf, final int v, int pos) {
final int start = v >> 24;
if (start == 0) {
buf[pos++] = (byte) (v >> 16);
buf[pos++] = (byte) (v >> 8);
} else if (start == 1) {
buf[pos++] = (byte) (v >> 8);
}
buf[pos] = (byte) v;
return 3 - start;
}
private static void writeBuf(final byte[] buf, final int v, int pos) {
buf[pos] = (byte) (v >> 16);
buf[pos + 1] = (byte) (v >> 8);
buf[pos + 2] = (byte) v;
}
private static final byte[] MIN_LONG = "-9223372036854775808".getBytes();
public static final void writeLong(final JsonStream stream, long value) throws IOException {
stream.ensure(22);
byte[] buf = stream.buf;
int pos = stream.count;
if (value < 0) {
if (value == Long.MIN_VALUE) {
System.arraycopy(MIN_LONG, 0, buf, pos, MIN_LONG.length);
stream.count = pos + MIN_LONG.length;
return;
}
value = -value;
buf[pos++] = '-';
}
final long q1 = value / 1000;
if (q1 == 0) {
pos += writeFirstBuf(buf, DIGITS[(int) value], pos);
stream.count = pos;
return;
}
final int r1 = (int) (value - q1 * 1000);
final long q2 = q1 / 1000;
if (q2 == 0) {
final int v1 = DIGITS[r1];
final int v2 = DIGITS[(int) q1];
int off = writeFirstBuf(buf, v2, pos);
writeBuf(buf, v1, pos + off);
stream.count = pos + 3 + off;
return;
}
final int r2 = (int) (q1 - q2 * 1000);
final long q3 = q2 / 1000;
if (q3 == 0) {
final int v1 = DIGITS[r1];
final int v2 = DIGITS[r2];
final int v3 = DIGITS[(int) q2];
pos += writeFirstBuf(buf, v3, pos);
writeBuf(buf, v2, pos);
writeBuf(buf, v1, pos + 3);
stream.count = pos + 6;
return;
}
final int r3 = (int) (q2 - q3 * 1000);
final int q4 = (int) (q3 / 1000);
if (q4 == 0) {
final int v1 = DIGITS[r1];
final int v2 = DIGITS[r2];
final int v3 = DIGITS[r3];
final int v4 = DIGITS[(int) q3];
pos += writeFirstBuf(buf, v4, pos);
writeBuf(buf, v3, pos);
writeBuf(buf, v2, pos + 3);
writeBuf(buf, v1, pos + 6);
stream.count = pos + 9;
return;
}
final int r4 = (int) (q3 - q4 * 1000);
final int q5 = q4 / 1000;
if (q5 == 0) {
final int v1 = DIGITS[r1];
final int v2 = DIGITS[r2];
final int v3 = DIGITS[r3];
final int v4 = DIGITS[r4];
final int v5 = DIGITS[q4];
pos += writeFirstBuf(buf, v5, pos);
writeBuf(buf, v4, pos);
writeBuf(buf, v3, pos + 3);
writeBuf(buf, v2, pos + 6);
writeBuf(buf, v1, pos + 9);
stream.count = pos + 12;
return;
}
final int r5 = q4 - q5 * 1000;
final int q6 = q5 / 1000;
final int v1 = DIGITS[r1];
final int v2 = DIGITS[r2];
final int v3 = DIGITS[r3];
final int v4 = DIGITS[r4];
final int v5 = DIGITS[r5];
if (q6 == 0) {
pos += writeFirstBuf(buf, DIGITS[q5], pos);
} else {
final int r6 = q5 - q6 * 1000;
buf[pos++] = (byte) (q6 + '0');
writeBuf(buf, DIGITS[r6], pos);
pos += 3;
}
writeBuf(buf, v5, pos);
writeBuf(buf, v4, pos + 3);
writeBuf(buf, v3, pos + 6);
writeBuf(buf, v2, pos + 9);
writeBuf(buf, v1, pos + 12);
stream.count = pos + 15;
}
private static final int POW10[] = {1, 10, 100, 1000, 10000, 100000, 1000000};
public static final void writeFloat(JsonStream stream, float val) throws IOException {
if (val < 0) {
if (val == Float.NEGATIVE_INFINITY) {
stream.writeVal("-Infinity");
return;
}
stream.write('-');
val = -val;
}
if (val > 0x4ffffff) {
if (val == Float.POSITIVE_INFINITY) {
stream.writeVal("Infinity");
return;
}
stream.writeRaw(Float.toString(val));
return;
}
int precision = 6;
int exp = 1000000; // 6
long lval = (long)(val * exp + 0.5);
stream.writeVal(lval / exp);
long fval = lval % exp;
if (fval == 0) {
return;
}
stream.write('.');
stream.ensure(11);
for (int p = precision - 1; p > 0 && fval < POW10[p]; p--) {
stream.buf[stream.count++] = '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) {
if (val == Double.NEGATIVE_INFINITY) {
stream.writeVal("-Infinity");
return;
}
val = -val;
stream.write('-');
}
if (val > 0x4ffffff) {
if (val == Double.POSITIVE_INFINITY) {
stream.writeVal("Infinity");
return;
}
stream.writeRaw(Double.toString(val));
return;
}
int precision = 6;
int exp = 1000000; // 6
long lval = (long)(val * exp + 0.5);
stream.writeVal(lval / exp);
long fval = lval % exp;
if (fval == 0) {
return;
}
stream.write('.');
stream.ensure(11);
for (int p = precision - 1; p > 0 && fval < POW10[p]; p--) {
stream.buf[stream.count++] = '0';
}
stream.writeVal(fval);
while(stream.buf[stream.count-1] == '0') {
stream.count--;
}
}
}