Skip to content

Commit 0625e83

Browse files
committed
Code clean up
1 parent 466c0ce commit 0625e83

File tree

2 files changed

+14
-28
lines changed
  • jsoniter-scala-core

2 files changed

+14
-28
lines changed

jsoniter-scala-core/jvm/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonReader.scala

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,9 +1717,8 @@ final class JsonReader private[jsoniter_scala](
17171717
} else parseYearWithByte(t, loadMoreOrError(pos))
17181718

17191719
private[this] def parseNon4DigitYearWithByte(t: Byte, maxDigits: Int, y: Int, p: Int): Int = {
1720-
val bs = y + 0x30303030
1721-
val b1 = bs.toByte
1722-
if (b1 != '-' && b1 != '+') fourDigitYearWithByteError(t, p, bs)
1720+
val b1 = (y + 0x30).toByte
1721+
if (b1 != '-' && b1 != '+') fourDigitYearWithByteError(t, p, y)
17231722
var pos = p + 1
17241723
var buf = this.buf
17251724
var year = ByteArrayAccess.getInt(buf, pos) - 0x30303030
@@ -3809,17 +3808,11 @@ final class JsonReader private[jsoniter_scala](
38093808
private[this] def isLeap(year: Int): Boolean =
38103809
(year & 0x3) == 0 && (year * -1030792151 - 2061584303 > -1975684958 || (year & 0xF) == 0) // year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
38113810

3812-
private[this] def fourDigitYearWithByteError(t: Byte, pos: Int, bs: Int): Nothing = {
3813-
val b1 = bs.toByte
3814-
if (b1 >= '0' && b1 <= '9') {
3815-
val b2 = (bs >> 8).toByte
3816-
val b3 = (bs >> 16).toByte
3817-
val b4 = (bs >> 24).toByte
3818-
if (b2 < '0' || b2 > '9') digitError(pos + 1)
3819-
if (b3 < '0' || b3 > '9') digitError(pos + 2)
3820-
if (b4 < '0' || b4 > '9') digitError(pos + 3)
3821-
tokenError(t, pos + 4)
3822-
} else decodeError("expected '-' or '+' or digit", pos)
3811+
private[this] def fourDigitYearWithByteError(t: Byte, pos: Int, y: Int): Nothing = {
3812+
val m = (y + 0x76767676 | y) & 0x80808080
3813+
if (m == 0) tokenError(t, pos + 4)
3814+
else if (m.toByte != 0) decodeError("expected '-' or '+' or digit", pos)
3815+
else digitError((java.lang.Integer.numberOfTrailingZeros(m) >> 3) + pos)
38233816
}
38243817

38253818
private[this] def digitError(pos: Int): Nothing = decodeError("expected digit", pos)

jsoniter-scala-core/native/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonReader.scala

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,9 +1717,8 @@ final class JsonReader private[jsoniter_scala](
17171717
} else parseYearWithByte(t, loadMoreOrError(pos))
17181718

17191719
private[this] def parseNon4DigitYearWithByte(t: Byte, maxDigits: Int, y: Int, p: Int): Int = {
1720-
val bs = y + 0x30303030
1721-
val b1 = bs.toByte
1722-
if (b1 != '-' && b1 != '+') fourDigitYearWithByteError(t, p, bs)
1720+
val b1 = (y + 0x30).toByte
1721+
if (b1 != '-' && b1 != '+') fourDigitYearWithByteError(t, p, y)
17231722
var pos = p + 1
17241723
var buf = this.buf
17251724
var year = ByteArrayAccess.getInt(buf, pos) - 0x30303030
@@ -3805,17 +3804,11 @@ final class JsonReader private[jsoniter_scala](
38053804
private[this] def isLeap(year: Int): Boolean =
38063805
(year & 0x3) == 0 && (year * -1030792151 - 2061584303 > -1975684958 || (year & 0xF) == 0) // year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
38073806

3808-
private[this] def fourDigitYearWithByteError(t: Byte, pos: Int, bs: Int): Nothing = {
3809-
val b1 = bs.toByte
3810-
if (b1 >= '0' && b1 <= '9') {
3811-
val b2 = (bs >> 8).toByte
3812-
val b3 = (bs >> 16).toByte
3813-
val b4 = (bs >> 24).toByte
3814-
if (b2 < '0' || b2 > '9') digitError(pos + 1)
3815-
if (b3 < '0' || b3 > '9') digitError(pos + 2)
3816-
if (b4 < '0' || b4 > '9') digitError(pos + 3)
3817-
tokenError(t, pos + 4)
3818-
} else decodeError("expected '-' or '+' or digit", pos)
3807+
private[this] def fourDigitYearWithByteError(t: Byte, pos: Int, y: Int): Nothing = {
3808+
val m = (y + 0x76767676 | y) & 0x80808080
3809+
if (m == 0) tokenError(t, pos + 4)
3810+
else if (m.toByte != 0) decodeError("expected '-' or '+' or digit", pos)
3811+
else digitError((java.lang.Integer.numberOfTrailingZeros(m) >> 3) + pos)
38193812
}
38203813

38213814
private[this] def digitError(pos: Int): Nothing = decodeError("expected digit", pos)

0 commit comments

Comments
 (0)