2 ******************************************************************************
4 * Copyright (C) 2009-2011, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
10 package com.ibm.icu.impl;
12 import com.ibm.icu.text.UnicodeSet.SpanCondition;
15 * Helper class for frozen UnicodeSets, implements contains() and span() optimized for BMP code points.
17 * Latin-1: Look up bytes. 2-byte characters: Bits organized vertically. 3-byte characters: Use zero/one/mixed data
18 * per 64-block in U+0000..U+FFFF, with mixed for illegal ranges. Supplementary characters: Call contains() on the
21 public final class BMPSet {
22 public static int U16_SURROGATE_OFFSET = ((0xd800 << 10) + 0xdc00 - 0x10000);
25 * One boolean ('true' or 'false') per Latin-1 character.
27 private boolean[] latin1Contains;
30 * One bit per code point from U+0000..U+07FF. The bits are organized vertically; consecutive code points
31 * correspond to the same bit positions in consecutive table words. With code point parts lead=c{10..6}
32 * trail=c{5..0} it is set.contains(c)==(table7FF[trail] bit lead)
34 * Bits for 0..7F (non-shortest forms) are set to the result of contains(FFFD) for faster validity checking at
37 private int[] table7FF;
40 * One bit per 64 BMP code points. The bits are organized vertically; consecutive 64-code point blocks
41 * correspond to the same bit position in consecutive table words. With code point parts lead=c{15..12}
42 * t1=c{11..6} test bits (lead+16) and lead in bmpBlockBits[t1]. If the upper bit is 0, then the lower bit
43 * indicates if contains(c) for all code points in the 64-block. If the upper bit is 1, then the block is mixed
44 * and set.contains(c) must be called.
46 * Bits for 0..7FF (non-shortest forms) and D800..DFFF are set to the result of contains(FFFD) for faster
47 * validity checking at runtime.
49 private int[] bmpBlockBits;
52 * Inversion list indexes for restricted binary searches in findCodePoint(), from findCodePoint(U+0800, U+1000,
53 * U+2000, .., U+F000, U+10000). U+0800 is the first 3-byte-UTF-8 code point. Code points below U+0800 are
54 * always looked up in the bit tables. The last pair of indexes is for finding supplementary code points.
56 private int[] list4kStarts;
59 * The inversion list of the parent set, for the slower contains() implementation for mixed BMP blocks and for
60 * supplementary code points. The list is terminated with list[listLength-1]=0x110000.
62 private final int[] list;
63 private final int listLength; // length used; list may be longer to minimize reallocs
65 public BMPSet(final int[] parentList, int parentListLength) {
67 listLength = parentListLength;
68 latin1Contains = new boolean[0x100];
69 table7FF = new int[64];
70 bmpBlockBits = new int[64];
71 list4kStarts = new int[18];
74 * Set the list indexes for binary searches for U+0800, U+1000, U+2000, .., U+F000, U+10000. U+0800 is the
75 * first 3-byte-UTF-8 code point. Lower code points are looked up in the bit tables. The last pair of
76 * indexes is for finding supplementary code points.
78 list4kStarts[0] = findCodePoint(0x800, 0, listLength - 1);
80 for (i = 1; i <= 0x10; ++i) {
81 list4kStarts[i] = findCodePoint(i << 12, list4kStarts[i - 1], listLength - 1);
83 list4kStarts[0x11] = listLength - 1;
88 public BMPSet(final BMPSet otherBMPSet, final int[] newParentList, int newParentListLength) {
90 listLength = newParentListLength;
91 latin1Contains = otherBMPSet.latin1Contains.clone();
92 table7FF = otherBMPSet.table7FF.clone();
93 bmpBlockBits = otherBMPSet.bmpBlockBits.clone();
94 list4kStarts = otherBMPSet.list4kStarts.clone();
97 public boolean contains(int c) {
99 return (latin1Contains[c]);
100 } else if (c <= 0x7ff) {
101 return ((table7FF[c & 0x3f] & (1 << (c >> 6))) != 0);
102 } else if (c < 0xd800 || (c >= 0xe000 && c <= 0xffff)) {
104 int twoBits = (bmpBlockBits[(c >> 6) & 0x3f] >> lead) & 0x10001;
106 // All 64 code points with the same bits 15..6
107 // are either in the set or not.
108 return (0 != twoBits);
110 // Look up the code point in its 4k block of code points.
111 return containsSlow(c, list4kStarts[lead], list4kStarts[lead + 1]);
113 } else if (c <= 0x10ffff) {
114 // surrogate or supplementary code point
115 return containsSlow(c, list4kStarts[0xd], list4kStarts[0x11]);
117 // Out-of-range code points get false, consistent with long-standing
118 // behavior of UnicodeSet.contains(c).
124 * Span the initial substring for which each character c has spanCondition==contains(c). It must be
125 * spanCondition==0 or 1.
127 * @param start The start index
128 * @param end The end index
129 * @return The length of the span.
131 * NOTE: to reduce the overhead of function call to contains(c), it is manually inlined here. Check for
132 * sufficient length for trail unit for each surrogate pair. Handle single surrogates as surrogate code points
135 public final int span(CharSequence s, int start, int end, SpanCondition spanCondition) {
138 int limit = Math.min(s.length(), end);
139 if (SpanCondition.NOT_CONTAINED != spanCondition) {
144 if (!latin1Contains[c]) {
147 } else if (c <= 0x7ff) {
148 if ((table7FF[c & 0x3f] & (1 << (c >> 6))) == 0) {
151 } else if (c < 0xd800 ||
152 c >= 0xdc00 || (i + 1) == limit || (c2 = s.charAt(i + 1)) < 0xdc00 || c2 >= 0xe000) {
154 int twoBits = (bmpBlockBits[(c >> 6) & 0x3f] >> lead) & 0x10001;
156 // All 64 code points with the same bits 15..6
157 // are either in the set or not.
162 // Look up the code point in its 4k block of code points.
163 if (!containsSlow(c, list4kStarts[lead], list4kStarts[lead + 1])) {
169 int supplementary = UCharacterProperty.getRawSupplementary(c, c2);
170 if (!containsSlow(supplementary, list4kStarts[0x10], list4kStarts[0x11])) {
182 if (latin1Contains[c]) {
185 } else if (c <= 0x7ff) {
186 if ((table7FF[c & 0x3f] & (1 << (c >> 6))) != 0) {
189 } else if (c < 0xd800 ||
190 c >= 0xdc00 || (i + 1) == limit || (c2 = s.charAt(i + 1)) < 0xdc00 || c2 >= 0xe000) {
192 int twoBits = (bmpBlockBits[(c >> 6) & 0x3f] >> lead) & 0x10001;
194 // All 64 code points with the same bits 15..6
195 // are either in the set or not.
200 // Look up the code point in its 4k block of code points.
201 if (containsSlow(c, list4kStarts[lead], list4kStarts[lead + 1])) {
207 int supplementary = UCharacterProperty.getRawSupplementary(c, c2);
208 if (containsSlow(supplementary, list4kStarts[0x10], list4kStarts[0x11])) {
220 * Symmetrical with span().
221 * Span the trailing substring for which each character c has spanCondition==contains(c). It must be s.length >=
222 * limit and spanCondition==0 or 1.
224 * @return The string index which starts the span (i.e. inclusive).
226 public final int spanBack(CharSequence s, int limit, SpanCondition spanCondition) {
229 limit = Math.min(s.length(), limit);
230 if (SpanCondition.NOT_CONTAINED != spanCondition) {
233 c = s.charAt(--limit);
235 if (!latin1Contains[c]) {
238 } else if (c <= 0x7ff) {
239 if ((table7FF[c & 0x3f] & (1 << (c >> 6))) == 0) {
242 } else if (c < 0xd800 ||
243 c < 0xdc00 || 0 == limit || (c2 = s.charAt(limit - 1)) < 0xd800 || c2 >= 0xdc00) {
245 int twoBits = (bmpBlockBits[(c >> 6) & 0x3f] >> lead) & 0x10001;
247 // All 64 code points with the same bits 15..6
248 // are either in the set or not.
253 // Look up the code point in its 4k block of code points.
254 if (!containsSlow(c, list4kStarts[lead], list4kStarts[lead + 1])) {
260 int supplementary = UCharacterProperty.getRawSupplementary(c2, c);
261 if (!containsSlow(supplementary, list4kStarts[0x10], list4kStarts[0x11])) {
273 c = s.charAt(--limit);
275 if (latin1Contains[c]) {
278 } else if (c <= 0x7ff) {
279 if ((table7FF[c & 0x3f] & (1 << (c >> 6))) != 0) {
282 } else if (c < 0xd800 ||
283 c < 0xdc00 || 0 == limit || (c2 = s.charAt(limit - 1)) < 0xd800 || c2 >= 0xdc00) {
285 int twoBits = (bmpBlockBits[(c >> 6) & 0x3f] >> lead) & 0x10001;
287 // All 64 code points with the same bits 15..6
288 // are either in the set or not.
293 // Look up the code point in its 4k block of code points.
294 if (containsSlow(c, list4kStarts[lead], list4kStarts[lead + 1])) {
300 int supplementary = UCharacterProperty.getRawSupplementary(c2, c);
301 if (containsSlow(supplementary, list4kStarts[0x10], list4kStarts[0x11])) {
315 * Set bits in a bit rectangle in "vertical" bit organization. start<limit<=0x800
317 private static void set32x64Bits(int[] table, int start, int limit) {
318 assert (64 == table.length);
319 int lead = start >> 6; // Named for UTF-8 2-byte lead byte with upper 5 bits.
320 int trail = start & 0x3f; // Named for UTF-8 2-byte trail byte with lower 6 bits.
322 // Set one bit indicating an all-one block.
323 int bits = 1 << lead;
324 if ((start + 1) == limit) { // Single-character shortcut.
325 table[trail] |= bits;
329 int limitLead = limit >> 6;
330 int limitTrail = limit & 0x3f;
332 if (lead == limitLead) {
333 // Partial vertical bit column.
334 while (trail < limitTrail) {
335 table[trail++] |= bits;
338 // Partial vertical bit column,
339 // followed by a bit rectangle,
340 // followed by another partial vertical bit column.
343 table[trail++] |= bits;
344 } while (trail < 64);
347 if (lead < limitLead) {
348 bits = ~((1 << lead) - 1);
349 if (limitLead < 0x20) {
350 bits &= (1 << limitLead) - 1;
352 for (trail = 0; trail < 64; ++trail) {
353 table[trail] |= bits;
356 // limit<=0x800. If limit==0x800 then limitLead=32 and limitTrail=0.
357 // In that case, bits=1<<limitLead == 1<<0 == 1
358 // (because Java << uses only the lower 5 bits of the shift operand)
359 // but the bits value is not used because trail<limitTrail is already false.
360 bits = 1 << limitLead;
361 for (trail = 0; trail < limitTrail; ++trail) {
362 table[trail] |= bits;
367 private void initBits() {
371 // Set latin1Contains[].
373 start = list[listIndex++];
374 if (listIndex < listLength) {
375 limit = list[listIndex++];
379 if (start >= 0x100) {
383 latin1Contains[start++] = true;
384 } while (start < limit && start < 0x100);
385 } while (limit <= 0x100);
388 while (start < 0x800) {
389 set32x64Bits(table7FF, start, limit <= 0x800 ? limit : 0x800);
395 start = list[listIndex++];
396 if (listIndex < listLength) {
397 limit = list[listIndex++];
403 // Set bmpBlockBits[].
404 int minStart = 0x800;
405 while (start < 0x10000) {
406 if (limit > 0x10000) {
410 if (start < minStart) {
413 if (start < limit) { // Else: Another range entirely in a known mixed-value block.
414 if (0 != (start & 0x3f)) {
415 // Mixed-value block of 64 code points.
417 bmpBlockBits[start & 0x3f] |= 0x10001 << (start >> 6);
418 start = (start + 1) << 6; // Round up to the next block boundary.
419 minStart = start; // Ignore further ranges in this block.
422 if (start < (limit & ~0x3f)) {
423 // Multiple all-ones blocks of 64 code points each.
424 set32x64Bits(bmpBlockBits, start >> 6, limit >> 6);
427 if (0 != (limit & 0x3f)) {
428 // Mixed-value block of 64 code points.
430 bmpBlockBits[limit & 0x3f] |= 0x10001 << (limit >> 6);
431 limit = (limit + 1) << 6; // Round up to the next block boundary.
432 minStart = limit; // Ignore further ranges in this block.
437 if (limit == 0x10000) {
441 start = list[listIndex++];
442 if (listIndex < listLength) {
443 limit = list[listIndex++];
452 * Same as UnicodeSet.findCodePoint(int c) except that the binary search is restricted for finding code
453 * points in a certain range.
455 * For restricting the search for finding in the range start..end, pass in lo=findCodePoint(start) and
456 * hi=findCodePoint(end) with 0<=lo<=hi<len. findCodePoint(c) defaults to lo=0 and hi=len-1.
459 * a character in a subrange of MIN_VALUE..MAX_VALUE
461 * The lowest index to be returned.
463 * The highest index to be returned.
464 * @return the smallest integer i in the range lo..hi, inclusive, such that c < list[i]
466 private int findCodePoint(int c, int lo, int hi) {
469 set list[] c=0 1 3 4 7 8
470 === ============== ===========
471 [] [110000] 0 0 0 0 0 0
472 [\u0000-\u0003] [0, 4, 110000] 1 1 1 2 2 2
473 [\u0004-\u0007] [4, 8, 110000] 0 0 0 1 1 2
474 [:Any:] [0, 110000] 1 1 1 1 1 1
477 // Return the smallest i such that c < list[i]. Assume
478 // list[len - 1] == HIGH and that c is legal (0..HIGH-1).
481 // High runner test. c is often after the last range, so an
482 // initial check for this condition pays off.
483 if (lo >= hi || c >= list[hi - 1])
485 // invariant: c >= list[lo]
486 // invariant: c < list[hi]
488 int i = (lo + hi) >>> 1;
491 } else if (c < list[i]) {
500 private final boolean containsSlow(int c, int lo, int hi) {
501 return (0 != (findCodePoint(c, lo, hi) & 1));