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: bytestrieiterator.cpp
|
2010-12-31 18:21:36 +00:00
|
|
|
* encoding: US-ASCII
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* created on: 2010nov03
|
|
|
|
* created by: Markus W. Scherer
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
2011-03-03 21:56:36 +00:00
|
|
|
#include "unicode/bytestrie.h"
|
2010-12-31 18:21:36 +00:00
|
|
|
#include "unicode/stringpiece.h"
|
|
|
|
#include "charstr.h"
|
|
|
|
#include "uvectr32.h"
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2011-01-06 18:40:26 +00:00
|
|
|
BytesTrie::Iterator::Iterator(const void *trieBytes, int32_t maxStringLength,
|
|
|
|
UErrorCode &errorCode)
|
2010-12-31 18:21:36 +00:00
|
|
|
: bytes_(reinterpret_cast<const uint8_t *>(trieBytes)),
|
|
|
|
pos_(bytes_), initialPos_(bytes_),
|
|
|
|
remainingMatchLength_(-1), initialRemainingMatchLength_(-1),
|
2011-01-06 18:40:26 +00:00
|
|
|
str_(NULL), maxLength_(maxStringLength), value_(0), stack_(NULL) {
|
|
|
|
if(U_FAILURE(errorCode)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// str_ and stack_ are pointers so that it's easy to turn bytestrie.h into
|
|
|
|
// a public API header for which we would want it to depend only on
|
|
|
|
// other public headers.
|
|
|
|
// Unlike BytesTrie itself, its Iterator performs memory allocations anyway
|
|
|
|
// via the CharString and UVector32 implementations, so this additional
|
|
|
|
// cost is minimal.
|
|
|
|
str_=new CharString();
|
|
|
|
stack_=new UVector32(errorCode);
|
|
|
|
if(U_SUCCESS(errorCode) && (str_==NULL || stack_==NULL)) {
|
|
|
|
errorCode=U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2010-12-31 18:21:36 +00:00
|
|
|
|
2011-01-06 18:40:26 +00:00
|
|
|
BytesTrie::Iterator::Iterator(const BytesTrie &trie, int32_t maxStringLength,
|
|
|
|
UErrorCode &errorCode)
|
2010-12-31 18:21:36 +00:00
|
|
|
: bytes_(trie.bytes_), pos_(trie.pos_), initialPos_(trie.pos_),
|
|
|
|
remainingMatchLength_(trie.remainingMatchLength_),
|
|
|
|
initialRemainingMatchLength_(trie.remainingMatchLength_),
|
2011-01-06 18:40:26 +00:00
|
|
|
str_(NULL), maxLength_(maxStringLength), value_(0), stack_(NULL) {
|
|
|
|
if(U_FAILURE(errorCode)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
str_=new CharString();
|
|
|
|
stack_=new UVector32(errorCode);
|
|
|
|
if(U_FAILURE(errorCode)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(str_==NULL || stack_==NULL) {
|
|
|
|
errorCode=U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2010-12-31 18:21:36 +00:00
|
|
|
int32_t length=remainingMatchLength_; // Actual remaining match length minus 1.
|
|
|
|
if(length>=0) {
|
2011-01-07 05:28:58 +00:00
|
|
|
// Pending linear-match node, append remaining bytes to str_.
|
2010-12-31 18:21:36 +00:00
|
|
|
++length;
|
|
|
|
if(maxLength_>0 && length>maxLength_) {
|
|
|
|
length=maxLength_; // This will leave remainingMatchLength>=0 as a signal.
|
|
|
|
}
|
2011-01-06 18:40:26 +00:00
|
|
|
str_->append(reinterpret_cast<const char *>(pos_), length, errorCode);
|
2010-12-31 18:21:36 +00:00
|
|
|
pos_+=length;
|
|
|
|
remainingMatchLength_-=length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-06 18:40:26 +00:00
|
|
|
BytesTrie::Iterator::~Iterator() {
|
|
|
|
delete str_;
|
|
|
|
delete stack_;
|
|
|
|
}
|
|
|
|
|
|
|
|
BytesTrie::Iterator &
|
|
|
|
BytesTrie::Iterator::reset() {
|
2010-12-31 18:21:36 +00:00
|
|
|
pos_=initialPos_;
|
|
|
|
remainingMatchLength_=initialRemainingMatchLength_;
|
|
|
|
int32_t length=remainingMatchLength_+1; // Remaining match length.
|
|
|
|
if(maxLength_>0 && length>maxLength_) {
|
|
|
|
length=maxLength_;
|
|
|
|
}
|
2011-01-06 18:40:26 +00:00
|
|
|
str_->truncate(length);
|
2010-12-31 18:21:36 +00:00
|
|
|
pos_+=length;
|
|
|
|
remainingMatchLength_-=length;
|
2011-01-06 18:40:26 +00:00
|
|
|
stack_->setSize(0);
|
2010-12-31 18:21:36 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
2011-01-06 18:40:26 +00:00
|
|
|
BytesTrie::Iterator::hasNext() const { return pos_!=NULL || !stack_->isEmpty(); }
|
|
|
|
|
|
|
|
UBool
|
|
|
|
BytesTrie::Iterator::next(UErrorCode &errorCode) {
|
2010-12-31 18:21:36 +00:00
|
|
|
if(U_FAILURE(errorCode)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
const uint8_t *pos=pos_;
|
|
|
|
if(pos==NULL) {
|
2011-01-06 18:40:26 +00:00
|
|
|
if(stack_->isEmpty()) {
|
2010-12-31 18:21:36 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
// Pop the state off the stack and continue with the next outbound edge of
|
|
|
|
// the branch node.
|
2011-01-06 18:40:26 +00:00
|
|
|
int32_t stackSize=stack_->size();
|
|
|
|
int32_t length=stack_->elementAti(stackSize-1);
|
|
|
|
pos=bytes_+stack_->elementAti(stackSize-2);
|
|
|
|
stack_->setSize(stackSize-2);
|
|
|
|
str_->truncate(length&0xffff);
|
2010-12-31 18:21:36 +00:00
|
|
|
length=(int32_t)((uint32_t)length>>16);
|
|
|
|
if(length>1) {
|
|
|
|
pos=branchNext(pos, length, errorCode);
|
|
|
|
if(pos==NULL) {
|
|
|
|
return TRUE; // Reached a final value.
|
|
|
|
}
|
|
|
|
} else {
|
2011-01-06 18:40:26 +00:00
|
|
|
str_->append((char)*pos++, errorCode);
|
2010-12-31 18:21:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(remainingMatchLength_>=0) {
|
|
|
|
// We only get here if we started in a pending linear-match node
|
|
|
|
// with more than maxLength remaining bytes.
|
|
|
|
return truncateAndStop();
|
|
|
|
}
|
|
|
|
for(;;) {
|
|
|
|
int32_t node=*pos++;
|
2011-01-06 22:25:56 +00:00
|
|
|
if(node>=kMinValueLead) {
|
2010-12-31 18:21:36 +00:00
|
|
|
// Deliver value for the byte sequence so far.
|
2011-01-06 22:25:56 +00:00
|
|
|
UBool isFinal=(UBool)(node&kValueIsFinal);
|
|
|
|
value_=readValue(pos, node>>1);
|
2011-01-06 18:40:26 +00:00
|
|
|
if(isFinal || (maxLength_>0 && str_->length()==maxLength_)) {
|
2010-12-31 18:21:36 +00:00
|
|
|
pos_=NULL;
|
|
|
|
} else {
|
2011-01-06 22:25:56 +00:00
|
|
|
pos_=skipValue(pos, node);
|
2010-12-31 18:21:36 +00:00
|
|
|
}
|
2011-01-06 18:40:26 +00:00
|
|
|
sp_.set(str_->data(), str_->length());
|
2010-12-31 18:21:36 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2011-01-06 18:40:26 +00:00
|
|
|
if(maxLength_>0 && str_->length()==maxLength_) {
|
2010-12-31 18:21:36 +00:00
|
|
|
return truncateAndStop();
|
|
|
|
}
|
2011-01-06 22:25:56 +00:00
|
|
|
if(node<kMinLinearMatch) {
|
2010-12-31 18:21:36 +00:00
|
|
|
if(node==0) {
|
|
|
|
node=*pos++;
|
|
|
|
}
|
|
|
|
pos=branchNext(pos, node+1, errorCode);
|
|
|
|
if(pos==NULL) {
|
|
|
|
return TRUE; // Reached a final value.
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Linear-match node, append length bytes to str_.
|
2011-01-06 22:25:56 +00:00
|
|
|
int32_t length=node-kMinLinearMatch+1;
|
2011-01-06 18:40:26 +00:00
|
|
|
if(maxLength_>0 && str_->length()+length>maxLength_) {
|
|
|
|
str_->append(reinterpret_cast<const char *>(pos),
|
|
|
|
maxLength_-str_->length(), errorCode);
|
2010-12-31 18:21:36 +00:00
|
|
|
return truncateAndStop();
|
|
|
|
}
|
2011-01-06 18:40:26 +00:00
|
|
|
str_->append(reinterpret_cast<const char *>(pos), length, errorCode);
|
2010-12-31 18:21:36 +00:00
|
|
|
pos+=length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-06 18:40:26 +00:00
|
|
|
UBool
|
|
|
|
BytesTrie::Iterator::truncateAndStop() {
|
|
|
|
pos_=NULL;
|
|
|
|
sp_.set(str_->data(), str_->length());
|
2011-01-07 05:28:58 +00:00
|
|
|
value_=-1; // no real value for str
|
2011-01-06 18:40:26 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-12-31 18:21:36 +00:00
|
|
|
// Branch node, needs to take the first outbound edge and push state for the rest.
|
|
|
|
const uint8_t *
|
2011-01-06 18:40:26 +00:00
|
|
|
BytesTrie::Iterator::branchNext(const uint8_t *pos, int32_t length, UErrorCode &errorCode) {
|
2011-01-06 22:25:56 +00:00
|
|
|
while(length>kMaxBranchLinearSubNodeLength) {
|
2010-12-31 18:21:36 +00:00
|
|
|
++pos; // ignore the comparison byte
|
|
|
|
// Push state for the greater-or-equal edge.
|
2011-01-06 22:25:56 +00:00
|
|
|
stack_->addElement((int32_t)(skipDelta(pos)-bytes_), errorCode);
|
2011-01-06 18:40:26 +00:00
|
|
|
stack_->addElement(((length-(length>>1))<<16)|str_->length(), errorCode);
|
2010-12-31 18:21:36 +00:00
|
|
|
// Follow the less-than edge.
|
|
|
|
length>>=1;
|
2011-01-06 22:25:56 +00:00
|
|
|
pos=jumpByDelta(pos);
|
2010-12-31 18:21:36 +00:00
|
|
|
}
|
|
|
|
// List of key-value pairs where values are either final values or jump deltas.
|
|
|
|
// Read the first (key, value) pair.
|
|
|
|
uint8_t trieByte=*pos++;
|
|
|
|
int32_t node=*pos++;
|
2011-01-06 22:25:56 +00:00
|
|
|
UBool isFinal=(UBool)(node&kValueIsFinal);
|
|
|
|
int32_t value=readValue(pos, node>>1);
|
|
|
|
pos=skipValue(pos, node);
|
2011-01-06 18:40:26 +00:00
|
|
|
stack_->addElement((int32_t)(pos-bytes_), errorCode);
|
|
|
|
stack_->addElement(((length-1)<<16)|str_->length(), errorCode);
|
|
|
|
str_->append((char)trieByte, errorCode);
|
2010-12-31 18:21:36 +00:00
|
|
|
if(isFinal) {
|
|
|
|
pos_=NULL;
|
2011-01-06 18:40:26 +00:00
|
|
|
sp_.set(str_->data(), str_->length());
|
2010-12-31 18:21:36 +00:00
|
|
|
value_=value;
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
return pos+value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|