mirror of
https://github.com/chatmail/core.git
synced 2026-04-17 21:46:35 +03:00
remove unwrap-flag from dc_truncate() - it was never really used on-purpose
This commit is contained in:
committed by
holger krekel
parent
5cc26762c2
commit
0e4031348f
@@ -21,8 +21,8 @@ pub(crate) fn dc_exactly_one_bit_set(v: i32) -> bool {
|
|||||||
|
|
||||||
/// Shortens a string to a specified length and adds "..." or "[...]" to the end of
|
/// Shortens a string to a specified length and adds "..." or "[...]" to the end of
|
||||||
/// the shortened string.
|
/// the shortened string.
|
||||||
pub(crate) fn dc_truncate(buf: &str, approx_chars: usize, do_unwrap: bool) -> Cow<str> {
|
pub(crate) fn dc_truncate(buf: &str, approx_chars: usize) -> Cow<str> {
|
||||||
let ellipse = if do_unwrap { "..." } else { "[...]" };
|
let ellipse = "[...]";
|
||||||
|
|
||||||
let count = buf.chars().count();
|
let count = buf.chars().count();
|
||||||
if approx_chars > 0 && count > approx_chars + ellipse.len() {
|
if approx_chars > 0 && count > approx_chars + ellipse.len() {
|
||||||
@@ -538,54 +538,42 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_dc_truncate_1() {
|
fn test_dc_truncate_1() {
|
||||||
let s = "this is a little test string";
|
let s = "this is a little test string";
|
||||||
assert_eq!(dc_truncate(s, 16, false), "this is a [...]");
|
assert_eq!(dc_truncate(s, 16), "this is a [...]");
|
||||||
assert_eq!(dc_truncate(s, 16, true), "this is a ...");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_dc_truncate_2() {
|
fn test_dc_truncate_2() {
|
||||||
assert_eq!(dc_truncate("1234", 2, false), "1234");
|
assert_eq!(dc_truncate("1234", 2), "1234");
|
||||||
assert_eq!(dc_truncate("1234", 2, true), "1234");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_dc_truncate_3() {
|
fn test_dc_truncate_3() {
|
||||||
assert_eq!(dc_truncate("1234567", 1, false), "1[...]");
|
assert_eq!(dc_truncate("1234567", 1), "1[...]");
|
||||||
assert_eq!(dc_truncate("1234567", 1, true), "1...");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_dc_truncate_4() {
|
fn test_dc_truncate_4() {
|
||||||
assert_eq!(dc_truncate("123456", 4, false), "123456");
|
assert_eq!(dc_truncate("123456", 4), "123456");
|
||||||
assert_eq!(dc_truncate("123456", 4, true), "123456");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_dc_truncate_edge() {
|
fn test_dc_truncate_edge() {
|
||||||
assert_eq!(dc_truncate("", 4, false), "");
|
assert_eq!(dc_truncate("", 4), "");
|
||||||
assert_eq!(dc_truncate("", 4, true), "");
|
|
||||||
|
|
||||||
assert_eq!(dc_truncate("\n hello \n world", 4, false), "\n [...]");
|
assert_eq!(dc_truncate("\n hello \n world", 4), "\n [...]");
|
||||||
assert_eq!(dc_truncate("\n hello \n world", 4, true), "\n ...");
|
|
||||||
|
|
||||||
|
assert_eq!(dc_truncate("𐠈0Aᝮa𫝀®!ꫛa¡0A𐢧00𐹠®A 丽ⷐએ", 1), "𐠈[...]");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
dc_truncate("𐠈0Aᝮa𫝀®!ꫛa¡0A𐢧00𐹠®A 丽ⷐએ", 1, false),
|
dc_truncate("𐠈0Aᝮa𫝀®!ꫛa¡0A𐢧00𐹠®A 丽ⷐએ", 0),
|
||||||
"𐠈[...]"
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
dc_truncate("𐠈0Aᝮa𫝀®!ꫛa¡0A𐢧00𐹠®A 丽ⷐએ", 0, false),
|
|
||||||
"𐠈0Aᝮa𫝀®!ꫛa¡0A𐢧00𐹠®A 丽ⷐએ"
|
"𐠈0Aᝮa𫝀®!ꫛa¡0A𐢧00𐹠®A 丽ⷐએ"
|
||||||
);
|
);
|
||||||
|
|
||||||
// 9 characters, so no truncation
|
// 9 characters, so no truncation
|
||||||
assert_eq!(
|
assert_eq!(dc_truncate("𑒀ὐ¢🜀\u{1e01b}A a🟠", 6), "𑒀ὐ¢🜀\u{1e01b}A a🟠",);
|
||||||
dc_truncate("𑒀ὐ¢🜀\u{1e01b}A a🟠", 6, false),
|
|
||||||
"𑒀ὐ¢🜀\u{1e01b}A a🟠",
|
|
||||||
);
|
|
||||||
|
|
||||||
// 12 characters, truncation
|
// 12 characters, truncation
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
dc_truncate("𑒀ὐ¢🜀\u{1e01b}A a🟠bcd", 6, false),
|
dc_truncate("𑒀ὐ¢🜀\u{1e01b}A a🟠bcd", 6),
|
||||||
"𑒀ὐ¢🜀\u{1e01b}A[...]",
|
"𑒀ὐ¢🜀\u{1e01b}A[...]",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -701,11 +689,10 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_dc_truncate(
|
fn test_dc_truncate(
|
||||||
buf: String,
|
buf: String,
|
||||||
approx_chars in 0..10000usize,
|
approx_chars in 0..10000usize
|
||||||
do_unwrap: bool,
|
|
||||||
) {
|
) {
|
||||||
let res = dc_truncate(&buf, approx_chars, do_unwrap);
|
let res = dc_truncate(&buf, approx_chars);
|
||||||
let el_len = if do_unwrap { 3 } else { 5 };
|
let el_len = 5;
|
||||||
let l = res.chars().count();
|
let l = res.chars().count();
|
||||||
if approx_chars > 0 {
|
if approx_chars > 0 {
|
||||||
assert!(
|
assert!(
|
||||||
@@ -719,11 +706,7 @@ mod tests {
|
|||||||
|
|
||||||
if approx_chars > 0 && buf.chars().count() > approx_chars + el_len {
|
if approx_chars > 0 && buf.chars().count() > approx_chars + el_len {
|
||||||
let l = res.len();
|
let l = res.len();
|
||||||
if do_unwrap {
|
assert_eq!(&res[l-5..l], "[...]", "missing ellipsis in {}", &res);
|
||||||
assert_eq!(&res[l-3..l], "...", "missing ellipsis in {}", &res);
|
|
||||||
} else {
|
|
||||||
assert_eq!(&res[l-5..l], "[...]", "missing ellipsis in {}", &res);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -443,7 +443,7 @@ impl Message {
|
|||||||
pub fn get_text(&self) -> Option<String> {
|
pub fn get_text(&self) -> Option<String> {
|
||||||
self.text
|
self.text
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|text| dc_truncate(text, 30000, false).to_string())
|
.map(|text| dc_truncate(text, 30000).to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_filename(&self) -> Option<String> {
|
pub fn get_filename(&self) -> Option<String> {
|
||||||
@@ -814,7 +814,7 @@ pub fn get_msg_info(context: &Context, msg_id: MsgId) -> String {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
let rawtxt = rawtxt.unwrap_or_default();
|
let rawtxt = rawtxt.unwrap_or_default();
|
||||||
let rawtxt = dc_truncate(rawtxt.trim(), 100_000, false);
|
let rawtxt = dc_truncate(rawtxt.trim(), 100_000);
|
||||||
|
|
||||||
let fts = dc_timestamp_to_str(msg.get_timestamp());
|
let fts = dc_timestamp_to_str(msg.get_timestamp());
|
||||||
ret += &format!("Sent: {}", fts);
|
ret += &format!("Sent: {}", fts);
|
||||||
@@ -1150,10 +1150,10 @@ pub fn get_summarytext_by_raw(
|
|||||||
if text.as_ref().is_empty() {
|
if text.as_ref().is_empty() {
|
||||||
prefix
|
prefix
|
||||||
} else if prefix.is_empty() {
|
} else if prefix.is_empty() {
|
||||||
dc_truncate(text.as_ref(), approx_characters, true).to_string()
|
dc_truncate(text.as_ref(), approx_characters).to_string()
|
||||||
} else {
|
} else {
|
||||||
let tmp = format!("{} – {}", prefix, text.as_ref());
|
let tmp = format!("{} – {}", prefix, text.as_ref());
|
||||||
dc_truncate(&tmp, approx_characters, true).to_string()
|
dc_truncate(&tmp, approx_characters).to_string()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
prefix
|
prefix
|
||||||
|
|||||||
Reference in New Issue
Block a user