From 179de609b592febe80aa648a5e5391c4d643bc56 Mon Sep 17 00:00:00 2001 From: Alex Lyon Date: Sun, 28 Apr 2019 03:49:18 -0700 Subject: [PATCH] Replace trim_{left,right} with trim_{start,end} and co. (using Rerast) --- src/fmt/parasplit.rs | 2 +- src/paste/paste.rs | 4 ++-- src/ptx/ptx.rs | 4 ++-- src/stdbuf/stdbuf.rs | 4 ++-- src/tsort/tsort.rs | 2 +- src/uname/uname.rs | 2 +- tests/common/util.rs | 4 ++-- util/rewrite_rules.rs | 18 ++++++++++++++++++ 8 files changed, 29 insertions(+), 11 deletions(-) mode change 100755 => 100644 src/tsort/tsort.rs create mode 100644 util/rewrite_rules.rs diff --git a/src/fmt/parasplit.rs b/src/fmt/parasplit.rs index 42ceccf81..5ffe4593e 100644 --- a/src/fmt/parasplit.rs +++ b/src/fmt/parasplit.rs @@ -520,7 +520,7 @@ impl<'a> WordSplit<'a> { impl<'a> WordSplit<'a> { fn new<'b>(opts: &'b FmtOptions, string: &'b str) -> WordSplit<'b> { // wordsplits *must* start at a non-whitespace character - let trim_string = string.trim_left(); + let trim_string = string.trim_start(); WordSplit { opts, string: trim_string, diff --git a/src/paste/paste.rs b/src/paste/paste.rs index 0b5c13635..8ad29568b 100644 --- a/src/paste/paste.rs +++ b/src/paste/paste.rs @@ -94,7 +94,7 @@ fn paste(filenames: Vec, serial: bool, delimiters: String) { match file.read_line(&mut line) { Ok(0) => break, Ok(_) => { - output.push_str(line.trim_right()); + output.push_str(line.trim_end()); output.push_str(&delimiters[delim_count % delimiters.len()]); } Err(e) => crash!(1, "{}", e.to_string()), @@ -118,7 +118,7 @@ fn paste(filenames: Vec, serial: bool, delimiters: String) { eof[i] = true; eof_count += 1; } - Ok(_) => output.push_str(line.trim_right()), + Ok(_) => output.push_str(line.trim_end()), Err(e) => crash!(1, "{}", e.to_string()), } } diff --git a/src/ptx/ptx.rs b/src/ptx/ptx.rs index 7ae696bc9..cc30242d5 100644 --- a/src/ptx/ptx.rs +++ b/src/ptx/ptx.rs @@ -416,7 +416,7 @@ fn format_tex_line(config: &Config, word_ref: &WordRef, line: &str, reference: & output.push_str(&format!("\\{} ", config.macro_name)); let all_before = if config.input_ref { let before = &line[0..word_ref.position]; - adjust_tex_str(before.trim().trim_left_matches(reference)) + adjust_tex_str(before.trim().trim_start_matches(reference)) } else { adjust_tex_str(&line[0..word_ref.position]) }; @@ -447,7 +447,7 @@ fn format_roff_line(config: &Config, word_ref: &WordRef, line: &str, reference: output.push_str(&format!(".{}", config.macro_name)); let all_before = if config.input_ref { let before = &line[0..word_ref.position]; - adjust_roff_str(before.trim().trim_left_matches(reference)) + adjust_roff_str(before.trim().trim_start_matches(reference)) } else { adjust_roff_str(&line[0..word_ref.position]) }; diff --git a/src/stdbuf/stdbuf.rs b/src/stdbuf/stdbuf.rs index ab7258e9a..1b58ad5a2 100644 --- a/src/stdbuf/stdbuf.rs +++ b/src/stdbuf/stdbuf.rs @@ -93,8 +93,8 @@ fn print_usage(opts: &Options) { } fn parse_size(size: &str) -> Option { - let ext = size.trim_left_matches(|c: char| c.is_digit(10)); - let num = size.trim_right_matches(|c: char| c.is_alphabetic()); + let ext = size.trim_start_matches(|c: char| c.is_digit(10)); + let num = size.trim_end_matches(|c: char| c.is_alphabetic()); let mut recovered = num.to_owned(); recovered.push_str(ext); if recovered != size { diff --git a/src/tsort/tsort.rs b/src/tsort/tsort.rs old mode 100755 new mode 100644 index 145dd61fe..f930e620d --- a/src/tsort/tsort.rs +++ b/src/tsort/tsort.rs @@ -79,7 +79,7 @@ pub fn uumain(args: Vec) -> i32 { let mut line = String::new(); match reader.read_line(&mut line) { Ok(_) => { - let tokens: Vec = line.trim_right() + let tokens: Vec = line.trim_end() .split_whitespace() .map(|s| s.to_owned()) .collect(); diff --git a/src/uname/uname.rs b/src/uname/uname.rs index 05fce6366..7adc1b4e9 100644 --- a/src/uname/uname.rs +++ b/src/uname/uname.rs @@ -122,7 +122,7 @@ pub fn uumain(args: Vec) -> i32 { output.push_str(HOST_OS); output.push_str(" "); } - println!("{}", output.trim_right()); + println!("{}", output.trim_end()); 0 } diff --git a/tests/common/util.rs b/tests/common/util.rs index 8c46e6288..43e83f1e6 100644 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -110,8 +110,8 @@ impl CmdResult { /// stderr_only is a better choice unless stdout may or will be non-empty pub fn stderr_is>(&self, msg: T) -> Box<&CmdResult> { assert_eq!( - String::from(msg.as_ref()).trim_right(), - self.stderr.trim_right() + String::from(msg.as_ref()).trim_end(), + self.stderr.trim_end() ); Box::new(self) } diff --git a/util/rewrite_rules.rs b/util/rewrite_rules.rs new file mode 100644 index 000000000..6aba8166c --- /dev/null +++ b/util/rewrite_rules.rs @@ -0,0 +1,18 @@ +//! Rules to update the codebase using Rerast + +/// Converts try!() to ? +fn try_to_question_mark>(r: Result) -> Result { + replace!(try!(r) => r?); + unreachable!() +} + +fn trim_left_to_start(s: &str) { + replace!(s.trim_left() => s.trim_start()); + replace!(s.trim_right() => s.trim_end()); +} + +fn trim_left_matches_to_start bool>(s: &str, inner: P) { + replace!(s.trim_left_matches(inner) => s.trim_start_matches(inner)); + replace!(s.trim_right_matches(inner) => s.trim_end_matches(inner)); +} +