intl/icu/source/i18n/regexst.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 //
michael@0 2 // regexst.h
michael@0 3 //
michael@0 4 // Copyright (C) 2004-2013, International Business Machines Corporation and others.
michael@0 5 // All Rights Reserved.
michael@0 6 //
michael@0 7 // This file contains class RegexStaticSets
michael@0 8 //
michael@0 9 // This class is internal to the regular expression implementation.
michael@0 10 // For the public Regular Expression API, see the file "unicode/regex.h"
michael@0 11 //
michael@0 12 // RegexStaticSets groups together the common UnicodeSets that are needed
michael@0 13 // for compiling or executing RegularExpressions. This grouping simplifies
michael@0 14 // the thread safe lazy creation and sharing of these sets across
michael@0 15 // all instances of regular expressions.
michael@0 16 //
michael@0 17 #include "unicode/utypes.h"
michael@0 18
michael@0 19 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
michael@0 20
michael@0 21 #include "unicode/unistr.h"
michael@0 22 #include "unicode/uniset.h"
michael@0 23 #include "unicode/uchar.h"
michael@0 24 #include "unicode/regex.h"
michael@0 25 #include "uprops.h"
michael@0 26 #include "cmemory.h"
michael@0 27 #include "cstring.h"
michael@0 28 #include "uassert.h"
michael@0 29 #include "ucln_in.h"
michael@0 30 #include "umutex.h"
michael@0 31
michael@0 32 #include "regexcst.h" // Contains state table for the regex pattern parser.
michael@0 33 // generated by a Perl script.
michael@0 34 #include "regexst.h"
michael@0 35
michael@0 36
michael@0 37
michael@0 38 U_NAMESPACE_BEGIN
michael@0 39
michael@0 40
michael@0 41 //------------------------------------------------------------------------------
michael@0 42 //
michael@0 43 // Unicode Set pattern strings for all of the required constant sets.
michael@0 44 // Initialized with hex values for portability to EBCDIC based machines.
michael@0 45 // Really ugly, but there's no good way to avoid it.
michael@0 46 //
michael@0 47 //------------------------------------------------------------------------------
michael@0 48
michael@0 49 // "Rule Char" Characters are those with no special meaning, and therefore do not
michael@0 50 // need to be escaped to appear as literals in a regexp. Expressed
michael@0 51 // as the inverse of those needing escaping -- [^\*\?\+\[\(\)\{\}\^\$\|\\\.]
michael@0 52 static const UChar gRuleSet_rule_char_pattern[] = {
michael@0 53 // [ ^ \ * \ ? \ + \ [ \ ( / )
michael@0 54 0x5b, 0x5e, 0x5c, 0x2a, 0x5c, 0x3f, 0x5c, 0x2b, 0x5c, 0x5b, 0x5c, 0x28, 0x5c, 0x29,
michael@0 55 // \ { \ } \ ^ \ $ \ | \ \ \ . ]
michael@0 56 0x5c, 0x7b,0x5c, 0x7d, 0x5c, 0x5e, 0x5c, 0x24, 0x5c, 0x7c, 0x5c, 0x5c, 0x5c, 0x2e, 0x5d, 0};
michael@0 57
michael@0 58
michael@0 59 static const UChar gRuleSet_digit_char_pattern[] = {
michael@0 60 // [ 0 - 9 ]
michael@0 61 0x5b, 0x30, 0x2d, 0x39, 0x5d, 0};
michael@0 62
michael@0 63 //
michael@0 64 // Here are the backslash escape characters that ICU's unescape() function
michael@0 65 // will handle.
michael@0 66 //
michael@0 67 static const UChar gUnescapeCharPattern[] = {
michael@0 68 // [ a c e f n r t u U x ]
michael@0 69 0x5b, 0x61, 0x63, 0x65, 0x66, 0x6e, 0x72, 0x74, 0x75, 0x55, 0x78, 0x5d, 0};
michael@0 70
michael@0 71
michael@0 72 //
michael@0 73 // Unicode Set Definitions for Regular Expression \w
michael@0 74 //
michael@0 75 static const UChar gIsWordPattern[] = {
michael@0 76 // [ \ p { A l p h a b e t i c }
michael@0 77 0x5b, 0x5c, 0x70, 0x7b, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x62, 0x65, 0x74, 0x69, 0x63, 0x7d,
michael@0 78 // \ p { M } Mark
michael@0 79 0x5c, 0x70, 0x7b, 0x4d, 0x7d,
michael@0 80 // \ p { N d } Digit_Numeric
michael@0 81 0x5c, 0x70, 0x7b, 0x4e, 0x64, 0x7d,
michael@0 82 // \ p { P c } Connector_Punctuation
michael@0 83 0x5c, 0x70, 0x7b, 0x50, 0x63, 0x7d,
michael@0 84 // \ u 2 0 0 c \ u 2 0 0 d ]
michael@0 85 0x5c, 0x75, 0x32, 0x30, 0x30, 0x63, 0x5c, 0x75, 0x32, 0x30, 0x30, 0x64, 0x5d, 0};
michael@0 86
michael@0 87
michael@0 88 //
michael@0 89 // Unicode Set Definitions for Regular Expression \s
michael@0 90 //
michael@0 91 static const UChar gIsSpacePattern[] = {
michael@0 92 // [ \ p { W h i t e S p a c e } ]
michael@0 93 0x5b, 0x5c, 0x70, 0x7b, 0x57, 0x68, 0x69, 0x74, 0x65, 0x53, 0x70, 0x61, 0x63, 0x65, 0x7d, 0x5d, 0};
michael@0 94
michael@0 95
michael@0 96 //
michael@0 97 // UnicodeSets used in implementation of Grapheme Cluster detection, \X
michael@0 98 //
michael@0 99 static const UChar gGC_ControlPattern[] = {
michael@0 100 // [ [ : Z l : ] [ : Z p : ]
michael@0 101 0x5b, 0x5b, 0x3a, 0x5A, 0x6c, 0x3a, 0x5d, 0x5b, 0x3a, 0x5A, 0x70, 0x3a, 0x5d,
michael@0 102 // [ : C c : ] [ : C f : ] -
michael@0 103 0x5b, 0x3a, 0x43, 0x63, 0x3a, 0x5d, 0x5b, 0x3a, 0x43, 0x66, 0x3a, 0x5d, 0x2d,
michael@0 104 // [ : G r a p h e m e _
michael@0 105 0x5b, 0x3a, 0x47, 0x72, 0x61, 0x70, 0x68, 0x65, 0x6d, 0x65, 0x5f,
michael@0 106 // E x t e n d : ] ]
michael@0 107 0x45, 0x78, 0x74, 0x65, 0x6e, 0x64, 0x3a, 0x5d, 0x5d, 0};
michael@0 108
michael@0 109 static const UChar gGC_ExtendPattern[] = {
michael@0 110 // [ \ p { G r a p h e m e _
michael@0 111 0x5b, 0x5c, 0x70, 0x7b, 0x47, 0x72, 0x61, 0x70, 0x68, 0x65, 0x6d, 0x65, 0x5f,
michael@0 112 // E x t e n d } ]
michael@0 113 0x45, 0x78, 0x74, 0x65, 0x6e, 0x64, 0x7d, 0x5d, 0};
michael@0 114
michael@0 115 static const UChar gGC_LPattern[] = {
michael@0 116 // [ \ p { H a n g u l _ S y l
michael@0 117 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
michael@0 118 // l a b l e _ T y p e = L } ]
michael@0 119 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x7d, 0x5d, 0};
michael@0 120
michael@0 121 static const UChar gGC_VPattern[] = {
michael@0 122 // [ \ p { H a n g u l _ S y l
michael@0 123 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
michael@0 124 // l a b l e _ T y p e = V } ]
michael@0 125 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x56, 0x7d, 0x5d, 0};
michael@0 126
michael@0 127 static const UChar gGC_TPattern[] = {
michael@0 128 // [ \ p { H a n g u l _ S y l
michael@0 129 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
michael@0 130 // l a b l e _ T y p e = T } ]
michael@0 131 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x54, 0x7d, 0x5d, 0};
michael@0 132
michael@0 133 static const UChar gGC_LVPattern[] = {
michael@0 134 // [ \ p { H a n g u l _ S y l
michael@0 135 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
michael@0 136 // l a b l e _ T y p e = L V } ]
michael@0 137 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x56, 0x7d, 0x5d, 0};
michael@0 138
michael@0 139 static const UChar gGC_LVTPattern[] = {
michael@0 140 // [ \ p { H a n g u l _ S y l
michael@0 141 0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
michael@0 142 // l a b l e _ T y p e = L V T } ]
michael@0 143 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x56, 0x54, 0x7d, 0x5d, 0};
michael@0 144
michael@0 145
michael@0 146 RegexStaticSets *RegexStaticSets::gStaticSets = NULL;
michael@0 147 UInitOnce gStaticSetsInitOnce = U_INITONCE_INITIALIZER;
michael@0 148
michael@0 149 RegexStaticSets::RegexStaticSets(UErrorCode *status)
michael@0 150 :
michael@0 151 fUnescapeCharSet(UnicodeString(TRUE, gUnescapeCharPattern, -1), *status),
michael@0 152 fRuleDigitsAlias(NULL),
michael@0 153 fEmptyText(NULL)
michael@0 154 {
michael@0 155 // First zero out everything
michael@0 156 int i;
michael@0 157 for (i=0; i<URX_LAST_SET; i++) {
michael@0 158 fPropSets[i] = NULL;
michael@0 159 }
michael@0 160 // Then init the sets to their correct values.
michael@0 161 fPropSets[URX_ISWORD_SET] = new UnicodeSet(UnicodeString(TRUE, gIsWordPattern, -1), *status);
michael@0 162 fPropSets[URX_ISSPACE_SET] = new UnicodeSet(UnicodeString(TRUE, gIsSpacePattern, -1), *status);
michael@0 163 fPropSets[URX_GC_EXTEND] = new UnicodeSet(UnicodeString(TRUE, gGC_ExtendPattern, -1), *status);
michael@0 164 fPropSets[URX_GC_CONTROL] = new UnicodeSet(UnicodeString(TRUE, gGC_ControlPattern, -1), *status);
michael@0 165 fPropSets[URX_GC_L] = new UnicodeSet(UnicodeString(TRUE, gGC_LPattern, -1), *status);
michael@0 166 fPropSets[URX_GC_V] = new UnicodeSet(UnicodeString(TRUE, gGC_VPattern, -1), *status);
michael@0 167 fPropSets[URX_GC_T] = new UnicodeSet(UnicodeString(TRUE, gGC_TPattern, -1), *status);
michael@0 168 fPropSets[URX_GC_LV] = new UnicodeSet(UnicodeString(TRUE, gGC_LVPattern, -1), *status);
michael@0 169 fPropSets[URX_GC_LVT] = new UnicodeSet(UnicodeString(TRUE, gGC_LVTPattern, -1), *status);
michael@0 170
michael@0 171 // Check for null pointers
michael@0 172 if (fPropSets[URX_ISWORD_SET] == NULL || fPropSets[URX_ISSPACE_SET] == NULL || fPropSets[URX_GC_EXTEND] == NULL ||
michael@0 173 fPropSets[URX_GC_CONTROL] == NULL || fPropSets[URX_GC_L] == NULL || fPropSets[URX_GC_V] == NULL ||
michael@0 174 fPropSets[URX_GC_T] == NULL || fPropSets[URX_GC_LV] == NULL || fPropSets[URX_GC_LVT] == NULL) {
michael@0 175 goto ExitConstrDeleteAll;
michael@0 176 }
michael@0 177 if (U_FAILURE(*status)) {
michael@0 178 // Bail out if we were unable to create the above sets.
michael@0 179 // The rest of the initialization needs them, so we cannot proceed.
michael@0 180 return;
michael@0 181 }
michael@0 182
michael@0 183
michael@0 184 //
michael@0 185 // The following sets are dynamically constructed, because their
michael@0 186 // initialization strings would be unreasonable.
michael@0 187 //
michael@0 188
michael@0 189
michael@0 190 //
michael@0 191 // "Normal" is the set of characters that don't need special handling
michael@0 192 // when finding grapheme cluster boundaries.
michael@0 193 //
michael@0 194 fPropSets[URX_GC_NORMAL] = new UnicodeSet(0, UnicodeSet::MAX_VALUE);
michael@0 195 // Null pointer check
michael@0 196 if (fPropSets[URX_GC_NORMAL] == NULL) {
michael@0 197 goto ExitConstrDeleteAll;
michael@0 198 }
michael@0 199 fPropSets[URX_GC_NORMAL]->remove(0xac00, 0xd7a4);
michael@0 200 fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_CONTROL]);
michael@0 201 fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_L]);
michael@0 202 fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_V]);
michael@0 203 fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_T]);
michael@0 204
michael@0 205 // Initialize the 8-bit fast bit sets from the parallel full
michael@0 206 // UnicodeSets.
michael@0 207 for (i=0; i<URX_LAST_SET; i++) {
michael@0 208 if (fPropSets[i]) {
michael@0 209 fPropSets[i]->compact();
michael@0 210 fPropSets8[i].init(fPropSets[i]);
michael@0 211 }
michael@0 212 }
michael@0 213
michael@0 214 // Sets used while parsing rules, but not referenced from the parse state table
michael@0 215 fRuleSets[kRuleSet_rule_char-128] = UnicodeSet(UnicodeString(TRUE, gRuleSet_rule_char_pattern, -1), *status);
michael@0 216 fRuleSets[kRuleSet_digit_char-128] = UnicodeSet(UnicodeString(TRUE, gRuleSet_digit_char_pattern, -1), *status);
michael@0 217 fRuleDigitsAlias = &fRuleSets[kRuleSet_digit_char-128];
michael@0 218 for (i=0; i<(int32_t)(sizeof(fRuleSets)/sizeof(fRuleSets[0])); i++) {
michael@0 219 fRuleSets[i].compact();
michael@0 220 }
michael@0 221
michael@0 222 // Finally, initialize an empty string for utility purposes
michael@0 223 fEmptyText = utext_openUChars(NULL, NULL, 0, status);
michael@0 224
michael@0 225 return; // If we reached this point, everything is fine so just exit
michael@0 226
michael@0 227 ExitConstrDeleteAll: // Remove fPropSets and fRuleSets and return error
michael@0 228 for (i=0; i<URX_LAST_SET; i++) {
michael@0 229 delete fPropSets[i];
michael@0 230 fPropSets[i] = NULL;
michael@0 231 }
michael@0 232 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 233 }
michael@0 234
michael@0 235
michael@0 236 RegexStaticSets::~RegexStaticSets() {
michael@0 237 int32_t i;
michael@0 238
michael@0 239 for (i=0; i<URX_LAST_SET; i++) {
michael@0 240 delete fPropSets[i];
michael@0 241 fPropSets[i] = NULL;
michael@0 242 }
michael@0 243 fRuleDigitsAlias = NULL;
michael@0 244
michael@0 245 utext_close(fEmptyText);
michael@0 246 }
michael@0 247
michael@0 248
michael@0 249 //------------------------------------------------------------------------------
michael@0 250 //
michael@0 251 // regex_cleanup Memory cleanup function, free/delete all
michael@0 252 // cached memory. Called by ICU's u_cleanup() function.
michael@0 253 //
michael@0 254 //------------------------------------------------------------------------------
michael@0 255 UBool
michael@0 256 RegexStaticSets::cleanup(void) {
michael@0 257 delete RegexStaticSets::gStaticSets;
michael@0 258 RegexStaticSets::gStaticSets = NULL;
michael@0 259 gStaticSetsInitOnce.reset();
michael@0 260 return TRUE;
michael@0 261 }
michael@0 262
michael@0 263 U_CDECL_BEGIN
michael@0 264 static UBool U_CALLCONV
michael@0 265 regex_cleanup(void) {
michael@0 266 return RegexStaticSets::cleanup();
michael@0 267 }
michael@0 268
michael@0 269 static void U_CALLCONV initStaticSets(UErrorCode &status) {
michael@0 270 U_ASSERT(RegexStaticSets::gStaticSets == NULL);
michael@0 271 ucln_i18n_registerCleanup(UCLN_I18N_REGEX, regex_cleanup);
michael@0 272 RegexStaticSets::gStaticSets = new RegexStaticSets(&status);
michael@0 273 if (U_FAILURE(status)) {
michael@0 274 delete RegexStaticSets::gStaticSets;
michael@0 275 RegexStaticSets::gStaticSets = NULL;
michael@0 276 }
michael@0 277 if (RegexStaticSets::gStaticSets == NULL && U_SUCCESS(status)) {
michael@0 278 status = U_MEMORY_ALLOCATION_ERROR;
michael@0 279 }
michael@0 280 }
michael@0 281 U_CDECL_END
michael@0 282
michael@0 283 void RegexStaticSets::initGlobals(UErrorCode *status) {
michael@0 284 umtx_initOnce(gStaticSetsInitOnce, &initStaticSets, *status);
michael@0 285 }
michael@0 286
michael@0 287 U_NAMESPACE_END
michael@0 288 #endif // !UCONFIG_NO_REGULAR_EXPRESSIONS

mercurial