1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
6 * Copyright (C) 2007-2012, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 ******************************************************************************
10 * file name: bmpset.cpp
12 * tab size: 8 (not used)
15 * created on: 2007jan29
16 * created by: Markus W. Scherer
19 #include "unicode/utypes.h"
20 #include "unicode/uniset.h"
21 #include "unicode/utf8.h"
22 #include "unicode/utf16.h"
29 BMPSet::BMPSet(const int32_t *parentList
, int32_t parentListLength
) :
30 list(parentList
), listLength(parentListLength
) {
31 uprv_memset(asciiBytes
, 0, sizeof(asciiBytes
));
32 uprv_memset(table7FF
, 0, sizeof(table7FF
));
33 uprv_memset(bmpBlockBits
, 0, sizeof(bmpBlockBits
));
36 * Set the list indexes for binary searches for
37 * U+0800, U+1000, U+2000, .., U+F000, U+10000.
38 * U+0800 is the first 3-byte-UTF-8 code point. Lower code points are
39 * looked up in the bit tables.
40 * The last pair of indexes is for finding supplementary code points.
42 list4kStarts
[0]=findCodePoint(0x800, 0, listLength
-1);
44 for(i
=1; i
<=0x10; ++i
) {
45 list4kStarts
[i
]=findCodePoint(i
<<12, list4kStarts
[i
-1], listLength
-1);
47 list4kStarts
[0x11]=listLength
-1;
53 BMPSet::BMPSet(const BMPSet
&otherBMPSet
, const int32_t *newParentList
, int32_t newParentListLength
) :
54 list(newParentList
), listLength(newParentListLength
) {
55 uprv_memcpy(asciiBytes
, otherBMPSet
.asciiBytes
, sizeof(asciiBytes
));
56 uprv_memcpy(table7FF
, otherBMPSet
.table7FF
, sizeof(table7FF
));
57 uprv_memcpy(bmpBlockBits
, otherBMPSet
.bmpBlockBits
, sizeof(bmpBlockBits
));
58 uprv_memcpy(list4kStarts
, otherBMPSet
.list4kStarts
, sizeof(list4kStarts
));
65 * Set bits in a bit rectangle in "vertical" bit organization.
68 static void set32x64Bits(uint32_t table
[64], int32_t start
, int32_t limit
) {
69 U_ASSERT(start
<limit
);
70 U_ASSERT(limit
<=0x800);
72 int32_t lead
=start
>>6; // Named for UTF-8 2-byte lead byte with upper 5 bits.
73 int32_t trail
=start
&0x3f; // Named for UTF-8 2-byte trail byte with lower 6 bits.
75 // Set one bit indicating an all-one block.
76 uint32_t bits
=(uint32_t)1<<lead
;
77 if((start
+1)==limit
) { // Single-character shortcut.
82 int32_t limitLead
=limit
>>6;
83 int32_t limitTrail
=limit
&0x3f;
86 // Partial vertical bit column.
87 while(trail
<limitTrail
) {
91 // Partial vertical bit column,
92 // followed by a bit rectangle,
93 // followed by another partial vertical bit column.
103 bits
&=(1<<limitLead
)-1;
105 for(trail
=0; trail
<64; ++trail
) {
109 // limit<=0x800. If limit==0x800 then limitLead=32 and limitTrail=0.
110 // In that case, bits=1<<limitLead is undefined but the bits value
111 // is not used because trail<limitTrail is already false.
112 bits
=(uint32_t)1<<((limitLead
== 0x20) ? (limitLead
- 1) : limitLead
);
113 for(trail
=0; trail
<limitTrail
; ++trail
) {
119 void BMPSet::initBits() {
120 UChar32 start
, limit
;
125 start
=list
[listIndex
++];
126 if(listIndex
<listLength
) {
127 limit
=list
[listIndex
++];
135 asciiBytes
[start
++]=1;
136 } while(start
<limit
&& start
<0x80);
137 } while(limit
<=0x80);
141 set32x64Bits(table7FF
, start
, limit
<=0x800 ? limit
: 0x800);
147 start
=list
[listIndex
++];
148 if(listIndex
<listLength
) {
149 limit
=list
[listIndex
++];
155 // Set bmpBlockBits[].
156 int32_t minStart
=0x800;
157 while(start
<0x10000) {
165 if(start
<limit
) { // Else: Another range entirely in a known mixed-value block.
167 // Mixed-value block of 64 code points.
169 bmpBlockBits
[start
&0x3f]|=0x10001<<(start
>>6);
170 start
=(start
+1)<<6; // Round up to the next block boundary.
171 minStart
=start
; // Ignore further ranges in this block.
174 if(start
<(limit
&~0x3f)) {
175 // Multiple all-ones blocks of 64 code points each.
176 set32x64Bits(bmpBlockBits
, start
>>6, limit
>>6);
180 // Mixed-value block of 64 code points.
182 bmpBlockBits
[limit
&0x3f]|=0x10001<<(limit
>>6);
183 limit
=(limit
+1)<<6; // Round up to the next block boundary.
184 minStart
=limit
; // Ignore further ranges in this block.
193 start
=list
[listIndex
++];
194 if(listIndex
<listLength
) {
195 limit
=list
[listIndex
++];
203 * Override some bits and bytes to the result of contains(FFFD)
204 * for faster validity checking at runtime.
205 * No need to set 0 values where they were reset to 0 in the constructor
206 * and not modified by initBits().
207 * (asciiBytes[] trail bytes, table7FF[] 0..7F, bmpBlockBits[] 0..7FF)
208 * Need to set 0 values for surrogates D800..DFFF.
210 void BMPSet::overrideIllegal() {
214 if(containsSlow(0xfffd, list4kStarts
[0xf], list4kStarts
[0x10])) {
215 // contains(FFFD)==TRUE
216 for(i
=0x80; i
<0xc0; ++i
) {
220 bits
=3; // Lead bytes 0xC0 and 0xC1.
221 for(i
=0; i
<64; ++i
) {
225 bits
=1; // Lead byte 0xE0.
226 for(i
=0; i
<32; ++i
) { // First half of 4k block.
227 bmpBlockBits
[i
]|=bits
;
230 mask
=~(0x10001<<0xd); // Lead byte 0xED.
232 for(i
=32; i
<64; ++i
) { // Second half of 4k block.
233 bmpBlockBits
[i
]=(bmpBlockBits
[i
]&mask
)|bits
;
236 // contains(FFFD)==FALSE
237 mask
=~(0x10001<<0xd); // Lead byte 0xED.
238 for(i
=32; i
<64; ++i
) { // Second half of 4k block.
239 bmpBlockBits
[i
]&=mask
;
244 int32_t BMPSet::findCodePoint(UChar32 c
, int32_t lo
, int32_t hi
) const {
247 set list[] c=0 1 3 4 7 8
248 === ============== ===========
249 [] [110000] 0 0 0 0 0 0
250 [\u0000-\u0003] [0, 4, 110000] 1 1 1 2 2 2
251 [\u0004-\u0007] [4, 8, 110000] 0 0 0 1 1 2
252 [:Any:] [0, 110000] 1 1 1 1 1 1
255 // Return the smallest i such that c < list[i]. Assume
256 // list[len - 1] == HIGH and that c is legal (0..HIGH-1).
259 // High runner test. c is often after the last range, so an
260 // initial check for this condition pays off.
261 if (lo
>= hi
|| c
>= list
[hi
-1])
263 // invariant: c >= list[lo]
264 // invariant: c < list[hi]
266 int32_t i
= (lo
+ hi
) >> 1;
269 } else if (c
< list
[i
]) {
279 BMPSet::contains(UChar32 c
) const {
280 if((uint32_t)c
<=0x7f) {
281 return (UBool
)asciiBytes
[c
];
282 } else if((uint32_t)c
<=0x7ff) {
283 return (UBool
)((table7FF
[c
&0x3f]&((uint32_t)1<<(c
>>6)))!=0);
284 } else if((uint32_t)c
<0xd800 || (c
>=0xe000 && c
<=0xffff)) {
286 uint32_t twoBits
=(bmpBlockBits
[(c
>>6)&0x3f]>>lead
)&0x10001;
288 // All 64 code points with the same bits 15..6
289 // are either in the set or not.
290 return (UBool
)twoBits
;
292 // Look up the code point in its 4k block of code points.
293 return containsSlow(c
, list4kStarts
[lead
], list4kStarts
[lead
+1]);
295 } else if((uint32_t)c
<=0x10ffff) {
296 // surrogate or supplementary code point
297 return containsSlow(c
, list4kStarts
[0xd], list4kStarts
[0x11]);
299 // Out-of-range code points get FALSE, consistent with long-standing
300 // behavior of UnicodeSet::contains(c).
306 * Check for sufficient length for trail unit for each surrogate pair.
307 * Handle single surrogates as surrogate code points as usual in ICU.
310 BMPSet::span(const UChar
*s
, const UChar
*limit
, USetSpanCondition spanCondition
) const {
321 } else if(c
<=0x7ff) {
322 if((table7FF
[c
&0x3f]&((uint32_t)1<<(c
>>6)))==0) {
325 } else if(c
<0xd800 || c
>=0xe000) {
327 uint32_t twoBits
=(bmpBlockBits
[(c
>>6)&0x3f]>>lead
)&0x10001;
329 // All 64 code points with the same bits 15..6
330 // are either in the set or not.
335 // Look up the code point in its 4k block of code points.
336 if(!containsSlow(c
, list4kStarts
[lead
], list4kStarts
[lead
+1])) {
340 } else if(c
>=0xdc00 || (s
+1)==limit
|| (c2
=s
[1])<0xdc00 || c2
>=0xe000) {
341 // surrogate code point
342 if(!containsSlow(c
, list4kStarts
[0xd], list4kStarts
[0xe])) {
347 if(!containsSlow(U16_GET_SUPPLEMENTARY(c
, c2
), list4kStarts
[0x10], list4kStarts
[0x11])) {
361 } else if(c
<=0x7ff) {
362 if((table7FF
[c
&0x3f]&((uint32_t)1<<(c
>>6)))!=0) {
365 } else if(c
<0xd800 || c
>=0xe000) {
367 uint32_t twoBits
=(bmpBlockBits
[(c
>>6)&0x3f]>>lead
)&0x10001;
369 // All 64 code points with the same bits 15..6
370 // are either in the set or not.
375 // Look up the code point in its 4k block of code points.
376 if(containsSlow(c
, list4kStarts
[lead
], list4kStarts
[lead
+1])) {
380 } else if(c
>=0xdc00 || (s
+1)==limit
|| (c2
=s
[1])<0xdc00 || c2
>=0xe000) {
381 // surrogate code point
382 if(containsSlow(c
, list4kStarts
[0xd], list4kStarts
[0xe])) {
387 if(containsSlow(U16_GET_SUPPLEMENTARY(c
, c2
), list4kStarts
[0x10], list4kStarts
[0x11])) {
397 /* Symmetrical with span(). */
399 BMPSet::spanBack(const UChar
*s
, const UChar
*limit
, USetSpanCondition spanCondition
) const {
410 } else if(c
<=0x7ff) {
411 if((table7FF
[c
&0x3f]&((uint32_t)1<<(c
>>6)))==0) {
414 } else if(c
<0xd800 || c
>=0xe000) {
416 uint32_t twoBits
=(bmpBlockBits
[(c
>>6)&0x3f]>>lead
)&0x10001;
418 // All 64 code points with the same bits 15..6
419 // are either in the set or not.
424 // Look up the code point in its 4k block of code points.
425 if(!containsSlow(c
, list4kStarts
[lead
], list4kStarts
[lead
+1])) {
429 } else if(c
<0xdc00 || s
==limit
|| (c2
=*(limit
-1))<0xd800 || c2
>=0xdc00) {
430 // surrogate code point
431 if(!containsSlow(c
, list4kStarts
[0xd], list4kStarts
[0xe])) {
436 if(!containsSlow(U16_GET_SUPPLEMENTARY(c2
, c
), list4kStarts
[0x10], list4kStarts
[0x11])) {
453 } else if(c
<=0x7ff) {
454 if((table7FF
[c
&0x3f]&((uint32_t)1<<(c
>>6)))!=0) {
457 } else if(c
<0xd800 || c
>=0xe000) {
459 uint32_t twoBits
=(bmpBlockBits
[(c
>>6)&0x3f]>>lead
)&0x10001;
461 // All 64 code points with the same bits 15..6
462 // are either in the set or not.
467 // Look up the code point in its 4k block of code points.
468 if(containsSlow(c
, list4kStarts
[lead
], list4kStarts
[lead
+1])) {
472 } else if(c
<0xdc00 || s
==limit
|| (c2
=*(limit
-1))<0xd800 || c2
>=0xdc00) {
473 // surrogate code point
474 if(containsSlow(c
, list4kStarts
[0xd], list4kStarts
[0xe])) {
479 if(containsSlow(U16_GET_SUPPLEMENTARY(c2
, c
), list4kStarts
[0x10], list4kStarts
[0x11])) {
493 * Precheck for sufficient trail bytes at end of string only once per span.
497 BMPSet::spanUTF8(const uint8_t *s
, int32_t length
, USetSpanCondition spanCondition
) const {
498 const uint8_t *limit
=s
+length
;
501 // Initial all-ASCII span.
504 if(!asciiBytes
[b
] || ++s
==limit
) {
508 } while((int8_t)b
>=0);
511 if(asciiBytes
[b
] || ++s
==limit
) {
515 } while((int8_t)b
>=0);
517 length
=(int32_t)(limit
-s
);
520 if(spanCondition
!=USET_SPAN_NOT_CONTAINED
) {
521 spanCondition
=USET_SPAN_CONTAINED
; // Pin to 0/1 values.
524 const uint8_t *limit0
=limit
;
527 * Make sure that the last 1/2/3/4-byte sequence before limit is complete
528 * or runs into a lead byte.
529 * In the span loop compare s with limit only once
530 * per multi-byte character.
532 * Give a trailing illegal sequence the same value as the result of contains(FFFD),
533 * including it if that is part of the span, otherwise set limit0 to before
534 * the truncated sequence.
538 // b>=0x80: lead or trail byte
540 // single trail byte, check for preceding 3- or 4-byte lead byte
541 if(length
>=2 && (b
=*(limit
-2))>=0xe0) {
543 if(asciiBytes
[0x80]!=spanCondition
) {
546 } else if(b
<0xc0 && b
>=0x80 && length
>=3 && (b
=*(limit
-3))>=0xf0) {
547 // 4-byte lead byte with only two trail bytes
549 if(asciiBytes
[0x80]!=spanCondition
) {
554 // lead byte with no trail bytes
556 if(asciiBytes
[0x80]!=spanCondition
) {
567 // ASCII; or trail bytes with the result of contains(FFFD).
572 } else if(++s
==limit
) {
581 } else if(++s
==limit
) {
588 ++s
; // Advance past the lead byte.
591 if( /* handle U+0000..U+FFFF inline */
592 (t1
=(uint8_t)(s
[0]-0x80)) <= 0x3f &&
593 (t2
=(uint8_t)(s
[1]-0x80)) <= 0x3f
596 uint32_t twoBits
=(bmpBlockBits
[t1
]>>b
)&0x10001;
598 // All 64 code points with this lead byte and middle trail byte
599 // are either in the set or not.
600 if(twoBits
!=(uint32_t)spanCondition
) {
604 // Look up the code point in its 4k block of code points.
605 UChar32 c
=(b
<<12)|(t1
<<6)|t2
;
606 if(containsSlow(c
, list4kStarts
[b
], list4kStarts
[b
+1]) != spanCondition
) {
613 } else if( /* handle U+10000..U+10FFFF inline */
614 (t1
=(uint8_t)(s
[0]-0x80)) <= 0x3f &&
615 (t2
=(uint8_t)(s
[1]-0x80)) <= 0x3f &&
616 (t3
=(uint8_t)(s
[2]-0x80)) <= 0x3f
618 // Give an illegal sequence the same value as the result of contains(FFFD).
619 UChar32 c
=((UChar32
)(b
-0xf0)<<18)|((UChar32
)t1
<<12)|(t2
<<6)|t3
;
620 if( ( (0x10000<=c
&& c
<=0x10ffff) ?
621 containsSlow(c
, list4kStarts
[0x10], list4kStarts
[0x11]) :
630 } else /* 0xc0<=b<0xe0 */ {
631 if( /* handle U+0000..U+07FF inline */
632 (t1
=(uint8_t)(*s
-0x80)) <= 0x3f
634 if((USetSpanCondition
)((table7FF
[t1
]&((uint32_t)1<<(b
&0x1f)))!=0) != spanCondition
) {
642 // Give an illegal sequence the same value as the result of contains(FFFD).
643 // Handle each byte of an illegal sequence separately to simplify the code;
644 // no need to optimize error handling.
645 if(asciiBytes
[0x80]!=spanCondition
) {
654 * While going backwards through UTF-8 optimize only for ASCII.
655 * Unlike UTF-16, UTF-8 is not forward-backward symmetrical, that is, it is not
656 * possible to tell from the last byte in a multi-byte sequence how many
657 * preceding bytes there should be. Therefore, going backwards through UTF-8
658 * is much harder than going forward.
661 BMPSet::spanBackUTF8(const uint8_t *s
, int32_t length
, USetSpanCondition spanCondition
) const {
662 if(spanCondition
!=USET_SPAN_NOT_CONTAINED
) {
663 spanCondition
=USET_SPAN_CONTAINED
; // Pin to 0/1 values.
676 } else if(length
==0) {
680 } while((int8_t)b
>=0);
685 } else if(length
==0) {
689 } while((int8_t)b
>=0);
695 // trail byte: collect a multi-byte character
696 // (or lead byte in last-trail position)
697 c
=utf8_prevCharSafeBody(s
, 0, &length
, b
, -3);
698 // c is a valid code point, not ASCII, not a surrogate
700 if((USetSpanCondition
)((table7FF
[c
&0x3f]&((uint32_t)1<<(c
>>6)))!=0) != spanCondition
) {
703 } else if(c
<=0xffff) {
705 uint32_t twoBits
=(bmpBlockBits
[(c
>>6)&0x3f]>>lead
)&0x10001;
707 // All 64 code points with the same bits 15..6
708 // are either in the set or not.
709 if(twoBits
!=(uint32_t)spanCondition
) {
713 // Look up the code point in its 4k block of code points.
714 if(containsSlow(c
, list4kStarts
[lead
], list4kStarts
[lead
+1]) != spanCondition
) {
719 if(containsSlow(c
, list4kStarts
[0x10], list4kStarts
[0x11]) != spanCondition
) {