mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-06-03 06:41:14 -03:00
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.
This commit is contained in:
@@ -49,6 +49,7 @@
|
|||||||
mod wchar;
|
mod wchar;
|
||||||
mod wchar_ext;
|
mod wchar_ext;
|
||||||
mod wchar_ffi;
|
mod wchar_ffi;
|
||||||
|
mod wcstringutil;
|
||||||
mod wgetopt;
|
mod wgetopt;
|
||||||
mod wutil;
|
mod wutil;
|
||||||
|
|
||||||
|
|||||||
30
fish-rust/src/wcstringutil.rs
Normal file
30
fish-rust/src/wcstringutil.rs
Normal file
@@ -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"
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -6,7 +6,6 @@
|
|||||||
pub mod wcstoi;
|
pub mod wcstoi;
|
||||||
mod wrealpath;
|
mod wrealpath;
|
||||||
|
|
||||||
use crate::wchar::{wstr, WString};
|
|
||||||
pub(crate) use gettext::{wgettext, wgettext_fmt};
|
pub(crate) use gettext::{wgettext, wgettext_fmt};
|
||||||
pub use normalize_path::*;
|
pub use normalize_path::*;
|
||||||
pub(crate) use printf::sprintf;
|
pub(crate) use printf::sprintf;
|
||||||
@@ -29,30 +28,3 @@ pub fn perror(s: &str) {
|
|||||||
let _ = stderr.write_all(slice);
|
let _ = stderr.write_all(slice);
|
||||||
let _ = stderr.write_all(b"\n");
|
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"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::wchar::{wstr, WString, L};
|
use crate::wchar::{wstr, WString, L};
|
||||||
use crate::wutil::join_strings;
|
use crate::wcstringutil::join_strings;
|
||||||
|
|
||||||
/// Given an input path, "normalize" it:
|
/// Given an input path, "normalize" it:
|
||||||
/// 1. Collapse multiple /s into a single /, except maybe at the beginning.
|
/// 1. Collapse multiple /s into a single /, except maybe at the beginning.
|
||||||
|
|||||||
Reference in New Issue
Block a user