stage2 更多的工具函数 stage1支持更多的内置函数

This commit is contained in:
zzy committed 2026-09-17 18:47:56 +08:00
1 parent 1a6df012cd
commit b8e2bba140
10 files changed
+1713 -7

No files matched your search

+216
View File
@@ -0,0 +1,216 @@
// Expect retcode = 0
// ASCII 字符工具。输入和输出都是单个字节,不解释 UTF-8 或本地化规则。
type .{ .Error, .make } = @import("error.spl");
type DigitResult = enum {
Ok: u8,
Err: Error,
}
fn is_ascii(c: u8): bool {
ret c <= 127;
}
fn is_control(c: u8): bool {
ret c <= 31 || c == 127;
}
fn is_space(c: u8): bool {
ret c == 32 || (c >= 9 && c <= 13);
}
fn is_blank(c: u8): bool {
ret c == 32 || c == 9;
}
fn is_digit(c: u8): bool {
ret c >= 48 && c <= 57;
}
fn is_lower(c: u8): bool {
ret c >= 97 && c <= 122;
}
fn is_upper(c: u8): bool {
ret c >= 65 && c <= 90;
}
fn is_alpha(c: u8): bool {
ret is_lower(c) || is_upper(c);
}
fn is_alnum(c: u8): bool {
ret is_alpha(c) || is_digit(c);
}
fn is_hex_digit(c: u8): bool {
ret is_digit(c) || (c >= 65 && c <= 70) || (c >= 97 && c <= 102);
}
fn is_print(c: u8): bool {
ret c >= 32 && c <= 126;
}
fn is_graph(c: u8): bool {
ret c >= 33 && c <= 126;
}
fn is_punct(c: u8): bool {
ret is_graph(c) && !is_alnum(c);
}
fn to_lower(c: u8): u8 {
if is_upper(c) { ret c + 32; }
ret c;
}
fn to_upper(c: u8): u8 {
if is_lower(c) { ret c - 32; }
ret c;
}
// radix 必须位于 2..36。大小写字母均表示 10..35。
fn digit_value(c: u8, radix: u8): DigitResult {
if radix < 2 || radix > 36 {
ret .Err(make(.InvalidArgument));
}
var value: u8 = 0;
if is_digit(c) {
value = c - 48;
} else if is_upper(c) {
value = c - 65 + 10;
} else if is_lower(c) {
value = c - 97 + 10;
} else {
ret .Err(make(.InvalidDigit));
}
if value >= radix { ret .Err(make(.InvalidDigit)); }
ret .Ok(value);
}
// value 必须位于 0..35。uppercase 只影响 10..35 的字母形式。
fn digit_char(value: u8, uppercase: bool): DigitResult {
if value >= 36 { ret .Err(make(.OutOfRange)); }
if value < 10 { ret .Ok(value + 48); }
if uppercase { ret .Ok(value - 10 + 65); }
ret .Ok(value - 10 + 97);
}
fn expect_digit(result: DigitResult, expected: u8) {
match result {
.Ok(value) => { @assert(value == expected, "digit value"); },
.Err(error) => { @assert(false, "unexpected digit error"); },
}
}
fn expect_error(result: DigitResult, expected_invalid_argument: bool) {
match result {
.Ok(value) => { @assert(false, "expected digit error"); },
.Err(error) => {
match error.kind {
.InvalidArgument => {
@assert(expected_invalid_argument, "unexpected invalid radix");
},
.InvalidDigit => {
@assert(!expected_invalid_argument, "unexpected invalid digit");
},
_ => { @assert(false, "unexpected digit error kind"); },
}
},
}
}
fn expect_range_error(result: DigitResult) {
match result {
.Ok(value) => { @assert(false, "expected digit range error"); },
.Err(error) => {
match error.kind {
.OutOfRange => {},
_ => { @assert(false, "unexpected digit range error kind"); },
}
},
}
}
fn test_all_bytes() {
var i: usize = 0;
while i < 256 {
var c = i as u8;
@assert(is_ascii(c) == (i <= 127), "ASCII boundary");
@assert(is_control(c) == (i <= 31 || i == 127), "control boundary");
@assert(is_print(c) == (i >= 32 && i <= 126), "print boundary");
@assert(is_graph(c) == (i >= 33 && i <= 126), "graph boundary");
@assert(is_blank(c) == (i == 9 || i == 32), "blank set");
@assert(is_space(c) == (i == 9 || i == 10 || i == 11 || i == 12 || i == 13 || i == 32),
"space set");
@assert(is_digit(c) == (i >= 48 && i <= 57), "digit set");
@assert(is_upper(c) == (i >= 65 && i <= 90), "upper set");
@assert(is_lower(c) == (i >= 97 && i <= 122), "lower set");
@assert(is_hex_digit(c) == ((i >= 48 && i <= 57) ||
(i >= 65 && i <= 70) ||
(i >= 97 && i <= 102)),
"hex digit set");
@assert(is_alpha(c) == (is_upper(c) || is_lower(c)), "alpha relation");
@assert(is_alnum(c) == (is_alpha(c) || is_digit(c)), "alnum relation");
@assert(is_punct(c) == (is_graph(c) && !is_alnum(c)), "punct relation");
if is_upper(c) {
@assert(to_lower(c) == c + 32, "upper to lower");
@assert(to_upper(to_lower(c)) == c, "case round trip");
} else if is_lower(c) {
@assert(to_upper(c) == c - 32, "lower to upper");
@assert(to_lower(to_upper(c)) == c, "case round trip");
} else {
@assert(to_lower(c) == c, "lower preserves non-letter");
@assert(to_upper(c) == c, "upper preserves non-letter");
}
i = i + 1;
}
}
fn test_digits() {
var radix: usize = 2;
while radix <= 36 {
var value: usize = 0;
while value < radix {
var lower: u8 = 0;
match digit_char(value as u8, false) {
.Ok(c) => { lower = c; },
.Err(error) => { @assert(false, "lower digit generation"); },
}
expect_digit(digit_value(lower, radix as u8), value as u8);
var upper: u8 = 0;
match digit_char(value as u8, true) {
.Ok(c) => { upper = c; },
.Err(error) => { @assert(false, "upper digit generation"); },
}
expect_digit(digit_value(upper, radix as u8), value as u8);
if value < 10 {
@assert(lower == upper, "decimal digits ignore case");
} else {
@assert(lower == upper + 32, "letter digit case distance");
}
value = value + 1;
}
radix = radix + 1;
}
expect_error(digit_value(48, 0), true);
expect_error(digit_value(48, 1), true);
expect_error(digit_value(48, 37), true);
expect_error(digit_value(47, 10), false);
expect_error(digit_value(58, 10), false);
expect_error(digit_value(50, 2), false);
expect_error(digit_value(103, 16), false);
expect_range_error(digit_char(36, false));
expect_range_error(digit_char(255, true));
}
fn main() {
test_all_bytes();
test_digits();
}