diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/utils/TypeConvert.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/utils/TypeConvert.java index 49204b765ddbc5dee540d31c69888ec6a8c857f2..81362811de3c5143b33e6276efeece26e09e6920 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/utils/TypeConvert.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/utils/TypeConvert.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 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 @@ -35,6 +35,7 @@ import com.oracle.truffle.r.runtime.data.RIntVector; import com.oracle.truffle.r.runtime.data.RLogicalVector; import com.oracle.truffle.r.runtime.data.RVector; import com.oracle.truffle.r.runtime.data.model.RAbstractStringVector; +import com.oracle.truffle.r.runtime.data.model.RAbstractVector; public abstract class TypeConvert extends RExternalBuiltinNode.Arg5 { @@ -123,31 +124,41 @@ public abstract class TypeConvert extends RExternalBuiltinNode.Arg5 { } String s = x.getDataAt(i); - try { - int intVal = RRuntime.string2intNoCheck(s, true); - return readIntVector(x, i, intVal, naStrings); - } catch (NumberFormatException ix) { + if (RRuntime.hasHexPrefix(s)) { + // this is a mess + // double takes precedense even if s is a hexadecimal integer try { double doubleVal = RRuntime.string2doubleNoCheck(s, true); + return readDoubleVector(x, i, doubleVal, naStrings); + } catch (NumberFormatException ix) { + // fall through + } + } else { + try { + int intVal = RRuntime.string2intNoCheck(s, true); + return readIntVector(x, i, intVal, naStrings); + } catch (NumberFormatException ix) { try { - return readDoubleVector(x, i, doubleVal, naStrings); - } catch (NumberFormatException lx) { - // fall through - } - } catch (NumberFormatException dx) { - try { - byte logicalVal = RRuntime.string2logicalNoCheck(s, true); + double doubleVal = RRuntime.string2doubleNoCheck(s, true); try { - return readLogicalVector(x, i, logicalVal, naStrings); + return readDoubleVector(x, i, doubleVal, naStrings); + } catch (NumberFormatException lx) { + // fall through + } + } catch (NumberFormatException dx) { + try { + byte logicalVal = RRuntime.string2logicalNoCheck(s, true); + try { + return readLogicalVector(x, i, logicalVal, naStrings); + } catch (NumberFormatException lx) { + // fall through + } } catch (NumberFormatException lx) { // fall through } - } catch (NumberFormatException lx) { - // fall through } } } - // fall through target - conversion to int, double or logical failed if (asIs == RRuntime.LOGICAL_TRUE) { diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/utils/TestTypeConvert.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/utils/TestTypeConvert.java index bc17a53675c0835d35dd2890716885b057e9656a..972ac0da24429ada9a1df550e8466f806b13cf2b 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/utils/TestTypeConvert.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/utils/TestTypeConvert.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 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 @@ -39,4 +39,23 @@ public class TestTypeConvert extends TestBase { // looks like integer, but is double (because it would be INT_NA) assertEval("type.convert('-2147483648')"); } + + private static final String[] LIT_VALUES = new String[]{"0xFFF", "0xFFFFFFFFFFF", "123", "2147483648"}; + + @Test + public void testConvertLiterals() { + for (String suf : new String[]{"", "L"}) { + for (String sign : new String[]{"", "-", "+"}) { + String l = sign + "%0" + suf; + assertEval(template("type.convert('" + l + "')", LIT_VALUES)); + assertEval(template("typeof(type.convert('" + l + "'))", LIT_VALUES)); + } + } + } + + @Test + public void testFirstTypeMustBeOfModeTest() { + // UnsupportedSpecializationException: Unexpected values provided for ... + assertEval(Ignored.Unimplemented, "type.convert('NA', 1)"); + } }