scoop:scoop.json:0.1.0
src/integral_number.scoop
Selected production source · src/integral_number.scoop
1
package scoop.json3
/** Interprets an already validated JSON number; no decimal expansion or floating point is needed. */4
internal fun readJsonIntegralLong(text: String): Long {5
val source = text.iterator()6
var current = source.next()7
val negative = current == Some('-')8
if (negative) current = source.next()9
var index = if (negative) 1L else 0L10
var digits = 0L11
var whole = 0L12
var fraction = false13
var first = -1L14
var last = -1L15
var firstIndex = 0L16
var lastIndex = 0L17
var hasExponent = false18
while (true) {19
val character = when (current) { case Some(val value) -> value; case None -> break }20
if (character == 'e' || character == 'E') { hasExponent = true; break }21
if (character == '.') fraction = true22
else {23
if (!fraction) whole += 1L24
if (character != '0') {25
if (first < 0L) { first = digits; firstIndex = index }26
last = digits27
lastIndex = index28
}29
digits += 1L30
}31
index += 1L32
current = source.next()33
}34
if (first < 0L) return 0L35
val significant = last - first + 1L36
if (significant > 19L) throw integralLongError()37
val bound = if (text.byteLength > Long.MAX_VALUE - 19L) Long.MAX_VALUE else text.byteLength + 19L38
val exponent = if (hasExponent) readJsonIntegralExponent(source, bound) else 0L39
val base = whole - first40
if (exponent > 0L && base > Long.MAX_VALUE - exponent) throw integralLongError()41
if (exponent < 0L && base < Long.MIN_VALUE - exponent) throw integralLongError()42
val length = base + exponent43
if (length < significant || length > 19L) throw integralLongError()44
val limit = if (negative) 9223372036854775808UL else 9223372036854775807UL45
var magnitude = 0UL46
index = 0L47
for (character in text) {48
if (index > lastIndex) break49
if (index >= firstIndex && character != '.')50
magnitude = appendJsonIntegralDigit(magnitude, (character.code - 48).toULong(), limit)51
index += 1L52
}53
for (_ in significant..<length) magnitude = appendJsonIntegralDigit(magnitude, 0UL, limit)54
return if (negative) -magnitude.toLong() else magnitude.toLong()55
}57
private fun readJsonIntegralExponent(source: Iterator<Char>, limit: Long): Long {58
var current = source.next()59
val negative = current == Some('-')60
if (negative || current == Some('+')) current = source.next()61
// Beyond this magnitude, a nonzero mantissa cannot cancel the exponent into a Long.62
var exponent = 0L63
while (true) {64
val character = when (current) { case Some(val value) -> value; case None -> break }65
val digit = (character.code - 48).toLong()66
if (exponent > (limit - digit) / 10L) throw integralLongError()67
exponent = exponent * 10L + digit68
current = source.next()69
}70
return if (negative) -exponent else exponent71
}73
private fun appendJsonIntegralDigit(value: ULong, digit: ULong, limit: ULong): ULong {74
if (value > (limit - digit) / 10UL) throw integralLongError()75
return value * 10UL + digit76
}78
private fun integralLongError(): DecodingException = DecodingException("", "number is not an exact Long integer")