stou.ha (3963B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use ascii; 5 use encoding::utf8; 6 use strings; 7 use types; 8 9 fn rune_to_integer(r: rune) (u64 | void) = { 10 if (ascii::isdigit(r)) 11 return (r: u32 - '0'): u64 12 else if (ascii::isalpha(r) && ascii::islower(r)) 13 return (r: u32 - 'a'): u64 + 10 14 else if (ascii::isalpha(r) && ascii::isupper(r)) 15 return (r: u32 - 'A'): u64 + 10; 16 }; 17 18 fn parseint(s: str, base: base) ((bool, u64) | invalid | overflow) = { 19 if (base == base::DEFAULT) { 20 base = base::DEC; 21 }; 22 assert(base == 2 || base == 8 || base == 10 || base == 16); 23 24 if (len(s) == 0) { 25 return 0: invalid; 26 }; 27 28 let buf = strings::toutf8(s); 29 let i = 0z; 30 31 let sign = buf[i] == '-'; 32 if (sign || buf[i] == '+') { 33 i += 1; 34 }; 35 36 // Require at least one digit. 37 if (i == len(buf)) { 38 return i: invalid; 39 }; 40 41 let n = 0u64; 42 for (i < len(buf); i += 1) { 43 const digit = match (rune_to_integer(buf[i]: rune)) { 44 case void => 45 return i: invalid; 46 case let d: u64 => 47 yield d; 48 }; 49 50 if (digit >= base) { 51 return i: invalid; 52 }; 53 54 const old = n; 55 56 n *= base; 57 n += digit; 58 59 if (n < old) { 60 return overflow; 61 }; 62 }; 63 return (sign, n); 64 }; 65 66 // Converts a string to a u64. Returns [[invalid]] if the string is empty or 67 // contains invalid characters. Returns [[overflow]] if the number is too large 68 // to be represented by a u64. 69 export fn stou64(s: str, base: base = base::DEC) (u64 | invalid | overflow) = { 70 let (sign, u) = parseint(s, base)?; 71 if (sign) { 72 return overflow; 73 }; 74 return u; 75 }; 76 77 fn stoumax(s: str, base: base, max: u64) (u64 | invalid | overflow) = { 78 const n = stou64(s, base)?; 79 if (n > max) { 80 return overflow; 81 }; 82 return n; 83 }; 84 85 // Converts a string to a u32. Returns [[invalid]] if the string is empty or 86 // contains invalid characters. Returns [[overflow]] if the number is too large 87 // to be represented by a u32. 88 export fn stou32(s: str, base: base = base::DEC) (u32 | invalid | overflow) = 89 stoumax(s, base, types::U32_MAX)?: u32; 90 91 // Converts a string to a u16. Returns [[invalid]] if the string is empty or 92 // contains invalid characters. Returns [[overflow]] if the number is too large 93 // to be represented by a u16. 94 export fn stou16(s: str, base: base = base::DEC) (u16 | invalid | overflow) = 95 stoumax(s, base, types::U16_MAX)?: u16; 96 97 // Converts a string to a u8. Returns [[invalid]] if the string is empty or 98 // contains invalid characters. Returns [[overflow]] if the number is too large 99 // to be represented by a u8. 100 export fn stou8(s: str, base: base = base::DEC) (u8 | invalid | overflow) = 101 stoumax(s, base, types::U8_MAX)?: u8; 102 103 // Converts a string to a uint in the given base. Returns [[invalid]] if the 104 // string is empty or contains invalid characters. Returns [[overflow]] if the 105 // number is too large to be represented by a uint. 106 export fn stou(s: str, base: base = base::DEC) (uint | invalid | overflow) = 107 stoumax(s, base, types::UINT_MAX)?: uint; 108 109 // Converts a string to a size. Returns [[invalid]] if the string is empty or 110 // contains invalid characters. Returns [[overflow]] if the number is too large 111 // to be represented by a size. 112 export fn stoz(s: str, base: base = base::DEC) (size | invalid | overflow) = 113 stoumax(s, base, types::SIZE_MAX)?: size; 114 115 @test fn stou() void = { 116 assert(stou64("") as invalid == 0); 117 assert(stou64("+") as invalid == 1); 118 assert(stou64("+a") as invalid == 1); 119 assert(stou64("abc") as invalid == 0); 120 assert(stou64("1a") as invalid == 1); 121 122 assert(stou64("18446744073709551616") is overflow); 123 assert(stou64("184467440737095516150") is overflow); 124 assert(stou64("-1") is overflow); 125 126 assert(stou64("0") as u64 == 0); 127 assert(stou64("1") as u64 == 1); 128 assert(stou64("18446744073709551615") as u64 == 18446744073709551615); 129 }; 130 131 @test fn stou_bases() void = { 132 assert(stou64("7f", 16) as u64 == 0x7f); 133 assert(stou64("7F", 16) as u64 == 0x7f); 134 assert(stou64("37", 8) as u64 == 0o37); 135 assert(stou64("110101", 2) as u64 == 0b110101); 136 };