From 8079381d013718f65a2f02df37791a9f494e6957 Mon Sep 17 00:00:00 2001 From: Marc Schoolderman Date: Fri, 23 Aug 2024 22:44:10 +0200 Subject: [PATCH] overhaul of suggested_break --- src/log/syslog.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/log/syslog.rs b/src/log/syslog.rs index 4501dc768..756456b81 100644 --- a/src/log/syslog.rs +++ b/src/log/syslog.rs @@ -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) } }