]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/rbbidata57.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ***************************************************************************
5 * Copyright (C) 1999-2014 International Business Machines Corporation *
6 * and others. All rights reserved. *
7 ***************************************************************************
10 #include "unicode/utypes.h"
12 #if !UCONFIG_NO_BREAK_ITERATION
14 #include "unicode/utypes.h"
15 #include "rbbidata57.h"
26 //-----------------------------------------------------------------------------------
28 // Trie access folding function. Copied as-is from properties code in uchar.c
30 //-----------------------------------------------------------------------------------
32 static int32_t U_CALLCONV
33 getFoldingOffset(uint32_t data
) {
34 /* if bit 15 is set, then the folding offset is in bits 14..0 of the 16-bit trie result */
36 return (int32_t)(data
&0x7fff);
45 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
50 RBBIDataWrapper57::RBBIDataWrapper57(const RBBIDataHeader57
*data
, UErrorCode
&status
) {
55 RBBIDataWrapper57::RBBIDataWrapper57(const RBBIDataHeader57
*data
, enum EDontAdopt
, UErrorCode
&status
) {
61 RBBIDataWrapper57::RBBIDataWrapper57(UDataMemory
* udm
, UErrorCode
&status
) {
63 if (U_FAILURE(status
)) {
66 const DataHeader
*dh
= udm
->pHeader
;
67 int32_t headerSize
= dh
->dataHeader
.headerSize
;
68 if ( !(headerSize
>= 20 &&
69 dh
->info
.isBigEndian
== U_IS_BIG_ENDIAN
&&
70 dh
->info
.charsetFamily
== U_CHARSET_FAMILY
&&
71 dh
->info
.dataFormat
[0] == 0x42 && // dataFormat="Brk "
72 dh
->info
.dataFormat
[1] == 0x72 &&
73 dh
->info
.dataFormat
[2] == 0x6b &&
74 dh
->info
.dataFormat
[3] == 0x20)
75 // Note: info.fFormatVersion is duplicated in the RBBIDataHeader57, and is
76 // validated when checking that.
78 status
= U_INVALID_FORMAT_ERROR
;
81 const char *dataAsBytes
= reinterpret_cast<const char *>(dh
);
82 const RBBIDataHeader57
*rbbidh
= reinterpret_cast<const RBBIDataHeader57
*>(dataAsBytes
+ headerSize
);
87 //-----------------------------------------------------------------------------
89 // init(). Does most of the work of construction, shared between the
92 //-----------------------------------------------------------------------------
93 void RBBIDataWrapper57::init0() {
100 fRuleStatusTable
= NULL
;
103 fDontFreeData
= TRUE
;
106 void RBBIDataWrapper57::init(const RBBIDataHeader57
*data
, UErrorCode
&status
) {
107 if (U_FAILURE(status
)) {
111 if (fHeader
->fMagic
!= 0xb1a0 || fHeader
->fFormatVersion
[0] != 3)
113 status
= U_INVALID_FORMAT_ERROR
;
116 // Note: in ICU version 3.2 and earlier, there was a formatVersion 1
117 // that is no longer supported. At that time fFormatVersion was
118 // an int32_t field, rather than an array of 4 bytes.
120 fDontFreeData
= FALSE
;
121 if (data
->fFTableLen
!= 0) {
122 fForwardTable
= (RBBIStateTable
*)((char *)data
+ fHeader
->fFTable
);
124 if (data
->fRTableLen
!= 0) {
125 fReverseTable
= (RBBIStateTable
*)((char *)data
+ fHeader
->fRTable
);
127 if (data
->fSFTableLen
!= 0) {
128 fSafeFwdTable
= (RBBIStateTable
*)((char *)data
+ fHeader
->fSFTable
);
130 if (data
->fSRTableLen
!= 0) {
131 fSafeRevTable
= (RBBIStateTable
*)((char *)data
+ fHeader
->fSRTable
);
135 utrie_unserialize(&fTrie
,
136 (uint8_t *)data
+ fHeader
->fTrie
,
139 if (U_FAILURE(status
)) {
142 fTrie
.getFoldingOffset
=getFoldingOffset
;
145 fRuleSource
= (UChar
*)((char *)data
+ fHeader
->fRuleSource
);
146 fRuleString
.setTo(TRUE
, fRuleSource
, -1);
147 U_ASSERT(data
->fRuleSourceLen
> 0);
149 fRuleStatusTable
= (int32_t *)((char *)data
+ fHeader
->fStatusTable
);
150 fStatusMaxIdx
= data
->fStatusTableLen
/ sizeof(int32_t);
155 char *debugEnv
= getenv("U_RBBIDEBUG");
156 if (debugEnv
&& uprv_strstr(debugEnv
, "data")) {this->printData();}
161 //-----------------------------------------------------------------------------
163 // Destructor. Don't call this - use removeReference() instead.
165 //-----------------------------------------------------------------------------
166 RBBIDataWrapper57::~RBBIDataWrapper57() {
167 U_ASSERT(fRefCount
== 0);
169 udata_close(fUDataMem
);
170 } else if (!fDontFreeData
) {
171 uprv_free((void *)fHeader
);
177 //-----------------------------------------------------------------------------
179 // Operator == Consider two RBBIDataWrappers to be equal if they
180 // refer to the same underlying data. Although
181 // the data wrappers are normally shared between
182 // iterator instances, it's possible to independently
183 // open the same data twice, and get two instances, which
184 // should still be ==.
186 //-----------------------------------------------------------------------------
187 UBool
RBBIDataWrapper57::operator ==(const RBBIDataWrapper57
&other
) const {
188 if (fHeader
== other
.fHeader
) {
191 if (fHeader
->fLength
!= other
.fHeader
->fLength
) {
194 if (uprv_memcmp(fHeader
, other
.fHeader
, fHeader
->fLength
) == 0) {
200 int32_t RBBIDataWrapper57::hashCode() {
201 return fHeader
->fFTableLen
;
206 //-----------------------------------------------------------------------------
208 // Reference Counting. A single RBBIDataWrapper57 object is shared among
209 // however many RulesBasedBreakIterator instances are
210 // referencing the same data.
212 //-----------------------------------------------------------------------------
213 void RBBIDataWrapper57::removeReference() {
214 if (umtx_atomic_dec(&fRefCount
) == 0) {
220 RBBIDataWrapper57
*RBBIDataWrapper57::addReference() {
221 umtx_atomic_inc(&fRefCount
);
227 //-----------------------------------------------------------------------------
229 // getRuleSourceString
231 //-----------------------------------------------------------------------------
232 const UnicodeString
&RBBIDataWrapper57::getRuleSourceString() const {
237 //-----------------------------------------------------------------------------
239 // print - debugging function to dump the runtime data tables.
241 //-----------------------------------------------------------------------------
243 void RBBIDataWrapper57::printTable(const char *heading
, const RBBIStateTable
*table
) {
247 RBBIDebugPrintf(" %s\n", heading
);
249 RBBIDebugPrintf("State | Acc LA TagIx");
250 for (c
=0; c
<fHeader
->fCatCount
; c
++) {RBBIDebugPrintf("%3d ", c
);}
251 RBBIDebugPrintf("\n------|---------------"); for (c
=0;c
<fHeader
->fCatCount
; c
++) {
252 RBBIDebugPrintf("----");
254 RBBIDebugPrintf("\n");
257 RBBIDebugPrintf(" N U L L T A B L E\n\n");
260 for (s
=0; s
<table
->fNumStates
; s
++) {
261 RBBIStateTableRow
*row
= (RBBIStateTableRow
*)
262 (table
->fTableData
+ (table
->fRowLen
* s
));
263 RBBIDebugPrintf("%4d | %3d %3d %3d ", s
, row
->fAccepting
, row
->fLookAhead
, row
->fTagIdx
);
264 for (c
=0; c
<fHeader
->fCatCount
; c
++) {
265 RBBIDebugPrintf("%3d ", row
->fNextState
[c
]);
267 RBBIDebugPrintf("\n");
269 RBBIDebugPrintf("\n");
275 void RBBIDataWrapper57::printData() {
276 RBBIDebugPrintf("RBBI Data at %p\n", (void *)fHeader
);
277 RBBIDebugPrintf(" Version = {%d %d %d %d}\n", fHeader
->fFormatVersion
[0], fHeader
->fFormatVersion
[1],
278 fHeader
->fFormatVersion
[2], fHeader
->fFormatVersion
[3]);
279 RBBIDebugPrintf(" total length of data = %d\n", fHeader
->fLength
);
280 RBBIDebugPrintf(" number of character categories = %d\n\n", fHeader
->fCatCount
);
282 printTable("Forward State Transition Table", fForwardTable
);
283 printTable("Reverse State Transition Table", fReverseTable
);
284 printTable("Safe Forward State Transition Table", fSafeFwdTable
);
285 printTable("Safe Reverse State Transition Table", fSafeRevTable
);
287 RBBIDebugPrintf("\nOrignal Rules source:\n");
288 for (int32_t c
=0; fRuleSource
[c
] != 0; c
++) {
289 RBBIDebugPrintf("%c", fRuleSource
[c
]);
291 RBBIDebugPrintf("\n\n");
299 //-----------------------------------------------------------------------------
301 // ubrk_swap - in standard rbbidata.cpp
303 //-----------------------------------------------------------------------------
305 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */