12ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller/* GENERATED SOURCE. DO NOT MODIFY. */
2f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert// © 2016 and later: Unicode, Inc. and others.
3f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert// License & terms of use: http://www.unicode.org/copyright.html#License
42ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller/*
52ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller**********************************************************************
62ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller* Copyright (c) 2003-2011, International Business Machines
72ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller* Corporation and others.  All Rights Reserved.
82ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller**********************************************************************
92ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller* Author: Alan Liu
102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller* Created: September 23 2003
112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller* Since: ICU 2.8
122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller**********************************************************************
132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller*/
142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerpackage android.icu.impl;
152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport java.text.ParsePosition;
172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport android.icu.text.SymbolTable;
192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport android.icu.text.UTF16;
202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller/**
222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * An iterator that returns 32-bit code points.  This class is deliberately
232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * <em>not</em> related to any of the JDK or ICU4J character iterator classes
242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * in order to minimize complexity.
252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @author Alan Liu
26836e6b40a94ec3fb7545a76cb072960442b7eee9Neil Fuller * @hide Only a subset of ICU is exposed in Android
272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */
282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerpublic class RuleCharacterIterator {
292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    // TODO: Ideas for later.  (Do not implement if not needed, lest the
312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    // code coverage numbers go down due to unused methods.)
322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    // 1. Add a copy constructor, equals() method, clone() method.
332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    // 2. Rather than return DONE, throw an exception if the end
342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    // is reached -- this is an alternate usage model, probably not useful.
352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    // 3. Return isEscaped from next().  If this happens,
362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    // don't keep an isEscaped member variable.
372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Text being iterated.
40f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert     */
412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private String text;
422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Position of iterator.
452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private ParsePosition pos;
472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Symbol table used to parse and dereference variables.  May be null.
502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private SymbolTable sym;
522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Current variable expansion, or null if none.
552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private char[] buf;
572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Position within buf[].  Meaningless if buf == null.
602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private int bufPos;
622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Flag indicating whether the last character was parsed from an escape.
652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private boolean isEscaped;
672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Value returned when there are no more characters to iterate.
702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int DONE = -1;
722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Bitmask option to enable parsing of variable names.  If (options &
752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * PARSE_VARIABLES) != 0, then an embedded variable will be expanded to
762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * its value.  Variables are parsed using the SymbolTable API.
772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int PARSE_VARIABLES = 1;
792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Bitmask option to enable parsing of escape sequences.  If (options &
822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * PARSE_ESCAPES) != 0, then an embedded escape sequence will be expanded
832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * to its value.  Escapes are parsed using Utility.unescapeAt().
842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
85f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert    public static final int PARSE_ESCAPES   = 2;
862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Bitmask option to enable skipping of whitespace.  If (options &
892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * SKIP_WHITESPACE) != 0, then Unicode Pattern_White_Space characters will be silently
902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * skipped, as if they were not present in the input.
912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int SKIP_WHITESPACE = 4;
932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Constructs an iterator over the given text, starting at the given
962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * position.
972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @param text the text to be iterated
982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @param sym the symbol table, or null if there is none.  If sym is null,
992ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * then variables will not be deferenced, even if the PARSE_VARIABLES
1002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * option is set.
1012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @param pos upon input, the index of the next character to return.  If a
1022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * variable has been dereferenced, then pos will <em>not</em> increment as
1032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * characters of the variable value are iterated.
1042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
1052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public RuleCharacterIterator(String text, SymbolTable sym,
1062ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                                 ParsePosition pos) {
1072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (text == null || pos.getIndex() > text.length()) {
1082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            throw new IllegalArgumentException();
1092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
1102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        this.text = text;
1112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        this.sym = sym;
1122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        this.pos = pos;
1132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        buf = null;
1142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
115f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert
1162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
1172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Returns true if this iterator has no more characters to return.
1182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
1192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public boolean atEnd() {
1202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return buf == null && pos.getIndex() == text.length();
1212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
1222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
1242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Returns the next character using the given options, or DONE if there
1252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * are no more characters, and advance the position to the next
1262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * character.
1272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @param options one or more of the following options, bitwise-OR-ed
1282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * together: PARSE_VARIABLES, PARSE_ESCAPES, SKIP_WHITESPACE.
1292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @return the current 32-bit code point, or DONE
1302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
1312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public int next(int options) {
1322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int c = DONE;
1332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        isEscaped = false;
1342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        for (;;) {
1362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            c = _current();
1372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            _advance(UTF16.getCharCount(c));
1382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (c == SymbolTable.SYMBOL_REF && buf == null &&
1402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                (options & PARSE_VARIABLES) != 0 && sym != null) {
1412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                String name = sym.parseReference(text, pos, text.length());
1422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // If name == null there was an isolated SYMBOL_REF;
1432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // return it.  Caller must be prepared for this.
1442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                if (name == null) {
1452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    break;
1462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                }
1472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                bufPos = 0;
1482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                buf = sym.lookup(name);
1492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                if (buf == null) {
1502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    throw new IllegalArgumentException(
1512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                                "Undefined variable: " + name);
1522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                }
1532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // Handle empty variable value
1542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                if (buf.length == 0) {
1552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    buf = null;
1562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                }
1572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                continue;
1582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
1592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if ((options & SKIP_WHITESPACE) != 0 &&
1612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                PatternProps.isWhiteSpace(c)) {
1622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                continue;
1632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
1642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (c == '\\' && (options & PARSE_ESCAPES) != 0) {
1662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                int offset[] = new int[] { 0 };
1672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                c = Utility.unescapeAt(lookahead(), offset);
1682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                jumpahead(offset[0]);
1692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                isEscaped = true;
1702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                if (c < 0) {
1712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    throw new IllegalArgumentException("Invalid escape");
1722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                }
1732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
1742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            break;
1762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
1772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return c;
1792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
1802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
1822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Returns true if the last character returned by next() was
1832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * escaped.  This will only be the case if the option passed in to
1842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * next() included PARSE_ESCAPED and the next character was an
1852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * escape sequence.
1862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
1872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public boolean isEscaped() {
1882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return isEscaped;
1892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
1902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
1922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Returns true if this iterator is currently within a variable expansion.
1932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
1942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public boolean inVariable() {
1952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return buf != null;
1962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
1972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
1992ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Returns an object which, when later passed to setPos(), will
2002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * restore this iterator's position.  Usage idiom:
2012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *
2022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * RuleCharacterIterator iterator = ...;
2032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Object pos = iterator.getPos(null); // allocate position object
2042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * for (;;) {
2052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *   pos = iterator.getPos(pos); // reuse position object
2062ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *   int c = iterator.next(...);
2072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *   ...
2082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * }
2092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * iterator.setPos(pos);
2102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *
2112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @param p a position object previously returned by getPos(),
2122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * or null.  If not null, it will be updated and returned.  If
2132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * null, a new position object will be allocated and returned.
2142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @return a position object which may be passed to setPos(),
2152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * either `p,' or if `p' == null, a newly-allocated object
2162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
2172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public Object getPos(Object p) {
2182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (p == null) {
2192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return new Object[] {buf, new int[] {pos.getIndex(), bufPos}};
2202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
2212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        Object[] a = (Object[]) p;
2222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        a[0] = buf;
2232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int[] v = (int[]) a[1];
2242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        v[0] = pos.getIndex();
2252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        v[1] = bufPos;
2262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return p;
2272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
2282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
2302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Restores this iterator to the position it had when getPos()
2312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * returned the given object.
2322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @param p a position object previously returned by getPos()
2332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
2342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public void setPos(Object p) {
2352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        Object[] a = (Object[]) p;
2362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        buf = (char[]) a[0];
2372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int[] v = (int[]) a[1];
2382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        pos.setIndex(v[0]);
2392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        bufPos = v[1];
2402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
2412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
2432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Skips ahead past any ignored characters, as indicated by the given
2442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * options.  This is useful in conjunction with the lookahead() method.
2452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *
2462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Currently, this only has an effect for SKIP_WHITESPACE.
2472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @param options one or more of the following options, bitwise-OR-ed
2482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * together: PARSE_VARIABLES, PARSE_ESCAPES, SKIP_WHITESPACE.
2492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
2502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public void skipIgnored(int options) {
2512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if ((options & SKIP_WHITESPACE) != 0) {
2522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            for (;;) {
2532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                int a = _current();
2542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                if (!PatternProps.isWhiteSpace(a)) break;
2552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                _advance(UTF16.getCharCount(a));
2562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
2572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
2582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
2592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
2612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Returns a string containing the remainder of the characters to be
2622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * returned by this iterator, without any option processing.  If the
2632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * iterator is currently within a variable expansion, this will only
2642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * extend to the end of the variable expansion.  This method is provided
2652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * so that iterators may interoperate with string-based APIs.  The typical
2662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * sequence of calls is to call skipIgnored(), then call lookahead(), then
2672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * parse the string returned by lookahead(), then call jumpahead() to
2682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * resynchronize the iterator.
2692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @return a string containing the characters to be returned by future
2702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * calls to next()
2712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
2722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public String lookahead() {
2732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (buf != null) {
2742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return new String(buf, bufPos, buf.length - bufPos);
2752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        } else {
2762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return text.substring(pos.getIndex());
2772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
2782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
2792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
2812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Advances the position by the given number of 16-bit code units.
2822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * This is useful in conjunction with the lookahead() method.
2832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @param count the number of 16-bit code units to jump over
2842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
2852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public void jumpahead(int count) {
2862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (count < 0) {
2872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            throw new IllegalArgumentException();
2882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
2892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (buf != null) {
2902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            bufPos += count;
2912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (bufPos > buf.length) {
2922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                throw new IllegalArgumentException();
2932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
2942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (bufPos == buf.length) {
2952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                buf = null;
2962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
2972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        } else {
2982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            int i = pos.getIndex() + count;
2992ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            pos.setIndex(i);
3002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (i > text.length()) {
3012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                throw new IllegalArgumentException();
3022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
3032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
3042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
3052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
3062ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
3072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Returns a string representation of this object, consisting of the
3082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * characters being iterated, with a '|' marking the current position.
3092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Position within an expanded variable is <em>not</em> indicated.
3102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @return a string representation of this object
3112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
312f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert    @Override
3132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public String toString() {
3142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int b = pos.getIndex();
3152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return text.substring(0, b) + '|' + text.substring(b);
3162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
3172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
3182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
3192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Returns the current 32-bit code point without parsing escapes, parsing
3202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * variables, or skipping whitespace.
3212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @return the current 32-bit code point
3222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
3232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private int _current() {
3242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (buf != null) {
3252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return UTF16.charAt(buf, 0, buf.length, bufPos);
3262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        } else {
3272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            int i = pos.getIndex();
3282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return (i < text.length()) ? UTF16.charAt(text, i) : DONE;
3292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
3302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
331f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert
3322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
3332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Advances the position by the given amount.
3342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @param count the number of 16-bit code units to advance past
3352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
3362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private void _advance(int count) {
3372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (buf != null) {
3382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            bufPos += count;
3392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (bufPos == buf.length) {
3402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                buf = null;
3412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
3422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        } else {
3432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            pos.setIndex(pos.getIndex() + count);
3442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (pos.getIndex() > text.length()) {
3452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                pos.setIndex(text.length());
3462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
3472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
3482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
3492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller}