77
88use std:: cmp;
99use std:: cmp:: PartialEq ;
10- use std:: collections:: { BTreeSet , HashMap , HashSet } ;
10+ use std:: collections:: { BTreeSet , HashSet } ;
1111use std:: ffi:: { OsStr , OsString } ;
1212use std:: fmt:: Write as FmtWrite ;
1313use std:: fs:: File ;
@@ -279,10 +279,10 @@ struct FileContent {
279279 offset : usize ,
280280}
281281
282- type FileMap = HashMap < OsString , FileContent > ;
282+ type FileMap = Vec < ( OsString , FileContent ) > ;
283283
284284fn read_input ( input_files : & [ OsString ] , config : & Config ) -> std:: io:: Result < FileMap > {
285- let mut file_map: FileMap = HashMap :: new ( ) ;
285+ let mut file_map: FileMap = FileMap :: new ( ) ;
286286 let mut offset: usize = 0 ;
287287
288288 let sentence_splitter = config
@@ -304,14 +304,14 @@ fn read_input(input_files: &[OsString], config: &Config) -> std::io::Result<File
304304 // Since we will be jumping around the line a lot, we dump the content into a Vec<char>, which can be indexed in constant time.
305305 let chars_lines: Vec < Vec < char > > = lines. iter ( ) . map ( |x| x. chars ( ) . collect ( ) ) . collect ( ) ;
306306 let size = lines. len ( ) ;
307- file_map. insert (
307+ file_map. push ( (
308308 filename. clone ( ) ,
309309 FileContent {
310310 lines,
311311 chars_lines,
312312 offset,
313313 } ,
314- ) ;
314+ ) ) ;
315315 offset += size;
316316 }
317317 Ok ( file_map)
@@ -793,8 +793,17 @@ fn write_traditional_output(
793793 }
794794
795795 for word_ref in words {
796- let file_map_value: & FileContent = file_map
797- . get ( & word_ref. filename )
796+ // Since `ptx` accepts duplicate file arguments (e.g., `ptx file file`),
797+ // simply looking up by filename is ambiguous.
798+ // We use the `global_line_nr` (which is unique across the entire input stream)
799+ // to identify which file covers this line.
800+ let ( _, file_map_value) = file_map
801+ . iter ( )
802+ . find ( |( name, content) | {
803+ name == & word_ref. filename
804+ && word_ref. global_line_nr >= content. offset
805+ && word_ref. global_line_nr < content. offset + content. lines . len ( )
806+ } )
798807 . expect ( "Missing file in file map" ) ;
799808 let FileContent {
800809 ref lines,
0 commit comments