Skip to content

Commit f985303

Browse files
authored
Rollup merge of #147947 - yotamofek:pr/lib/strip-circumfix, r=joboet
Implement `strip_circumfix` lib feature Tracking issue: #147946
2 parents 3551657 + 827bd00 commit f985303

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

library/core/src/slice/mod.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2725,6 +2725,38 @@ impl<T> [T] {
27252725
None
27262726
}
27272727

2728+
/// Returns a subslice with the prefix and suffix removed.
2729+
///
2730+
/// If the slice starts with `prefix` and ends with `suffix`, returns the subslice after the
2731+
/// prefix and before the suffix, wrapped in `Some`.
2732+
///
2733+
/// If the slice does not start with `prefix` or does not end with `suffix`, returns `None`.
2734+
///
2735+
/// # Examples
2736+
///
2737+
/// ```
2738+
/// #![feature(strip_circumfix)]
2739+
///
2740+
/// let v = &[10, 50, 40, 30];
2741+
/// assert_eq!(v.strip_circumfix(&[10], &[30]), Some(&[50, 40][..]));
2742+
/// assert_eq!(v.strip_circumfix(&[10], &[40, 30]), Some(&[50][..]));
2743+
/// assert_eq!(v.strip_circumfix(&[10, 50], &[40, 30]), Some(&[][..]));
2744+
/// assert_eq!(v.strip_circumfix(&[50], &[30]), None);
2745+
/// assert_eq!(v.strip_circumfix(&[10], &[40]), None);
2746+
/// assert_eq!(v.strip_circumfix(&[], &[40, 30]), Some(&[10, 50][..]));
2747+
/// assert_eq!(v.strip_circumfix(&[10, 50], &[]), Some(&[40, 30][..]));
2748+
/// ```
2749+
#[must_use = "returns the subslice without modifying the original"]
2750+
#[unstable(feature = "strip_circumfix", issue = "147946")]
2751+
pub fn strip_circumfix<S, P>(&self, prefix: &P, suffix: &S) -> Option<&[T]>
2752+
where
2753+
T: PartialEq,
2754+
S: SlicePattern<Item = T> + ?Sized,
2755+
P: SlicePattern<Item = T> + ?Sized,
2756+
{
2757+
self.strip_prefix(prefix)?.strip_suffix(suffix)
2758+
}
2759+
27282760
/// Returns a subslice with the optional prefix removed.
27292761
///
27302762
/// If the slice starts with `prefix`, returns the subslice after the prefix. If `prefix`

library/core/src/str/mod.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2447,6 +2447,42 @@ impl str {
24472447
suffix.strip_suffix_of(self)
24482448
}
24492449

2450+
/// Returns a string slice with the prefix and suffix removed.
2451+
///
2452+
/// If the string starts with the pattern `prefix` and ends with the pattern `suffix`, returns
2453+
/// the substring after the prefix and before the suffix, wrapped in `Some`.
2454+
/// Unlike [`trim_start_matches`] and [`trim_end_matches`], this method removes both the prefix
2455+
/// and suffix exactly once.
2456+
///
2457+
/// If the string does not start with `prefix` or does not end with `suffix`, returns `None`.
2458+
///
2459+
/// Each [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2460+
/// function or closure that determines if a character matches.
2461+
///
2462+
/// [`char`]: prim@char
2463+
/// [pattern]: self::pattern
2464+
/// [`trim_start_matches`]: Self::trim_start_matches
2465+
/// [`trim_end_matches`]: Self::trim_end_matches
2466+
///
2467+
/// # Examples
2468+
///
2469+
/// ```
2470+
/// #![feature(strip_circumfix)]
2471+
///
2472+
/// assert_eq!("bar:hello:foo".strip_circumfix("bar:", ":foo"), Some("hello"));
2473+
/// assert_eq!("bar:foo".strip_circumfix("foo", "foo"), None);
2474+
/// assert_eq!("foo:bar;".strip_circumfix("foo:", ';'), Some("bar"));
2475+
/// ```
2476+
#[must_use = "this returns the remaining substring as a new slice, \
2477+
without modifying the original"]
2478+
#[unstable(feature = "strip_circumfix", issue = "147946")]
2479+
pub fn strip_circumfix<P: Pattern, S: Pattern>(&self, prefix: P, suffix: S) -> Option<&str>
2480+
where
2481+
for<'a> S::Searcher<'a>: ReverseSearcher<'a>,
2482+
{
2483+
self.strip_prefix(prefix)?.strip_suffix(suffix)
2484+
}
2485+
24502486
/// Returns a string slice with the optional prefix removed.
24512487
///
24522488
/// If the string starts with the pattern `prefix`, returns the substring after the prefix.

0 commit comments

Comments
 (0)