Skip to content

Commit

Permalink
gff/io/reader/line: Skip blank lines
Browse files Browse the repository at this point in the history
See _Generic Feature Format Version 3 (GFF3)_ (2020-08-18): "Blank lines
should be ignored by parsers..."
  • Loading branch information
zaeleus committed Nov 26, 2024
1 parent 2ec50fb commit ab5888c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
5 changes: 5 additions & 0 deletions noodles-gff/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@

This changes `Reader::records` to `Reader::record_bufs`.

* gff/io/reader/line: Skip blank lines.

See _Generic Feature Format Version 3 (GFF3)_ (2020-08-18): "Blank lines
should be ignored by parsers..."

* gff/line: Hoist buffer to line.

This moves the owned line buffer to `Line`. The structure is now a struct
Expand Down
37 changes: 35 additions & 2 deletions noodles-gff/src/async/io/reader/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,40 @@ pub(super) async fn read_line<R>(reader: &mut R, line: &mut Line) -> io::Result<
where
R: AsyncBufRead + Unpin,
{
use crate::io::reader::line::is_blank;

let buf = &mut line.0;
buf.clear();
super::read_line(reader, buf).await

loop {
buf.clear();

let n = super::read_line(reader, buf).await?;

if n == 0 || !is_blank(buf) {
return Ok(n);
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn test_read_line() -> io::Result<()> {
const DATA: &[u8] = b"\n#comment\n\t\n";

let mut line = Line::default();
let mut lines: Vec<String> = Vec::new();

let mut src = DATA;

while read_line(&mut src, &mut line).await? != 0 {
lines.push(line.as_ref().into());
}

assert_eq!(lines, [String::from("#comment")]);

Ok(())
}
}
2 changes: 1 addition & 1 deletion noodles-gff/src/io/reader.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! GFF reader and iterators.
mod line;
pub(crate) mod line;
mod line_bufs;
mod record_bufs;

Expand Down
39 changes: 37 additions & 2 deletions noodles-gff/src/io/reader/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,41 @@ where
R: BufRead,
{
let buf = &mut line.0;
buf.clear();
super::read_line(reader, buf)

loop {
buf.clear();

let n = super::read_line(reader, buf)?;

if n == 0 || !is_blank(buf) {
return Ok(n);
}
}
}

pub(crate) fn is_blank(s: &str) -> bool {
s.chars().all(char::is_whitespace)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_read_line() -> io::Result<()> {
const DATA: &[u8] = b"\n#comment\n\t\n";

let mut line = Line::default();
let mut lines: Vec<String> = Vec::new();

let mut src = DATA;

while read_line(&mut src, &mut line)? != 0 {
lines.push(line.as_ref().into());
}

assert_eq!(lines, [String::from("#comment")]);

Ok(())
}
}

0 comments on commit ab5888c

Please sign in to comment.