fish-shell/fish-rust/src/wcstringutil.rs
Johannes Altmanninger ed3a0b2bc3 Move join_strings into wcstringutil.rs
On the C++ side it lives in wcstringutil.cpp.  We should probably keep
it there until we have ported the entirety of that file.
2023-04-02 15:17:06 +02:00

31 lines
801 B
Rust

//! Helper functions for working with wcstring.
use crate::wchar::{wstr, WString};
/// Joins strings with a separator.
pub fn join_strings(strs: &[&wstr], sep: char) -> WString {
if strs.is_empty() {
return WString::new();
}
let capacity = strs.iter().fold(0, |acc, s| acc + s.len()) + strs.len() - 1;
let mut result = WString::with_capacity(capacity);
for (i, s) in strs.iter().enumerate() {
if i > 0 {
result.push(sep);
}
result.push_utfstr(s);
}
result
}
#[test]
fn test_join_strings() {
use crate::wchar::L;
assert_eq!(join_strings(&[], '/'), "");
assert_eq!(join_strings(&[L!("foo")], '/'), "foo");
assert_eq!(
join_strings(&[L!("foo"), L!("bar"), L!("baz")], '/'),
"foo/bar/baz"
);
}