Add exact size support to the --size filter (#669)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Platon Pronko · committed by David Peter · parent 2338b3b · Browse files · 4 files·+20 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+20 added−7 removed
- tests:+7 added−0 removed
- other:+13 added−7 removed
Provenance
- Commit
e97a1e9- Tree
9076165- Author
Platon Pronko <platon7pronko@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
2338b3b
4 files changed+20 added−7 removed
modified:doc/fd.1+2 added
177 unmodified lines178 .IP '+'179 file size must be greater than or equal to this180 .IP '-'181 file size must be less than or equal to this182+.P183+If neither '+' nor '-' is specified, file size must be exactly equal to this.184 .IP 'NUM'185 The numeric size (e.g. 500)186 .IP 'UNIT'187 The units for NUM. They are not case-sensitive.139 unmodified linesmodified:src/filter/size.rs+9 added-6 removed
1 use lazy_static::lazy_static;2 use regex::Regex;3 4 lazy_static! {5- static ref SIZE_CAPTURES: Regex = Regex::new(r"(?i)^([+-])(\d+)(b|[kmgt]i?b?)$").unwrap();5+ static ref SIZE_CAPTURES: Regex = Regex::new(r"(?i)^([+-]?)(\d+)(b|[kmgt]i?b?)$").unwrap();6 }7 8 #[derive(Clone, Copy, Debug, PartialEq)]9 pub enum SizeFilter {10 Max(u64),11 Min(u64),12+ Equals(u64),13 }14 15 // SI prefixes (powers of 10)16 const KILO: u64 = 1000;32 unmodified lines49 _ => return None,50 };51 52 let size = quantity * multiplier;52- Some(match limit_kind {53- "+" => SizeFilter::Min(size),54- _ => SizeFilter::Max(size),55- })53+ match limit_kind {54+ "+" => Some(SizeFilter::Min(size)),55+ "-" => Some(SizeFilter::Max(size)),56+ "" => Some(SizeFilter::Equals(size)),57+ _ => None58+ }59 }60 61 pub fn is_within(&self, size: u64) -> bool {62 match *self {63 SizeFilter::Max(limit) => size <= limit,64 SizeFilter::Min(limit) => size >= limit,65+ SizeFilter::Equals(limit) => size == limit,66 }67 }68 }69 103 unmodified lines173 }174 175 // Invalid parse data176 gen_size_filter_failure! {173- ensure_missing_symbol_returns_none: "10M",177 ensure_missing_number_returns_none: "+g",178 ensure_missing_unit_returns_none: "+18",179 ensure_bad_format_returns_none_1: "$10M",180 ensure_bad_format_returns_none_2: "badval",32 unmodified linesmodified:src/app.rs+2 added-1 removed
388 unmodified lines389 .help("Limit results based on the size of files.")390 .long_help(391 "Limit results based on the size of files using the format <+-><NUM><UNIT>.\n \392 '+': file size must be greater than or equal to this\n \393- '-': file size must be less than or equal to this\n \393+ '-': file size must be less than or equal to this\n\394+ If neither '+' nor '-' is specified, file size must be exactly equal to this.\n \395 'NUM': The numeric size (e.g. 500)\n \396 'UNIT': The units for NUM. They are not case-sensitive.\n\397 Allowed unit values:\n \398 'b': bytes\n \184 unmodified linesmodified:tests/tests.rs+7 added
1385 unmodified lines1386 );1387 1388 // Zero sized files.1389 te.assert_output(&["", "--size", "-0B"], "0_bytes.foo");1390+ te.assert_output(&["", "--size", "0B"], "0_bytes.foo");1391+ te.assert_output(&["", "--size=0B"], "0_bytes.foo");1392+ te.assert_output(&["", "-S", "0B"], "0_bytes.foo");1393 1394 // Files with 2 bytes or more.1395 te.assert_output(1396 &["", "--size", "+2B"],8 unmodified lines1405 1406 // Files with size between 1 byte and 11 bytes.1407 te.assert_output(&["", "--size", "+1B", "--size", "-11B"], "11_bytes.foo");1408 1409+ // Files with size equal 11 bytes.1410+ te.assert_output(&["", "--size", "11B"], "11_bytes.foo");1411+1412 // Files with size between 1 byte and 30 bytes.1413 te.assert_output(1414 &["", "--size", "+1B", "--size", "-30B"],1415 "11_bytes.foo23 unmodified lines1439 te.assert_output(&["", "--size", "+3k", "--size", "-3ki"], "3_kilobytes.foo");1440 1441 // Files with size equal 4 kibibytes.1442 te.assert_output(&["", "--size", "+4ki", "--size", "-4ki"], "4_kibibytes.foo");1443+ te.assert_output(&["", "--size", "4ki"], "4_kibibytes.foo");1444 }1445 1446 #[cfg(test)]1447 fn create_file_with_modified<P: AsRef<Path>>(path: P, duration_in_secs: u64) {226 unmodified lines