History

FAIL: not really successful to continue down the 'fold' road (#198)

Sebastian Thiel committed 5 years ago

Commit d9afc221 parent 13646e82 files · +40 −4Browse files

Showing the changes to git-object/src/commit (1 of 2 files). Show all

Unified · Split

git-object/src/commit/message.rs+27−4
@@ -8,9 +8,10 @@ use crate::{ · 7 unmodified lines
8mod decode {8mod decode {
9 use crate::bstr::{BStr, ByteSlice};9 use crate::bstr::{BStr, ByteSlice};
10 use nom::branch::alt;10 use nom::branch::alt;
11 use nom::bytes::complete::{tag, take_till1};11 use nom::bytes::complete::{tag, take_till1, take_until1};
12 use nom::combinator::{all_consuming, opt};12 use nom::combinator::{all_consuming, map, opt, peek, recognize};
13 use nom::error::ParseError;13 use nom::error::ParseError;
14 use nom::multi::{fold_many1, length_data};
14 use nom::sequence::{pair, terminated};15 use nom::sequence::{pair, terminated};
15 use nom::IResult;16 use nom::IResult;
16 17
@@ -18,8 +19,30 @@ mod decode { · 1 unmodified line
18 alt((tag(b"\r\n"), tag(b"\n")))(i)19 alt((tag(b"\r\n"), tag(b"\n")))(i)
19 }20 }
20 21
21 fn subject<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a [u8], E> {22 fn subject<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a BStr, E> {
22 take_till1(|c| c == b'\n')(i)23 map(
24 length_data(peek(fold_many1(
25 recognize(terminated(take_till1(|c| c == b'\n'), tag(b"\n"))),
26 || 0,
27 |acc: usize, item: &'a [u8]| acc + item.len(),
28 ))),
29 |s| s.as_bstr(),
30 )(i)
31 }
32
33 #[cfg(test)]
34 mod tests {
35 use super::*;
36
37 #[test]
38 fn subject_ending_in_newline_with_newline() {
39 assert_eq!(subject::<()>(b"a\nb\n").unwrap(), (b"".as_ref(), b"a\nb\nc".as_bstr()));
40 }
41
42 #[test]
43 fn subject_with_single_newline() {
44 assert_eq!(subject::<()>(b"a\n").unwrap(), (b"".as_ref(), b"a\n".as_bstr()));
45 }
23 }46 }
24 47
25 /// Parse a signature from the bytes input `i` using `nom`.48 /// Parse a signature from the bytes input `i` using `nom`.