@@ -8,6 +8,7 @@ use crate::fmt;
8
8
use crate :: hash:: { Hash , Hasher } ;
9
9
use crate :: ops;
10
10
use crate :: rc:: Rc ;
11
+ use crate :: str:: pattern:: Pattern ;
11
12
use crate :: str:: FromStr ;
12
13
use crate :: sync:: Arc ;
13
14
@@ -978,6 +979,69 @@ impl OsStr {
978
979
pub fn eq_ignore_ascii_case < S : AsRef < OsStr > > ( & self , other : S ) -> bool {
979
980
self . inner . eq_ignore_ascii_case ( & other. as_ref ( ) . inner )
980
981
}
982
+
983
+ /// Returns `true` if the given pattern matches a prefix of this `OsStr`.
984
+ ///
985
+ /// Returns `false` if it does not.
986
+ ///
987
+ /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
988
+ /// function or closure that determines if a character matches.
989
+ ///
990
+ /// [`char`]: prim@char
991
+ /// [pattern]: crate::str::pattern
992
+ ///
993
+ /// # Examples
994
+ ///
995
+ /// Basic usage:
996
+ ///
997
+ /// ```
998
+ /// #![feature(osstr_str_prefix_fns)]
999
+ ///
1000
+ /// use std::ffi::OsString;
1001
+ ///
1002
+ /// let bananas = OsString::from("bananas");
1003
+ ///
1004
+ /// assert!(bananas.starts_with("bana"));
1005
+ /// assert!(!bananas.starts_with("nana"));
1006
+ /// ```
1007
+ #[ unstable( feature = "osstr_str_prefix_fns" , issue = "none" ) ]
1008
+ #[ must_use]
1009
+ #[ inline]
1010
+ pub fn starts_with < ' a , P : Pattern < ' a > > ( & ' a self , pattern : P ) -> bool {
1011
+ self . inner . starts_with ( pattern)
1012
+ }
1013
+
1014
+ /// Returns this `OsStr` with the given prefix removed.
1015
+ ///
1016
+ /// If the `OsStr` starts with the pattern `prefix`, returns the substring
1017
+ /// after the prefix, wrapped in `Some`.
1018
+ ///
1019
+ /// If the `OsStr` does not start with `prefix`, returns `None`.
1020
+ ///
1021
+ /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1022
+ /// function or closure that determines if a character matches.
1023
+ ///
1024
+ /// [`char`]: prim@char
1025
+ /// [pattern]: crate::str::pattern
1026
+ ///
1027
+ /// # Examples
1028
+ ///
1029
+ /// ```
1030
+ /// #![feature(osstr_str_prefix_fns)]
1031
+ ///
1032
+ /// use std::ffi::{OsStr, OsString};
1033
+ ///
1034
+ /// let foobar = OsString::from("foo:bar");
1035
+ ///
1036
+ /// assert_eq!(foobar.strip_prefix("foo:"), Some(OsStr::new("bar")));
1037
+ /// assert_eq!(foobar.strip_prefix("bar"), None);
1038
+ /// ```
1039
+ #[ unstable( feature = "osstr_str_prefix_fns" , issue = "none" ) ]
1040
+ #[ must_use]
1041
+ #[ inline]
1042
+ pub fn strip_prefix < ' a , P : Pattern < ' a > > ( & ' a self , prefix : P ) -> Option < & ' a OsStr > {
1043
+ Some ( OsStr :: from_inner ( self . inner . strip_prefix ( prefix) ?) )
1044
+ }
981
1045
}
982
1046
983
1047
#[ stable( feature = "box_from_os_str" , since = "1.17.0" ) ]
0 commit comments