From 2e81ad1b118c6b1be590ad9a5e38ff081603f4e2 Mon Sep 17 00:00:00 2001 From: Kornel Date: Tue, 3 Sep 2019 12:31:31 +0100 Subject: Mojibake --- front_end/src/front_end.rs | 9 ++++++++- server/Cargo.toml | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/front_end/src/front_end.rs b/front_end/src/front_end.rs index 9faf38a..2a5a954 100644 --- a/front_end/src/front_end.rs +++ b/front_end/src/front_end.rs @@ -203,11 +203,17 @@ pub fn render_static_page(out: &mut impl Write, title: String, page: &Markup, re Ok(()) } -pub fn limit_text_len<'t>(text: &'t str, len_min: usize, len_max: usize) -> Cow<'t, str> { +pub fn limit_text_len<'t>(text: &'t str, mut len_min: usize, mut len_max: usize) -> Cow<'t, str> { assert!(len_min <= len_max); if text.len() <= len_max { return text.into(); } + while !text.is_char_boundary(len_max) { + len_max += 1; + } + while !text.is_char_boundary(len_min) { + len_min += 1; + } let mut cut = &text[..len_max]; let optional = &cut[len_min..]; if let Some(pos) = optional.find(&['.',',','!','\n','?',')',']'][..]).or_else(|| optional.find(' ')) { @@ -220,6 +226,7 @@ pub fn limit_text_len<'t>(text: &'t str, len_min: usize, len_max: usize) -> Cow< fn limit_text_len_test() { assert_eq!("hello world", limit_text_len("hello world", 100, 200)); assert_eq!("hel…", limit_text_len("hello world", 1, 3)); + assert_eq!("こん…", limit_text_len("こんにちは、世界", 3, 5)); assert_eq!("hello…", limit_text_len("hello world", 1, 10)); assert_eq!("hello world…", limit_text_len("hello world! long", 1, 15)); assert_eq!("hello (world)…", limit_text_len("hello (world) long! lorem ipsum", 1, 15)); diff --git a/server/Cargo.toml b/server/Cargo.toml index c9f3462..f2147f1 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "crates-server" -version = "0.7.1" +version = "0.7.2" authors = ["Kornel "] edition = "2018" description = "Crates.rs web server" -- cgit v1.2.3