Home | History | Annotate | Download | only in text
      1 /* GENERATED SOURCE. DO NOT MODIFY. */
      2 //  2016 and later: Unicode, Inc. and others.
      3 // License & terms of use: http://www.unicode.org/copyright.html#License
      4 /*
      5  *******************************************************************************
      6  * Copyright (C) 2014, International Business Machines Corporation and         *
      7  * others. All Rights Reserved.                                                *
      8  *******************************************************************************
      9  */
     10 package android.icu.text;
     11 
     12 import java.text.CharacterIterator;
     13 
     14 import android.icu.impl.Assert;
     15 import android.icu.util.BytesTrie;
     16 import android.icu.util.BytesTrie.Result;
     17 
     18 class BytesDictionaryMatcher extends DictionaryMatcher {
     19     private final byte[] characters;
     20     private final int transform;
     21 
     22     public BytesDictionaryMatcher(byte[] chars, int transform) {
     23         characters = chars;
     24         Assert.assrt((transform & DictionaryData.TRANSFORM_TYPE_MASK) == DictionaryData.TRANSFORM_TYPE_OFFSET);
     25         // while there is only one transform type so far, save the entire transform constant so that
     26         // if we add any others, we need only change code in transform() and the assert above rather
     27         // than adding a "transform type" variable
     28         this.transform = transform;
     29     }
     30 
     31     private int transform(int c) {
     32         if (c == 0x200D) {
     33             return 0xFF;
     34         } else if (c == 0x200C) {
     35             return 0xFE;
     36         }
     37 
     38         int delta = c - (transform & DictionaryData.TRANSFORM_OFFSET_MASK);
     39         if (delta < 0 || 0xFD < delta) {
     40             return -1;
     41         }
     42         return delta;
     43     }
     44 
     45     @Override
     46     public int matches(CharacterIterator text_, int maxLength, int[] lengths, int[] count_, int limit, int[] values) {
     47         UCharacterIterator text = UCharacterIterator.getInstance(text_);
     48         BytesTrie bt = new BytesTrie(characters, 0);
     49         int c = text.nextCodePoint();
     50         if (c == UCharacterIterator.DONE) {
     51             return 0;
     52         }
     53         Result result = bt.first(transform(c));
     54         // TODO: should numChars count Character.charCount() ?
     55         int numChars = 1;
     56         int count = 0;
     57         for (;;) {
     58             if (result.hasValue()) {
     59                 if (count < limit) {
     60                     if (values != null) {
     61                         values[count] = bt.getValue();
     62                     }
     63                     lengths[count] = numChars;
     64                     count++;
     65                 }
     66                 if (result == Result.FINAL_VALUE) {
     67                     break;
     68                 }
     69             } else if (result == Result.NO_MATCH) {
     70                 break;
     71             }
     72 
     73             if (numChars >= maxLength) {
     74                 break;
     75             }
     76 
     77             c = text.nextCodePoint();
     78             if (c == UCharacterIterator.DONE) {
     79                 break;
     80             }
     81             ++numChars;
     82             result = bt.next(transform(c));
     83         }
     84         count_[0] = count;
     85         return numChars;
     86     }
     87 
     88     @Override
     89     public int getType() {
     90         return DictionaryData.TRIE_TYPE_BYTES;
     91     }
     92 }
     93 
     94 
     95