Class RuleCharacterIterator


  • public class RuleCharacterIterator
    extends java.lang.Object
    An iterator that returns 32-bit code points. This class is deliberately not related to any of the JDK or ICU4J character iterator classes in order to minimize complexity.
    Since:
    ICU 2.8
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String buf
      Current variable expansion, or null if none.
      private int bufPos
      Position within buf[].
      static int DONE
      Value returned when there are no more characters to iterate.
      private boolean isEscaped
      Flag indicating whether the last character was parsed from an escape.
      static int PARSE_ESCAPES
      Bitmask option to enable parsing of escape sequences.
      static int PARSE_VARIABLES
      Bitmask option to enable parsing of variable names.
      private java.text.ParsePosition pos
      Position of iterator.
      static int SKIP_WHITESPACE
      Bitmask option to enable skipping of whitespace.
      private SymbolTable sym
      Symbol table used to parse and dereference variables.
      private java.lang.String text
      Text being iterated.
    • Constructor Summary

      Constructors 
      Constructor Description
      RuleCharacterIterator​(java.lang.String text, SymbolTable sym, java.text.ParsePosition pos)
      Constructs an iterator over the given text, starting at the given position.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void _advance​(int count)
      Advances the position by the given amount.
      private int _current()
      Returns the current 32-bit code point without parsing escapes, parsing variables, or skipping whitespace.
      boolean atEnd()
      Returns true if this iterator has no more characters to return.
      java.lang.String getCurrentBuffer()
      Returns a string containing the remainder of the characters to be returned by this iterator, without any option processing.
      int getCurrentBufferPos()  
      RuleCharacterIterator.Position getPos​(RuleCharacterIterator.Position p)
      Returns an object which, when later passed to setPos(), will restore this iterator's position.
      boolean inVariable()
      Returns true if this iterator is currently within a variable expansion.
      boolean isEscaped()
      Returns true if the last character returned by next() was escaped.
      void jumpahead​(int count)
      Advances the position by the given number of 16-bit code units.
      int next​(int options)
      Returns the next character using the given options, or DONE if there are no more characters, and advance the position to the next character.
      void setPos​(RuleCharacterIterator.Position p)
      Restores this iterator to the position it had when getPos() returned the given object.
      void skipIgnored​(int options)
      Skips ahead past any ignored characters, as indicated by the given options.
      java.lang.String toString()
      Returns a string representation of this object, consisting of the characters being iterated, with a '|' marking the current position.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • text

        private java.lang.String text
        Text being iterated.
      • pos

        private java.text.ParsePosition pos
        Position of iterator.
      • sym

        private SymbolTable sym
        Symbol table used to parse and dereference variables. May be null.
      • buf

        private java.lang.String buf
        Current variable expansion, or null if none.
      • bufPos

        private int bufPos
        Position within buf[]. Meaningless if buf == null.
      • isEscaped

        private boolean isEscaped
        Flag indicating whether the last character was parsed from an escape.
      • DONE

        public static final int DONE
        Value returned when there are no more characters to iterate.
        See Also:
        Constant Field Values
      • PARSE_VARIABLES

        public static final int PARSE_VARIABLES
        Bitmask option to enable parsing of variable names. If (options & PARSE_VARIABLES) != 0, then an embedded variable will be expanded to its value. Variables are parsed using the SymbolTable API.
        See Also:
        Constant Field Values
      • PARSE_ESCAPES

        public static final int PARSE_ESCAPES
        Bitmask option to enable parsing of escape sequences. If (options & PARSE_ESCAPES) != 0, then an embedded escape sequence will be expanded to its value. Escapes are parsed using Utility.unescapeAndLengthAt().
        See Also:
        Constant Field Values
      • SKIP_WHITESPACE

        public static final int SKIP_WHITESPACE
        Bitmask option to enable skipping of whitespace. If (options & SKIP_WHITESPACE) != 0, then Unicode Pattern_White_Space characters will be silently skipped, as if they were not present in the input.
        See Also:
        Constant Field Values
    • Constructor Detail

      • RuleCharacterIterator

        public RuleCharacterIterator​(java.lang.String text,
                                     SymbolTable sym,
                                     java.text.ParsePosition pos)
        Constructs an iterator over the given text, starting at the given position.
        Parameters:
        text - the text to be iterated
        sym - the symbol table, or null if there is none. If sym is null, then variables will not be dereferenced, even if the PARSE_VARIABLES option is set.
        pos - upon input, the index of the next character to return. If a variable has been dereferenced, then pos will not increment as characters of the variable value are iterated.
    • Method Detail

      • atEnd

        public boolean atEnd()
        Returns true if this iterator has no more characters to return.
      • next

        public int next​(int options)
        Returns the next character using the given options, or DONE if there are no more characters, and advance the position to the next character.
        Parameters:
        options - one or more of the following options, bitwise-OR-ed together: PARSE_VARIABLES, PARSE_ESCAPES, SKIP_WHITESPACE.
        Returns:
        the current 32-bit code point, or DONE
      • isEscaped

        public boolean isEscaped()
        Returns true if the last character returned by next() was escaped. This will only be the case if the option passed in to next() included PARSE_ESCAPED and the next character was an escape sequence.
      • inVariable

        public boolean inVariable()
        Returns true if this iterator is currently within a variable expansion.
      • getPos

        public RuleCharacterIterator.Position getPos​(RuleCharacterIterator.Position p)
        Returns an object which, when later passed to setPos(), will restore this iterator's position. Usage idiom: RuleCharacterIterator iterator = ...; Position pos = iterator.getPos(null); // allocate position object for (;;) { pos = iterator.getPos(pos); // reuse position object int c = iterator.next(...); ... } iterator.setPos(pos);
        Parameters:
        p - a position object previously returned by getPos(), or null. If not null, it will be updated and returned. If null, a new position object will be allocated and returned.
        Returns:
        a position object which may be passed to setPos(), either p, or if p == null, a newly-allocated object
      • setPos

        public void setPos​(RuleCharacterIterator.Position p)
        Restores this iterator to the position it had when getPos() returned the given object.
        Parameters:
        p - a position object previously returned by getPos()
      • skipIgnored

        public void skipIgnored​(int options)
        Skips ahead past any ignored characters, as indicated by the given options. This is useful in conjunction with the lookahead() method. Currently, this only has an effect for SKIP_WHITESPACE.
        Parameters:
        options - one or more of the following options, bitwise-OR-ed together: PARSE_VARIABLES, PARSE_ESCAPES, SKIP_WHITESPACE.
      • getCurrentBuffer

        public java.lang.String getCurrentBuffer()
        Returns a string containing the remainder of the characters to be returned by this iterator, without any option processing. If the iterator is currently within a variable expansion, this will only extend to the end of the variable expansion. This method, together with getCurrentBufferPos() (which replace the former lookahead()), is provided so that iterators may interoperate with string-based APIs. The typical sequence of calls is to call skipIgnored(), then call these methods, then parse that substring, then call jumpahead() to resynchronize the iterator.
        Returns:
        a string containing the characters to be returned by future calls to next()
      • getCurrentBufferPos

        public int getCurrentBufferPos()
      • jumpahead

        public void jumpahead​(int count)
        Advances the position by the given number of 16-bit code units. This is useful in conjunction with getCurrentBuffer()+getCurrentBufferPos() (formerly lookahead()).
        Parameters:
        count - the number of 16-bit code units to jump over
      • toString

        public java.lang.String toString()
        Returns a string representation of this object, consisting of the characters being iterated, with a '|' marking the current position. Position within an expanded variable is not indicated.
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation of this object
      • _current

        private int _current()
        Returns the current 32-bit code point without parsing escapes, parsing variables, or skipping whitespace.
        Returns:
        the current 32-bit code point
      • _advance

        private void _advance​(int count)
        Advances the position by the given amount.
        Parameters:
        count - the number of 16-bit code units to advance past