1
0
mirror of https://github.com/uutils/coreutils synced 2024-07-05 17:08:59 +00:00

cksum/hashsum: Simplify the determine_regex function

This commit is contained in:
Sylvestre Ledru 2024-06-04 21:39:58 +02:00
parent c2292a8da6
commit 6922e7f057

View File

@ -2,7 +2,7 @@
// //
// For the full copyright and license information, please view the LICENSE // For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code. // file that was distributed with this source code.
// spell-checker:ignore anotherfile invalidchecksum // spell-checker:ignore anotherfile invalidchecksum regexes
use data_encoding::BASE64; use data_encoding::BASE64;
use os_display::Quotable; use os_display::Quotable;
@ -301,21 +301,19 @@ fn determine_regex(
input_is_stdin: bool, input_is_stdin: bool,
lines: &[String], lines: &[String],
) -> UResult<(Regex, bool)> { ) -> UResult<(Regex, bool)> {
let algo_based_regex = Regex::new(ALGO_BASED_REGEX).unwrap(); let regexes = [
let double_space_regex = Regex::new(DOUBLE_SPACE_REGEX).unwrap(); (Regex::new(ALGO_BASED_REGEX).unwrap(), true),
let single_space_regex = Regex::new(SINGLE_SPACE_REGEX).unwrap(); (Regex::new(DOUBLE_SPACE_REGEX).unwrap(), false),
let algo_based_regex_base64 = Regex::new(ALGO_BASED_REGEX_BASE64).unwrap(); (Regex::new(SINGLE_SPACE_REGEX).unwrap(), false),
(Regex::new(ALGO_BASED_REGEX_BASE64).unwrap(), true),
];
for line in lines { for line in lines {
let line_trim = line.trim(); let line_trim = line.trim();
if algo_based_regex.is_match(line_trim) { for (regex, is_algo_based) in &regexes {
return Ok((algo_based_regex, true)); if regex.is_match(line_trim) {
} else if double_space_regex.is_match(line_trim) { return Ok((regex.clone(), *is_algo_based));
return Ok((double_space_regex, false)); }
} else if single_space_regex.is_match(line_trim) {
return Ok((single_space_regex, false));
} else if algo_based_regex_base64.is_match(line_trim) {
return Ok((algo_based_regex_base64, true));
} }
} }
Err(ChecksumError::NoProperlyFormattedChecksumLinesFound { Err(ChecksumError::NoProperlyFormattedChecksumLinesFound {