Showing the changes to git-object/src/commit (1 of 2 files). Show all
Unified · Split
| @@ -6,25 +6,25 @@ use crate::{ · 5 unmodified lines | |||
| 6 | }; | 6 | }; |
| 7 | 7 | ||
| 8 | mod decode { | 8 | mod decode { |
| 9 | use nom::{ | ||
| 10 | branch::alt, | ||
| 11 | bytes::complete::{tag, take_while}, | ||
| 12 | combinator::map, | ||
| 13 | error::ParseError, | ||
| 14 | sequence::{pair, terminated}, | ||
| 15 | IResult, | ||
| 16 | }; | ||
| 17 | |||
| 18 | use crate::bstr::{BStr, ByteSlice}; | 9 | use crate::bstr::{BStr, ByteSlice}; |
| 19 | 10 | ||
| 20 | pub fn newline<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a [u8], E> { | ||
| 21 | alt((tag(b"\r\n"), tag(b"\n")))(i) | ||
| 22 | } | ||
| 23 | /// Returns title and body, without separator | 11 | /// Returns title and body, without separator |
| 24 | pub fn bytes<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&[u8], &'a BStr, E> { | 12 | pub fn bytes(i: &[u8]) -> (&BStr, Option<&BStr>) { |
| 25 | map(terminated(take_while(|c| c != b'\n'), pair(newline, newline)), |t| { | 13 | let message = i; |
| 26 | t.as_bstr() | 14 | match message |
| 27 | })(i) | 15 | .find(b"\n\n") |
| 16 | .map(|pos| (2, pos)) | ||
| 17 | .or_else(|| message.find(b"\r\n\r\n").map(|pos| (4, pos))) | ||
| 18 | { | ||
| 19 | Some((sep_len, end_of_title)) => { | ||
| 20 | let body = &message[end_of_title + sep_len..]; | ||
| 21 | ( | ||
| 22 | &message[..end_of_title].as_bstr(), | ||
| 23 | if body.is_empty() { None } else { Some(body.as_bstr()) }, | ||
| 24 | ) | ||
| 25 | } | ||
| 26 | None => (message.as_bstr(), None), | ||
| 27 | } | ||
| 28 | } | 28 | } |
| 29 | } | 29 | } |
| 30 | 30 | ||
| @@ -33,9 +33,7 @@ impl<'a> MessageRef<'a> { · 2 unmodified lines | |||
| 33 | /// | 33 | /// |
| 34 | /// Note that this cannot fail as everything will be interpreted as title if there is no body separator. | 34 | /// Note that this cannot fail as everything will be interpreted as title if there is no body separator. |
| 35 | pub fn from_bytes(input: &'a [u8]) -> Self { | 35 | pub fn from_bytes(input: &'a [u8]) -> Self { |
| 36 | let (title, body) = decode::bytes::<()>(input) | 36 | let (title, body) = decode::bytes(input); |
| 37 | .map(|(body, title)| (title, Some(body.as_bstr()))) | ||
| 38 | .unwrap_or_else(|_| (input.as_bstr(), None)); | ||
| 39 | MessageRef { title, body } | 37 | MessageRef { title, body } |
| 40 | } | 38 | } |
| 41 | /// Produce a short commit summary for the message title. | 39 | /// Produce a short commit summary for the message title. |