]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/rbbidata.cpp
2 ***************************************************************************
3 * Copyright (C) 1999-2003 International Business Machines Corporation *
4 * and others. All rights reserved. *
5 ***************************************************************************
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_BREAK_ITERATION
12 #include "unicode/utypes.h"
24 //-----------------------------------------------------------------------------------
26 // Trie access folding function. Copied as-is from properties code in uchar.c
28 //-----------------------------------------------------------------------------------
30 static int32_t U_CALLCONV
31 getFoldingOffset(uint32_t data
) {
32 /* if bit 15 is set, then the folding offset is in bits 14..0 of the 16-bit trie result */
34 return (int32_t)(data
&0x7fff);
43 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
48 RBBIDataWrapper::RBBIDataWrapper(const RBBIDataHeader
*data
, UErrorCode
&status
) {
52 RBBIDataWrapper::RBBIDataWrapper(UDataMemory
* udm
, UErrorCode
&status
) {
53 const RBBIDataHeader
*d
= (const RBBIDataHeader
*)
54 ((char *)&(udm
->pHeader
->info
) + udm
->pHeader
->info
.size
);
59 //-----------------------------------------------------------------------------
61 // init(). Does most of the work of construction, shared between the
64 //-----------------------------------------------------------------------------
65 void RBBIDataWrapper::init(const RBBIDataHeader
*data
, UErrorCode
&status
) {
66 if (U_FAILURE(status
)) {
70 if (fHeader
->fMagic
!= 0xb1a0) {
71 status
= U_BRK_INTERNAL_ERROR
;
76 fForwardTable
= (RBBIStateTable
*)((char *)data
+ fHeader
->fFTable
);
78 if (data
->fRTableLen
!= 0) {
79 fReverseTable
= (RBBIStateTable
*)((char *)data
+ fHeader
->fRTable
);
83 utrie_unserialize(&fTrie
,
84 (uint8_t *)data
+ fHeader
->fTrie
,
87 if (U_FAILURE(status
)) {
90 fTrie
.getFoldingOffset
=getFoldingOffset
;
93 fRuleSource
= (UChar
*)((char *)data
+ fHeader
->fRuleSource
);
94 fRuleString
.setTo(TRUE
, fRuleSource
, -1);
99 char *debugEnv
= getenv("U_RBBIDEBUG");
100 if (debugEnv
&& uprv_strstr(debugEnv
, "data")) {this->printData();}
105 //-----------------------------------------------------------------------------
107 // Destructor. Don't call this - use removeReferenc() instead.
109 //-----------------------------------------------------------------------------
110 RBBIDataWrapper::~RBBIDataWrapper() {
111 U_ASSERT(fRefCount
== 0);
113 udata_close(fUDataMem
);
115 uprv_free((void *)fHeader
);
121 //-----------------------------------------------------------------------------
123 // Operator == Consider two RBBIDataWrappers to be equal if they
124 // refer to the same underlying data. Although
125 // the data wrappers are normally shared between
126 // iterator instances, it's possible to independently
127 // open the same data twice, and get two instances, which
128 // should still be ==.
130 //-----------------------------------------------------------------------------
131 UBool
RBBIDataWrapper::operator ==(const RBBIDataWrapper
&other
) const {
132 if (fHeader
== other
.fHeader
) {
135 if (fHeader
->fLength
!= other
.fHeader
->fLength
) {
138 if (uprv_memcmp(fHeader
, other
.fHeader
, fHeader
->fLength
) == 0) {
144 int32_t RBBIDataWrapper::hashCode() {
145 return fHeader
->fFTableLen
;
150 //-----------------------------------------------------------------------------
152 // Reference Counting. A single RBBIDataWrapper object is shared among
153 // however many RulesBasedBreakIterator instances are
154 // referencing the same data.
156 //-----------------------------------------------------------------------------
157 void RBBIDataWrapper::removeReference() {
158 if (umtx_atomic_dec(&fRefCount
) == 0) {
164 RBBIDataWrapper
*RBBIDataWrapper::addReference() {
165 umtx_atomic_inc(&fRefCount
);
171 //-----------------------------------------------------------------------------
173 // getRuleSourceString
175 //-----------------------------------------------------------------------------
176 const UnicodeString
&RBBIDataWrapper::getRuleSourceString() {
181 //-----------------------------------------------------------------------------
183 // print - debugging function to dump the runtime data tables.
185 //-----------------------------------------------------------------------------
186 void RBBIDataWrapper::printData() {
190 RBBIDebugPrintf("RBBI Data at %p\n", (void *)fHeader
);
191 RBBIDebugPrintf(" Version = %d\n", fHeader
->fVersion
);
192 RBBIDebugPrintf(" total length of data = %d\n", fHeader
->fLength
);
193 RBBIDebugPrintf(" number of character categories = %d\n\n", fHeader
->fCatCount
);
195 RBBIDebugPrintf(" Forward State Transition Table\n");
196 RBBIDebugPrintf("State | Acc LA Tag");
197 for (c
=0; c
<fHeader
->fCatCount
; c
++) {RBBIDebugPrintf("%3d ", c
);}
198 RBBIDebugPrintf("\n------|---------------"); for (c
=0;c
<fHeader
->fCatCount
; c
++) {RBBIDebugPrintf("----");}
199 RBBIDebugPrintf("\n");
201 for (s
=0; s
<fForwardTable
->fNumStates
; s
++) {
202 RBBIStateTableRow
*row
= (RBBIStateTableRow
*)
203 (fForwardTable
->fTableData
+ (fForwardTable
->fRowLen
* s
));
204 RBBIDebugPrintf("%4d | %3d %3d %3d ", s
, row
->fAccepting
, row
->fLookAhead
, row
->fTag
);
205 for (c
=0; c
<fHeader
->fCatCount
; c
++) {
206 RBBIDebugPrintf("%3d ", row
->fNextState
[c
]);
208 RBBIDebugPrintf("\n");
211 RBBIDebugPrintf("\nOrignal Rules source:\n");
214 if (fRuleSource
[c
] == 0)
216 RBBIDebugPrintf("%c", fRuleSource
[c
]);
219 RBBIDebugPrintf("\n\n");
227 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */