11// Copyright 2018-2024 the Deno authors. MIT license.
22
3- use std:: borrow:: Cow ;
4- use std:: cell:: OnceCell ;
53use std:: collections:: HashMap ;
64use std:: ffi:: OsStr ;
75use std:: ffi:: OsString ;
@@ -752,8 +750,6 @@ pub enum EvaluateWordTextError {
752750 NotUtf8Pattern { part : OsString } ,
753751 #[ error( "glob: no matches found '{}'" , pattern) ]
754752 NoFilesMatched { pattern : String } ,
755- #[ error( "tilde expansion requires valid utf-8 (text: '{}')" , lossy_text) ]
756- TildeExpansionInvalidUtf8 { lossy_text : String } ,
757753 #[ error( "invalid utf-8: {}" , err) ]
758754 InvalidUtf8 {
759755 #[ from]
@@ -800,13 +796,9 @@ fn evaluate_word_parts(
800796
801797 fn evaluate_word_text (
802798 state : & ShellState ,
803- mut text_parts : Vec < TextPart > ,
799+ text_parts : Vec < TextPart > ,
804800 is_quoted : bool ,
805801 ) -> Result < Vec < OsString > , EvaluateWordTextError > {
806- if !is_quoted {
807- tilde_expand_text_parts ( state, & mut text_parts) ?;
808- }
809-
810802 if !is_quoted
811803 && text_parts
812804 . iter ( )
@@ -894,57 +886,6 @@ fn evaluate_word_parts(
894886 }
895887 }
896888
897- fn tilde_expand_text_parts (
898- state : & ShellState ,
899- text_parts : & mut Vec < TextPart > ,
900- ) -> Result < ( ) , EvaluateWordTextError > {
901- let lazy_home_dir: OnceCell < PathBuf > = OnceCell :: new ( ) ;
902- // tilde expansion
903- for part in text_parts {
904- match part {
905- TextPart :: Text ( os_string) => {
906- let text = os_string. to_string_lossy ( ) ;
907- let mut was_last_escape = false ;
908- let mut new_text = OsString :: new ( ) ;
909- let mut last_index = 0 ;
910- for ( index, c) in text. char_indices ( ) {
911- match c {
912- '\\' => {
913- was_last_escape = true ;
914- }
915- '~' if was_last_escape => {
916- new_text. push ( & text[ last_index..index - 1 ] ) ;
917- new_text. push ( "~" ) ;
918- last_index = index + 1 ;
919- }
920- '~' if !was_last_escape => {
921- if let Cow :: Owned ( text) = text {
922- return Err ( EvaluateWordTextError :: TildeExpansionInvalidUtf8 { lossy_text : text } ) ;
923- }
924- new_text. push ( & text[ last_index..index] ) ;
925- last_index = index + 1 ;
926- // push the home dir
927- let home_dir = lazy_home_dir. get_or_init ( ||sys_traits:: impls:: real_home_dir_with_env ( state) . unwrap_or_else ( || PathBuf :: from ( "~" ) ) ) ;
928- new_text. push ( home_dir) ;
929- }
930- _ => {
931- was_last_escape = false ;
932- }
933- }
934- }
935- if last_index > 0 {
936- new_text. push ( & text[ last_index..] ) ;
937- * os_string = new_text;
938- }
939- } ,
940- TextPart :: Quoted ( _) => {
941- // do not expand in quotes
942- } ,
943- }
944- }
945- Ok ( ( ) )
946- }
947-
948889 fn evaluate_word_parts_inner (
949890 parts : Vec < WordPart > ,
950891 is_quoted : bool ,
@@ -963,6 +904,9 @@ fn evaluate_word_parts(
963904 None
964905 }
965906 WordPart :: Variable ( name) => state. get_var ( OsStr :: new ( & name) ) . cloned ( ) ,
907+ WordPart :: Tilde => {
908+ sys_traits:: impls:: real_home_dir_with_env ( state) . map ( |s| s. into_os_string ( ) )
909+ }
966910 WordPart :: Command ( list) => Some (
967911 evaluate_command_substitution (
968912 list,
0 commit comments