Debian Codegraph @

src/parsing/number_parser.rs

raw symbols SCIP
1 use thiserrorrust-analyzer cargo thiserror 2.0.12 crate/::Errorrust-analyzer cargo thiserror-impl 2.0.12 Error:;
2
3 const FLOAT_IDENTIFIERSrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/FLOAT_IDENTIFIERS.: [&str; 2] = ["f32", "f64"];
4 const INTEGER_IDENTIFIERSrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/INTEGER_IDENTIFIERS.: [&str; 10] =
5 ["u8", "u16", "u32", "u64", "u128", "i8", "i16", "i32", "i64", "i128"];
6 const BOOLEAN_IDENTIFIERSrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/BOOLEAN_IDENTIFIERS.: [&str; 2] = ["true", "false"];
7 const NEGATIVE_SIGNrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/NEGATIVE_SIGN.: &str = "-";
8
9 const HEX_PREFIXrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/HEX_PREFIX.: &str = "0x";
10 const HEX_RADIXrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/HEX_RADIX.: u32 = 16;
11
12 const BINARY_PREFIXrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/BINARY_PREFIX.: &str = "0b";
13 const BINARY_RADIXrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/BINARY_RADIX.: u32 = 2;
14
15 const OCTAL_PREFIXrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/OCTAL_PREFIX.: &str = "0o";
16 const OCTAL_RADIXrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/OCTAL_RADIX.: u32 = 8;
17
18 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Errorrust-analyzer cargo thiserror-impl 2.0.12 Error:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
19 pub(craterust-analyzer cargo bitfields-impl 1.0.0 crate/) enum NumberParseErrorrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/NumberParseError# {
20 #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("floats are not supported")]
21 FloatNotSupportedrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/NumberParseError#FloatNotSupported#,
22 #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("invalid integer or boolean")]
23 InvalidNumberStringrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/NumberParseError#InvalidNumberString#,
24 }
25
26 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
27 pub(craterust-analyzer cargo bitfields-impl 1.0.0 crate/) struct ParsedNumberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber# {
28 pub(craterust-analyzer cargo bitfields-impl 1.0.0 crate/) numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number.: u128,
29 pub(craterust-analyzer cargo bitfields-impl 1.0.0 crate/) negativerust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#negative.: bool,
30 pub(craterust-analyzer cargo bitfields-impl 1.0.0 crate/) has_integer_suffixrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#has_integer_suffix.: bool,
31 }
32
33 /// Parses a number string.
34 pub(craterust-analyzer cargo bitfields-impl 1.0.0 crate/) fn parse_number_stringrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/parse_number_string().(s: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<ParsedNumberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#, NumberParseErrorrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/NumberParseError#> {
35 let trimmed_str = s.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().().replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().(" ", "").replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().("_", "").to_ascii_lowercaserust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]to_ascii_lowercase().();
36
37 // Check if the string is a float.
38 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().trimmed_str.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().(HEX_PREFIXrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/HEX_PREFIX.) {
39 let contains_float_identifier =
40 FLOAT_IDENTIFIERSrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/FLOAT_IDENTIFIERS..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]any().(|&identifier| trimmed_str.ends_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]ends_with().(identifier));
41 if trimmed_str.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().(".") || contains_float_identifier {
42 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(NumberParseErrorrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/NumberParseError#::FloatNotSupportedrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/NumberParseError#FloatNotSupported#);
43 }
44 }
45
46 // Check if the string is a boolean.
47 if BOOLEAN_IDENTIFIERSrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/BOOLEAN_IDENTIFIERS..containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&trimmed_str.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()) {
48 return match trimmed_str.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().() {
49 "true" => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ParsedNumberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber# { numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number.: 1, negativerust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#negative.: false, has_integer_suffixrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#has_integer_suffix.: false }),
50 "false" => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ParsedNumberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber# { numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number.: 0, negativerust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#negative.: false, has_integer_suffixrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#has_integer_suffix.: false }),
51 _ => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(NumberParseErrorrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/NumberParseError#::InvalidNumberStringrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/NumberParseError#InvalidNumberString#),
52 };
53 }
54
55 let has_integer_suffix =
56 INTEGER_IDENTIFIERSrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/INTEGER_IDENTIFIERS..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]any().(|&identifier| trimmed_str.ends_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]ends_with().(identifier));
57
58 // Remove integer identifiers.
59 let trimmed_str = INTEGER_IDENTIFIERSrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/INTEGER_IDENTIFIERS.
60 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
61 .foldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]fold().(trimmed_str, |acc, &identifier| acc.replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().(identifier, ""));
62
63 let negative_number = trimmed_str.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().(NEGATIVE_SIGNrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/NEGATIVE_SIGN.);
64 let trimmed_str = trimmed_str.trim_start_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_start_matches().(NEGATIVE_SIGNrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/NEGATIVE_SIGN.);
65
66 // Check if the string is a hexadecimal number.
67 if trimmed_str.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().(HEX_PREFIXrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/HEX_PREFIX.) {
68 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ParsedNumberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber# {
69 numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number.: u128::from_str_radixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u128]from_str_radix().(trimmed_str.trim_start_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_start_matches().(HEX_PREFIXrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/HEX_PREFIX.), HEX_RADIXrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/HEX_RADIX.)
70 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(),
71 negativerust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#negative.: negative_number,
72 has_integer_suffixrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#has_integer_suffix.,
73 })
74 } else if trimmed_str.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().(BINARY_PREFIXrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/BINARY_PREFIX.) {
75 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ParsedNumberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber# {
76 numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number.: u128::from_str_radixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u128]from_str_radix().(
77 trimmed_str.trim_start_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_start_matches().(BINARY_PREFIXrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/BINARY_PREFIX.),
78 BINARY_RADIXrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/BINARY_RADIX.,
79 )
80 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(),
81 negativerust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#negative.: negative_number,
82 has_integer_suffixrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#has_integer_suffix.,
83 })
84 } else if trimmed_str.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().(OCTAL_PREFIXrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/OCTAL_PREFIX.) {
85 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ParsedNumberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber# {
86 numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number.: u128::from_str_radixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u128]from_str_radix().(trimmed_str.trim_start_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_start_matches().(OCTAL_PREFIXrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/OCTAL_PREFIX.), OCTAL_RADIXrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/OCTAL_RADIX.)
87 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(),
88 negativerust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#negative.: negative_number,
89 has_integer_suffixrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#has_integer_suffix.,
90 })
91 } else {
92 // Regular number
93 match trimmed_str.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<u128>() {
94 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(number) => {
95 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ParsedNumberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber# { numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number., negativerust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#negative.: negative_number, has_integer_suffixrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#has_integer_suffix. })
96 }
97 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(NumberParseErrorrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/NumberParseError#::InvalidNumberStringrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/NumberParseError#InvalidNumberString#),
98 }
99 }
100 }
101
102 #[cfg(test)]
103 mod testsrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/tests/ {
104 use superrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/::*;
105
106 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
107 fn test_parse_decimal_stringrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/tests/test_parse_decimal_string().() {
108 let integer_type = parse_number_stringrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/parse_number_string().("12").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
109
110 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(integer_type, ParsedNumberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber# {
111 numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number.: 12,
112 negativerust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#negative.: false,
113 has_integer_suffixrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#has_integer_suffix.: false
114 });
115 }
116
117 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
118 fn test_parse_negative_decimal_stringrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/tests/test_parse_negative_decimal_string().() {
119 let integer_type = parse_number_stringrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/parse_number_string().("-12").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
120
121 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(integer_type, ParsedNumberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber# {
122 numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number.: 12,
123 negativerust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#negative.: true,
124 has_integer_suffixrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#has_integer_suffix.: false
125 });
126 }
127
128 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
129 fn test_parse_decimal_integer_suffix_stringrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/tests/test_parse_decimal_integer_suffix_string().() {
130 let integer_type = parse_number_stringrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/parse_number_string().("-12i8").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
131
132 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(integer_type, ParsedNumberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber# {
133 numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number.: 12,
134 negativerust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#negative.: true,
135 has_integer_suffixrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#has_integer_suffix.: true
136 });
137 }
138
139 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
140 fn test_parse_hex_stringrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/tests/test_parse_hex_string().() {
141 let integer_type = parse_number_stringrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/parse_number_string().("0x1234").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
142
143 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(integer_type, ParsedNumberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber# {
144 numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number.: 0x1234,
145 negativerust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#negative.: false,
146 has_integer_suffixrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#has_integer_suffix.: false
147 });
148 }
149
150 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
151 fn test_parse_octal_stringrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/tests/test_parse_octal_string().() {
152 let integer_type = parse_number_stringrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/parse_number_string().("0o12").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
153
154 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(integer_type, ParsedNumberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber# {
155 numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number.: 10,
156 negativerust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#negative.: false,
157 has_integer_suffixrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#has_integer_suffix.: false
158 });
159 }
160 }