1/*
2 *******************************************************************************
3 * Copyright (C) 2014, International Business Machines Corporation and         *
4 * others. All Rights Reserved.                                                *
5 *******************************************************************************
6 */
7package com.ibm.icu.text;
8
9import java.text.CharacterIterator;
10
11import com.ibm.icu.util.BytesTrie.Result;
12import com.ibm.icu.util.CharsTrie;
13
14class CharsDictionaryMatcher extends DictionaryMatcher {
15    private CharSequence characters;
16
17    public CharsDictionaryMatcher(CharSequence chars) {
18        characters = chars;
19    }
20
21    public int matches(CharacterIterator text_, int maxLength, int[] lengths, int[] count_, int limit, int[] values) {
22        UCharacterIterator text = UCharacterIterator.getInstance(text_);
23        CharsTrie uct = new CharsTrie(characters, 0);
24        int c = text.nextCodePoint();
25        if (c == UCharacterIterator.DONE) {
26            return 0;
27        }
28        Result result = uct.firstForCodePoint(c);
29        // TODO: should numChars count Character.charCount?
30        int numChars = 1;
31        int count = 0;
32        for (;;) {
33            if (result.hasValue()) {
34                if (count < limit) {
35                    if (values != null) {
36                        values[count] = uct.getValue();
37                    }
38                    lengths[count] = numChars;
39                    count++;
40                }
41
42                if (result == Result.FINAL_VALUE) {
43                    break;
44                }
45            } else if (result == Result.NO_MATCH) {
46                break;
47            }
48
49            if (numChars >= maxLength) {
50                break;
51            }
52            c = text.nextCodePoint();
53            if (c == UCharacterIterator.DONE) {
54                break;
55            }
56            ++numChars;
57            result = uct.nextForCodePoint(c);
58        }
59        count_[0] = count;
60        return numChars;
61    }
62
63    public int getType() {
64        return DictionaryData.TRIE_TYPE_UCHARS;
65    }
66}
67
68