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