From 6542ea4951fc336c8116b35991864fac9333fcd9 Mon Sep 17 00:00:00 2001 From: Laurence Tratt Date: Mon, 3 Jun 2024 13:04:37 +0100 Subject: [PATCH] Report the text line where pattern failed to match from. Previously when `...` failed, it told you how-far-it-got before realising it had failed, rather than the more useful where-it-started. This commit fixes that. --- src/lib.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fab39ab..ffff4ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -271,18 +271,29 @@ impl<'a> FMatcher<'a> { match (ptnl, textl) { (Some(x), Some(y)) => { if x.trim() == LINE_ANCHOR_WILDCARD { + let text_lines_off_orig = text_lines_off; ptnl = ptn_lines.next(); ptn_lines_off += 1; match ptnl { Some(x) => { + let mut succ = false; while let Some(y) = textl { - text_lines_off += 1; if self.match_line(&mut names, x, y) { + succ = true; break; } + text_lines_off += 1; textl = text_lines.next(); } - text_lines_off -= 1; + if !succ { + return Err(FMatchError { + output_formatter: self.options.output_formatter, + ptn: self.ptn.to_owned(), + text: text.to_owned(), + ptn_line_off: ptn_lines_off, + text_line_off: text_lines_off_orig, + }); + } } None => return Ok(()), } @@ -1011,7 +1022,7 @@ mod tests { assert_eq!(helper("...\nb\nc\nd\n", "a\nb\nc\n0\ne"), (4, 4)); assert_eq!(helper("...\nc\nd\n", "a\nb\nc\n0\ne"), (3, 4)); - assert_eq!(helper("...\nd\n", "a\nb\nc\n0\ne"), (2, 5)); + assert_eq!(helper("...\nd\n", "a\nb\nc\n0\ne"), (2, 1)); assert_eq!(helper("a\n..~\nc\nd\ne", "a\nb\nc\nd"), (3, 2)); assert_eq!(helper("a\n..~\nc\nd", "a\nb\nc\ne"), (3, 2));