2003-05-02 21:35:01 +00:00
|
|
|
//
|
|
|
|
// regexst.h
|
|
|
|
//
|
2008-01-14 23:25:13 +00:00
|
|
|
// Copyright (C) 2004-2008, International Business Machines Corporation and others.
|
2003-05-02 21:35:01 +00:00
|
|
|
// All Rights Reserved.
|
|
|
|
//
|
|
|
|
// This file contains class RegexStaticSets
|
|
|
|
//
|
|
|
|
// This class is internal to the regular expression implementation.
|
|
|
|
// For the public Regular Expression API, see the file "unicode/regex.h"
|
|
|
|
//
|
|
|
|
// RegexStaticSets groups together the common UnicodeSets that are needed
|
|
|
|
// for compiling or executing RegularExpressions. This grouping simplifies
|
|
|
|
// the thread safe lazy creation and sharing of these sets across
|
|
|
|
// all instances of regular expressions.
|
|
|
|
//
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_REGULAR_EXPRESSIONS
|
|
|
|
|
|
|
|
#include "unicode/unistr.h"
|
|
|
|
#include "unicode/uniset.h"
|
|
|
|
#include "unicode/uchar.h"
|
|
|
|
#include "unicode/regex.h"
|
|
|
|
#include "uprops.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
#include "uassert.h"
|
|
|
|
#include "ucln_in.h"
|
|
|
|
#include "umutex.h"
|
|
|
|
|
|
|
|
#include "regexcst.h" // Contains state table for the regex pattern parser.
|
|
|
|
// generated by a Perl script.
|
|
|
|
#include "regexst.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
|
2007-06-22 01:06:31 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2003-05-02 21:35:01 +00:00
|
|
|
//
|
|
|
|
// Unicode Set pattern strings for all of the required constant sets.
|
|
|
|
// Initialized with hex values for portability to EBCDIC based machines.
|
|
|
|
// Really ugly, but there's no good way to avoid it.
|
|
|
|
//
|
2007-06-22 01:06:31 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2003-05-02 21:35:01 +00:00
|
|
|
|
|
|
|
// "Rule Char" Characters are those with no special meaning, and therefore do not
|
|
|
|
// need to be escaped to appear as literals in a regexp. Expressed
|
|
|
|
// as the inverse of those needing escaping -- [^\*\?\+\[\(\)\{\}\^\$\|\\\.]
|
|
|
|
static const UChar gRuleSet_rule_char_pattern[] = {
|
|
|
|
// [ ^ \ * \ ? \ + \ [ \ ( / )
|
|
|
|
0x5b, 0x5e, 0x5c, 0x2a, 0x5c, 0x3f, 0x5c, 0x2b, 0x5c, 0x5b, 0x5c, 0x28, 0x5c, 0x29,
|
|
|
|
// \ { \ } \ ^ \ $ \ | \ \ \ . ]
|
|
|
|
0x5c, 0x7b,0x5c, 0x7d, 0x5c, 0x5e, 0x5c, 0x24, 0x5c, 0x7c, 0x5c, 0x5c, 0x5c, 0x2e, 0x5d, 0};
|
|
|
|
|
|
|
|
|
|
|
|
static const UChar gRuleSet_digit_char_pattern[] = {
|
|
|
|
// [ 0 - 9 ]
|
|
|
|
0x5b, 0x30, 0x2d, 0x39, 0x5d, 0};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Here are the backslash escape characters that ICU's unescape() function
|
|
|
|
// will handle.
|
|
|
|
//
|
|
|
|
static const UChar gUnescapeCharPattern[] = {
|
2003-05-09 16:55:08 +00:00
|
|
|
// [ a c e f n r t u U x ]
|
|
|
|
0x5b, 0x61, 0x63, 0x65, 0x66, 0x6e, 0x72, 0x74, 0x75, 0x55, 0x78, 0x5d, 0};
|
2003-05-02 21:35:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Unicode Set Definitions for Regular Expression \w
|
|
|
|
//
|
|
|
|
static const UChar gIsWordPattern[] = {
|
2004-10-26 23:13:40 +00:00
|
|
|
// [ \ p { A l p h a b e t i c }
|
|
|
|
0x5b, 0x5c, 0x70, 0x7b, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x62, 0x65, 0x74, 0x69, 0x63, 0x7d,
|
|
|
|
// \ p { M } Mark
|
2007-12-11 21:30:10 +00:00
|
|
|
0x5c, 0x70, 0x7b, 0x4d, 0x7d,
|
2004-10-26 23:13:40 +00:00
|
|
|
// \ p { N d } Digit_Numeric
|
|
|
|
0x5c, 0x70, 0x7b, 0x4e, 0x64, 0x7d,
|
|
|
|
// \ p { P c } ] Connector_Punctuation
|
|
|
|
0x5c, 0x70, 0x7b, 0x50, 0x63, 0x7d, 0x5d, 0};
|
2003-05-02 21:35:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Unicode Set Definitions for Regular Expression \s
|
|
|
|
//
|
2007-06-22 01:06:31 +00:00
|
|
|
static const UChar gIsSpacePattern[] = {
|
2004-10-26 23:13:40 +00:00
|
|
|
// [ \ p { W h i t e S p a c e } ]
|
|
|
|
0x5b, 0x5c, 0x70, 0x7b, 0x57, 0x68, 0x69, 0x74, 0x65, 0x53, 0x70, 0x61, 0x63, 0x65, 0x7d, 0x5d, 0};
|
2003-05-02 21:35:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// UnicodeSets used in implementation of Grapheme Cluster detection, \X
|
|
|
|
//
|
2007-06-22 01:06:31 +00:00
|
|
|
static const UChar gGC_ControlPattern[] = {
|
2007-12-11 21:30:10 +00:00
|
|
|
// [ [ : Z l : ] [ : Z p : ]
|
|
|
|
0x5b, 0x5b, 0x3a, 0x5A, 0x6c, 0x3a, 0x5d, 0x5b, 0x3a, 0x5A, 0x70, 0x3a, 0x5d,
|
2004-04-08 23:38:02 +00:00
|
|
|
// [ : C c : ] [ : C f : ] -
|
|
|
|
0x5b, 0x3a, 0x43, 0x63, 0x3a, 0x5d, 0x5b, 0x3a, 0x43, 0x66, 0x3a, 0x5d, 0x2d,
|
|
|
|
// [ : G r a p h e m e _
|
|
|
|
0x5b, 0x3a, 0x47, 0x72, 0x61, 0x70, 0x68, 0x65, 0x6d, 0x65, 0x5f,
|
2004-11-11 23:34:58 +00:00
|
|
|
// E x t e n d : ] ]
|
|
|
|
0x45, 0x78, 0x74, 0x65, 0x6e, 0x64, 0x3a, 0x5d, 0x5d, 0};
|
2003-05-02 21:35:01 +00:00
|
|
|
|
2007-06-22 01:06:31 +00:00
|
|
|
static const UChar gGC_ExtendPattern[] = {
|
2003-05-27 03:03:47 +00:00
|
|
|
// [ \ p { G r a p h e m e _
|
|
|
|
0x5b, 0x5c, 0x70, 0x7b, 0x47, 0x72, 0x61, 0x70, 0x68, 0x65, 0x6d, 0x65, 0x5f,
|
2004-04-08 23:38:02 +00:00
|
|
|
// E x t e n d } ]
|
|
|
|
0x45, 0x78, 0x74, 0x65, 0x6e, 0x64, 0x7d, 0x5d, 0};
|
2003-05-02 21:35:01 +00:00
|
|
|
|
2007-06-22 01:06:31 +00:00
|
|
|
static const UChar gGC_LPattern[] = {
|
2007-12-11 21:30:10 +00:00
|
|
|
// [ \ p { H a n g u l _ S y l
|
2003-05-27 03:03:47 +00:00
|
|
|
0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
|
|
|
|
// l a b l e _ T y p e = L } ]
|
2007-12-11 21:30:10 +00:00
|
|
|
0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x7d, 0x5d, 0};
|
2003-05-02 21:35:01 +00:00
|
|
|
|
2007-06-22 01:06:31 +00:00
|
|
|
static const UChar gGC_VPattern[] = {
|
2007-12-11 21:30:10 +00:00
|
|
|
// [ \ p { H a n g u l _ S y l
|
2003-05-27 03:03:47 +00:00
|
|
|
0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
|
|
|
|
// l a b l e _ T y p e = V } ]
|
2007-12-11 21:30:10 +00:00
|
|
|
0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x56, 0x7d, 0x5d, 0};
|
2003-05-02 21:35:01 +00:00
|
|
|
|
2007-06-22 01:06:31 +00:00
|
|
|
static const UChar gGC_TPattern[] = {
|
2007-12-11 21:30:10 +00:00
|
|
|
// [ \ p { H a n g u l _ S y l
|
2003-05-27 03:03:47 +00:00
|
|
|
0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
|
|
|
|
// l a b l e _ T y p e = T } ]
|
2007-12-11 21:30:10 +00:00
|
|
|
0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x54, 0x7d, 0x5d, 0};
|
2003-05-27 03:03:47 +00:00
|
|
|
|
2007-06-22 01:06:31 +00:00
|
|
|
static const UChar gGC_LVPattern[] = {
|
2007-12-11 21:30:10 +00:00
|
|
|
// [ \ p { H a n g u l _ S y l
|
2003-05-27 03:03:47 +00:00
|
|
|
0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
|
|
|
|
// l a b l e _ T y p e = L V } ]
|
2007-12-11 21:30:10 +00:00
|
|
|
0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x56, 0x7d, 0x5d, 0};
|
2003-05-27 03:03:47 +00:00
|
|
|
|
2007-06-22 01:06:31 +00:00
|
|
|
static const UChar gGC_LVTPattern[] = {
|
2007-12-11 21:30:10 +00:00
|
|
|
// [ \ p { H a n g u l _ S y l
|
2003-05-27 03:03:47 +00:00
|
|
|
0x5b, 0x5c, 0x70, 0x7b, 0x48, 0x61, 0x6e, 0x67, 0x75, 0x6c, 0x5f, 0x53, 0x79, 0x6c,
|
|
|
|
// l a b l e _ T y p e = L V T } ]
|
2007-12-11 21:30:10 +00:00
|
|
|
0x6c, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x54, 0x79, 0x70, 0x65, 0x3d, 0x4c, 0x56, 0x54, 0x7d, 0x5d, 0};
|
|
|
|
|
2003-05-02 21:35:01 +00:00
|
|
|
|
2003-05-28 01:42:29 +00:00
|
|
|
RegexStaticSets *RegexStaticSets::gStaticSets = NULL;
|
2003-05-02 21:35:01 +00:00
|
|
|
|
2007-06-22 01:06:31 +00:00
|
|
|
RegexStaticSets::RegexStaticSets(UErrorCode *status)
|
|
|
|
:
|
|
|
|
fUnescapeCharSet(UnicodeString(TRUE, gUnescapeCharPattern, -1), *status),
|
|
|
|
fRuleDigitsAlias(NULL)
|
|
|
|
{
|
2007-12-11 21:30:10 +00:00
|
|
|
// First zero out everything
|
2003-05-02 21:35:01 +00:00
|
|
|
int i;
|
|
|
|
for (i=0; i<URX_LAST_SET; i++) {
|
|
|
|
fPropSets[i] = NULL;
|
|
|
|
}
|
|
|
|
// Then init the sets to their correct values.
|
2007-06-22 01:06:31 +00:00
|
|
|
fPropSets[URX_ISWORD_SET] = new UnicodeSet(UnicodeString(TRUE, gIsWordPattern, -1), *status);
|
2007-12-11 21:30:10 +00:00
|
|
|
fPropSets[URX_ISSPACE_SET] = new UnicodeSet(UnicodeString(TRUE, gIsSpacePattern, -1), *status);
|
2007-06-22 01:06:31 +00:00
|
|
|
fPropSets[URX_GC_EXTEND] = new UnicodeSet(UnicodeString(TRUE, gGC_ExtendPattern, -1), *status);
|
|
|
|
fPropSets[URX_GC_CONTROL] = new UnicodeSet(UnicodeString(TRUE, gGC_ControlPattern, -1), *status);
|
|
|
|
fPropSets[URX_GC_L] = new UnicodeSet(UnicodeString(TRUE, gGC_LPattern, -1), *status);
|
|
|
|
fPropSets[URX_GC_V] = new UnicodeSet(UnicodeString(TRUE, gGC_VPattern, -1), *status);
|
|
|
|
fPropSets[URX_GC_T] = new UnicodeSet(UnicodeString(TRUE, gGC_TPattern, -1), *status);
|
|
|
|
fPropSets[URX_GC_LV] = new UnicodeSet(UnicodeString(TRUE, gGC_LVPattern, -1), *status);
|
|
|
|
fPropSets[URX_GC_LVT] = new UnicodeSet(UnicodeString(TRUE, gGC_LVTPattern, -1), *status);
|
2008-01-14 23:25:13 +00:00
|
|
|
|
|
|
|
// Check for null pointers
|
2008-02-09 00:30:33 +00:00
|
|
|
if (fPropSets[URX_ISWORD_SET] == NULL || fPropSets[URX_ISSPACE_SET] == NULL || fPropSets[URX_GC_EXTEND] == NULL ||
|
|
|
|
fPropSets[URX_GC_CONTROL] == NULL || fPropSets[URX_GC_L] == NULL || fPropSets[URX_GC_V] == NULL ||
|
|
|
|
fPropSets[URX_GC_T] == NULL || fPropSets[URX_GC_LV] == NULL || fPropSets[URX_GC_LVT] == NULL) {
|
|
|
|
goto ExitConstrDeleteAll;
|
2008-01-14 23:25:13 +00:00
|
|
|
}
|
2003-06-05 21:28:59 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
// Bail out if we were unable to create the above sets.
|
|
|
|
// The rest of the initialization needs them, so we cannot proceed.
|
|
|
|
return;
|
|
|
|
}
|
2007-12-11 21:30:10 +00:00
|
|
|
|
|
|
|
|
2003-05-02 21:35:01 +00:00
|
|
|
//
|
|
|
|
// The following sets are dynamically constructed, because their
|
2007-06-22 01:06:31 +00:00
|
|
|
// initialization strings would be unreasonable.
|
2003-05-02 21:35:01 +00:00
|
|
|
//
|
2007-12-11 21:30:10 +00:00
|
|
|
|
|
|
|
|
2003-05-02 21:35:01 +00:00
|
|
|
//
|
|
|
|
// "Normal" is the set of characters that don't need special handling
|
|
|
|
// when finding grapheme cluster boundaries.
|
|
|
|
//
|
2007-06-22 01:06:31 +00:00
|
|
|
fPropSets[URX_GC_NORMAL] = new UnicodeSet(0, UnicodeSet::MAX_VALUE);
|
2008-01-14 23:25:13 +00:00
|
|
|
// Null pointer check
|
|
|
|
if (fPropSets[URX_GC_NORMAL] == NULL) {
|
|
|
|
goto ExitConstrDeleteAll;
|
|
|
|
}
|
2003-05-02 21:35:01 +00:00
|
|
|
fPropSets[URX_GC_NORMAL]->remove(0xac00, 0xd7a4);
|
|
|
|
fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_CONTROL]);
|
|
|
|
fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_L]);
|
|
|
|
fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_V]);
|
|
|
|
fPropSets[URX_GC_NORMAL]->removeAll(*fPropSets[URX_GC_T]);
|
2007-12-11 21:30:10 +00:00
|
|
|
|
2003-05-02 21:35:01 +00:00
|
|
|
// Initialize the 8-bit fast bit sets from the parallel full
|
|
|
|
// UnicodeSets.
|
|
|
|
for (i=0; i<URX_LAST_SET; i++) {
|
2007-06-22 01:06:31 +00:00
|
|
|
if (fPropSets[i]) {
|
|
|
|
fPropSets[i]->compact();
|
|
|
|
fPropSets8[i].init(fPropSets[i]);
|
|
|
|
}
|
2003-05-02 21:35:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sets used while parsing rules, but not referenced from the parse state table
|
2008-02-23 19:15:18 +00:00
|
|
|
fRuleSets[kRuleSet_rule_char-128] = UnicodeSet(UnicodeString(TRUE, gRuleSet_rule_char_pattern, -1), *status);
|
|
|
|
fRuleSets[kRuleSet_digit_char-128] = UnicodeSet(UnicodeString(TRUE, gRuleSet_digit_char_pattern, -1), *status);
|
|
|
|
fRuleDigitsAlias = &fRuleSets[kRuleSet_digit_char-128];
|
2007-06-22 01:06:31 +00:00
|
|
|
for (i=0; i<(int32_t)(sizeof(fRuleSets)/sizeof(fRuleSets[0])); i++) {
|
2008-02-23 19:15:18 +00:00
|
|
|
fRuleSets[i].compact();
|
2007-06-22 01:06:31 +00:00
|
|
|
}
|
2008-01-14 23:25:13 +00:00
|
|
|
return; // If we reached this point, everything is fine so just exit
|
2008-02-23 19:15:18 +00:00
|
|
|
|
2008-01-14 23:25:13 +00:00
|
|
|
ExitConstrDeleteAll: // Remove fPropSets and fRuleSets and return error
|
2008-02-09 00:30:33 +00:00
|
|
|
for (i=0; i<URX_LAST_SET; i++) {
|
|
|
|
delete fPropSets[i];
|
|
|
|
fPropSets[i] = NULL;
|
|
|
|
}
|
2008-02-23 19:15:18 +00:00
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
2004-09-12 23:07:29 +00:00
|
|
|
}
|
2003-05-02 21:35:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
RegexStaticSets::~RegexStaticSets() {
|
2007-06-22 01:06:31 +00:00
|
|
|
int32_t i;
|
2003-05-02 21:35:01 +00:00
|
|
|
|
|
|
|
for (i=0; i<URX_LAST_SET; i++) {
|
|
|
|
delete fPropSets[i];
|
|
|
|
fPropSets[i] = NULL;
|
|
|
|
}
|
2007-06-22 01:06:31 +00:00
|
|
|
fRuleDigitsAlias = NULL;
|
2004-09-12 23:07:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-22 01:06:31 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-09-12 23:07:29 +00:00
|
|
|
//
|
|
|
|
// regex_cleanup Memory cleanup function, free/delete all
|
|
|
|
// cached memory. Called by ICU's u_cleanup() function.
|
|
|
|
//
|
2007-06-22 01:06:31 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-09-12 23:07:29 +00:00
|
|
|
UBool
|
|
|
|
RegexStaticSets::cleanup(void) {
|
|
|
|
delete RegexStaticSets::gStaticSets;
|
|
|
|
RegexStaticSets::gStaticSets = NULL;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2003-05-02 21:35:01 +00:00
|
|
|
|
2004-09-12 23:07:29 +00:00
|
|
|
U_CDECL_BEGIN
|
|
|
|
static UBool U_CALLCONV
|
|
|
|
regex_cleanup(void) {
|
|
|
|
return RegexStaticSets::cleanup();
|
|
|
|
}
|
|
|
|
U_CDECL_END
|
2003-05-02 21:35:01 +00:00
|
|
|
|
2003-05-28 01:42:29 +00:00
|
|
|
void RegexStaticSets::initGlobals(UErrorCode *status) {
|
2006-08-29 04:57:05 +00:00
|
|
|
RegexStaticSets *p;
|
|
|
|
UMTX_CHECK(NULL, gStaticSets, p);
|
2003-05-02 21:35:01 +00:00
|
|
|
if (p == NULL) {
|
|
|
|
p = new RegexStaticSets(status);
|
2008-01-14 23:25:13 +00:00
|
|
|
if (p == NULL) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2003-05-02 21:35:01 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
2003-06-05 21:28:59 +00:00
|
|
|
delete p;
|
2003-05-02 21:35:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
umtx_lock(NULL);
|
2003-05-28 01:42:29 +00:00
|
|
|
if (gStaticSets == NULL) {
|
|
|
|
gStaticSets = p;
|
2003-05-02 21:35:01 +00:00
|
|
|
p = NULL;
|
|
|
|
}
|
|
|
|
umtx_unlock(NULL);
|
2003-05-28 01:42:29 +00:00
|
|
|
if (p) {
|
|
|
|
delete p;
|
|
|
|
}
|
2004-09-12 23:07:29 +00:00
|
|
|
ucln_i18n_registerCleanup(UCLN_I18N_REGEX, regex_cleanup);
|
2003-05-02 21:35:01 +00:00
|
|
|
}
|
|
|
|
}
|
2007-12-11 21:30:10 +00:00
|
|
|
|
2003-05-02 21:35:01 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
#endif // !UCONFIG_NO_REGULAR_EXPRESSIONS
|