@@ -2764,6 +2764,89 @@ impl<T> [T] {
27642764 None
27652765 }
27662766
2767+ /// Returns a subslice with the optional prefix removed.
2768+ ///
2769+ /// If the slice starts with `prefix`, returns the subslice after the prefix. If `prefix`
2770+ /// is empty or the slice does not start with `prefix`, simply returns the original slice.
2771+ /// If `prefix` is equal to the original slice, returns an empty slice.
2772+ ///
2773+ /// # Examples
2774+ ///
2775+ /// ```
2776+ /// #![feature(trim_prefix_suffix)]
2777+ ///
2778+ /// let v = &[10, 40, 30];
2779+ ///
2780+ /// // Prefix present - removes it
2781+ /// assert_eq!(v.trim_prefix(&[10]), &[40, 30][..]);
2782+ /// assert_eq!(v.trim_prefix(&[10, 40]), &[30][..]);
2783+ /// assert_eq!(v.trim_prefix(&[10, 40, 30]), &[][..]);
2784+ ///
2785+ /// // Prefix absent - returns original slice
2786+ /// assert_eq!(v.trim_prefix(&[50]), &[10, 40, 30][..]);
2787+ /// assert_eq!(v.trim_prefix(&[10, 50]), &[10, 40, 30][..]);
2788+ ///
2789+ /// let prefix : &str = "he";
2790+ /// assert_eq!(b"hello".trim_prefix(prefix.as_bytes()), b"llo".as_ref());
2791+ /// ```
2792+ #[ must_use = "returns the subslice without modifying the original" ]
2793+ #[ unstable( feature = "trim_prefix_suffix" , issue = "142312" ) ]
2794+ pub fn trim_prefix < P : SlicePattern < Item = T > + ?Sized > ( & self , prefix : & P ) -> & [ T ]
2795+ where
2796+ T : PartialEq ,
2797+ {
2798+ // This function will need rewriting if and when SlicePattern becomes more sophisticated.
2799+ let prefix = prefix. as_slice ( ) ;
2800+ let n = prefix. len ( ) ;
2801+ if n <= self . len ( ) {
2802+ let ( head, tail) = self . split_at ( n) ;
2803+ if head == prefix {
2804+ return tail;
2805+ }
2806+ }
2807+ self
2808+ }
2809+
2810+ /// Returns a subslice with the optional suffix removed.
2811+ ///
2812+ /// If the slice ends with `suffix`, returns the subslice before the suffix. If `suffix`
2813+ /// is empty or the slice does not end with `suffix`, simply returns the original slice.
2814+ /// If `suffix` is equal to the original slice, returns an empty slice.
2815+ ///
2816+ /// # Examples
2817+ ///
2818+ /// ```
2819+ /// #![feature(trim_prefix_suffix)]
2820+ ///
2821+ /// let v = &[10, 40, 30];
2822+ ///
2823+ /// // Suffix present - removes it
2824+ /// assert_eq!(v.trim_suffix(&[30]), &[10, 40][..]);
2825+ /// assert_eq!(v.trim_suffix(&[40, 30]), &[10][..]);
2826+ /// assert_eq!(v.trim_suffix(&[10, 40, 30]), &[][..]);
2827+ ///
2828+ /// // Suffix absent - returns original slice
2829+ /// assert_eq!(v.trim_suffix(&[50]), &[10, 40, 30][..]);
2830+ /// assert_eq!(v.trim_suffix(&[50, 30]), &[10, 40, 30][..]);
2831+ /// ```
2832+ #[ must_use = "returns the subslice without modifying the original" ]
2833+ #[ unstable( feature = "trim_prefix_suffix" , issue = "142312" ) ]
2834+ pub fn trim_suffix < P : SlicePattern < Item = T > + ?Sized > ( & self , suffix : & P ) -> & [ T ]
2835+ where
2836+ T : PartialEq ,
2837+ {
2838+ // This function will need rewriting if and when SlicePattern becomes more sophisticated.
2839+ let suffix = suffix. as_slice ( ) ;
2840+ let ( len, n) = ( self . len ( ) , suffix. len ( ) ) ;
2841+ if n <= len {
2842+ let ( head, tail) = self . split_at ( len - n) ;
2843+ if tail == suffix {
2844+ return head;
2845+ }
2846+ }
2847+ self
2848+ }
2849+
27672850 /// Binary searches this slice for a given element.
27682851 /// If the slice is not sorted, the returned result is unspecified and
27692852 /// meaningless.
0 commit comments