| 1 | use thiserror::Error; |
| 2 | |
| 3 | const FLOAT_IDENTIFIERS: [&str; 2] = ["f32", "f64"]; |
| 4 | const INTEGER_IDENTIFIERS: [&str; 10] = |
| 5 | ["u8", "u16", "u32", "u64", "u128", "i8", "i16", "i32", "i64", "i128"]; |
| 6 | const BOOLEAN_IDENTIFIERS: [&str; 2] = ["true", "false"]; |
| 7 | const NEGATIVE_SIGN: &str = "-"; |
| 8 | |
| 9 | const HEX_PREFIX: &str = "0x"; |
| 10 | const HEX_RADIX: u32 = 16; |
| 11 | |
| 12 | const BINARY_PREFIX: &str = "0b"; |
| 13 | const BINARY_RADIX: u32 = 2; |
| 14 | |
| 15 | const OCTAL_PREFIX: &str = "0o"; |
| 16 | const OCTAL_RADIX: u32 = 8; |
| 17 | |
| 18 | #[derive(Error, Debug)] |
| 19 | pub(crate) enum NumberParseError { |
| 20 | #[error("floats are not supported")] |
| 21 | FloatNotSupported, |
| 22 | #[error("invalid integer or boolean")] |
| 23 | InvalidNumberString, |
| 24 | } |
| 25 | |
| 26 | #[derive(Debug, Copy, Clone, PartialEq)] |
| 27 | pub(crate) struct ParsedNumber { |
| 28 | pub(crate) number: u128, |
| 29 | pub(crate) negative: bool, |
| 30 | pub(crate) has_integer_suffix: bool, |
| 31 | } |
| 32 | |
| 33 | /// Parses a number string. |
| 34 | pub(crate) fn parse_number_string(s: &str) -> Result<ParsedNumber, NumberParseError> { |
| 35 | let trimmed_str = s.trim().replace(" ", "").replace("_", "").to_ascii_lowercase(); |
| 36 | |
| 37 | // Check if the string is a float. |
| 38 | if !trimmed_str.starts_with(HEX_PREFIX) { |
| 39 | let contains_float_identifier = |
| 40 | FLOAT_IDENTIFIERS.iter().any(|&identifier| trimmed_str.ends_with(identifier)); |
| 41 | if trimmed_str.contains(".") || contains_float_identifier { |
| 42 | return Err(NumberParseError::FloatNotSupported); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // Check if the string is a boolean. |
| 47 | if BOOLEAN_IDENTIFIERS.contains(&trimmed_str.as_str()) { |
| 48 | return match trimmed_str.as_str() { |
| 49 | "true" => Ok(ParsedNumber { number: 1, negative: false, has_integer_suffix: false }), |
| 50 | "false" => Ok(ParsedNumber { number: 0, negative: false, has_integer_suffix: false }), |
| 51 | _ => Err(NumberParseError::InvalidNumberString), |
| 52 | }; |
| 53 | } |
| 54 | |
| 55 | let has_integer_suffix = |
| 56 | INTEGER_IDENTIFIERS.iter().any(|&identifier| trimmed_str.ends_with(identifier)); |
| 57 | |
| 58 | // Remove integer identifiers. |
| 59 | let trimmed_str = INTEGER_IDENTIFIERS |
| 60 | .iter() |
| 61 | .fold(trimmed_str, |acc, &identifier| acc.replace(identifier, "")); |
| 62 | |
| 63 | let negative_number = trimmed_str.starts_with(NEGATIVE_SIGN); |
| 64 | let trimmed_str = trimmed_str.trim_start_matches(NEGATIVE_SIGN); |
| 65 | |
| 66 | // Check if the string is a hexadecimal number. |
| 67 | if trimmed_str.starts_with(HEX_PREFIX) { |
| 68 | Ok(ParsedNumber { |
| 69 | number: u128::from_str_radix(trimmed_str.trim_start_matches(HEX_PREFIX), HEX_RADIX) |
| 70 | .unwrap(), |
| 71 | negative: negative_number, |
| 72 | has_integer_suffix, |
| 73 | }) |
| 74 | } else if trimmed_str.starts_with(BINARY_PREFIX) { |
| 75 | Ok(ParsedNumber { |
| 76 | number: u128::from_str_radix( |
| 77 | trimmed_str.trim_start_matches(BINARY_PREFIX), |
| 78 | BINARY_RADIX, |
| 79 | ) |
| 80 | .unwrap(), |
| 81 | negative: negative_number, |
| 82 | has_integer_suffix, |
| 83 | }) |
| 84 | } else if trimmed_str.starts_with(OCTAL_PREFIX) { |
| 85 | Ok(ParsedNumber { |
| 86 | number: u128::from_str_radix(trimmed_str.trim_start_matches(OCTAL_PREFIX), OCTAL_RADIX) |
| 87 | .unwrap(), |
| 88 | negative: negative_number, |
| 89 | has_integer_suffix, |
| 90 | }) |
| 91 | } else { |
| 92 | // Regular number |
| 93 | match trimmed_str.parse::<u128>() { |
| 94 | Ok(number) => { |
| 95 | Ok(ParsedNumber { number, negative: negative_number, has_integer_suffix }) |
| 96 | } |
| 97 | Err(_) => Err(NumberParseError::InvalidNumberString), |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | #[cfg(test)] |
| 103 | mod tests { |
| 104 | use super::*; |
| 105 | |
| 106 | #[test] |
| 107 | fn test_parse_decimal_string() { |
| 108 | let integer_type = parse_number_string("12").unwrap(); |
| 109 | |
| 110 | assert_eq!(integer_type, ParsedNumber { |
| 111 | number: 12, |
| 112 | negative: false, |
| 113 | has_integer_suffix: false |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn test_parse_negative_decimal_string() { |
| 119 | let integer_type = parse_number_string("-12").unwrap(); |
| 120 | |
| 121 | assert_eq!(integer_type, ParsedNumber { |
| 122 | number: 12, |
| 123 | negative: true, |
| 124 | has_integer_suffix: false |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn test_parse_decimal_integer_suffix_string() { |
| 130 | let integer_type = parse_number_string("-12i8").unwrap(); |
| 131 | |
| 132 | assert_eq!(integer_type, ParsedNumber { |
| 133 | number: 12, |
| 134 | negative: true, |
| 135 | has_integer_suffix: true |
| 136 | }); |
| 137 | } |
| 138 | |
| 139 | #[test] |
| 140 | fn test_parse_hex_string() { |
| 141 | let integer_type = parse_number_string("0x1234").unwrap(); |
| 142 | |
| 143 | assert_eq!(integer_type, ParsedNumber { |
| 144 | number: 0x1234, |
| 145 | negative: false, |
| 146 | has_integer_suffix: false |
| 147 | }); |
| 148 | } |
| 149 | |
| 150 | #[test] |
| 151 | fn test_parse_octal_string() { |
| 152 | let integer_type = parse_number_string("0o12").unwrap(); |
| 153 | |
| 154 | assert_eq!(integer_type, ParsedNumber { |
| 155 | number: 10, |
| 156 | negative: false, |
| 157 | has_integer_suffix: false |
| 158 | }); |
| 159 | } |
| 160 | } |