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