1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 12:37:49 +00:00

uu+tests: use strip_prefix and strip_suffix

This commit is contained in:
Thomas Queiroz 2021-10-23 22:41:51 -03:00
parent d72a135b42
commit 007f1b9f84
No known key found for this signature in database
GPG key ID: 229D2DDF7ECA5F8F
5 changed files with 19 additions and 39 deletions

View file

@ -131,21 +131,15 @@ fn basename(fullname: &str, suffix: &str) -> String {
// Convert to path buffer and get last path component // Convert to path buffer and get last path component
let pb = PathBuf::from(path); let pb = PathBuf::from(path);
match pb.components().last() { 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(), 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()
}

View file

@ -2103,11 +2103,9 @@ fn get_security_context(config: &Config, p_buf: &Path, must_dereference: bool) -
} }
Ok(None) => substitute_string, Ok(None) => substitute_string,
Ok(Some(context)) => { Ok(Some(context)) => {
let mut context = context.as_bytes(); let context = context.as_bytes();
if context.ends_with(&[0]) {
// TODO: replace with `strip_prefix()` when MSRV >= 1.51 let context = context.strip_suffix(&[0]).unwrap_or(context);
context = &context[..context.len() - 1]
};
String::from_utf8(context.to_vec()).unwrap_or_else(|e| { String::from_utf8(context.to_vec()).unwrap_or_else(|e| {
show_warning!( show_warning!(
"getting security context of: {}: {}", "getting security context of: {}: {}",

View file

@ -193,16 +193,13 @@ pub fn read<T: Read>(
/// Split `read` into `Line`s, and add them to `lines`. /// Split `read` into `Line`s, and add them to `lines`.
fn parse_lines<'a>( fn parse_lines<'a>(
mut read: &'a str, read: &'a str,
lines: &mut Vec<Line<'a>>, lines: &mut Vec<Line<'a>>,
line_data: &mut LineData<'a>, line_data: &mut LineData<'a>,
separator: u8, separator: u8,
settings: &GlobalSettings, settings: &GlobalSettings,
) { ) {
// Strip a trailing separator. TODO: Once our MinRustV is 1.45 or above, use strip_suffix() instead. let read = read.strip_suffix(separator as char).unwrap_or(read);
if read.ends_with(separator as char) {
read = &read[..read.len() - 1];
}
assert!(lines.is_empty()); assert!(lines.is_empty());
assert!(line_data.selections.is_empty()); assert!(line_data.selections.is_empty());

View file

@ -163,12 +163,11 @@ pub fn strip_minus_from_mode(args: &mut Vec<String>) -> bool {
if arg == "--" { if arg == "--" {
break; break;
} }
if arg.starts_with('-') { if let Some(arg_stripped) = arg.strip_prefix('-') {
if let Some(second) = arg.chars().nth(1) { if let Some(second) = arg.chars().nth(1) {
match second { match second {
'r' | 'w' | 'x' | 'X' | 's' | 't' | 'u' | 'g' | 'o' | '0'..='7' => { '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_stripped.to_string();
*arg = arg[1..arg.len()].to_string();
return true; return true;
} }
_ => {} _ => {}

View file

@ -728,20 +728,12 @@ impl AtPath {
// Source: // Source:
// http://stackoverflow.com/questions/31439011/getfinalpathnamebyhandle-without-prepended // http://stackoverflow.com/questions/31439011/getfinalpathnamebyhandle-without-prepended
let prefix = "\\\\?\\"; let prefix = "\\\\?\\";
// FixME: replace ...
#[allow(clippy::manual_strip)] if let Some(stripped) = s.strip_prefix(prefix) {
if s.starts_with(prefix) { String::from(stripped)
String::from(&s[prefix.len()..])
} else { } else {
s s
} }
// ... with ...
// if let Some(stripped) = s.strip_prefix(prefix) {
// String::from(stripped)
// } else {
// s
// }
// ... when using MSRV with stabilized `strip_prefix()`
} }
} }