Skip to content

Commit 63c7edd

Browse files
drinkcatsylvestre
authored andcommitted
cat: add LineNumber.to_str to clean up tests, limit to 32 digits
1 parent def37d9 commit 63c7edd

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

src/uu/cat/src/cat.rs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@ mod splice;
3333
const USAGE: &str = help_usage!("cat.md");
3434
const ABOUT: &str = help_about!("cat.md");
3535

36+
// Allocate 32 digits for the line number.
37+
// An estimate is that we can print about 1e8 lines/seconds, so 32 digits
38+
// would be enough for billions of universe lifetimes.
39+
const LINE_NUMBER_BUF_SIZE: usize = 32;
40+
3641
struct LineNumber {
37-
buf: Vec<u8>,
42+
buf: [u8; LINE_NUMBER_BUF_SIZE],
3843
print_start: usize,
3944
num_start: usize,
4045
num_end: usize,
@@ -48,9 +53,7 @@ struct LineNumber {
4853
// called, using uucore's fast_inc function that operates on strings.
4954
impl LineNumber {
5055
fn new() -> Self {
51-
// 1024-digit long line number should be enough to run `cat` for the lifetime of the universe.
52-
let size = 1024;
53-
let mut buf = vec![b'0'; size];
56+
let mut buf = [b'0'; LINE_NUMBER_BUF_SIZE];
5457

5558
let init_str = " 1\t";
5659
let print_start = buf.len() - init_str.len();
@@ -68,12 +71,17 @@ impl LineNumber {
6871
}
6972

7073
fn increment(&mut self) {
71-
fast_inc_one(self.buf.as_mut_slice(), &mut self.num_start, self.num_end);
74+
fast_inc_one(&mut self.buf, &mut self.num_start, self.num_end);
7275
self.print_start = self.print_start.min(self.num_start);
7376
}
7477

78+
#[inline]
79+
fn to_str(&self) -> &[u8] {
80+
&self.buf[self.print_start..]
81+
}
82+
7583
fn write(&self, writer: &mut impl Write) -> io::Result<()> {
76-
writer.write_all(&self.buf[self.print_start..])
84+
writer.write_all(self.to_str())
7785
}
7886
}
7987

@@ -790,36 +798,21 @@ mod tests {
790798
#[test]
791799
fn test_incrementing_string() {
792800
let mut incrementing_string = super::LineNumber::new();
793-
assert_eq!(
794-
b" 1\t",
795-
&incrementing_string.buf[incrementing_string.print_start..]
796-
);
801+
assert_eq!(b" 1\t", incrementing_string.to_str());
797802
incrementing_string.increment();
798-
assert_eq!(
799-
b" 2\t",
800-
&incrementing_string.buf[incrementing_string.print_start..]
801-
);
803+
assert_eq!(b" 2\t", incrementing_string.to_str());
802804
// Run through to 100
803805
for _ in 3..=100 {
804806
incrementing_string.increment();
805807
}
806-
assert_eq!(
807-
b" 100\t",
808-
&incrementing_string.buf[incrementing_string.print_start..]
809-
);
808+
assert_eq!(b" 100\t", incrementing_string.to_str());
810809
// Run through until we overflow the original size.
811810
for _ in 101..=1_000_000 {
812811
incrementing_string.increment();
813812
}
814813
// Confirm that the start position moves when we overflow the original size.
815-
assert_eq!(
816-
b"1000000\t",
817-
&incrementing_string.buf[incrementing_string.print_start..]
818-
);
814+
assert_eq!(b"1000000\t", incrementing_string.to_str());
819815
incrementing_string.increment();
820-
assert_eq!(
821-
b"1000001\t",
822-
&incrementing_string.buf[incrementing_string.print_start..]
823-
);
816+
assert_eq!(b"1000001\t", incrementing_string.to_str());
824817
}
825818
}

0 commit comments

Comments
 (0)