X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/57a6839dcb3bba09e8228b822b290604668416fe..ef6cf650f4a75c3f97de06b51fa104f2069b9ea2:/icuSources/common/dictionarydata.cpp?ds=sidebyside diff --git a/icuSources/common/dictionarydata.cpp b/icuSources/common/dictionarydata.cpp index f174f9d4..c0e6662d 100644 --- a/icuSources/common/dictionarydata.cpp +++ b/icuSources/common/dictionarydata.cpp @@ -1,6 +1,6 @@ /* ******************************************************************************* -* Copyright (C) 2014, International Business Machines +* Copyright (C) 2014-2016, International Business Machines * Corporation and others. All Rights Reserved. ******************************************************************************* * dictionarydata.h @@ -40,22 +40,31 @@ int32_t UCharsDictionaryMatcher::getType() const { return DictionaryData::TRIE_TYPE_UCHARS; } -int32_t UCharsDictionaryMatcher::matches(UText *text, int32_t maxLength, int32_t *lengths, int32_t &count, int32_t limit, int32_t *values) const { +int32_t UCharsDictionaryMatcher::matches(UText *text, int32_t maxLength, int32_t limit, + int32_t *lengths, int32_t *cpLengths, int32_t *values, + int32_t *prefix) const { + UCharsTrie uct(characters); - UChar32 c = utext_next32(text); - if (c < 0) { - return 0; - } - UStringTrieResult result = uct.first(c); - int32_t numChars = 1; - count = 0; - for (;;) { + int32_t startingTextIndex = (int32_t)utext_getNativeIndex(text); + int32_t wordCount = 0; + int32_t codePointsMatched = 0; + + for (UChar32 c = utext_next32(text); c >= 0; c=utext_next32(text)) { + UStringTrieResult result = (codePointsMatched == 0) ? uct.first(c) : uct.next(c); + int32_t lengthMatched = (int32_t)utext_getNativeIndex(text) - startingTextIndex; + codePointsMatched += 1; if (USTRINGTRIE_HAS_VALUE(result)) { - if (count < limit) { + if (wordCount < limit) { if (values != NULL) { - values[count] = uct.getValue(); + values[wordCount] = uct.getValue(); + } + if (lengths != NULL) { + lengths[wordCount] = lengthMatched; + } + if (cpLengths != NULL) { + cpLengths[wordCount] = codePointsMatched; } - lengths[count++] = numChars; + ++wordCount; } if (result == USTRINGTRIE_FINAL_VALUE) { break; @@ -64,20 +73,15 @@ int32_t UCharsDictionaryMatcher::matches(UText *text, int32_t maxLength, int32_t else if (result == USTRINGTRIE_NO_MATCH) { break; } - - // TODO: why do we have a text limit if the UText knows its length? - if (numChars >= maxLength) { + if (lengthMatched >= maxLength) { break; } + } - c = utext_next32(text); - if (c < 0) { - break; - } - ++numChars; - result = uct.next(c); + if (prefix != NULL) { + *prefix = codePointsMatched; } - return numChars; + return wordCount; } BytesDictionaryMatcher::~BytesDictionaryMatcher() { @@ -104,22 +108,30 @@ int32_t BytesDictionaryMatcher::getType() const { return DictionaryData::TRIE_TYPE_BYTES; } -int32_t BytesDictionaryMatcher::matches(UText *text, int32_t maxLength, int32_t *lengths, int32_t &count, int32_t limit, int32_t *values) const { +int32_t BytesDictionaryMatcher::matches(UText *text, int32_t maxLength, int32_t limit, + int32_t *lengths, int32_t *cpLengths, int32_t *values, + int32_t *prefix) const { BytesTrie bt(characters); - UChar32 c = utext_next32(text); - if (c < 0) { - return 0; - } - UStringTrieResult result = bt.first(transform(c)); - int32_t numChars = 1; - count = 0; - for (;;) { + int32_t startingTextIndex = (int32_t)utext_getNativeIndex(text); + int32_t wordCount = 0; + int32_t codePointsMatched = 0; + + for (UChar32 c = utext_next32(text); c >= 0; c=utext_next32(text)) { + UStringTrieResult result = (codePointsMatched == 0) ? bt.first(transform(c)) : bt.next(transform(c)); + int32_t lengthMatched = (int32_t)utext_getNativeIndex(text) - startingTextIndex; + codePointsMatched += 1; if (USTRINGTRIE_HAS_VALUE(result)) { - if (count < limit) { + if (wordCount < limit) { if (values != NULL) { - values[count] = bt.getValue(); + values[wordCount] = bt.getValue(); + } + if (lengths != NULL) { + lengths[wordCount] = lengthMatched; + } + if (cpLengths != NULL) { + cpLengths[wordCount] = codePointsMatched; } - lengths[count++] = numChars; + ++wordCount; } if (result == USTRINGTRIE_FINAL_VALUE) { break; @@ -128,20 +140,15 @@ int32_t BytesDictionaryMatcher::matches(UText *text, int32_t maxLength, int32_t else if (result == USTRINGTRIE_NO_MATCH) { break; } - - // TODO: why do we have a text limit if the UText knows its length? - if (numChars >= maxLength) { + if (lengthMatched >= maxLength) { break; } + } - c = utext_next32(text); - if (c < 0) { - break; - } - ++numChars; - result = bt.next(transform(c)); + if (prefix != NULL) { + *prefix = codePointsMatched; } - return numChars; + return wordCount; }