2002-06-25 17:23:07 +00:00
|
|
|
/*
|
2002-08-08 00:39:13 +00:00
|
|
|
***************************************************************************
|
2002-06-25 17:23:07 +00:00
|
|
|
* Copyright (C) 1999-2002 International Business Machines Corporation *
|
2002-08-08 00:39:13 +00:00
|
|
|
* and others. All rights reserved. *
|
|
|
|
***************************************************************************
|
2002-06-25 17:23:07 +00:00
|
|
|
*/
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "rbbidata.h"
|
2002-08-01 16:17:41 +00:00
|
|
|
#include "rbbirb.h"
|
2002-06-25 17:23:07 +00:00
|
|
|
#include "utrie.h"
|
|
|
|
#include "udatamem.h"
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "cmemory.h"
|
2002-08-01 16:17:41 +00:00
|
|
|
#include "cstring.h"
|
2002-08-01 17:41:06 +00:00
|
|
|
#include "umutex.h"
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2002-07-22 23:24:55 +00:00
|
|
|
#include "uassert.h"
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
2002-08-01 16:17:41 +00:00
|
|
|
// Constructors.
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
RBBIDataWrapper::RBBIDataWrapper(const RBBIDataHeader *data, UErrorCode &status) {
|
|
|
|
init(data, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
RBBIDataWrapper::RBBIDataWrapper(UDataMemory* udm, UErrorCode &status) {
|
|
|
|
const RBBIDataHeader *d = (const RBBIDataHeader *)
|
|
|
|
((char *)&(udm->pHeader->info) + udm->pHeader->info.size);
|
|
|
|
init(d, status);
|
|
|
|
fUDataMem = udm;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Trie access folding function. Copied as-is from properties code in uchar.c
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------------
|
2002-06-27 23:49:17 +00:00
|
|
|
U_CDECL_BEGIN
|
2002-06-25 17:23:07 +00:00
|
|
|
static int32_t U_CALLCONV
|
|
|
|
getFoldingOffset(uint32_t data) {
|
|
|
|
/* if bit 15 is set, then the folding offset is in bits 14..0 of the 16-bit trie result */
|
|
|
|
if(data&0x8000) {
|
|
|
|
return (int32_t)(data&0x7fff);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2002-06-27 23:49:17 +00:00
|
|
|
U_CDECL_END
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// init(). Does most of the work of construction, shared between the
|
2002-08-01 16:17:41 +00:00
|
|
|
// constructors.
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void RBBIDataWrapper::init(const RBBIDataHeader *data, UErrorCode &status) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fHeader = data;
|
|
|
|
if (fHeader->fMagic != 0xb1a0) {
|
|
|
|
status = U_BRK_INTERNAL_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fUDataMem = NULL;
|
|
|
|
fForwardTable = (RBBIStateTable *)((char *)data + fHeader->fFTable);
|
|
|
|
fReverseTable = NULL;
|
|
|
|
if (data->fRTableLen != 0) {
|
|
|
|
fReverseTable = (RBBIStateTable *)((char *)data + fHeader->fRTable);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
utrie_unserialize(&fTrie,
|
|
|
|
(uint8_t *)data + fHeader->fTrie,
|
|
|
|
fHeader->fTrieLen,
|
|
|
|
&status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fTrie.getFoldingOffset=getFoldingOffset;
|
|
|
|
|
|
|
|
|
|
|
|
fRuleSource = (UChar *)((char *)data + fHeader->fRuleSource);
|
|
|
|
fRuleString.setTo(TRUE, fRuleSource, -1);
|
|
|
|
|
2002-08-01 16:17:41 +00:00
|
|
|
fRefCount = 1;
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2002-08-01 16:17:41 +00:00
|
|
|
#ifdef RBBI_DEBUG
|
|
|
|
char *debugEnv = getenv("U_RBBIDEBUG");
|
|
|
|
if (debugEnv && uprv_strstr(debugEnv, "data")) {this->printData();}
|
|
|
|
#endif
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Destructor. Don't call this - use removeReferenc() instead.
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
RBBIDataWrapper::~RBBIDataWrapper() {
|
2002-07-22 23:24:55 +00:00
|
|
|
U_ASSERT(fRefCount == 0);
|
2002-06-25 17:23:07 +00:00
|
|
|
if (fUDataMem) {
|
|
|
|
udata_close(fUDataMem);
|
|
|
|
} else {
|
|
|
|
uprv_free((void *)fHeader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-01 16:17:41 +00:00
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Operator == Consider two RBBIDataWrappers to be equal if they
|
|
|
|
// refer to the same underlying data. Although
|
|
|
|
// the data wrappers are normally shared between
|
|
|
|
// iterator instances, it's possible to independently
|
|
|
|
// open the same data twice, and get two instances, which
|
|
|
|
// should still be ==.
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
UBool RBBIDataWrapper::operator ==(const RBBIDataWrapper &other) const {
|
|
|
|
if (fHeader == other.fHeader) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (fHeader->fLength != other.fHeader->fLength) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (uprv_memcmp(fHeader, other.fHeader, fHeader->fLength) == 0) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t RBBIDataWrapper::hashCode() {
|
|
|
|
return fHeader->fFTableLen;
|
2002-11-30 04:41:53 +00:00
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Reference Counting. A single RBBIDataWrapper object is shared among
|
|
|
|
// however many RulesBasedBreakIterator instances are
|
|
|
|
// referencing the same data.
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void RBBIDataWrapper::removeReference() {
|
2002-08-08 00:39:13 +00:00
|
|
|
if (umtx_atomic_dec(&fRefCount) == 0) {
|
2002-06-25 17:23:07 +00:00
|
|
|
delete this;
|
|
|
|
}
|
2002-11-30 04:41:53 +00:00
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
RBBIDataWrapper *RBBIDataWrapper::addReference() {
|
2002-08-01 17:41:06 +00:00
|
|
|
umtx_atomic_inc(&fRefCount);
|
2002-06-25 17:23:07 +00:00
|
|
|
return this;
|
2002-11-30 04:41:53 +00:00
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// getRuleSourceString
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
const UnicodeString &RBBIDataWrapper::getRuleSourceString() {
|
|
|
|
return fRuleString;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// print - debugging function to dump the runtime data tables.
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void RBBIDataWrapper::printData() {
|
|
|
|
uint32_t c, s;
|
|
|
|
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("RBBI Data at %p\n", (void *)fHeader);
|
|
|
|
RBBIDebugPrintf(" Version = %d\n", fHeader->fVersion);
|
|
|
|
RBBIDebugPrintf(" total length of data = %d\n", fHeader->fLength);
|
|
|
|
RBBIDebugPrintf(" number of character categories = %d\n\n", fHeader->fCatCount);
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf(" Forward State Transition Table\n");
|
|
|
|
RBBIDebugPrintf("State | Acc LA Tag");
|
2002-11-30 04:41:53 +00:00
|
|
|
for (c=0; c<fHeader->fCatCount; c++) {RBBIDebugPrintf("%3d ", c);}
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("\n------|---------------"); for (c=0;c<fHeader->fCatCount; c++) {RBBIDebugPrintf("----");}
|
|
|
|
RBBIDebugPrintf("\n");
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
for (s=0; s<fForwardTable->fNumStates; s++) {
|
|
|
|
RBBIStateTableRow *row = (RBBIStateTableRow *)
|
|
|
|
(fForwardTable->fTableData + (fForwardTable->fRowLen * s));
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("%4d | %3d %3d %3d ", s, row->fAccepting, row->fLookAhead, row->fTag);
|
2002-06-25 17:23:07 +00:00
|
|
|
for (c=0; c<fHeader->fCatCount; c++) {
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("%3d ", row->fNextState[c]);
|
2002-11-30 04:41:53 +00:00
|
|
|
}
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("\n");
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
|
|
|
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("\nOrignal Rules source:\n");
|
2002-06-25 17:23:07 +00:00
|
|
|
c = 0;
|
|
|
|
for (;;) {
|
|
|
|
if (fRuleSource[c] == 0)
|
|
|
|
break;
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("%c", fRuleSource[c]);
|
2002-06-25 17:23:07 +00:00
|
|
|
c++;
|
|
|
|
}
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("\n\n");
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
2002-09-20 01:54:48 +00:00
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_BREAK_ITERATION */
|