Scoop API

scoop:scoop.std:0.1.0

src/text/number_format.scoop

Selected production source · src/text/number_format.scoop

1package scoop.text
3/**
4 * Text could not be parsed as the requested numeric or strict Boolean value.
5 */
6public class NumberFormatException public constructor(message: String) : Exception(Some(message))
8internal fun requireTextRadix(radix: Int) {
9 if (radix < 2 || radix > 36) throw IllegalArgumentException(Some("radix must be in 2..36"))
12internal fun textDigit(value: Char): Int {
13 val code = value.code
14 if (code >= 48 && code <= 57) return code - 48
15 if (code >= 65 && code <= 90) return code - 65 + 10
16 if (code >= 97 && code <= 122) return code - 97 + 10
17 return -1
20internal fun <T> requireParsedNumber(value: T?): T = when (value) {
21 case Some(val result) -> result
22 case None -> throw NumberFormatException("invalid number")
25/**
26 * Accepts exactly lowercase true or false; all other text returns None.
27 */
28public fun String.toBooleanStrictOrNone(): Boolean? {
29 if (this == "true") return Some(true)
30 if (this == "false") return Some(false)
31 return None
34/**
35 * Accepts exactly lowercase true or false; other text throws NumberFormatException.
36 */
37public fun String.toBooleanStrict(): Boolean = requireParsedNumber<Boolean>(toBooleanStrictOrNone())