aboutsummaryrefslogtreecommitdiff
path: root/base/src/format.rs
blob: 46b2da684b7882c744abbad8e9593dc14c699744 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use std::borrow::Cow;

pub fn rejoin(s: &[&str], splits: &[char]) -> Cow<'static, str> {
    let len = s.iter().map(|s| s.len()).fold(0, |a, b| a + b);
    let mut out = s.iter()
                   .zip(splits.iter())
                   .fold(String::with_capacity(len), |mut s, (b, &split)| {
                       s.push_str(b);
                       s.push(split);
                       s
                   });
    out.pop();
    Cow::Owned(out)
}

pub fn strip_one(s: &str) -> String {
    if s.len() >= 2 { s[1..(s.len() - 1)].to_owned() } else { String::new() }
}