fixes some issues with underscores in float literals. add more tests.

fixes some potential issues with underscores in float literals.
adds more checks for badly positionned underscores in float literals.
adds more test files.
This commit is contained in:
Parashurama
2016-05-19 21:03:22 +02:00
parent d3e9589b3d
commit 5d7d8816e5
3 changed files with 54 additions and 5 deletions

View File

@@ -328,7 +328,7 @@ proc nimParseBiggestFloat(s: string, number: var BiggestFloat,
fraction: uint64
frac_exponent= 0
exp_sign = 1
first_digit = 0
first_digit = -1
has_sign = false
# Sign?
@@ -359,6 +359,7 @@ proc nimParseBiggestFloat(s: string, number: var BiggestFloat,
# Skip leading zero
while s[i] == '0':
inc(i)
while s[i] == '_': inc(i)
if s[i] in {'0'..'9'}:
first_digit = (s[i].ord - '0'.ord)
@@ -366,7 +367,7 @@ proc nimParseBiggestFloat(s: string, number: var BiggestFloat,
while s[i] in {'0'..'9'}:
inc(kdigits)
integer = integer * 10'u64 + (s[i].ord - '0'.ord).uint64
inc(i);
inc(i)
while s[i] == '_': inc(i)
# Fractional part?
@@ -374,11 +375,12 @@ proc nimParseBiggestFloat(s: string, number: var BiggestFloat,
inc(i)
# if no integer part, Skip leading zeros
if kdigits <= 0:
while s[i] in {'_','0'}:
inc(i)
while s[i] == '0':
inc(frac_exponent)
inc(i)
while s[i] == '_': inc(i)
if s[i] in {'0'..'9'}:
if first_digit == -1 and s[i] in {'0'..'9'}:
first_digit = (s[i].ord - '0'.ord)
# get fractional part
while s[i] in {'0'..'9'}:

21
tests/float/tfloat6.nim Normal file
View File

@@ -0,0 +1,21 @@
discard """
file: "tfloat6.nim"
output: '''1e-06 : 1e-06
1e-06 : 1e-06
0.001 : 0.001
1e-06 : 1e-06
1e-06 : 1e-06
10.000001 : 10.000001
100.000001 : 100.000001'''
"""
import strutils
echo "0.00_0001".parseFloat(), " : ", 1E-6
echo "0.00__00_01".parseFloat(), " : ", 1E-6
echo "0.0_01".parseFloat(), " : ", 0.001
echo "0.00_000_1".parseFloat(), " : ", 1E-6
echo "0.00000_1".parseFloat(), " : ", 1E-6
echo "1_0.00_0001".parseFloat(), " : ", 10.000001
echo "1__00.00_0001".parseFloat(), " : ", 1_00.000001

26
tests/float/tfloat7.nim Normal file
View File

@@ -0,0 +1,26 @@
discard """
file: "tfloat6.nim"
output: '''passed.
passed.
passed.
passed.
passed.
passed.
passed.'''
"""
import strutils
template expect_fail(x: expr) =
try:
discard x
echo("expected to fail!")
except ValueError:
echo("passed.")
expect_fail("1_0._00_0001".parseFloat())
expect_fail("_1_0_00.0001".parseFloat())
expect_fail("10.00.01".parseFloat())
expect_fail("10.00E_01".parseFloat())
expect_fail("10.00E_01".parseFloat())
expect_fail("10.00E".parseFloat())
expect_fail("10.00A".parseFloat())