Scoop API

scoop:scoop.json:0.1.0

src/integral_number.scoop

Selected production source · src/integral_number.scoop

1package scoop.json
3/** Interprets an already validated JSON number; no decimal expansion or floating point is needed. */
4internal 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 0L
10 var digits = 0L
11 var whole = 0L
12 var fraction = false
13 var first = -1L
14 var last = -1L
15 var firstIndex = 0L
16 var lastIndex = 0L
17 var hasExponent = false
18 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 = true
22 else {
23 if (!fraction) whole += 1L
24 if (character != '0') {
25 if (first < 0L) { first = digits; firstIndex = index }
26 last = digits
27 lastIndex = index
28 }
29 digits += 1L
30 }
31 index += 1L
32 current = source.next()
33 }
34 if (first < 0L) return 0L
35 val significant = last - first + 1L
36 if (significant > 19L) throw integralLongError()
37 val bound = if (text.byteLength > Long.MAX_VALUE - 19L) Long.MAX_VALUE else text.byteLength + 19L
38 val exponent = if (hasExponent) readJsonIntegralExponent(source, bound) else 0L
39 val base = whole - first
40 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 + exponent
43 if (length < significant || length > 19L) throw integralLongError()
44 val limit = if (negative) 9223372036854775808UL else 9223372036854775807UL
45 var magnitude = 0UL
46 index = 0L
47 for (character in text) {
48 if (index > lastIndex) break
49 if (index >= firstIndex && character != '.')
50 magnitude = appendJsonIntegralDigit(magnitude, (character.code - 48).toULong(), limit)
51 index += 1L
52 }
53 for (_ in significant..<length) magnitude = appendJsonIntegralDigit(magnitude, 0UL, limit)
54 return if (negative) -magnitude.toLong() else magnitude.toLong()
57private 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 = 0L
63 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 + digit
68 current = source.next()
69 }
70 return if (negative) -exponent else exponent
73private fun appendJsonIntegralDigit(value: ULong, digit: ULong, limit: ULong): ULong {
74 if (value > (limit - digit) / 10UL) throw integralLongError()
75 return value * 10UL + digit
78private fun integralLongError(): DecodingException = DecodingException("", "number is not an exact Long integer")