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) 2009-2014, International Business Machines Corporation and
72ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * others. All Rights Reserved.
82ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller *******************************************************************************
92ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */
102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerpackage android.icu.impl;
122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport java.io.DataOutputStream;
142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport java.io.IOException;
152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport java.io.OutputStream;
162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport java.nio.ByteBuffer;
172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller/**
202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @author aheninger
212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller *
222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * A read-only Trie2, holding 16 bit data values.
232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller *
242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * A Trie2 is a highly optimized data structure for mapping from Unicode
252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * code points (values ranging from 0 to 0x10ffff) to a 16 or 32 bit value.
262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller *
272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * See class Trie2 for descriptions of the API for accessing the contents of a trie.
282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller *
292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * The fundamental data access methods are declared final in this class, with
302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * the intent that applications might gain a little extra performance, when compared
312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * with calling the same methods via the abstract UTrie2 base class.
32836e6b40a94ec3fb7545a76cb072960442b7eee9Neil Fuller * @hide Only a subset of ICU is exposed in Android
332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */
342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerpublic final class Trie2_16 extends Trie2 {
352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *  Internal constructor, not for general use.
392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    Trie2_16() {
412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Create a Trie2 from its serialized form.  Inverse of utrie2_serialize().
462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * The serialized format is identical between ICU4C and ICU4J, so this function
472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * will work with serialized Trie2s from either.
482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *
492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * The serialized Trie2 in the bytes may be in either little or big endian byte order.
502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * This allows using serialized Tries from ICU4C without needing to consider the
512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * byte order of the system that created them.
522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *
532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @param bytes a byte buffer to the serialized form of a UTrie2.
542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @return An unserialized Trie2_16, ready for use.
552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @throws IllegalArgumentException if the buffer does not contain a serialized Trie2.
562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @throws IOException if a read error occurs in the buffer.
572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @throws ClassCastException if the bytes contain a serialized Trie2_32
582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static Trie2_16  createFromSerialized(ByteBuffer bytes) throws IOException {
602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return (Trie2_16) Trie2.createFromSerialized(bytes);
612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Get the value for a code point as stored in the Trie2.
652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *
662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @param codePoint the code point
672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @return the value
682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    @Override
702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public final int get(int codePoint) {
712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int value;
722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int ix;
732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (codePoint >= 0) {
752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (codePoint < 0x0d800 || (codePoint > 0x0dbff && codePoint <= 0x0ffff)) {
762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // Ordinary BMP code point, excluding leading surrogates.
772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // BMP uses a single level lookup.  BMP index starts at offset 0 in the Trie2 index.
782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // 16 bit data is stored in the index array itself.
792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                ix = index[codePoint >> UTRIE2_SHIFT_2];
802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                ix = (ix << UTRIE2_INDEX_SHIFT) + (codePoint & UTRIE2_DATA_MASK);
812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                value = index[ix];
822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                return value;
832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (codePoint <= 0xffff) {
852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // Lead Surrogate Code Point.  A Separate index section is stored for
862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // lead surrogate code units and code points.
872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                //   The main index has the code unit data.
882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                //   For this function, we need the code point data.
892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // Note: this expression could be refactored for slightly improved efficiency, but
902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                //       surrogate code points will be so rare in practice that it's not worth it.
912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                ix = index[UTRIE2_LSCP_INDEX_2_OFFSET + ((codePoint - 0xd800) >> UTRIE2_SHIFT_2)];
922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                ix = (ix << UTRIE2_INDEX_SHIFT) + (codePoint & UTRIE2_DATA_MASK);
932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                value = index[ix];
942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                return value;
952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (codePoint < highStart) {
972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // Supplemental code point, use two-level lookup.
982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                ix = (UTRIE2_INDEX_1_OFFSET - UTRIE2_OMITTED_BMP_INDEX_1_LENGTH) + (codePoint >> UTRIE2_SHIFT_1);
992ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                ix = index[ix];
1002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                ix += (codePoint >> UTRIE2_SHIFT_2) & UTRIE2_INDEX_2_MASK;
1012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                ix = index[ix];
1022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                ix = (ix << UTRIE2_INDEX_SHIFT) + (codePoint & UTRIE2_DATA_MASK);
1032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                value = index[ix];
1042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                return value;
1052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
1062ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (codePoint <= 0x10ffff) {
1072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                value = index[highValueIndex];
1082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                return value;
1092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
1102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
1112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // Fall through.  The code point is outside of the legal range of 0..0x10ffff.
1132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return errorValue;
1142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
1152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
1182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Get a Trie2 value for a UTF-16 code unit.
1192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *
1202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * This function returns the same value as get() if the input
1212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * character is outside of the lead surrogate range
1222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *
1232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * There are two values stored in a Trie2 for inputs in the lead
1242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * surrogate range.  This function returns the alternate value,
1252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * while Trie2.get() returns the main value.
1262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *
1272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @param codeUnit a 16 bit code unit or lead surrogate value.
1282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @return the value
1292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
1302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    @Override
1312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public int getFromU16SingleLead(char codeUnit) {
1322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int value;
1332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int ix;
1342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // Because the input is a 16 bit char, we can skip the tests for it being in
1362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // the BMP range.  It is.
1372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        ix = index[codeUnit >> UTRIE2_SHIFT_2];
1382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        ix = (ix << UTRIE2_INDEX_SHIFT) + (codeUnit & UTRIE2_DATA_MASK);
1392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        value = index[ix];
1402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return value;
1412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
1422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
1452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Serialize a Trie2_16 onto an OutputStream.
1462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *
1472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * A Trie2 can be serialized multiple times.
1482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * The serialized data is compatible with ICU4C UTrie2 serialization.
1492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Trie2 serialization is unrelated to Java object serialization.
1502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *
1512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @param os the stream to which the serialized Trie2 data will be written.
1522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @return the number of bytes written.
1532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @throw IOException on an error writing to the OutputStream.
1542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
1552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public int serialize(OutputStream os) throws IOException {
1562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        DataOutputStream dos = new DataOutputStream(os);
1572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int  bytesWritten = 0;
1582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        bytesWritten += serializeHeader(dos);
1602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        for (int i=0; i<dataLength; i++) {
1612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            dos.writeChar(index[data16+i]);
1622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
1632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        bytesWritten += dataLength*2;
1642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return bytesWritten;
1652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
1662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
1682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @return the number of bytes of the serialized trie
1692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
1702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public int getSerializedLength() {
1712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return 16+(header.indexLength+dataLength)*2;
1722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
1732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
1752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Given a starting code point, find the last in a range of code points,
1762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * all with the same value.
1772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *
1782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * This function is part of the implementation of iterating over the
1792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Trie2's contents.
1802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @param startingCP The code point at which to begin looking.
1812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @return The last code point with the same value as the starting code point.
1822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
1832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    @Override
1842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    int rangeEnd(int startingCP, int limit, int value) {
1852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int   cp = startingCP;
1862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int   block = 0;
1872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int   index2Block = 0;
1882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // Loop runs once for each of
1902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        //   - a partial data block
1912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        //   - a reference to the null (default) data block.
1922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        //   - a reference to the index2 null block
1932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller      outerLoop:
1952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        for (;;) {
1962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (cp >= limit) {
1972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                break;
1982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
1992ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (cp < 0x0d800 || (cp > 0x0dbff && cp <= 0x0ffff)) {
2002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // Ordinary BMP code point, excluding leading surrogates.
2012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // BMP uses a single level lookup.  BMP index starts at offset 0 in the Trie2 index.
2022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // 16 bit data is stored in the index array itself.
2032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                index2Block = 0;
2042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                block       = index[cp >> UTRIE2_SHIFT_2] << UTRIE2_INDEX_SHIFT;
2052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            } else if (cp < 0xffff) {
2062ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // Lead Surrogate Code Point, 0xd800 <= cp < 0xdc00
2072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                index2Block = UTRIE2_LSCP_INDEX_2_OFFSET;
2082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                block       = index[index2Block + ((cp - 0xd800) >> UTRIE2_SHIFT_2)] << UTRIE2_INDEX_SHIFT;
2092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            } else if (cp < highStart) {
2102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // Supplemental code point, use two-level lookup.
2112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                int ix = (UTRIE2_INDEX_1_OFFSET - UTRIE2_OMITTED_BMP_INDEX_1_LENGTH) + (cp >> UTRIE2_SHIFT_1);
2122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                index2Block = index[ix];
2132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                block = index[index2Block + ((cp >> UTRIE2_SHIFT_2) & UTRIE2_INDEX_2_MASK)] << UTRIE2_INDEX_SHIFT;
2142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            } else  {
2152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // Code point above highStart.
2162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                if (value == index[highValueIndex]) {
2172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    cp = limit;
2182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                }
2192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                break;
2202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
2212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (index2Block == index2NullOffset) {
2232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                if (value != initialValue) {
2242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    break;
2252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                }
2262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                cp += UTRIE2_CP_PER_INDEX_1_ENTRY;
2272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            } else if (block == dataNullOffset) {
2282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // The block at dataNullOffset has all values == initialValue.
2292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // Because Trie2 iteration always proceeds in ascending order, we will always
2302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                //   encounter a null block at its beginning, and can skip over
2312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                //   a number of code points equal to the length of the block.
2322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                if (value != initialValue) {
2332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    break;
2342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                }
2352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                cp += UTRIE2_DATA_BLOCK_LENGTH;
2362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            } else {
2372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // Current position refers to an ordinary data block.
2382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // Walk over the data entries, checking the values.
2392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                int startIx = block + (cp & UTRIE2_DATA_MASK);
2402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                int limitIx = block + UTRIE2_DATA_BLOCK_LENGTH;
2412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                for (int ix = startIx; ix<limitIx; ix++) {
2422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    if (index[ix] != value) {
2432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        // We came to an entry with a different value.
2442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        //   We are done.
2452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        cp += (ix - startIx);
2462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        break outerLoop;
2472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    }
2482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                }
2492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // The ordinary data block contained our value until its end.
2502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                //  Advance the current code point, and continue the outerloop.
2512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                cp += limitIx - startIx;
2522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
2532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
2542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (cp > limit) {
2552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            cp = limit;
2562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
2572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return cp - 1;
2592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
2602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller}
261