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

sort: improve comments

This commit is contained in:
Michael Debertol 2021-05-08 22:31:53 +02:00
parent 1afeb55881
commit d686f7e48f

View file

@ -582,11 +582,14 @@ impl FieldSelector {
self.from.field != 1 || self.from.char == 0 || self.to.is_some() self.from.field != 1 || self.from.char == 0 || self.to.is_some()
} }
fn get_selection(&self, line: &str, fields: Option<&[Field]>) -> Selection { /// Get the selection that corresponds to this selector for the line.
let mut range = SelectionRange::new(self.get_range(&line, fields)); /// If needs_fields returned false, tokens may be None.
fn get_selection(&self, line: &str, tokens: Option<&[Field]>) -> Selection {
let mut range = SelectionRange::new(self.get_range(&line, tokens));
let num_cache = if self.settings.mode == SortMode::Numeric let num_cache = if self.settings.mode == SortMode::Numeric
|| self.settings.mode == SortMode::HumanNumeric || self.settings.mode == SortMode::HumanNumeric
{ {
// Parse NumInfo for this number.
let (info, num_range) = NumInfo::parse( let (info, num_range) = NumInfo::parse(
range.get_str(&line), range.get_str(&line),
NumInfoParseSettings { NumInfoParseSettings {
@ -595,20 +598,23 @@ impl FieldSelector {
decimal_pt: Some(DECIMAL_PT), decimal_pt: Some(DECIMAL_PT),
}, },
); );
// Shorten the range to what we need to pass to numeric_str_cmp later.
range.shorten(num_range); range.shorten(num_range);
Some(Box::new(NumCache::WithInfo(info))) Some(Box::new(NumCache::WithInfo(info)))
} else if self.settings.mode == SortMode::GeneralNumeric { } else if self.settings.mode == SortMode::GeneralNumeric {
// Parse this number as f64, as this is the requirement for general numeric sorting.
let str = range.get_str(&line); let str = range.get_str(&line);
Some(Box::new(NumCache::AsF64(general_f64_parse( Some(Box::new(NumCache::AsF64(general_f64_parse(
&str[get_leading_gen(str)], &str[get_leading_gen(str)],
)))) ))))
} else { } else {
// This is not a numeric sort, so we don't need a NumCache.
None None
}; };
Selection { range, num_cache } Selection { range, num_cache }
} }
/// Look up the slice that corresponds to this selector for the given line. /// Look up the range in the line that corresponds to this selector.
/// If needs_fields returned false, tokens may be None. /// If needs_fields returned false, tokens may be None.
fn get_range<'a>(&self, line: &'a str, tokens: Option<&[Field]>) -> Range<usize> { fn get_range<'a>(&self, line: &'a str, tokens: Option<&[Field]>) -> Range<usize> {
enum Resolution { enum Resolution {
@ -1356,7 +1362,8 @@ enum GeneralF64ParseResult {
Infinity, Infinity,
} }
/// Parse the beginning string into an f64, returning -inf instead of NaN on errors. /// Parse the beginning string into a GeneralF64ParseResult.
/// Using a GeneralF64ParseResult instead of f64 is necessary to correctly order floats.
#[inline(always)] #[inline(always)]
fn general_f64_parse(a: &str) -> GeneralF64ParseResult { fn general_f64_parse(a: &str) -> GeneralF64ParseResult {
// The actual behavior here relies on Rust's implementation of parsing floating points. // The actual behavior here relies on Rust's implementation of parsing floating points.