Scoop API

scoop:scoop.std:0.1.0

src/text/signed_text.scoop

Selected production source · src/text/signed_text.scoop

1package scoop.text
3import scoop.map
5/**
6 * Parses a Byte in radix 2..36, returning None for invalid digits or out-of-range values. An
7 * invalid radix throws IllegalArgumentException.
8 */
9public fun String.toByteOrNone(radix: Int = 10): Byte? =
10 parseSignedInteger(this, radix, Int8.MAX_VALUE.toULong()).map<Long, Int8> { value -> value.toByte() }
12/**
13 * Parses a Byte in radix 2..36; invalid or out-of-range text throws NumberFormatException and an
14 * invalid radix throws IllegalArgumentException.
15 */
16public fun String.toByte(radix: Int = 10): Byte = requireParsedNumber<Int8>(toByteOrNone(radix))
18/**
19 * Parses a Short in radix 2..36, returning None for invalid digits or out-of-range values. An
20 * invalid radix throws IllegalArgumentException.
21 */
22public fun String.toShortOrNone(radix: Int = 10): Short? =
23 parseSignedInteger(this, radix, Int16.MAX_VALUE.toULong()).map<Long, Int16> { value -> value.toShort() }
25/**
26 * Parses a Short in radix 2..36; invalid or out-of-range text throws NumberFormatException and an
27 * invalid radix throws IllegalArgumentException.
28 */
29public fun String.toShort(radix: Int = 10): Short = requireParsedNumber<Int16>(toShortOrNone(radix))
31/**
32 * Parses a Int in radix 2..36, returning None for invalid digits or out-of-range values. An
33 * invalid radix throws IllegalArgumentException.
34 */
35public fun String.toIntOrNone(radix: Int = 10): Int? =
36 parseSignedInteger(this, radix, Int.MAX_VALUE.toULong()).map<Long, Int> { value -> value.toInt() }
38/**
39 * Parses a Int in radix 2..36; invalid or out-of-range text throws NumberFormatException and an
40 * invalid radix throws IllegalArgumentException.
41 */
42public fun String.toInt(radix: Int = 10): Int = requireParsedNumber<Int>(toIntOrNone(radix))
44/**
45 * Parses a Long in radix 2..36, returning None for invalid digits or out-of-range values. An
46 * invalid radix throws IllegalArgumentException.
47 */
48public fun String.toLongOrNone(radix: Int = 10): Long? =
49 parseSignedInteger(this, radix, Long.MAX_VALUE.toULong()).map<Long, Long> { value -> value.toLong() }
51/**
52 * Parses a Long in radix 2..36; invalid or out-of-range text throws NumberFormatException and an
53 * invalid radix throws IllegalArgumentException.
54 */
55public fun String.toLong(radix: Int = 10): Long = requireParsedNumber<Long>(toLongOrNone(radix))