Search in sources :

Example 36 with TruffleString

use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.

the class TStringUTF8Tests method testCodePointLength2.

@Test
public void testCodePointLength2() {
    byte[] arr = TStringTestUtil.byteArray(0, 0, 0xc0, 0xbf);
    TruffleString a = TruffleString.fromByteArrayUncached(arr, 0, arr.length, UTF_8, false);
    Assert.assertEquals(4, a.codePointLengthUncached(UTF_8));
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) Test(org.junit.Test)

Example 37 with TruffleString

use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.

the class TStringUTF8Tests method testIndexOf2.

@Test
public void testIndexOf2() {
    TruffleString a = TruffleString.fromCodePointUncached(0x102, UTF_8);
    TruffleString b = TruffleString.fromCodePointUncached(0x10_0304, UTF_8);
    TruffleString s1 = a.repeatUncached(10, UTF_8);
    TruffleString s2 = a.concatUncached(b, UTF_8, false);
    Assert.assertEquals(-1, s1.byteIndexOfStringUncached(s2, 0, s1.byteLength(UTF_8), UTF_8));
    Assert.assertEquals(-1, s1.indexOfStringUncached(s2, 0, s1.codePointLengthUncached(UTF_8), UTF_8));
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) Test(org.junit.Test)

Example 38 with TruffleString

use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.

the class TStringRegionEqualByteIndexTest method testWithMask.

@Test
public void testWithMask() throws Exception {
    String javaStrA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int[] codepointsA = toIntArray(javaStrA);
    TruffleString strA = TruffleString.fromJavaStringUncached(javaStrA, TruffleString.Encoding.UTF_16);
    TruffleString strB = TruffleString.fromJavaStringUncached("abc", TruffleString.Encoding.UTF_16);
    TruffleString.WithMask[] withMask = { TruffleString.WithMask.createUncached(strB.switchEncodingUncached(TruffleString.Encoding.UTF_8), new byte[] { 0x20, 0x20, 0x20 }, TruffleString.Encoding.UTF_8), TruffleString.WithMask.createUTF16Uncached(strB.switchEncodingUncached(TruffleString.Encoding.UTF_16), new char[] { 0x20, 0x20, 0x20 }), TruffleString.WithMask.createUTF32Uncached(strB.switchEncodingUncached(TruffleString.Encoding.UTF_32), new int[] { 0x20, 0x20, 0x20 }) };
    TruffleString.Encoding[] encodings = { TruffleString.Encoding.UTF_8, TruffleString.Encoding.UTF_16, TruffleString.Encoding.UTF_32 };
    for (int i = 0; i < encodings.length; i++) {
        TruffleString.Encoding encoding = encodings[i];
        byte[] arr = new byte[strA.byteLength(encoding)];
        strA.switchEncodingUncached(encoding).copyToByteArrayNodeUncached(0, arr, 0, arr.length, encoding);
        int iFinal = i;
        checkStringVariants(arr, TruffleString.CodeRange.ASCII, true, encoding, codepointsA, null, (a, array, codeRange, isValid, enc, codepoints, byteIndices) -> {
            Assert.assertTrue(node.execute(a, 0, withMask[iFinal], 0, strB.switchEncodingUncached(encoding).byteLength(encoding), encoding));
        });
    }
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) AbstractTruffleString(com.oracle.truffle.api.strings.AbstractTruffleString) TruffleString(com.oracle.truffle.api.strings.TruffleString) AbstractTruffleString(com.oracle.truffle.api.strings.AbstractTruffleString) Test(org.junit.Test)

Example 39 with TruffleString

use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.

the class HostToTypeNode method convertImpl.

private static Object convertImpl(Object value, Class<?> targetType, Type genericType, boolean allowsImplementation, boolean primitiveTargetType, HostContext context, InteropLibrary interop, boolean useCustomTargetTypes, HostTargetMappingNode targetMapping, BranchProfile error) {
    if (useCustomTargetTypes) {
        Object result = targetMapping.execute(value, targetType, context, interop, false, HIGHEST, STRICT);
        if (result != HostTargetMappingNode.NO_RESULT) {
            return result;
        }
    }
    Object convertedValue;
    if (primitiveTargetType) {
        convertedValue = HostUtil.convertLossLess(value, targetType, interop);
        if (convertedValue != null) {
            return convertedValue;
        }
    }
    HostLanguage language = HostLanguage.get(interop);
    if (HostObject.isJavaInstance(language, targetType, value)) {
        return HostObject.valueOf(language, value);
    }
    if (useCustomTargetTypes) {
        convertedValue = targetMapping.execute(value, targetType, context, interop, false, STRICT + 1, LOOSE);
        if (convertedValue != HostTargetMappingNode.NO_RESULT) {
            return convertedValue;
        }
    }
    if (primitiveTargetType) {
        convertedValue = HostUtil.convertLossy(value, targetType, interop);
        if (convertedValue != null) {
            return convertedValue;
        }
    }
    if (targetType == Value.class && context != null) {
        return value instanceof Value ? value : context.asValue(interop, value);
    } else if (interop.isNull(value)) {
        if (targetType.isPrimitive()) {
            throw HostInteropErrors.nullCoercion(context, value, targetType);
        }
        return null;
    } else if (value instanceof TruffleObject) {
        convertedValue = asJavaObject(context, (TruffleObject) value, targetType, genericType, allowsImplementation);
        if (convertedValue != null) {
            return convertedValue;
        }
    // no default conversion available but we can still try target type mappings.
    } else if (value instanceof TruffleString && targetType.isAssignableFrom(String.class)) {
        try {
            return interop.asString(value);
        } catch (UnsupportedMessageException e) {
            throw shouldNotReachHere(e);
        }
    }
    if (targetType.isInstance(value)) {
        convertedValue = value;
    } else {
        if (useCustomTargetTypes) {
            Object result = targetMapping.execute(value, targetType, context, interop, false, LOOSE + 1, LOWEST);
            if (result != HostTargetMappingNode.NO_RESULT) {
                return result;
            }
        }
        error.enter();
        throw HostInteropErrors.cannotConvertPrimitive(context, value, targetType);
    }
    return targetType.cast(convertedValue);
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) Value(org.graalvm.polyglot.Value) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Aggregations

TruffleString (com.oracle.truffle.api.strings.TruffleString)39 Test (org.junit.Test)26 MutableTruffleString (com.oracle.truffle.api.strings.MutableTruffleString)16 AbstractTruffleString (com.oracle.truffle.api.strings.AbstractTruffleString)9 RootCallTarget (com.oracle.truffle.api.RootCallTarget)3 SLExpressionNode (com.oracle.truffle.sl.nodes.SLExpressionNode)3 SLRootNode (com.oracle.truffle.sl.nodes.SLRootNode)3 Encoding (org.graalvm.shadowed.org.jcodings.Encoding)3 FrameDescriptor (com.oracle.truffle.api.frame.FrameDescriptor)2 RootNode (com.oracle.truffle.api.nodes.RootNode)2 TruffleStringBuilder (com.oracle.truffle.api.strings.TruffleStringBuilder)2 TruffleStringIterator (com.oracle.truffle.api.strings.TruffleStringIterator)2 SLEvalRootNode (com.oracle.truffle.sl.nodes.SLEvalRootNode)2 SLStringLiteralNode (com.oracle.truffle.sl.nodes.expression.SLStringLiteralNode)2 BigInteger (java.math.BigInteger)2 CallTarget (com.oracle.truffle.api.CallTarget)1 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)1 Specialization (com.oracle.truffle.api.dsl.Specialization)1 Frame (com.oracle.truffle.api.frame.Frame)1 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)1