2010-12-31 18:21:36 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
2011-01-05 21:05:47 +00:00
|
|
|
* Copyright (C) 2010-2011, International Business Machines
|
2010-12-31 18:21:36 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*******************************************************************************
|
2011-01-05 21:05:47 +00:00
|
|
|
* file name: ucharstrie.h
|
2010-12-31 18:21:36 +00:00
|
|
|
* encoding: US-ASCII
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* created on: 2010nov14
|
|
|
|
* created by: Markus W. Scherer
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
2011-02-03 05:28:01 +00:00
|
|
|
#include "unicode/appendable.h"
|
2011-03-03 21:56:36 +00:00
|
|
|
#include "unicode/ucharstrie.h"
|
2010-12-31 18:21:36 +00:00
|
|
|
#include "unicode/uobject.h"
|
2011-07-27 05:53:56 +00:00
|
|
|
#include "unicode/utf16.h"
|
2011-01-27 21:41:03 +00:00
|
|
|
#include "cmemory.h"
|
2010-12-31 18:21:36 +00:00
|
|
|
#include "uassert.h"
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2011-01-27 21:41:03 +00:00
|
|
|
UCharsTrie::~UCharsTrie() {
|
|
|
|
uprv_free(ownedArray_);
|
|
|
|
}
|
|
|
|
|
2011-01-05 21:05:47 +00:00
|
|
|
UStringTrieResult
|
|
|
|
UCharsTrie::current() const {
|
2010-12-31 18:21:36 +00:00
|
|
|
const UChar *pos=pos_;
|
|
|
|
if(pos==NULL) {
|
2011-01-05 21:05:47 +00:00
|
|
|
return USTRINGTRIE_NO_MATCH;
|
2010-12-31 18:21:36 +00:00
|
|
|
} else {
|
|
|
|
int32_t node;
|
|
|
|
return (remainingMatchLength_<0 && (node=*pos)>=kMinValueLead) ?
|
2011-01-05 21:05:47 +00:00
|
|
|
valueResult(node) : USTRINGTRIE_NO_VALUE;
|
2010-12-31 18:21:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 05:53:56 +00:00
|
|
|
UStringTrieResult
|
|
|
|
UCharsTrie::firstForCodePoint(UChar32 cp) {
|
|
|
|
return cp<=0xffff ?
|
|
|
|
first(cp) :
|
|
|
|
(USTRINGTRIE_HAS_NEXT(first(U16_LEAD(cp))) ?
|
|
|
|
next(U16_TRAIL(cp)) :
|
|
|
|
USTRINGTRIE_NO_MATCH);
|
|
|
|
}
|
|
|
|
|
|
|
|
UStringTrieResult
|
|
|
|
UCharsTrie::nextForCodePoint(UChar32 cp) {
|
|
|
|
return cp<=0xffff ?
|
|
|
|
next(cp) :
|
|
|
|
(USTRINGTRIE_HAS_NEXT(next(U16_LEAD(cp))) ?
|
|
|
|
next(U16_TRAIL(cp)) :
|
|
|
|
USTRINGTRIE_NO_MATCH);
|
|
|
|
}
|
|
|
|
|
2011-01-05 21:05:47 +00:00
|
|
|
UStringTrieResult
|
|
|
|
UCharsTrie::branchNext(const UChar *pos, int32_t length, int32_t uchar) {
|
2010-12-31 18:21:36 +00:00
|
|
|
// Branch according to the current unit.
|
|
|
|
if(length==0) {
|
|
|
|
length=*pos++;
|
|
|
|
}
|
|
|
|
++length;
|
|
|
|
// The length of the branch is the number of units to select from.
|
|
|
|
// The data structure encodes a binary search.
|
|
|
|
while(length>kMaxBranchLinearSubNodeLength) {
|
|
|
|
if(uchar<*pos++) {
|
|
|
|
length>>=1;
|
|
|
|
pos=jumpByDelta(pos);
|
|
|
|
} else {
|
|
|
|
length=length-(length>>1);
|
|
|
|
pos=skipDelta(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Drop down to linear search for the last few units.
|
|
|
|
// length>=2 because the loop body above sees length>kMaxBranchLinearSubNodeLength>=3
|
|
|
|
// and divides length by 2.
|
|
|
|
do {
|
|
|
|
if(uchar==*pos++) {
|
2011-01-05 21:05:47 +00:00
|
|
|
UStringTrieResult result;
|
2010-12-31 18:21:36 +00:00
|
|
|
int32_t node=*pos;
|
|
|
|
if(node&kValueIsFinal) {
|
|
|
|
// Leave the final value for getValue() to read.
|
2011-01-05 21:05:47 +00:00
|
|
|
result=USTRINGTRIE_FINAL_VALUE;
|
2010-12-31 18:21:36 +00:00
|
|
|
} else {
|
|
|
|
// Use the non-final value as the jump delta.
|
|
|
|
++pos;
|
2011-01-07 05:28:58 +00:00
|
|
|
// int32_t delta=readValue(pos, node);
|
2010-12-31 18:21:36 +00:00
|
|
|
int32_t delta;
|
|
|
|
if(node<kMinTwoUnitValueLead) {
|
|
|
|
delta=node;
|
|
|
|
} else if(node<kThreeUnitValueLead) {
|
|
|
|
delta=((node-kMinTwoUnitValueLead)<<16)|*pos++;
|
|
|
|
} else {
|
|
|
|
delta=(pos[0]<<16)|pos[1];
|
|
|
|
pos+=2;
|
|
|
|
}
|
|
|
|
// end readValue()
|
|
|
|
pos+=delta;
|
|
|
|
node=*pos;
|
2011-01-05 21:05:47 +00:00
|
|
|
result= node>=kMinValueLead ? valueResult(node) : USTRINGTRIE_NO_VALUE;
|
2010-12-31 18:21:36 +00:00
|
|
|
}
|
|
|
|
pos_=pos;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
--length;
|
|
|
|
pos=skipValue(pos);
|
|
|
|
} while(length>1);
|
|
|
|
if(uchar==*pos++) {
|
|
|
|
pos_=pos;
|
|
|
|
int32_t node=*pos;
|
2011-01-05 21:05:47 +00:00
|
|
|
return node>=kMinValueLead ? valueResult(node) : USTRINGTRIE_NO_VALUE;
|
2010-12-31 18:21:36 +00:00
|
|
|
} else {
|
|
|
|
stop();
|
2011-01-05 21:05:47 +00:00
|
|
|
return USTRINGTRIE_NO_MATCH;
|
2010-12-31 18:21:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-05 21:05:47 +00:00
|
|
|
UStringTrieResult
|
|
|
|
UCharsTrie::nextImpl(const UChar *pos, int32_t uchar) {
|
2010-12-31 18:21:36 +00:00
|
|
|
int32_t node=*pos++;
|
|
|
|
for(;;) {
|
|
|
|
if(node<kMinLinearMatch) {
|
|
|
|
return branchNext(pos, node, uchar);
|
|
|
|
} else if(node<kMinValueLead) {
|
|
|
|
// Match the first of length+1 units.
|
|
|
|
int32_t length=node-kMinLinearMatch; // Actual match length minus 1.
|
|
|
|
if(uchar==*pos++) {
|
|
|
|
remainingMatchLength_=--length;
|
|
|
|
pos_=pos;
|
|
|
|
return (length<0 && (node=*pos)>=kMinValueLead) ?
|
2011-01-05 21:05:47 +00:00
|
|
|
valueResult(node) : USTRINGTRIE_NO_VALUE;
|
2010-12-31 18:21:36 +00:00
|
|
|
} else {
|
|
|
|
// No match.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if(node&kValueIsFinal) {
|
|
|
|
// No further matching units.
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// Skip intermediate value.
|
|
|
|
pos=skipNodeValue(pos, node);
|
|
|
|
node&=kNodeTypeMask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stop();
|
2011-01-05 21:05:47 +00:00
|
|
|
return USTRINGTRIE_NO_MATCH;
|
2010-12-31 18:21:36 +00:00
|
|
|
}
|
|
|
|
|
2011-01-05 21:05:47 +00:00
|
|
|
UStringTrieResult
|
|
|
|
UCharsTrie::next(int32_t uchar) {
|
2010-12-31 18:21:36 +00:00
|
|
|
const UChar *pos=pos_;
|
|
|
|
if(pos==NULL) {
|
2011-01-05 21:05:47 +00:00
|
|
|
return USTRINGTRIE_NO_MATCH;
|
2010-12-31 18:21:36 +00:00
|
|
|
}
|
|
|
|
int32_t length=remainingMatchLength_; // Actual remaining match length minus 1.
|
|
|
|
if(length>=0) {
|
|
|
|
// Remaining part of a linear-match node.
|
|
|
|
if(uchar==*pos++) {
|
|
|
|
remainingMatchLength_=--length;
|
|
|
|
pos_=pos;
|
|
|
|
int32_t node;
|
|
|
|
return (length<0 && (node=*pos)>=kMinValueLead) ?
|
2011-01-05 21:05:47 +00:00
|
|
|
valueResult(node) : USTRINGTRIE_NO_VALUE;
|
2010-12-31 18:21:36 +00:00
|
|
|
} else {
|
|
|
|
stop();
|
2011-01-05 21:05:47 +00:00
|
|
|
return USTRINGTRIE_NO_MATCH;
|
2010-12-31 18:21:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nextImpl(pos, uchar);
|
|
|
|
}
|
|
|
|
|
2011-01-05 21:05:47 +00:00
|
|
|
UStringTrieResult
|
|
|
|
UCharsTrie::next(const UChar *s, int32_t sLength) {
|
2010-12-31 18:21:36 +00:00
|
|
|
if(sLength<0 ? *s==0 : sLength==0) {
|
|
|
|
// Empty input.
|
|
|
|
return current();
|
|
|
|
}
|
|
|
|
const UChar *pos=pos_;
|
|
|
|
if(pos==NULL) {
|
2011-01-05 21:05:47 +00:00
|
|
|
return USTRINGTRIE_NO_MATCH;
|
2010-12-31 18:21:36 +00:00
|
|
|
}
|
|
|
|
int32_t length=remainingMatchLength_; // Actual remaining match length minus 1.
|
|
|
|
for(;;) {
|
|
|
|
// Fetch the next input unit, if there is one.
|
|
|
|
// Continue a linear-match node without rechecking sLength<0.
|
|
|
|
int32_t uchar;
|
|
|
|
if(sLength<0) {
|
|
|
|
for(;;) {
|
|
|
|
if((uchar=*s++)==0) {
|
|
|
|
remainingMatchLength_=length;
|
|
|
|
pos_=pos;
|
|
|
|
int32_t node;
|
|
|
|
return (length<0 && (node=*pos)>=kMinValueLead) ?
|
2011-01-05 21:05:47 +00:00
|
|
|
valueResult(node) : USTRINGTRIE_NO_VALUE;
|
2010-12-31 18:21:36 +00:00
|
|
|
}
|
|
|
|
if(length<0) {
|
|
|
|
remainingMatchLength_=length;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(uchar!=*pos) {
|
|
|
|
stop();
|
2011-01-05 21:05:47 +00:00
|
|
|
return USTRINGTRIE_NO_MATCH;
|
2010-12-31 18:21:36 +00:00
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
--length;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(;;) {
|
|
|
|
if(sLength==0) {
|
|
|
|
remainingMatchLength_=length;
|
|
|
|
pos_=pos;
|
|
|
|
int32_t node;
|
|
|
|
return (length<0 && (node=*pos)>=kMinValueLead) ?
|
2011-01-05 21:05:47 +00:00
|
|
|
valueResult(node) : USTRINGTRIE_NO_VALUE;
|
2010-12-31 18:21:36 +00:00
|
|
|
}
|
|
|
|
uchar=*s++;
|
|
|
|
--sLength;
|
|
|
|
if(length<0) {
|
|
|
|
remainingMatchLength_=length;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(uchar!=*pos) {
|
|
|
|
stop();
|
2011-01-05 21:05:47 +00:00
|
|
|
return USTRINGTRIE_NO_MATCH;
|
2010-12-31 18:21:36 +00:00
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
--length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int32_t node=*pos++;
|
|
|
|
for(;;) {
|
|
|
|
if(node<kMinLinearMatch) {
|
2011-01-05 21:05:47 +00:00
|
|
|
UStringTrieResult result=branchNext(pos, node, uchar);
|
|
|
|
if(result==USTRINGTRIE_NO_MATCH) {
|
|
|
|
return USTRINGTRIE_NO_MATCH;
|
2010-12-31 18:21:36 +00:00
|
|
|
}
|
|
|
|
// Fetch the next input unit, if there is one.
|
|
|
|
if(sLength<0) {
|
|
|
|
if((uchar=*s++)==0) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(sLength==0) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
uchar=*s++;
|
|
|
|
--sLength;
|
|
|
|
}
|
2011-01-05 21:05:47 +00:00
|
|
|
if(result==USTRINGTRIE_FINAL_VALUE) {
|
2010-12-31 18:21:36 +00:00
|
|
|
// No further matching units.
|
|
|
|
stop();
|
2011-01-05 21:05:47 +00:00
|
|
|
return USTRINGTRIE_NO_MATCH;
|
2010-12-31 18:21:36 +00:00
|
|
|
}
|
|
|
|
pos=pos_; // branchNext() advanced pos and wrote it to pos_ .
|
|
|
|
node=*pos++;
|
|
|
|
} else if(node<kMinValueLead) {
|
|
|
|
// Match length+1 units.
|
|
|
|
length=node-kMinLinearMatch; // Actual match length minus 1.
|
|
|
|
if(uchar!=*pos) {
|
|
|
|
stop();
|
2011-01-05 21:05:47 +00:00
|
|
|
return USTRINGTRIE_NO_MATCH;
|
2010-12-31 18:21:36 +00:00
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
--length;
|
|
|
|
break;
|
|
|
|
} else if(node&kValueIsFinal) {
|
|
|
|
// No further matching units.
|
|
|
|
stop();
|
2011-01-05 21:05:47 +00:00
|
|
|
return USTRINGTRIE_NO_MATCH;
|
2010-12-31 18:21:36 +00:00
|
|
|
} else {
|
|
|
|
// Skip intermediate value.
|
|
|
|
pos=skipNodeValue(pos, node);
|
|
|
|
node&=kNodeTypeMask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const UChar *
|
2011-01-05 21:05:47 +00:00
|
|
|
UCharsTrie::findUniqueValueFromBranch(const UChar *pos, int32_t length,
|
|
|
|
UBool haveUniqueValue, int32_t &uniqueValue) {
|
2010-12-31 18:21:36 +00:00
|
|
|
while(length>kMaxBranchLinearSubNodeLength) {
|
2011-01-07 05:28:58 +00:00
|
|
|
++pos; // ignore the comparison unit
|
2010-12-31 18:21:36 +00:00
|
|
|
if(NULL==findUniqueValueFromBranch(jumpByDelta(pos), length>>1, haveUniqueValue, uniqueValue)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
length=length-(length>>1);
|
|
|
|
pos=skipDelta(pos);
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
++pos; // ignore a comparison unit
|
|
|
|
// handle its value
|
|
|
|
int32_t node=*pos++;
|
|
|
|
UBool isFinal=(UBool)(node>>15);
|
|
|
|
node&=0x7fff;
|
|
|
|
int32_t value=readValue(pos, node);
|
|
|
|
pos=skipValue(pos, node);
|
|
|
|
if(isFinal) {
|
|
|
|
if(haveUniqueValue) {
|
|
|
|
if(value!=uniqueValue) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uniqueValue=value;
|
|
|
|
haveUniqueValue=TRUE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(!findUniqueValue(pos+value, haveUniqueValue, uniqueValue)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
haveUniqueValue=TRUE;
|
|
|
|
}
|
|
|
|
} while(--length>1);
|
|
|
|
return pos+1; // ignore the last comparison unit
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
2011-01-05 21:05:47 +00:00
|
|
|
UCharsTrie::findUniqueValue(const UChar *pos, UBool haveUniqueValue, int32_t &uniqueValue) {
|
2010-12-31 18:21:36 +00:00
|
|
|
int32_t node=*pos++;
|
|
|
|
for(;;) {
|
|
|
|
if(node<kMinLinearMatch) {
|
|
|
|
if(node==0) {
|
|
|
|
node=*pos++;
|
|
|
|
}
|
|
|
|
pos=findUniqueValueFromBranch(pos, node+1, haveUniqueValue, uniqueValue);
|
|
|
|
if(pos==NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
haveUniqueValue=TRUE;
|
|
|
|
node=*pos++;
|
|
|
|
} else if(node<kMinValueLead) {
|
|
|
|
// linear-match node
|
|
|
|
pos+=node-kMinLinearMatch+1; // Ignore the match units.
|
|
|
|
node=*pos++;
|
|
|
|
} else {
|
|
|
|
UBool isFinal=(UBool)(node>>15);
|
|
|
|
int32_t value;
|
|
|
|
if(isFinal) {
|
|
|
|
value=readValue(pos, node&0x7fff);
|
|
|
|
} else {
|
|
|
|
value=readNodeValue(pos, node);
|
|
|
|
}
|
|
|
|
if(haveUniqueValue) {
|
|
|
|
if(value!=uniqueValue) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uniqueValue=value;
|
|
|
|
haveUniqueValue=TRUE;
|
|
|
|
}
|
|
|
|
if(isFinal) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
pos=skipNodeValue(pos, node);
|
|
|
|
node&=kNodeTypeMask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
2011-01-05 21:05:47 +00:00
|
|
|
UCharsTrie::getNextUChars(Appendable &out) const {
|
2010-12-31 18:21:36 +00:00
|
|
|
const UChar *pos=pos_;
|
|
|
|
if(pos==NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(remainingMatchLength_>=0) {
|
2011-02-03 05:28:01 +00:00
|
|
|
out.appendCodeUnit(*pos); // Next unit of a pending linear-match node.
|
2010-12-31 18:21:36 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int32_t node=*pos++;
|
|
|
|
if(node>=kMinValueLead) {
|
|
|
|
if(node&kValueIsFinal) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
pos=skipNodeValue(pos, node);
|
|
|
|
node&=kNodeTypeMask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(node<kMinLinearMatch) {
|
|
|
|
if(node==0) {
|
|
|
|
node=*pos++;
|
|
|
|
}
|
2011-02-03 05:28:01 +00:00
|
|
|
out.reserveAppendCapacity(++node);
|
|
|
|
getNextBranchUChars(pos, node, out);
|
2010-12-31 18:21:36 +00:00
|
|
|
return node;
|
|
|
|
} else {
|
|
|
|
// First unit of the linear-match node.
|
2011-02-03 05:28:01 +00:00
|
|
|
out.appendCodeUnit(*pos);
|
2010-12-31 18:21:36 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-01-05 21:05:47 +00:00
|
|
|
UCharsTrie::getNextBranchUChars(const UChar *pos, int32_t length, Appendable &out) {
|
2010-12-31 18:21:36 +00:00
|
|
|
while(length>kMaxBranchLinearSubNodeLength) {
|
|
|
|
++pos; // ignore the comparison unit
|
|
|
|
getNextBranchUChars(jumpByDelta(pos), length>>1, out);
|
|
|
|
length=length-(length>>1);
|
|
|
|
pos=skipDelta(pos);
|
|
|
|
}
|
|
|
|
do {
|
2011-02-03 05:28:01 +00:00
|
|
|
out.appendCodeUnit(*pos++);
|
2010-12-31 18:21:36 +00:00
|
|
|
pos=skipValue(pos);
|
|
|
|
} while(--length>1);
|
2011-02-03 05:28:01 +00:00
|
|
|
out.appendCodeUnit(*pos);
|
2010-12-31 18:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|