1use git_object::bstr::{BStr, ByteSlice};
2use nom::{
3 branch::alt,
4 bytes::complete::{tag, take_while_m_n},
5 error::ParseError,
6 IResult,
7};
8
9fn is_hex_digit_lc(b: u8) -> bool {
10 matches!(b, b'0'..=b'9' | b'a'..=b'f')
11}
12
13pub fn hex_hash<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a BStr, E> {
15 take_while_m_n(
18 git_hash::Kind::shortest().len_in_hex(),
19 git_hash::Kind::longest().len_in_hex(),
20 is_hex_digit_lc,
21 )(i)
22 .map(|(i, hex)| (i, hex.as_bstr()))
23}
24
25pub fn newline<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a [u8], E> {
26 alt((tag(b"\r\n"), tag(b"\n")))(i)
27}