Skip to content

Commit

Permalink
fix: prevent newlines from creating empty log events
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanbabcock committed Nov 24, 2024
1 parent acf7860 commit 26d3236
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
1 change: 0 additions & 1 deletion examples/audio_waveform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ pub fn main() -> Result<()> {
match event {
FfmpegEvent::Error(e) | FfmpegEvent::Log(_, e) => {
// todo: create `.inspect_err()` iter to simplify this pattern
// todo: prevent reading newline events as Unknown
println!("{e}");
}
FfmpegEvent::OutputChunk(chunk) => {
Expand Down
20 changes: 18 additions & 2 deletions src/read_until_any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
use std::io::{BufRead, ErrorKind, Result};

/// `BufRead::read_until` with multiple delimiters.
/// Reads from the provided buffer until any of the delimiter bytes match.
/// The output buffer will include the ending delimiter.
/// Also skips over zero-length reads.
/// See [`BufRead::read_until`](https://doc.rust-lang.org/std/io/trait.BufRead.html#method.read_until).
pub fn read_until_any<R: BufRead + ?Sized>(
r: &mut R,
delims: &[u8],
Expand All @@ -17,10 +20,23 @@ pub fn read_until_any<R: BufRead + ?Sized>(
Err(e) => return Err(e),
};

let start_delims = if read == 0 {
available
.iter()
.take_while(|&&b| delims.iter().any(|&d| d == b))
.count()
} else {
0
};

// NB: `memchr` crate would be faster, but it's unstable and not worth the dependency.
let first_delim_index = available
.iter()
.position(|b| delims.iter().any(|d| *d == *b));
.skip(start_delims)
.position(|&b| delims.iter().any(|&d| d == b))
.map(|i| i + start_delims);

// println!("start_delims: {start_delims}, first_delim_index: {first_delim_index:?}");

match first_delim_index {
Some(i) => {
Expand Down

0 comments on commit 26d3236

Please sign in to comment.