Skip to content

Commit 696fc4d

Browse files
committed
remove negative boolean flag for readInt
1 parent 48aed51 commit 696fc4d

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

src/main/java/com/jsoniter/IterImpl.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ public static int updateStringCopyBound(final JsonIterator iter, final int bound
323323
return bound;
324324
}
325325

326-
static final int readInt(final JsonIterator iter, final byte c, final boolean negative) throws IOException {
326+
static final int readInt(final JsonIterator iter, final byte c) throws IOException {
327327
int ind = IterImplNumber.intDigits[c];
328328
if (ind == 0) {
329329
IterImplForStreaming.assertNotLeadingZero(iter);
@@ -337,52 +337,52 @@ static final int readInt(final JsonIterator iter, final byte c, final boolean ne
337337
int ind2 = IterImplNumber.intDigits[iter.buf[i]];
338338
if (ind2 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
339339
iter.head = i;
340-
return negative ? -ind : ind;
340+
return -ind;
341341
}
342342
int ind3 = IterImplNumber.intDigits[iter.buf[++i]];
343343
if (ind3 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
344344
iter.head = i;
345345
ind = ind * 10 + ind2;
346-
return negative ? -ind : ind;
346+
return -ind;
347347
}
348348
int ind4 = IterImplNumber.intDigits[iter.buf[++i]];
349349
if (ind4 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
350350
iter.head = i;
351351
ind = ind * 100 + ind2 * 10 + ind3;
352-
return negative ? -ind : ind;
352+
return -ind;
353353
}
354354
int ind5 = IterImplNumber.intDigits[iter.buf[++i]];
355355
if (ind5 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
356356
iter.head = i;
357357
ind = ind * 1000 + ind2 * 100 + ind3 * 10 + ind4;
358-
return negative ? -ind : ind;
358+
return -ind;
359359
}
360360
int ind6 = IterImplNumber.intDigits[iter.buf[++i]];
361361
if (ind6 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
362362
iter.head = i;
363363
ind = ind * 10000 + ind2 * 1000 + ind3 * 100 + ind4 * 10 + ind5;
364-
return negative ? -ind : ind;
364+
return -ind;
365365
}
366366
int ind7 = IterImplNumber.intDigits[iter.buf[++i]];
367367
if (ind7 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
368368
iter.head = i;
369369
ind = ind * 100000 + ind2 * 10000 + ind3 * 1000 + ind4 * 100 + ind5 * 10 + ind6;
370-
return negative ? -ind : ind;
370+
return -ind;
371371
}
372372
int ind8 = IterImplNumber.intDigits[iter.buf[++i]];
373373
if (ind8 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
374374
iter.head = i;
375375
ind = ind * 1000000 + ind2 * 100000 + ind3 * 10000 + ind4 * 1000 + ind5 * 100 + ind6 * 10 + ind7;
376-
return negative ? -ind : ind;
376+
return -ind;
377377
}
378378
int ind9 = IterImplNumber.intDigits[iter.buf[++i]];
379379
ind = ind * 10000000 + ind2 * 1000000 + ind3 * 100000 + ind4 * 10000 + ind5 * 1000 + ind6 * 100 + ind7 * 10 + ind8;
380380
iter.head = i;
381381
if (ind9 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
382-
return negative ? -ind : ind;
382+
return -ind;
383383
}
384384
}
385-
return IterImplForStreaming.readIntSlowPath(iter, ind, negative);
385+
return IterImplForStreaming.readIntSlowPath(iter, ind);
386386
}
387387

388388
static final long readLong(final JsonIterator iter, final byte c, final boolean negative) throws IOException {

src/main/java/com/jsoniter/IterImplForStreaming.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -514,29 +514,27 @@ static long readLongSlowPath(final JsonIterator iter, long value, final boolean
514514
}
515515
}
516516

517-
static int readIntSlowPath(final JsonIterator iter, int value, final boolean negative) throws IOException {
517+
static int readIntSlowPath(final JsonIterator iter, int value) throws IOException {
518518
value = -value; // add negatives to avoid redundant checks for Integer.MIN_VALUE on each iteration
519-
int limit = negative ? Integer.MIN_VALUE : -Integer.MAX_VALUE;
520519
int multmin = -214748364; // limit / 10
521520
for (; ; ) {
522521
for (int i = iter.head; i < iter.tail; i++) {
523522
int ind = IterImplNumber.intDigits[iter.buf[i]];
524523
if (ind == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
525524
iter.head = i;
526-
return negative ? value : -value;
525+
return value;
527526
}
528527
if (value < multmin) {
529528
throw iter.reportError("readIntSlowPath", "value is too large for int");
530529
}
531-
value = (value << 3) + (value << 1);
532-
if (value < limit + ind) {
530+
value = (value << 3) + (value << 1) - ind;
531+
if (value >= 0) {
533532
throw iter.reportError("readIntSlowPath", "value is too large for int");
534533
}
535-
value -= ind;
536534
}
537535
if (!IterImpl.loadMore(iter)) {
538536
iter.head = iter.tail;
539-
return negative ? value : -value;
537+
return value;
540538
}
541539
}
542540
}
@@ -607,7 +605,7 @@ static final long readLong(final JsonIterator iter, final byte c, final boolean
607605
return IterImplForStreaming.readLongSlowPath(iter, ind, negative);
608606
}
609607

610-
static final int readInt(final JsonIterator iter, final byte c, final boolean negative) throws IOException {
608+
static final int readInt(final JsonIterator iter, final byte c) throws IOException {
611609
int ind = IterImplNumber.intDigits[c];
612610
if (ind == 0) {
613611
assertNotLeadingZero(iter);
@@ -616,7 +614,7 @@ static final int readInt(final JsonIterator iter, final byte c, final boolean ne
616614
if (ind == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
617615
throw iter.reportError("readInt", "expect 0~9");
618616
}
619-
return IterImplForStreaming.readIntSlowPath(iter, ind, negative);
617+
return IterImplForStreaming.readIntSlowPath(iter, ind);
620618
}
621619

622620
static void assertNotLeadingZero(JsonIterator iter) throws IOException {

src/main/java/com/jsoniter/IterImplNumber.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,15 @@ public static final float readFloat(final JsonIterator iter) throws IOException
7575

7676
public static final int readInt(final JsonIterator iter) throws IOException {
7777
byte c = IterImpl.nextToken(iter);
78-
boolean negative = c == '-';
79-
return IterImpl.readInt(iter, negative ? IterImpl.readByte(iter) : c, negative);
78+
if (c == '-') {
79+
return IterImpl.readInt(iter, IterImpl.readByte(iter));
80+
} else {
81+
int val = IterImpl.readInt(iter, c);
82+
if (val == Integer.MIN_VALUE) {
83+
throw iter.reportError("readInt", "value is too large for int");
84+
}
85+
return -val;
86+
}
8087
}
8188

8289
public static final long readLong(JsonIterator iter) throws IOException {

0 commit comments

Comments
 (0)