@@ -2,6 +2,7 @@ use pyo3::exceptions::{PyFileNotFoundError, PyUnicodeDecodeError};
22use pyo3:: prelude:: * ;
33use regex:: Regex ;
44use std:: collections:: HashMap ;
5+ use std:: ffi:: OsStr ;
56use std:: fs;
67use std:: path:: { Path , PathBuf } ;
78use unindent:: unindent;
@@ -199,33 +200,21 @@ impl FileSystem for FakeBasicFileSystem {
199200 }
200201
201202 fn split ( & self , file_name : & str ) -> ( String , String ) {
202- let components: Vec < & str > = file_name. split ( '/' ) . collect ( ) ;
203-
204- if components. is_empty ( ) {
205- return ( "" . to_string ( ) , "" . to_string ( ) ) ;
206- }
207-
208- let tail = components. last ( ) . unwrap_or ( & "" ) ; // Last component, or empty if components is empty (shouldn't happen from split)
209-
210- let head_components = & components[ ..components. len ( ) - 1 ] ; // All components except the last
211-
212- let head = if head_components. is_empty ( ) {
213- // Case for single component paths like "filename.txt" or empty string ""
214- "" . to_string ( )
215- } else if file_name. starts_with ( '/' )
216- && head_components. len ( ) == 1
217- && head_components[ 0 ] . is_empty ( )
218- {
219- // Special handling for paths starting with '/', e.g., "/" or "/filename.txt"
220- // If components were ["", ""], head_components is [""] -> should be "/"
221- // If components were ["", "file.txt"], head_components is [""] -> should be "/"
222- "/" . to_string ( )
203+ let path;
204+ let head;
205+ let tail;
206+ if let Some ( file_name_without_trailing_slash) = file_name. strip_suffix ( "/" ) {
207+ head = Path :: new ( file_name_without_trailing_slash) ;
208+ tail = OsStr :: new ( "" ) ;
223209 } else {
224- // Default joining for multiple components
225- head_components. join ( "/" )
226- } ;
227-
228- ( head, tail. to_string ( ) )
210+ path = Path :: new ( file_name) ;
211+ head = path. parent ( ) . unwrap_or ( Path :: new ( "" ) ) ;
212+ tail = path. file_name ( ) . unwrap_or ( OsStr :: new ( "" ) ) ;
213+ }
214+ (
215+ head. to_str ( ) . unwrap ( ) . to_string ( ) ,
216+ tail. to_str ( ) . unwrap ( ) . to_string ( ) ,
217+ )
229218 }
230219
231220 /// Checks if a file or directory exists within the file system.
0 commit comments