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

Replace trim_{left,right} with trim_{start,end} and co. (using Rerast)

This commit is contained in:
Alex Lyon 2019-04-28 03:49:18 -07:00
parent c7d115b1ad
commit 179de609b5
8 changed files with 29 additions and 11 deletions

View file

@ -520,7 +520,7 @@ impl<'a> WordSplit<'a> {
impl<'a> WordSplit<'a> { impl<'a> WordSplit<'a> {
fn new<'b>(opts: &'b FmtOptions, string: &'b str) -> WordSplit<'b> { fn new<'b>(opts: &'b FmtOptions, string: &'b str) -> WordSplit<'b> {
// wordsplits *must* start at a non-whitespace character // wordsplits *must* start at a non-whitespace character
let trim_string = string.trim_left(); let trim_string = string.trim_start();
WordSplit { WordSplit {
opts, opts,
string: trim_string, string: trim_string,

View file

@ -94,7 +94,7 @@ fn paste(filenames: Vec<String>, serial: bool, delimiters: String) {
match file.read_line(&mut line) { match file.read_line(&mut line) {
Ok(0) => break, Ok(0) => break,
Ok(_) => { Ok(_) => {
output.push_str(line.trim_right()); output.push_str(line.trim_end());
output.push_str(&delimiters[delim_count % delimiters.len()]); output.push_str(&delimiters[delim_count % delimiters.len()]);
} }
Err(e) => crash!(1, "{}", e.to_string()), Err(e) => crash!(1, "{}", e.to_string()),
@ -118,7 +118,7 @@ fn paste(filenames: Vec<String>, serial: bool, delimiters: String) {
eof[i] = true; eof[i] = true;
eof_count += 1; eof_count += 1;
} }
Ok(_) => output.push_str(line.trim_right()), Ok(_) => output.push_str(line.trim_end()),
Err(e) => crash!(1, "{}", e.to_string()), Err(e) => crash!(1, "{}", e.to_string()),
} }
} }

View file

@ -416,7 +416,7 @@ fn format_tex_line(config: &Config, word_ref: &WordRef, line: &str, reference: &
output.push_str(&format!("\\{} ", config.macro_name)); output.push_str(&format!("\\{} ", config.macro_name));
let all_before = if config.input_ref { let all_before = if config.input_ref {
let before = &line[0..word_ref.position]; 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 { } else {
adjust_tex_str(&line[0..word_ref.position]) 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)); output.push_str(&format!(".{}", config.macro_name));
let all_before = if config.input_ref { let all_before = if config.input_ref {
let before = &line[0..word_ref.position]; 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 { } else {
adjust_roff_str(&line[0..word_ref.position]) adjust_roff_str(&line[0..word_ref.position])
}; };

View file

@ -93,8 +93,8 @@ fn print_usage(opts: &Options) {
} }
fn parse_size(size: &str) -> Option<u64> { fn parse_size(size: &str) -> Option<u64> {
let ext = size.trim_left_matches(|c: char| c.is_digit(10)); let ext = size.trim_start_matches(|c: char| c.is_digit(10));
let num = size.trim_right_matches(|c: char| c.is_alphabetic()); let num = size.trim_end_matches(|c: char| c.is_alphabetic());
let mut recovered = num.to_owned(); let mut recovered = num.to_owned();
recovered.push_str(ext); recovered.push_str(ext);
if recovered != size { if recovered != size {

2
src/tsort/tsort.rs Executable file → Normal file
View file

@ -79,7 +79,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
let mut line = String::new(); let mut line = String::new();
match reader.read_line(&mut line) { match reader.read_line(&mut line) {
Ok(_) => { Ok(_) => {
let tokens: Vec<String> = line.trim_right() let tokens: Vec<String> = line.trim_end()
.split_whitespace() .split_whitespace()
.map(|s| s.to_owned()) .map(|s| s.to_owned())
.collect(); .collect();

View file

@ -122,7 +122,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
output.push_str(HOST_OS); output.push_str(HOST_OS);
output.push_str(" "); output.push_str(" ");
} }
println!("{}", output.trim_right()); println!("{}", output.trim_end());
0 0
} }

View file

@ -110,8 +110,8 @@ impl CmdResult {
/// stderr_only is a better choice unless stdout may or will be non-empty /// stderr_only is a better choice unless stdout may or will be non-empty
pub fn stderr_is<T: AsRef<str>>(&self, msg: T) -> Box<&CmdResult> { pub fn stderr_is<T: AsRef<str>>(&self, msg: T) -> Box<&CmdResult> {
assert_eq!( assert_eq!(
String::from(msg.as_ref()).trim_right(), String::from(msg.as_ref()).trim_end(),
self.stderr.trim_right() self.stderr.trim_end()
); );
Box::new(self) Box::new(self)
} }

18
util/rewrite_rules.rs Normal file
View file

@ -0,0 +1,18 @@
//! Rules to update the codebase using Rerast
/// Converts try!() to ?
fn try_to_question_mark<T, E, X: From<E>>(r: Result<T, E>) -> Result<T, X> {
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<P: FnMut(char) -> 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));
}