Search in sources :

Example 1 with TruffleStringBuilder

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

the class TStringBuilderAppendIntTest method testAll.

@Test
public void testAll() throws Exception {
    forAllEncodings(encoding -> {
        if (isAsciiCompatible(encoding)) {
            for (int n : new int[] { Integer.MIN_VALUE, Integer.MIN_VALUE + 1, Short.MIN_VALUE, -12345, -1, 0, 1, 12345, Short.MAX_VALUE, Integer.MAX_VALUE - 1, Integer.MAX_VALUE }) {
                String javaString = String.valueOf(n);
                int[] codepoints = TStringTestUtil.toIntArray(javaString);
                byte[] array = TStringTestUtil.asciiArray(encoding, javaString);
                TruffleStringBuilder sb = TruffleStringBuilder.create(encoding);
                node.execute(sb, n);
                checkStringBuilderResult(array, TruffleString.CodeRange.ASCII, true, encoding, codepoints, sb);
            }
        }
    });
}
Also used : TruffleStringBuilder(com.oracle.truffle.api.strings.TruffleStringBuilder) TruffleString(com.oracle.truffle.api.strings.TruffleString) Test(org.junit.Test)

Example 2 with TruffleStringBuilder

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

the class TStringConstructorTests method testFromByteArray.

@Test
public void testFromByteArray() throws Exception {
    forAllEncodings((TruffleString.Encoding encoding) -> {
        if (isAsciiCompatible(encoding)) {
            byte[] ascii = new byte[128 << getStride(encoding)];
            for (int i = 0; i < 128; i++) {
                TStringTestUtil.writeValue(ascii, getStride(encoding), i, i);
            }
            for (boolean copy : new boolean[] { true, false }) {
                TruffleString s = fromByteArrayUncached(ascii, 0, ascii.length, encoding, copy);
                int readByteOffset = 0;
                if (isUTF32(encoding) && ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
                    readByteOffset = 3;
                }
                if (isUTF16(encoding) && ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
                    readByteOffset = 1;
                }
                TruffleStringIterator it = s.createCodePointIteratorUncached(encoding);
                for (int i = 0; i < 128; i++) {
                    Assert.assertEquals(i, s.readByteUncached(byteIndex(i, encoding) + readByteOffset, encoding));
                    Assert.assertEquals(i, s.codePointAtByteIndexUncached(byteIndex(i, encoding), encoding));
                    Assert.assertEquals(i, s.codePointAtIndexUncached(i, encoding));
                    Assert.assertTrue(it.hasNext());
                    Assert.assertEquals(i, it.nextUncached());
                    Assert.assertEquals(i, s.indexOfCodePointUncached(i, 0, 128, encoding));
                    Assert.assertEquals(i, s.indexOfStringUncached(fromCodePointUncached(i, encoding), 0, 128, encoding));
                }
            }
        }
    });
    forAllEncodingsAndCodePointLists((TruffleString.Encoding encoding, int[] codepointArray) -> {
        final int[][] cps;
        if (isUTF16(encoding) || isUTF32(encoding)) {
            cps = new int[][] { new int[] { 0x00, 0x7f }, new int[] { 0x00, 0x7f, 0xff }, new int[] { 0x00, 0x7f, 0xff, 0x100, 0xffff }, codepointArray };
        } else {
            cps = new int[][] { codepointArray };
        }
        for (int[] codepoints : cps) {
            TruffleStringBuilder sbBytes = TruffleStringBuilder.create(encoding);
            TruffleStringBuilder sbCP = TruffleStringBuilder.create(encoding);
            TruffleStringBuilder sbCPStrings = TruffleStringBuilder.create(encoding);
            Encoding jCoding = Encodings.getJCoding(encoding);
            int byteLength = 0;
            int[] byteIndices = new int[codepoints.length];
            for (int i = 0; i < codepoints.length; i++) {
                byteIndices[i] = byteLength;
                byteLength += jCoding.codeToMbcLength(codepoints[i]);
            }
            byte[] array = new byte[byteLength];
            for (int i = 0; i < codepoints.length; i++) {
                jCoding.codeToMbc(codepoints[i], array, byteIndices[i]);
                sbCP.appendCodePointUncached(codepoints[i]);
                sbCPStrings.appendStringUncached(fromCodePointUncached(codepoints[i], encoding));
            }
            if (isUTF32(encoding)) {
                for (int cp : codepoints) {
                    sbBytes.appendCodePointUncached(cp);
                }
            } else if (isUTF16(encoding)) {
                for (int i = 0; i < byteLength / 2; i++) {
                    sbBytes.appendCharUTF16Uncached((char) TStringTestUtil.readValue(array, 1, i));
                }
            } else {
                for (byte b : array) {
                    sbBytes.appendByteUncached(b);
                }
            }
            AbstractTruffleString[] strings = new AbstractTruffleString[isUTF32(encoding) || isUTF16(encoding) ? 9 : 8];
            strings[0] = fromByteArrayUncached(array, 0, array.length, encoding, true);
            strings[1] = fromByteArrayUncached(array, 0, array.length, encoding, false);
            strings[2] = fromNativePointerUncached(PointerObject.create(array), 0, array.length, encoding, false);
            strings[3] = fromNativePointerUncached(PointerObject.create(array), 0, array.length, encoding, true);
            strings[4] = MutableTruffleString.fromByteArrayUncached(array, 0, array.length, encoding, true);
            strings[5] = MutableTruffleString.fromNativePointerUncached(PointerObject.create(array), 0, array.length, encoding, false);
            strings[6] = strings[2].asMutableTruffleStringUncached(encoding);
            strings[7] = strings[4].asTruffleStringUncached(encoding);
            if (isUTF16(encoding)) {
                char[] charArray = new char[array.length / 2];
                for (int i = 0; i < charArray.length; i++) {
                    charArray[i] = (char) TStringTestUtil.readValue(array, 1, i);
                }
                strings[8] = fromCharArrayUTF16Uncached(charArray, 0, charArray.length);
            } else if (isUTF32(encoding)) {
                strings[8] = fromIntArrayUTF32Uncached(codepoints, 0, codepoints.length);
            }
            for (AbstractTruffleString s : strings) {
                for (TruffleStringBuilder sb : new TruffleStringBuilder[] { sbBytes, sbCP, sbCPStrings }) {
                    TruffleString sbs = sb.toStringUncached();
                    Assert.assertTrue(sbs.equalsUncached(s, encoding));
                    Assert.assertEquals(sbs, s);
                    Assert.assertEquals(sbs.hashCode(), s.hashCode());
                    Assert.assertEquals(sbs.hashCodeUncached(encoding), s.hashCodeUncached(encoding));
                    Assert.assertEquals(sbs.getCodeRangeUncached(encoding), s.getCodeRangeUncached(encoding));
                }
                TruffleStringBuilder sbCMP = TruffleStringBuilder.create(encoding);
                for (int i = 0; i < codepoints.length - 1; i++) {
                    sbCMP.appendCodePointUncached(codepoints[i]);
                }
                sbCMP.appendCodePointUncached(codepoints[codepoints.length - 1] - 1);
                TruffleString cmp = sbCMP.toStringUncached();
                Assert.assertEquals(0, s.compareBytesUncached(s, encoding));
                Assert.assertTrue(s.compareBytesUncached(cmp, encoding) > 0);
                Assert.assertTrue(cmp.compareBytesUncached(s, encoding) < 0);
                s.toJavaStringUncached();
                TruffleStringIterator it = s.createCodePointIteratorUncached(encoding);
                Assert.assertEquals(codepoints.length, s.codePointLengthUncached(encoding));
                Assert.assertTrue(s.isValidUncached(encoding));
                for (int i = 0; i < array.length; i++) {
                    Assert.assertEquals(Byte.toUnsignedInt(array[i]), s.readByteUncached(i, encoding));
                }
                checkInternalByteArrayEquals(array, s.getInternalByteArrayUncached(encoding));
                byte[] copy = new byte[array.length];
                s.copyToByteArrayNodeUncached(0, copy, 0, copy.length, encoding);
                Assert.assertArrayEquals(array, copy);
                PointerObject pointerObject = PointerObject.create(array.length);
                s.copyToNativeMemoryNodeUncached(0, pointerObject, 0, array.length, encoding);
                Assert.assertTrue(pointerObject.contentEquals(array));
                for (int i = 0; i < codepoints.length; i++) {
                    Assert.assertEquals(codepoints[i], s.codePointAtIndexUncached(i, encoding));
                    Assert.assertEquals(codepoints[i], s.codePointAtByteIndexUncached(byteIndices[i], encoding));
                    Assert.assertEquals(i, s.indexOfCodePointUncached(codepoints[i], 0, codepoints.length, encoding));
                    Assert.assertEquals(byteIndices[i], s.byteIndexOfCodePointUncached(codepoints[i], 0, byteLength, encoding));
                    Assert.assertEquals(i, s.lastIndexOfCodePointUncached(codepoints[i], codepoints.length, 0, encoding));
                    Assert.assertEquals(byteIndices[i], s.lastByteIndexOfCodePointUncached(codepoints[i], byteLength, 0, encoding));
                    TruffleString s1 = fromByteArrayUncached(array, byteIndices[i], (i + 1 < codepoints.length ? byteIndices[i + 1] : array.length) - byteIndices[i], encoding, true);
                    TruffleString s2 = fromByteArrayUncached(array, byteIndices[i], (i + 2 < codepoints.length ? byteIndices[i + 2] : array.length) - byteIndices[i], encoding, false);
                    TruffleString s3 = fromByteArrayUncached(array, byteIndices[i], (i + 3 < codepoints.length ? byteIndices[i + 3] : array.length) - byteIndices[i], encoding, false);
                    s1.toJavaStringUncached();
                    s2.toJavaStringUncached();
                    s3.toJavaStringUncached();
                    for (TruffleString substring : new TruffleString[] { s1, s2, s3 }) {
                        Assert.assertEquals(i, s.indexOfStringUncached(substring, 0, codepoints.length, encoding));
                        Assert.assertEquals(byteIndices[i], s.byteIndexOfStringUncached(substring, 0, byteLength, encoding));
                        Assert.assertEquals(i, s.lastIndexOfStringUncached(substring, codepoints.length, 0, encoding));
                        Assert.assertEquals(byteIndices[i], s.lastByteIndexOfStringUncached(substring, byteLength, 0, encoding));
                        Assert.assertTrue(s.regionEqualsUncached(i, substring, 0, substring.codePointLengthUncached(encoding), encoding));
                        Assert.assertTrue(s.regionEqualByteIndexUncached(byteIndices[i], substring, 0, substring.byteLength(encoding), encoding));
                    }
                    Assert.assertTrue(it.hasNext());
                    Assert.assertEquals(codepoints[i], it.nextUncached());
                }
            }
        }
    });
}
Also used : TruffleStringIterator(com.oracle.truffle.api.strings.TruffleStringIterator) TruffleStringBuilder(com.oracle.truffle.api.strings.TruffleStringBuilder) Encoding(org.graalvm.shadowed.org.jcodings.Encoding) MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) 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 3 with TruffleStringBuilder

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

the class SLStackTraceBuiltin method createStackTrace.

@TruffleBoundary
private static TruffleString createStackTrace() {
    final TruffleStringBuilder str = TruffleStringBuilder.create(SLLanguage.STRING_ENCODING);
    Truffle.getRuntime().iterateFrames(new FrameInstanceVisitor<Integer>() {

        // skip stack trace builtin
        private int skip = 1;

        @Override
        public Integer visitFrame(FrameInstance frameInstance) {
            if (skip > 0) {
                skip--;
                return null;
            }
            CallTarget callTarget = frameInstance.getCallTarget();
            Frame frame = frameInstance.getFrame(FrameAccess.READ_ONLY);
            RootNode rn = ((RootCallTarget) callTarget).getRootNode();
            // ignore internal or interop stack frames
            if (rn.isInternal() || rn.getLanguageInfo() == null) {
                return 1;
            }
            if (str.byteLength() > 0) {
                str.appendStringUncached(SLStrings.fromJavaString(System.getProperty("line.separator")));
            }
            str.appendStringUncached(FRAME);
            str.appendStringUncached(getRootNodeName(rn));
            FrameDescriptor frameDescriptor = frame.getFrameDescriptor();
            int count = frameDescriptor.getNumberOfSlots();
            for (int i = 0; i < count; i++) {
                str.appendStringUncached(SEPARATOR);
                str.appendStringUncached((TruffleString) frameDescriptor.getSlotName(i));
                str.appendStringUncached(EQUALS);
                str.appendStringUncached(SLStrings.fromObject(frame.getValue(i)));
            }
            return null;
        }
    });
    return str.toStringUncached();
}
Also used : FrameDescriptor(com.oracle.truffle.api.frame.FrameDescriptor) SLRootNode(com.oracle.truffle.sl.nodes.SLRootNode) SLEvalRootNode(com.oracle.truffle.sl.nodes.SLEvalRootNode) RootNode(com.oracle.truffle.api.nodes.RootNode) Frame(com.oracle.truffle.api.frame.Frame) TruffleString(com.oracle.truffle.api.strings.TruffleString) RootCallTarget(com.oracle.truffle.api.RootCallTarget) CallTarget(com.oracle.truffle.api.CallTarget) TruffleStringBuilder(com.oracle.truffle.api.strings.TruffleStringBuilder) FrameInstance(com.oracle.truffle.api.frame.FrameInstance) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 4 with TruffleStringBuilder

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

the class TStringBuilderAppendByteTest method testAll.

@Test
public void testAll() throws Exception {
    forAllStrings(true, (a, array, codeRange, isValid, encoding, codepoints, byteIndices) -> {
        if (encoding != TruffleString.Encoding.UTF_16 && encoding != TruffleString.Encoding.UTF_32) {
            TruffleStringBuilder sb = TruffleStringBuilder.create(encoding);
            for (byte b : array) {
                node.execute(sb, b);
            }
            checkStringBuilderResult(array, codeRange, isValid, encoding, codepoints, sb);
        }
    });
}
Also used : TruffleStringBuilder(com.oracle.truffle.api.strings.TruffleStringBuilder) Test(org.junit.Test)

Example 5 with TruffleStringBuilder

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

the class TStringBuilderAppendJavaStringUTF16Test method testAll.

@Test
public void testAll() throws Exception {
    forAllStrings(new TruffleString.Encoding[] { TruffleString.Encoding.UTF_16 }, true, (a, array, codeRange, isValid, encoding, codepoints, byteIndices) -> {
        TruffleStringBuilder sb = TruffleStringBuilder.create(encoding);
        for (int i = 0; i < array.length / 2; i++) {
            node.execute(sb, String.valueOf((char) TStringTestUtil.readValue(array, 1, i)), 0, 1);
        }
        checkStringBuilderResult(array, codeRange, isValid, encoding, codepoints, sb);
        String javaString = a.toJavaStringUncached();
        sb = TruffleStringBuilder.create(encoding);
        for (int i = 0; i < array.length / 2; i++) {
            node.execute(sb, javaString, i, 1);
        }
        checkStringBuilderResult(array, codeRange, isValid, encoding, codepoints, sb);
    });
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) TruffleStringBuilder(com.oracle.truffle.api.strings.TruffleStringBuilder) TruffleString(com.oracle.truffle.api.strings.TruffleString) Test(org.junit.Test)

Aggregations

TruffleStringBuilder (com.oracle.truffle.api.strings.TruffleStringBuilder)10 Test (org.junit.Test)9 TruffleString (com.oracle.truffle.api.strings.TruffleString)8 CallTarget (com.oracle.truffle.api.CallTarget)1 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)1 RootCallTarget (com.oracle.truffle.api.RootCallTarget)1 Frame (com.oracle.truffle.api.frame.Frame)1 FrameDescriptor (com.oracle.truffle.api.frame.FrameDescriptor)1 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)1 RootNode (com.oracle.truffle.api.nodes.RootNode)1 AbstractTruffleString (com.oracle.truffle.api.strings.AbstractTruffleString)1 MutableTruffleString (com.oracle.truffle.api.strings.MutableTruffleString)1 TruffleStringIterator (com.oracle.truffle.api.strings.TruffleStringIterator)1 SLEvalRootNode (com.oracle.truffle.sl.nodes.SLEvalRootNode)1 SLRootNode (com.oracle.truffle.sl.nodes.SLRootNode)1 Encoding (org.graalvm.shadowed.org.jcodings.Encoding)1