From bbfa25a17b86ac8e20c667c9165548216ceb524e Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+tobealive@users.noreply.github.com> Date: Thu, 27 Apr 2023 05:43:00 +0200 Subject: [PATCH] toml: fix scanner floating point detection (#18062) --- vlib/toml/scanner/scanner.v | 14 ++++++++++++++ vlib/toml/tests/reflect_test.v | 25 +++++++++++++++++-------- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/vlib/toml/scanner/scanner.v b/vlib/toml/scanner/scanner.v index 8abe0872fe..ad8a051e69 100644 --- a/vlib/toml/scanner/scanner.v +++ b/vlib/toml/scanner/scanner.v @@ -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 diff --git a/vlib/toml/tests/reflect_test.v b/vlib/toml/tests/reflect_test.v index 4ae07e8101..c204f6c866 100644 --- a/vlib/toml/tests/reflect_test.v +++ b/vlib/toml/tests/reflect_test.v @@ -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