2 ******************************************************************************
4 * Copyright (C) 2007-2012, 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"
19 #include "unicode/utf8.h"
20 #include "unicode/utf16.h"
27 BMPSet::BMPSet(const int32_t *parentList
, int32_t parentListLength
) :
28 list(parentList
), listLength(parentListLength
) {
29 uprv_memset(asciiBytes
, 0, sizeof(asciiBytes
));
30 uprv_memset(table7FF
, 0, sizeof(table7FF
));
31 uprv_memset(bmpBlockBits
, 0, sizeof(bmpBlockBits
));
34 * Set the list indexes for binary searches for
35 * U+0800, U+1000, U+2000, .., U+F000, U+10000.
36 * U+0800 is the first 3-byte-UTF-8 code point. Lower code points are
37 * looked up in the bit tables.
38 * The last pair of indexes is for finding supplementary code points.
40 list4kStarts
[0]=findCodePoint(0x800, 0, listLength
-1);
42 for(i
=1; i
<=0x10; ++i
) {
43 list4kStarts
[i
]=findCodePoint(i
<<12, list4kStarts
[i
-1], listLength
-1);
45 list4kStarts
[0x11]=listLength
-1;
51 BMPSet::BMPSet(const BMPSet
&otherBMPSet
, const int32_t *newParentList
, int32_t newParentListLength
) :
52 list(newParentList
), listLength(newParentListLength
) {
53 uprv_memcpy(asciiBytes
, otherBMPSet
.asciiBytes
, sizeof(asciiBytes
));
54 uprv_memcpy(table7FF
, otherBMPSet
.table7FF
, sizeof(table7FF
));
55 uprv_memcpy(bmpBlockBits
, otherBMPSet
.bmpBlockBits
, sizeof(bmpBlockBits
));
56 uprv_memcpy(list4kStarts
, otherBMPSet
.list4kStarts
, sizeof(list4kStarts
));
63 * Set bits in a bit rectangle in "vertical" bit organization.
66 static void set32x64Bits(uint32_t table
[64], int32_t start
, int32_t limit
) {
67 U_ASSERT(start
<limit
);
68 U_ASSERT(limit
<=0x800);
70 int32_t lead
=start
>>6; // Named for UTF-8 2-byte lead byte with upper 5 bits.
71 int32_t trail
=start
&0x3f; // Named for UTF-8 2-byte trail byte with lower 6 bits.
73 // Set one bit indicating an all-one block.
74 uint32_t bits
=(uint32_t)1<<lead
;
75 if((start
+1)==limit
) { // Single-character shortcut.
80 int32_t limitLead
=limit
>>6;
81 int32_t limitTrail
=limit
&0x3f;
84 // Partial vertical bit column.
85 while(trail
<limitTrail
) {
89 // Partial vertical bit column,
90 // followed by a bit rectangle,
91 // followed by another partial vertical bit column.
101 bits
&=(1<<limitLead
)-1;
103 for(trail
=0; trail
<64; ++trail
) {
107 // limit<=0x800. If limit==0x800 then limitLead=32 and limitTrail=0.
108 // In that case, bits=1<<limitLead is undefined but the bits value
109 // is not used because trail<limitTrail is already false.
110 bits
=(uint32_t)1<<((limitLead
== 0x20) ? (limitLead
- 1) : limitLead
);
111 for(trail
=0; trail
<limitTrail
; ++trail
) {
117 void BMPSet::initBits() {
118 UChar32 start
, limit
;
123 start
=list
[listIndex
++];
124 if(listIndex
<listLength
) {
125 limit
=list
[listIndex
++];
133 asciiBytes
[start
++]=1;
134 } while(start
<limit
&& start
<0x80);
135 } while(limit
<=0x80);
139 set32x64Bits(table7FF
, start
, limit
<=0x800 ? limit
: 0x800);
145 start
=list
[listIndex
++];
146 if(listIndex
<listLength
) {
147 limit
=list
[listIndex
++];
153 // Set bmpBlockBits[].
154 int32_t minStart
=0x800;
155 while(start
<0x10000) {
163 if(start
<limit
) { // Else: Another range entirely in a known mixed-value block.
165 // Mixed-value block of 64 code points.
167 bmpBlockBits
[start
&0x3f]|=0x10001<<(start
>>6);
168 start
=(start
+1)<<6; // Round up to the next block boundary.
169 minStart
=start
; // Ignore further ranges in this block.
172 if(start
<(limit
&~0x3f)) {
173 // Multiple all-ones blocks of 64 code points each.
174 set32x64Bits(bmpBlockBits
, start
>>6, limit
>>6);
178 // Mixed-value block of 64 code points.
180 bmpBlockBits
[limit
&0x3f]|=0x10001<<(limit
>>6);
181 limit
=(limit
+1)<<6; // Round up to the next block boundary.
182 minStart
=limit
; // Ignore further ranges in this block.
191 start
=list
[listIndex
++];
192 if(listIndex
<listLength
) {
193 limit
=list
[listIndex
++];
201 * Override some bits and bytes to the result of contains(FFFD)
202 * for faster validity checking at runtime.
203 * No need to set 0 values where they were reset to 0 in the constructor
204 * and not modified by initBits().
205 * (asciiBytes[] trail bytes, table7FF[] 0..7F, bmpBlockBits[] 0..7FF)
206 * Need to set 0 values for surrogates D800..DFFF.
208 void BMPSet::overrideIllegal() {
212 if(containsSlow(0xfffd, list4kStarts
[0xf], list4kStarts
[0x10])) {
213 // contains(FFFD)==TRUE
214 for(i
=0x80; i
<0xc0; ++i
) {
218 bits
=3; // Lead bytes 0xC0 and 0xC1.
219 for(i
=0; i
<64; ++i
) {
223 bits
=1; // Lead byte 0xE0.
224 for(i
=0; i
<32; ++i
) { // First half of 4k block.
225 bmpBlockBits
[i
]|=bits
;
228 mask
=~(0x10001<<0xd); // Lead byte 0xED.
230 for(i
=32; i
<64; ++i
) { // Second half of 4k block.
231 bmpBlockBits
[i
]=(bmpBlockBits
[i
]&mask
)|bits
;
234 // contains(FFFD)==FALSE
235 mask
=~(0x10001<<0xd); // Lead byte 0xED.
236 for(i
=32; i
<64; ++i
) { // Second half of 4k block.
237 bmpBlockBits
[i
]&=mask
;
242 int32_t BMPSet::findCodePoint(UChar32 c
, int32_t lo
, int32_t hi
) const {
245 set list[] c=0 1 3 4 7 8
246 === ============== ===========
247 [] [110000] 0 0 0 0 0 0
248 [\u0000-\u0003] [0, 4, 110000] 1 1 1 2 2 2
249 [\u0004-\u0007] [4, 8, 110000] 0 0 0 1 1 2
250 [:Any:] [0, 110000] 1 1 1 1 1 1
253 // Return the smallest i such that c < list[i]. Assume
254 // list[len - 1] == HIGH and that c is legal (0..HIGH-1).
257 // High runner test. c is often after the last range, so an
258 // initial check for this condition pays off.
259 if (lo
>= hi
|| c
>= list
[hi
-1])
261 // invariant: c >= list[lo]
262 // invariant: c < list[hi]
264 int32_t i
= (lo
+ hi
) >> 1;
267 } else if (c
< list
[i
]) {
277 BMPSet::contains(UChar32 c
) const {
278 if((uint32_t)c
<=0x7f) {
279 return (UBool
)asciiBytes
[c
];
280 } else if((uint32_t)c
<=0x7ff) {
281 return (UBool
)((table7FF
[c
&0x3f]&((uint32_t)1<<(c
>>6)))!=0);
282 } else if((uint32_t)c
<0xd800 || (c
>=0xe000 && c
<=0xffff)) {
284 uint32_t twoBits
=(bmpBlockBits
[(c
>>6)&0x3f]>>lead
)&0x10001;
286 // All 64 code points with the same bits 15..6
287 // are either in the set or not.
288 return (UBool
)twoBits
;
290 // Look up the code point in its 4k block of code points.
291 return containsSlow(c
, list4kStarts
[lead
], list4kStarts
[lead
+1]);
293 } else if((uint32_t)c
<=0x10ffff) {
294 // surrogate or supplementary code point
295 return containsSlow(c
, list4kStarts
[0xd], list4kStarts
[0x11]);
297 // Out-of-range code points get FALSE, consistent with long-standing
298 // behavior of UnicodeSet::contains(c).
304 * Check for sufficient length for trail unit for each surrogate pair.
305 * Handle single surrogates as surrogate code points as usual in ICU.
308 BMPSet::span(const UChar
*s
, const UChar
*limit
, USetSpanCondition spanCondition
) const {
319 } else if(c
<=0x7ff) {
320 if((table7FF
[c
&0x3f]&((uint32_t)1<<(c
>>6)))==0) {
323 } else if(c
<0xd800 || c
>=0xe000) {
325 uint32_t twoBits
=(bmpBlockBits
[(c
>>6)&0x3f]>>lead
)&0x10001;
327 // All 64 code points with the same bits 15..6
328 // are either in the set or not.
333 // Look up the code point in its 4k block of code points.
334 if(!containsSlow(c
, list4kStarts
[lead
], list4kStarts
[lead
+1])) {
338 } else if(c
>=0xdc00 || (s
+1)==limit
|| (c2
=s
[1])<0xdc00 || c2
>=0xe000) {
339 // surrogate code point
340 if(!containsSlow(c
, list4kStarts
[0xd], list4kStarts
[0xe])) {
345 if(!containsSlow(U16_GET_SUPPLEMENTARY(c
, c2
), list4kStarts
[0x10], list4kStarts
[0x11])) {
359 } else if(c
<=0x7ff) {
360 if((table7FF
[c
&0x3f]&((uint32_t)1<<(c
>>6)))!=0) {
363 } else if(c
<0xd800 || c
>=0xe000) {
365 uint32_t twoBits
=(bmpBlockBits
[(c
>>6)&0x3f]>>lead
)&0x10001;
367 // All 64 code points with the same bits 15..6
368 // are either in the set or not.
373 // Look up the code point in its 4k block of code points.
374 if(containsSlow(c
, list4kStarts
[lead
], list4kStarts
[lead
+1])) {
378 } else if(c
>=0xdc00 || (s
+1)==limit
|| (c2
=s
[1])<0xdc00 || c2
>=0xe000) {
379 // surrogate code point
380 if(containsSlow(c
, list4kStarts
[0xd], list4kStarts
[0xe])) {
385 if(containsSlow(U16_GET_SUPPLEMENTARY(c
, c2
), list4kStarts
[0x10], list4kStarts
[0x11])) {
395 /* Symmetrical with span(). */
397 BMPSet::spanBack(const UChar
*s
, const UChar
*limit
, USetSpanCondition spanCondition
) const {
408 } else if(c
<=0x7ff) {
409 if((table7FF
[c
&0x3f]&((uint32_t)1<<(c
>>6)))==0) {
412 } else if(c
<0xd800 || c
>=0xe000) {
414 uint32_t twoBits
=(bmpBlockBits
[(c
>>6)&0x3f]>>lead
)&0x10001;
416 // All 64 code points with the same bits 15..6
417 // are either in the set or not.
422 // Look up the code point in its 4k block of code points.
423 if(!containsSlow(c
, list4kStarts
[lead
], list4kStarts
[lead
+1])) {
427 } else if(c
<0xdc00 || s
==limit
|| (c2
=*(limit
-1))<0xd800 || c2
>=0xdc00) {
428 // surrogate code point
429 if(!containsSlow(c
, list4kStarts
[0xd], list4kStarts
[0xe])) {
434 if(!containsSlow(U16_GET_SUPPLEMENTARY(c2
, c
), list4kStarts
[0x10], list4kStarts
[0x11])) {
451 } else if(c
<=0x7ff) {
452 if((table7FF
[c
&0x3f]&((uint32_t)1<<(c
>>6)))!=0) {
455 } else if(c
<0xd800 || c
>=0xe000) {
457 uint32_t twoBits
=(bmpBlockBits
[(c
>>6)&0x3f]>>lead
)&0x10001;
459 // All 64 code points with the same bits 15..6
460 // are either in the set or not.
465 // Look up the code point in its 4k block of code points.
466 if(containsSlow(c
, list4kStarts
[lead
], list4kStarts
[lead
+1])) {
470 } else if(c
<0xdc00 || s
==limit
|| (c2
=*(limit
-1))<0xd800 || c2
>=0xdc00) {
471 // surrogate code point
472 if(containsSlow(c
, list4kStarts
[0xd], list4kStarts
[0xe])) {
477 if(containsSlow(U16_GET_SUPPLEMENTARY(c2
, c
), list4kStarts
[0x10], list4kStarts
[0x11])) {
491 * Precheck for sufficient trail bytes at end of string only once per span.
495 BMPSet::spanUTF8(const uint8_t *s
, int32_t length
, USetSpanCondition spanCondition
) const {
496 const uint8_t *limit
=s
+length
;
499 // Initial all-ASCII span.
502 if(!asciiBytes
[b
] || ++s
==limit
) {
506 } while((int8_t)b
>=0);
509 if(asciiBytes
[b
] || ++s
==limit
) {
513 } while((int8_t)b
>=0);
515 length
=(int32_t)(limit
-s
);
518 if(spanCondition
!=USET_SPAN_NOT_CONTAINED
) {
519 spanCondition
=USET_SPAN_CONTAINED
; // Pin to 0/1 values.
522 const uint8_t *limit0
=limit
;
525 * Make sure that the last 1/2/3/4-byte sequence before limit is complete
526 * or runs into a lead byte.
527 * In the span loop compare s with limit only once
528 * per multi-byte character.
530 * Give a trailing illegal sequence the same value as the result of contains(FFFD),
531 * including it if that is part of the span, otherwise set limit0 to before
532 * the truncated sequence.
536 // b>=0x80: lead or trail byte
538 // single trail byte, check for preceding 3- or 4-byte lead byte
539 if(length
>=2 && (b
=*(limit
-2))>=0xe0) {
541 if(asciiBytes
[0x80]!=spanCondition
) {
544 } else if(b
<0xc0 && b
>=0x80 && length
>=3 && (b
=*(limit
-3))>=0xf0) {
545 // 4-byte lead byte with only two trail bytes
547 if(asciiBytes
[0x80]!=spanCondition
) {
552 // lead byte with no trail bytes
554 if(asciiBytes
[0x80]!=spanCondition
) {
565 // ASCII; or trail bytes with the result of contains(FFFD).
570 } else if(++s
==limit
) {
579 } else if(++s
==limit
) {
586 ++s
; // Advance past the lead byte.
589 if( /* handle U+0000..U+FFFF inline */
590 (t1
=(uint8_t)(s
[0]-0x80)) <= 0x3f &&
591 (t2
=(uint8_t)(s
[1]-0x80)) <= 0x3f
594 uint32_t twoBits
=(bmpBlockBits
[t1
]>>b
)&0x10001;
596 // All 64 code points with this lead byte and middle trail byte
597 // are either in the set or not.
598 if(twoBits
!=(uint32_t)spanCondition
) {
602 // Look up the code point in its 4k block of code points.
603 UChar32 c
=(b
<<12)|(t1
<<6)|t2
;
604 if(containsSlow(c
, list4kStarts
[b
], list4kStarts
[b
+1]) != spanCondition
) {
611 } else if( /* handle U+10000..U+10FFFF inline */
612 (t1
=(uint8_t)(s
[0]-0x80)) <= 0x3f &&
613 (t2
=(uint8_t)(s
[1]-0x80)) <= 0x3f &&
614 (t3
=(uint8_t)(s
[2]-0x80)) <= 0x3f
616 // Give an illegal sequence the same value as the result of contains(FFFD).
617 UChar32 c
=((UChar32
)(b
-0xf0)<<18)|((UChar32
)t1
<<12)|(t2
<<6)|t3
;
618 if( ( (0x10000<=c
&& c
<=0x10ffff) ?
619 containsSlow(c
, list4kStarts
[0x10], list4kStarts
[0x11]) :
628 } else /* 0xc0<=b<0xe0 */ {
629 if( /* handle U+0000..U+07FF inline */
630 (t1
=(uint8_t)(*s
-0x80)) <= 0x3f
632 if((USetSpanCondition
)((table7FF
[t1
]&((uint32_t)1<<(b
&0x1f)))!=0) != spanCondition
) {
640 // Give an illegal sequence the same value as the result of contains(FFFD).
641 // Handle each byte of an illegal sequence separately to simplify the code;
642 // no need to optimize error handling.
643 if(asciiBytes
[0x80]!=spanCondition
) {
652 * While going backwards through UTF-8 optimize only for ASCII.
653 * Unlike UTF-16, UTF-8 is not forward-backward symmetrical, that is, it is not
654 * possible to tell from the last byte in a multi-byte sequence how many
655 * preceding bytes there should be. Therefore, going backwards through UTF-8
656 * is much harder than going forward.
659 BMPSet::spanBackUTF8(const uint8_t *s
, int32_t length
, USetSpanCondition spanCondition
) const {
660 if(spanCondition
!=USET_SPAN_NOT_CONTAINED
) {
661 spanCondition
=USET_SPAN_CONTAINED
; // Pin to 0/1 values.
674 } else if(length
==0) {
678 } while((int8_t)b
>=0);
683 } else if(length
==0) {
687 } while((int8_t)b
>=0);
693 // trail byte: collect a multi-byte character
694 // (or lead byte in last-trail position)
695 c
=utf8_prevCharSafeBody(s
, 0, &length
, b
, -3);
696 // c is a valid code point, not ASCII, not a surrogate
698 if((USetSpanCondition
)((table7FF
[c
&0x3f]&((uint32_t)1<<(c
>>6)))!=0) != spanCondition
) {
701 } else if(c
<=0xffff) {
703 uint32_t twoBits
=(bmpBlockBits
[(c
>>6)&0x3f]>>lead
)&0x10001;
705 // All 64 code points with the same bits 15..6
706 // are either in the set or not.
707 if(twoBits
!=(uint32_t)spanCondition
) {
711 // Look up the code point in its 4k block of code points.
712 if(containsSlow(c
, list4kStarts
[lead
], list4kStarts
[lead
+1]) != spanCondition
) {
717 if(containsSlow(c
, list4kStarts
[0x10], list4kStarts
[0x11]) != spanCondition
) {