From 56e8864426229de7b181c141baddf788c22b4925 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Fri, 7 Sep 2018 11:56:30 -0400 Subject: grep-matcher: add LineTerminator::is_suffix This centralizes the logic for checking whether a line has a line terminator or not. --- grep-matcher/src/lib.rs | 10 ++++++++++ grep-printer/src/standard.rs | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/grep-matcher/src/lib.rs b/grep-matcher/src/lib.rs index 49d6358f..9a067efa 100644 --- a/grep-matcher/src/lib.rs +++ b/grep-matcher/src/lib.rs @@ -266,6 +266,16 @@ impl LineTerminator { LineTerminatorImp::CRLF => &[b'\r', b'\n'], } } + + /// Returns true if and only if the given slice ends with this line + /// terminator. + /// + /// If this line terminator is `CRLF`, then this only checks whether the + /// last byte is `\n`. + #[inline] + pub fn is_suffix(&self, slice: &[u8]) -> bool { + slice.last().map_or(false, |&b| b == self.as_byte()) + } } impl Default for LineTerminator { diff --git a/grep-printer/src/standard.rs b/grep-printer/src/standard.rs index 183fa4b5..6c8976b8 100644 --- a/grep-printer/src/standard.rs +++ b/grep-printer/src/standard.rs @@ -1396,7 +1396,7 @@ impl<'a, M: Matcher, W: WriteColor> StandardImpl<'a, M, W> { } fn has_line_terminator(&self, buf: &[u8]) -> bool { - buf.last() == Some(&self.searcher.line_terminator().as_byte()) + self.searcher.line_terminator().is_suffix(buf) } fn is_context(&self) -> bool { -- cgit v1.2.3