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
88 mod decode {
99 use crate::bstr::{BStr, ByteSlice};
1010 use nom::branch::alt;
11− use nom::bytes::complete::{tag, take_till1};
12− use nom::combinator::{all_consuming, opt};
11+ use nom::bytes::complete::{tag, take_till1, take_until1};
12+ use nom::combinator::{all_consuming, map, opt, peek, recognize};
1313 use nom::error::ParseError;
14+ use nom::multi::{fold_many1, length_data};
1415 use nom::sequence::{pair, terminated};
1516 use nom::IResult;
1617
@@ -18,8 +19,30 @@ mod decode { · 1 unmodified line
1819 alt((tag(b"\r\n"), tag(b"\n")))(i)
1920 }
2021
21− fn subject<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a [u8], E> {
22− take_till1(|c| c == b'\n')(i)
22+ fn subject<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a BStr, E> {
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+ }
2346 }
2447
2548 /// Parse a signature from the bytes input `i` using `nom`.