mirror of
https://github.com/fish-shell/fish-shell.git
synced 2024-12-26 12:13:46 +08:00
ed3a0b2bc3
On the C++ side it lives in wcstringutil.cpp. We should probably keep it there until we have ported the entirety of that file.
31 lines
801 B
Rust
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"
|
|
);
|
|
}
|