grep-matcher: add LineTerminator::is_suffix

This centralizes the logic for checking whether a line has a line
terminator or not.
This commit is contained in:
Andrew Gallant 2018-09-07 11:56:30 -04:00
parent b8f619d16e
commit 56e8864426
No known key found for this signature in database
GPG Key ID: B2E3A4923F8B0D44
2 changed files with 11 additions and 1 deletions

View File

@ -266,6 +266,16 @@ impl LineTerminator {
LineTerminatorImp::CRLF => &[b'\r', b'\n'], 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 { impl Default for LineTerminator {

View File

@ -1396,7 +1396,7 @@ impl<'a, M: Matcher, W: WriteColor> StandardImpl<'a, M, W> {
} }
fn has_line_terminator(&self, buf: &[u8]) -> bool { 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 { fn is_context(&self) -> bool {