michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2011, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: * file name: patternprops.cpp michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2011mar13 michael@0: * created by: Markus W. Scherer michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "patternprops.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: /* michael@0: * One byte per Latin-1 character. michael@0: * Bit 0 is set if either Pattern property is true, michael@0: * bit 1 if Pattern_Syntax is true, michael@0: * bit 2 if Pattern_White_Space is true. michael@0: * That is, Pattern_Syntax is encoded as 3 and Pattern_White_Space as 5. michael@0: */ michael@0: static const uint8_t latin1[256]={ michael@0: // WS: 9..D michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 5, 5, 5, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: // WS: 20 Syntax: 21..2F michael@0: 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, michael@0: // Syntax: 3A..40 michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, michael@0: 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: // Syntax: 5B..5E michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, michael@0: // Syntax: 60 michael@0: 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: // Syntax: 7B..7E michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, michael@0: // WS: 85 michael@0: 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: // Syntax: A1..A7, A9, AB, AC, AE michael@0: 0, 3, 3, 3, 3, 3, 3, 3, 0, 3, 0, 3, 3, 0, 3, 0, michael@0: // Syntax: B0, B1, B6, BB, BF michael@0: 3, 3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 3, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: // Syntax: D7 michael@0: 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: // Syntax: F7 michael@0: 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0 michael@0: }; michael@0: michael@0: /* michael@0: * One byte per 32 characters from U+2000..U+303F indexing into michael@0: * a small table of 32-bit data words. michael@0: * The first two data words are all-zeros and all-ones. michael@0: */ michael@0: static const uint8_t index2000[130]={ michael@0: 2, 3, 4, 0, 0, 0, 0, 0, // 20xx michael@0: 0, 0, 0, 0, 5, 1, 1, 1, // 21xx michael@0: 1, 1, 1, 1, 1, 1, 1, 1, // 22xx michael@0: 1, 1, 1, 1, 1, 1, 1, 1, // 23xx michael@0: 1, 1, 1, 0, 0, 0, 0, 0, // 24xx michael@0: 1, 1, 1, 1, 1, 1, 1, 1, // 25xx michael@0: 1, 1, 1, 1, 1, 1, 1, 1, // 26xx michael@0: 1, 1, 1, 6, 7, 1, 1, 1, // 27xx michael@0: 1, 1, 1, 1, 1, 1, 1, 1, // 28xx michael@0: 1, 1, 1, 1, 1, 1, 1, 1, // 29xx michael@0: 1, 1, 1, 1, 1, 1, 1, 1, // 2Axx michael@0: 1, 1, 1, 1, 1, 1, 1, 1, // 2Bxx michael@0: 0, 0, 0, 0, 0, 0, 0, 0, // 2Cxx michael@0: 0, 0, 0, 0, 0, 0, 0, 0, // 2Dxx michael@0: 1, 1, 1, 1, 0, 0, 0, 0, // 2Exx michael@0: 0, 0, 0, 0, 0, 0, 0, 0, // 2Fxx michael@0: 8, 9 // 3000..303F michael@0: }; michael@0: michael@0: /* michael@0: * One 32-bit integer per 32 characters. Ranges of all-false and all-true michael@0: * are mapped to the first two values, other ranges map to appropriate bit patterns. michael@0: */ michael@0: static const uint32_t syntax2000[]={ michael@0: 0, michael@0: 0xffffffff, michael@0: 0xffff0000, // 2: 2010..201F michael@0: 0x7fff00ff, // 3: 2020..2027, 2030..203E michael@0: 0x7feffffe, // 4: 2041..2053, 2055..205E michael@0: 0xffff0000, // 5: 2190..219F michael@0: 0x003fffff, // 6: 2760..2775 michael@0: 0xfff00000, // 7: 2794..279F michael@0: 0xffffff0e, // 8: 3001..3003, 3008..301F michael@0: 0x00010001 // 9: 3020, 3030 michael@0: }; michael@0: michael@0: /* michael@0: * Same as syntax2000, but with additional bits set for the michael@0: * Pattern_White_Space characters 200E 200F 2028 2029. michael@0: */ michael@0: static const uint32_t syntaxOrWhiteSpace2000[]={ michael@0: 0, michael@0: 0xffffffff, michael@0: 0xffffc000, // 2: 200E..201F michael@0: 0x7fff03ff, // 3: 2020..2029, 2030..203E michael@0: 0x7feffffe, // 4: 2041..2053, 2055..205E michael@0: 0xffff0000, // 5: 2190..219F michael@0: 0x003fffff, // 6: 2760..2775 michael@0: 0xfff00000, // 7: 2794..279F michael@0: 0xffffff0e, // 8: 3001..3003, 3008..301F michael@0: 0x00010001 // 9: 3020, 3030 michael@0: }; michael@0: michael@0: UBool michael@0: PatternProps::isSyntax(UChar32 c) { michael@0: if(c<0) { michael@0: return FALSE; michael@0: } else if(c<=0xff) { michael@0: return (UBool)(latin1[c]>>1)&1; michael@0: } else if(c<0x2010) { michael@0: return FALSE; michael@0: } else if(c<=0x3030) { michael@0: uint32_t bits=syntax2000[index2000[(c-0x2000)>>5]]; michael@0: return (UBool)((bits>>(c&0x1f))&1); michael@0: } else if(0xfd3e<=c && c<=0xfe46) { michael@0: return c<=0xfd3f || 0xfe45<=c; michael@0: } else { michael@0: return FALSE; michael@0: } michael@0: } michael@0: michael@0: UBool michael@0: PatternProps::isSyntaxOrWhiteSpace(UChar32 c) { michael@0: if(c<0) { michael@0: return FALSE; michael@0: } else if(c<=0xff) { michael@0: return (UBool)(latin1[c]&1); michael@0: } else if(c<0x200e) { michael@0: return FALSE; michael@0: } else if(c<=0x3030) { michael@0: uint32_t bits=syntaxOrWhiteSpace2000[index2000[(c-0x2000)>>5]]; michael@0: return (UBool)((bits>>(c&0x1f))&1); michael@0: } else if(0xfd3e<=c && c<=0xfe46) { michael@0: return c<=0xfd3f || 0xfe45<=c; michael@0: } else { michael@0: return FALSE; michael@0: } michael@0: } michael@0: michael@0: UBool michael@0: PatternProps::isWhiteSpace(UChar32 c) { michael@0: if(c<0) { michael@0: return FALSE; michael@0: } else if(c<=0xff) { michael@0: return (UBool)(latin1[c]>>2)&1; michael@0: } else if(0x200e<=c && c<=0x2029) { michael@0: return c<=0x200f || 0x2028<=c; michael@0: } else { michael@0: return FALSE; michael@0: } michael@0: } michael@0: michael@0: const UChar * michael@0: PatternProps::skipWhiteSpace(const UChar *s, int32_t length) { michael@0: while(length>0 && isWhiteSpace(*s)) { michael@0: ++s; michael@0: --length; michael@0: } michael@0: return s; michael@0: } michael@0: michael@0: const UChar * michael@0: PatternProps::trimWhiteSpace(const UChar *s, int32_t &length) { michael@0: if(length<=0 || (!isWhiteSpace(s[0]) && !isWhiteSpace(s[length-1]))) { michael@0: return s; michael@0: } michael@0: int32_t start=0; michael@0: int32_t limit=length; michael@0: while(start0 && !isSyntaxOrWhiteSpace(*s)) { michael@0: ++s; michael@0: --length; michael@0: } michael@0: return s; michael@0: } michael@0: michael@0: U_NAMESPACE_END