public final class TruffleStringIterator extends Object
TruffleString
's codepoints, without
having to re-calculate codepoint indices on every access.
Usage Example:
abstract static class SomeNode extends Node {
@Specialization
static void someSpecialization(
TruffleString string,
@Cached TruffleString.CreateCodePointIteratorNode createCodePointIteratorNode,
@Cached TruffleStringIterator.NextNode nextNode,
@Cached TruffleString.CodePointLengthNode codePointLengthNode,
@Cached TruffleString.CodePointAtIndexNode codePointAtIndexNode) {
// iterating over a string's code points using TruffleStringIterator
TruffleStringIterator iterator = createCodePointIteratorNode.execute(string, Encoding.UTF_8);
while (iterator.hasNext()) {
System.out.printf("%x%n", nextNode.execute(iterator));
}
// uncached variant:
TruffleStringIterator iterator2 = string.createCodePointIteratorUncached(Encoding.UTF_8);
while (iterator2.hasNext()) {
System.out.printf("%x%n", iterator2.nextUncached());
}
// suboptimal variant: using CodePointAtIndexNode in a loop
int codePointLength = codePointLengthNode.execute(string, Encoding.UTF_8);
for (int i = 0; i < codePointLength; i++) {
// performance problem: codePointAtIndexNode may have to calculate the byte index
// corresponding
// to codepoint index i for every loop iteration
System.out.printf("%x%n", codePointAtIndexNode.execute(string, i, Encoding.UTF_8));
}
}
}
Modifier and Type | Class and Description |
---|---|
static class |
TruffleStringIterator.NextNode
Returns the next codepoint in the string.
|
static class |
TruffleStringIterator.PreviousNode
Returns the previous codepoint in the string.
|
Modifier and Type | Method and Description |
---|---|
int |
getByteIndex()
Returns the next codepoint's byte index, where "byte index" refers the codepoint's first byte
in forward mode, while in backward mode it refers to the first byte after the
codepoint.
|
boolean |
hasNext()
Returns
true if there are more codepoints remaining. |
boolean |
hasPrevious()
Returns
true if there are more codepoints remaining in reverse direction. |
int |
nextUncached()
Shorthand for calling the uncached version of
TruffleStringIterator.NextNode . |
int |
previousUncached()
Shorthand for calling the uncached version of
TruffleStringIterator.PreviousNode . |
public boolean hasNext()
true
if there are more codepoints remaining.public boolean hasPrevious()
true
if there are more codepoints remaining in reverse direction.public int getByteIndex()
public int nextUncached()
TruffleStringIterator.NextNode
.public int previousUncached()
TruffleStringIterator.PreviousNode
.