From 078f6ffc1cf18546079d03bee99f0903c9e14703 Mon Sep 17 00:00:00 2001 From: mo8it Date: Tue, 26 Mar 2024 02:26:26 +0100 Subject: [PATCH] Add comments --- src/exercise.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/exercise.rs b/src/exercise.rs index e841aed..cdf8d20 100644 --- a/src/exercise.rs +++ b/src/exercise.rs @@ -241,6 +241,7 @@ path = "{}.rs""#, }; let mut current_line_number: usize = 1; + // Keep the last `CONTEXT` lines while iterating over the file lines. let mut prev_lines: [_; CONTEXT] = array::from_fn(|_| String::with_capacity(256)); let mut line = String::with_capacity(256); @@ -260,6 +261,7 @@ path = "{}.rs""#, if contains_not_done_comment(&line) { let mut context = Vec::with_capacity(2 * CONTEXT + 1); + // Previous lines. for (ind, prev_line) in prev_lines .into_iter() .take(current_line_number - 1) @@ -273,18 +275,22 @@ path = "{}.rs""#, }); } + // Current line. context.push(ContextLine { line, number: current_line_number, important: true, }); + // Next lines. for ind in 0..CONTEXT { let mut next_line = String::with_capacity(256); let Ok(n) = read_line(&mut next_line) else { + // If an error occurs, just ignore the next lines. break; }; + // Reached the end of the file. if n == 0 { break; } @@ -300,10 +306,12 @@ path = "{}.rs""#, } current_line_number += 1; - // Recycle the buffers. + // Add the current line as a previous line and shift the older lines by one. for prev_line in &mut prev_lines { mem::swap(&mut line, prev_line); } + // The current line now contains the oldest previous line. + // Recycle it for reading the next line. line.clear(); } }