Skip to content

Commit 3ad0a21

Browse files
committed
ptx: handle duplicate input files
1 parent cc103ec commit 3ad0a21

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

src/uu/ptx/src/ptx.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use std::cmp;
99
use std::cmp::PartialEq;
10-
use std::collections::{BTreeSet, HashMap, HashSet};
10+
use std::collections::{BTreeSet, HashSet};
1111
use std::ffi::{OsStr, OsString};
1212
use std::fmt::Write as FmtWrite;
1313
use std::fs::File;
@@ -269,10 +269,10 @@ struct FileContent {
269269
offset: usize,
270270
}
271271

272-
type FileMap = HashMap<OsString, FileContent>;
272+
type FileMap = Vec<(OsString, FileContent)>;
273273

274274
fn read_input(input_files: &[OsString]) -> std::io::Result<FileMap> {
275-
let mut file_map: FileMap = HashMap::new();
275+
let mut file_map: FileMap = FileMap::new();
276276
let mut offset: usize = 0;
277277
for filename in input_files {
278278
let reader: BufReader<Box<dyn Read>> = BufReader::new(if filename == "-" {
@@ -287,14 +287,14 @@ fn read_input(input_files: &[OsString]) -> std::io::Result<FileMap> {
287287
// 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.
288288
let chars_lines: Vec<Vec<char>> = lines.iter().map(|x| x.chars().collect()).collect();
289289
let size = lines.len();
290-
file_map.insert(
290+
file_map.push((
291291
filename.clone(),
292292
FileContent {
293293
lines,
294294
chars_lines,
295295
offset,
296296
},
297-
);
297+
));
298298
offset += size;
299299
}
300300
Ok(file_map)
@@ -751,8 +751,13 @@ fn write_traditional_output(
751751
}
752752

753753
for word_ref in words {
754-
let file_map_value: &FileContent = file_map
755-
.get(&word_ref.filename)
754+
let (_, file_map_value) = file_map
755+
.iter()
756+
.find(|(name, content)| {
757+
name == &word_ref.filename
758+
&& word_ref.global_line_nr >= content.offset
759+
&& word_ref.global_line_nr < content.offset + content.lines.len()
760+
})
756761
.expect("Missing file in file map");
757762
let FileContent {
758763
ref lines,

tests/by-util/test_ptx.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,11 @@ fn test_unicode_truncation_alignment() {
301301
.succeeds()
302302
.stdout_only(" / bar\n föö/\n");
303303
}
304+
305+
#[test]
306+
fn test_duplicate_input_files() {
307+
new_ucmd!()
308+
.args(&["one_word", "one_word"])
309+
.succeeds()
310+
.stdout_is(" rust\n rust\n");
311+
}

tests/fixtures/ptx/one_word

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rust

0 commit comments

Comments
 (0)