Skip to content

Commit

Permalink
Add a primitive to parse a \n- or \r\n-terminated line (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
scouten authored Dec 11, 2023
1 parent 85b4c60 commit b8098fc
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ repository = "https://github.com/scouten/asciidoc-parser"
rust-version = "1.72.0"

[dependencies]
nom = "7.1"
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![deny(warnings)]
#![doc = include_str!("../README.md")]

pub(crate) mod primitives;
pub mod strings;

#[cfg(test)]
Expand Down
26 changes: 26 additions & 0 deletions src/primitives/line.rs
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
}
}
6 changes: 6 additions & 0 deletions src/primitives/mod.rs
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;
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
#![allow(clippy::unwrap_used)]

mod fixtures;
mod primitives;
mod strings;
37 changes: 37 additions & 0 deletions src/tests/primitives/line.rs
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")));
}
}
1 change: 1 addition & 0 deletions src/tests/primitives/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod line;

0 comments on commit b8098fc

Please sign in to comment.