Skip to content
Snippets Groups Projects
Commit 51c406b6 authored by Tomas Stupka's avatar Tomas Stupka
Browse files

string2DoubleNoCheck should handle non integer hex literals

parent 249d6ea6
No related branches found
No related tags found
No related merge requests found
......@@ -39,6 +39,7 @@ import com.oracle.truffle.r.runtime.data.model.RAbstractLogicalVector;
import com.oracle.truffle.r.runtime.data.model.RAbstractStringVector;
import com.oracle.truffle.r.runtime.data.model.RAbstractVector;
import com.oracle.truffle.r.runtime.env.frame.FrameSlotChangeMonitor;
import java.math.BigInteger;
public class RRuntime {
......@@ -387,10 +388,15 @@ public class RRuntime {
try {
return Double.parseDouble(v);
} catch (NumberFormatException e) {
if (v.startsWith("0x")) {
try {
return int2double(Integer.decode(v));
} catch (NumberFormatException ein) {
if (hasHexPrefix(v)) {
switch (v.charAt(0)) {
case '-':
return -1 * new BigInteger(v.substring(3, v.length()), 16).doubleValue();
case '+':
return new BigInteger(v.substring(3, v.length()), 16).doubleValue();
default:
assert v.charAt(0) == '0';
return new BigInteger(v.substring(2, v.length()), 16).doubleValue();
}
}
if (exceptionOnFail) {
......@@ -400,6 +406,10 @@ public class RRuntime {
return DOUBLE_NA;
}
public static boolean hasHexPrefix(String s) {
return s.startsWith("0x") || s.startsWith("-0x") || s.startsWith("+0x");
}
@TruffleBoundary
public static double string2doubleNoCheck(String v) {
return string2doubleNoCheck(v, false);
......
/*
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -61,6 +61,27 @@ public class TestParser extends TestBase {
assertEval("0xa.bp1i");
}
private static final String[] HEX_VALUES = new String[]{"0xFFF", "0xFFFFFFFFFFF"};
@Test
public void testHexLiterals() {
for (String sign : new String[]{"", "-", "+"}) {
for (String suffix : new String[]{"", "L"}) {
String l = sign + "%0" + suffix;
assertEval(template(l, HEX_VALUES));
assertEval(template("typeof(" + l + ")", HEX_VALUES));
}
}
}
@Test
public void testNonIntegerQualifiedWithLWarning() {
assertEval(Ignored.OutputFormatting, "12345678909876543212L; 12345678909876543212L; 12345678909876543212L");
assertEval(Ignored.OutputFormatting, "123456789098765432121L\n123456789098765432121L");
assertEval(Ignored.OutputFormatting, "0xFFFFFFFFFFFL; 0xFFFFFFFFFFFL; 0xFFFFFFFFFFFL");
assertEval(Ignored.OutputFormatting, "0xFFFFFFFFFFFL\n0xFFFFFFFFFFFL");
}
@Test
public void testSpaceEscapeSequence() {
assertEval("\"\\ \" == \" \"");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment