Rollup merge of #111009 - scottmcm:ascii-char, r=BurntSushi

Add `ascii::Char` (ACP#179)

ACP second: https://github.com/rust-lang/libs-team/issues/179#issuecomment-1527900570
New tracking issue: https://github.com/rust-lang/rust/issues/110998

For now this is an `enum` as `@kupiakos` [suggested](https://github.com/rust-lang/libs-team/issues/179#issuecomment-1527959724), with the variants under a different feature flag.

There's lots more things that could be added here, and place for further doc updates, but this seems like a plausible starting point PR.

I've gone through and put an `as_ascii` next to every `is_ascii`: on `u8`, `char`, `[u8]`, and `str`.

As a demonstration, made a commit updating some formatting code to use this: https://github.com/scottmcm/rust/commit/ascii-char-in-fmt (I don't want to include that in this PR, though, because that brings in perf questions that don't exist if this is just adding new unstable APIs.)
This commit is contained in:
Matthias Krüger
2023-05-04 19:18:21 +02:00
committed by GitHub
12 changed files with 724 additions and 1 deletions

View File

@@ -0,0 +1,37 @@
// compile-flags: -C opt-level=1
// ignore-debug (the extra assertions get in the way)
#![crate_type = "lib"]
#![feature(ascii_char)]
use std::ascii::Char as AsciiChar;
// CHECK-LABEL: i8 @unwrap_digit_from_remainder(i32
#[no_mangle]
pub fn unwrap_digit_from_remainder(v: u32) -> AsciiChar {
// CHECK-NOT: icmp
// CHECK-NOT: panic
// CHECK: %[[R:.+]] = urem i32 %v, 10
// CHECK-NEXT: %[[T:.+]] = trunc i32 %[[R]] to i8
// CHECK-NEXT: %[[D:.+]] = or i8 %[[T]], 48
// CHECK-NEXT: ret i8 %[[D]]
// CHECK-NOT: icmp
// CHECK-NOT: panic
AsciiChar::digit((v % 10) as u8).unwrap()
}
// CHECK-LABEL: i8 @unwrap_from_masked(i8
#[no_mangle]
pub fn unwrap_from_masked(b: u8) -> AsciiChar {
// CHECK-NOT: icmp
// CHECK-NOT: panic
// CHECK: %[[M:.+]] = and i8 %b, 127
// CHECK-NEXT: ret i8 %[[M]]
// CHECK-NOT: icmp
// CHECK-NOT: panic
AsciiChar::from_u8(b & 0x7f).unwrap()
}