1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

toml: fix scanner floating point detection (#18062)

This commit is contained in:
Turiiya 2023-04-27 05:43:00 +02:00 committed by GitHub
parent 8f767c9189
commit bbfa25a17b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 8 deletions

View File

@ -591,6 +591,20 @@ fn (mut s Scanner) extract_number() !string {
s.col++
for s.pos < s.text.len {
c = s.at()
// Adjust scanner position to floating point numbers
mut i, mut float_precision := 1, 0
for c_ := u8(s.peek(i)); c_ != scanner.end_of_text && c_ != `\n`; c_ = u8(s.peek(i)) {
if !c_.is_digit() && c_ != `.` && c_ != `,` {
float_precision = 0
break
}
if c_.is_digit() && c == `.` {
float_precision++
}
i++
}
s.pos += float_precision
s.col += float_precision
// Handle signed exponent notation. I.e.: 3e2, 3E2, 3e-2, 3E+2, 3e0, 3.1e2, 3.1E2, -1E-1
if c in [`e`, `E`] && s.peek(1) in [`+`, `-`] && u8(s.peek(2)).is_digit() {
s.pos += 2

View File

@ -18,6 +18,13 @@ bools = [true, false, true, true]
floats = [0.0, 1.0, 2.0, 3.0]
floats_br = [
0.0,
1.05,
2.025,
3.5001
]
int_map = {"a" = 0, "b" = 1, "c" = 2, "d" = 3}
[bio]
@ -44,14 +51,15 @@ struct Bio {
}
struct User {
name string
age int
height f64
birthday toml.Date
strings []string
bools []bool
floats []f32
int_map map[string]int
name string
age int
height f64
birthday toml.Date
strings []string
bools []bool
floats []f32
floats_br []f32
int_map map[string]int
config toml.Any
mut:
@ -73,6 +81,7 @@ fn test_reflect() {
assert user.strings == ['v matures', 'like rings', 'spread in the', 'water']
assert user.bools == [true, false, true, true]
assert user.floats == [f32(0.0), 1.0, 2.0, 3.0]
assert user.floats_br == [f32(0.0), 1.05, 2.025, 3.5001]
assert user.int_map == {
'a': 0
'b': 1