Skip to content

Commit

Permalink
Reduce number of split_at calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael A. Plikk committed Sep 6, 2018
1 parent 38d287f commit 986c772
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions clippy_lints/src/literal_representation.rs
Expand Up @@ -155,17 +155,18 @@ impl<'a> DigitInfo<'a> {
(Some(p), s)
};

let len = sans_prefix.len();
let mut last_d = '\0';
for (d_idx, d) in sans_prefix.char_indices() {
let suffix_start = if last_d == '_' {
d_idx - 1
} else {
d_idx
};
let (digits, suffix) = sans_prefix.split_at(suffix_start);
if !float && (d == 'i' || d == 'u') ||
float && (d == 'f' || d == 'e' || d == 'E') ||
!float && is_mistyped_suffix(suffix) {
!float && is_possible_suffix_index(&sans_prefix, suffix_start, len) {
let (digits, suffix) = sans_prefix.split_at(suffix_start);
return Self {
digits,
radix,
Expand Down Expand Up @@ -557,3 +558,8 @@ impl LiteralRepresentation {
fn is_mistyped_suffix(suffix: &str) -> bool {
["_8", "_16", "_32", "_64"].contains(&suffix)
}

fn is_possible_suffix_index(lit: &str, idx: usize, len: usize) -> bool {
((len > 3 && idx == len - 3) || (len > 2 && idx == len - 2)) &&
is_mistyped_suffix(lit.split_at(idx).1)
}

0 comments on commit 986c772

Please sign in to comment.