From 52a23fcadde8e4b9bd33b84bd4ce29cf9c1a9cc0 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 16 Feb 2018 23:17:17 +0300 Subject: [PATCH] Support '.' floating literals in Double.parseDouble See #327 --- .../org/teavm/classlib/java/lang/TDouble.java | 35 ++++++++++-------- .../org/teavm/classlib/java/lang/TFloat.java | 37 +++++++++++-------- .../teavm/classlib/java/lang/DoubleTest.java | 1 + .../teavm/classlib/java/lang/FloatTest.java | 30 ++++++++------- 4 files changed, 57 insertions(+), 46 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java index e2cbe4d1c..f046473b2 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java @@ -83,27 +83,30 @@ public class TDouble extends TNumber implements TComparable { ++index; } char c = string.charAt(index); - if (c < '0' || c > '9') { - throw new TNumberFormatException(); - } + long mantissa = 0; int exp = 0; - while (string.charAt(index) == '0') { - if (++index == string.length()) { - return 0; - } - } - while (index < string.length()) { - c = string.charAt(index); + if (c != '.') { if (c < '0' || c > '9') { - break; + throw new TNumberFormatException(); } - if (mantissa < 1E17) { - mantissa = mantissa * 10 + (c - '0'); - } else { - ++exp; + while (string.charAt(index) == '0') { + if (++index == string.length()) { + return 0; + } + } + while (index < string.length()) { + c = string.charAt(index); + if (c < '0' || c > '9') { + break; + } + if (mantissa < 1E17) { + mantissa = mantissa * 10 + (c - '0'); + } else { + ++exp; + } + ++index; } - ++index; } if (index < string.length() && string.charAt(index) == '.') { ++index; diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TFloat.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TFloat.java index adf873b64..ee9d6329d 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TFloat.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TFloat.java @@ -117,28 +117,33 @@ public class TFloat extends TNumber implements TComparable { ++index; } char c = string.charAt(index); - if (c < '0' || c > '9') { - throw new TNumberFormatException(); - } + int mantissa = 0; int exp = 0; - while (string.charAt(index) == '0') { - if (++index == string.length()) { - return 0; - } - } - while (index < string.length()) { - c = string.charAt(index); + if (c != '.') { if (c < '0' || c > '9') { - break; + throw new TNumberFormatException(); } - if (mantissa < 1E8) { - mantissa = mantissa * 10 + (c - '0'); - } else { - ++exp; + + while (string.charAt(index) == '0') { + if (++index == string.length()) { + return 0; + } + } + while (index < string.length()) { + c = string.charAt(index); + if (c < '0' || c > '9') { + break; + } + if (mantissa < 1E8) { + mantissa = mantissa * 10 + (c - '0'); + } else { + ++exp; + } + ++index; } - ++index; } + if (index < string.length() && string.charAt(index) == '.') { ++index; boolean hasOneDigit = false; diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/DoubleTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/DoubleTest.java index 10f4687e7..75d4aab04 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/DoubleTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/DoubleTest.java @@ -34,6 +34,7 @@ public class DoubleTest { assertEquals(23, Double.parseDouble("00230000e-4"), 1E-12); assertEquals(23, Double.parseDouble("2300000000000000000000e-20"), 1E-12); assertEquals(23, Double.parseDouble("2300000000000000000000e-20"), 1E-12); + assertEquals(0.1, Double.parseDouble(".1"), 0.001); } @Test diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/FloatTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/FloatTest.java index cc18bacd5..852da9cf4 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/FloatTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/FloatTest.java @@ -25,28 +25,30 @@ import org.teavm.junit.TeaVMTestRunner; public class FloatTest { @Test public void parsed() { - assertEquals(23, Double.parseDouble("23"), 1E-12); - assertEquals(23, Double.parseDouble("23.0"), 1E-12); - assertEquals(23, Double.parseDouble("23E0"), 1E-12); - assertEquals(23, Double.parseDouble("2.30000E1"), 1E-12); - assertEquals(23, Double.parseDouble("0.23E2"), 1E-12); - assertEquals(23, Double.parseDouble("0.000023E6"), 1E-12); - assertEquals(23, Double.parseDouble("00230000e-4"), 1E-12); - assertEquals(23, Double.parseDouble("2300000000000000000000e-20"), 1E-12); - assertEquals(23, Double.parseDouble("2300000000000000000000e-20"), 1E-12); + assertEquals(23, Float.parseFloat("23"), 1E-12F); + assertEquals(23, Float.parseFloat("23.0"), 1E-12F); + assertEquals(23, Float.parseFloat("23E0"), 1E-12F); + assertEquals(23, Float.parseFloat("2.30000E1"), 1E-12F); + assertEquals(23, Float.parseFloat("0.23E2"), 1E-12F); + assertEquals(23, Float.parseFloat("0.000023E6"), 1E-12F); + assertEquals(23, Float.parseFloat("00230000e-4"), 1E-12F); + assertEquals(23, Float.parseFloat("2300000000000000000000e-20"), 1E-12F); + assertEquals(23, Float.parseFloat("2300000000000000000000e-20"), 1E-12F); + assertEquals(23, Float.parseFloat("2300000000000000000000e-20"), 1E-12F); + assertEquals(0.1F, Float.parseFloat("0.1"), 0.001F); } @Test public void negativeParsed() { - assertEquals(-23, Double.parseDouble("-23"), 1E-12); + assertEquals(-23, Float.parseFloat("-23"), 1E-12F); } @Test public void zeroParsed() { - assertEquals(0, Double.parseDouble("0.0"), 1E-12); - assertEquals(0, Double.parseDouble("23E-8000"), 1E-12); - assertEquals(0, Double.parseDouble("00000"), 1E-12); - assertEquals(0, Double.parseDouble("00000.0000"), 1E-12); + assertEquals(0, Float.parseFloat("0.0"), 1E-12F); + assertEquals(0, Float.parseFloat("23E-8000"), 1E-12F); + assertEquals(0, Float.parseFloat("00000"), 1E-12F); + assertEquals(0, Float.parseFloat("00000.0000"), 1E-12F); } @Test