40 lines
1.0 KiB
Rust
40 lines
1.0 KiB
Rust
//! String helpers Slint lacks (wired into the `SfText` global by `glue!`).
|
|
|
|
/// Without its last character.
|
|
pub fn drop_last(s: &str) -> String {
|
|
let mut s = s.to_string();
|
|
s.pop();
|
|
s
|
|
}
|
|
|
|
/// `n` bullets (a masked password).
|
|
pub fn dots(n: usize) -> String {
|
|
"•".repeat(n)
|
|
}
|
|
|
|
/// The characters of a key event's text that can be typed: no controls, no
|
|
/// Slint special keys (arrows etc. arrive as private-use code points).
|
|
pub fn typable(s: &str) -> String {
|
|
s.chars().filter(|c| !c.is_control() && !('\u{E000}'..='\u{F8FF}').contains(c)).collect()
|
|
}
|
|
|
|
/// At most `n` characters.
|
|
pub fn truncate(s: &str, n: usize) -> String {
|
|
s.chars().take(n).collect()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn edits() {
|
|
assert_eq!(drop_last("abő"), "ab");
|
|
assert_eq!(drop_last(""), "");
|
|
assert_eq!(dots(3), "•••");
|
|
assert_eq!(truncate("árvíz", 3), "árv");
|
|
assert_eq!(typable("a\u{F700}\n"), "a");
|
|
assert_eq!(typable("Ő"), "Ő");
|
|
}
|
|
}
|