You might be entirely correct. I noticed a small perf issue with the backwards scanning and had it rewrite it not using rfind("_"). First time I prompted it, though, it didn't actually manage to fix the problem and gave me some mild gaslighting until I told it to explicitly remove the rfind() call. Now I think the result should be comparable to or beat a regexp, but it is getting quite a bit more complex:
fn strip_suffix(s: &str) -> String {
let mut idx = s.len();
for (i, c) in s.char_indices().rev() {
if c == '_' {
idx = i;
break;
} else if !c.is_ascii_digit() {
return s.to_string();
}
}
if idx == s.len() {
return s.to_string();
}
if s[idx+1..].chars().all(|c| c.is_ascii_digit()) {
return s[..idx].to_string();
}
s.to_string()
}
.char_indexes().rev() there worries me a bit as well now that I look at it... haven't tested that at all.