1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/common/bmpset.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,161 @@ 1.4 +/* 1.5 +****************************************************************************** 1.6 +* 1.7 +* Copyright (C) 2007, International Business Machines 1.8 +* Corporation and others. All Rights Reserved. 1.9 +* 1.10 +****************************************************************************** 1.11 +* file name: bmpset.h 1.12 +* encoding: US-ASCII 1.13 +* tab size: 8 (not used) 1.14 +* indentation:4 1.15 +* 1.16 +* created on: 2007jan29 1.17 +* created by: Markus W. Scherer 1.18 +*/ 1.19 + 1.20 +#ifndef __BMPSET_H__ 1.21 +#define __BMPSET_H__ 1.22 + 1.23 +#include "unicode/utypes.h" 1.24 +#include "unicode/uniset.h" 1.25 + 1.26 +U_NAMESPACE_BEGIN 1.27 + 1.28 +/* 1.29 + * Helper class for frozen UnicodeSets, implements contains() and span() 1.30 + * optimized for BMP code points. Structured to be UTF-8-friendly. 1.31 + * 1.32 + * ASCII: Look up bytes. 1.33 + * 2-byte characters: Bits organized vertically. 1.34 + * 3-byte characters: Use zero/one/mixed data per 64-block in U+0000..U+FFFF, 1.35 + * with mixed for illegal ranges. 1.36 + * Supplementary characters: Call contains() on the parent set. 1.37 + */ 1.38 +class BMPSet : public UMemory { 1.39 +public: 1.40 + BMPSet(const int32_t *parentList, int32_t parentListLength); 1.41 + BMPSet(const BMPSet &otherBMPSet, const int32_t *newParentList, int32_t newParentListLength); 1.42 + virtual ~BMPSet(); 1.43 + 1.44 + virtual UBool contains(UChar32 c) const; 1.45 + 1.46 + /* 1.47 + * Span the initial substring for which each character c has spanCondition==contains(c). 1.48 + * It must be s<limit and spanCondition==0 or 1. 1.49 + * @return The string pointer which limits the span. 1.50 + */ 1.51 + const UChar *span(const UChar *s, const UChar *limit, USetSpanCondition spanCondition) const; 1.52 + 1.53 + /* 1.54 + * Span the trailing substring for which each character c has spanCondition==contains(c). 1.55 + * It must be s<limit and spanCondition==0 or 1. 1.56 + * @return The string pointer which starts the span. 1.57 + */ 1.58 + const UChar *spanBack(const UChar *s, const UChar *limit, USetSpanCondition spanCondition) const; 1.59 + 1.60 + /* 1.61 + * Span the initial substring for which each character c has spanCondition==contains(c). 1.62 + * It must be length>0 and spanCondition==0 or 1. 1.63 + * @return The string pointer which limits the span. 1.64 + */ 1.65 + const uint8_t *spanUTF8(const uint8_t *s, int32_t length, USetSpanCondition spanCondition) const; 1.66 + 1.67 + /* 1.68 + * Span the trailing substring for which each character c has spanCondition==contains(c). 1.69 + * It must be length>0 and spanCondition==0 or 1. 1.70 + * @return The start of the span. 1.71 + */ 1.72 + int32_t spanBackUTF8(const uint8_t *s, int32_t length, USetSpanCondition spanCondition) const; 1.73 + 1.74 +private: 1.75 + void initBits(); 1.76 + void overrideIllegal(); 1.77 + 1.78 + /** 1.79 + * Same as UnicodeSet::findCodePoint(UChar32 c) const except that the 1.80 + * binary search is restricted for finding code points in a certain range. 1.81 + * 1.82 + * For restricting the search for finding in the range start..end, 1.83 + * pass in 1.84 + * lo=findCodePoint(start) and 1.85 + * hi=findCodePoint(end) 1.86 + * with 0<=lo<=hi<len. 1.87 + * findCodePoint(c) defaults to lo=0 and hi=len-1. 1.88 + * 1.89 + * @param c a character in a subrange of MIN_VALUE..MAX_VALUE 1.90 + * @param lo The lowest index to be returned. 1.91 + * @param hi The highest index to be returned. 1.92 + * @return the smallest integer i in the range lo..hi, 1.93 + * inclusive, such that c < list[i] 1.94 + */ 1.95 + int32_t findCodePoint(UChar32 c, int32_t lo, int32_t hi) const; 1.96 + 1.97 + inline UBool containsSlow(UChar32 c, int32_t lo, int32_t hi) const; 1.98 + 1.99 + /* 1.100 + * One byte per ASCII character, or trail byte in lead position. 1.101 + * 0 or 1 for ASCII characters. 1.102 + * The value for trail bytes is the result of contains(FFFD) 1.103 + * for faster validity checking at runtime. 1.104 + */ 1.105 + UBool asciiBytes[0xc0]; 1.106 + 1.107 + /* 1.108 + * One bit per code point from U+0000..U+07FF. 1.109 + * The bits are organized vertically; consecutive code points 1.110 + * correspond to the same bit positions in consecutive table words. 1.111 + * With code point parts 1.112 + * lead=c{10..6} 1.113 + * trail=c{5..0} 1.114 + * it is set.contains(c)==(table7FF[trail] bit lead) 1.115 + * 1.116 + * Bits for 0..7F (non-shortest forms) are set to the result of contains(FFFD) 1.117 + * for faster validity checking at runtime. 1.118 + */ 1.119 + uint32_t table7FF[64]; 1.120 + 1.121 + /* 1.122 + * One bit per 64 BMP code points. 1.123 + * The bits are organized vertically; consecutive 64-code point blocks 1.124 + * correspond to the same bit position in consecutive table words. 1.125 + * With code point parts 1.126 + * lead=c{15..12} 1.127 + * t1=c{11..6} 1.128 + * test bits (lead+16) and lead in bmpBlockBits[t1]. 1.129 + * If the upper bit is 0, then the lower bit indicates if contains(c) 1.130 + * for all code points in the 64-block. 1.131 + * If the upper bit is 1, then the block is mixed and set.contains(c) 1.132 + * must be called. 1.133 + * 1.134 + * Bits for 0..7FF (non-shortest forms) and D800..DFFF are set to 1.135 + * the result of contains(FFFD) for faster validity checking at runtime. 1.136 + */ 1.137 + uint32_t bmpBlockBits[64]; 1.138 + 1.139 + /* 1.140 + * Inversion list indexes for restricted binary searches in 1.141 + * findCodePoint(), from 1.142 + * findCodePoint(U+0800, U+1000, U+2000, .., U+F000, U+10000). 1.143 + * U+0800 is the first 3-byte-UTF-8 code point. Code points below U+0800 are 1.144 + * always looked up in the bit tables. 1.145 + * The last pair of indexes is for finding supplementary code points. 1.146 + */ 1.147 + int32_t list4kStarts[18]; 1.148 + 1.149 + /* 1.150 + * The inversion list of the parent set, for the slower contains() implementation 1.151 + * for mixed BMP blocks and for supplementary code points. 1.152 + * The list is terminated with list[listLength-1]=0x110000. 1.153 + */ 1.154 + const int32_t *list; 1.155 + int32_t listLength; 1.156 +}; 1.157 + 1.158 +inline UBool BMPSet::containsSlow(UChar32 c, int32_t lo, int32_t hi) const { 1.159 + return (UBool)(findCodePoint(c, lo, hi) & 1); 1.160 +} 1.161 + 1.162 +U_NAMESPACE_END 1.163 + 1.164 +#endif