Skip to content

Commit

Permalink
fix: fixed whitespace corruption in case of unparsed data
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus committed Jul 9, 2021
1 parent c2ed777 commit c3554ad
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ categories = ["command-line-utilities"]
description = "Utility for viewing json-formatted log files."
keywords = ["cli", "human", "log"]
name = "hl"
version = "0.9.2"
version = "0.9.3"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
25 changes: 14 additions & 11 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,34 +141,37 @@ impl App {
formatter: &mut RecordFormatter,
buf: &mut Vec<u8>,
) {
for data in segment.data().split(|c| *c == b'\n') {
let data = trim_right(data, |ch| ch == b'\r');
for data in rtrim(segment.data(), b'\n').split(|c| *c == b'\n') {
if data.len() == 0 {
buf.push(b'\n');
continue;
}
let mut stream = json::Deserializer::from_slice(data).into_iter::<RawRecord>();
let mut some = false;
while let Some(Ok(record)) = stream.next() {
some = true;
let record = parser.parse(record);
if record.matches(&self.options.filter) {
formatter.format_record(buf, &record);
}
}
let remainder = trim_right(&data[stream.byte_offset()..], |ch| match ch {
b'\r' | b'\n' | b' ' | b'\t' => true,
_ => false,
});
if remainder.len() > 0 {
let remainder = if some {
&data[stream.byte_offset()..]
} else {
data
};
if remainder.len() != 0 {
buf.extend_from_slice(remainder);
buf.push(b'\n');
}
}
}
}

fn trim_right<'a, F: Fn(u8) -> bool>(slice: &'a [u8], predicate: F) -> &'a [u8] {
if let Some(pos) = slice.iter().rposition(|&ch| !predicate(ch)) {
&slice[..pos + 1]
fn rtrim<'a>(s: &'a [u8], c: u8) -> &'a [u8] {
if s.len() > 0 && s[s.len() - 1] == c {
&s[..s.len() - 1]
} else {
&slice[0..0]
s
}
}

0 comments on commit c3554ad

Please sign in to comment.