diff --git a/fish-rust/src/lib.rs b/fish-rust/src/lib.rs index e0af59d0d..09c26a2ec 100644 --- a/fish-rust/src/lib.rs +++ b/fish-rust/src/lib.rs @@ -49,6 +49,7 @@ mod wchar; mod wchar_ext; mod wchar_ffi; +mod wcstringutil; mod wgetopt; mod wutil; diff --git a/fish-rust/src/wcstringutil.rs b/fish-rust/src/wcstringutil.rs new file mode 100644 index 000000000..0fa5f820e --- /dev/null +++ b/fish-rust/src/wcstringutil.rs @@ -0,0 +1,30 @@ +//! 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" + ); +} diff --git a/fish-rust/src/wutil/mod.rs b/fish-rust/src/wutil/mod.rs index f3954790a..2da5179ea 100644 --- a/fish-rust/src/wutil/mod.rs +++ b/fish-rust/src/wutil/mod.rs @@ -6,7 +6,6 @@ pub mod wcstoi; mod wrealpath; -use crate::wchar::{wstr, WString}; pub(crate) use gettext::{wgettext, wgettext_fmt}; pub use normalize_path::*; pub(crate) use printf::sprintf; @@ -29,30 +28,3 @@ pub fn perror(s: &str) { let _ = stderr.write_all(slice); let _ = stderr.write_all(b"\n"); } - -/// 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" - ); -} diff --git a/fish-rust/src/wutil/normalize_path.rs b/fish-rust/src/wutil/normalize_path.rs index a26eaa68d..304d9ba48 100644 --- a/fish-rust/src/wutil/normalize_path.rs +++ b/fish-rust/src/wutil/normalize_path.rs @@ -1,5 +1,5 @@ use crate::wchar::{wstr, WString, L}; -use crate::wutil::join_strings; +use crate::wcstringutil::join_strings; /// Given an input path, "normalize" it: /// 1. Collapse multiple /s into a single /, except maybe at the beginning.