From 007f1b9f84a39bad9c5c36c785322df491f675d3 Mon Sep 17 00:00:00 2001 From: Thomas Queiroz Date: Sat, 23 Oct 2021 22:41:51 -0300 Subject: [PATCH] uu+tests: use strip_prefix and strip_suffix --- src/uu/basename/src/basename.rs | 24 +++++++++--------------- src/uu/ls/src/ls.rs | 8 +++----- src/uu/sort/src/chunks.rs | 7 ++----- src/uucore/src/lib/features/mode.rs | 5 ++--- tests/common/util.rs | 14 +++----------- 5 files changed, 19 insertions(+), 39 deletions(-) diff --git a/src/uu/basename/src/basename.rs b/src/uu/basename/src/basename.rs index 464c0e4f1..f7f4a3d08 100644 --- a/src/uu/basename/src/basename.rs +++ b/src/uu/basename/src/basename.rs @@ -131,21 +131,15 @@ fn basename(fullname: &str, suffix: &str) -> String { // Convert to path buffer and get last path component let pb = PathBuf::from(path); match pb.components().last() { - Some(c) => strip_suffix(c.as_os_str().to_str().unwrap(), suffix), + Some(c) => { + let name = c.as_os_str().to_str().unwrap(); + if name == suffix { + name.to_string() + } else { + name.strip_suffix(suffix).unwrap_or(name).to_string() + } + } + None => "".to_owned(), } } - -// can be replaced with strip_suffix once MSRV is 1.45 -#[allow(clippy::manual_strip)] -fn strip_suffix(name: &str, suffix: &str) -> String { - if name == suffix { - return name.to_owned(); - } - - if name.ends_with(suffix) { - return name[..name.len() - suffix.len()].to_owned(); - } - - name.to_owned() -} diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index 3fa3b4f8e..1b6a73b39 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -2103,11 +2103,9 @@ fn get_security_context(config: &Config, p_buf: &Path, must_dereference: bool) - } Ok(None) => substitute_string, Ok(Some(context)) => { - let mut context = context.as_bytes(); - if context.ends_with(&[0]) { - // TODO: replace with `strip_prefix()` when MSRV >= 1.51 - context = &context[..context.len() - 1] - }; + let context = context.as_bytes(); + + let context = context.strip_suffix(&[0]).unwrap_or(context); String::from_utf8(context.to_vec()).unwrap_or_else(|e| { show_warning!( "getting security context of: {}: {}", diff --git a/src/uu/sort/src/chunks.rs b/src/uu/sort/src/chunks.rs index 80d6060d4..bfe6aa73b 100644 --- a/src/uu/sort/src/chunks.rs +++ b/src/uu/sort/src/chunks.rs @@ -193,16 +193,13 @@ pub fn read( /// Split `read` into `Line`s, and add them to `lines`. fn parse_lines<'a>( - mut read: &'a str, + read: &'a str, lines: &mut Vec>, line_data: &mut LineData<'a>, separator: u8, settings: &GlobalSettings, ) { - // Strip a trailing separator. TODO: Once our MinRustV is 1.45 or above, use strip_suffix() instead. - if read.ends_with(separator as char) { - read = &read[..read.len() - 1]; - } + let read = read.strip_suffix(separator as char).unwrap_or(read); assert!(lines.is_empty()); assert!(line_data.selections.is_empty()); diff --git a/src/uucore/src/lib/features/mode.rs b/src/uucore/src/lib/features/mode.rs index 5e299f988..2206177a3 100644 --- a/src/uucore/src/lib/features/mode.rs +++ b/src/uucore/src/lib/features/mode.rs @@ -163,12 +163,11 @@ pub fn strip_minus_from_mode(args: &mut Vec) -> bool { if arg == "--" { break; } - if arg.starts_with('-') { + if let Some(arg_stripped) = arg.strip_prefix('-') { if let Some(second) = arg.chars().nth(1) { match second { 'r' | 'w' | 'x' | 'X' | 's' | 't' | 'u' | 'g' | 'o' | '0'..='7' => { - // TODO: use strip_prefix() once minimum rust version reaches 1.45.0 - *arg = arg[1..arg.len()].to_string(); + *arg = arg_stripped.to_string(); return true; } _ => {} diff --git a/tests/common/util.rs b/tests/common/util.rs index c7ac816e1..e71f18573 100644 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -728,20 +728,12 @@ impl AtPath { // Source: // http://stackoverflow.com/questions/31439011/getfinalpathnamebyhandle-without-prepended let prefix = "\\\\?\\"; - // FixME: replace ... - #[allow(clippy::manual_strip)] - if s.starts_with(prefix) { - String::from(&s[prefix.len()..]) + + if let Some(stripped) = s.strip_prefix(prefix) { + String::from(stripped) } else { s } - // ... with ... - // if let Some(stripped) = s.strip_prefix(prefix) { - // String::from(stripped) - // } else { - // s - // } - // ... when using MSRV with stabilized `strip_prefix()` } }