Skip to content

Commit 862477b

Browse files
committed
std: add pattern matching to OsStr
Implement Haystack for &OsStr and Pattern<&OsStr> for &str, char and Predicate. Furthermore, add prefix/suffix matching/stripping and splitting methods to OsStr type to make use of those patterns. Using OsStr as a pattern is *not* implemented. With #118485 - I added find and rfind
1 parent e8cf9a8 commit 862477b

9 files changed

Lines changed: 3407 additions & 5 deletions

File tree

library/core/src/str_bytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<F: Flavour> Bytes<F> {
6767
///
6868
/// It may be more convenient to use [`Bytes::from_str`] for `&str` and
6969
/// [`Bytes::from_bytes`] for `&[u8]`.
70-
unsafe fn new(bytes: &[u8]) -> &Self {
70+
pub unsafe fn new(bytes: &[u8]) -> &Self {
7171
// SAFETY: `Bytes` is a `repr(transparent)` wrapper around `[u8]`.
7272
unsafe { &*(bytes as *const [u8] as *const Bytes<F>) }
7373
}

library/core/src/wtf8.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,14 @@ pub enum Utf8BoundaryError {
503503
}
504504

505505
/// Copied from core::str::raw::slice_unchecked
506+
impl Wtf8 {
507+
#[inline]
508+
pub unsafe fn get_unchecked(&self, range: core::ops::Range<usize>) -> &Self {
509+
// SAFETY: Caller promises `range` is valid.
510+
unsafe { slice_unchecked(self, range.start, range.end) }
511+
}
512+
}
513+
506514
#[inline]
507515
unsafe fn slice_unchecked(s: &Wtf8, begin: usize, end: usize) -> &Wtf8 {
508516
// SAFETY: memory layout of a &[u8] and &Wtf8 are the same
@@ -662,3 +670,20 @@ unsafe impl CloneToUninit for Wtf8 {
662670
unsafe { self.bytes.clone_to_uninit(dst) }
663671
}
664672
}
673+
674+
#[unstable(feature = "pattern", issue = "27721")]
675+
impl<'a> From<&'a Wtf8> for &'a core::str_bytes::Bytes<core::str_bytes::Wtf8> {
676+
fn from(wtf8: &'a Wtf8) -> Self {
677+
// SAFETY: As name implies, `Wtf8`’s bytes are guaranteed to be WTF-8
678+
// so `Wtf8` flavour is correct.
679+
unsafe { core::str_bytes::Bytes::new(&wtf8.bytes) }
680+
}
681+
}
682+
683+
#[unstable(feature = "pattern", issue = "27721")]
684+
impl<'a> From<&'a core::str_bytes::Bytes<core::str_bytes::Wtf8>> for &'a Wtf8 {
685+
fn from(bytes: &'a core::str_bytes::Bytes<core::str_bytes::Wtf8>) -> Self {
686+
// SAFETY: `Bytes<Wtf8>` is guaranteed to be well-formed WTF-8.
687+
unsafe { Wtf8::from_bytes_unchecked(bytes.as_bytes()) }
688+
}
689+
}

0 commit comments

Comments
 (0)