24 lines
556 B
Rust
24 lines
556 B
Rust
pub fn is_number(string: String) -> bool {
|
|
let string = string.to_lowercase();
|
|
|
|
if string.contains("inf") || string.contains("nan") {
|
|
return false;
|
|
}
|
|
|
|
let mut parts = string.split('e');
|
|
|
|
let valid_significand = match parts.next() {
|
|
Some(part) => part.parse::<f64>().is_ok(),
|
|
None => unreachable!(),
|
|
};
|
|
|
|
let valid_exponent = match parts.next() {
|
|
Some(part) => part.parse::<isize>().is_ok(),
|
|
None => true,
|
|
};
|
|
|
|
let nothing_remaining = parts.next().is_none();
|
|
|
|
valid_significand && valid_exponent && nothing_remaining
|
|
}
|