stage2 更多的工具库

This commit is contained in:
zzy committed 2026-09-17 22:32:36 +08:00
1 parent 4141e7d574
commit 16a1f030d4
15 files changed
+1914 -109

No files matched your search

+37 -36
View File
@@ -17,23 +17,23 @@ fn is_control(c: u8): bool {
}
fn is_space(c: u8): bool {
ret c == 32 || (c >= 9 && c <= 13);
ret c == ' ' || (c >= '\t' && c <= '\r');
}
fn is_blank(c: u8): bool {
ret c == 32 || c == 9;
ret c == ' ' || c == '\t';
}
fn is_digit(c: u8): bool {
ret c >= 48 && c <= 57;
ret c >= '0' && c <= '9';
}
fn is_lower(c: u8): bool {
ret c >= 97 && c <= 122;
ret c >= 'a' && c <= 'z';
}
fn is_upper(c: u8): bool {
ret c >= 65 && c <= 90;
ret c >= 'A' && c <= 'Z';
}
fn is_alpha(c: u8): bool {
@@ -45,15 +45,15 @@ fn is_alnum(c: u8): bool {
}
fn is_hex_digit(c: u8): bool {
ret is_digit(c) || (c >= 65 && c <= 70) || (c >= 97 && c <= 102);
ret is_digit(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
}
fn is_print(c: u8): bool {
ret c >= 32 && c <= 126;
ret c >= ' ' && c <= '~';
}
fn is_graph(c: u8): bool {
ret c >= 33 && c <= 126;
ret c >= '!' && c <= '~';
}
fn is_punct(c: u8): bool {
@@ -61,12 +61,12 @@ fn is_punct(c: u8): bool {
}
fn to_lower(c: u8): u8 {
if is_upper(c) { ret c + 32; }
if is_upper(c) { ret c + ('a' - 'A'); }
ret c;
}
fn to_upper(c: u8): u8 {
if is_lower(c) { ret c - 32; }
if is_lower(c) { ret c - ('a' - 'A'); }
ret c;
}
@@ -78,11 +78,11 @@ fn digit_value(c: u8, radix: u8): DigitResult {
var value: u8 = 0;
if is_digit(c) {
value = c - 48;
value = c - '0';
} else if is_upper(c) {
value = c - 65 + 10;
value = c - 'A' + 10;
} else if is_lower(c) {
value = c - 97 + 10;
value = c - 'a' + 10;
} else {
ret .Err(make(.InvalidDigit));
}
@@ -94,9 +94,9 @@ fn digit_value(c: u8, radix: u8): DigitResult {
// 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);
if value < 10 { ret .Ok(value + '0'); }
if uppercase { ret .Ok(value - 10 + 'A'); }
ret .Ok(value - 10 + 'a');
}
fn expect_digit(result: DigitResult, expected: u8) {
@@ -141,27 +141,28 @@ fn test_all_bytes() {
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),
@assert(is_print(c) == (c >= ' ' && c <= '~'), "print boundary");
@assert(is_graph(c) == (c >= '!' && c <= '~'), "graph boundary");
@assert(is_blank(c) == (c == '\t' || c == ' '), "blank set");
@assert(is_space(c) == (c == '\t' || c == '\n' || c == '\x0B' || c == '\x0C' ||
c == '\r' || c == ' '),
"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)),
@assert(is_digit(c) == (c >= '0' && c <= '9'), "digit set");
@assert(is_upper(c) == (c >= 'A' && c <= 'Z'), "upper set");
@assert(is_lower(c) == (c >= 'a' && c <= 'z'), "lower set");
@assert(is_hex_digit(c) == ((c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'F') ||
(c >= 'a' && c <= 'f')),
"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_lower(c) == c + ('a' - 'A'), "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_upper(c) == c - ('a' - 'A'), "lower to upper");
@assert(to_lower(to_upper(c)) == c, "case round trip");
} else {
@assert(to_lower(c) == c, "lower preserves non-letter");
@@ -192,20 +193,20 @@ fn test_digits() {
if value < 10 {
@assert(lower == upper, "decimal digits ignore case");
} else {
@assert(lower == upper + 32, "letter digit case distance");
@assert(lower == upper + ('a' - 'A'), "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_error(digit_value('0', 0), true);
expect_error(digit_value('0', 1), true);
expect_error(digit_value('0', 37), true);
expect_error(digit_value('/', 10), false);
expect_error(digit_value(':', 10), false);
expect_error(digit_value('2', 2), false);
expect_error(digit_value('g', 16), false);
expect_range_error(digit_char(36, false));
expect_range_error(digit_char(255, true));
}