Skip to content

Commit bf47ad2

Browse files
committed
remove negative boolean flag for readLong and readDouble
1 parent 696fc4d commit bf47ad2

File tree

3 files changed

+42
-36
lines changed

3 files changed

+42
-36
lines changed

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ static final int readInt(final JsonIterator iter, final byte c) throws IOExcepti
385385
return IterImplForStreaming.readIntSlowPath(iter, ind);
386386
}
387387

388-
static final long readLong(final JsonIterator iter, final byte c, final boolean negative) throws IOException {
388+
static final long readLong(final JsonIterator iter, final byte c) throws IOException {
389389
long ind = IterImplNumber.intDigits[c];
390390
if (ind == 0) {
391391
IterImplForStreaming.assertNotLeadingZero(iter);
@@ -399,60 +399,59 @@ static final long readLong(final JsonIterator iter, final byte c, final boolean
399399
int ind2 = IterImplNumber.intDigits[iter.buf[i]];
400400
if (ind2 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
401401
iter.head = i;
402-
return negative ? -ind : ind;
402+
return -ind;
403403
}
404404
int ind3 = IterImplNumber.intDigits[iter.buf[++i]];
405405
if (ind3 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
406406
iter.head = i;
407407
ind = ind * 10 + ind2;
408-
return negative ? -ind : ind;
408+
return -ind;
409409
}
410410
int ind4 = IterImplNumber.intDigits[iter.buf[++i]];
411411
if (ind4 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
412412
iter.head = i;
413413
ind = ind * 100 + ind2 * 10 + ind3;
414-
return negative ? -ind : ind;
414+
return -ind;
415415
}
416416
int ind5 = IterImplNumber.intDigits[iter.buf[++i]];
417417
if (ind5 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
418418
iter.head = i;
419419
ind = ind * 1000 + ind2 * 100 + ind3 * 10 + ind4;
420-
return negative ? -ind : ind;
420+
return -ind;
421421
}
422422
int ind6 = IterImplNumber.intDigits[iter.buf[++i]];
423423
if (ind6 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
424424
iter.head = i;
425425
ind = ind * 10000 + ind2 * 1000 + ind3 * 100 + ind4 * 10 + ind5;
426-
return negative ? -ind : ind;
426+
return -ind;
427427
}
428428
int ind7 = IterImplNumber.intDigits[iter.buf[++i]];
429429
if (ind7 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
430430
iter.head = i;
431431
ind = ind * 100000 + ind2 * 10000 + ind3 * 1000 + ind4 * 100 + ind5 * 10 + ind6;
432-
return negative ? -ind : ind;
432+
return -ind;
433433
}
434434
int ind8 = IterImplNumber.intDigits[iter.buf[++i]];
435435
if (ind8 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
436436
iter.head = i;
437437
ind = ind * 1000000 + ind2 * 100000 + ind3 * 10000 + ind4 * 1000 + ind5 * 100 + ind6 * 10 + ind7;
438-
return negative ? -ind : ind;
438+
return -ind;
439439
}
440440
int ind9 = IterImplNumber.intDigits[iter.buf[++i]];
441441
ind = ind * 10000000 + ind2 * 1000000 + ind3 * 100000 + ind4 * 10000 + ind5 * 1000 + ind6 * 100 + ind7 * 10 + ind8;
442442
iter.head = i;
443443
if (ind9 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
444-
return negative ? -ind : ind;
444+
return -ind;
445445
}
446446
}
447-
return IterImplForStreaming.readLongSlowPath(iter, ind, negative);
447+
return IterImplForStreaming.readLongSlowPath(iter, ind);
448448
}
449449

450-
static final double readDouble(final JsonIterator iter, final boolean negative) throws IOException {
450+
static final double readDouble(final JsonIterator iter) throws IOException {
451451
int oldHead = iter.head;
452452
try {
453453
try {
454454
long value = IterImplNumber.readLong(iter); // without the dot & sign
455-
value = negative ? -value : value;
456455
if (iter.head == iter.tail) {
457456
return value;
458457
}
@@ -461,29 +460,30 @@ static final double readDouble(final JsonIterator iter, final boolean negative)
461460
iter.head++;
462461
int start = iter.head;
463462
c = iter.buf[iter.head++];
464-
long decimalPart = readLong(iter, c, negative);
463+
long decimalPart = readLong(iter, c);
464+
if (decimalPart == Long.MIN_VALUE) {
465+
return IterImplForStreaming.readDoubleSlowPath(iter);
466+
}
467+
decimalPart = -decimalPart;
465468
int decimalPlaces = iter.head - start;
466469
if (decimalPlaces > 0 && decimalPlaces < IterImplNumber.POW10.length && (iter.head - oldHead) < 10) {
467470
return value + (decimalPart / (double) IterImplNumber.POW10[decimalPlaces]);
468471
} else {
469472
iter.head = oldHead;
470-
double result = IterImplForStreaming.readDoubleSlowPath(iter);
471-
return negative ? -result : result;
473+
return IterImplForStreaming.readDoubleSlowPath(iter);
472474
}
473475
} else {
474476
return value;
475477
}
476478
} finally {
477479
if (iter.head < iter.tail && (iter.buf[iter.head] == 'e' || iter.buf[iter.head] == 'E')) {
478480
iter.head = oldHead;
479-
double result = IterImplForStreaming.readDoubleSlowPath(iter);
480-
return negative ? -result : result;
481+
return IterImplForStreaming.readDoubleSlowPath(iter);
481482
}
482483
}
483484
} catch (JsonException e) {
484485
iter.head = oldHead;
485-
double result = IterImplForStreaming.readDoubleSlowPath(iter);
486-
return negative ? -result : result;
486+
return IterImplForStreaming.readDoubleSlowPath(iter);
487487
}
488488
}
489489
}

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -487,29 +487,27 @@ public final static int readStringSlowPath(JsonIterator iter, int j) throws IOEx
487487
}
488488
}
489489

490-
static long readLongSlowPath(final JsonIterator iter, long value, final boolean negative) throws IOException {
490+
static long readLongSlowPath(final JsonIterator iter, long value) throws IOException {
491491
value = -value; // add negatives to avoid redundant checks for Long.MIN_VALUE on each iteration
492-
long limit = negative ? Long.MIN_VALUE : -Long.MAX_VALUE;
493492
long multmin = -922337203685477580L; // limit / 10
494493
for (; ; ) {
495494
for (int i = iter.head; i < iter.tail; i++) {
496495
int ind = IterImplNumber.intDigits[iter.buf[i]];
497496
if (ind == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
498497
iter.head = i;
499-
return negative ? value : -value;
498+
return value;
500499
}
501500
if (value < multmin) {
502501
throw iter.reportError("readLongSlowPath", "value is too large for long");
503502
}
504-
value = (value << 3) + (value << 1);
505-
if (value < limit + ind) {
503+
value = (value << 3) + (value << 1) - ind;
504+
if (value >= 0) {
506505
throw iter.reportError("readLongSlowPath", "value is too large for long");
507506
}
508-
value -= ind;
509507
}
510508
if (!IterImpl.loadMore(iter)) {
511509
iter.head = iter.tail;
512-
return negative ? value : -value;
510+
return value;
513511
}
514512
}
515513
}
@@ -588,12 +586,11 @@ public static final String readNumber(final JsonIterator iter) throws IOExceptio
588586
}
589587
}
590588

591-
static final double readDouble(final JsonIterator iter, final boolean negative) throws IOException {
592-
double result = readDoubleSlowPath(iter);
593-
return negative ? -result : result;
589+
static final double readDouble(final JsonIterator iter) throws IOException {
590+
return readDoubleSlowPath(iter);
594591
}
595592

596-
static final long readLong(final JsonIterator iter, final byte c, final boolean negative) throws IOException {
593+
static final long readLong(final JsonIterator iter, final byte c) throws IOException {
597594
long ind = IterImplNumber.intDigits[c];
598595
if (ind == 0) {
599596
assertNotLeadingZero(iter);
@@ -602,7 +599,7 @@ static final long readLong(final JsonIterator iter, final byte c, final boolean
602599
if (ind == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
603600
throw iter.reportError("readLong", "expect 0~9");
604601
}
605-
return IterImplForStreaming.readLongSlowPath(iter, ind, negative);
602+
return IterImplForStreaming.readLongSlowPath(iter, ind);
606603
}
607604

608605
static final int readInt(final JsonIterator iter, final byte c) throws IOException {

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ class IterImplNumber {
6262
}
6363

6464
public static final double readDouble(final JsonIterator iter) throws IOException {
65-
boolean negative = IterImpl.nextToken(iter) == '-';
66-
if (!negative) {
65+
final byte c = IterImpl.nextToken(iter);
66+
if (c == '-') {
67+
return -IterImpl.readDouble(iter);
68+
} else {
6769
iter.unreadByte();
70+
return IterImpl.readDouble(iter);
6871
}
69-
return IterImpl.readDouble(iter, negative);
7072
}
7173

7274
public static final float readFloat(final JsonIterator iter) throws IOException {
@@ -88,7 +90,14 @@ public static final int readInt(final JsonIterator iter) throws IOException {
8890

8991
public static final long readLong(JsonIterator iter) throws IOException {
9092
byte c = IterImpl.nextToken(iter);
91-
boolean negative = c == '-';
92-
return IterImpl.readLong(iter, negative ? IterImpl.readByte(iter) : c, negative);
93+
if (c == '-') {
94+
return IterImpl.readLong(iter, IterImpl.readByte(iter));
95+
} else {
96+
long val = IterImpl.readLong(iter, c);
97+
if (val == Long.MIN_VALUE) {
98+
throw iter.reportError("readLong", "value is too large for long");
99+
}
100+
return -val;
101+
}
93102
}
94103
}

0 commit comments

Comments
 (0)