diff options
Diffstat (limited to 'base/src/format.rs')
-rw-r--r-- | base/src/format.rs | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/base/src/format.rs b/base/src/format.rs new file mode 100644 index 0000000..46b2da6 --- /dev/null +++ b/base/src/format.rs @@ -0,0 +1,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() } +} |