michael@0: // michael@0: // regexst.h michael@0: // michael@0: // Copyright (C) 2004-2013, International Business Machines Corporation and others. michael@0: // All Rights Reserved. michael@0: // michael@0: // This file contains class RegexStaticSets michael@0: // michael@0: // This class is internal to the regular expression implementation. michael@0: // For the public Regular Expression API, see the file "unicode/regex.h" michael@0: // michael@0: // RegexStaticSets groups together the common UnicodeSets that are needed michael@0: // for compiling or executing RegularExpressions. This grouping simplifies michael@0: // the thread safe lazy creation and sharing of these sets across michael@0: // all instances of regular expressions. michael@0: // michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_REGULAR_EXPRESSIONS michael@0: michael@0: #include "unicode/unistr.h" michael@0: #include "unicode/uniset.h" michael@0: #include "unicode/uchar.h" michael@0: #include "unicode/regex.h" michael@0: #include "uprops.h" michael@0: #include "cmemory.h" michael@0: #include "cstring.h" michael@0: #include "uassert.h" michael@0: #include "ucln_in.h" michael@0: #include "umutex.h" michael@0: michael@0: #include "regexcst.h" // Contains state table for the regex pattern parser. michael@0: // generated by a Perl script. michael@0: #include "regexst.h" michael@0: michael@0: michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: // michael@0: // Unicode Set pattern strings for all of the required constant sets. michael@0: // Initialized with hex values for portability to EBCDIC based machines. michael@0: // Really ugly, but there's no good way to avoid it. michael@0: // michael@0: //------------------------------------------------------------------------------ michael@0: michael@0: // "Rule Char" Characters are those with no special meaning, and therefore do not michael@0: // need to be escaped to appear as literals in a regexp. Expressed michael@0: // as the inverse of those needing escaping -- [^\*\?\+\[\(\)\{\}\^\$\|\\\.] michael@0: static const UChar gRuleSet_rule_char_pattern[] = { michael@0: // [ ^ \ * \ ? \ + \ [ \ ( / ) michael@0: 0x5b, 0x5e, 0x5c, 0x2a, 0x5c, 0x3f, 0x5c, 0x2b, 0x5c, 0x5b, 0x5c, 0x28, 0x5c, 0x29, michael@0: // \ { \ } \ ^ \ $ \ | \ \ \ . ] michael@0: 0x5c, 0x7b,0x5c, 0x7d, 0x5c, 0x5e, 0x5c, 0x24, 0x5c, 0x7c, 0x5c, 0x5c, 0x5c, 0x2e, 0x5d, 0}; michael@0: michael@0: michael@0: static const UChar gRuleSet_digit_char_pattern[] = { michael@0: // [ 0 - 9 ] michael@0: 0x5b, 0x30, 0x2d, 0x39, 0x5d, 0}; michael@0: michael@0: // michael@0: // Here are the backslash escape characters that ICU's unescape() function michael@0: // will handle. michael@0: // michael@0: static const UChar gUnescapeCharPattern[] = { michael@0: // [ a c e f n r t u U x ] michael@0: 0x5b, 0x61, 0x63, 0x65, 0x66, 0x6e, 0x72, 0x74, 0x75, 0x55, 0x78, 0x5d, 0}; michael@0: michael@0: michael@0: // michael@0: // Unicode Set Definitions for Regular Expression \w michael@0: // michael@0: static const UChar gIsWordPattern[] = { michael@0: // [ \ p { A l p h a b e t i c } michael@0: 0x5b, 0x5c, 0x70, 0x7b, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x62, 0x65, 0x74, 0x69, 0x63, 0x7d, michael@0: // \ p { M } Mark michael@0: 0x5c, 0x70, 0x7b, 0x4d, 0x7d, michael@0: // \ p { N d } Digit_Numeric michael@0: 0x5c, 0x70, 0x7b, 0x4e, 0x64, 0x7d, michael@0: // \ p { P c } Connector_Punctuation michael@0: 0x5c, 0x70, 0x7b, 0x50, 0x63, 0x7d, michael@0: // \ u 2 0 0 c \ u 2 0 0 d ] michael@0: 0x5c, 0x75, 0x32, 0x30, 0x30, 0x63, 0x5c, 0x75, 0x32, 0x30, 0x30, 0x64, 0x5d, 0}; michael@0: michael@0: michael@0: // michael@0: // Unicode Set Definitions for Regular Expression \s michael@0: // michael@0: static const UChar gIsSpacePattern[] = { michael@0: // [ \ p { W h i t e S p a c e } ] michael@0: 0x5b, 0x5c, 0x70, 0x7b, 0x57, 0x68, 0x69, 0x74, 0x65, 0x53, 0x70, 0x61, 0x63, 0x65, 0x7d, 0x5d, 0}; michael@0: michael@0: michael@0: // michael@0: // UnicodeSets used in implementation of Grapheme Cluster detection, \X michael@0: // michael@0: static const UChar gGC_ControlPattern[] = { michael@0: // [ [ : Z l : ] [ : Z p : ] michael@0: 0x5b, 0x5b, 0x3a, 0x5A, 0x6c, 0x3a, 0x5d, 0x5b, 0x3a, 0x5A, 0x70, 0x3a, 0x5d, michael@0: // [ : C c : ] [ : C f : ] - michael@0: 0x5b, 0x3a, 0x43, 0x63, 0x3a, 0x5d, 0x5b, 0x3a, 0x43, 0x66, 0x3a, 0x5d, 0x2d, michael@0: // [ : G r a p h e m e _ michael@0: 0x5b, 0x3a, 0x47, 0x72, 0x61, 0x70, 0x68, 0x65, 0x6d, 0x65, 0x5f, michael@0: // E x t e n d : ] ] michael@0: 0x45, 0x78, 0x74, 0x65, 0x6e, 0x64, 0x3a, 0x5d, 0x5d, 0}; michael@0: michael@0: static const UChar gGC_ExtendPattern[] = { michael@0: // [ \ p { G r a p h e m e _ michael@0: 0x5b, 0x5c, 0x70, 0x7b, 0x47, 0x72, 0x61, 0x70, 0x68, 0x65, 0x6d, 0x65, 0x5f, michael@0: // E x t e n d } ] michael@0: 0x45, 0x78, 0x74, 0x65, 0x6e, 0x64, 0x7d, 0x5d, 0}; michael@0: michael@0: static const UChar gGC_LPattern[] = { michael@0: // [ \ p { H a n g u l _ S y l michael@0: 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c, michael@0: // l a b l e _ T y p e = L } ] michael@0: 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x7d, 0x5d, 0}; michael@0: michael@0: static const UChar gGC_VPattern[] = { michael@0: // [ \ p { H a n g u l _ S y l michael@0: 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c, michael@0: // l a b l e _ T y p e = V } ] michael@0: 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x56, 0x7d, 0x5d, 0}; michael@0: michael@0: static const UChar gGC_TPattern[] = { michael@0: // [ \ p { H a n g u l _ S y l michael@0: 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c, michael@0: // l a b l e _ T y p e = T } ] michael@0: 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x54, 0x7d, 0x5d, 0}; michael@0: michael@0: static const UChar gGC_LVPattern[] = { michael@0: // [ \ p { H a n g u l _ S y l michael@0: 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c, michael@0: // l a b l e _ T y p e = L V } ] michael@0: 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x56, 0x7d, 0x5d, 0}; michael@0: michael@0: static const UChar gGC_LVTPattern[] = { michael@0: // [ \ p { H a n g u l _ S y l michael@0: 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c, michael@0: // l a b l e _ T y p e = L V T } ] michael@0: 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x56, 0x54, 0x7d, 0x5d, 0}; michael@0: michael@0: michael@0: RegexStaticSets *RegexStaticSets::gStaticSets = NULL; michael@0: UInitOnce gStaticSetsInitOnce = U_INITONCE_INITIALIZER; michael@0: michael@0: RegexStaticSets::RegexStaticSets(UErrorCode *status) michael@0: : michael@0: fUnescapeCharSet(UnicodeString(TRUE, gUnescapeCharPattern, -1), *status), michael@0: fRuleDigitsAlias(NULL), michael@0: fEmptyText(NULL) michael@0: { michael@0: // First zero out everything michael@0: int i; michael@0: for (i=0; iremove(0xac00, 0xd7a4); michael@0: fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_CONTROL]); michael@0: fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_L]); michael@0: fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_V]); michael@0: fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_T]); michael@0: michael@0: // Initialize the 8-bit fast bit sets from the parallel full michael@0: // UnicodeSets. michael@0: for (i=0; icompact(); michael@0: fPropSets8[i].init(fPropSets[i]); michael@0: } michael@0: } michael@0: michael@0: // Sets used while parsing rules, but not referenced from the parse state table michael@0: fRuleSets[kRuleSet_rule_char-128] = UnicodeSet(UnicodeString(TRUE, gRuleSet_rule_char_pattern, -1), *status); michael@0: fRuleSets[kRuleSet_digit_char-128] = UnicodeSet(UnicodeString(TRUE, gRuleSet_digit_char_pattern, -1), *status); michael@0: fRuleDigitsAlias = &fRuleSets[kRuleSet_digit_char-128]; michael@0: for (i=0; i<(int32_t)(sizeof(fRuleSets)/sizeof(fRuleSets[0])); i++) { michael@0: fRuleSets[i].compact(); michael@0: } michael@0: michael@0: // Finally, initialize an empty string for utility purposes michael@0: fEmptyText = utext_openUChars(NULL, NULL, 0, status); michael@0: michael@0: return; // If we reached this point, everything is fine so just exit michael@0: michael@0: ExitConstrDeleteAll: // Remove fPropSets and fRuleSets and return error michael@0: for (i=0; i