Scoop API

scoop:scoop.std:0.1.0

src/text/decimal_scan.scoop

Selected production source · src/text/decimal_scan.scoop

1package scoop.text
3internal struct ParsedDecimal(
4 val magnitude: DecimalMagnitude, val negative: Boolean,
5 val digits: Long, val order: Long, val sticky: Boolean
6)
8internal fun scanDecimal(text: String, digitLimit: Long): ParsedDecimal? {
9 val source = text.iterator()
10 var current = source.next()
11 val negative = current == Some('-')
12 if (negative || current == Some('+')) current = source.next()
13 val magnitude = DecimalMagnitude()
14 var dot = false
15 var anyDigit = false
16 var fractional = 0L
17 var significant = 0L
18 var kept = 0L
19 var sticky = false
20 while (true) {
21 val character = when (current) { case None -> break; case Some(val value) -> value }
22 if (character == '.' && !dot) {
23 dot = true
24 } else if (character >= '0' && character <= '9') {
25 anyDigit = true
26 if (dot) fractional += 1L
27 val digit = (character.code - 48).toUInt()
28 if (significant != 0L || digit != 0u) {
29 significant += 1L
30 if (kept < digitLimit) {
31 magnitude.multiplyAdd(10u, digit)
32 kept += 1L
33 } else if (digit != 0u) {
34 sticky = true
35 }
36 }
37 } else {
38 break
39 }
40 current = source.next()
41 }
42 if (!anyDigit) return None
43 var exponent = 0uL
44 var exponentNegative = false
45 if (current == Some('e') || current == Some('E')) {
46 current = source.next()
47 exponentNegative = current == Some('-')
48 if (exponentNegative || current == Some('+')) current = source.next()
49 if (current == None) return None
50 while (true) {
51 val character = when (current) { case None -> break; case Some(val value) -> value }
52 if (character < '0' || character > '9') return None
53 exponent = appendDecimalExponent(exponent, (character.code - 48).toULong())
54 current = source.next()
55 }
56 }
57 if (current != None) return None
58 val order = if (significant == 0L) 0L
59 else decimalOrder(exponent, exponentNegative, significant - 1L - fractional)
60 return Some(ParsedDecimal(magnitude, negative, kept, order, sticky))