use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.
the class TStringCornerCaseTests method testConcatMutable.
@Test
public void testConcatMutable() {
TruffleString a = TruffleString.Encoding.UTF_8.getEmpty();
MutableTruffleString b = MutableTruffleString.fromByteArrayUncached(new byte[0], 0, 0, TruffleString.Encoding.BYTES, false);
Assert.assertTrue(a.concatUncached(b, TruffleString.Encoding.UTF_8, true).isEmpty());
}
use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.
the class TStringTestBase method checkStringVariants.
protected static void checkStringVariants(byte[] array, TruffleString.CodeRange codeRange, boolean isValid, TruffleString.Encoding encoding, int[] codepoints, int[] byteIndices, TestStrings test) throws Exception {
byte[] arrayPadded = pad(array);
for (AbstractTruffleString string : new AbstractTruffleString[] { TruffleString.fromByteArrayUncached(array, 0, array.length, encoding, false), TruffleString.fromNativePointerUncached(PointerObject.create(array), 0, array.length, encoding, false), TruffleString.fromNativePointerUncached(PointerObject.create(array), 0, array.length, encoding, true), MutableTruffleString.fromByteArrayUncached(array, 0, array.length, encoding, true), MutableTruffleString.fromNativePointerUncached(PointerObject.create(array), 0, array.length, encoding, false), MutableTruffleString.fromNativePointerUncached(PointerObject.create(array), 0, array.length, encoding, true), TruffleString.fromByteArrayUncached(arrayPadded, 1, array.length, encoding, false), TruffleString.fromNativePointerUncached(PointerObject.create(arrayPadded), 1, array.length, encoding, false), MutableTruffleString.fromByteArrayUncached(arrayPadded, 1, array.length, encoding, true), MutableTruffleString.fromNativePointerUncached(PointerObject.create(arrayPadded), 1, array.length, encoding, false), MutableTruffleString.fromNativePointerUncached(PointerObject.create(arrayPadded), 1, array.length, encoding, true) }) {
test.run(string, array, codeRange, isValid, encoding, codepoints, byteIndices);
}
if (codeRange == TruffleString.CodeRange.ASCII && isAsciiCompatible(encoding)) {
byte[] bytesUTF16 = new byte[(codepoints.length + 1) * 2];
for (int i = 0; i < codepoints.length; i++) {
TStringTestUtil.writeValue(bytesUTF16, 1, i, codepoints[i]);
}
TStringTestUtil.writeValue(bytesUTF16, 1, codepoints.length, 0xffff);
TruffleString string = TruffleString.fromByteArrayUncached(bytesUTF16, 0, bytesUTF16.length, UTF_16, false).substringByteIndexUncached(0, bytesUTF16.length - 2, UTF_16, true).switchEncodingUncached(encoding);
test.run(string, array, codeRange, isValid, encoding, codepoints, byteIndices);
}
if (codeRange == TruffleString.CodeRange.ASCII && isAsciiCompatible(encoding) || codeRange == TruffleString.CodeRange.LATIN_1 && isUTF16(encoding)) {
byte[] bytesUTF32 = new byte[(codepoints.length + 1) * 4];
for (int i = 0; i < codepoints.length; i++) {
TStringTestUtil.writeValue(bytesUTF32, 2, i, codepoints[i]);
}
TStringTestUtil.writeValue(bytesUTF32, 2, codepoints.length, 0x10ffff);
TruffleString string = TruffleString.fromByteArrayUncached(bytesUTF32, 0, bytesUTF32.length, UTF_32, false).substringByteIndexUncached(0, bytesUTF32.length - 4, UTF_32, true).switchEncodingUncached(encoding);
test.run(string, array, codeRange, isValid, encoding, codepoints, byteIndices);
}
}
use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.
the class TStringTests method testASCII.
@Test
public void testASCII() {
String s = "asdf";
TruffleString ts = fjs(s);
Assert.assertEquals(s, ts.toString());
Assert.assertEquals(s.length(), ts.byteLength(TruffleString.Encoding.UTF_16) >> 1);
Assert.assertEquals(s.length(), ts.codePointLengthUncached(TruffleString.Encoding.UTF_16));
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
int iBytes = i << 1;
int lengthBytes = s.length() << 1;
TruffleString cs = TruffleString.FromCodePointNode.getUncached().execute(c, TruffleString.Encoding.UTF_16);
Assert.assertEquals(c, ts.readCharUTF16Uncached(i));
Assert.assertEquals(c, ts.codePointAtIndexUncached(i, TruffleString.Encoding.UTF_16));
Assert.assertEquals(c, ts.codePointAtByteIndexUncached(iBytes, TruffleString.Encoding.UTF_16));
Assert.assertEquals(i, ts.indexOfCodePointUncached(c, 0, s.length(), TruffleString.Encoding.UTF_16));
Assert.assertEquals(iBytes, ts.byteIndexOfCodePointUncached(c, 0, lengthBytes, TruffleString.Encoding.UTF_16));
Assert.assertEquals(i, ts.indexOfStringUncached(cs, 0, s.length(), TruffleString.Encoding.UTF_16));
Assert.assertEquals(iBytes, ts.byteIndexOfStringUncached(cs, 0, lengthBytes, TruffleString.Encoding.UTF_16));
Assert.assertEquals(i, ts.lastIndexOfCodePointUncached(c, s.length(), 0, TruffleString.Encoding.UTF_16));
Assert.assertEquals(iBytes, ts.lastByteIndexOfCodePointUncached(c, lengthBytes, 0, TruffleString.Encoding.UTF_16));
Assert.assertEquals(i, ts.lastIndexOfStringUncached(cs, s.length(), 0, TruffleString.Encoding.UTF_16));
Assert.assertEquals(iBytes, ts.lastByteIndexOfStringUncached(cs, lengthBytes, 0, TruffleString.Encoding.UTF_16));
}
}
use of com.oracle.truffle.api.strings.TruffleString 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());
}
}
}
});
}
use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.
the class TStringBasicTests method testTransCode.
private static void testTransCode(int codepoint, TruffleString.Encoding encodingA) throws Exception {
TruffleString stringA = TruffleString.fromCodePointUncached(codepoint, encodingA);
Assert.assertEquals(codepoint, stringA.codePointAtIndexUncached(0, encodingA));
if (isAsciiCompatible(encodingA)) {
for (TruffleString.Encoding encodingB : TruffleString.Encoding.values()) {
if (isAsciiCompatible(encodingB) && codepoint <= 0x7f) {
switchEncodingEquivalentCodePoint(codepoint, encodingA, stringA, encodingB);
}
}
}
if (isUTF(encodingA)) {
if (codepoint <= 0xff) {
switchEncodingEquivalentCodePoint(codepoint, encodingA, stringA, TruffleString.Encoding.ISO_8859_1);
}
for (TruffleString.Encoding encodingB : new TruffleString.Encoding[] { TruffleString.Encoding.UTF_8, TruffleString.Encoding.UTF_16, TruffleString.Encoding.UTF_32 }) {
switchEncodingEquivalentCodePoint(codepoint, encodingA, stringA, encodingB);
}
}
}
Aggregations