Skip to content

Commit

Permalink
implement take_until1 and take_until_and_consume1
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Mar 19, 2017
1 parent 3000d54 commit 8bae40d
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,31 @@ macro_rules! take_until_and_consume (
);
);

/// `take_until_and_consume1!(tag) => &[T] -> IResult<&[T], &[T]>`
/// generates a parser consuming bytes (at least 1) until the specified byte sequence is found, and consumes it
#[macro_export]
macro_rules! take_until_and_consume1 (
($i:expr, $substr:expr) => (
{
use $crate::InputLength;

let res: $crate::IResult<_,_> = if 1 + $substr.input_len() > $i.input_len() {
$crate::IResult::Incomplete($crate::Needed::Size($substr.input_len()))
} else {
match ($i).find_substring($substr) {
None => {
$crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntilAndConsume,$i))
},
Some(index) => {
$crate::IResult::Done($i.slice(index+$substr.input_len()..), $i.slice(0..index))
},
}
};
res
}
);
);

/// `take_until!(tag) => &[T] -> IResult<&[T], &[T]>`
/// consumes data until it finds the specified tag
#[macro_export]
Expand Down Expand Up @@ -593,6 +618,33 @@ macro_rules! take_until (
);
);

/// `take_until1!(tag) => &[T] -> IResult<&[T], &[T]>`
/// consumes data until it finds the specified tag
#[macro_export]
macro_rules! take_until1 (
($i:expr, $substr:expr) => (
{
use $crate::InputLength;
use $crate::FindSubstring;
use $crate::Slice;

let res: $crate::IResult<_,_> = if 1+$substr.input_len() > $i.input_len() {
$crate::IResult::Incomplete($crate::Needed::Size($substr.input_len()))
} else {
match ($i).find_substring($substr) {
None => {
$crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntil,$i))
},
Some(index) => {
$crate::IResult::Done($i.slice(index..), $i.slice(0..index))
},
}
};
res
}
);
);

/// `take_until_either_and_consume!(tag) => &[T] -> IResult<&[T], &[T]>`
/// consumes data until it finds any of the specified characters, and consume it
#[macro_export]
Expand Down

0 comments on commit 8bae40d

Please sign in to comment.