diff options
| author | sam skeoch <djscythe@noreply.codeberg.org> | 2025-02-22 17:22:57 +0000 |
|---|---|---|
| committer | sam skeoch <djscythe@noreply.codeberg.org> | 2025-05-16 13:53:32 +0100 |
| commit | 7d9f437f993e213c1f9aeb9c07454f6fd1648821 (patch) | |
| tree | 095bb7acc1e6f950c570e99cb177355491b5428d /library/core/src/str/mod.rs | |
| parent | 1b9efcd18f1cb95348c3ffadd4db47bfa15292e5 (diff) | |
| download | rust-7d9f437f993e213c1f9aeb9c07454f6fd1648821.tar.gz rust-7d9f437f993e213c1f9aeb9c07454f6fd1648821.zip | |
Add as_ascii_unchecked() methods to char, str, and u8
Diffstat (limited to 'library/core/src/str/mod.rs')
| -rw-r--r-- | library/core/src/str/mod.rs | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 9e7e949b722..2eb8f496085 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -2634,6 +2634,21 @@ impl str { self.as_bytes().as_ascii() } + /// Converts this string slice into a slice of [ASCII characters](ascii::Char), + /// without checking whether they are valid. + /// + /// # Safety + /// + /// Every character in this string must be ASCII, or else this is UB. + #[unstable(feature = "ascii_char", issue = "110998")] + #[must_use] + #[inline] + pub const unsafe fn as_ascii_unchecked(&self) -> &[ascii::Char] { + // SAFETY: the caller promised that every byte of this string slice + // is ASCII. + unsafe { self.as_bytes().as_ascii_unchecked() } + } + /// Checks that two strings are an ASCII case-insensitive match. /// /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`, |
