@@ -76,6 +76,33 @@ impl Numeric {
76
76
pub fn sign ( & self ) -> NumericSign {
77
77
self . sign
78
78
}
79
+
80
+ fn nan ( ) -> Self {
81
+ Self {
82
+ sign : NumericSign :: NaN ,
83
+ scale : 0 ,
84
+ weight : 0 ,
85
+ digits : vec ! [ ] ,
86
+ }
87
+ }
88
+
89
+ fn infinity ( ) -> Self {
90
+ Self {
91
+ sign : NumericSign :: PositiveInfinity ,
92
+ scale : 0 ,
93
+ weight : 0 ,
94
+ digits : vec ! [ ] ,
95
+ }
96
+ }
97
+
98
+ fn negative_infinity ( ) -> Self {
99
+ Self {
100
+ sign : NumericSign :: NegativeInfinity ,
101
+ scale : 0 ,
102
+ weight : 0 ,
103
+ digits : vec ! [ ] ,
104
+ }
105
+ }
79
106
}
80
107
81
108
impl std:: fmt:: Display for Numeric {
@@ -161,33 +188,15 @@ impl FromStr for Numeric {
161
188
let mut scale = 0 ;
162
189
let mut sign = NumericSign :: Positive ;
163
190
164
- match value {
165
- "NaN" | "nan" => {
166
- return Ok ( Numeric {
167
- sign : NumericSign :: NaN ,
168
- scale : 0 ,
169
- weight : 0 ,
170
- digits : vec ! [ ] ,
171
- } )
172
- }
173
- "Infinity" | "infinity" | "inf" => {
174
- return Ok ( Numeric {
175
- sign : NumericSign :: PositiveInfinity ,
176
- scale : 0 ,
177
- weight : 0 ,
178
- digits : vec ! [ ] ,
179
- } )
180
- }
181
- "-Infinity" | "-infinity" | "-inf" => {
182
- return Ok ( Numeric {
183
- sign : NumericSign :: NegativeInfinity ,
184
- scale : 0 ,
185
- weight : 0 ,
186
- digits : vec ! [ ] ,
187
- } )
188
- }
189
- _ => { }
190
- } ;
191
+ if value. eq_ignore_ascii_case ( "NaN" ) {
192
+ return Ok ( Numeric :: nan ( ) ) ;
193
+ }
194
+ if value. eq_ignore_ascii_case ( "Infinity" ) || value. eq_ignore_ascii_case ( "Inf" ) {
195
+ return Ok ( Numeric :: infinity ( ) ) ;
196
+ }
197
+ if value. eq_ignore_ascii_case ( "-Infinity" ) || value. eq_ignore_ascii_case ( "-Inf" ) {
198
+ return Ok ( Numeric :: negative_infinity ( ) ) ;
199
+ }
191
200
192
201
let mut s = value. as_bytes ( ) ;
193
202
if let Some ( & b'-' ) = s. first ( ) {
@@ -206,7 +215,7 @@ impl FromStr for Numeric {
206
215
207
216
if let Some ( mut e) = e {
208
217
if e. is_empty ( ) {
209
- return Err ( "empty sientific notation string" . into ( ) ) ;
218
+ return Err ( "empty scientific notation string" . into ( ) ) ;
210
219
}
211
220
212
221
let mut positive = true ;
@@ -221,7 +230,7 @@ impl FromStr for Numeric {
221
230
222
231
for & b in e {
223
232
if !b. is_ascii_digit ( ) {
224
- return Err ( "sientific notation string contain non-digit character" . into ( ) ) ;
233
+ return Err ( "scientific notation string contain non-digit character" . into ( ) ) ;
225
234
}
226
235
exp = exp * 10 + ( b - b'0' ) as u16 ;
227
236
}
@@ -591,7 +600,7 @@ mod test {
591
600
}
592
601
593
602
#[ test]
594
- fn test_from_sientific_notation ( ) {
603
+ fn test_from_scientific_notation ( ) {
595
604
let cases = & [
596
605
(
597
606
"2e4" ,
0 commit comments