From 6a63520785e65d8bfe9fd9e4f1db39d7ffab045d Mon Sep 17 00:00:00 2001 From: "D.Loh" Date: Thu, 6 Feb 2020 20:30:58 -0800 Subject: eliminate panic on len not lie on boundary check boundary, add truncate len per 1 as needed till checking pass --- askama_shared/src/filters/mod.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'askama_shared/src') diff --git a/askama_shared/src/filters/mod.rs b/askama_shared/src/filters/mod.rs index f5d2720..11216a5 100644 --- a/askama_shared/src/filters/mod.rs +++ b/askama_shared/src/filters/mod.rs @@ -161,7 +161,11 @@ pub fn truncate(s: &dyn fmt::Display, len: &usize) -> Result { if s.len() < *len { Ok(s) } else { - s.truncate(*len); + let mut real_len = *len; + while !s.is_char_boundary(real_len) { + real_len += 1; + } + s.truncate(real_len); s.push_str("..."); Ok(s) } -- cgit