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