diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/BasePackage.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/BasePackage.java index 7231a8e6d83326cddde9bc8cef7594df5ed6851c..3e4ab57a47327e420f7fdf56bbcfad403d61085c 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/BasePackage.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/BasePackage.java @@ -666,6 +666,7 @@ public class BasePackage extends RBuiltinPackage { add(UpdateClass.class, UpdateClassNodeGen::create); add(UpdateDim.class, UpdateDimNodeGen::create); add(UpdateDimNames.class, UpdateDimNamesNodeGen::create); + add(Utf8ToInt.class, Utf8ToIntNodeGen::create); add(EnvFunctions.UpdateEnvironment.class, EnvFunctionsFactory.UpdateEnvironmentNodeGen::create); add(UpdateLength.class, UpdateLengthNodeGen::create); add(UpdateLevels.class, UpdateLevelsNodeGen::create); diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Internal.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Internal.java index e5a98a22eb71cae46ddad53897953989485cd5c9..c77b2e404f7fe46f7c3dd1f9d1deb1d6e8d0369c 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Internal.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Internal.java @@ -125,7 +125,7 @@ public abstract class Internal extends RBuiltinNode { "pweibull", "qweibull", "dnchisq", "pnchisq", "qnchisq", "dnt", "pnt", "qnt", "dwilcox", "pwilcox", "qwilcox", "besselI", "besselK", "dnbinom_mu", "pnbinom_mu", "qnbinom_mu", "dhyper", "phyper", "qhyper", "dnbeta", "pnbeta", "qnbeta", "dnf", "pnf", "qnf", "dtukey", "ptukey", "qtukey", "rchisq", "rexp", "rgeom", "rpois", "rt", "rsignrank", "rbeta", "rbinom", "rcauchy", "rf", "rgamma", "rlnorm", "rlogis", "rnbinom", "rnbinom_mu", "rnchisq", "rnorm", "runif", "rweibull", "rwilcox", "rhyper", "sample2", "format.info", - "grepRaw", "regexec", "adist", "aregexec", "chartr", "intToBits", "rawToBits", "packBits", "utf8ToInt", "intToUtf8", "strtrim", "eapply", "machine", "save", + "grepRaw", "regexec", "adist", "aregexec", "chartr", "intToBits", "rawToBits", "packBits", "intToUtf8", "strtrim", "eapply", "machine", "save", "saveToConn", "dput", "dump", "prmatrix", "gcinfo", "gctorture", "gctorture2", "memory.profile", "recordGraphics", "sys.calls", "sys.on.exit", "rank", "builtins", "bodyCode", "rapply", "islistfactor", "inspect", "mem.limits", "merge", "capabilitiesX11", "Cstack_info", "file.show", "file.choose", "polyroot", "mkCode", "bcClose", "is.builtin.internal", "disassemble", "bcVersion", "load.from.file", "save.to.file", "growconst", "putconst", "getconst", "enableJIT", "setNumMathThreads", "setMaxNumMathThreads", "isatty", diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Utf8ToInt.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Utf8ToInt.java new file mode 100644 index 0000000000000000000000000000000000000000..85b423645f69c4f0ac9df19b344c4d7be714a410 --- /dev/null +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Utf8ToInt.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2016, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.truffle.r.nodes.builtin.base; + +import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.notEmpty; +import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.size; +import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.stringValue; +import static com.oracle.truffle.r.runtime.builtins.RBehavior.PURE; +import static com.oracle.truffle.r.runtime.builtins.RBuiltinKind.INTERNAL; + +import com.oracle.truffle.api.dsl.Specialization; +import com.oracle.truffle.r.nodes.builtin.CastBuilder; +import com.oracle.truffle.r.nodes.builtin.RBuiltinNode; +import com.oracle.truffle.r.runtime.RError; +import com.oracle.truffle.r.runtime.RRuntime; +import com.oracle.truffle.r.runtime.builtins.RBuiltin; +import com.oracle.truffle.r.runtime.data.RDataFactory; +import com.oracle.truffle.r.runtime.data.model.RAbstractIntVector; +import com.oracle.truffle.r.runtime.data.model.RAbstractStringVector; + +@RBuiltin(name = "utf8ToInt", kind = INTERNAL, parameterNames = {"x"}, behavior = PURE) +public abstract class Utf8ToInt extends RBuiltinNode { + + @Override + protected void createCasts(CastBuilder casts) { + casts.arg(0, "x").defaultError(RError.SHOW_CALLER, RError.Message.ARG_MUST_BE_CHARACTER_VECTOR_LENGTH_ONE, "x").mustBe(stringValue()).asStringVector().mustBe(notEmpty()).shouldBe(size(1), + RError.SHOW_CALLER, RError.Message.ARG_SHOULD_BE_CHARACTER_VECTOR_LENGTH_ONE).findFirst(); + } + + @Specialization + protected RAbstractIntVector utf8ToInt(String value) { + RAbstractIntVector ret; + if (!RRuntime.isNA(value)) { + int valueLen = value.length(); + int[] result = new int[valueLen]; + for (int i = 0; i < valueLen; i++) { + char c = value.charAt(i); + result[i] = c; + } + ret = RDataFactory.createIntVector(result, true); + } else { // NA_character_ + ret = RDataFactory.createIntVector(new int[]{RRuntime.INT_NA}, false); + } + return ret; + } + +} diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test index bef395ff97c3f3532232a6fe9a6497723cbd4646..4dad57349fa713356bf4d4918aa9edb3bfe5f9a8 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test @@ -54582,11 +54582,54 @@ Slot "y": [1] 77 88 -##com.oracle.truffle.r.test.builtins.TestBuiltin_utf8ToInt.testutf8ToInt1#Ignored.Unknown# +##com.oracle.truffle.r.test.builtins.TestBuiltin_utf8ToInt.testUtf8ToInt# +#utf8ToInt('') +integer(0) + +##com.oracle.truffle.r.test.builtins.TestBuiltin_utf8ToInt.testUtf8ToInt# +#utf8ToInt('Hello') +[1] 72 101 108 108 111 + +##com.oracle.truffle.r.test.builtins.TestBuiltin_utf8ToInt.testUtf8ToInt# +#utf8ToInt('a') +[1] 97 + +##com.oracle.truffle.r.test.builtins.TestBuiltin_utf8ToInt.testUtf8ToInt# +#utf8ToInt(5) +Error in utf8ToInt(5) : argument must be a character vector of length 1 + +##com.oracle.truffle.r.test.builtins.TestBuiltin_utf8ToInt.testUtf8ToInt# +#utf8ToInt(NA) +Error in utf8ToInt(NA) : argument must be a character vector of length 1 + +##com.oracle.truffle.r.test.builtins.TestBuiltin_utf8ToInt.testUtf8ToInt# +#utf8ToInt(NULL) +Error in utf8ToInt(NULL) : + argument must be a character vector of length 1 + +##com.oracle.truffle.r.test.builtins.TestBuiltin_utf8ToInt.testUtf8ToInt#Output.IgnoreWhitespace# +#utf8ToInt(c('a', 'b')) +[1] 97 +Warning message: +In utf8ToInt(c("a", "b")) : + argument should be a character vector of length 1 +all but the first element will be ignored + +##com.oracle.truffle.r.test.builtins.TestBuiltin_utf8ToInt.testUtf8ToInt# +#utf8ToInt(character(0)) +Error in utf8ToInt(character(0)) : + argument must be a character vector of length 1 + +##com.oracle.truffle.r.test.builtins.TestBuiltin_utf8ToInt.testUtf8ToInt# +#utf8ToInt(numeric(0)) +Error in utf8ToInt(numeric(0)) : + argument must be a character vector of length 1 + +##com.oracle.truffle.r.test.builtins.TestBuiltin_utf8ToInt.testutf8ToInt1# #argv <- list('lasy'); .Internal(utf8ToInt(argv[[1]])) [1] 108 97 115 121 -##com.oracle.truffle.r.test.builtins.TestBuiltin_utf8ToInt.testutf8ToInt3#Ignored.Unknown# +##com.oracle.truffle.r.test.builtins.TestBuiltin_utf8ToInt.testutf8ToInt3# #argv <- structure(list(x = NA_character_), .Names = 'x');do.call('utf8ToInt', argv) [1] NA diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_utf8ToInt.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_utf8ToInt.java index 02c42c7ce06ea4323842449050146f1cbe4ceba2..1f4d98fe1b3befb31dc6d1b78dda3382c92a1eed 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_utf8ToInt.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_utf8ToInt.java @@ -17,13 +17,27 @@ import com.oracle.truffle.r.test.TestBase; // Checkstyle: stop line length check public class TestBuiltin_utf8ToInt extends TestBase { + @Test + public void testUtf8ToInt() { + assertEval("utf8ToInt('a')"); + assertEval("utf8ToInt('Hello')"); + assertEval("utf8ToInt('')"); + assertEval("utf8ToInt(5)"); + assertEval("utf8ToInt(character(0))"); + assertEval("utf8ToInt(numeric(0))"); + assertEval("utf8ToInt(NULL)"); + assertEval("utf8ToInt(NA)"); + assertEval(Output.IgnoreWhitespace, "utf8ToInt(c('a', 'b'))"); // no extra newline in + // warning msg + } + @Test public void testutf8ToInt1() { - assertEval(Ignored.Unknown, "argv <- list('lasy'); .Internal(utf8ToInt(argv[[1]]))"); + assertEval("argv <- list('lasy'); .Internal(utf8ToInt(argv[[1]]))"); } @Test public void testutf8ToInt3() { - assertEval(Ignored.Unknown, "argv <- structure(list(x = NA_character_), .Names = 'x');do.call('utf8ToInt', argv)"); + assertEval("argv <- structure(list(x = NA_character_), .Names = 'x');do.call('utf8ToInt', argv)"); } }