1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-09-15 11:36:16 +00:00

more: reduce memory usage a bit

This commit is contained in:
Terts Diepraam 2024-05-13 19:39:45 +02:00 committed by Sylvestre Ledru
parent 9dec29f613
commit 2fef5be8f7

View file

@ -447,7 +447,7 @@ struct Pager<'a> {
upper_mark: usize, upper_mark: usize,
// The number of rows that fit on the screen // The number of rows that fit on the screen
content_rows: u16, content_rows: u16,
lines: Vec<String>, lines: Vec<&'a str>,
next_file: Option<&'a str>, next_file: Option<&'a str>,
line_count: usize, line_count: usize,
silent: bool, silent: bool,
@ -456,7 +456,7 @@ struct Pager<'a> {
} }
impl<'a> Pager<'a> { impl<'a> Pager<'a> {
fn new(rows: u16, lines: Vec<String>, next_file: Option<&'a str>, options: &Options) -> Self { fn new(rows: u16, lines: Vec<&'a str>, next_file: Option<&'a str>, options: &Options) -> Self {
let line_count = lines.len(); let line_count = lines.len();
Self { Self {
upper_mark: options.from_line, upper_mark: options.from_line,
@ -608,7 +608,7 @@ impl<'a> Pager<'a> {
} }
} }
fn search_pattern_in_file(lines: &[String], pattern: &Option<String>) -> Option<usize> { fn search_pattern_in_file(lines: &[&str], pattern: &Option<String>) -> Option<usize> {
let pattern = pattern.clone().unwrap_or_default(); let pattern = pattern.clone().unwrap_or_default();
if lines.is_empty() || pattern.is_empty() { if lines.is_empty() || pattern.is_empty() {
return None; return None;
@ -630,8 +630,10 @@ fn paging_add_back_message(options: &Options, stdout: &mut std::io::Stdout) -> U
} }
// Break the lines on the cols of the terminal // Break the lines on the cols of the terminal
fn break_buff(buff: &str, cols: usize) -> Vec<String> { fn break_buff(buff: &str, cols: usize) -> Vec<&str> {
let mut lines = Vec::with_capacity(buff.lines().count()); // We _could_ do a precise with_capacity here, but that would require scanning the
// whole buffer. Just guess a value instead.
let mut lines = Vec::with_capacity(2048);
for l in buff.lines() { for l in buff.lines() {
lines.append(&mut break_line(l, cols)); lines.append(&mut break_line(l, cols));
@ -639,11 +641,11 @@ fn break_buff(buff: &str, cols: usize) -> Vec<String> {
lines lines
} }
fn break_line(line: &str, cols: usize) -> Vec<String> { fn break_line(line: &str, cols: usize) -> Vec<&str> {
let width = UnicodeWidthStr::width(line); let width = UnicodeWidthStr::width(line);
let mut lines = Vec::new(); let mut lines = Vec::new();
if width < cols { if width < cols {
lines.push(line.to_string()); lines.push(line);
return lines; return lines;
} }
@ -655,14 +657,14 @@ fn break_line(line: &str, cols: usize) -> Vec<String> {
total_width += width; total_width += width;
if total_width > cols { if total_width > cols {
lines.push(line[last_index..index].to_string()); lines.push(&line[last_index..index]);
last_index = index; last_index = index;
total_width = width; total_width = width;
} }
} }
if last_index != line.len() { if last_index != line.len() {
lines.push(line[last_index..].to_string()); lines.push(&line[last_index..]);
} }
lines lines
} }
@ -727,29 +729,16 @@ mod tests {
#[test] #[test]
fn test_search_pattern_empty_pattern() { fn test_search_pattern_empty_pattern() {
let lines = vec![String::from("line1"), String::from("line2")]; let lines = vec!["line1", "line2"];
let pattern = None; let pattern = None;
assert_eq!(None, search_pattern_in_file(&lines, &pattern)); assert_eq!(None, search_pattern_in_file(&lines, &pattern));
} }
#[test] #[test]
fn test_search_pattern_found_pattern() { fn test_search_pattern_found_pattern() {
let lines = vec![ let lines = vec!["line1", "line2", "pattern"];
String::from("line1"), let lines2 = vec!["line1", "line2", "pattern", "pattern2"];
String::from("line2"), let lines3 = vec!["line1", "line2", "other_pattern"];
String::from("pattern"),
];
let lines2 = vec![
String::from("line1"),
String::from("line2"),
String::from("pattern"),
String::from("pattern2"),
];
let lines3 = vec![
String::from("line1"),
String::from("line2"),
String::from("other_pattern"),
];
let pattern = Some(String::from("pattern")); let pattern = Some(String::from("pattern"));
assert_eq!(2, search_pattern_in_file(&lines, &pattern).unwrap()); assert_eq!(2, search_pattern_in_file(&lines, &pattern).unwrap());
assert_eq!(2, search_pattern_in_file(&lines2, &pattern).unwrap()); assert_eq!(2, search_pattern_in_file(&lines2, &pattern).unwrap());
@ -758,11 +747,7 @@ mod tests {
#[test] #[test]
fn test_search_pattern_not_found_pattern() { fn test_search_pattern_not_found_pattern() {
let lines = vec![ let lines = vec!["line1", "line2", "something"];
String::from("line1"),
String::from("line2"),
String::from("something"),
];
let pattern = Some(String::from("pattern")); let pattern = Some(String::from("pattern"));
assert_eq!(None, search_pattern_in_file(&lines, &pattern)); assert_eq!(None, search_pattern_in_file(&lines, &pattern));
} }