Skip to content

Commit

Permalink
Merge pull request #490 from epage/sub
Browse files Browse the repository at this point in the history
fix(token): Be consistent on `take_until("")`
  • Loading branch information
epage authored Feb 28, 2024
2 parents c3e3e4d + 9d50921 commit c8fb68e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3478,19 +3478,19 @@ fn memchr3(token: (u8, u8, u8), slice: &[u8]) -> Option<usize> {

#[inline(always)]
fn memmem(slice: &[u8], literal: &[u8]) -> Option<crate::lib::std::ops::Range<usize>> {
if literal.len() == 1 {
memchr(literal[0], slice).map(|i| i..i + 1)
} else {
memmem_(slice, literal)
match literal.len() {
0 => Some(0..0),
1 => memchr(literal[0], slice).map(|i| i..i + 1),
_ => memmem_(slice, literal),
}
}

#[inline(always)]
fn memmem2(slice: &[u8], literal: (&[u8], &[u8])) -> Option<crate::lib::std::ops::Range<usize>> {
if literal.0.len() == 1 && literal.1.len() == 1 {
memchr2((literal.0[0], literal.1[0]), slice).map(|i| i..i + 1)
} else {
memmem2_(slice, literal)
match (literal.0.len(), literal.1.len()) {
(0, _) | (_, 0) => Some(0..0),
(1, 1) => memchr2((literal.0[0], literal.1[0]), slice).map(|i| i..i + 1),
_ => memmem2_(slice, literal),
}
}

Expand All @@ -3499,10 +3499,10 @@ fn memmem3(
slice: &[u8],
literal: (&[u8], &[u8], &[u8]),
) -> Option<crate::lib::std::ops::Range<usize>> {
if literal.0.len() == 1 && literal.1.len() == 1 && literal.2.len() == 1 {
memchr3((literal.0[0], literal.1[0], literal.2[0]), slice).map(|i| i..i + 1)
} else {
memmem3_(slice, literal)
match (literal.0.len(), literal.1.len(), literal.2.len()) {
(0, _, _) | (_, 0, _) | (_, _, 0) => Some(0..0),
(1, 1, 1) => memchr3((literal.0[0], literal.1[0], literal.2[0]), slice).map(|i| i..i + 1),
_ => memmem3_(slice, literal),
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/token/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ fn complete_take_until() {
);
}

#[test]
fn complete_take_until_empty() {
fn take_until_empty(i: &str) -> IResult<&str, &str> {
take_until(0, "").parse_peek(i)
}
assert_eq!(take_until_empty(""), Ok(("", "")));
assert_eq!(take_until_empty("end"), Ok(("end", "")));
}

#[test]
fn complete_literal_case_insensitive() {
fn caseless_bytes(i: &[u8]) -> IResult<&[u8], &[u8]> {
Expand Down

0 comments on commit c8fb68e

Please sign in to comment.