Search in sources :

Example 21 with TruffleString

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

the class SLReadPropertyNode method readSLObject.

@Specialization(limit = "LIBRARY_LIMIT")
protected Object readSLObject(SLObject receiver, Object name, @CachedLibrary("receiver") DynamicObjectLibrary objectLibrary, @Cached SLToTruffleStringNode toTruffleStringNode) {
    TruffleString nameTS = toTruffleStringNode.execute(name);
    Object result = objectLibrary.getOrDefault(receiver, nameTS, null);
    if (result == null) {
        // read was not successful. In SL we only have basic support for errors.
        throw SLUndefinedNameException.undefinedProperty(this, nameTS);
    }
    return result;
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) SLObject(com.oracle.truffle.sl.runtime.SLObject) Specialization(com.oracle.truffle.api.dsl.Specialization)

Example 22 with TruffleString

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

the class TStringCornerCaseTests method testForceEncodingStringCompaction.

@Test
public void testForceEncodingStringCompaction() {
    TruffleString a = TruffleString.fromJavaStringUncached("abc", TruffleString.Encoding.UTF_8);
    TruffleString forced = a.forceEncodingUncached(TruffleString.Encoding.UTF_8, TruffleString.Encoding.UTF_8);
    Assert.assertEquals(3, forced.byteLength(TruffleString.Encoding.UTF_8));
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) Test(org.junit.Test)

Example 23 with TruffleString

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

the class TStringCornerCaseTests method testTranscodeYieldsEmptyString.

@Test
public void testTranscodeYieldsEmptyString() {
    byte[] arr = byteArray(27, 40, 66);
    TruffleString a = TruffleString.fromByteArrayUncached(arr, 0, arr.length, TruffleString.Encoding.ISO_2022_JP, false);
    Assert.assertEquals("", a.toString());
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) Test(org.junit.Test)

Example 24 with TruffleString

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

the class TStringTestBase method testIndexOfString.

protected static void testIndexOfString(AbstractTruffleString a, byte[] array, boolean isValid, TruffleString.Encoding encoding, int[] codepoints, int[] byteIndices, boolean byteIndex, boolean lastIndex, TestIndexOfString test) {
    if (!isValid) {
        // ignore broken strings
        return;
    }
    int lastCPI = codepoints.length - 1;
    int firstCodepoint = codepoints[0];
    int lastCodepoint = codepoints[lastCPI];
    TruffleString first = TruffleString.fromCodePointUncached(firstCodepoint, encoding);
    TruffleString firstSubstring = a.substringByteIndexUncached(0, codepoints.length == 1 ? array.length : byteIndices[1], encoding, true);
    TruffleString last = TruffleString.fromCodePointUncached(lastCodepoint, encoding);
    TruffleString lastSubstring = a.substringByteIndexUncached(byteIndices[lastCPI], array.length - byteIndices[lastCPI], encoding, true);
    int expectedFirst = lastIndex ? lastIndexOfCodePoint(codepoints, byteIndices, byteIndex, codepoints.length, 0, firstCodepoint) : 0;
    int expectedLast = lastIndex ? byteIndex ? byteIndices[lastCPI] : lastCPI : indexOfCodePoint(codepoints, byteIndices, byteIndex, 0, codepoints.length, lastCodepoint);
    int fromIndex;
    int toIndex;
    if (lastIndex) {
        fromIndex = byteIndex ? array.length : codepoints.length;
        toIndex = 0;
    } else {
        fromIndex = 0;
        toIndex = byteIndex ? array.length : codepoints.length;
    }
    test.run(first, fromIndex, toIndex, expectedFirst);
    test.run(firstSubstring, fromIndex, toIndex, expectedFirst);
    test.run(last, fromIndex, toIndex, expectedLast);
    test.run(lastSubstring, fromIndex, toIndex, expectedLast);
    test.run(first, 0, 0, -1);
    int i1 = byteIndex ? byteIndices[1] : 1;
    int iLast1 = byteIndex ? byteIndices[codepoints.length - 1] : codepoints.length - 1;
    if (lastIndex) {
        expectedFirst = lastIndexOfCodePoint(codepoints, byteIndices, byteIndex, codepoints.length, 1, firstCodepoint);
        expectedLast = lastIndexOfCodePoint(codepoints, byteIndices, byteIndex, codepoints.length - 1, 0, lastCodepoint);
        test.run(first, fromIndex, i1, expectedFirst);
        test.run(firstSubstring, fromIndex, i1, expectedFirst);
        test.run(last, iLast1, toIndex, expectedLast);
        test.run(lastSubstring, iLast1, toIndex, expectedLast);
    } else {
        expectedFirst = indexOfCodePoint(codepoints, byteIndices, byteIndex, 1, codepoints.length, firstCodepoint);
        expectedLast = indexOfCodePoint(codepoints, byteIndices, byteIndex, 0, codepoints.length - 1, lastCodepoint);
        test.run(first, i1, toIndex, expectedFirst);
        test.run(firstSubstring, i1, toIndex, expectedFirst);
        test.run(last, fromIndex, iLast1, expectedLast);
        test.run(lastSubstring, fromIndex, iLast1, expectedLast);
    }
}
Also used : AbstractTruffleString(com.oracle.truffle.api.strings.AbstractTruffleString) MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) TruffleString(com.oracle.truffle.api.strings.TruffleString)

Example 25 with TruffleString

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

the class TStringTestBase method checkStringBuilderResult.

protected static void checkStringBuilderResult(byte[] array, TruffleString.CodeRange codeRange, boolean isValid, TruffleString.Encoding encoding, int[] codepoints, TruffleStringBuilder sb) {
    TruffleString string = sb.toStringUncached();
    assertBytesEqual(string, encoding, array);
    assertCodePointsEqual(string, encoding, codepoints);
    Assert.assertEquals(codeRange, string.getCodeRangeUncached(encoding));
    Assert.assertEquals(isValid, string.isValidUncached(encoding));
}
Also used : AbstractTruffleString(com.oracle.truffle.api.strings.AbstractTruffleString) MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) TruffleString(com.oracle.truffle.api.strings.TruffleString)

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