Search in sources :

Example 1 with TruffleStringIterator

use of com.oracle.truffle.api.strings.TruffleStringIterator 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 2 with TruffleStringIterator

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

the class TStringToJavaStringTest method testAll.

@Test
public void testAll() throws Exception {
    forAllStrings(true, (a, array, codeRange, isValid, encoding, codepoints, byteIndices) -> {
        String s = node.execute(a);
        if (codeRange == TruffleString.CodeRange.ASCII || isUTF(encoding)) {
            TruffleStringIterator it = a.createCodePointIteratorUncached(encoding);
            int i = 0;
            while (it.hasNext()) {
                int expected = s.codePointAt(i);
                Assert.assertEquals(expected, it.nextUncached());
                i += expected > 0xffff ? 2 : 1;
            }
        }
        if (a instanceof TruffleString) {
            Assert.assertTrue(InteropLibrary.getUncached(a).isString(a));
            Assert.assertEquals(s, InteropLibrary.getUncached(a).asString(a));
        }
        if (encoding == TruffleString.Encoding.BYTES && codeRange != TruffleString.CodeRange.ASCII) {
            StringBuilder sb = new StringBuilder(array.length * 4);
            for (byte b : array) {
                if (b < 0) {
                    sb.append(String.format("\\x%02x", b));
                } else {
                    sb.append((char) b);
                }
            }
            Assert.assertEquals(sb.toString(), a.toString());
        } else {
            Assert.assertEquals(s, a.toString());
        }
    });
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) TruffleStringIterator(com.oracle.truffle.api.strings.TruffleStringIterator) TruffleString(com.oracle.truffle.api.strings.TruffleString) Test(org.junit.Test)

Example 3 with TruffleStringIterator

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

the class TStringBasicTests method testEncodeDecode.

private static void testEncodeDecode(int codepoint, TruffleString.Encoding encoding) {
    Assert.assertEquals(codepoint, TruffleString.fromCodePointUncached(codepoint, encoding).codePointAtIndexUncached(0, encoding));
    Assert.assertEquals(codepoint, TruffleString.fromCodePointUncached(codepoint, encoding).createCodePointIteratorUncached(encoding).nextUncached());
    Assert.assertEquals(codepoint, TruffleString.fromCodePointUncached(codepoint, encoding).createBackwardCodePointIteratorUncached(encoding).previousUncached());
    if (isAsciiCompatible(encoding) && codepoint <= 0x7f || isUTF(encoding)) {
        String javaString = TruffleString.fromCodePointUncached(codepoint, encoding).toJavaStringUncached();
        Assert.assertEquals(codepoint, javaString.codePointAt(0));
        Assert.assertEquals(codepoint, TruffleString.fromJavaStringUncached(javaString, TruffleString.Encoding.UTF_16).codePointAtIndexUncached(0, TruffleString.Encoding.UTF_16));
        for (int first : new int[] { 'x', codepoint }) {
            Assert.assertEquals(codepoint, TruffleString.fromCodePointUncached(first, encoding).concatUncached(TruffleString.fromCodePointUncached(codepoint, encoding), encoding, true).codePointAtIndexUncached(1, encoding));
            Assert.assertEquals(codepoint, TruffleString.fromCodePointUncached(first, encoding).concatUncached(TruffleString.fromCodePointUncached(codepoint, encoding), encoding, true).substringUncached(1, 1, encoding, true).codePointAtIndexUncached(0, encoding));
            TruffleStringIterator it = TruffleString.fromCodePointUncached(first, encoding).concatUncached(TruffleString.fromCodePointUncached(codepoint, encoding), encoding, true).createCodePointIteratorUncached(encoding);
            Assert.assertEquals(first, it.nextUncached());
            Assert.assertEquals(codepoint, it.nextUncached());
            Assert.assertEquals(codepoint, it.previousUncached());
            Assert.assertEquals(first, it.previousUncached());
        }
    }
}
Also used : TruffleStringIterator(com.oracle.truffle.api.strings.TruffleStringIterator) TruffleString(com.oracle.truffle.api.strings.TruffleString)

Aggregations

TruffleString (com.oracle.truffle.api.strings.TruffleString)3 TruffleStringIterator (com.oracle.truffle.api.strings.TruffleStringIterator)3 Test (org.junit.Test)2 AbstractTruffleString (com.oracle.truffle.api.strings.AbstractTruffleString)1 MutableTruffleString (com.oracle.truffle.api.strings.MutableTruffleString)1 TruffleStringBuilder (com.oracle.truffle.api.strings.TruffleStringBuilder)1 Encoding (org.graalvm.shadowed.org.jcodings.Encoding)1