Skip to content

Commit f01d1e8

Browse files
authored
dirname: use Cow::Borrowed to avoid unnecessary heap allocations (#10294)
+ remove useless comments
1 parent d943324 commit f01d1e8

File tree

2 files changed

+2
-17
lines changed

2 files changed

+2
-17
lines changed

src/uu/dirname/src/dirname.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fn dirname_string_manipulation(path_bytes: &[u8]) -> Cow<'_, [u8]> {
7474
Cow::Borrowed(b".")
7575
};
7676
}
77-
return Cow::Owned(bytes[..slash_start].to_vec());
77+
return Cow::Borrowed(&bytes[..slash_start]);
7878
}
7979
}
8080

@@ -92,7 +92,7 @@ fn dirname_string_manipulation(path_bytes: &[u8]) -> Cow<'_, [u8]> {
9292
return Cow::Borrowed(b"/");
9393
}
9494

95-
return Cow::Owned(result.to_vec());
95+
return Cow::Borrowed(result);
9696
}
9797

9898
// No slash found, return "."

tests/by-util/test_dirname.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ fn test_emoji_handling() {
106106

107107
#[test]
108108
fn test_trailing_dot() {
109-
// Basic case: path ending with /. should return parent without stripping last component
110-
// This matches GNU coreutils behavior and fixes issue #8910
111109
new_ucmd!()
112110
.arg("/home/dos/.")
113111
.succeeds()
@@ -183,26 +181,19 @@ fn test_trailing_dot_non_utf8() {
183181
use std::ffi::OsStr;
184182
use std::os::unix::ffi::OsStrExt;
185183

186-
// Create a path with non-UTF-8 bytes ending in /.
187184
let non_utf8_bytes = b"/test_\xFF\xFE/.";
188185
let non_utf8_path = OsStr::from_bytes(non_utf8_bytes);
189186

190-
// Test that dirname handles non-UTF-8 paths with /. suffix
191187
let result = new_ucmd!().arg(non_utf8_path).succeeds();
192188

193-
// The output should be the path without the /. suffix
194189
let output = result.stdout_str_lossy();
195190
assert!(!output.is_empty());
196191
assert!(output.contains("test_"));
197-
// Should not contain the . at the end
198192
assert!(!output.trim().ends_with('.'));
199193
}
200194

201195
#[test]
202196
fn test_existing_behavior_preserved() {
203-
// Ensure we didn't break existing test cases
204-
// These tests verify backward compatibility
205-
206197
// Normal paths without /. should work as before
207198
new_ucmd!().arg("/home/dos").succeeds().stdout_is("/home\n");
208199

@@ -237,9 +228,6 @@ fn test_multiple_paths_comprehensive() {
237228

238229
#[test]
239230
fn test_all_dot_slash_variations() {
240-
// Tests for all the cases mentioned in issue #8910 comment
241-
// https://github.com/uutils/coreutils/issues/8910#issuecomment-3408735720
242-
243231
new_ucmd!().arg("foo//.").succeeds().stdout_is("foo\n");
244232

245233
new_ucmd!().arg("foo///.").succeeds().stdout_is("foo\n");
@@ -256,9 +244,6 @@ fn test_all_dot_slash_variations() {
256244

257245
#[test]
258246
fn test_dot_slash_component_preservation() {
259-
// Ensure that /. components in the middle are preserved
260-
// These should NOT be normalized away
261-
262247
new_ucmd!().arg("a/./b").succeeds().stdout_is("a/.\n");
263248

264249
new_ucmd!()

0 commit comments

Comments
 (0)