1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
**********************************************************************
* Copyright (C) 2014, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
*
* scriptset.cpp
*
* created on: 2013 Jan 7
* created by: Andy Heninger
*/
#include "unicode/utypes.h"
#include "unicode/uchar.h"
#include "unicode/unistr.h"
#include "scriptset.h"
#include "uassert.h"
#include "cmemory.h"
U_NAMESPACE_BEGIN
//----------------------------------------------------------------------------
//
// ScriptSet implementation
//
//----------------------------------------------------------------------------
ScriptSet::ScriptSet() {
uprv_memset(bits, 0, sizeof(bits));
}
ScriptSet::~ScriptSet() {
}
ScriptSet::ScriptSet(const ScriptSet &other) {
*this = other;
}
ScriptSet & ScriptSet::operator =(const ScriptSet &other) {
uprv_memcpy(bits, other.bits, sizeof(bits));
return *this;
}
bool ScriptSet::operator == (const ScriptSet &other) const {
for (uint32_t i=0; i<UPRV_LENGTHOF(bits); i++) {
if (bits[i] != other.bits[i]) {
return false;
}
}
return true;
}
UBool ScriptSet::test(UScriptCode script, UErrorCode &status) const {
if (U_FAILURE(status)) {
return false;
}
if (script < 0 || (int32_t)script >= SCRIPT_LIMIT) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return false;
}
uint32_t index = script / 32;
uint32_t bit = 1 << (script & 31);
return ((bits[index] & bit) != 0);
}
ScriptSet &ScriptSet::set(UScriptCode script, UErrorCode &status) {
if (U_FAILURE(status)) {
return *this;
}
if (script < 0 || (int32_t)script >= SCRIPT_LIMIT) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return *this;
}
uint32_t index = script / 32;
uint32_t bit = 1 << (script & 31);
bits[index] |= bit;
return *this;
}
ScriptSet &ScriptSet::reset(UScriptCode script, UErrorCode &status) {
if (U_FAILURE(status)) {
return *this;
}
if (script < 0 || (int32_t)script >= SCRIPT_LIMIT) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return *this;
}
uint32_t index = script / 32;
uint32_t bit = 1 << (script & 31);
bits[index] &= ~bit;
return *this;
}
ScriptSet &ScriptSet::Union(const ScriptSet &other) {
for (uint32_t i=0; i<UPRV_LENGTHOF(bits); i++) {
bits[i] |= other.bits[i];
}
return *this;
}
ScriptSet &ScriptSet::intersect(const ScriptSet &other) {
for (uint32_t i=0; i<UPRV_LENGTHOF(bits); i++) {
bits[i] &= other.bits[i];
}
return *this;
}
ScriptSet &ScriptSet::intersect(UScriptCode script, UErrorCode &status) {
ScriptSet t;
t.set(script, status);
if (U_SUCCESS(status)) {
this->intersect(t);
}
return *this;
}
UBool ScriptSet::intersects(const ScriptSet &other) const {
for (uint32_t i=0; i<UPRV_LENGTHOF(bits); i++) {
if ((bits[i] & other.bits[i]) != 0) {
return true;
}
}
return false;
}
UBool ScriptSet::contains(const ScriptSet &other) const {
ScriptSet t(*this);
t.intersect(other);
return (t == other);
}
ScriptSet &ScriptSet::setAll() {
for (uint32_t i=0; i<UPRV_LENGTHOF(bits); i++) {
bits[i] = 0xffffffffu;
}
return *this;
}
ScriptSet &ScriptSet::resetAll() {
uprv_memset(bits, 0, sizeof(bits));
return *this;
}
int32_t ScriptSet::countMembers() const {
// This bit counter is good for sparse numbers of '1's, which is
// very much the case that we will usually have.
int32_t count = 0;
for (uint32_t i=0; i<UPRV_LENGTHOF(bits); i++) {
uint32_t x = bits[i];
while (x > 0) {
count++;
x &= (x - 1); // and off the least significant one bit.
}
}
return count;
}
int32_t ScriptSet::hashCode() const {
int32_t hash = 0;
for (int32_t i=0; i<UPRV_LENGTHOF(bits); i++) {
hash ^= bits[i];
}
return hash;
}
int32_t ScriptSet::nextSetBit(int32_t fromIndex) const {
// TODO: Wants a better implementation.
if (fromIndex < 0) {
return -1;
}
UErrorCode status = U_ZERO_ERROR;
for (int32_t scriptIndex = fromIndex; scriptIndex < SCRIPT_LIMIT; scriptIndex++) {
if (test((UScriptCode)scriptIndex, status)) {
return scriptIndex;
}
}
return -1;
}
UBool ScriptSet::isEmpty() const {
for (uint32_t i=0; i<UPRV_LENGTHOF(bits); i++) {
if (bits[i] != 0) {
return false;
}
}
return true;
}
UnicodeString &ScriptSet::displayScripts(UnicodeString &dest) const {
UBool firstTime = true;
for (int32_t i = nextSetBit(0); i >= 0; i = nextSetBit(i + 1)) {
if (!firstTime) {
dest.append((UChar)0x20);
}
firstTime = false;
const char *scriptName = uscript_getShortName((UScriptCode(i)));
dest.append(UnicodeString(scriptName, -1, US_INV));
}
return dest;
}
ScriptSet &ScriptSet::parseScripts(const UnicodeString &scriptString, UErrorCode &status) {
resetAll();
if (U_FAILURE(status)) {
return *this;
}
UnicodeString oneScriptName;
for (int32_t i=0; i<scriptString.length();) {
UChar32 c = scriptString.char32At(i);
i = scriptString.moveIndex32(i, 1);
if (!u_isUWhiteSpace(c)) {
oneScriptName.append(c);
if (i < scriptString.length()) {
continue;
}
}
if (oneScriptName.length() > 0) {
char buf[40];
oneScriptName.extract(0, oneScriptName.length(), buf, sizeof(buf)-1, US_INV);
buf[sizeof(buf)-1] = 0;
int32_t sc = u_getPropertyValueEnum(UCHAR_SCRIPT, buf);
if (sc == UCHAR_INVALID_CODE) {
status = U_ILLEGAL_ARGUMENT_ERROR;
} else {
this->set((UScriptCode)sc, status);
}
if (U_FAILURE(status)) {
return *this;
}
oneScriptName.remove();
}
}
return *this;
}
void ScriptSet::setScriptExtensions(UChar32 codePoint, UErrorCode& status) {
if (U_FAILURE(status)) { return; }
static const int32_t FIRST_GUESS_SCRIPT_CAPACITY = 20;
MaybeStackArray<UScriptCode,FIRST_GUESS_SCRIPT_CAPACITY> scripts;
UErrorCode internalStatus = U_ZERO_ERROR;
int32_t script_count = -1;
while (true) {
script_count = uscript_getScriptExtensions(
codePoint, scripts.getAlias(), scripts.getCapacity(), &internalStatus);
if (internalStatus == U_BUFFER_OVERFLOW_ERROR) {
// Need to allocate more space
if (scripts.resize(script_count) == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
internalStatus = U_ZERO_ERROR;
} else {
break;
}
}
// Check if we failed for some reason other than buffer overflow
if (U_FAILURE(internalStatus)) {
status = internalStatus;
return;
}
// Load the scripts into the ScriptSet and return
for (int32_t i = 0; i < script_count; i++) {
this->set(scripts[i], status);
if (U_FAILURE(status)) { return; }
}
}
U_NAMESPACE_END
U_CAPI UBool U_EXPORT2
uhash_equalsScriptSet(const UElement key1, const UElement key2) {
icu::ScriptSet *s1 = static_cast<icu::ScriptSet *>(key1.pointer);
icu::ScriptSet *s2 = static_cast<icu::ScriptSet *>(key2.pointer);
return (*s1 == *s2);
}
U_CAPI int8_t U_EXPORT2
uhash_compareScriptSet(UElement key0, UElement key1) {
icu::ScriptSet *s0 = static_cast<icu::ScriptSet *>(key0.pointer);
icu::ScriptSet *s1 = static_cast<icu::ScriptSet *>(key1.pointer);
int32_t diff = s0->countMembers() - s1->countMembers();
if (diff != 0) return static_cast<UBool>(diff);
int32_t i0 = s0->nextSetBit(0);
int32_t i1 = s1->nextSetBit(0);
while ((diff = i0-i1) == 0 && i0 > 0) {
i0 = s0->nextSetBit(i0+1);
i1 = s1->nextSetBit(i1+1);
}
return (int8_t)diff;
}
U_CAPI int32_t U_EXPORT2
uhash_hashScriptSet(const UElement key) {
icu::ScriptSet *s = static_cast<icu::ScriptSet *>(key.pointer);
return s->hashCode();
}
U_CAPI void U_EXPORT2
uhash_deleteScriptSet(void *obj) {
icu::ScriptSet *s = static_cast<icu::ScriptSet *>(obj);
delete s;
}
|