-
Notifications
You must be signed in to change notification settings - Fork 390
Expand file tree
/
Copy pathlib.rs
More file actions
59 lines (52 loc) · 1.89 KB
/
lib.rs
File metadata and controls
59 lines (52 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::fmt::Debug;
/// Returns a possibly modified version of `s` that fits within the specified bounds (in terms of
/// the number of UTF-8 characters).
///
/// More precisely, middle characters are removed such that the return value has at most `max_len`
/// characters. Some examples:
/// ```
/// use ic_nervous_system_string::clamp_string_len;
/// println!("{}", clamp_string_len("abcdef", 5)); // a...f
/// println!("{}", clamp_string_len("abcde", 5)); // abcde
/// println!("{}", clamp_string_len("abcd", 5)); // abcd
/// ```
///
/// This is analogous clamp method on numeric types in that this makes the value bounded.
pub fn clamp_string_len(s: &str, max_len: usize) -> String {
// Collect into a vector so that we can safely index the input.
let chars: Vec<_> = s.chars().collect();
if max_len <= 3 {
return chars.into_iter().take(max_len).collect();
}
if chars.len() <= max_len {
return s.to_string();
}
let ellipsis = "...";
let content_len = max_len - ellipsis.len();
let tail_len = content_len / 2;
let head_len = content_len - tail_len;
let tail_begin = chars.len() - tail_len;
format!(
"{}{}{}",
chars[..head_len].iter().collect::<String>(),
ellipsis,
chars[tail_begin..].iter().collect::<String>(),
)
}
pub fn clamp_debug_len(object: &impl Debug, max_len: usize) -> String {
clamp_string_len(&format!("{object:#?}"), max_len)
}
/// Hex-encodes bytes, truncating with a summary if longer than `max_bytes`.
pub fn humanize_blob(bytes: &[u8], max_bytes: usize) -> String {
if bytes.len() <= max_bytes {
bytes.iter().map(|b| format!("{b:02x}")).collect()
} else {
let hex: String = bytes[..max_bytes]
.iter()
.map(|b| format!("{b:02x}"))
.collect();
format!("{hex}... ({} bytes total)", bytes.len())
}
}
#[cfg(test)]
mod tests;