From 4e2cfdb8cea063b1eff68c60561f679f8f6fe46c Mon Sep 17 00:00:00 2001 From: TechHara Date: Wed, 14 Dec 2022 04:07:12 -0500 Subject: [PATCH] add more comments --- src/uu/cut/src/cut.rs | 6 ++++++ src/uu/cut/src/whitespace_searcher.rs | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/uu/cut/src/cut.rs b/src/uu/cut/src/cut.rs index cc8a348bb..3ecf2f34b 100644 --- a/src/uu/cut/src/cut.rs +++ b/src/uu/cut/src/cut.rs @@ -296,13 +296,17 @@ fn cut_fields_whitespace( // delimiter character positions, since each delimiter sequence length can vary. for &Range { low, high } in ranges { if low - fields_pos > 0 { + // current field is not in the range, so jump to the field corresponding to the + // beginning of the range if any low_idx = match delim_search.nth(low - fields_pos - 1) { Some((_, last)) => last, None => break, }; } + // at this point, current field is the first in the range for _ in 0..=high - low { + // skip printing delimiter if this is the first matching field for this line if print_delim { out.write_all(out_delim.as_bytes())?; } else { @@ -310,6 +314,7 @@ fn cut_fields_whitespace( } match delim_search.next() { + // print the current field up to the next whitespace Some((first, last)) => { let segment = &line[low_idx..first]; @@ -319,6 +324,7 @@ fn cut_fields_whitespace( fields_pos = high + 1; } None => { + // this is the last field in the line, so print the rest let segment = &line[low_idx..]; out.write_all(segment)?; diff --git a/src/uu/cut/src/whitespace_searcher.rs b/src/uu/cut/src/whitespace_searcher.rs index b17910c7a..392b951f4 100644 --- a/src/uu/cut/src/whitespace_searcher.rs +++ b/src/uu/cut/src/whitespace_searcher.rs @@ -25,8 +25,8 @@ impl<'a> Iterator for WhitespaceSearcher<'a> { type Item = (usize, usize); // Iterate over sequences of consecutive whitespace (space and/or tab) characters. - // Returns (first, last) positions of each sequence, where `first` is inclusive and `last` is exclusive. - // The delimiter sequence byte-length is equal to `last - first` + // Returns (first, last) positions of each sequence, where `haystack[first..last]` + // corresponds to the delimiter. fn next(&mut self) -> Option { if let Some(match_idx) = memchr2(b' ', b'\t', self.haystack) { let mut skip = match_idx + 1;