2 ******************************************************************************
4 * Copyright (C) 2007-2008, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
8 * file name: bmpset.cpp
10 * tab size: 8 (not used)
13 * created on: 2007jan29
14 * created by: Markus W. Scherer
17 #include "unicode/utypes.h"
18 #include "unicode/uniset.h"
24 BMPSet::BMPSet(const int32_t *parentList
, int32_t parentListLength
) :
25 list(parentList
), listLength(parentListLength
) {
26 uprv_memset(asciiBytes
, 0, sizeof(asciiBytes
));
27 uprv_memset(table7FF
, 0, sizeof(table7FF
));
28 uprv_memset(bmpBlockBits
, 0, sizeof(bmpBlockBits
));
31 * Set the list indexes for binary searches for
32 * U+0800, U+1000, U+2000, .., U+F000, U+10000.
33 * U+0800 is the first 3-byte-UTF-8 code point. Lower code points are
34 * looked up in the bit tables.
35 * The last pair of indexes is for finding supplementary code points.
37 list4kStarts
[0]=findCodePoint(0x800, 0, listLength
-1);
39 for(i
=1; i
<=0x10; ++i
) {
40 list4kStarts
[i
]=findCodePoint(i
<<12, list4kStarts
[i
-1], listLength
-1);
42 list4kStarts
[0x11]=listLength
-1;
48 BMPSet::BMPSet(const BMPSet
&otherBMPSet
, const int32_t *newParentList
, int32_t newParentListLength
) :
49 list(newParentList
), listLength(newParentListLength
) {
50 uprv_memcpy(asciiBytes
, otherBMPSet
.asciiBytes
, sizeof(asciiBytes
));
51 uprv_memcpy(table7FF
, otherBMPSet
.table7FF
, sizeof(table7FF
));
52 uprv_memcpy(bmpBlockBits
, otherBMPSet
.bmpBlockBits
, sizeof(bmpBlockBits
));
53 uprv_memcpy(list4kStarts
, otherBMPSet
.list4kStarts
, sizeof(list4kStarts
));
60 * Set bits in a bit rectangle in "vertical" bit organization.
63 static void set32x64Bits(uint32_t table
[64], int32_t start
, int32_t limit
) {
64 int32_t lead
=start
>>6;
65 int32_t trail
=start
&0x3f;
67 // Set one bit indicating an all-one block.
68 uint32_t bits
=(uint32_t)1<<lead
;
69 if((start
+1)==limit
) { // Single-character shortcut.
74 int32_t limitLead
=limit
>>6;
75 int32_t limitTrail
=limit
&0x3f;
78 // Partial vertical bit column.
79 while(trail
<limitTrail
) {
83 // Partial vertical bit column,
84 // followed by a bit rectangle,
85 // followed by another partial vertical bit column.
95 bits
&=(1<<limitLead
)-1;
97 for(trail
=0; trail
<64; ++trail
) {
102 for(trail
=0; trail
<limitTrail
; ++trail
) {
108 void BMPSet::initBits() {
109 UChar32 start
, limit
;
114 start
=list
[listIndex
++];
115 if(listIndex
<listLength
) {
116 limit
=list
[listIndex
++];
124 asciiBytes
[start
++]=1;
125 } while(start
<limit
&& start
<0x80);
126 } while(limit
<=0x80);
130 set32x64Bits(table7FF
, start
, limit
<=0x800 ? limit
: 0x800);
136 start
=list
[listIndex
++];
137 if(listIndex
<listLength
) {
138 limit
=list
[listIndex
++];
144 // Set bmpBlockBits[].
145 int32_t minStart
=0x800;
146 while(start
<0x10000) {
154 if(start
<limit
) { // Else: Another range entirely in a known mixed-value block.
156 // Mixed-value block of 64 code points.
158 bmpBlockBits
[start
&0x3f]|=0x10001<<(start
>>6);
159 start
=(start
+1)<<6; // Round up to the next block boundary.
160 minStart
=start
; // Ignore further ranges in this block.
163 if(start
<(limit
&~0x3f)) {
164 // Multiple all-ones blocks of 64 code points each.
165 set32x64Bits(bmpBlockBits
, start
>>6, limit
>>6);
169 // Mixed-value block of 64 code points.
171 bmpBlockBits
[limit
&0x3f]|=0x10001<<(limit
>>6);
172 limit
=(limit
+1)<<6; // Round up to the next block boundary.
173 minStart
=limit
; // Ignore further ranges in this block.
182 start
=list
[listIndex
++];
183 if(listIndex
<listLength
) {
184 limit
=list
[listIndex
++];
192 * Override some bits and bytes to the result of contains(FFFD)
193 * for faster validity checking at runtime.
194 * No need to set 0 values where they were reset to 0 in the constructor
195 * and not modified by initBits().
196 * (asciiBytes[] trail bytes, table7FF[] 0..7F, bmpBlockBits[] 0..7FF)
197 * Need to set 0 values for surrogates D800..DFFF.
199 void BMPSet::overrideIllegal() {
203 if(containsSlow(0xfffd, list4kStarts
[0xf], list4kStarts
[0x10])) {
204 // contains(FFFD)==TRUE
205 for(i
=0x80; i
<0xc0; ++i
) {
209 bits
=3; // Lead bytes 0xC0 and 0xC1.
210 for(i
=0; i
<64; ++i
) {
214 bits
=1; // Lead byte 0xE0.
215 for(i
=0; i
<32; ++i
) { // First half of 4k block.
216 bmpBlockBits
[i
]|=bits
;
219 mask
=~(0x10001<<0xd); // Lead byte 0xED.
221 for(i
=32; i
<64; ++i
) { // Second half of 4k block.
222 bmpBlockBits
[i
]=(bmpBlockBits
[i
]&mask
)|bits
;
225 // contains(FFFD)==FALSE
226 mask
=~(0x10001<<0xd); // Lead byte 0xED.
227 for(i
=32; i
<64; ++i
) { // Second half of 4k block.
228 bmpBlockBits
[i
]&=mask
;
233 int32_t BMPSet::findCodePoint(UChar32 c
, int32_t lo
, int32_t hi
) const {
236 set list[] c=0 1 3 4 7 8
237 === ============== ===========
238 [] [110000] 0 0 0 0 0 0
239 [\u0000-\u0003] [0, 4, 110000] 1 1 1 2 2 2
240 [\u0004-\u0007] [4, 8, 110000] 0 0 0 1 1 2
241 [:Any:] [0, 110000] 1 1 1 1 1 1
244 // Return the smallest i such that c < list[i]. Assume
245 // list[len - 1] == HIGH and that c is legal (0..HIGH-1).
248 // High runner test. c is often after the last range, so an
249 // initial check for this condition pays off.
250 if (lo
>= hi
|| c
>= list
[hi
-1])
252 // invariant: c >= list[lo]
253 // invariant: c < list[hi]
255 int32_t i
= (lo
+ hi
) >> 1;
258 } else if (c
< list
[i
]) {
268 BMPSet::contains(UChar32 c
) const {
269 if((uint32_t)c
<=0x7f) {
270 return (UBool
)asciiBytes
[c
];
271 } else if((uint32_t)c
<=0x7ff) {
272 return (UBool
)((table7FF
[c
&0x3f]&((uint32_t)1<<(c
>>6)))!=0);
273 } else if((uint32_t)c
<0xd800 || (c
>=0xe000 && c
<=0xffff)) {
275 uint32_t twoBits
=(bmpBlockBits
[(c
>>6)&0x3f]>>lead
)&0x10001;
277 // All 64 code points with the same bits 15..6
278 // are either in the set or not.
279 return (UBool
)twoBits
;
281 // Look up the code point in its 4k block of code points.
282 return containsSlow(c
, list4kStarts
[lead
], list4kStarts
[lead
+1]);
284 } else if((uint32_t)c
<=0x10ffff) {
285 // surrogate or supplementary code point
286 return containsSlow(c
, list4kStarts
[0xd], list4kStarts
[0x11]);
288 // Out-of-range code points get FALSE, consistent with long-standing
289 // behavior of UnicodeSet::contains(c).
295 * Check for sufficient length for trail unit for each surrogate pair.
296 * Handle single surrogates as surrogate code points as usual in ICU.
299 BMPSet::span(const UChar
*s
, const UChar
*limit
, USetSpanCondition spanCondition
) const {
310 } else if(c
<=0x7ff) {
311 if((table7FF
[c
&0x3f]&((uint32_t)1<<(c
>>6)))==0) {
314 } else if(c
<0xd800 || c
>=0xe000) {
316 uint32_t twoBits
=(bmpBlockBits
[(c
>>6)&0x3f]>>lead
)&0x10001;
318 // All 64 code points with the same bits 15..6
319 // are either in the set or not.
324 // Look up the code point in its 4k block of code points.
325 if(!containsSlow(c
, list4kStarts
[lead
], list4kStarts
[lead
+1])) {
329 } else if(c
>=0xdc00 || (s
+1)==limit
|| (c2
=s
[1])<0xdc00 || c2
>=0xe000) {
330 // surrogate code point
331 if(!containsSlow(c
, list4kStarts
[0xd], list4kStarts
[0xe])) {
336 if(!containsSlow(U16_GET_SUPPLEMENTARY(c
, c2
), list4kStarts
[0x10], list4kStarts
[0x11])) {
350 } else if(c
<=0x7ff) {
351 if((table7FF
[c
&0x3f]&((uint32_t)1<<(c
>>6)))!=0) {
354 } else if(c
<0xd800 || c
>=0xe000) {
356 uint32_t twoBits
=(bmpBlockBits
[(c
>>6)&0x3f]>>lead
)&0x10001;
358 // All 64 code points with the same bits 15..6
359 // are either in the set or not.
364 // Look up the code point in its 4k block of code points.
365 if(containsSlow(c
, list4kStarts
[lead
], list4kStarts
[lead
+1])) {
369 } else if(c
>=0xdc00 || (s
+1)==limit
|| (c2
=s
[1])<0xdc00 || c2
>=0xe000) {
370 // surrogate code point
371 if(containsSlow(c
, list4kStarts
[0xd], list4kStarts
[0xe])) {
376 if(containsSlow(U16_GET_SUPPLEMENTARY(c
, c2
), list4kStarts
[0x10], list4kStarts
[0x11])) {
386 /* Symmetrical with span(). */
388 BMPSet::spanBack(const UChar
*s
, const UChar
*limit
, USetSpanCondition spanCondition
) const {
399 } else if(c
<=0x7ff) {
400 if((table7FF
[c
&0x3f]&((uint32_t)1<<(c
>>6)))==0) {
403 } else if(c
<0xd800 || c
>=0xe000) {
405 uint32_t twoBits
=(bmpBlockBits
[(c
>>6)&0x3f]>>lead
)&0x10001;
407 // All 64 code points with the same bits 15..6
408 // are either in the set or not.
413 // Look up the code point in its 4k block of code points.
414 if(!containsSlow(c
, list4kStarts
[lead
], list4kStarts
[lead
+1])) {
418 } else if(c
<0xdc00 || s
==limit
|| (c2
=*(limit
-1))<0xd800 || c2
>=0xdc00) {
419 // surrogate code point
420 if(!containsSlow(c
, list4kStarts
[0xd], list4kStarts
[0xe])) {
425 if(!containsSlow(U16_GET_SUPPLEMENTARY(c2
, c
), list4kStarts
[0x10], list4kStarts
[0x11])) {
442 } else if(c
<=0x7ff) {
443 if((table7FF
[c
&0x3f]&((uint32_t)1<<(c
>>6)))!=0) {
446 } else if(c
<0xd800 || c
>=0xe000) {
448 uint32_t twoBits
=(bmpBlockBits
[(c
>>6)&0x3f]>>lead
)&0x10001;
450 // All 64 code points with the same bits 15..6
451 // are either in the set or not.
456 // Look up the code point in its 4k block of code points.
457 if(containsSlow(c
, list4kStarts
[lead
], list4kStarts
[lead
+1])) {
461 } else if(c
<0xdc00 || s
==limit
|| (c2
=*(limit
-1))<0xd800 || c2
>=0xdc00) {
462 // surrogate code point
463 if(containsSlow(c
, list4kStarts
[0xd], list4kStarts
[0xe])) {
468 if(containsSlow(U16_GET_SUPPLEMENTARY(c2
, c
), list4kStarts
[0x10], list4kStarts
[0x11])) {
482 * Precheck for sufficient trail bytes at end of string only once per span.
486 BMPSet::spanUTF8(const uint8_t *s
, int32_t length
, USetSpanCondition spanCondition
) const {
487 const uint8_t *limit
=s
+length
;
490 // Initial all-ASCII span.
493 if(!asciiBytes
[b
] || ++s
==limit
) {
497 } while((int8_t)b
>=0);
500 if(asciiBytes
[b
] || ++s
==limit
) {
504 } while((int8_t)b
>=0);
506 length
=(int32_t)(limit
-s
);
509 if(spanCondition
!=USET_SPAN_NOT_CONTAINED
) {
510 spanCondition
=USET_SPAN_CONTAINED
; // Pin to 0/1 values.
513 const uint8_t *limit0
=limit
;
516 * Make sure that the last 1/2/3/4-byte sequence before limit is complete
517 * or runs into a lead byte.
518 * In the span loop compare s with limit only once
519 * per multi-byte character.
521 * Give a trailing illegal sequence the same value as the result of contains(FFFD),
522 * including it if that is part of the span, otherwise set limit0 to before
523 * the truncated sequence.
527 // b>=0x80: lead or trail byte
529 // single trail byte, check for preceding 3- or 4-byte lead byte
530 if(length
>=2 && (b
=*(limit
-2))>=0xe0) {
532 if(asciiBytes
[0x80]!=spanCondition
) {
535 } else if(b
<0xc0 && b
>=0x80 && length
>=3 && (b
=*(limit
-3))>=0xf0) {
536 // 4-byte lead byte with only two trail bytes
538 if(asciiBytes
[0x80]!=spanCondition
) {
543 // lead byte with no trail bytes
545 if(asciiBytes
[0x80]!=spanCondition
) {
556 // ASCII; or trail bytes with the result of contains(FFFD).
561 } else if(++s
==limit
) {
570 } else if(++s
==limit
) {
577 ++s
; // Advance past the lead byte.
580 if( /* handle U+0000..U+FFFF inline */
581 (t1
=(uint8_t)(s
[0]-0x80)) <= 0x3f &&
582 (t2
=(uint8_t)(s
[1]-0x80)) <= 0x3f
585 uint32_t twoBits
=(bmpBlockBits
[t1
]>>b
)&0x10001;
587 // All 64 code points with this lead byte and middle trail byte
588 // are either in the set or not.
589 if(twoBits
!=(uint32_t)spanCondition
) {
593 // Look up the code point in its 4k block of code points.
594 UChar32 c
=(b
<<12)|(t1
<<6)|t2
;
595 if(containsSlow(c
, list4kStarts
[b
], list4kStarts
[b
+1]) != spanCondition
) {
602 } else if( /* handle U+10000..U+10FFFF inline */
603 (t1
=(uint8_t)(s
[0]-0x80)) <= 0x3f &&
604 (t2
=(uint8_t)(s
[1]-0x80)) <= 0x3f &&
605 (t3
=(uint8_t)(s
[2]-0x80)) <= 0x3f
607 // Give an illegal sequence the same value as the result of contains(FFFD).
608 UChar32 c
=((UChar32
)(b
-0xf0)<<18)|((UChar32
)t1
<<12)|(t2
<<6)|t3
;
609 if( ( (0x10000<=c
&& c
<=0x10ffff) ?
610 containsSlow(c
, list4kStarts
[0x10], list4kStarts
[0x11]) :
619 } else /* 0xc0<=b<0xe0 */ {
620 if( /* handle U+0000..U+07FF inline */
621 (t1
=(uint8_t)(*s
-0x80)) <= 0x3f
623 if((USetSpanCondition
)((table7FF
[t1
]&((uint32_t)1<<(b
&0x1f)))!=0) != spanCondition
) {
631 // Give an illegal sequence the same value as the result of contains(FFFD).
632 // Handle each byte of an illegal sequence separately to simplify the code;
633 // no need to optimize error handling.
634 if(asciiBytes
[0x80]!=spanCondition
) {
643 * While going backwards through UTF-8 optimize only for ASCII.
644 * Unlike UTF-16, UTF-8 is not forward-backward symmetrical, that is, it is not
645 * possible to tell from the last byte in a multi-byte sequence how many
646 * preceding bytes there should be. Therefore, going backwards through UTF-8
647 * is much harder than going forward.
650 BMPSet::spanBackUTF8(const uint8_t *s
, int32_t length
, USetSpanCondition spanCondition
) const {
651 if(spanCondition
!=USET_SPAN_NOT_CONTAINED
) {
652 spanCondition
=USET_SPAN_CONTAINED
; // Pin to 0/1 values.
665 } else if(length
==0) {
669 } while((int8_t)b
>=0);
674 } else if(length
==0) {
678 } while((int8_t)b
>=0);
685 // trail byte: collect a multi-byte character
686 c
=utf8_prevCharSafeBody(s
, 0, &length
, b
, -1);
691 // lead byte in last-trail position
694 // c is a valid code point, not ASCII, not a surrogate
696 if((USetSpanCondition
)((table7FF
[c
&0x3f]&((uint32_t)1<<(c
>>6)))!=0) != spanCondition
) {
699 } else if(c
<=0xffff) {
701 uint32_t twoBits
=(bmpBlockBits
[(c
>>6)&0x3f]>>lead
)&0x10001;
703 // All 64 code points with the same bits 15..6
704 // are either in the set or not.
705 if(twoBits
!=(uint32_t)spanCondition
) {
709 // Look up the code point in its 4k block of code points.
710 if(containsSlow(c
, list4kStarts
[lead
], list4kStarts
[lead
+1]) != spanCondition
) {
715 if(containsSlow(c
, list4kStarts
[0x10], list4kStarts
[0x11]) != spanCondition
) {