12ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller/* GENERATED SOURCE. DO NOT MODIFY. */
22ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller/*
32ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller *******************************************************************************
42ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Copyright (C) 2013, International Business Machines Corporation and         *
52ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * others. All Rights Reserved.                                                *
62ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller *******************************************************************************
72ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */
82ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerpackage android.icu.impl;
92ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport java.text.CharacterIterator;
112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport android.icu.text.UTF16;
132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
141537b2f39245c07b00aa78c3600f7aebcb172490Neil Fuller/**
151537b2f39245c07b00aa78c3600f7aebcb172490Neil Fuller * @hide Only a subset of ICU is exposed in Android
16836e6b40a94ec3fb7545a76cb072960442b7eee9Neil Fuller */
172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerpublic final class CharacterIteration {
182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    // disallow instantiation
192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private CharacterIteration() { }
202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    // 32 bit Char value returned from when an iterator has run out of range.
222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    //     Positive value so fast case (not end, not surrogate) can be checked
232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    //     with a single test.
242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int DONE32 = 0x7fffffff;
252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Move the iterator forward to the next code point, and return that code point,
282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *   leaving the iterator positioned at char returned.
292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *   For Supplementary chars, the iterator is left positioned at the lead surrogate.
302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @param ci  The character iterator
312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @return    The next code point.
322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static int next32(CharacterIterator ci) {
342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // If the current position is at a surrogate pair, move to the trail surrogate
352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        //   which leaves it in position for underlying iterator's next() to work.
362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int c= ci.current();
372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (c >= UTF16.LEAD_SURROGATE_MIN_VALUE && c<=UTF16.LEAD_SURROGATE_MAX_VALUE) {
382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            c = ci.next();
392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (c<UTF16.TRAIL_SURROGATE_MIN_VALUE || c>UTF16.TRAIL_SURROGATE_MAX_VALUE) {
402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller               c = ci.previous();
412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // For BMP chars, this next() is the real deal.
452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        c = ci.next();
462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // If we might have a lead surrogate, we need to peak ahead to get the trail
482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        //  even though we don't want to really be positioned there.
492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (c >= UTF16.LEAD_SURROGATE_MIN_VALUE) {
502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            c = nextTrail32(ci, c);
512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (c >= UTF16.SUPPLEMENTARY_MIN_VALUE && c != DONE32) {
542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            // We got a supplementary char.  Back the iterator up to the postion
552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            // of the lead surrogate.
562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            ci.previous();
572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return c;
592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   }
602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    // Out-of-line portion of the in-line Next32 code.
632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    // The call site does an initial ci.next() and calls this function
642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    //    if the 16 bit value it gets is >= LEAD_SURROGATE_MIN_VALUE.
652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    // NOTE:  we leave the underlying char iterator positioned in the
662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    //        middle of a surrogate pair.  ci.next() will work correctly
672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    //        from there, but the ci.getIndex() will be wrong, and needs
682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    //        adjustment.
692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static int nextTrail32(CharacterIterator ci, int lead) {
702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (lead == CharacterIterator.DONE && ci.getIndex() >= ci.getEndIndex()) {
712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return DONE32;
722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int retVal = lead;
742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (lead <= UTF16.LEAD_SURROGATE_MAX_VALUE) {
752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            char  cTrail = ci.next();
762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (UTF16.isTrailSurrogate(cTrail)) {
772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                retVal = ((lead  - UTF16.LEAD_SURROGATE_MIN_VALUE) << 10) +
782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                            (cTrail - UTF16.TRAIL_SURROGATE_MIN_VALUE) +
792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                            UTF16.SUPPLEMENTARY_MIN_VALUE;
802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            } else {
812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                ci.previous();
822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return retVal;
852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static int previous32(CharacterIterator ci) {
882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (ci.getIndex() <= ci.getBeginIndex()) {
892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return DONE32;
902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        char trail = ci.previous();
922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int retVal = trail;
932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (UTF16.isTrailSurrogate(trail) && ci.getIndex()>ci.getBeginIndex()) {
942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            char lead = ci.previous();
952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (UTF16.isLeadSurrogate(lead)) {
962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                retVal = (((int)lead  - UTF16.LEAD_SURROGATE_MIN_VALUE) << 10) +
972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                          ((int)trail - UTF16.TRAIL_SURROGATE_MIN_VALUE) +
982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                          UTF16.SUPPLEMENTARY_MIN_VALUE;
992ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            } else {
1002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                ci.next();
1012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
1022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
1032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return retVal;
1042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
1052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1062ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static int current32(CharacterIterator ci) {
1072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        char  lead   = ci.current();
1082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int   retVal = lead;
1092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (retVal < UTF16.LEAD_SURROGATE_MIN_VALUE) {
1102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return retVal;
1112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
1122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (UTF16.isLeadSurrogate(lead)) {
1132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            int  trail = (int)ci.next();
1142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            ci.previous();
1152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (UTF16.isTrailSurrogate((char)trail)) {
1162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                retVal = ((lead  - UTF16.LEAD_SURROGATE_MIN_VALUE) << 10) +
1172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                         (trail - UTF16.TRAIL_SURROGATE_MIN_VALUE) +
1182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                         UTF16.SUPPLEMENTARY_MIN_VALUE;
1192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
1202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller         } else {
1212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (lead == CharacterIterator.DONE) {
1222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                if (ci.getIndex() >= ci.getEndIndex())   {
1232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    retVal = DONE32;
1242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                }
1252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
1262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller         }
1272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return retVal;
1282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
1292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller}
130