Skip to main content

dcbor/
string_util.rs

1import_stdlib!();
2
3pub fn flanked(s: &str, left: &str, right: &str) -> String {
4    left.to_owned() + s + right
5}
6
7pub fn is_printable(c: char) -> bool {
8    !c.is_ascii() || (32..=126).contains(&(c as u32))
9}
10
11pub fn sanitized(string: &str) -> Option<String> {
12    let mut has_printable = false;
13    let chars: Vec<_> = string
14        .chars()
15        .map(|c| {
16            if is_printable(c) {
17                has_printable = true;
18                c
19            } else {
20                '.'
21            }
22        })
23        .collect();
24    if !has_printable {
25        None
26    } else {
27        Some(chars.into_iter().collect())
28    }
29}