From ed3a0b2bc3b96c76af6efcab856c82e4c3fa8f43 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sun, 26 Mar 2023 13:34:51 +0200 Subject: [PATCH] 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. --- fish-rust/src/lib.rs | 1 + fish-rust/src/wcstringutil.rs | 30 +++++++++++++++++++++++++++ fish-rust/src/wutil/mod.rs | 28 ------------------------- fish-rust/src/wutil/normalize_path.rs | 2 +- 4 files changed, 32 insertions(+), 29 deletions(-) create mode 100644 fish-rust/src/wcstringutil.rs 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 wait_handle; 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 wcstod; 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.