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));
}
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));
}
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));
});
}
}
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);
}
Aggregations