#include "unicode/utypes.h"
#if !UCONFIG_NO_REGULAR_EXPRESSIONS
#include "unicode/unistr.h"
#include "unicode/uniset.h"
#include "unicode/uchar.h"
#include "unicode/uchriter.h"
#include "unicode/parsepos.h"
#include "unicode/parseerr.h"
#include "unicode/regex.h"
#include "util.h"
#include "cmemory.h"
#include "cstring.h"
#include "uvectr32.h"
#include "uassert.h"
#include "ucln_in.h"
#include "mutex.h"
#include "regeximp.h"
#include "regexcst.h" // Contains state table for the regex pattern parser.
#include "regexcmp.h"
#include "regexst.h"
U_NAMESPACE_BEGIN
RegexCompile::RegexCompile(RegexPattern *rxp, UErrorCode &status) : fParenStack(status)
{
fStatus = &status;
fRXPat = rxp;
fScanIndex = 0;
fNextIndex = 0;
fPeekChar = -1;
fLineNum = 1;
fCharNum = 0;
fQuoteMode = FALSE;
fInBackslashQuote = FALSE;
fModeFlags = fRXPat->fFlags;
fEOLComments = TRUE;
fMatchOpenParen = -1;
fMatchCloseParen = -1;
fStringOpStart = -1;
if (U_SUCCESS(status) && U_FAILURE(rxp->fDeferredStatus)) {
status = rxp->fDeferredStatus;
}
}
RegexCompile::~RegexCompile() {
}
void RegexCompile::compile(
const UnicodeString &pat, UParseError &pp, UErrorCode &e) {
fStatus = &e;
fParseErr = &pp;
fStackPtr = 0;
fStack[fStackPtr] = 0;
if (U_FAILURE(*fStatus)) {
return;
}
U_ASSERT(fRXPat->fPattern.length() == 0);
fRXPat->fPattern = pat;
fRXPat->fStaticSets = RegexStaticSets::gStaticSets->fPropSets;
fRXPat->fStaticSets8 = RegexStaticSets::gStaticSets->fPropSets8;
fPatternLength = pat.length();
uint16_t state = 1;
const RegexTableEl *tableEl;
nextChar(fC);
for (;;) {
if (U_FAILURE(*fStatus)) {
break;
}
U_ASSERT(state != 0);
tableEl = &gRuleParseStateTable[state];
REGEX_SCAN_DEBUG_PRINTF(("char, line, col = (\'%c\', %d, %d) state=%s ",
fC.fChar, fLineNum, fCharNum, RegexStateNames[state]));
for (;;) { REGEX_SCAN_DEBUG_PRINTF(("."));
if (tableEl->fCharClass < 127 && fC.fQuoted == FALSE && tableEl->fCharClass == fC.fChar) {
break;
}
if (tableEl->fCharClass == 255) {
break;
}
if (tableEl->fCharClass == 254 && fC.fQuoted) {
break;
}
if (tableEl->fCharClass == 253 && fC.fChar == (UChar32)-1) {
break;
}
if (tableEl->fCharClass >= 128 && tableEl->fCharClass < 240 && fC.fQuoted == FALSE && fC.fChar != (UChar32)-1) { UnicodeSet *uniset = RegexStaticSets::gStaticSets->fRuleSets[tableEl->fCharClass-128];
if (uniset->contains(fC.fChar)) {
break;
}
}
tableEl++;
}
REGEX_SCAN_DEBUG_PRINTF(("\n"));
if (doParseActions((EParseAction)tableEl->fAction) == FALSE) {
break;
}
if (tableEl->fPushState != 0) {
fStackPtr++;
if (fStackPtr >= kStackSize) {
error(U_REGEX_INTERNAL_ERROR);
REGEX_SCAN_DEBUG_PRINTF(("RegexCompile::parse() - state stack overflow.\n"));
fStackPtr--;
}
fStack[fStackPtr] = tableEl->fPushState;
}
if (tableEl->fNextChar) {
nextChar(fC);
}
if (tableEl->fNextState != 255) {
state = tableEl->fNextState;
} else {
state = fStack[fStackPtr];
fStackPtr--;
if (fStackPtr < 0) {
fStackPtr++;
error(U_REGEX_MISMATCHED_PAREN);
}
}
}
int32_t loc;
for (loc=0; loc<fRXPat->fCompiledPat->size(); loc++) {
int32_t op = fRXPat->fCompiledPat->elementAti(loc);
int32_t opType = URX_TYPE(op);
if (opType == URX_BACKREF || opType == URX_BACKREF_I) {
int32_t where = URX_VAL(op);
if (where > fRXPat->fGroupMap->size()) {
error(U_REGEX_INVALID_BACK_REF);
break;
}
where = fRXPat->fGroupMap->elementAti(where-1);
op = URX_BUILD(opType, where);
fRXPat->fCompiledPat->setElementAt(op, loc);
}
}
fRXPat->fMaxCaptureDigits = 1;
int32_t n = 10;
for (;;) {
if (n > fRXPat->fGroupMap->size()) {
break;
}
fRXPat->fMaxCaptureDigits++;
n *= 10;
}
fRXPat->fFrameSize+=2;
fRXPat->fMinMatchLen = minMatchLength(3, fRXPat->fCompiledPat->size()-1);
matchStartType();
OptDotStar();
stripNOPs();
int32_t numSets = fRXPat->fSets->size();
fRXPat->fSets8 = new Regex8BitSet[numSets];
int32_t i;
for (i=0; i<numSets; i++) {
UnicodeSet *s = (UnicodeSet *)fRXPat->fSets->elementAt(i);
fRXPat->fSets8[i].init(s);
}
}
UBool RegexCompile::doParseActions(EParseAction action)
{
UBool returnVal = TRUE;
switch ((Regex_PatternParseAction)action) {
case doPatStart:
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_STATE_SAVE, 2), *fStatus);
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_JMP, 3), *fStatus);
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_FAIL, 0), *fStatus);
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_NOP, 0), *fStatus);
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_NOP, 0), *fStatus);
fParenStack.push(-1, *fStatus); fParenStack.push( 3, *fStatus); break;
case doPatFinish:
handleCloseParen();
if (fParenStack.size() > 0) {
error(U_REGEX_MISMATCHED_PAREN);
}
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_END, 0), *fStatus);
returnVal = FALSE;
break;
case doOrOperator:
{
int32_t savePosition = fParenStack.popi();
int32_t op = fRXPat->fCompiledPat->elementAti(savePosition);
U_ASSERT(URX_TYPE(op) == URX_NOP); op = URX_BUILD(URX_STATE_SAVE, fRXPat->fCompiledPat->size()+1);
fRXPat->fCompiledPat->setElementAt(op, savePosition);
op = URX_BUILD(URX_JMP, 0);
fRXPat->fCompiledPat->addElement(op, *fStatus);
fParenStack.push(fRXPat->fCompiledPat->size()-1, *fStatus);
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_NOP, 0), *fStatus);
fParenStack.push(fRXPat->fCompiledPat->size()-1, *fStatus);
}
break;
case doOpenCaptureParen:
{
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_NOP, 0), *fStatus);
int32_t varsLoc = fRXPat->fFrameSize; fRXPat->fFrameSize += 3;
int32_t cop = URX_BUILD(URX_START_CAPTURE, varsLoc);
fRXPat->fCompiledPat->addElement(cop, *fStatus);
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_NOP, 0), *fStatus);
fParenStack.push(fModeFlags, *fStatus); fParenStack.push(capturing, *fStatus); fParenStack.push(fRXPat->fCompiledPat->size()-3, *fStatus); fParenStack.push(fRXPat->fCompiledPat->size()-1, *fStatus);
fRXPat->fGroupMap->addElement(varsLoc, *fStatus);
}
break;
case doOpenNonCaptureParen:
{
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_NOP, 0), *fStatus);
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_NOP, 0), *fStatus);
fParenStack.push(fModeFlags, *fStatus); fParenStack.push(plain, *fStatus); fParenStack.push(fRXPat->fCompiledPat->size()-2, *fStatus); fParenStack.push(fRXPat->fCompiledPat->size()-1, *fStatus); }
break;
case doOpenAtomicParen:
{
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_NOP, 0), *fStatus);
int32_t varLoc = fRXPat->fDataSize; fRXPat->fDataSize += 1; int32_t stoOp = URX_BUILD(URX_STO_SP, varLoc);
fRXPat->fCompiledPat->addElement(stoOp, *fStatus);
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_NOP, 0), *fStatus);
fParenStack.push(fModeFlags, *fStatus); fParenStack.push(atomic, *fStatus); fParenStack.push(fRXPat->fCompiledPat->size()-3, *fStatus); fParenStack.push(fRXPat->fCompiledPat->size()-1, *fStatus); }
break;
case doOpenLookAhead:
{
int32_t dataLoc = fRXPat->fDataSize;
fRXPat->fDataSize += 2;
int32_t op = URX_BUILD(URX_LA_START, dataLoc);
fRXPat->fCompiledPat->addElement(op, *fStatus);
op = URX_BUILD(URX_NOP, 0);
fRXPat->fCompiledPat->addElement(op, *fStatus);
fRXPat->fCompiledPat->addElement(op, *fStatus);
fParenStack.push(fModeFlags, *fStatus); fParenStack.push(lookAhead, *fStatus); fParenStack.push(fRXPat->fCompiledPat->size()-2, *fStatus); fParenStack.push(fRXPat->fCompiledPat->size()-1, *fStatus); }
break;
case doOpenLookAheadNeg:
{
int32_t dataLoc = fRXPat->fDataSize;
fRXPat->fDataSize += 2;
int32_t op = URX_BUILD(URX_LA_START, dataLoc);
fRXPat->fCompiledPat->addElement(op, *fStatus);
op = URX_BUILD(URX_STATE_SAVE, 0); fRXPat->fCompiledPat->addElement(op, *fStatus);
op = URX_BUILD(URX_NOP, 0);
fRXPat->fCompiledPat->addElement(op, *fStatus);
fParenStack.push(fModeFlags, *fStatus); fParenStack.push( negLookAhead, *fStatus); fParenStack.push(fRXPat->fCompiledPat->size()-2, *fStatus); fParenStack.push(fRXPat->fCompiledPat->size()-1, *fStatus);
}
break;
case doOpenLookBehind:
{
int32_t dataLoc = fRXPat->fDataSize;
fRXPat->fDataSize += 4;
int32_t op = URX_BUILD(URX_LB_START, dataLoc);
fRXPat->fCompiledPat->addElement(op, *fStatus);
op = URX_BUILD(URX_LB_CONT, dataLoc);
fRXPat->fCompiledPat->addElement(op, *fStatus);
fRXPat->fCompiledPat->addElement(0, *fStatus); fRXPat->fCompiledPat->addElement(0, *fStatus);
op = URX_BUILD(URX_NOP, 0);
fRXPat->fCompiledPat->addElement(op, *fStatus);
fRXPat->fCompiledPat->addElement(op, *fStatus);
fParenStack.push(fModeFlags, *fStatus); fParenStack.push(lookBehind, *fStatus); fParenStack.push(fRXPat->fCompiledPat->size()-2, *fStatus); fParenStack.push(fRXPat->fCompiledPat->size()-1, *fStatus);
}
break;
case doOpenLookBehindNeg:
{
int32_t dataLoc = fRXPat->fDataSize;
fRXPat->fDataSize += 4;
int32_t op = URX_BUILD(URX_LB_START, dataLoc);
fRXPat->fCompiledPat->addElement(op, *fStatus);
op = URX_BUILD(URX_LBN_CONT, dataLoc);
fRXPat->fCompiledPat->addElement(op, *fStatus);
fRXPat->fCompiledPat->addElement(0, *fStatus); fRXPat->fCompiledPat->addElement(0, *fStatus); fRXPat->fCompiledPat->addElement(0, *fStatus);
op = URX_BUILD(URX_NOP, 0);
fRXPat->fCompiledPat->addElement(op, *fStatus);
fRXPat->fCompiledPat->addElement(op, *fStatus);
fParenStack.push(fModeFlags, *fStatus); fParenStack.push(lookBehindN, *fStatus); fParenStack.push(fRXPat->fCompiledPat->size()-2, *fStatus); fParenStack.push(fRXPat->fCompiledPat->size()-1, *fStatus);
}
break;
case doConditionalExpr:
case doPerlInline:
error(U_REGEX_UNIMPLEMENTED);
break;
case doCloseParen:
handleCloseParen();
if (fParenStack.size() <= 0) {
error(U_REGEX_MISMATCHED_PAREN);
}
break;
case doNOP:
break;
case doBadOpenParenType:
case doRuleError:
error(U_REGEX_RULE_SYNTAX);
break;
case doMismatchedParenErr:
error(U_REGEX_MISMATCHED_PAREN);
break;
case doPlus:
{
int32_t topLoc = blockTopLoc(FALSE); int32_t frameLoc;
if (topLoc == fRXPat->fCompiledPat->size() - 1) {
int32_t repeatedOp = fRXPat->fCompiledPat->elementAti(topLoc);
if (URX_TYPE(repeatedOp) == URX_SETREF) {
int32_t loopOpI = URX_BUILD(URX_LOOP_SR_I, URX_VAL(repeatedOp));
fRXPat->fCompiledPat->addElement(loopOpI, *fStatus);
frameLoc = fRXPat->fFrameSize;
fRXPat->fFrameSize++;
int32_t loopOpC = URX_BUILD(URX_LOOP_C, frameLoc);
fRXPat->fCompiledPat->addElement(loopOpC, *fStatus);
break;
}
if (URX_TYPE(repeatedOp) == URX_DOTANY ||
URX_TYPE(repeatedOp) == URX_DOTANY_ALL) {
int32_t loopOpI = URX_BUILD(URX_LOOP_DOT_I, 0);
if (URX_TYPE(repeatedOp) == URX_DOTANY_ALL) {
loopOpI |= 1;
}
fRXPat->fCompiledPat->addElement(loopOpI, *fStatus);
frameLoc = fRXPat->fFrameSize;
fRXPat->fFrameSize++;
int32_t loopOpC = URX_BUILD(URX_LOOP_C, frameLoc);
fRXPat->fCompiledPat->addElement(loopOpC, *fStatus);
break;
}
}
if (minMatchLength(topLoc, fRXPat->fCompiledPat->size()-1) == 0) {
insertOp(topLoc);
frameLoc = fRXPat->fFrameSize;
fRXPat->fFrameSize++;
int32_t op = URX_BUILD(URX_STO_INP_LOC, frameLoc);
fRXPat->fCompiledPat->setElementAt(op, topLoc);
op = URX_BUILD(URX_JMP_SAV_X, topLoc+1);
fRXPat->fCompiledPat->addElement(op, *fStatus);
} else {
int32_t jmpOp = URX_BUILD(URX_JMP_SAV, topLoc);
fRXPat->fCompiledPat->addElement(jmpOp, *fStatus);
}
}
break;
case doNGPlus:
{
int32_t topLoc = blockTopLoc(FALSE);
int32_t saveStateOp = URX_BUILD(URX_STATE_SAVE, topLoc);
fRXPat->fCompiledPat->addElement(saveStateOp, *fStatus);
}
break;
case doOpt:
{
int32_t saveStateLoc = blockTopLoc(TRUE);
int32_t saveStateOp = URX_BUILD(URX_STATE_SAVE, fRXPat->fCompiledPat->size());
fRXPat->fCompiledPat->setElementAt(saveStateOp, saveStateLoc);
}
break;
case doNGOpt:
{
int32_t jmp1_loc = blockTopLoc(TRUE);
int32_t jmp2_loc = fRXPat->fCompiledPat->size();
int32_t jmp1_op = URX_BUILD(URX_JMP, jmp2_loc+1);
fRXPat->fCompiledPat->setElementAt(jmp1_op, jmp1_loc);
int32_t jmp2_op = URX_BUILD(URX_JMP, jmp2_loc+2);
fRXPat->fCompiledPat->addElement(jmp2_op, *fStatus);
int32_t save_op = URX_BUILD(URX_STATE_SAVE, jmp1_loc+1);
fRXPat->fCompiledPat->addElement(save_op, *fStatus);
}
break;
case doStar:
{
int32_t topLoc = blockTopLoc(FALSE);
int32_t dataLoc = -1;
if (topLoc == fRXPat->fCompiledPat->size() - 1) {
int32_t repeatedOp = fRXPat->fCompiledPat->elementAti(topLoc);
if (URX_TYPE(repeatedOp) == URX_SETREF) {
int32_t loopOpI = URX_BUILD(URX_LOOP_SR_I, URX_VAL(repeatedOp));
fRXPat->fCompiledPat->setElementAt(loopOpI, topLoc);
dataLoc = fRXPat->fFrameSize;
fRXPat->fFrameSize++;
int32_t loopOpC = URX_BUILD(URX_LOOP_C, dataLoc);
fRXPat->fCompiledPat->addElement(loopOpC, *fStatus);
break;
}
if (URX_TYPE(repeatedOp) == URX_DOTANY ||
URX_TYPE(repeatedOp) == URX_DOTANY_ALL) {
int32_t loopOpI = URX_BUILD(URX_LOOP_DOT_I, 0);
if (URX_TYPE(repeatedOp) == URX_DOTANY_ALL) {
loopOpI |= 1;
}
fRXPat->fCompiledPat->setElementAt(loopOpI, topLoc);
dataLoc = fRXPat->fFrameSize;
fRXPat->fFrameSize++;
int32_t loopOpC = URX_BUILD(URX_LOOP_C, dataLoc);
fRXPat->fCompiledPat->addElement(loopOpC, *fStatus);
break;
}
}
int32_t saveStateLoc = blockTopLoc(TRUE);
int32_t jmpOp = URX_BUILD(URX_JMP_SAV, saveStateLoc+1);
if (minMatchLength(saveStateLoc, fRXPat->fCompiledPat->size()-1) == 0) {
insertOp(saveStateLoc);
dataLoc = fRXPat->fFrameSize;
fRXPat->fFrameSize++;
int32_t op = URX_BUILD(URX_STO_INP_LOC, dataLoc);
fRXPat->fCompiledPat->setElementAt(op, saveStateLoc+1);
jmpOp = URX_BUILD(URX_JMP_SAV_X, saveStateLoc+2);
}
int32_t continueLoc = fRXPat->fCompiledPat->size()+1;
int32_t saveStateOp = URX_BUILD(URX_STATE_SAVE, continueLoc);
fRXPat->fCompiledPat->setElementAt(saveStateOp, saveStateLoc);
fRXPat->fCompiledPat->addElement(jmpOp, *fStatus);
}
break;
case doNGStar:
{
int32_t jmpLoc = blockTopLoc(TRUE); int32_t saveLoc = fRXPat->fCompiledPat->size(); int32_t jmpOp = URX_BUILD(URX_JMP, saveLoc);
int32_t stateSaveOp = URX_BUILD(URX_STATE_SAVE, jmpLoc+1);
fRXPat->fCompiledPat->setElementAt(jmpOp, jmpLoc);
fRXPat->fCompiledPat->addElement(stateSaveOp, *fStatus);
}
break;
case doIntervalInit:
fIntervalLow = 0;
fIntervalUpper = -1;
break;
case doIntevalLowerDigit:
{
int32_t digitValue = u_charDigitValue(fC.fChar);
U_ASSERT(digitValue >= 0);
fIntervalLow = fIntervalLow*10 + digitValue;
if (fIntervalLow < 0) {
error(U_REGEX_NUMBER_TOO_BIG);
}
}
break;
case doIntervalUpperDigit:
{
if (fIntervalUpper < 0) {
fIntervalUpper = 0;
}
int32_t digitValue = u_charDigitValue(fC.fChar);
U_ASSERT(digitValue >= 0);
fIntervalUpper = fIntervalUpper*10 + digitValue;
if (fIntervalUpper < 0) {
error(U_REGEX_NUMBER_TOO_BIG);
}
}
break;
case doIntervalSame:
fIntervalUpper = fIntervalLow;
break;
case doInterval:
if (compileInlineInterval() == FALSE) {
compileInterval(URX_CTR_INIT, URX_CTR_LOOP);
}
break;
case doPossessiveInterval:
{
int32_t topLoc = blockTopLoc(FALSE);
compileInterval(URX_CTR_INIT, URX_CTR_LOOP);
insertOp(topLoc);
int32_t varLoc = fRXPat->fDataSize; fRXPat->fDataSize += 1; int32_t op = URX_BUILD(URX_STO_SP, varLoc);
fRXPat->fCompiledPat->setElementAt(op, topLoc);
int32_t loopOp = fRXPat->fCompiledPat->popi();
U_ASSERT(URX_TYPE(loopOp) == URX_CTR_LOOP && URX_VAL(loopOp) == topLoc);
loopOp++; fRXPat->fCompiledPat->push(loopOp, *fStatus);
op = URX_BUILD(URX_LD_SP, varLoc);
fRXPat->fCompiledPat->addElement(op, *fStatus);
}
break;
case doNGInterval:
compileInterval(URX_CTR_INIT_NG, URX_CTR_LOOP_NG);
break;
case doIntervalError:
error(U_REGEX_BAD_INTERVAL);
break;
case doLiteralChar:
literalChar(fC.fChar);
break;
case doDotAny:
{
int32_t op;
if (fModeFlags & UREGEX_DOTALL) {
op = URX_BUILD(URX_DOTANY_ALL, 0);
} else {
op = URX_BUILD(URX_DOTANY, 0);
}
fRXPat->fCompiledPat->addElement(op, *fStatus);
}
break;
case doCaret:
{
int32_t op = (fModeFlags & UREGEX_MULTILINE)? URX_CARET_M : URX_CARET;
fRXPat->fCompiledPat->addElement(URX_BUILD(op, 0), *fStatus);
}
break;
case doDollar:
{
int32_t op = (fModeFlags & UREGEX_MULTILINE)? URX_DOLLAR_M : URX_DOLLAR;
fRXPat->fCompiledPat->addElement(URX_BUILD(op, 0), *fStatus);
}
break;
case doBackslashA:
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_CARET, 0), *fStatus);
break;
case doBackslashB:
{
#if UCONFIG_NO_BREAK_ITERATION==1
if (fModeFlags & UREGEX_UWORD) {
error(U_UNSUPPORTED_ERROR);
}
#endif
int32_t op = (fModeFlags & UREGEX_UWORD)? URX_BACKSLASH_BU : URX_BACKSLASH_B;
fRXPat->fCompiledPat->addElement(URX_BUILD(op, 1), *fStatus);
}
break;
case doBackslashb:
{
#if UCONFIG_NO_BREAK_ITERATION==1
if (fModeFlags & UREGEX_UWORD) {
error(U_UNSUPPORTED_ERROR);
}
#endif
int32_t op = (fModeFlags & UREGEX_UWORD)? URX_BACKSLASH_BU : URX_BACKSLASH_B;
fRXPat->fCompiledPat->addElement(URX_BUILD(op, 0), *fStatus);
}
break;
case doBackslashD:
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_BACKSLASH_D, 1), *fStatus);
break;
case doBackslashd:
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_BACKSLASH_D, 0), *fStatus);
break;
case doBackslashG:
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_BACKSLASH_G, 0), *fStatus);
break;
case doBackslashS:
fRXPat->fCompiledPat->addElement(
URX_BUILD(URX_STAT_SETREF_N, URX_ISSPACE_SET), *fStatus);
break;
case doBackslashs:
fRXPat->fCompiledPat->addElement(
URX_BUILD(URX_STATIC_SETREF, URX_ISSPACE_SET), *fStatus);
break;
case doBackslashW:
fRXPat->fCompiledPat->addElement(
URX_BUILD(URX_STAT_SETREF_N, URX_ISWORD_SET), *fStatus);
break;
case doBackslashw:
fRXPat->fCompiledPat->addElement(
URX_BUILD(URX_STATIC_SETREF, URX_ISWORD_SET), *fStatus);
break;
case doBackslashX:
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_BACKSLASH_X, 0), *fStatus);
break;
case doBackslashZ:
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_DOLLAR, 0), *fStatus);
break;
case doBackslashz:
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_BACKSLASH_Z, 0), *fStatus);
break;
case doEscapeError:
error(U_REGEX_BAD_ESCAPE_SEQUENCE);
break;
case doExit:
returnVal = FALSE;
break;
case doProperty:
{
UnicodeSet *theSet = scanProp();
compileSet(theSet);
}
break;
case doScanUnicodeSet:
{
UnicodeSet *theSet = scanSet();
compileSet(theSet);
}
break;
case doEnterQuoteMode:
fQuoteMode = TRUE;
break;
case doBackRef:
{
int32_t numCaptureGroups = fRXPat->fGroupMap->size();
int32_t groupNum = 0;
UChar32 c = fC.fChar;
for (;;) {
int32_t digit = u_charDigitValue(c);
groupNum = groupNum * 10 + digit;
if (groupNum >= numCaptureGroups) {
break;
}
c = peekCharLL();
if (RegexStaticSets::gStaticSets->fRuleDigits->contains(c) == FALSE) {
break;
}
nextCharLL();
}
U_ASSERT(groupNum > 0);
int32_t op;
if (fModeFlags & UREGEX_CASE_INSENSITIVE) {
op = URX_BUILD(URX_BACKREF_I, groupNum);
} else {
op = URX_BUILD(URX_BACKREF, groupNum);
}
fRXPat->fCompiledPat->addElement(op, *fStatus);
}
break;
case doPossessivePlus:
{
int32_t topLoc = blockTopLoc(TRUE);
int32_t stoLoc = fRXPat->fDataSize;
fRXPat->fDataSize++; int32_t op = URX_BUILD(URX_STO_SP, stoLoc);
fRXPat->fCompiledPat->setElementAt(op, topLoc);
op = URX_BUILD(URX_STATE_SAVE, fRXPat->fCompiledPat->size()+2);
fRXPat->fCompiledPat->addElement(op, *fStatus);
op = URX_BUILD(URX_JMP, topLoc+1);
fRXPat->fCompiledPat->addElement(op, *fStatus);
op = URX_BUILD(URX_LD_SP, stoLoc);
fRXPat->fCompiledPat->addElement(op, *fStatus);
}
break;
case doPossessiveStar:
{
int32_t topLoc = blockTopLoc(TRUE);
insertOp(topLoc);
int32_t stoLoc = fRXPat->fDataSize;
fRXPat->fDataSize++; int32_t op = URX_BUILD(URX_STO_SP, stoLoc);
fRXPat->fCompiledPat->setElementAt(op, topLoc);
int32_t L7 = fRXPat->fCompiledPat->size()+1;
op = URX_BUILD(URX_STATE_SAVE, L7);
fRXPat->fCompiledPat->setElementAt(op, topLoc+1);
op = URX_BUILD(URX_JMP, topLoc+1);
fRXPat->fCompiledPat->addElement(op, *fStatus);
op = URX_BUILD(URX_LD_SP, stoLoc);
fRXPat->fCompiledPat->addElement(op, *fStatus);
}
break;
case doPossessiveOpt:
{
int32_t topLoc = blockTopLoc(TRUE);
insertOp(topLoc);
int32_t stoLoc = fRXPat->fDataSize;
fRXPat->fDataSize++; int32_t op = URX_BUILD(URX_STO_SP, stoLoc);
fRXPat->fCompiledPat->setElementAt(op, topLoc);
int32_t continueLoc = fRXPat->fCompiledPat->size()+1;
op = URX_BUILD(URX_STATE_SAVE, continueLoc);
fRXPat->fCompiledPat->setElementAt(op, topLoc+1);
op = URX_BUILD(URX_LD_SP, stoLoc);
fRXPat->fCompiledPat->addElement(op, *fStatus);
}
break;
case doBeginMatchMode:
fNewModeFlags = fModeFlags;
fSetModeFlag = TRUE;
break;
case doMatchMode: {
int32_t bit = 0;
switch (fC.fChar) {
case 0x69: bit = UREGEX_CASE_INSENSITIVE; break;
case 0x6d: bit = UREGEX_MULTILINE; break;
case 0x73: bit = UREGEX_DOTALL; break;
case 0x77: bit = UREGEX_UWORD; break;
case 0x78: bit = UREGEX_COMMENTS; break;
case 0x2d: fSetModeFlag = FALSE; break;
default:
U_ASSERT(FALSE); }
if (fSetModeFlag) {
fNewModeFlags |= bit;
} else {
fNewModeFlags &= ~bit;
}
}
break;
case doSetMatchMode:
fModeFlags = fNewModeFlags;
fixLiterals();
break;
case doMatchModeParen:
{
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_NOP, 0), *fStatus);
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_NOP, 0), *fStatus);
fParenStack.push(fModeFlags, *fStatus);
fParenStack.push(flags, *fStatus); fParenStack.push(fRXPat->fCompiledPat->size()-2, *fStatus); fParenStack.push(fRXPat->fCompiledPat->size()-1, *fStatus);
fModeFlags = fNewModeFlags;
}
break;
case doBadModeFlag:
error(U_REGEX_INVALID_FLAG);
break;
case doSuppressComments:
fEOLComments = FALSE;
break;
default:
U_ASSERT(FALSE);
error(U_REGEX_INTERNAL_ERROR);
break;
}
if (U_FAILURE(*fStatus)) {
returnVal = FALSE;
}
return returnVal;
};
void RegexCompile::literalChar(UChar32 c) {
int32_t op; int32_t opType;
int32_t patternLoc; int32_t stringLen;
op = fRXPat->fCompiledPat->lastElementi();
opType = URX_TYPE(op);
if (!(opType == URX_STRING_LEN || opType == URX_ONECHAR || opType == URX_ONECHAR_I)) {
fixLiterals();
}
if (fStringOpStart == -1) {
emitONE_CHAR(c);
fStringOpStart = fRXPat->fLiteralText.length();
fRXPat->fLiteralText.append(c);
return;
}
fRXPat->fLiteralText.append(c);
op = fRXPat->fCompiledPat->lastElementi();
opType = URX_TYPE(op);
U_ASSERT(opType == URX_ONECHAR || opType == URX_ONECHAR_I || opType == URX_STRING_LEN);
if (opType == URX_ONECHAR || opType == URX_ONECHAR_I) {
if (U16_IS_TRAIL(c) && U16_IS_LEAD(URX_VAL(op))) {
c = U16_GET_SUPPLEMENTARY(URX_VAL(op), c);
op = URX_BUILD(opType, c);
patternLoc = fRXPat->fCompiledPat->size() - 1;
fRXPat->fCompiledPat->setElementAt(op, patternLoc);
return;
}
if (fModeFlags & UREGEX_CASE_INSENSITIVE) {
op = URX_BUILD(URX_STRING_I, fStringOpStart);
} else {
op = URX_BUILD(URX_STRING, fStringOpStart);
}
patternLoc = fRXPat->fCompiledPat->size() - 1;
fRXPat->fCompiledPat->setElementAt(op, patternLoc);
op = URX_BUILD(URX_STRING_LEN, 0);
fRXPat->fCompiledPat->addElement(op, *fStatus);
}
stringLen = fRXPat->fLiteralText.length() - fStringOpStart;
op = URX_BUILD(URX_STRING_LEN, stringLen);
patternLoc = fRXPat->fCompiledPat->size() - 1;
fRXPat->fCompiledPat->setElementAt(op, patternLoc);
}
void RegexCompile::emitONE_CHAR(UChar32 c) {
int32_t op;
if ((fModeFlags & UREGEX_CASE_INSENSITIVE) &&
u_hasBinaryProperty(c, UCHAR_CASE_SENSITIVE)) {
c = u_foldCase(c, U_FOLD_CASE_DEFAULT);
op = URX_BUILD(URX_ONECHAR_I, c);
} else {
op = URX_BUILD(URX_ONECHAR, c);
}
fRXPat->fCompiledPat->addElement(op, *fStatus);
}
void RegexCompile::fixLiterals(UBool split) {
int32_t stringStart = fStringOpStart; int32_t op; int32_t opType; int32_t stringLastCharIdx;
UChar32 lastChar;
int32_t stringNextToLastCharIdx;
UChar32 nextToLastChar;
int32_t stringLen;
fStringOpStart = -1;
if (!split) {
return;
}
op = fRXPat->fCompiledPat->lastElementi();
opType = URX_TYPE(op);
if (opType != URX_STRING_LEN) {
return;
}
stringLen = URX_VAL(op);
stringLastCharIdx = fRXPat->fLiteralText.length();
stringLastCharIdx = fRXPat->fLiteralText.moveIndex32(stringLastCharIdx, -1);
lastChar = fRXPat->fLiteralText.char32At(stringLastCharIdx);
U_ASSERT(stringLastCharIdx > stringStart);
stringNextToLastCharIdx = fRXPat->fLiteralText.moveIndex32(stringLastCharIdx, -1);
U_ASSERT(stringNextToLastCharIdx >= stringStart);
nextToLastChar = fRXPat->fLiteralText.char32At(stringNextToLastCharIdx);
if (stringNextToLastCharIdx > stringStart) {
stringLen -= (fRXPat->fLiteralText.length() - stringLastCharIdx);
op = URX_BUILD(URX_STRING_LEN, stringLen);
fRXPat->fCompiledPat->setElementAt(op, fRXPat->fCompiledPat->size() -1);
emitONE_CHAR(lastChar);
} else {
fRXPat->fCompiledPat->setSize(fRXPat->fCompiledPat->size() -2);
emitONE_CHAR(nextToLastChar);
emitONE_CHAR(lastChar);
}
}
void RegexCompile::insertOp(int32_t where) {
UVector32 *code = fRXPat->fCompiledPat;
U_ASSERT(where>0 && where < code->size());
int32_t nop = URX_BUILD(URX_NOP, 0);
code->insertElementAt(nop, where, *fStatus);
int32_t loc;
for (loc=0; loc<code->size(); loc++) {
int32_t op = code->elementAti(loc);
int32_t opType = URX_TYPE(op);
int32_t opValue = URX_VAL(op);
if ((opType == URX_JMP ||
opType == URX_JMPX ||
opType == URX_STATE_SAVE ||
opType == URX_CTR_LOOP ||
opType == URX_CTR_LOOP_NG ||
opType == URX_JMP_SAV ||
opType == URX_RELOC_OPRND) && opValue > where) {
opValue++;
op = URX_BUILD(opType, opValue);
code->setElementAt(op, loc);
}
}
for (loc=0; loc<fParenStack.size(); loc++) {
int32_t x = fParenStack.elementAti(loc);
if (x>where) {
x++;
fParenStack.setElementAt(x, loc);
}
}
if (fMatchCloseParen > where) {
fMatchCloseParen++;
}
if (fMatchOpenParen > where) {
fMatchOpenParen++;
}
}
int32_t RegexCompile::blockTopLoc(UBool reserveLoc) {
int32_t theLoc;
if (fRXPat->fCompiledPat->size() == fMatchCloseParen)
{
theLoc = fMatchOpenParen; U_ASSERT(theLoc > 0);
U_ASSERT(URX_TYPE(((uint32_t)fRXPat->fCompiledPat->elementAti(theLoc))) == URX_NOP);
}
else {
fixLiterals(TRUE); theLoc = fRXPat->fCompiledPat->size()-1;
if (reserveLoc) {
int32_t nop = URX_BUILD(URX_NOP, 0);
fRXPat->fCompiledPat->insertElementAt(nop, theLoc, *fStatus);
}
}
return theLoc;
}
void RegexCompile::handleCloseParen() {
int32_t patIdx;
int32_t patOp;
if (fParenStack.size() <= 0) {
error(U_REGEX_MISMATCHED_PAREN);
return;
}
fixLiterals(FALSE);
for (;;) {
patIdx = fParenStack.popi();
if (patIdx < 0) {
break;
}
U_ASSERT(patIdx>0 && patIdx <= fRXPat->fCompiledPat->size());
patOp = fRXPat->fCompiledPat->elementAti(patIdx);
U_ASSERT(URX_VAL(patOp) == 0); patOp |= fRXPat->fCompiledPat->size(); fRXPat->fCompiledPat->setElementAt(patOp, patIdx);
fMatchOpenParen = patIdx;
}
fModeFlags = fParenStack.popi();
switch (patIdx) {
case plain:
case flags:
break;
case capturing:
{
int32_t captureOp = fRXPat->fCompiledPat->elementAti(fMatchOpenParen+1);
U_ASSERT(URX_TYPE(captureOp) == URX_START_CAPTURE);
int32_t frameVarLocation = URX_VAL(captureOp);
int32_t endCaptureOp = URX_BUILD(URX_END_CAPTURE, frameVarLocation);
fRXPat->fCompiledPat->addElement(endCaptureOp, *fStatus);
}
break;
case atomic:
{
int32_t stoOp = fRXPat->fCompiledPat->elementAti(fMatchOpenParen+1);
U_ASSERT(URX_TYPE(stoOp) == URX_STO_SP);
int32_t stoLoc = URX_VAL(stoOp);
int32_t ldOp = URX_BUILD(URX_LD_SP, stoLoc);
fRXPat->fCompiledPat->addElement(ldOp, *fStatus);
}
break;
case lookAhead:
{
int32_t startOp = fRXPat->fCompiledPat->elementAti(fMatchOpenParen-1);
U_ASSERT(URX_TYPE(startOp) == URX_LA_START);
int32_t dataLoc = URX_VAL(startOp);
int32_t op = URX_BUILD(URX_LA_END, dataLoc);
fRXPat->fCompiledPat->addElement(op, *fStatus);
}
break;
case negLookAhead:
{
int32_t startOp = fRXPat->fCompiledPat->elementAti(fMatchOpenParen-1);
U_ASSERT(URX_TYPE(startOp) == URX_LA_START);
int32_t dataLoc = URX_VAL(startOp);
int32_t op = URX_BUILD(URX_LA_END, dataLoc);
fRXPat->fCompiledPat->addElement(op, *fStatus);
op = URX_BUILD(URX_FAIL, 0);
fRXPat->fCompiledPat->addElement(op, *fStatus);
int32_t saveOp = fRXPat->fCompiledPat->elementAti(fMatchOpenParen);
U_ASSERT(URX_TYPE(saveOp) == URX_STATE_SAVE);
int32_t dest = fRXPat->fCompiledPat->size();
saveOp = URX_BUILD(URX_STATE_SAVE, dest);
fRXPat->fCompiledPat->setElementAt(saveOp, fMatchOpenParen);
}
break;
case lookBehind:
{
int32_t startOp = fRXPat->fCompiledPat->elementAti(fMatchOpenParen-4);
U_ASSERT(URX_TYPE(startOp) == URX_LB_START);
int32_t dataLoc = URX_VAL(startOp);
int32_t op = URX_BUILD(URX_LB_END, dataLoc);
fRXPat->fCompiledPat->addElement(op, *fStatus);
op = URX_BUILD(URX_LA_END, dataLoc);
fRXPat->fCompiledPat->addElement(op, *fStatus);
int32_t patEnd = fRXPat->fCompiledPat->size() - 1;
int32_t minML = minMatchLength(fMatchOpenParen, patEnd);
int32_t maxML = maxMatchLength(fMatchOpenParen, patEnd);
if (maxML == INT32_MAX) {
error(U_REGEX_LOOK_BEHIND_LIMIT);
break;
}
U_ASSERT(minML <= maxML);
fRXPat->fCompiledPat->setElementAt(minML, fMatchOpenParen-2);
fRXPat->fCompiledPat->setElementAt(maxML, fMatchOpenParen-1);
}
break;
case lookBehindN:
{
int32_t startOp = fRXPat->fCompiledPat->elementAti(fMatchOpenParen-5);
U_ASSERT(URX_TYPE(startOp) == URX_LB_START);
int32_t dataLoc = URX_VAL(startOp);
int32_t op = URX_BUILD(URX_LBN_END, dataLoc);
fRXPat->fCompiledPat->addElement(op, *fStatus);
int32_t patEnd = fRXPat->fCompiledPat->size() - 1;
int32_t minML = minMatchLength(fMatchOpenParen, patEnd);
int32_t maxML = maxMatchLength(fMatchOpenParen, patEnd);
if (maxML == INT32_MAX) {
error(U_REGEX_LOOK_BEHIND_LIMIT);
break;
}
U_ASSERT(minML <= maxML);
fRXPat->fCompiledPat->setElementAt(minML, fMatchOpenParen-3);
fRXPat->fCompiledPat->setElementAt(maxML, fMatchOpenParen-2);
op = URX_BUILD(URX_RELOC_OPRND, fRXPat->fCompiledPat->size());
fRXPat->fCompiledPat->setElementAt(op, fMatchOpenParen-1);
}
break;
default:
U_ASSERT(FALSE);
}
fMatchCloseParen = fRXPat->fCompiledPat->size();
}
void RegexCompile::compileSet(UnicodeSet *theSet)
{
if (theSet == NULL) {
return;
}
int32_t setSize = theSet->size();
UChar32 firstSetChar = theSet->charAt(0);
if (firstSetChar == -1) {
error(U_REGEX_SET_CONTAINS_STRING);
setSize = 0;
}
switch (setSize) {
case 0:
{
fRXPat->fCompiledPat->addElement(URX_BUILD(URX_BACKTRACK, 0), *fStatus);
delete theSet;
}
break;
case 1:
{
literalChar(firstSetChar);
delete theSet;
}
break;
default:
{
int32_t setNumber = fRXPat->fSets->size();
fRXPat->fSets->addElement(theSet, *fStatus);
int32_t setOp = URX_BUILD(URX_SETREF, setNumber);
fRXPat->fCompiledPat->addElement(setOp, *fStatus);
}
}
}
void RegexCompile::compileInterval(int32_t InitOp, int32_t LoopOp)
{
int32_t topOfBlock = blockTopLoc(TRUE);
insertOp(topOfBlock);
insertOp(topOfBlock);
insertOp(topOfBlock);
int32_t counterLoc = fRXPat->fFrameSize;
fRXPat->fFrameSize++;
int32_t op = URX_BUILD(InitOp, counterLoc);
fRXPat->fCompiledPat->setElementAt(op, topOfBlock);
int32_t loopEnd = fRXPat->fCompiledPat->size();
op = URX_BUILD(URX_RELOC_OPRND, loopEnd);
fRXPat->fCompiledPat->setElementAt(op, topOfBlock+1);
fRXPat->fCompiledPat->setElementAt(fIntervalLow, topOfBlock+2);
fRXPat->fCompiledPat->setElementAt(fIntervalUpper, topOfBlock+3);
op = URX_BUILD(LoopOp, topOfBlock);
fRXPat->fCompiledPat->addElement(op, *fStatus);
if ((fIntervalLow & 0xff000000) != 0 ||
fIntervalUpper > 0 && (fIntervalUpper & 0xff000000) != 0) {
error(U_REGEX_NUMBER_TOO_BIG);
}
if (fIntervalLow > fIntervalUpper && fIntervalUpper != -1) {
error(U_REGEX_MAX_LT_MIN);
}
}
UBool RegexCompile::compileInlineInterval() {
if (fIntervalUpper > 10 || fIntervalUpper < fIntervalLow) {
return FALSE;
}
int32_t topOfBlock = blockTopLoc(FALSE);
if (fIntervalUpper == 0) {
fRXPat->fCompiledPat->setSize(topOfBlock);
return TRUE;
}
if (topOfBlock != fRXPat->fCompiledPat->size()-1 && fIntervalUpper != 1) {
return FALSE;
}
int32_t op = fRXPat->fCompiledPat->elementAti(topOfBlock);
int32_t endOfSequenceLoc = fRXPat->fCompiledPat->size()-1
+ fIntervalUpper + (fIntervalUpper-fIntervalLow);
int32_t saveOp = URX_BUILD(URX_STATE_SAVE, endOfSequenceLoc);
if (fIntervalLow == 0) {
insertOp(topOfBlock);
fRXPat->fCompiledPat->setElementAt(saveOp, topOfBlock);
}
int32_t i;
for (i=1; i<fIntervalUpper; i++ ) {
if (i == fIntervalLow) {
fRXPat->fCompiledPat->addElement(saveOp, *fStatus);
}
if (i > fIntervalLow) {
fRXPat->fCompiledPat->addElement(saveOp, *fStatus);
}
fRXPat->fCompiledPat->addElement(op, *fStatus);
}
return TRUE;
}
void RegexCompile::matchStartType() {
if (U_FAILURE(*fStatus)) {
return;
}
int32_t loc; int32_t op; int32_t opType; int32_t currentLen = 0; int32_t numInitialStrings = 0;
UBool atStart = TRUE;
int32_t end = fRXPat->fCompiledPat->size();
UVector32 forwardedLength(end+1, *fStatus);
forwardedLength.setSize(end+1);
for (loc=3; loc<end; loc++) {
forwardedLength.setElementAt(INT32_MAX, loc);
}
for (loc = 3; loc<end; loc++) {
op = fRXPat->fCompiledPat->elementAti(loc);
opType = URX_TYPE(op);
U_ASSERT(currentLen>=0 && currentLen < INT32_MAX);
if (forwardedLength.elementAti(loc) < currentLen) {
currentLen = forwardedLength.elementAti(loc);
U_ASSERT(currentLen>=0 && currentLen < INT32_MAX);
}
switch (opType) {
case URX_RESERVED_OP:
case URX_END:
case URX_STRING_LEN:
case URX_NOP:
case URX_START_CAPTURE:
case URX_END_CAPTURE:
case URX_BACKSLASH_B:
case URX_BACKSLASH_BU:
case URX_BACKSLASH_G:
case URX_BACKSLASH_Z:
case URX_DOLLAR:
case URX_RELOC_OPRND:
case URX_STO_INP_LOC:
case URX_DOLLAR_M:
case URX_BACKTRACK:
case URX_BACKREF: case URX_BACKREF_I:
case URX_STO_SP: case URX_LD_SP:
break;
case URX_CARET:
if (atStart) {
fRXPat->fStartType = START_START;
}
break;
case URX_CARET_M:
if (atStart) {
fRXPat->fStartType = START_LINE;
}
break;
case URX_ONECHAR:
if (currentLen == 0) {
fRXPat->fInitialChars->add(URX_VAL(op));
numInitialStrings += 2;
}
currentLen++;
atStart = FALSE;
break;
case URX_SETREF:
if (currentLen == 0) {
int32_t sn = URX_VAL(op);
U_ASSERT(sn > 0 && sn < fRXPat->fSets->size());
const UnicodeSet *s = (UnicodeSet *)fRXPat->fSets->elementAt(sn);
fRXPat->fInitialChars->addAll(*s);
numInitialStrings += 2;
}
currentLen++;
atStart = FALSE;
break;
case URX_LOOP_SR_I:
if (currentLen == 0) {
int32_t sn = URX_VAL(op);
U_ASSERT(sn > 0 && sn < fRXPat->fSets->size());
const UnicodeSet *s = (UnicodeSet *)fRXPat->fSets->elementAt(sn);
fRXPat->fInitialChars->addAll(*s);
numInitialStrings += 2;
}
atStart = FALSE;
break;
case URX_LOOP_DOT_I:
if (currentLen == 0) {
fRXPat->fInitialChars->clear();
fRXPat->fInitialChars->complement();
numInitialStrings += 2;
}
atStart = FALSE;
break;
case URX_STATIC_SETREF:
if (currentLen == 0) {
int32_t sn = URX_VAL(op);
U_ASSERT(sn>0 && sn<URX_LAST_SET);
const UnicodeSet *s = fRXPat->fStaticSets[sn];
fRXPat->fInitialChars->addAll(*s);
numInitialStrings += 2;
}
currentLen++;
atStart = FALSE;
break;
case URX_STAT_SETREF_N:
if (currentLen == 0) {
int32_t sn = URX_VAL(op);
const UnicodeSet *s = fRXPat->fStaticSets[sn];
UnicodeSet sc(*s);
sc.complement();
fRXPat->fInitialChars->addAll(sc);
numInitialStrings += 2;
}
currentLen++;
atStart = FALSE;
break;
case URX_BACKSLASH_D:
if (currentLen == 0) {
UnicodeSet s;
s.applyIntPropertyValue(UCHAR_GENERAL_CATEGORY_MASK, U_GC_ND_MASK, *fStatus);
if (URX_VAL(op) != 0) {
s.complement();
}
fRXPat->fInitialChars->addAll(s);
numInitialStrings += 2;
}
currentLen++;
atStart = FALSE;
break;
case URX_ONECHAR_I:
if (currentLen == 0) {
UChar32 c = URX_VAL(op);
if (u_hasBinaryProperty(c, UCHAR_CASE_SENSITIVE)) {
UnicodeSet s(c, c);
s.closeOver(USET_CASE);
fRXPat->fInitialChars->addAll(s);
} else {
fRXPat->fInitialChars->add(c);
}
numInitialStrings += 2;
}
currentLen++;
atStart = FALSE;
break;
case URX_BACKSLASH_X: case URX_DOTANY_ALL: case URX_DOTANY:
case URX_DOTANY_ALL_PL:
case URX_DOTANY_PL:
if (currentLen == 0) {
fRXPat->fInitialChars->clear();
fRXPat->fInitialChars->complement();
numInitialStrings += 2;
}
currentLen++;
atStart = FALSE;
break;
case URX_JMPX:
loc++; case URX_JMP:
{
int32_t jmpDest = URX_VAL(op);
if (jmpDest < loc) {
currentLen = forwardedLength.elementAti(loc+1);
} else {
U_ASSERT(jmpDest <= end+1);
if (forwardedLength.elementAti(jmpDest) > currentLen) {
forwardedLength.setElementAt(currentLen, jmpDest);
}
}
}
atStart = FALSE;
break;
case URX_JMP_SAV:
case URX_JMP_SAV_X:
atStart = FALSE;
break;
case URX_FAIL:
currentLen = forwardedLength.elementAti(loc+1);
atStart = FALSE;
break;
case URX_STATE_SAVE:
{
int32_t jmpDest = URX_VAL(op);
if (jmpDest > loc) {
if (currentLen < forwardedLength.elementAti(jmpDest)) {
forwardedLength.setElementAt(currentLen, jmpDest);
}
}
}
atStart = FALSE;
break;
case URX_STRING:
{
loc++;
int32_t stringLenOp = fRXPat->fCompiledPat->elementAti(loc);
int32_t stringLen = URX_VAL(stringLenOp);
U_ASSERT(URX_TYPE(stringLenOp) == URX_STRING_LEN);
U_ASSERT(stringLenOp >= 2);
if (currentLen == 0) {
int32_t stringStartIdx = URX_VAL(op);
UChar32 c = fRXPat->fLiteralText.char32At(stringStartIdx);
fRXPat->fInitialChars->add(c);
numInitialStrings++;
fRXPat->fInitialStringIdx = stringStartIdx;
fRXPat->fInitialStringLen = stringLen;
}
currentLen += stringLen;
atStart = FALSE;
}
break;
case URX_STRING_I:
{
loc++;
int32_t stringLenOp = fRXPat->fCompiledPat->elementAti(loc);
int32_t stringLen = URX_VAL(stringLenOp);
U_ASSERT(URX_TYPE(stringLenOp) == URX_STRING_LEN);
U_ASSERT(stringLenOp >= 2);
if (currentLen == 0) {
int32_t stringStartIdx = URX_VAL(op);
UChar32 c = fRXPat->fLiteralText.char32At(stringStartIdx);
UnicodeSet s(c, c);
s.closeOver(USET_CASE);
fRXPat->fInitialChars->addAll(s);
numInitialStrings += 2; }
currentLen += stringLen;
atStart = FALSE;
}
break;
case URX_CTR_INIT:
case URX_CTR_INIT_NG:
{
int32_t loopEndLoc = fRXPat->fCompiledPat->elementAti(loc+1);
loopEndLoc = URX_VAL(loopEndLoc);
int32_t minLoopCount = fRXPat->fCompiledPat->elementAti(loc+2);
if (minLoopCount == 0) {
U_ASSERT(loopEndLoc <= end+1);
if (forwardedLength.elementAti(loopEndLoc) > currentLen) {
forwardedLength.setElementAt(currentLen, loopEndLoc);
}
}
loc+=3; }
atStart = FALSE;
break;
case URX_CTR_LOOP:
case URX_CTR_LOOP_NG:
atStart = FALSE;
break;
case URX_LOOP_C:
atStart = FALSE;
break;
case URX_LA_START:
case URX_LB_START:
{
int32_t depth = 0;
for (;;) {
loc++;
op = fRXPat->fCompiledPat->elementAti(loc);
if (URX_TYPE(op) == URX_LA_START || URX_TYPE(op) == URX_LB_START) {
depth++;
}
if (URX_TYPE(op) == URX_LA_END || URX_TYPE(op)==URX_LBN_END) {
if (depth == 0) {
break;
}
depth--;
}
if (URX_TYPE(op) == URX_STATE_SAVE) {
int32_t jmpDest = URX_VAL(op);
if (jmpDest > loc) {
if (currentLen < forwardedLength.elementAti(jmpDest)) {
forwardedLength.setElementAt(currentLen, jmpDest);
}
}
}
U_ASSERT(loc <= end);
}
}
break;
case URX_LA_END:
case URX_LB_CONT:
case URX_LB_END:
case URX_LBN_CONT:
case URX_LBN_END:
U_ASSERT(FALSE);
break;
default:
U_ASSERT(FALSE);
}
}
if (forwardedLength.elementAti(end+1) < currentLen) {
currentLen = forwardedLength.elementAti(end+1);
}
fRXPat->fInitialChars8->init(fRXPat->fInitialChars);
if (fRXPat->fStartType == START_START) {
} else if (numInitialStrings == 1 && fRXPat->fMinMatchLen > 0) {
UChar32 c = fRXPat->fLiteralText.char32At(fRXPat->fInitialStringIdx);
U_ASSERT(fRXPat->fInitialChars->contains(c));
fRXPat->fStartType = START_STRING;
fRXPat->fInitialChar = c;
} else if (fRXPat->fStartType == START_LINE) {
} else if (fRXPat->fMinMatchLen == 0) {
fRXPat->fStartType = START_NO_INFO;
} else if (fRXPat->fInitialChars->size() == 1) {
fRXPat->fStartType = START_CHAR;
fRXPat->fInitialChar = fRXPat->fInitialChars->charAt(0);
U_ASSERT(fRXPat->fInitialChar != (UChar32)-1);
} else if (fRXPat->fInitialChars->contains((UChar32)0, (UChar32)0x10ffff) == FALSE &&
fRXPat->fMinMatchLen > 0) {
fRXPat->fStartType = START_SET;
} else {
fRXPat->fStartType = START_NO_INFO;
}
return;
}
int32_t RegexCompile::minMatchLength(int32_t start, int32_t end) {
if (U_FAILURE(*fStatus)) {
return 0;
}
U_ASSERT(start <= end);
U_ASSERT(end < fRXPat->fCompiledPat->size());
int32_t loc;
int32_t op;
int32_t opType;
int32_t currentLen = 0;
UVector32 forwardedLength(end+2, *fStatus);
forwardedLength.setSize(end+2);
for (loc=start; loc<=end+1; loc++) {
forwardedLength.setElementAt(INT32_MAX, loc);
}
for (loc = start; loc<=end; loc++) {
op = fRXPat->fCompiledPat->elementAti(loc);
opType = URX_TYPE(op);
U_ASSERT(currentLen>=0 && currentLen < INT32_MAX);
if (forwardedLength.elementAti(loc) < currentLen) {
currentLen = forwardedLength.elementAti(loc);
U_ASSERT(currentLen>=0 && currentLen < INT32_MAX);
}
switch (opType) {
case URX_RESERVED_OP:
case URX_END:
case URX_STRING_LEN:
case URX_NOP:
case URX_START_CAPTURE:
case URX_END_CAPTURE:
case URX_BACKSLASH_B:
case URX_BACKSLASH_BU:
case URX_BACKSLASH_G:
case URX_BACKSLASH_Z:
case URX_CARET:
case URX_DOLLAR:
case URX_RELOC_OPRND:
case URX_STO_INP_LOC:
case URX_DOLLAR_M:
case URX_CARET_M:
case URX_BACKTRACK:
case URX_BACKREF: case URX_BACKREF_I:
case URX_STO_SP: case URX_LD_SP:
case URX_JMP_SAV:
case URX_JMP_SAV_X:
break;
case URX_ONECHAR:
case URX_STATIC_SETREF:
case URX_STAT_SETREF_N:
case URX_SETREF:
case URX_BACKSLASH_D:
case URX_ONECHAR_I:
case URX_BACKSLASH_X: case URX_DOTANY_ALL: case URX_DOTANY:
case URX_DOTANY_PL:
case URX_DOTANY_ALL_PL:
currentLen++;
break;
case URX_JMPX:
loc++; case URX_JMP:
{
int32_t jmpDest = URX_VAL(op);
if (jmpDest < loc) {
currentLen = forwardedLength.elementAti(loc+1);
} else {
U_ASSERT(jmpDest <= end+1);
if (forwardedLength.elementAti(jmpDest) > currentLen) {
forwardedLength.setElementAt(currentLen, jmpDest);
}
}
}
break;
case URX_FAIL:
{
currentLen = forwardedLength.elementAti(loc+1);
U_ASSERT(currentLen>=0 && currentLen < INT32_MAX);
}
break;
case URX_STATE_SAVE:
{
int32_t jmpDest = URX_VAL(op);
if (jmpDest > loc) {
if (currentLen < forwardedLength.elementAti(jmpDest)) {
forwardedLength.setElementAt(currentLen, jmpDest);
}
}
}
break;
case URX_STRING:
case URX_STRING_I:
{
loc++;
int32_t stringLenOp = fRXPat->fCompiledPat->elementAti(loc);
currentLen += URX_VAL(stringLenOp);
}
break;
case URX_CTR_INIT:
case URX_CTR_INIT_NG:
{
int32_t loopEndLoc = fRXPat->fCompiledPat->elementAti(loc+1);
loopEndLoc = URX_VAL(loopEndLoc);
int32_t minLoopCount = fRXPat->fCompiledPat->elementAti(loc+2);
if (minLoopCount == 0) {
loc = loopEndLoc;
} else {
loc+=3; }
}
break;
case URX_CTR_LOOP:
case URX_CTR_LOOP_NG:
break;
case URX_LOOP_SR_I:
case URX_LOOP_DOT_I:
case URX_LOOP_C:
break;
case URX_LA_START:
case URX_LB_START:
{
int32_t depth = 0;
for (;;) {
loc++;
op = fRXPat->fCompiledPat->elementAti(loc);
if (URX_TYPE(op) == URX_LA_START || URX_TYPE(op) == URX_LB_START) {
depth++;
}
if (URX_TYPE(op) == URX_LA_END || URX_TYPE(op)==URX_LBN_END) {
if (depth == 0) {
break;
}
depth--;
}
if (URX_TYPE(op) == URX_STATE_SAVE) {
int32_t jmpDest = URX_VAL(op);
if (jmpDest > loc) {
if (currentLen < forwardedLength.elementAti(jmpDest)) {
forwardedLength.setElementAt(currentLen, jmpDest);
}
}
}
U_ASSERT(loc <= end);
}
}
break;
case URX_LA_END:
case URX_LB_CONT:
case URX_LB_END:
case URX_LBN_CONT:
case URX_LBN_END:
break;
default:
U_ASSERT(FALSE);
}
}
if (forwardedLength.elementAti(end+1) < currentLen) {
currentLen = forwardedLength.elementAti(end+1);
U_ASSERT(currentLen>=0 && currentLen < INT32_MAX);
}
return currentLen;
}
int32_t RegexCompile::maxMatchLength(int32_t start, int32_t end) {
if (U_FAILURE(*fStatus)) {
return 0;
}
U_ASSERT(start <= end);
U_ASSERT(end < fRXPat->fCompiledPat->size());
int32_t loc;
int32_t op;
int32_t opType;
int32_t currentLen = 0;
UVector32 forwardedLength(end+1, *fStatus);
forwardedLength.setSize(end+1);
for (loc=start; loc<=end; loc++) {
forwardedLength.setElementAt(0, loc);
}
for (loc = start; loc<=end; loc++) {
op = fRXPat->fCompiledPat->elementAti(loc);
opType = URX_TYPE(op);
if (forwardedLength.elementAti(loc) > currentLen) {
currentLen = forwardedLength.elementAti(loc);
}
switch (opType) {
case URX_RESERVED_OP:
case URX_END:
case URX_STRING_LEN:
case URX_NOP:
case URX_START_CAPTURE:
case URX_END_CAPTURE:
case URX_BACKSLASH_B:
case URX_BACKSLASH_BU:
case URX_BACKSLASH_G:
case URX_BACKSLASH_Z:
case URX_CARET:
case URX_DOLLAR:
case URX_RELOC_OPRND:
case URX_STO_INP_LOC:
case URX_DOLLAR_M:
case URX_CARET_M:
case URX_BACKTRACK:
case URX_STO_SP: case URX_LD_SP:
case URX_LB_END:
case URX_LB_CONT:
case URX_LBN_CONT:
case URX_LBN_END:
break;
case URX_BACKREF: case URX_BACKREF_I:
case URX_BACKSLASH_X: case URX_DOTANY_PL:
case URX_DOTANY_ALL_PL:
currentLen = INT32_MAX;
break;
case URX_STATIC_SETREF:
case URX_STAT_SETREF_N:
case URX_SETREF:
case URX_BACKSLASH_D:
case URX_ONECHAR_I:
case URX_DOTANY_ALL:
case URX_DOTANY:
currentLen+=2;
break;
case URX_ONECHAR:
currentLen++;
if (URX_VAL(op) > 0x10000) {
currentLen++;
}
break;
case URX_JMP:
case URX_JMPX:
case URX_JMP_SAV:
case URX_JMP_SAV_X:
{
int32_t jmpDest = URX_VAL(op);
if (jmpDest < loc) {
currentLen = INT32_MAX;
} else {
if (forwardedLength.elementAti(jmpDest) < currentLen) {
forwardedLength.setElementAt(currentLen, jmpDest);
}
currentLen = 0;
}
}
break;
case URX_FAIL:
currentLen = forwardedLength.elementAti(loc+1);
break;
case URX_STATE_SAVE:
{
int32_t jmpDest = URX_VAL(op);
if (jmpDest > loc) {
if (currentLen > forwardedLength.elementAti(jmpDest)) {
forwardedLength.setElementAt(currentLen, jmpDest);
}
} else {
currentLen = INT32_MAX;
}
}
break;
case URX_STRING:
case URX_STRING_I:
{
loc++;
int32_t stringLenOp = fRXPat->fCompiledPat->elementAti(loc);
currentLen += URX_VAL(stringLenOp);
}
break;
case URX_CTR_INIT:
case URX_CTR_INIT_NG:
case URX_CTR_LOOP:
case URX_CTR_LOOP_NG:
case URX_LOOP_SR_I:
case URX_LOOP_DOT_I:
case URX_LOOP_C:
currentLen = INT32_MAX;
break;
case URX_LA_START:
case URX_LA_END:
break;
U_ASSERT(FALSE);
break;
case URX_LB_START:
{
int32_t depth = 0;
for (;;) {
loc++;
op = fRXPat->fCompiledPat->elementAti(loc);
if (URX_TYPE(op) == URX_LA_START || URX_TYPE(op) == URX_LB_START) {
depth++;
}
if (URX_TYPE(op) == URX_LA_END || URX_TYPE(op)==URX_LBN_END) {
if (depth == 0) {
break;
}
depth--;
}
U_ASSERT(loc < end);
}
}
break;
default:
U_ASSERT(FALSE);
}
if (currentLen == INT32_MAX) {
break;
}
}
return currentLen;
}
void RegexCompile::stripNOPs() {
if (U_FAILURE(*fStatus)) {
return;
}
int32_t end = fRXPat->fCompiledPat->size();
UVector32 deltas(end, *fStatus);
int32_t loc;
int32_t d = 0;
for (loc=0; loc<end; loc++) {
deltas.addElement(d, *fStatus);
int32_t op = fRXPat->fCompiledPat->elementAti(loc);
if (URX_TYPE(op) == URX_NOP) {
d++;
}
}
int32_t src;
int32_t dst = 0;
for (src=0; src<end; src++) {
int32_t op = fRXPat->fCompiledPat->elementAti(src);
int32_t opType = URX_TYPE(op);
switch (opType) {
case URX_NOP:
break;
case URX_STATE_SAVE:
case URX_JMP:
case URX_CTR_LOOP:
case URX_CTR_LOOP_NG:
case URX_RELOC_OPRND:
case URX_JMPX:
case URX_JMP_SAV:
case URX_JMP_SAV_X:
{
int32_t operandAddress = URX_VAL(op);
U_ASSERT(operandAddress>=0 && operandAddress<deltas.size());
int32_t fixedOperandAddress = operandAddress - deltas.elementAti(operandAddress);
op = URX_BUILD(opType, fixedOperandAddress);
fRXPat->fCompiledPat->setElementAt(op, dst);
dst++;
break;
}
case URX_RESERVED_OP:
case URX_RESERVED_OP_N:
case URX_BACKTRACK:
case URX_END:
case URX_ONECHAR:
case URX_STRING:
case URX_STRING_LEN:
case URX_START_CAPTURE:
case URX_END_CAPTURE:
case URX_STATIC_SETREF:
case URX_STAT_SETREF_N:
case URX_SETREF:
case URX_DOTANY:
case URX_FAIL:
case URX_BACKSLASH_B:
case URX_BACKSLASH_BU:
case URX_BACKSLASH_G:
case URX_BACKSLASH_X:
case URX_BACKSLASH_Z:
case URX_DOTANY_ALL:
case URX_DOTANY_ALL_PL:
case URX_DOTANY_PL:
case URX_BACKSLASH_D:
case URX_CARET:
case URX_DOLLAR:
case URX_CTR_INIT:
case URX_CTR_INIT_NG:
case URX_STO_SP:
case URX_LD_SP:
case URX_BACKREF:
case URX_STO_INP_LOC:
case URX_LA_START:
case URX_LA_END:
case URX_ONECHAR_I:
case URX_STRING_I:
case URX_BACKREF_I:
case URX_DOLLAR_M:
case URX_CARET_M:
case URX_LB_START:
case URX_LB_CONT:
case URX_LB_END:
case URX_LBN_CONT:
case URX_LBN_END:
case URX_LOOP_SR_I:
case URX_LOOP_DOT_I:
case URX_LOOP_C:
fRXPat->fCompiledPat->setElementAt(op, dst);
dst++;
break;
default:
U_ASSERT(FALSE);
error(U_REGEX_INTERNAL_ERROR);
}
}
fRXPat->fCompiledPat->setSize(dst);
}
void RegexCompile::OptDotStar() {
int32_t jmpLoc;
int32_t op = 0;
int32_t opType;
for (jmpLoc=fRXPat->fCompiledPat->size(); jmpLoc--;) {
U_ASSERT(jmpLoc>0);
op = fRXPat->fCompiledPat->elementAti(jmpLoc);
opType = URX_TYPE(op);
switch(opType) {
case URX_END:
case URX_NOP:
case URX_END_CAPTURE:
case URX_DOLLAR_M:
case URX_DOLLAR:
case URX_BACKSLASH_Z:
continue;
case URX_JMP_SAV:
break;
default:
return;
}
break; }
int32_t loopTopLoc = URX_VAL(op);
if (loopTopLoc != jmpLoc-1) {
return;
}
int32_t newOp;
int32_t oldOp = fRXPat->fCompiledPat->elementAti(loopTopLoc);
int32_t oldOpType = opType = URX_TYPE(oldOp);
if (oldOpType == URX_DOTANY) {
newOp = URX_BUILD(URX_DOTANY_PL, 0);
}
else if (oldOpType == URX_DOTANY_ALL) {
newOp = URX_BUILD(URX_DOTANY_ALL_PL, 0);
} else {
return; }
fRXPat->fCompiledPat->setElementAt(URX_BUILD(URX_NOP, 0), loopTopLoc);
fRXPat->fCompiledPat->setElementAt(newOp, jmpLoc);
}
void RegexCompile::error(UErrorCode e) {
if (U_SUCCESS(*fStatus)) {
*fStatus = e;
fParseErr->line = fLineNum;
fParseErr->offset = fCharNum;
uprv_memset(fParseErr->preContext, 0, sizeof(fParseErr->preContext));
uprv_memset(fParseErr->postContext, 0, sizeof(fParseErr->postContext));
fRXPat->fPattern.extractBetween(fScanIndex-U_PARSE_CONTEXT_LEN+1, fScanIndex,
fParseErr->preContext, 0);
fRXPat->fPattern.extractBetween(fScanIndex, fScanIndex+U_PARSE_CONTEXT_LEN-1,
fParseErr->postContext, 0);
}
}
static const UChar chCR = 0x0d; static const UChar chLF = 0x0a;
static const UChar chNEL = 0x85; static const UChar chLS = 0x2028; static const UChar chApos = 0x27; static const UChar chPound = 0x23; static const UChar chE = 0x45; static const UChar chBackSlash = 0x5c; static const UChar chLParen = 0x28;
static const UChar chRParen = 0x29;
static const UChar chLBracket = 0x5b;
static const UChar chRBracket = 0x5d;
static const UChar chRBrace = 0x7d;
static const UChar chUpperN = 0x4E;
static const UChar chLowerP = 0x70;
static const UChar chUpperP = 0x50;
UChar32 RegexCompile::nextCharLL() {
UChar32 ch;
UnicodeString &pattern = fRXPat->fPattern;
if (fPeekChar != -1) {
ch = fPeekChar;
fPeekChar = -1;
return ch;
}
if (fPatternLength==0 || fNextIndex >= fPatternLength) {
return (UChar32)-1;
}
ch = pattern.char32At(fNextIndex);
fNextIndex = pattern.moveIndex32(fNextIndex, 1);
if (ch == chCR ||
ch == chNEL ||
ch == chLS ||
ch == chLF && fLastChar != chCR) {
fLineNum++;
fCharNum=0;
if (fQuoteMode) {
error(U_REGEX_RULE_SYNTAX);
fQuoteMode = FALSE;
}
}
else {
if (ch != chLF) {
fCharNum++;
}
}
fLastChar = ch;
return ch;
}
UChar32 RegexCompile::peekCharLL() {
if (fPeekChar == -1) {
fPeekChar = nextCharLL();
}
return fPeekChar;
}
void RegexCompile::nextChar(RegexPatternChar &c) {
fScanIndex = fNextIndex;
c.fChar = nextCharLL();
c.fQuoted = FALSE;
if (fQuoteMode) {
c.fQuoted = TRUE;
if ((c.fChar==chBackSlash && peekCharLL()==chE) || c.fChar == (UChar32)-1) {
fQuoteMode = FALSE; nextCharLL(); nextChar(c); }
}
else if (fInBackslashQuote) {
fInBackslashQuote = FALSE;
}
else
{
if (fModeFlags & UREGEX_COMMENTS) {
for (;;) {
if (c.fChar == (UChar32)-1) {
break; }
if (c.fChar == chPound && fEOLComments == TRUE) {
for (;;) {
c.fChar = nextCharLL();
if (c.fChar == (UChar32)-1 || c.fChar == chCR ||
c.fChar == chLF ||
c.fChar == chNEL ||
c.fChar == chLS) {
break;
}
}
}
if (uprv_isRuleWhiteSpace(c.fChar) == FALSE) {
break;
}
c.fChar = nextCharLL();
}
}
int32_t startX = fNextIndex; int32_t endX = fNextIndex; if (c.fChar == chBackSlash) {
if (RegexStaticSets::gStaticSets->fUnescapeCharSet->contains(peekCharLL())) {
nextCharLL(); c.fQuoted = TRUE;
c.fChar = fRXPat->fPattern.unescapeAt(endX);
if (startX == endX) {
error(U_REGEX_BAD_ESCAPE_SEQUENCE);
}
fCharNum += endX - startX;
fNextIndex = endX;
}
else
{
fInBackslashQuote = TRUE;
}
}
}
fEOLComments = TRUE;
}
UnicodeSet *RegexCompile::scanSet() {
UnicodeSet *uset = NULL;
ParsePosition pos;
int startPos;
int i;
if (U_FAILURE(*fStatus)) {
return NULL;
}
pos.setIndex(fScanIndex);
startPos = fScanIndex;
UErrorCode localStatus = U_ZERO_ERROR;
uint32_t usetFlags = 0;
if (fModeFlags & UREGEX_CASE_INSENSITIVE) {
usetFlags |= USET_CASE_INSENSITIVE;
}
if (fModeFlags & UREGEX_COMMENTS) {
usetFlags |= USET_IGNORE_SPACE;
}
uset = new UnicodeSet(fRXPat->fPattern, pos,
usetFlags, NULL, localStatus);
if (U_FAILURE(localStatus)) {
REGEX_SCAN_DEBUG_PRINTF(("UnicodeSet parse postion.ErrorIndex = %d\n", pos.getIndex()));
error(localStatus);
delete uset;
return NULL;
}
i = pos.getIndex();
for (;;) {
if (fNextIndex >= i) {
break;
}
nextCharLL();
}
return uset;
};
UnicodeSet *RegexCompile::scanProp() {
UnicodeSet *uset = NULL;
if (U_FAILURE(*fStatus)) {
return NULL;
}
U_ASSERT(fC.fChar == chLowerP || fC.fChar == chUpperP || fC.fChar == chUpperN);
UnicodeString setPattern;
setPattern.append(chLBracket);
setPattern.append(chBackSlash);
for (;;) {
setPattern.append(fC.fChar);
if (fC.fChar == chRBrace) {
break;
}
nextChar(fC);
if (fC.fChar == -1) {
error(U_REGEX_PROPERTY_SYNTAX);
return NULL;
}
}
setPattern.append(chRBracket);
uint32_t usetFlags = 0;
if (fModeFlags & UREGEX_CASE_INSENSITIVE) {
usetFlags |= USET_CASE_INSENSITIVE;
}
if (fModeFlags & UREGEX_COMMENTS) {
usetFlags |= USET_IGNORE_SPACE;
}
uset = new UnicodeSet(setPattern, usetFlags, NULL, *fStatus);
if (U_FAILURE(*fStatus)) {
delete uset;
uset = NULL;
}
nextChar(fC); return uset;
};
U_NAMESPACE_END
#endif // !UCONFIG_NO_REGULAR_EXPRESSIONS