chore(cli): apply clippy fixes

This commit is contained in:
Amaan Qureshi 2024-02-04 01:30:33 -05:00
parent 1fb16a72ac
commit 04ff704bca
No known key found for this signature in database
GPG key ID: E67890ADC4227273
49 changed files with 1094 additions and 1277 deletions

View file

@ -28,7 +28,7 @@ pub enum NfaState {
},
}
#[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq, Default)]
pub struct Nfa {
pub states: Vec<NfaState>,
}
@ -47,40 +47,36 @@ pub struct NfaTransition {
pub states: Vec<u32>,
}
impl Default for Nfa {
fn default() -> Self {
Self { states: Vec::new() }
}
}
const END: u32 = char::MAX as u32 + 1;
impl CharacterSet {
/// Create a character set with a single character.
pub fn empty() -> Self {
CharacterSet { ranges: Vec::new() }
pub const fn empty() -> Self {
Self { ranges: Vec::new() }
}
/// Create a character set with a given *inclusive* range of characters.
#[allow(clippy::single_range_in_vec_init)]
pub fn from_range(mut first: char, mut last: char) -> Self {
if first > last {
swap(&mut first, &mut last);
}
CharacterSet {
Self {
ranges: vec![(first as u32)..(last as u32 + 1)],
}
}
/// Create a character set with a single character.
#[allow(clippy::single_range_in_vec_init)]
pub fn from_char(c: char) -> Self {
CharacterSet {
Self {
ranges: vec![(c as u32)..(c as u32 + 1)],
}
}
/// Create a character set containing all characters *not* present
/// in this character set.
pub fn negate(mut self) -> CharacterSet {
pub fn negate(mut self) -> Self {
let mut i = 0;
let mut previous_end = 0;
while i < self.ranges.len() {
@ -110,10 +106,10 @@ impl CharacterSet {
self
}
pub fn add(mut self, other: &CharacterSet) -> Self {
pub fn add(mut self, other: &Self) -> Self {
let mut index = 0;
for range in &other.ranges {
index = self.add_int_range(index, range.start as u32, range.end as u32);
index = self.add_int_range(index, range.start, range.end);
}
self
}
@ -143,7 +139,7 @@ impl CharacterSet {
i
}
pub fn does_intersect(&self, other: &CharacterSet) -> bool {
pub fn does_intersect(&self, other: &Self) -> bool {
let mut left_ranges = self.ranges.iter();
let mut right_ranges = other.ranges.iter();
let mut left_range = left_ranges.next();
@ -163,7 +159,7 @@ impl CharacterSet {
/// Get the set of characters that are present in both this set
/// and the other set. Remove those common characters from both
/// of the operands.
pub fn remove_intersection(&mut self, other: &mut CharacterSet) -> CharacterSet {
pub fn remove_intersection(&mut self, other: &mut Self) -> Self {
let mut intersection = Vec::new();
let mut left_i = 0;
let mut right_i = 0;
@ -209,29 +205,28 @@ impl CharacterSet {
}
}
}
Ordering::Equal => {
// [ L ]
// [ R ]
if left.end < right.end {
intersection.push(left.start..left.end);
right.start = left.end;
self.ranges.remove(left_i);
}
// [ L ]
// [ R ]
else if left.end == right.end {
intersection.push(left.clone());
self.ranges.remove(left_i);
other.ranges.remove(right_i);
}
// [ L ]
// [ R ]
else if left.end > right.end {
intersection.push(right.clone());
left.start = right.end;
other.ranges.remove(right_i);
}
// [ L ]
// [ R ]
Ordering::Equal if left.end < right.end => {
intersection.push(left.start..left.end);
right.start = left.end;
self.ranges.remove(left_i);
}
// [ L ]
// [ R ]
Ordering::Equal if left.end == right.end => {
intersection.push(left.clone());
self.ranges.remove(left_i);
other.ranges.remove(right_i);
}
// [ L ]
// [ R ]
Ordering::Equal if left.end > right.end => {
intersection.push(right.clone());
left.start = right.end;
other.ranges.remove(right_i);
}
Ordering::Equal => {}
Ordering::Greater => {
// [ L ]
// [ R ]
@ -271,30 +266,30 @@ impl CharacterSet {
}
}
}
CharacterSet {
Self {
ranges: intersection,
}
}
/// Produces a `CharacterSet` containing every character in `self` that is not present in
/// `other`.
pub fn difference(mut self, mut other: CharacterSet) -> CharacterSet {
pub fn difference(mut self, mut other: Self) -> Self {
self.remove_intersection(&mut other);
self
}
/// Produces a `CharacterSet` containing every character that is in _exactly one_ of `self` or
/// `other`, but is not present in both sets.
pub fn symmetric_difference(mut self, mut other: CharacterSet) -> CharacterSet {
pub fn symmetric_difference(mut self, mut other: Self) -> Self {
self.remove_intersection(&mut other);
self.add(&other)
}
pub fn iter<'a>(&'a self) -> impl Iterator<Item = u32> + 'a {
self.ranges.iter().flat_map(|r| r.clone())
pub fn iter(&self) -> impl Iterator<Item = u32> + '_ {
self.ranges.iter().flat_map(std::clone::Clone::clone)
}
pub fn chars<'a>(&'a self) -> impl Iterator<Item = char> + 'a {
pub fn chars(&self) -> impl Iterator<Item = char> + '_ {
self.iter().filter_map(char::from_u32)
}
@ -329,11 +324,10 @@ impl CharacterSet {
prev_range_successor += 1;
}
prev_range = Some(range.start..c);
None
} else {
prev_range = Some(c..c);
None
}
None
})
.collect()
}
@ -344,13 +338,19 @@ impl CharacterSet {
}
impl Ord for CharacterSet {
fn cmp(&self, other: &CharacterSet) -> Ordering {
fn cmp(&self, other: &Self) -> Ordering {
let count_cmp = self
.ranges
.iter()
.map(|r| r.len())
.map(std::iter::ExactSizeIterator::len)
.sum::<usize>()
.cmp(&other.ranges.iter().map(|r| r.len()).sum());
.cmp(
&other
.ranges
.iter()
.map(std::iter::ExactSizeIterator::len)
.sum(),
);
if count_cmp != Ordering::Equal {
return count_cmp;
}
@ -368,12 +368,12 @@ impl Ord for CharacterSet {
}
}
}
return Ordering::Equal;
Ordering::Equal
}
}
impl PartialOrd for CharacterSet {
fn partial_cmp(&self, other: &CharacterSet) -> Option<Ordering> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
@ -390,7 +390,7 @@ impl fmt::Debug for CharacterSet {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{:?}", c)?;
write!(f, "{c:?}")?;
}
write!(f, "]")?;
Ok(())
@ -398,8 +398,8 @@ impl fmt::Debug for CharacterSet {
}
impl Nfa {
pub fn new() -> Self {
Nfa { states: Vec::new() }
pub const fn new() -> Self {
Self { states: Vec::new() }
}
pub fn last_state_id(&self) -> u32 {
@ -409,9 +409,9 @@ impl Nfa {
impl fmt::Debug for Nfa {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Nfa {{ states: {{\n")?;
writeln!(f, "Nfa {{ states: {{")?;
for (i, state) in self.states.iter().enumerate() {
write!(f, " {}: {:?},\n", i, state)?;
writeln!(f, " {i}: {state:?},")?;
}
write!(f, "}} }}")?;
Ok(())
@ -434,7 +434,7 @@ impl<'a> NfaCursor<'a> {
}
pub fn force_reset(&mut self, states: Vec<u32>) {
self.state_ids = states
self.state_ids = states;
}
pub fn transition_chars(&self) -> impl Iterator<Item = (&CharacterSet, bool)> {
@ -472,9 +472,8 @@ impl<'a> NfaCursor<'a> {
let intersection = result[i].characters.remove_intersection(&mut chars);
if !intersection.is_empty() {
let mut intersection_states = result[i].states.clone();
match intersection_states.binary_search(&state) {
Err(j) => intersection_states.insert(j, state),
_ => {}
if let Err(j) = intersection_states.binary_search(&state) {
intersection_states.insert(j, state);
}
let intersection_transition = NfaTransition {
characters: intersection,
@ -824,8 +823,7 @@ mod tests {
.map(|(chars, is_sep, prec, state)| (chars, *is_sep, *prec, *state))
),
row.1,
"row {}",
i
"row {i}",
);
}
}
@ -966,12 +964,11 @@ mod tests {
row.right
);
let symm_difference = row.left_only.clone().add(&mut row.right_only.clone());
let symm_difference = row.left_only.clone().add(&row.right_only);
assert_eq!(
row.left.clone().symmetric_difference(row.right.clone()),
symm_difference,
"row {}b: {:?} ~~ {:?}",
i,
"row {i}b: {:?} ~~ {:?}",
row.left,
row.right
)
@ -1066,10 +1063,7 @@ mod tests {
expected_ranges,
} in table.iter()
{
let ruled_out_chars = ruled_out_chars
.into_iter()
.map(|c: &char| *c as u32)
.collect();
let ruled_out_chars = ruled_out_chars.iter().map(|c: &char| *c as u32).collect();
let mut set = CharacterSet::empty();
for c in chars {
set = set.add_char(*c);