Skip to content

Commit

Permalink
Fix byte indexing in not_line_ending
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Jan 15, 2018
1 parent 25e45bd commit 7bc5d42
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/nom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,16 @@ where
T: InputIter + InputLength + AtEof,
T: Compare<&'static str>,
<T as InputIter>::Item: AsChar,
<T as InputIter>::RawItem: AsChar,
{
match input.iter_elements().position(|item| {
match input.position(|item| {
let c = item.as_char();
c == '\r' || c == '\n'
}) {
None => Ok((input.slice(input.input_len()..), input)),
Some(index) => {
let mut it = input.iter_elements();
let nth = it.nth(index).unwrap().as_char();
let mut it = input.slice(index..).iter_elements();
let nth = it.next().unwrap().as_char();
if nth == '\r' {
let sliced = input.slice(index..);
let comp = sliced.compare("\r\n");
Expand Down
19 changes: 19 additions & 0 deletions tests/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,22 @@ fn issue_302(input: &[u8]) -> IResult<&[u8], Option<Vec<u64>>> {
( entries )
)
}

#[test]
fn issue_655() {
use nom::{line_ending, not_line_ending};
named!(twolines(&str) -> (&str, &str),
do_parse!(
l1 : not_line_ending >>
line_ending >>
l2 : not_line_ending >>
line_ending >>
((l1, l2))
)
);

assert_eq!(twolines("foo\nbar\n"), Ok(("", ("foo", "bar"))));
assert_eq!(twolines("féo\nbar\n"), Ok(("", ("féo", "bar"))));
assert_eq!(twolines("foé\nbar\n"), Ok(("", ("foé", "bar"))));
assert_eq!(twolines("foé\r\nbar\n"), Ok(("", ("foé", "bar"))));
}

0 comments on commit 7bc5d42

Please sign in to comment.