Skip to content

Commit ac0e81c

Browse files
committed
Prefer built-in Winnow parsers
1 parent 86d7fd1 commit ac0e81c

File tree

11 files changed

+54
-75
lines changed

11 files changed

+54
-75
lines changed

gix-actor/src/signature/decode.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub(crate) mod function {
55
use std::cell::RefCell;
66
use winnow::{
77
branch::alt,
8-
bytes::complete::{tag, take, take_until, take_while_m_n},
8+
bytes::complete::{take, take_until, take_while_m_n},
99
character::is_digit,
1010
error::{context, ContextError, ParseError},
1111
multi::many1_count,
@@ -24,9 +24,9 @@ pub(crate) mod function {
2424
let tzsign = RefCell::new(b'-'); // TODO: there should be no need for this.
2525
let (i, (identity, _, time, _tzsign_count, hours, minutes)) = context(
2626
"<name> <<email>> <timestamp> <+|-><HHMM>",
27-
tuple((
27+
(
2828
identity,
29-
tag(b" "),
29+
b" ",
3030
context("<timestamp>", |i| {
3131
terminated(take_until(SPACE), take(1usize))(i).and_then(|(i, v)| {
3232
btoi::<SecondsSinceUnixEpoch>(v)
@@ -37,8 +37,8 @@ pub(crate) mod function {
3737
context(
3838
"+|-",
3939
alt((
40-
many1_count(tag(b"-")).map(|_| *tzsign.borrow_mut() = b'-'), // TODO: this should be a non-allocating consumer of consecutive tags
41-
many1_count(tag(b"+")).map(|_| *tzsign.borrow_mut() = b'+'),
40+
many1_count(b"-").map(|_| *tzsign.borrow_mut() = b'-'), // TODO: this should be a non-allocating consumer of consecutive tags
41+
many1_count(b"+").map(|_| *tzsign.borrow_mut() = b'+'),
4242
)),
4343
),
4444
context("HH", |i| {
@@ -55,7 +55,7 @@ pub(crate) mod function {
5555
.map_err(|_| winnow::Err::from_error_kind(i, winnow::error::ErrorKind::MapRes))
5656
})
5757
}),
58-
)),
58+
),
5959
)(i)?;
6060

6161
let tzsign = tzsign.into_inner();
@@ -83,10 +83,10 @@ pub(crate) mod function {
8383
) -> IResult<&'a [u8], IdentityRef<'a>, E> {
8484
let (i, (name, email)) = context(
8585
"<name> <<email>>",
86-
tuple((
86+
(
8787
context("<name>", terminated(take_until(&b" <"[..]), take(2usize))),
8888
context("<email>", terminated(take_until(&b">"[..]), take(1usize))),
89-
)),
89+
),
9090
)(i)?;
9191

9292
Ok((

gix-object/src/commit/decode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::borrow::Cow;
33
use smallvec::SmallVec;
44
use winnow::{
55
branch::alt,
6-
bytes::complete::{is_not, tag},
6+
bytes::complete::is_not,
77
combinator::{all_consuming, opt},
88
error::{context, ContextError, ParseError},
99
multi::many0,
@@ -18,7 +18,7 @@ pub fn message<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(i: &'a [u8]
1818
return Err(winnow::Err::from_error_kind(i, winnow::error::ErrorKind::Eof)
1919
.map(|err: E| err.add_context(i, "newline + <message>")));
2020
}
21-
let (i, _) = context("a newline separates headers from the message", tag(NL))(i)?;
21+
let (i, _) = context("a newline separates headers from the message", NL)(i)?;
2222
Ok((&[], i.as_bstr()))
2323
}
2424

gix-object/src/commit/message/body.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::ops::Deref;
22

33
use winnow::{
4-
bytes::complete::{tag, take_until1},
4+
bytes::complete::take_until1,
55
combinator::all_consuming,
66
error::{ErrorKind, ParseError},
77
sequence::terminated,
@@ -33,7 +33,7 @@ pub struct TrailerRef<'a> {
3333
}
3434

3535
fn parse_single_line_trailer<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], (&'a BStr, &'a BStr), E> {
36-
let (value, token) = terminated(take_until1(b":".as_ref()), tag(b": "))(i.trim_end())?;
36+
let (value, token) = terminated(take_until1(b":".as_ref()), b": ")(i.trim_end())?;
3737
if token.trim_end().len() != token.len() || value.trim_start().len() != value.len() {
3838
Err(winnow::Err::from_error_kind(i, ErrorKind::Fail).cut())
3939
} else {

gix-object/src/commit/message/decode.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
use winnow::{
2-
branch::alt,
3-
bytes::complete::{tag, take_till1},
4-
combinator::all_consuming,
5-
error::ParseError,
6-
sequence::pair,
7-
IResult,
8-
};
1+
use winnow::{branch::alt, bytes::complete::take_till1, combinator::all_consuming, error::ParseError, prelude::*};
92

103
use crate::bstr::{BStr, ByteSlice};
114

125
pub(crate) fn newline<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a [u8], E> {
13-
alt((tag(b"\r\n"), tag(b"\n")))(i)
6+
alt((b"\r\n", b"\n"))(i)
147
}
158

169
fn subject_and_body<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], (&'a BStr, Option<&'a BStr>), E> {
@@ -20,7 +13,7 @@ fn subject_and_body<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8
2013
c = match take_till1::<_, _, E>(|c| c == b'\n' || c == b'\r')(c) {
2114
Ok((i1, segment)) => {
2215
consumed_bytes += segment.len();
23-
match pair::<_, _, _, E, _, _>(newline, newline)(i1) {
16+
match (newline::<E>, newline::<E>).parse_next(i1) {
2417
Ok((body, _)) => {
2518
return Ok((
2619
&[],

gix-object/src/parse.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use bstr::{BStr, BString, ByteVec};
22
use winnow::{
3-
bytes::complete::{is_not, tag, take_until, take_while_m_n},
3+
bytes::complete::{is_not, take_until, take_while_m_n},
44
combinator::{peek, recognize},
55
error::{context, ContextError, ParseError},
66
multi::many1_count,
7-
sequence::{preceded, terminated, tuple},
7+
sequence::{preceded, terminated},
88
IResult,
99
};
1010

@@ -19,14 +19,10 @@ pub(crate) fn any_header_field_multi_line<'a, E: ParseError<&'a [u8]> + ContextE
1919
) -> IResult<&'a [u8], (&'a [u8], BString), E> {
2020
let (i, (k, o)) = context(
2121
"name <multi-line-value>",
22-
peek(tuple((
23-
terminated(is_not(SPACE_OR_NL), tag(SPACE)),
24-
recognize(tuple((
25-
is_not(NL),
26-
tag(NL),
27-
many1_count(terminated(tuple((tag(SPACE), take_until(NL))), tag(NL))),
28-
))),
29-
))),
22+
peek((
23+
terminated(is_not(SPACE_OR_NL), SPACE),
24+
recognize((is_not(NL), NL, many1_count(terminated((SPACE, take_until(NL)), NL)))),
25+
)),
3026
)(i)?;
3127
assert!(!o.is_empty(), "we have parsed more than one value here");
3228
let end = &o[o.len() - 1] as *const u8 as usize;
@@ -48,17 +44,14 @@ pub(crate) fn header_field<'a, T, E: ParseError<&'a [u8]>>(
4844
name: &'static [u8],
4945
parse_value: impl Fn(&'a [u8]) -> IResult<&'a [u8], T, E>,
5046
) -> IResult<&'a [u8], T, E> {
51-
terminated(preceded(terminated(tag(name), tag(SPACE)), parse_value), tag(NL))(i)
47+
terminated(preceded(terminated(name, SPACE), parse_value), NL)(i)
5248
}
5349

5450
pub(crate) fn any_header_field<'a, T, E: ParseError<&'a [u8]>>(
5551
i: &'a [u8],
5652
parse_value: impl Fn(&'a [u8]) -> IResult<&'a [u8], T, E>,
5753
) -> IResult<&'a [u8], (&'a [u8], T), E> {
58-
terminated(
59-
tuple((terminated(is_not(SPACE_OR_NL), tag(SPACE)), parse_value)),
60-
tag(NL),
61-
)(i)
54+
terminated((terminated(is_not(SPACE_OR_NL), SPACE), parse_value), NL)(i)
6255
}
6356

6457
fn is_hex_digit_lc(b: u8) -> bool {

gix-object/src/tag/decode.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use winnow::{
44
character::is_alphabetic,
55
combinator::{all_consuming, opt, recognize},
66
error::{context, ContextError, ParseError},
7-
sequence::{preceded, tuple},
7+
sequence::preceded,
88
IResult,
99
};
1010

@@ -61,21 +61,21 @@ pub fn message<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], (&
6161
Ok((&[], (i, &[])))
6262
}
6363
let (i, (message, signature)) = alt((
64-
tuple((
64+
(
6565
take_until(PGP_SIGNATURE_BEGIN),
6666
preceded(
67-
tag(NL),
68-
recognize(tuple((
69-
tag(&PGP_SIGNATURE_BEGIN[1..]),
67+
NL,
68+
recognize((
69+
&PGP_SIGNATURE_BEGIN[1..],
7070
take_until(PGP_SIGNATURE_END),
71-
tag(PGP_SIGNATURE_END),
71+
PGP_SIGNATURE_END,
7272
take_while(|_| true),
73-
))),
73+
)),
7474
),
75-
)),
75+
),
7676
all_to_end,
7777
))(i)?;
78-
let (i, _) = opt(tag(NL))(i)?;
78+
let (i, _) = opt(NL)(i)?;
7979
Ok((
8080
i,
8181
(

gix-object/src/tree/ref_iter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ mod decode {
118118

119119
use bstr::ByteSlice;
120120
use winnow::{
121-
bytes::complete::{tag, take, take_while1, take_while_m_n},
121+
bytes::complete::{take, take_while1, take_while_m_n},
122122
character::is_digit,
123123
combinator::all_consuming,
124124
error::ParseError,
@@ -161,10 +161,10 @@ mod decode {
161161
}
162162

163163
pub fn entry<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&[u8], EntryRef<'_>, E> {
164-
let (i, mode) = terminated(take_while_m_n(5, 6, is_digit), tag(SPACE))(i)?;
164+
let (i, mode) = terminated(take_while_m_n(5, 6, is_digit), SPACE)(i)?;
165165
let mode = tree::EntryMode::try_from(mode)
166166
.map_err(|invalid| winnow::Err::from_error_kind(invalid, winnow::error::ErrorKind::MapRes))?;
167-
let (i, filename) = terminated(take_while1(|b| b != NULL[0]), tag(NULL))(i)?;
167+
let (i, filename) = terminated(take_while1(|b| b != NULL[0]), NULL)(i)?;
168168
let (i, oid) = take(20u8)(i)?; // TODO: make this compatible with other hash lengths
169169

170170
Ok((

gix-ref/src/parse.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
use gix_object::bstr::{BStr, ByteSlice};
2-
use winnow::{
3-
branch::alt,
4-
bytes::complete::{tag, take_while_m_n},
5-
error::ParseError,
6-
IResult,
7-
};
2+
use winnow::{branch::alt, bytes::complete::take_while_m_n, error::ParseError, IResult};
83

94
fn is_hex_digit_lc(b: u8) -> bool {
105
matches!(b, b'0'..=b'9' | b'a'..=b'f')
@@ -23,5 +18,5 @@ pub fn hex_hash<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &
2318
}
2419

2520
pub 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)
21+
alt((b"\r\n", b"\n"))(i)
2722
}

gix-ref/src/store/file/log/line.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ impl<'a> From<LineRef<'a>> for Line {
7575
pub mod decode {
7676
use gix_object::bstr::{BStr, ByteSlice};
7777
use winnow::{
78-
bytes::complete::{tag, take_while},
78+
bytes::complete::take_while,
7979
combinator::opt,
8080
error::{context, ContextError, ParseError},
81-
sequence::{terminated, tuple},
81+
sequence::terminated,
8282
IResult,
8383
};
8484

@@ -127,20 +127,20 @@ pub mod decode {
127127
if i.is_empty() {
128128
Ok((&[], i.as_bstr()))
129129
} else {
130-
terminated(take_while(|c| c != b'\n'), opt(tag(b"\n")))(i).map(|(i, o)| (i, o.as_bstr()))
130+
terminated(take_while(|c| c != b'\n'), opt(b'\n'))(i).map(|(i, o)| (i, o.as_bstr()))
131131
}
132132
}
133133

134134
fn one<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(bytes: &'a [u8]) -> IResult<&[u8], LineRef<'a>, E> {
135135
let (i, (old, new, signature, message_sep, message)) = context(
136136
"<old-hexsha> <new-hexsha> <name> <<email>> <timestamp> <tz>\\t<message>",
137-
tuple((
138-
context("<old-hexsha>", terminated(hex_hash, tag(b" "))),
139-
context("<new-hexsha>", terminated(hex_hash, tag(b" "))),
137+
(
138+
context("<old-hexsha>", terminated(hex_hash, b" ")),
139+
context("<new-hexsha>", terminated(hex_hash, b" ")),
140140
context("<name> <<email>> <timestamp>", gix_actor::signature::decode),
141-
opt(tag(b"\t")),
141+
opt(b'\t'),
142142
context("<optional message>", message),
143-
)),
143+
),
144144
)(bytes)?;
145145

146146
if message_sep.is_none() {

gix-ref/src/store/file/loose/reference/decode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::convert::{TryFrom, TryInto};
33
use gix_hash::ObjectId;
44
use gix_object::bstr::BString;
55
use winnow::{
6-
bytes::complete::{tag, take_while},
6+
bytes::complete::take_while,
77
combinator::{map, opt},
88
sequence::terminated,
99
IResult,
@@ -72,7 +72,7 @@ impl Reference {
7272

7373
fn parse(bytes: &[u8]) -> IResult<&[u8], MaybeUnsafeState> {
7474
let is_space = |b: u8| b == b' ';
75-
if let (path, Some(_ref_prefix)) = opt(terminated(tag("ref: "), take_while(is_space)))(bytes)? {
75+
if let (path, Some(_ref_prefix)) = opt(terminated("ref: ", take_while(is_space)))(bytes)? {
7676
map(
7777
terminated(take_while(|b| b != b'\r' && b != b'\n'), opt(newline)),
7878
|path| MaybeUnsafeState::UnvalidatedPath(path.into()),

0 commit comments

Comments
 (0)