如何在 nom 5.x 中模拟 take_until_and_consume?
How to emulate take_until_and_consume in nom 5.x?
我已将我的 nom
依赖项从 4.x 更新到 5.x 版本,发现宏 take_until_and_consume
已弃用。变更日志说:
"this can be replaced with take_until
combined with take
"
但我不知道如何用它们来模拟 take_until_and_consume
。
有没有人遇到过这样的版本更新问题或者谁知道怎么办?
我的意思是这个已弃用的宏 take_until_and_consume
. And these new: take
and take_until
我认为这是直截了当的,但这是一个通用的实现:
fn take_until_and_consume<T, I, E>(tag: T) -> impl Fn(I) -> nom::IResult<I, I, E>
where
E: nom::error::ParseError<I>,
I: nom::InputTake
+ nom::FindSubstring<T>
+ nom::Slice<std::ops::RangeFrom<usize>>
+ nom::InputIter<Item = u8>
+ nom::InputLength,
T: nom::InputLength + Clone,
{
use nom::bytes::streaming::take;
use nom::bytes::streaming::take_until;
use nom::sequence::terminated;
move |input| terminated(take_until(tag.clone()), take(tag.input_len()))(input)
}
#[test]
fn test_take_until_and_consume() {
let r = take_until_and_consume::<_, _, ()>("foo")(&b"abcd foo efgh"[..]);
assert_eq!(r, Ok((&b" efgh"[..], &b"abcd "[..])));
}
我已将我的 nom
依赖项从 4.x 更新到 5.x 版本,发现宏 take_until_and_consume
已弃用。变更日志说:
"this can be replaced with
take_until
combined withtake
"
但我不知道如何用它们来模拟 take_until_and_consume
。
有没有人遇到过这样的版本更新问题或者谁知道怎么办?
我的意思是这个已弃用的宏 take_until_and_consume
. And these new: take
and take_until
我认为这是直截了当的,但这是一个通用的实现:
fn take_until_and_consume<T, I, E>(tag: T) -> impl Fn(I) -> nom::IResult<I, I, E>
where
E: nom::error::ParseError<I>,
I: nom::InputTake
+ nom::FindSubstring<T>
+ nom::Slice<std::ops::RangeFrom<usize>>
+ nom::InputIter<Item = u8>
+ nom::InputLength,
T: nom::InputLength + Clone,
{
use nom::bytes::streaming::take;
use nom::bytes::streaming::take_until;
use nom::sequence::terminated;
move |input| terminated(take_until(tag.clone()), take(tag.input_len()))(input)
}
#[test]
fn test_take_until_and_consume() {
let r = take_until_and_consume::<_, _, ()>("foo")(&b"abcd foo efgh"[..]);
assert_eq!(r, Ok((&b" efgh"[..], &b"abcd "[..])));
}