Skip to content

Commit bdcba3b

Browse files
committed
dirname: get rid of needless Cow
1 parent 22bd036 commit bdcba3b

1 file changed

Lines changed: 11 additions & 12 deletions

File tree

src/uu/dirname/src/dirname.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// file that was distributed with this source code.
55

66
use clap::{Arg, ArgAction, Command};
7-
use std::borrow::Cow;
87
use std::ffi::OsString;
98
#[cfg(unix)]
109
use uucore::display::print_verbatim;
@@ -39,17 +38,17 @@ mod options {
3938
/// - GNU: <https://www.gnu.org/software/coreutils/manual/html_node/dirname-invocation.html>
4039
///
4140
/// See issue #8910 and similar fix in basename (#8373, commit c5268a897).
42-
fn dirname_string_manipulation(path_bytes: &[u8]) -> Cow<'_, [u8]> {
41+
fn dirname_string_manipulation(path_bytes: &[u8]) -> &[u8] {
4342
if path_bytes.is_empty() {
44-
return Cow::Borrowed(b".");
43+
return b".";
4544
}
4645

4746
let mut bytes = path_bytes;
4847

4948
// Step 1: Strip trailing slashes (but not if the entire path is slashes)
5049
let all_slashes = bytes.iter().all(|&b| b == b'/');
5150
if all_slashes {
52-
return Cow::Borrowed(b"/");
51+
return b"/";
5352
}
5453

5554
while bytes.len() > 1 && bytes.ends_with(b"/") {
@@ -69,12 +68,12 @@ fn dirname_string_manipulation(path_bytes: &[u8]) -> Cow<'_, [u8]> {
6968
if slash_start == 0 {
7069
// Result would be empty
7170
return if path_bytes.starts_with(b"/") {
72-
Cow::Borrowed(b"/")
71+
b"/"
7372
} else {
74-
Cow::Borrowed(b".")
73+
b"."
7574
};
7675
}
77-
return Cow::Borrowed(&bytes[..slash_start]);
76+
return &bytes[..slash_start];
7877
}
7978
}
8079

@@ -89,14 +88,14 @@ fn dirname_string_manipulation(path_bytes: &[u8]) -> Cow<'_, [u8]> {
8988
}
9089

9190
if result.is_empty() {
92-
return Cow::Borrowed(b"/");
91+
return b"/";
9392
}
9493

95-
return Cow::Borrowed(result);
94+
return result;
9695
}
9796

9897
// No slash found, return "."
99-
Cow::Borrowed(b".")
98+
b"."
10099
}
101100

102101
#[uucore::main(no_signals)]
@@ -122,13 +121,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
122121
#[cfg(unix)]
123122
{
124123
use std::os::unix::ffi::OsStrExt;
125-
let result_os = std::ffi::OsStr::from_bytes(&result);
124+
let result_os = std::ffi::OsStr::from_bytes(result);
126125
print_verbatim(result_os).unwrap();
127126
}
128127
#[cfg(not(unix))]
129128
{
130129
// On non-Unix, fall back to lossy conversion
131-
if let Ok(s) = std::str::from_utf8(&result) {
130+
if let Ok(s) = std::str::from_utf8(result) {
132131
print!("{s}");
133132
} else {
134133
// Fallback for non-UTF-8 paths on non-Unix systems

0 commit comments

Comments
 (0)