Package com.oracle.truffle.api.strings
Class TruffleStringIterator
java.lang.Object
com.oracle.truffle.api.strings.TruffleStringIterator
An iterator object that allows iterating over a
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));
}
}
}
- Since:
- 22.1
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Returns the next codepoint in the string.static class
Returns the previous codepoint in the string. -
Method Summary
Modifier and TypeMethodDescriptionint
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()
Returnstrue
if there are more codepoints remaining.boolean
Returnstrue
if there are more codepoints remaining in reverse direction.int
Shorthand for calling the uncached version ofTruffleStringIterator.NextNode
.int
Shorthand for calling the uncached version ofTruffleStringIterator.PreviousNode
.
-
Method Details
-
hasNext
public boolean hasNext()Returnstrue
if there are more codepoints remaining.- Since:
- 22.1
-
hasPrevious
public boolean hasPrevious()Returnstrue
if there are more codepoints remaining in reverse direction.- Since:
- 22.1
-
getByteIndex
public 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.- Since:
- 22.3
-
nextUncached
public int nextUncached()Shorthand for calling the uncached version ofTruffleStringIterator.NextNode
.- Since:
- 22.1
-
previousUncached
public int previousUncached()Shorthand for calling the uncached version ofTruffleStringIterator.PreviousNode
.- Since:
- 22.1
-