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) 2014, International Business Machines Corporation and         *
72ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * others. All Rights Reserved.                                                *
82ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller *******************************************************************************
92ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */
102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerpackage android.icu.text;
112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport java.text.CharacterIterator;
132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport android.icu.impl.Assert;
152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport android.icu.util.BytesTrie;
162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport android.icu.util.BytesTrie.Result;
172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerclass BytesDictionaryMatcher extends DictionaryMatcher {
192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private final byte[] characters;
202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private final int transform;
21f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert
222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public BytesDictionaryMatcher(byte[] chars, int transform) {
232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        characters = chars;
242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        Assert.assrt((transform & DictionaryData.TRANSFORM_TYPE_MASK) == DictionaryData.TRANSFORM_TYPE_OFFSET);
252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // while there is only one transform type so far, save the entire transform constant so that
262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // if we add any others, we need only change code in transform() and the assert above rather
272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // than adding a "transform type" variable
282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        this.transform = transform;
292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
30f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert
312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private int transform(int c) {
32f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert        if (c == 0x200D) {
332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return 0xFF;
342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        } else if (c == 0x200C) {
352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return 0xFE;
362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int delta = c - (transform & DictionaryData.TRANSFORM_OFFSET_MASK);
392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (delta < 0 || 0xFD < delta) {
402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return -1;
412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return delta;
432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
45f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert    @Override
462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public int matches(CharacterIterator text_, int maxLength, int[] lengths, int[] count_, int limit, int[] values) {
472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        UCharacterIterator text = UCharacterIterator.getInstance(text_);
482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        BytesTrie bt = new BytesTrie(characters, 0);
492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int c = text.nextCodePoint();
502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (c == UCharacterIterator.DONE) {
512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return 0;
522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        Result result = bt.first(transform(c));
542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // TODO: should numChars count Character.charCount() ?
552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int numChars = 1;
562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int count = 0;
572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        for (;;) {
582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (result.hasValue()) {
592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                if (count < limit) {
602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    if (values != null) {
612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        values[count] = bt.getValue();
622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    }
632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    lengths[count] = numChars;
642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    count++;
652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                }
662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                if (result == Result.FINAL_VALUE) {
672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    break;
682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                }
692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            } else if (result == Result.NO_MATCH) {
702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                break;
712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (numChars >= maxLength) {
742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                break;
752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            c = text.nextCodePoint();
782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (c == UCharacterIterator.DONE) {
792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                break;
802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            ++numChars;
822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            result = bt.next(transform(c));
832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        count_[0] = count;
852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return numChars;
862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
88f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert    @Override
892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public int getType() {
902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return DictionaryData.TRIE_TYPE_BYTES;
912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller}
932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
95