com.ibm.icu.impl.breakiter.CharsDictionaryMatcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of icu4j Show documentation
Show all versions of icu4j Show documentation
International Component for Unicode for Java (ICU4J) is a mature, widely used Java library
providing Unicode and Globalization support
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
* Copyright (C) 2014, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
package com.ibm.icu.impl.breakiter;
import java.text.CharacterIterator;
import com.ibm.icu.text.UCharacterIterator;
import com.ibm.icu.util.BytesTrie.Result;
import com.ibm.icu.util.CharsTrie;
class CharsDictionaryMatcher extends DictionaryMatcher {
private CharSequence characters;
public CharsDictionaryMatcher(CharSequence chars) {
characters = chars;
}
@Override
public int matches(CharacterIterator text_, int maxLength, int[] lengths, int[] count_, int limit, int[] values) {
UCharacterIterator text = UCharacterIterator.getInstance(text_);
CharsTrie uct = new CharsTrie(characters, 0);
int c = text.nextCodePoint();
if (c == UCharacterIterator.DONE) {
return 0;
}
Result result = uct.firstForCodePoint(c);
// TODO: should numChars count Character.charCount?
int numChars = 1;
int count = 0;
for (;;) {
if (result.hasValue()) {
if (count < limit) {
if (values != null) {
values[count] = uct.getValue();
}
lengths[count] = numChars;
count++;
}
if (result == Result.FINAL_VALUE) {
break;
}
} else if (result == Result.NO_MATCH) {
break;
}
if (numChars >= maxLength) {
break;
}
c = text.nextCodePoint();
if (c == UCharacterIterator.DONE) {
break;
}
++numChars;
result = uct.nextForCodePoint(c);
}
count_[0] = count;
return numChars;
}
@Override
public int getType() {
return DictionaryData.TRIE_TYPE_UCHARS;
}
}