]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/classes/core/src/com/ibm/icu/text/BytesDictionaryMatcher.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / classes / core / src / com / ibm / icu / text / BytesDictionaryMatcher.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2012, International Business Machines Corporation and         *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.text;
8
9 import java.text.CharacterIterator;
10
11 import com.ibm.icu.impl.Assert;
12 import com.ibm.icu.util.BytesTrie;
13 import com.ibm.icu.util.BytesTrie.Result;
14
15 class BytesDictionaryMatcher extends DictionaryMatcher {
16     private final byte[] characters;
17     private final int transform;
18     
19     public BytesDictionaryMatcher(byte[] chars, int transform) {
20         characters = chars;
21         Assert.assrt((transform & DictionaryData.TRANSFORM_TYPE_MASK) == DictionaryData.TRANSFORM_TYPE_OFFSET);
22         // while there is only one transform type so far, save the entire transform constant so that
23         // if we add any others, we need only change code in transform() and the assert above rather
24         // than adding a "transform type" variable
25         this.transform = transform;
26     }
27     
28     private int transform(int c) {
29         if (c == 0x200D) { 
30             return 0xFF;
31         } else if (c == 0x200C) {
32             return 0xFE;
33         }
34
35         int delta = c - (transform & DictionaryData.TRANSFORM_OFFSET_MASK);
36         if (delta < 0 || 0xFD < delta) {
37             return -1;
38         }
39         return delta;
40     }
41
42     public int matches(CharacterIterator text_, int maxLength, int[] lengths, int[] count_, int limit, int[] values) {
43         UCharacterIterator text = UCharacterIterator.getInstance(text_);
44         BytesTrie bt = new BytesTrie(characters, 0);
45         int c = text.nextCodePoint();
46         Result result = bt.first(transform(c));
47         // TODO: should numChars count Character.charCount() ?
48         int numChars = 1;
49         int count = 0;
50         for (;;) {
51             if (result.hasValue()) {
52                 if (count < limit) {
53                     if (values != null) {
54                         values[count] = bt.getValue();
55                     }
56                     lengths[count] = numChars;
57                     count++;
58                 }
59                 if (result == Result.FINAL_VALUE) {
60                     break;
61                 }
62             } else if (result == Result.NO_MATCH) {
63                 break;
64             }
65
66             if (numChars >= maxLength) {
67                 break;
68             }
69
70             c = text.nextCodePoint();
71             ++numChars;
72             result = bt.next(transform(c));
73         }
74         count_[0] = count;
75         return numChars;
76     }
77
78     public int getType() {
79         return DictionaryData.TRIE_TYPE_BYTES;
80     }
81 }
82
83