Skip to content
Snippets Groups Projects
Commit 74b04dce authored by Mick Jordan's avatar Mick Jordan
Browse files

implement rawToChar

parent caa1ded7
Branches
No related tags found
No related merge requests found
......@@ -55,6 +55,42 @@ public class RawFunctions {
}
@RBuiltin(name = "rawToChar", kind = RBuiltinKind.INTERNAL, parameterNames = {"x", "multiple"})
public abstract static class RawToChar extends RBuiltinNode {
@Specialization
protected RStringVector rawToChar(RAbstractRawVector x, byte multiple) {
if (RRuntime.isNA(multiple)) {
throw RError.error(getEncapsulatingSourceSection(), RError.Message.INVALID_LOGICAL, "multiple");
}
RStringVector result;
if (RRuntime.fromLogical(multiple)) {
String[] data = new String[x.getLength()];
for (int i = 0; i < data.length; i++) {
data[i] = new String(new byte[]{x.getDataAt(i).getValue()});
}
result = RDataFactory.createStringVector(data, RDataFactory.COMPLETE_VECTOR);
} else {
int j = 0;
byte[] data = new byte[x.getLength()];
for (int i = 0; i < data.length; i++) {
byte b = x.getDataAt(i).getValue();
if (b != 0) {
data[j++] = b;
}
}
result = RDataFactory.createStringVectorFromScalar(new String(data, 0, j));
}
return result;
}
@SuppressWarnings("unused")
@Fallback
protected Object rawToChar(Object x, Object multiple) {
throw RError.error(getEncapsulatingSourceSection(), RError.Message.ARGUMENT_MUST_BE_RAW_VECTOR, "x");
}
}
// TODO the rest of the functions
}
......@@ -375,6 +375,7 @@ public final class RError extends RuntimeException {
VALUE_OUT_OF_RANGE("value out of range in '%s'"),
MUST_BE_STRING("'%s' must be a character string"),
ARGUMENT_MUST_BE_STRING("argument '%s' must be a character string"),
ARGUMENT_MUST_BE_RAW_VECTOR("argument '%s' must be a raw vector"),
MUST_BE_NONNULL_STRING("'%s' must be non-null character string"),
IS_OF_WRONG_LENGTH("'%s' is of wrong length %d (!= %d)"),
IS_OF_WRONG_ARITY("'%d' argument passed to '%s' which requires '%d'"),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment