mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2026-01-15 09:41:07 +00:00
Merge pull request #2394 from youknowone/cleanup-trivial
clean up to remove unnessessary unwraps and to use ? operators
This commit is contained in:
commit
249ac23aab
7 changed files with 62 additions and 81 deletions
|
|
@ -229,10 +229,9 @@ fn unit_string_to_number(s: &str) -> Option<u64> {
|
|||
let mut offset = 0;
|
||||
let mut s_chars = s.chars().rev();
|
||||
|
||||
let (mut ch, multiple) = match s_chars.next() {
|
||||
Some('B') | Some('b') => ('B', 1000u64),
|
||||
Some(ch) => (ch, 1024u64),
|
||||
None => return None,
|
||||
let (mut ch, multiple) = match s_chars.next()? {
|
||||
'B' | 'b' => ('B', 1000u64),
|
||||
ch => (ch, 1024u64),
|
||||
};
|
||||
if ch == 'B' {
|
||||
ch = s_chars.next()?;
|
||||
|
|
|
|||
|
|
@ -247,7 +247,7 @@ fn nl<T: Read>(reader: &mut BufReader<T>, settings: &Settings) {
|
|||
let mut line_filter: fn(&str, ®ex::Regex) -> bool = pass_regex;
|
||||
for mut l in reader.lines().map(|r| r.unwrap()) {
|
||||
// Sanitize the string. We want to print the newline ourselves.
|
||||
if !l.is_empty() && l.chars().rev().next().unwrap() == '\n' {
|
||||
if l.chars().last() == Some('\n') {
|
||||
l.pop();
|
||||
}
|
||||
// Next we iterate through the individual chars to see if this
|
||||
|
|
|
|||
|
|
@ -297,30 +297,25 @@ fn parse_type_string(params: &str) -> Result<Vec<ParsedFormatterItemInfo>, Strin
|
|||
ch = chars.next();
|
||||
}
|
||||
if !decimal_size.is_empty() {
|
||||
byte_size = match decimal_size.parse() {
|
||||
Err(_) => {
|
||||
return Err(format!(
|
||||
"invalid number '{}' in format specification '{}'",
|
||||
decimal_size, params
|
||||
))
|
||||
}
|
||||
Ok(n) => n,
|
||||
}
|
||||
byte_size = decimal_size.parse().map_err(|_| {
|
||||
format!(
|
||||
"invalid number '{}' in format specification '{}'",
|
||||
decimal_size, params
|
||||
)
|
||||
})?;
|
||||
}
|
||||
}
|
||||
if is_format_dump_char(ch, &mut show_ascii_dump) {
|
||||
ch = chars.next();
|
||||
}
|
||||
|
||||
match od_format_type(type_char, byte_size) {
|
||||
Some(ft) => formats.push(ParsedFormatterItemInfo::new(ft, show_ascii_dump)),
|
||||
None => {
|
||||
return Err(format!(
|
||||
"invalid size '{}' in format specification '{}'",
|
||||
byte_size, params
|
||||
))
|
||||
}
|
||||
}
|
||||
let ft = od_format_type(type_char, byte_size).ok_or_else(|| {
|
||||
format!(
|
||||
"invalid size '{}' in format specification '{}'",
|
||||
byte_size, params
|
||||
)
|
||||
})?;
|
||||
formats.push(ParsedFormatterItemInfo::new(ft, show_ascii_dump));
|
||||
}
|
||||
|
||||
Ok(formats)
|
||||
|
|
@ -331,16 +326,13 @@ pub fn parse_format_flags_str(
|
|||
args_str: &Vec<&'static str>,
|
||||
) -> Result<Vec<FormatterItemInfo>, String> {
|
||||
let args: Vec<String> = args_str.iter().map(|s| s.to_string()).collect();
|
||||
match parse_format_flags(&args) {
|
||||
Err(e) => Err(e),
|
||||
Ok(v) => {
|
||||
// tests using this function assume add_ascii_dump is not set
|
||||
Ok(v.into_iter()
|
||||
.inspect(|f| assert!(!f.add_ascii_dump))
|
||||
.map(|f| f.formatter_item_info)
|
||||
.collect())
|
||||
}
|
||||
}
|
||||
parse_format_flags(&args).map(|v| {
|
||||
// tests using this function assume add_ascii_dump is not set
|
||||
v.into_iter()
|
||||
.inspect(|f| assert!(!f.add_ascii_dump))
|
||||
.map(|f| f.formatter_item_info)
|
||||
.collect()
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -401,18 +401,18 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
|||
|
||||
for file_group in file_groups {
|
||||
let result_options = build_options(&matches, &file_group, args.join(" "));
|
||||
let options = match result_options {
|
||||
Ok(options) => options,
|
||||
Err(err) => {
|
||||
print_error(&matches, err);
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
if result_options.is_err() {
|
||||
print_error(&matches, result_options.err().unwrap());
|
||||
return 1;
|
||||
}
|
||||
|
||||
let options = &result_options.unwrap();
|
||||
|
||||
let cmd_result = if file_group.len() == 1 {
|
||||
pr(file_group.get(0).unwrap(), options)
|
||||
let cmd_result = if let Ok(group) = file_group.iter().exactly_one() {
|
||||
pr(group, &options)
|
||||
} else {
|
||||
mpr(&file_group, options)
|
||||
mpr(&file_group, &options)
|
||||
};
|
||||
|
||||
let status = match cmd_result {
|
||||
|
|
@ -442,11 +442,12 @@ fn recreate_arguments(args: &[String]) -> Vec<String> {
|
|||
let mut arguments = args.to_owned();
|
||||
let num_option = args.iter().find_position(|x| n_regex.is_match(x.trim()));
|
||||
if let Some((pos, _value)) = num_option {
|
||||
let num_val_opt = args.get(pos + 1);
|
||||
if num_val_opt.is_some() && !num_regex.is_match(num_val_opt.unwrap()) {
|
||||
let could_be_file = arguments.remove(pos + 1);
|
||||
arguments.insert(pos + 1, format!("{}", NumberingMode::default().width));
|
||||
arguments.insert(pos + 2, could_be_file);
|
||||
if let Some(num_val_opt) = args.get(pos + 1) {
|
||||
if !num_regex.is_match(num_val_opt) {
|
||||
let could_be_file = arguments.remove(pos + 1);
|
||||
arguments.insert(pos + 1, format!("{}", NumberingMode::default().width));
|
||||
arguments.insert(pos + 2, could_be_file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -666,12 +667,14 @@ fn build_options(
|
|||
None => end_page_in_plus_option,
|
||||
};
|
||||
|
||||
if end_page.is_some() && start_page > end_page.unwrap() {
|
||||
return Err(PrError::EncounteredErrors(format!(
|
||||
"invalid --pages argument '{}:{}'",
|
||||
start_page,
|
||||
end_page.unwrap()
|
||||
)));
|
||||
if let Some(end_page) = end_page {
|
||||
if start_page > end_page {
|
||||
return Err(PrError::EncounteredErrors(format!(
|
||||
"invalid --pages argument '{}:{}'",
|
||||
start_page,
|
||||
end_page
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
let default_lines_per_page = if form_feed_used {
|
||||
|
|
@ -947,7 +950,7 @@ fn read_stream_and_create_pages(
|
|||
let current_page = x + 1;
|
||||
|
||||
current_page >= start_page
|
||||
&& (last_page.is_none() || current_page <= last_page.unwrap())
|
||||
&& last_page.map_or(true, |last_page| current_page <= last_page)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
|
@ -1030,8 +1033,7 @@ fn print_page(lines: &[FileLine], options: &OutputOptions, page: usize) -> Resul
|
|||
|
||||
let lines_written = write_columns(lines, options, out)?;
|
||||
|
||||
for index in 0..trailer_content.len() {
|
||||
let x = trailer_content.get(index).unwrap();
|
||||
for (index, x) in trailer_content.iter().enumerate() {
|
||||
out.write_all(x.as_bytes())?;
|
||||
if index + 1 != trailer_content.len() {
|
||||
out.write_all(line_separator)?;
|
||||
|
|
@ -1074,8 +1076,7 @@ fn write_columns(
|
|||
let mut offset = 0;
|
||||
for col in 0..columns {
|
||||
let mut inserted = 0;
|
||||
for i in offset..lines.len() {
|
||||
let line = lines.get(i).unwrap();
|
||||
for line in &lines[offset..] {
|
||||
if line.file_id != col {
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,18 +55,9 @@ impl Formatter for Decf {
|
|||
);
|
||||
// strip trailing zeroes
|
||||
if let Some(ref post_dec) = f_sci.post_decimal {
|
||||
let mut i = post_dec.len();
|
||||
{
|
||||
let mut it = post_dec.chars();
|
||||
while let Some(c) = it.next_back() {
|
||||
if c != '0' {
|
||||
break;
|
||||
}
|
||||
i -= 1;
|
||||
}
|
||||
}
|
||||
if i != post_dec.len() {
|
||||
f_sci.post_decimal = Some(String::from(&post_dec[0..i]));
|
||||
let trimmed = post_dec.trim_end_matches('0');
|
||||
if trimmed.len() != post_dec.len() {
|
||||
f_sci.post_decimal = Some(trimmed.to_owned());
|
||||
}
|
||||
}
|
||||
let f_fl = get_primitive_dec(
|
||||
|
|
|
|||
|
|
@ -247,8 +247,12 @@ pub fn get_primitive_dec(
|
|||
first_segment.len() as isize - 1,
|
||||
)
|
||||
} else {
|
||||
match first_segment.chars().next() {
|
||||
Some('0') => {
|
||||
match first_segment
|
||||
.chars()
|
||||
.next()
|
||||
.expect("float_common: no chars in first segment.")
|
||||
{
|
||||
'0' => {
|
||||
let it = second_segment.chars().enumerate();
|
||||
let mut m: isize = 0;
|
||||
let mut pre = String::from("0");
|
||||
|
|
@ -266,10 +270,7 @@ pub fn get_primitive_dec(
|
|||
}
|
||||
(pre, post, m)
|
||||
}
|
||||
Some(_) => (first_segment, second_segment, 0),
|
||||
None => {
|
||||
panic!("float_common: no chars in first segment.");
|
||||
}
|
||||
_ => (first_segment, second_segment, 0),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -363,10 +363,7 @@ fn parse_size(size: &str) -> Result<u64, ()> {
|
|||
// Get the numeric part of the size argument. For example, if the
|
||||
// argument is "123K", then the numeric part is "123".
|
||||
let numeric_string: String = size.chars().take_while(|c| c.is_digit(10)).collect();
|
||||
let number: u64 = match numeric_string.parse() {
|
||||
Ok(n) => n,
|
||||
Err(_) => return Err(()),
|
||||
};
|
||||
let number: u64 = numeric_string.parse().map_err(|_| ())?;
|
||||
|
||||
// Get the alphabetic units part of the size argument and compute
|
||||
// the factor it represents. For example, if the argument is "123K",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue