diff --git a/src/uu/fmt/src/fmt.rs b/src/uu/fmt/src/fmt.rs index 3a494c868..3a02c6429 100644 --- a/src/uu/fmt/src/fmt.rs +++ b/src/uu/fmt/src/fmt.rs @@ -49,11 +49,9 @@ pub struct FmtOptions { tagged: bool, mail: bool, split_only: bool, - use_prefix: bool, - prefix: String, + prefix: Option, xprefix: bool, - use_anti_prefix: bool, - anti_prefix: String, + anti_prefix: Option, xanti_prefix: bool, uniform: bool, quick: bool, @@ -83,19 +81,8 @@ impl FmtOptions { let xprefix = matches.contains_id(OPT_EXACT_PREFIX); let xanti_prefix = matches.contains_id(OPT_SKIP_PREFIX); - let mut prefix = String::new(); - let mut use_prefix = false; - if let Some(s) = matches.get_one::(OPT_PREFIX).map(String::from) { - prefix = s; - use_prefix = true; - }; - - let mut anti_prefix = String::new(); - let mut use_anti_prefix = false; - if let Some(s) = matches.get_one::(OPT_SKIP_PREFIX).map(String::from) { - anti_prefix = s; - use_anti_prefix = true; - }; + let prefix = matches.get_one::(OPT_PREFIX).map(String::from); + let anti_prefix = matches.get_one::(OPT_SKIP_PREFIX).map(String::from); let mut width = 75; let mut goal = 70; @@ -143,10 +130,8 @@ impl FmtOptions { uniform, quick, split_only, - use_prefix, prefix, xprefix, - use_anti_prefix, anti_prefix, xanti_prefix, width, diff --git a/src/uu/fmt/src/parasplit.rs b/src/uu/fmt/src/parasplit.rs index 311ddbc9b..f22400dff 100644 --- a/src/uu/fmt/src/parasplit.rs +++ b/src/uu/fmt/src/parasplit.rs @@ -80,24 +80,20 @@ impl<'a> FileLines<'a> { /// returns true if this line should be formatted fn match_prefix(&self, line: &str) -> (bool, usize) { - if !self.opts.use_prefix { + let Some(prefix) = &self.opts.prefix else { return (true, 0); - } + }; - FileLines::match_prefix_generic(&self.opts.prefix[..], line, self.opts.xprefix) + FileLines::match_prefix_generic(prefix, line, self.opts.xprefix) } /// returns true if this line should be formatted fn match_anti_prefix(&self, line: &str) -> bool { - if !self.opts.use_anti_prefix { + let Some(anti_prefix) = &self.opts.anti_prefix else { return true; - } + }; - match FileLines::match_prefix_generic( - &self.opts.anti_prefix[..], - line, - self.opts.xanti_prefix, - ) { + match FileLines::match_prefix_generic(anti_prefix, line, self.opts.xanti_prefix) { (true, _) => false, (_, _) => true, } @@ -176,7 +172,7 @@ impl<'a> Iterator for FileLines<'a> { // not truly blank we will not allow mail headers on the // following line) if pmatch - && n[poffset + self.opts.prefix.len()..] + && n[poffset + self.opts.prefix.as_ref().map_or(0, |s| s.len())..] .chars() .all(char::is_whitespace) { @@ -190,7 +186,7 @@ impl<'a> Iterator for FileLines<'a> { } // figure out the indent, prefix, and prefixindent ending points - let prefix_end = poffset + self.opts.prefix.len(); + let prefix_end = poffset + self.opts.prefix.as_ref().map_or(0, |s| s.len()); let (indent_end, prefix_len, indent_len) = self.compute_indent(&n[..], prefix_end); Some(Line::FormatLine(FileLine { @@ -357,7 +353,7 @@ impl<'a> Iterator for ParagraphStream<'a> { } } else if in_mail { // lines following mail headers must begin with spaces - if fl.indent_end == 0 || (self.opts.use_prefix && fl.pfxind_end == 0) { + if fl.indent_end == 0 || (self.opts.prefix.is_some() && fl.pfxind_end == 0) { break; // this line does not begin with spaces } } else if !second_done {