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