Skip to content

Commit d5daab4

Browse files
author
sam skeoch
committed
Add as_ascii_unchecked() method to primitive type str
1 parent 82f93a2 commit d5daab4

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

library/core/src/str/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,6 +2633,21 @@ impl str {
26332633
self.as_bytes().as_ascii()
26342634
}
26352635

2636+
/// Converts this string slice into a slice of [ASCII characters](ascii::Char),
2637+
/// without checking whether they are valid.
2638+
///
2639+
/// # Safety
2640+
///
2641+
/// Every character in this string must be ASCII, or else this is UB.
2642+
#[unstable(feature = "ascii_char", issue = "110998")]
2643+
#[must_use]
2644+
#[inline]
2645+
pub const unsafe fn as_ascii_unchecked(&self) -> &[ascii::Char] {
2646+
// SAFETY: the caller promised that every byte of this string slice
2647+
// is ASCII.
2648+
unsafe { self.as_bytes().as_ascii_unchecked() }
2649+
}
2650+
26362651
/// Checks that two strings are an ASCII case-insensitive match.
26372652
///
26382653
/// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,

0 commit comments

Comments
 (0)