File tree Expand file tree Collapse file tree 5 files changed +35
-9
lines changed Expand file tree Collapse file tree 5 files changed +35
-9
lines changed Original file line number Diff line number Diff line change @@ -511,12 +511,12 @@ pub fn (s string) i16() i16 {
511
511
512
512
// f32 returns the value of the string as f32 `'1.0'.f32() == f32(1)`.
513
513
pub fn (s string) f32 () f32 {
514
- return f32 (strconv.atof64 (s))
514
+ return f32 (strconv.atof64 (s) or { 0 } )
515
515
}
516
516
517
517
// f64 returns the value of the string as f64 `'1.0'.f64() == f64(1)`.
518
518
pub fn (s string) f64 () f64 {
519
- return strconv.atof64 (s)
519
+ return strconv.atof64 (s) or { 0 }
520
520
}
521
521
522
522
// u8 returns the value of the string as u8 `'1'.u8() == u8(1)`.
Original file line number Diff line number Diff line change @@ -104,6 +104,7 @@ pub const (
104
104
parser_mzero = 2 // number is negative, module smaller
105
105
parser_pinf = 3 // number is higher than +HUGE_VAL
106
106
parser_minf = 4 // number is lower than -HUGE_VAL
107
+ parser_invalid_number = 5 // invalid number, used for '#@%^' for example
107
108
//
108
109
// char constants
109
110
// Note: Modify these if working with non-ASCII encoding
@@ -232,6 +233,9 @@ fn parser(s string) (int, PrepNumber) {
232
233
result = strconv.parser_pzero
233
234
}
234
235
}
236
+ if i == 0 && s.len > 0 {
237
+ return strconv.parser_invalid_number, pn
238
+ }
235
239
return result, pn
236
240
}
237
241
@@ -403,9 +407,9 @@ fn converter(mut pn PrepNumber) u64 {
403
407
// Public functions
404
408
405
409
// atof64 return a f64 from a string doing a parsing operation
406
- pub fn atof64 (s string ) f64 {
410
+ pub fn atof64 (s string ) ? f64 {
407
411
if s.len == 0 {
408
- return 0
412
+ return error ( 'expected a number found an empty string' )
409
413
}
410
414
mut pn := PrepNumber{}
411
415
mut res_parsing := 0
@@ -428,7 +432,9 @@ pub fn atof64(s string) f64 {
428
432
strconv.parser_minf {
429
433
res.u = strconv.double_minus_infinity
430
434
}
431
- else {}
435
+ else {
436
+ return error ('not a number' )
437
+ }
432
438
}
433
439
return unsafe { res.f }
434
440
}
Original file line number Diff line number Diff line change 1
1
module strconv
2
2
3
3
// atof64 return a f64 from a string doing a parsing operation
4
- pub fn atof64 (s string ) f64 {
4
+ pub fn atof64 (s string ) ? f64 {
5
+ // TODO: handle parsing invalid numbers as close as possible to the pure V version
6
+ // that may be slower, but more portable, and will guarantee that higher level code
7
+ // works the same in the JS version, as well as in the C and Native versions.
5
8
res := 0.0
6
9
#res.val = Number(s.str)
7
10
Original file line number Diff line number Diff line change @@ -36,7 +36,8 @@ fn test_atof() {
36
36
// check conversion case 1 string <=> string
37
37
for c, x in src_num {
38
38
// slow atof
39
- assert strconv.atof64 (src_num_str[c]).strlong () == x.strlong ()
39
+ val := strconv.atof64 (src_num_str[c]) or { panic (err) }
40
+ assert val.strlong () == x.strlong ()
40
41
41
42
// quick atof
42
43
mut s1 := (strconv.atof_quick (src_num_str[c]).str ())
@@ -56,7 +57,8 @@ fn test_atof() {
56
57
// we don't test atof_quick beacuse we already know the rounding error
57
58
for c, x in src_num_str {
58
59
b := src_num[c].strlong ()
59
- a1 := strconv.atof64 (x).strlong ()
60
+ value := strconv.atof64 (x) or { panic (err) }
61
+ a1 := value.strlong ()
60
62
assert a1 == b
61
63
}
62
64
@@ -73,3 +75,18 @@ fn test_atof() {
73
75
assert * ptr == u64 (0x8000000000000000 )
74
76
println ('DONE!' )
75
77
}
78
+
79
+ fn test_atof_errors () {
80
+ if x := strconv.atof64 ('' ) {
81
+ eprintln ('> x: $x ' )
82
+ assert false // strconv.atof64 should have failed
83
+ } else {
84
+ assert err.str () == 'expected a number found an empty string'
85
+ }
86
+ if x := strconv.atof64 ('####' ) {
87
+ eprintln ('> x: $x ' )
88
+ assert false // strconv.atof64 should have failed
89
+ } else {
90
+ assert err.str () == 'not a number'
91
+ }
92
+ }
Original file line number Diff line number Diff line change @@ -191,7 +191,7 @@ pub fn (mut e Eval) expr(expr ast.Expr, expecting ast.Type) Object {
191
191
}) // TODO: numbers larger than 2^63 (for u64)
192
192
}
193
193
ast.FloatLiteral {
194
- return f64 (strconv.atof64 (expr.val))
194
+ return f64 (strconv.atof64 (expr.val) or { e. error (err. str ()) } )
195
195
}
196
196
ast.BoolLiteral {
197
197
return expr.val
You can’t perform that action at this time.
0 commit comments