-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a primitive to parse a \n- or \r\n-terminated line (#4)
- Loading branch information
Showing
7 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ repository = "https://github.com/scouten/asciidoc-parser" | |
rust-version = "1.72.0" | ||
|
||
[dependencies] | ||
nom = "7.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use nom::{bytes::complete::take_till, IResult}; | ||
|
||
#[allow(dead_code)] // TEMPORARY | ||
pub(crate) fn line(input: &str) -> IResult<&str, &str> { | ||
take_till(|c| c == '\n')(input) | ||
.map(|ri| trim_rem_start_matches(ri, '\n')) | ||
.map(|ri| trim_rem_end_matches(ri, '\r')) | ||
} | ||
|
||
#[allow(dead_code)] // TEMPORARY | ||
fn trim_rem_start_matches<'a>(rem_inp: (&'a str, &'a str), c: char) -> (&'a str, &'a str) { | ||
if let Some(rem) = rem_inp.0.strip_prefix(c) { | ||
(rem, rem_inp.1) | ||
} else { | ||
rem_inp | ||
} | ||
} | ||
|
||
#[allow(dead_code)] // TEMPORARY | ||
fn trim_rem_end_matches<'a>(rem_inp: (&'a str, &'a str), c: char) -> (&'a str, &'a str) { | ||
if let Some(inp) = rem_inp.1.strip_suffix(c) { | ||
(rem_inp.0, inp) | ||
} else { | ||
rem_inp | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//! Contains various primitive parsing routines. | ||
//! Not part of the public API surface. | ||
mod line; | ||
#[allow(unused_imports)] | ||
pub(crate) use line::line; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ | |
#![allow(clippy::unwrap_used)] | ||
|
||
mod fixtures; | ||
mod primitives; | ||
mod strings; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
mod fn_line { | ||
use crate::primitives::line; | ||
|
||
#[test] | ||
fn empty_source() { | ||
assert_eq!(line(""), Ok(("", ""))); | ||
} | ||
|
||
#[test] | ||
fn simple_line() { | ||
assert_eq!(line("abc"), Ok(("", "abc"))); | ||
} | ||
|
||
#[test] | ||
fn consumes_lf() { | ||
// Should consume but not return \n. | ||
assert_eq!(line("abc\ndef"), Ok(("def", "abc"))); | ||
} | ||
|
||
#[test] | ||
fn consumes_crlf() { | ||
// Should consume but not return \r\n. | ||
assert_eq!(line("abc\r\ndef"), Ok(("def", "abc"))); | ||
} | ||
|
||
#[test] | ||
fn doesnt_consume_lfcr() { | ||
// Should consume \n but not a subsequent \r. | ||
assert_eq!(line("abc\n\rdef"), Ok(("\rdef", "abc"))); | ||
} | ||
|
||
#[test] | ||
fn doesnt_consume_standalone_cr() { | ||
// Shouldn't terminate line at \r without \n. | ||
assert_eq!(line("abc\rdef"), Ok(("", "abc\rdef"))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod line; |