1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

chore: standardize creation of empty strings

This commit is contained in:
Daniel Hofstetter 2022-11-30 10:37:59 +01:00
parent efa0fd498b
commit 9a2174ba02
15 changed files with 48 additions and 48 deletions

View file

@ -172,6 +172,6 @@ fn basename(fullname: &str, suffix: &str) -> String {
} }
} }
None => "".to_owned(), None => String::new(),
} }
} }

View file

@ -120,7 +120,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let escaped = matches.get_flag(options::ENABLE_BACKSLASH_ESCAPE); let escaped = matches.get_flag(options::ENABLE_BACKSLASH_ESCAPE);
let values: Vec<String> = match matches.get_many::<String>(options::STRING) { let values: Vec<String> = match matches.get_many::<String>(options::STRING) {
Some(s) => s.map(|s| s.to_string()).collect(), Some(s) => s.map(|s| s.to_string()).collect(),
None => vec!["".to_string()], None => vec![String::new()],
}; };
execute(no_newline, escaped, &values) execute(no_newline, escaped, &values)

View file

@ -163,7 +163,7 @@ impl<'a> Iterator for FileLines<'a> {
// Err(true) indicates that this was a linebreak, // Err(true) indicates that this was a linebreak,
// which is important to know when detecting mail headers // which is important to know when detecting mail headers
if n.chars().all(char::is_whitespace) { if n.chars().all(char::is_whitespace) {
return Some(Line::NoFormatLine("".to_owned(), true)); return Some(Line::NoFormatLine(String::new(), true));
} }
let (pmatch, poffset) = self.match_prefix(&n[..]); let (pmatch, poffset) = self.match_prefix(&n[..]);

View file

@ -2400,7 +2400,7 @@ fn display_item_long(
} }
}; };
let dfn = display_file_name(item, config, None, "".to_owned(), out).contents; let dfn = display_file_name(item, config, None, String::new(), out).contents;
write!(out, " {} {}{}", display_date(md, config), dfn, config.eol)?; write!(out, " {} {}{}", display_date(md, config), dfn, config.eol)?;
} else { } else {
@ -2473,7 +2473,7 @@ fn display_item_long(
write!(out, " {}", pad_right("?", padding.uname))?; write!(out, " {}", pad_right("?", padding.uname))?;
} }
let dfn = display_file_name(item, config, None, "".to_owned(), out).contents; let dfn = display_file_name(item, config, None, String::new(), out).contents;
let date_len = 12; let date_len = 12;
writeln!( writeln!(

View file

@ -293,7 +293,7 @@ impl Params {
// For example, if `prefix` is "a/b/c/d", then `directory` is // For example, if `prefix` is "a/b/c/d", then `directory` is
// "a/b/c" is `prefix` gets reassigned to "d". // "a/b/c" is `prefix` gets reassigned to "d".
let (directory, prefix) = if prefix.ends_with(MAIN_SEPARATOR) { let (directory, prefix) = if prefix.ends_with(MAIN_SEPARATOR) {
(prefix, "".to_string()) (prefix, String::new())
} else { } else {
let path = Path::new(&prefix); let path = Path::new(&prefix);
let directory = match path.parent() { let directory = match path.parent() {

View file

@ -90,8 +90,8 @@ impl Default for FormatOptions {
grouping: false, grouping: false,
padding: None, padding: None,
precision: None, precision: None,
prefix: String::from(""), prefix: String::new(),
suffix: String::from(""), suffix: String::new(),
zero_padding: false, zero_padding: false,
} }
} }
@ -112,8 +112,8 @@ impl FromStr for FormatOptions {
let mut iter = s.chars().peekable(); let mut iter = s.chars().peekable();
let mut options = Self::default(); let mut options = Self::default();
let mut padding = String::from(""); let mut padding = String::new();
let mut precision = String::from(""); let mut precision = String::new();
let mut double_percentage_counter = 0; let mut double_percentage_counter = 0;
// '%' chars in the prefix, if any, must appear in blocks of even length, for example: "%%%%" and // '%' chars in the prefix, if any, must appear in blocks of even length, for example: "%%%%" and

View file

@ -49,7 +49,7 @@ impl InputOffset {
(Radix::Hexadecimal, Some(l)) => format!("{:06X} ({:06X})", self.byte_pos, l), (Radix::Hexadecimal, Some(l)) => format!("{:06X} ({:06X})", self.byte_pos, l),
(Radix::Octal, None) => format!("{:07o}", self.byte_pos), (Radix::Octal, None) => format!("{:07o}", self.byte_pos),
(Radix::Octal, Some(l)) => format!("{:07o} ({:07o})", self.byte_pos, l), (Radix::Octal, Some(l)) => format!("{:07o} ({:07o})", self.byte_pos, l),
(Radix::NoPrefix, None) => String::from(""), (Radix::NoPrefix, None) => String::new(),
(Radix::NoPrefix, Some(l)) => format!("({:07o})", l), (Radix::NoPrefix, Some(l)) => format!("({:07o})", l),
} }
} }

View file

@ -322,7 +322,7 @@ fn current_tty() -> String {
.trim_start_matches("/dev/") .trim_start_matches("/dev/")
.to_owned() .to_owned()
} else { } else {
"".to_owned() String::new()
} }
} }
} }
@ -358,7 +358,7 @@ impl Who {
let cur_tty = if self.my_line_only { let cur_tty = if self.my_line_only {
current_tty() current_tty()
} else { } else {
"".to_owned() String::new()
}; };
for ut in records { for ut in records {

View file

@ -169,7 +169,7 @@ impl MountInfo {
// Why do we cast this to i32? // Why do we cast this to i32?
self.dev_id = (stat.dev() as i32).to_string(); self.dev_id = (stat.dev() as i32).to_string();
} else { } else {
self.dev_id = "".to_string(); self.dev_id = String::new();
} }
} }
// set MountInfo::dummy // set MountInfo::dummy
@ -219,7 +219,7 @@ impl MountInfo {
+ FIELDS_OFFSET + FIELDS_OFFSET
+ 1; + 1;
let mut m = Self { let mut m = Self {
dev_id: "".to_string(), dev_id: String::new(),
dev_name: raw[after_fields + 1].to_string(), dev_name: raw[after_fields + 1].to_string(),
fs_type: raw[after_fields].to_string(), fs_type: raw[after_fields].to_string(),
mount_root: raw[3].to_string(), mount_root: raw[3].to_string(),
@ -233,10 +233,10 @@ impl MountInfo {
} }
LINUX_MTAB => { LINUX_MTAB => {
let mut m = Self { let mut m = Self {
dev_id: "".to_string(), dev_id: String::new(),
dev_name: raw[0].to_string(), dev_name: raw[0].to_string(),
fs_type: raw[2].to_string(), fs_type: raw[2].to_string(),
mount_root: "".to_string(), mount_root: String::new(),
mount_dir: raw[1].to_string(), mount_dir: raw[1].to_string(),
mount_option: raw[3].to_string(), mount_option: raw[3].to_string(),
remote: false, remote: false,
@ -305,8 +305,8 @@ impl MountInfo {
dev_name, dev_name,
fs_type: fs_type.unwrap_or_default(), fs_type: fs_type.unwrap_or_default(),
mount_root, mount_root,
mount_dir: "".to_string(), mount_dir: String::new(),
mount_option: "".to_string(), mount_option: String::new(),
remote: false, remote: false,
dummy: false, dummy: false,
}; };
@ -324,7 +324,7 @@ impl MountInfo {
impl From<StatFs> for MountInfo { impl From<StatFs> for MountInfo {
fn from(statfs: StatFs) -> Self { fn from(statfs: StatFs) -> Self {
let mut info = Self { let mut info = Self {
dev_id: "".to_string(), dev_id: String::new(),
dev_name: unsafe { dev_name: unsafe {
// spell-checker:disable-next-line // spell-checker:disable-next-line
CStr::from_ptr(&statfs.f_mntfromname[0]) CStr::from_ptr(&statfs.f_mntfromname[0])
@ -343,8 +343,8 @@ impl From<StatFs> for MountInfo {
.to_string_lossy() .to_string_lossy()
.into_owned() .into_owned()
}, },
mount_root: "".to_string(), mount_root: String::new(),
mount_option: "".to_string(), mount_option: String::new(),
remote: false, remote: false,
dummy: false, dummy: false,
}; };

View file

@ -36,9 +36,9 @@ fn truncate(mut format: FormatPrimitive) -> FormatPrimitive {
if trimmed.is_empty() { if trimmed.is_empty() {
// If there are no nonzero digits after the decimal point, // If there are no nonzero digits after the decimal point,
// use integer formatting by clearing post_decimal and suffix. // use integer formatting by clearing post_decimal and suffix.
format.post_decimal = Some("".into()); format.post_decimal = Some(String::new());
if format.suffix == Some("e+00".into()) { if format.suffix == Some("e+00".into()) {
format.suffix = Some("".into()); format.suffix = Some(String::new());
} }
} else if trimmed.len() != post_dec.len() { } else if trimmed.len() != post_dec.len() {
// Otherwise, update the format to remove only the trailing // Otherwise, update the format to remove only the trailing
@ -108,7 +108,7 @@ fn round(mut format: FormatPrimitive) -> FormatPrimitive {
} else { } else {
// If the rounded post_decimal is entirely zeroes, discard // If the rounded post_decimal is entirely zeroes, discard
// it and use integer formatting instead. // it and use integer formatting instead.
post_decimal_str = "".into(); post_decimal_str = String::new();
} }
format.post_decimal = Some(post_decimal_str); format.post_decimal = Some(post_decimal_str);

View file

@ -1174,7 +1174,7 @@ fn test_ls_long_symlink_color() {
captures.get(1).unwrap().as_str().to_string(), captures.get(1).unwrap().as_str().to_string(),
captures.get(2).unwrap().as_str().to_string(), captures.get(2).unwrap().as_str().to_string(),
), ),
None => ("".to_string(), input.to_string()), None => (String::new(), input.to_string()),
} }
} }

View file

@ -57,7 +57,7 @@ fn test_long_format_multiple_users() {
// and an account that (probably) doesn't exist // and an account that (probably) doesn't exist
let runner = match std::env::var("USER") { let runner = match std::env::var("USER") {
Ok(user) => user, Ok(user) => user,
Err(_) => "".to_string(), Err(_) => String::new(),
}; };
let args = ["-l", "root", "root", "root", &runner, "no_such_user"]; let args = ["-l", "root", "root", "root", &runner, "no_such_user"];
let ts = TestScenario::new(util_name!()); let ts = TestScenario::new(util_name!());

View file

@ -2485,8 +2485,8 @@ fn test_follow_inotify_only_regular() {
p.kill().unwrap(); p.kill().unwrap();
let (buf_stdout, buf_stderr) = take_stdout_stderr(&mut p); let (buf_stdout, buf_stderr) = take_stdout_stderr(&mut p);
assert_eq!(buf_stdout, "".to_string()); assert_eq!(buf_stdout, String::new());
assert_eq!(buf_stderr, "".to_string()); assert_eq!(buf_stderr, String::new());
} }
fn take_stdout_stderr(p: &mut std::process::Child) -> (String, String) { fn take_stdout_stderr(p: &mut std::process::Child) -> (String, String) {

View file

@ -127,7 +127,7 @@ impl RandomString {
D: Distribution<u8>, D: Distribution<u8>,
{ {
if length == 0 { if length == 0 {
return String::from(""); return String::new();
} else if length == 1 { } else if length == 1 {
return if num_delimiter > 0 { return if num_delimiter > 0 {
String::from(delimiter as char) String::from(delimiter as char)

View file

@ -747,7 +747,7 @@ impl AtPath {
log_info("resolve_link", self.plus_as_string(path)); log_info("resolve_link", self.plus_as_string(path));
match fs::read_link(self.plus(path)) { match fs::read_link(self.plus(path)) {
Ok(p) => self.minus_as_string(p.to_str().unwrap()), Ok(p) => self.minus_as_string(p.to_str().unwrap()),
Err(_) => "".to_string(), Err(_) => String::new(),
} }
} }
@ -1483,7 +1483,7 @@ mod tests {
#[test] #[test]
fn test_code_is() { fn test_code_is() {
let res = CmdResult { let res = CmdResult {
bin_path: "".into(), bin_path: String::new(),
util_name: None, util_name: None,
tmpd: None, tmpd: None,
code: Some(32), code: Some(32),
@ -1498,7 +1498,7 @@ mod tests {
#[should_panic] #[should_panic]
fn test_code_is_fail() { fn test_code_is_fail() {
let res = CmdResult { let res = CmdResult {
bin_path: "".into(), bin_path: String::new(),
util_name: None, util_name: None,
tmpd: None, tmpd: None,
code: Some(32), code: Some(32),
@ -1512,7 +1512,7 @@ mod tests {
#[test] #[test]
fn test_failure() { fn test_failure() {
let res = CmdResult { let res = CmdResult {
bin_path: "".into(), bin_path: String::new(),
util_name: None, util_name: None,
tmpd: None, tmpd: None,
code: None, code: None,
@ -1527,7 +1527,7 @@ mod tests {
#[should_panic] #[should_panic]
fn test_failure_fail() { fn test_failure_fail() {
let res = CmdResult { let res = CmdResult {
bin_path: "".into(), bin_path: String::new(),
util_name: None, util_name: None,
tmpd: None, tmpd: None,
code: None, code: None,
@ -1541,7 +1541,7 @@ mod tests {
#[test] #[test]
fn test_success() { fn test_success() {
let res = CmdResult { let res = CmdResult {
bin_path: "".into(), bin_path: String::new(),
util_name: None, util_name: None,
tmpd: None, tmpd: None,
code: None, code: None,
@ -1556,7 +1556,7 @@ mod tests {
#[should_panic] #[should_panic]
fn test_success_fail() { fn test_success_fail() {
let res = CmdResult { let res = CmdResult {
bin_path: "".into(), bin_path: String::new(),
util_name: None, util_name: None,
tmpd: None, tmpd: None,
code: None, code: None,
@ -1570,7 +1570,7 @@ mod tests {
#[test] #[test]
fn test_no_stderr_output() { fn test_no_stderr_output() {
let res = CmdResult { let res = CmdResult {
bin_path: "".into(), bin_path: String::new(),
util_name: None, util_name: None,
tmpd: None, tmpd: None,
code: None, code: None,
@ -1586,7 +1586,7 @@ mod tests {
#[should_panic] #[should_panic]
fn test_no_stderr_fail() { fn test_no_stderr_fail() {
let res = CmdResult { let res = CmdResult {
bin_path: "".into(), bin_path: String::new(),
util_name: None, util_name: None,
tmpd: None, tmpd: None,
code: None, code: None,
@ -1602,7 +1602,7 @@ mod tests {
#[should_panic] #[should_panic]
fn test_no_stdout_fail() { fn test_no_stdout_fail() {
let res = CmdResult { let res = CmdResult {
bin_path: "".into(), bin_path: String::new(),
util_name: None, util_name: None,
tmpd: None, tmpd: None,
code: None, code: None,
@ -1617,7 +1617,7 @@ mod tests {
#[test] #[test]
fn test_std_does_not_contain() { fn test_std_does_not_contain() {
let res = CmdResult { let res = CmdResult {
bin_path: "".into(), bin_path: String::new(),
util_name: None, util_name: None,
tmpd: None, tmpd: None,
code: None, code: None,
@ -1633,7 +1633,7 @@ mod tests {
#[should_panic] #[should_panic]
fn test_stdout_does_not_contain_fail() { fn test_stdout_does_not_contain_fail() {
let res = CmdResult { let res = CmdResult {
bin_path: "".into(), bin_path: String::new(),
util_name: None, util_name: None,
tmpd: None, tmpd: None,
code: None, code: None,
@ -1649,7 +1649,7 @@ mod tests {
#[should_panic] #[should_panic]
fn test_stderr_does_not_contain_fail() { fn test_stderr_does_not_contain_fail() {
let res = CmdResult { let res = CmdResult {
bin_path: "".into(), bin_path: String::new(),
util_name: None, util_name: None,
tmpd: None, tmpd: None,
code: None, code: None,
@ -1664,7 +1664,7 @@ mod tests {
#[test] #[test]
fn test_stdout_matches() { fn test_stdout_matches() {
let res = CmdResult { let res = CmdResult {
bin_path: "".into(), bin_path: String::new(),
util_name: None, util_name: None,
tmpd: None, tmpd: None,
code: None, code: None,
@ -1682,7 +1682,7 @@ mod tests {
#[should_panic] #[should_panic]
fn test_stdout_matches_fail() { fn test_stdout_matches_fail() {
let res = CmdResult { let res = CmdResult {
bin_path: "".into(), bin_path: String::new(),
util_name: None, util_name: None,
tmpd: None, tmpd: None,
code: None, code: None,
@ -1699,7 +1699,7 @@ mod tests {
#[should_panic] #[should_panic]
fn test_stdout_not_matches_fail() { fn test_stdout_not_matches_fail() {
let res = CmdResult { let res = CmdResult {
bin_path: "".into(), bin_path: String::new(),
util_name: None, util_name: None,
tmpd: None, tmpd: None,
code: None, code: None,
@ -1715,7 +1715,7 @@ mod tests {
#[test] #[test]
fn test_normalized_newlines_stdout_is() { fn test_normalized_newlines_stdout_is() {
let res = CmdResult { let res = CmdResult {
bin_path: "".into(), bin_path: String::new(),
util_name: None, util_name: None,
tmpd: None, tmpd: None,
code: None, code: None,
@ -1733,7 +1733,7 @@ mod tests {
#[should_panic] #[should_panic]
fn test_normalized_newlines_stdout_is_fail() { fn test_normalized_newlines_stdout_is_fail() {
let res = CmdResult { let res = CmdResult {
bin_path: "".into(), bin_path: String::new(),
util_name: None, util_name: None,
tmpd: None, tmpd: None,
code: None, code: None,