Class TruffleStringIterator

java.lang.Object
com.oracle.truffle.api.strings.TruffleStringIterator

public final class TruffleStringIterator extends Object
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
  • Method Details

    • hasNext

      public boolean hasNext()
      Returns true if there are more codepoints remaining.
      Since:
      22.1
    • hasPrevious

      public boolean hasPrevious()
      Returns true 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 of TruffleStringIterator.NextNode.
      Since:
      22.1
    • previousUncached

      public int previousUncached()
      Shorthand for calling the uncached version of TruffleStringIterator.PreviousNode.
      Since:
      22.1