summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKornel <kornel@geekhood.net>2019-09-03 12:31:31 +0100
committerKornel <kornel@geekhood.net>2019-09-03 12:31:31 +0100
commit2e81ad1b118c6b1be590ad9a5e38ff081603f4e2 (patch)
treecce3c549d457ac52b2105aedde60dd168beba728
parentdfc5ddc6a96572c9592c4ef01127c9c8a1de00b4 (diff)
Mojibake
-rw-r--r--front_end/src/front_end.rs9
-rw-r--r--server/Cargo.toml2
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 <kornel@geekhood.net>"]
edition = "2018"
description = "Crates.rs web server"