Skip to content

Commit

Permalink
overhaul of suggested_break
Browse files Browse the repository at this point in the history
  • Loading branch information
squell committed Aug 23, 2024
1 parent 0a86606 commit 8079381
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/log/syslog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,15 @@ fn floor_char_boundary(data: &str, mut index: usize) -> usize {
/// This function WILL return a non-zero result if `max_size` is large enough to fit
/// at least the first character of `message`.
fn suggested_break(message: &str, max_size: usize) -> usize {
let mut truncate_boundary = floor_char_boundary(message, max_size);

// don't overzealously truncate log messages
truncate_boundary = message[..truncate_boundary]
.rfind(|c: char| c.is_ascii_whitespace())
.unwrap_or(truncate_boundary);

if truncate_boundary == 0 {
truncate_boundary
// method A: try to split the message on an ascii white space character
// method B: split on the utf8 character boundary that consumes the most data
if let Some(pos) = message[1..max_size]
.bytes()
.rposition(|c| c.is_ascii_whitespace())
{
pos + 1
} else {
max_size
floor_char_boundary(message, max_size)
}
}

Expand Down

0 comments on commit 8079381

Please sign in to comment.