X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/57a6839dcb3bba09e8228b822b290604668416fe..340931cb2e044a2141d11567dd0f782524e32994:/icuSources/i18n/regexcmp.cpp diff --git a/icuSources/i18n/regexcmp.cpp b/icuSources/i18n/regexcmp.cpp index a27f5eec..7274ca9a 100644 --- a/icuSources/i18n/regexcmp.cpp +++ b/icuSources/i18n/regexcmp.cpp @@ -1,7 +1,9 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html // // file: regexcmp.cpp // -// Copyright (C) 2002-2014 International Business Machines Corporation and others. +// Copyright (C) 2002-2016 International Business Machines Corporation and others. // All Rights Reserved. // // This file contains the ICU regular expression compiler, which is responsible @@ -26,11 +28,11 @@ #include "patternprops.h" #include "putilimp.h" #include "cmemory.h" +#include "cstr.h" #include "cstring.h" #include "uvectr32.h" #include "uvectr64.h" #include "uassert.h" -#include "ucln_in.h" #include "uinvchar.h" #include "regeximp.h" @@ -71,6 +73,8 @@ RegexCompile::RegexCompile(RegexPattern *rxp, UErrorCode &status) : fMatchOpenParen = -1; fMatchCloseParen = -1; + fCaptureName = NULL; + fLastSetLiteral = U_SENTINEL; if (U_SUCCESS(status) && U_FAILURE(rxp->fDeferredStatus)) { status = rxp->fDeferredStatus; @@ -87,6 +91,8 @@ static const UChar chDash = 0x2d; // '-' // //------------------------------------------------------------------------------ RegexCompile::~RegexCompile() { + delete fCaptureName; // Normally will be NULL, but can exist if pattern + // compilation stops with a syntax error. } static inline void addCategory(UnicodeSet *set, int32_t value, UErrorCode& ec) { @@ -139,6 +145,9 @@ void RegexCompile::compile( // Prepare the RegexPattern object to receive the compiled pattern. fRXPat->fPattern = utext_clone(fRXPat->fPattern, pat, FALSE, TRUE, fStatus); + if (U_FAILURE(*fStatus)) { + return; + } fRXPat->fStaticSets = RegexStaticSets::gStaticSets->fPropSets; fRXPat->fStaticSets8 = RegexStaticSets::gStaticSets->fPropSets8; @@ -284,17 +293,6 @@ void RegexCompile::compile( // The pattern has now been read and processed, and the compiled code generated. // - // - // Compute the number of digits requried for the largest capture group number. - // - fRXPat->fMaxCaptureDigits = 1; - int32_t n = 10; - int32_t groupCount = fRXPat->fGroupMap->size(); - while (n <= groupCount) { - fRXPat->fMaxCaptureDigits++; - n *= 10; - } - // // The pattern's fFrameSize so far has accumulated the requirements for // storage for capture parentheses, counters, etc. that are encountered @@ -302,7 +300,7 @@ void RegexCompile::compile( // present in the saved state: the input string position (int64_t) and // the position in the compiled pattern. // - fRXPat->fFrameSize+=RESTACKFRAME_HDRCOUNT; + allocateStackData(RESTACKFRAME_HDRCOUNT); // // Optimization pass 1: NOPs, back-references, and case-folding @@ -368,9 +366,9 @@ UBool RegexCompile::doParseActions(int32_t action) // the start of an ( grouping. //4 NOP Resreved, will be replaced by a save if there are // OR | operators at the top level - 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); + appendOp(URX_STATE_SAVE, 2); + appendOp(URX_JMP, 3); + appendOp(URX_FAIL, 0); // Standard open nonCapture paren action emits the two NOPs and // sets up the paren stack frame. @@ -393,7 +391,7 @@ UBool RegexCompile::doParseActions(int32_t action) } // add the END operation to the compiled pattern. - fRXPat->fCompiledPat->addElement(URX_BUILD(URX_END, 0), *fStatus); + appendOp(URX_END, 0); // Terminate the pattern compilation state machine. returnVal = FALSE; @@ -415,14 +413,13 @@ UBool RegexCompile::doParseActions(int32_t action) int32_t savePosition = fParenStack.popi(); int32_t op = (int32_t)fRXPat->fCompiledPat->elementAti(savePosition); U_ASSERT(URX_TYPE(op) == URX_NOP); // original contents of reserved location - op = URX_BUILD(URX_STATE_SAVE, fRXPat->fCompiledPat->size()+1); + op = buildOp(URX_STATE_SAVE, fRXPat->fCompiledPat->size()+1); fRXPat->fCompiledPat->setElementAt(op, savePosition); // Append an JMP operation into the compiled pattern. The operand for // the JMP will eventually be the location following the ')' for the // group. This will be patched in later, when the ')' is encountered. - op = URX_BUILD(URX_JMP, 0); - fRXPat->fCompiledPat->addElement(op, *fStatus); + appendOp(URX_JMP, 0); // Push the position of the newly added JMP op onto the parentheses stack. // This registers if for fixup when this block's close paren is encountered. @@ -431,14 +428,31 @@ UBool RegexCompile::doParseActions(int32_t action) // Append a NOP to the compiled pattern. This is the slot reserved // for a SAVE in the event that there is yet another '|' following // this one. - fRXPat->fCompiledPat->addElement(URX_BUILD(URX_NOP, 0), *fStatus); + appendOp(URX_NOP, 0); fParenStack.push(fRXPat->fCompiledPat->size()-1, *fStatus); } break; + case doBeginNamedCapture: + // Scanning (?append(fC.fChar); + break; + + case doBadNamedCapture: + error(U_REGEX_INVALID_CAPTURE_GROUP_NAME); + break; + case doOpenCaptureParen: - // Open Paren. + // Open Capturing Paren, possibly named. // Compile to a // - NOP, which later may be replaced by a save-state if the // parenthesized group gets a * quantifier, followed by @@ -457,12 +471,10 @@ UBool RegexCompile::doParseActions(int32_t action) // END_CAPTURE is encountered. { fixLiterals(); - fRXPat->fCompiledPat->addElement(URX_BUILD(URX_NOP, 0), *fStatus); - int32_t varsLoc = fRXPat->fFrameSize; // Reserve three slots in match stack frame. - 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); + appendOp(URX_NOP, 0); + int32_t varsLoc = allocateStackData(3); // Reserve three slots in match stack frame. + appendOp(URX_START_CAPTURE, varsLoc); + appendOp(URX_NOP, 0); // On the Parentheses stack, start a new frame and add the postions // of the two NOPs. Depending on what follows in the pattern, the @@ -475,8 +487,24 @@ UBool RegexCompile::doParseActions(int32_t action) // Save the mapping from group number to stack frame variable position. fRXPat->fGroupMap->addElement(varsLoc, *fStatus); + + // If this is a named capture group, add the name->group number mapping. + if (fCaptureName != NULL) { + if (!fRXPat->initNamedCaptureMap()) { + if (U_SUCCESS(*fStatus)) { + error(fRXPat->fDeferredStatus); + } + break; + } + int32_t groupNumber = fRXPat->fGroupMap->size(); + int32_t previousMapping = uhash_puti(fRXPat->fNamedCaptureMap, fCaptureName, groupNumber, fStatus); + fCaptureName = NULL; // hash table takes ownership of the name (key) string. + if (previousMapping > 0 && U_SUCCESS(*fStatus)) { + error(U_REGEX_INVALID_CAPTURE_GROUP_NAME); + } + } } - break; + break; case doOpenNonCaptureParen: // Open non-caputuring (grouping only) Paren. @@ -487,8 +515,8 @@ UBool RegexCompile::doParseActions(int32_t action) // is an '|' alternation within the parens. { fixLiterals(); - fRXPat->fCompiledPat->addElement(URX_BUILD(URX_NOP, 0), *fStatus); - fRXPat->fCompiledPat->addElement(URX_BUILD(URX_NOP, 0), *fStatus); + appendOp(URX_NOP, 0); + appendOp(URX_NOP, 0); // On the Parentheses stack, start a new frame and add the postions // of the two NOPs. @@ -510,12 +538,10 @@ UBool RegexCompile::doParseActions(int32_t action) // is an '|' alternation within the parens. { fixLiterals(); - fRXPat->fCompiledPat->addElement(URX_BUILD(URX_NOP, 0), *fStatus); - int32_t varLoc = fRXPat->fDataSize; // Reserve a data location for saving the - fRXPat->fDataSize += 1; // state stack ptr. - int32_t stoOp = URX_BUILD(URX_STO_SP, varLoc); - fRXPat->fCompiledPat->addElement(stoOp, *fStatus); - fRXPat->fCompiledPat->addElement(URX_BUILD(URX_NOP, 0), *fStatus); + appendOp(URX_NOP, 0); + int32_t varLoc = allocateData(1); // Reserve a data location for saving the state stack ptr. + appendOp(URX_STO_SP, varLoc); + appendOp(URX_NOP, 0); // On the Parentheses stack, start a new frame and add the postions // of the two NOPs. Depending on what follows in the pattern, the @@ -541,7 +567,7 @@ UBool RegexCompile::doParseActions(int32_t action) // sequence; don't change without making updates there too. // // Compiles to - // 1 START_LA dataLoc Saves SP, Input Pos + // 1 LA_START dataLoc Saves SP, Input Pos, Active input region. // 2. STATE_SAVE 4 on failure of lookahead, goto 4 // 3 JMP 6 continue ... // @@ -555,29 +581,21 @@ UBool RegexCompile::doParseActions(int32_t action) // 8. code for parenthesized stuff. // 9. LA_END // - // Two data slots are reserved, for saving the stack ptr and the input position. + // Four data slots are reserved, for saving state on entry to the look-around + // 0: stack pointer on entry. + // 1: input position on entry. + // 2: fActiveStart, the active bounds start on entry. + // 3: fActiveLimit, the active bounds limit on entry. { fixLiterals(); - 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, fRXPat->fCompiledPat->size()+ 2); - fRXPat->fCompiledPat->addElement(op, *fStatus); - - op = URX_BUILD(URX_JMP, fRXPat->fCompiledPat->size()+ 3); - fRXPat->fCompiledPat->addElement(op, *fStatus); - - op = URX_BUILD(URX_LA_END, dataLoc); - fRXPat->fCompiledPat->addElement(op, *fStatus); - - op = URX_BUILD(URX_BACKTRACK, 0); - fRXPat->fCompiledPat->addElement(op, *fStatus); - - op = URX_BUILD(URX_NOP, 0); - fRXPat->fCompiledPat->addElement(op, *fStatus); - fRXPat->fCompiledPat->addElement(op, *fStatus); + int32_t dataLoc = allocateData(4); + appendOp(URX_LA_START, dataLoc); + appendOp(URX_STATE_SAVE, fRXPat->fCompiledPat->size()+ 2); + appendOp(URX_JMP, fRXPat->fCompiledPat->size()+ 3); + appendOp(URX_LA_END, dataLoc); + appendOp(URX_BACKTRACK, 0); + appendOp(URX_NOP, 0); + appendOp(URX_NOP, 0); // On the Parentheses stack, start a new frame and add the postions // of the NOPs. @@ -591,27 +609,26 @@ UBool RegexCompile::doParseActions(int32_t action) case doOpenLookAheadNeg: // Negated Lookahead. (?! stuff ) // Compiles to - // 1. START_LA dataloc + // 1. LA_START dataloc // 2. SAVE_STATE 7 // Fail within look-ahead block restores to this state, // // which continues with the match. // 3. NOP // Std. Open Paren sequence, for possible '|' // 4. code for parenthesized stuff. - // 5. END_LA // Cut back stack, remove saved state from step 2. + // 5. LA_END // Cut back stack, remove saved state from step 2. // 6. BACKTRACK // code in block succeeded, so neg. lookahead fails. // 7. END_LA // Restore match region, in case look-ahead was using // an alternate (transparent) region. + // Four data slots are reserved, for saving state on entry to the look-around + // 0: stack pointer on entry. + // 1: input position on entry. + // 2: fActiveStart, the active bounds start on entry. + // 3: fActiveLimit, the active bounds limit on entry. { fixLiterals(); - 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); // dest address will be patched later. - fRXPat->fCompiledPat->addElement(op, *fStatus); - - op = URX_BUILD(URX_NOP, 0); - fRXPat->fCompiledPat->addElement(op, *fStatus); + int32_t dataLoc = allocateData(4); + appendOp(URX_LA_START, dataLoc); + appendOp(URX_STATE_SAVE, 0); // dest address will be patched later. + appendOp(URX_NOP, 0); // On the Parentheses stack, start a new frame and add the postions // of the StateSave and NOP. @@ -642,30 +659,28 @@ UBool RegexCompile::doParseActions(int32_t action) // Allocate a block of matcher data, to contain (when running a match) // 0: Stack ptr on entry // 1: Input Index on entry - // 2: Start index of match current match attempt. - // 3: Original Input String len. + // 2: fActiveStart, the active bounds start on entry. + // 3: fActiveLimit, the active bounds limit on entry. + // 4: Start index of match current match attempt. + // The first four items must match the layout of data for LA_START / LA_END // Generate match code for any pending literals. fixLiterals(); // Allocate data space - int32_t dataLoc = fRXPat->fDataSize; - fRXPat->fDataSize += 4; + int32_t dataLoc = allocateData(5); // Emit URX_LB_START - int32_t op = URX_BUILD(URX_LB_START, dataLoc); - fRXPat->fCompiledPat->addElement(op, *fStatus); + appendOp(URX_LB_START, dataLoc); // Emit URX_LB_CONT - op = URX_BUILD(URX_LB_CONT, dataLoc); - fRXPat->fCompiledPat->addElement(op, *fStatus); - fRXPat->fCompiledPat->addElement(0, *fStatus); // MinMatchLength. To be filled later. - fRXPat->fCompiledPat->addElement(0, *fStatus); // MaxMatchLength. To be filled later. + appendOp(URX_LB_CONT, dataLoc); + appendOp(URX_RESERVED_OP, 0); // MinMatchLength. To be filled later. + appendOp(URX_RESERVED_OP, 0); // MaxMatchLength. To be filled later. - // Emit the NOP - op = URX_BUILD(URX_NOP, 0); - fRXPat->fCompiledPat->addElement(op, *fStatus); - fRXPat->fCompiledPat->addElement(op, *fStatus); + // Emit the NOPs + appendOp(URX_NOP, 0); + appendOp(URX_NOP, 0); // On the Parentheses stack, start a new frame and add the postions // of the URX_LB_CONT and the NOP. @@ -698,31 +713,29 @@ UBool RegexCompile::doParseActions(int32_t action) // Allocate a block of matcher data, to contain (when running a match) // 0: Stack ptr on entry // 1: Input Index on entry - // 2: Start index of match current match attempt. - // 3: Original Input String len. + // 2: fActiveStart, the active bounds start on entry. + // 3: fActiveLimit, the active bounds limit on entry. + // 4: Start index of match current match attempt. + // The first four items must match the layout of data for LA_START / LA_END // Generate match code for any pending literals. fixLiterals(); // Allocate data space - int32_t dataLoc = fRXPat->fDataSize; - fRXPat->fDataSize += 4; + int32_t dataLoc = allocateData(5); // Emit URX_LB_START - int32_t op = URX_BUILD(URX_LB_START, dataLoc); - fRXPat->fCompiledPat->addElement(op, *fStatus); + appendOp(URX_LB_START, dataLoc); // Emit URX_LBN_CONT - op = URX_BUILD(URX_LBN_CONT, dataLoc); - fRXPat->fCompiledPat->addElement(op, *fStatus); - fRXPat->fCompiledPat->addElement(0, *fStatus); // MinMatchLength. To be filled later. - fRXPat->fCompiledPat->addElement(0, *fStatus); // MaxMatchLength. To be filled later. - fRXPat->fCompiledPat->addElement(0, *fStatus); // Continue Loc. To be filled later. + appendOp(URX_LBN_CONT, dataLoc); + appendOp(URX_RESERVED_OP, 0); // MinMatchLength. To be filled later. + appendOp(URX_RESERVED_OP, 0); // MaxMatchLength. To be filled later. + appendOp(URX_RESERVED_OP, 0); // Continue Loc. To be filled later. - // Emit the NOP - op = URX_BUILD(URX_NOP, 0); - fRXPat->fCompiledPat->addElement(op, *fStatus); - fRXPat->fCompiledPat->addElement(op, *fStatus); + // Emit the NOPs + appendOp(URX_NOP, 0); + appendOp(URX_NOP, 0); // On the Parentheses stack, start a new frame and add the postions // of the URX_LB_CONT and the NOP. @@ -792,12 +805,9 @@ UBool RegexCompile::doParseActions(int32_t action) if (URX_TYPE(repeatedOp) == URX_SETREF) { // Emit optimized code for [char set]+ - 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); + appendOp(URX_LOOP_SR_I, URX_VAL(repeatedOp)); + frameLoc = allocateStackData(1); + appendOp(URX_LOOP_C, frameLoc); break; } @@ -805,7 +815,7 @@ UBool RegexCompile::doParseActions(int32_t action) URX_TYPE(repeatedOp) == URX_DOTANY_ALL || URX_TYPE(repeatedOp) == URX_DOTANY_UNIX) { // Emit Optimized code for .+ operations. - int32_t loopOpI = URX_BUILD(URX_LOOP_DOT_I, 0); + int32_t loopOpI = buildOp(URX_LOOP_DOT_I, 0); if (URX_TYPE(repeatedOp) == URX_DOTANY_ALL) { // URX_LOOP_DOT_I operand is a flag indicating ". matches any" mode. loopOpI |= 1; @@ -813,11 +823,9 @@ UBool RegexCompile::doParseActions(int32_t action) if (fModeFlags & UREGEX_UNIX_LINES) { loopOpI |= 2; } - fRXPat->fCompiledPat->addElement(loopOpI, *fStatus); - frameLoc = fRXPat->fFrameSize; - fRXPat->fFrameSize++; - int32_t loopOpC = URX_BUILD(URX_LOOP_C, frameLoc); - fRXPat->fCompiledPat->addElement(loopOpC, *fStatus); + appendOp(loopOpI); + frameLoc = allocateStackData(1); + appendOp(URX_LOOP_C, frameLoc); break; } @@ -831,18 +839,15 @@ UBool RegexCompile::doParseActions(int32_t action) // Zero length match is possible. // Emit the code sequence that can handle it. insertOp(topLoc); - frameLoc = fRXPat->fFrameSize; - fRXPat->fFrameSize++; + frameLoc = allocateStackData(1); - int32_t op = URX_BUILD(URX_STO_INP_LOC, frameLoc); + int32_t op = buildOp(URX_STO_INP_LOC, frameLoc); fRXPat->fCompiledPat->setElementAt(op, topLoc); - op = URX_BUILD(URX_JMP_SAV_X, topLoc+1); - fRXPat->fCompiledPat->addElement(op, *fStatus); + appendOp(URX_JMP_SAV_X, topLoc+1); } else { // Simpler code when the repeated body must match something non-empty - int32_t jmpOp = URX_BUILD(URX_JMP_SAV, topLoc); - fRXPat->fCompiledPat->addElement(jmpOp, *fStatus); + appendOp(URX_JMP_SAV, topLoc); } } break; @@ -854,8 +859,7 @@ UBool RegexCompile::doParseActions(int32_t action) // 3. ... { int32_t topLoc = blockTopLoc(FALSE); - int32_t saveStateOp = URX_BUILD(URX_STATE_SAVE, topLoc); - fRXPat->fCompiledPat->addElement(saveStateOp, *fStatus); + appendOp(URX_STATE_SAVE, topLoc); } break; @@ -869,7 +873,7 @@ UBool RegexCompile::doParseActions(int32_t action) // Insert the state save into the compiled pattern, and we're done. { int32_t saveStateLoc = blockTopLoc(TRUE); - int32_t saveStateOp = URX_BUILD(URX_STATE_SAVE, fRXPat->fCompiledPat->size()); + int32_t saveStateOp = buildOp(URX_STATE_SAVE, fRXPat->fCompiledPat->size()); fRXPat->fCompiledPat->setElementAt(saveStateOp, saveStateLoc); } break; @@ -888,14 +892,12 @@ UBool RegexCompile::doParseActions(int32_t action) int32_t jmp1_loc = blockTopLoc(TRUE); int32_t jmp2_loc = fRXPat->fCompiledPat->size(); - int32_t jmp1_op = URX_BUILD(URX_JMP, jmp2_loc+1); + int32_t jmp1_op = buildOp(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); + appendOp(URX_JMP, jmp2_loc+2); - int32_t save_op = URX_BUILD(URX_STATE_SAVE, jmp1_loc+1); - fRXPat->fCompiledPat->addElement(save_op, *fStatus); + appendOp(URX_STATE_SAVE, jmp1_loc+1); } break; @@ -935,12 +937,10 @@ UBool RegexCompile::doParseActions(int32_t action) if (URX_TYPE(repeatedOp) == URX_SETREF) { // Emit optimized code for a [char set]* - int32_t loopOpI = URX_BUILD(URX_LOOP_SR_I, URX_VAL(repeatedOp)); + int32_t loopOpI = buildOp(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); + dataLoc = allocateStackData(1); + appendOp(URX_LOOP_C, dataLoc); break; } @@ -948,7 +948,7 @@ UBool RegexCompile::doParseActions(int32_t action) URX_TYPE(repeatedOp) == URX_DOTANY_ALL || URX_TYPE(repeatedOp) == URX_DOTANY_UNIX) { // Emit Optimized code for .* operations. - int32_t loopOpI = URX_BUILD(URX_LOOP_DOT_I, 0); + int32_t loopOpI = buildOp(URX_LOOP_DOT_I, 0); if (URX_TYPE(repeatedOp) == URX_DOTANY_ALL) { // URX_LOOP_DOT_I operand is a flag indicating . matches any mode. loopOpI |= 1; @@ -957,10 +957,8 @@ UBool RegexCompile::doParseActions(int32_t action) loopOpI |= 2; } fRXPat->fCompiledPat->setElementAt(loopOpI, topLoc); - dataLoc = fRXPat->fFrameSize; - fRXPat->fFrameSize++; - int32_t loopOpC = URX_BUILD(URX_LOOP_C, dataLoc); - fRXPat->fCompiledPat->addElement(loopOpC, *fStatus); + dataLoc = allocateStackData(1); + appendOp(URX_LOOP_C, dataLoc); break; } } @@ -969,30 +967,29 @@ UBool RegexCompile::doParseActions(int32_t action) // The optimizations did not apply. int32_t saveStateLoc = blockTopLoc(TRUE); - int32_t jmpOp = URX_BUILD(URX_JMP_SAV, saveStateLoc+1); + int32_t jmpOp = buildOp(URX_JMP_SAV, saveStateLoc+1); // Check for minimum match length of zero, which requires // extra loop-breaking code. if (minMatchLength(saveStateLoc, fRXPat->fCompiledPat->size()-1) == 0) { insertOp(saveStateLoc); - dataLoc = fRXPat->fFrameSize; - fRXPat->fFrameSize++; + dataLoc = allocateStackData(1); - int32_t op = URX_BUILD(URX_STO_INP_LOC, dataLoc); + int32_t op = buildOp(URX_STO_INP_LOC, dataLoc); fRXPat->fCompiledPat->setElementAt(op, saveStateLoc+1); - jmpOp = URX_BUILD(URX_JMP_SAV_X, saveStateLoc+2); + jmpOp = buildOp(URX_JMP_SAV_X, saveStateLoc+2); } // Locate the position in the compiled pattern where the match will continue // after completing the *. (4 or 5 in the comment above) int32_t continueLoc = fRXPat->fCompiledPat->size()+1; - // Put together the save state op store it into the compiled code. - int32_t saveStateOp = URX_BUILD(URX_STATE_SAVE, continueLoc); + // Put together the save state op and store it into the compiled code. + int32_t saveStateOp = buildOp(URX_STATE_SAVE, continueLoc); fRXPat->fCompiledPat->setElementAt(saveStateOp, saveStateLoc); // Append the URX_JMP_SAV or URX_JMPX operation to the compiled pattern. - fRXPat->fCompiledPat->addElement(jmpOp, *fStatus); + appendOp(jmpOp); } break; @@ -1006,10 +1003,9 @@ UBool RegexCompile::doParseActions(int32_t action) { int32_t jmpLoc = blockTopLoc(TRUE); // loc 1. int32_t saveLoc = fRXPat->fCompiledPat->size(); // loc 3. - int32_t jmpOp = URX_BUILD(URX_JMP, saveLoc); - int32_t stateSaveOp = URX_BUILD(URX_STATE_SAVE, jmpLoc+1); + int32_t jmpOp = buildOp(URX_JMP, saveLoc); fRXPat->fCompiledPat->setElementAt(jmpOp, jmpLoc); - fRXPat->fCompiledPat->addElement(stateSaveOp, *fStatus); + appendOp(URX_STATE_SAVE, jmpLoc+1); } break; @@ -1027,9 +1023,11 @@ UBool RegexCompile::doParseActions(int32_t action) { int32_t digitValue = u_charDigitValue(fC.fChar); U_ASSERT(digitValue >= 0); - fIntervalLow = fIntervalLow*10 + digitValue; - if (fIntervalLow < 0) { + int64_t val = (int64_t)fIntervalLow*10 + digitValue; + if (val > INT32_MAX) { error(U_REGEX_NUMBER_TOO_BIG); + } else { + fIntervalLow = (int32_t)val; } } break; @@ -1042,9 +1040,11 @@ UBool RegexCompile::doParseActions(int32_t action) } int32_t digitValue = u_charDigitValue(fC.fChar); U_ASSERT(digitValue >= 0); - fIntervalUpper = fIntervalUpper*10 + digitValue; - if (fIntervalUpper < 0) { + int64_t val = (int64_t)fIntervalUpper*10 + digitValue; + if (val > INT32_MAX) { error(U_REGEX_NUMBER_TOO_BIG); + } else { + fIntervalUpper = (int32_t)val; } } break; @@ -1078,9 +1078,9 @@ UBool RegexCompile::doParseActions(int32_t action) // First the STO_SP before the start of the loop insertOp(topLoc); - int32_t varLoc = fRXPat->fDataSize; // Reserve a data location for saving the - fRXPat->fDataSize += 1; // state stack ptr. - int32_t op = URX_BUILD(URX_STO_SP, varLoc); + + int32_t varLoc = allocateData(1); // Reserve a data location for saving the + int32_t op = buildOp(URX_STO_SP, varLoc); fRXPat->fCompiledPat->setElementAt(op, topLoc); int32_t loopOp = (int32_t)fRXPat->fCompiledPat->popi(); @@ -1089,8 +1089,7 @@ UBool RegexCompile::doParseActions(int32_t action) fRXPat->fCompiledPat->push(loopOp, *fStatus); // Then the LD_SP after the end of the loop - op = URX_BUILD(URX_LD_SP, varLoc); - fRXPat->fCompiledPat->addElement(op, *fStatus); + appendOp(URX_LD_SP, varLoc); } break; @@ -1126,55 +1125,49 @@ UBool RegexCompile::doParseActions(int32_t action) // scanned a ".", match any single character. { fixLiterals(FALSE); - int32_t op; if (fModeFlags & UREGEX_DOTALL) { - op = URX_BUILD(URX_DOTANY_ALL, 0); + appendOp(URX_DOTANY_ALL, 0); } else if (fModeFlags & UREGEX_UNIX_LINES) { - op = URX_BUILD(URX_DOTANY_UNIX, 0); + appendOp(URX_DOTANY_UNIX, 0); } else { - op = URX_BUILD(URX_DOTANY, 0); + appendOp(URX_DOTANY, 0); } - fRXPat->fCompiledPat->addElement(op, *fStatus); } break; case doCaret: { fixLiterals(FALSE); - int32_t op = 0; if ( (fModeFlags & UREGEX_MULTILINE) == 0 && (fModeFlags & UREGEX_UNIX_LINES) == 0) { - op = URX_CARET; + appendOp(URX_CARET, 0); } else if ((fModeFlags & UREGEX_MULTILINE) != 0 && (fModeFlags & UREGEX_UNIX_LINES) == 0) { - op = URX_CARET_M; + appendOp(URX_CARET_M, 0); } else if ((fModeFlags & UREGEX_MULTILINE) == 0 && (fModeFlags & UREGEX_UNIX_LINES) != 0) { - op = URX_CARET; // Only testing true start of input. + appendOp(URX_CARET, 0); // Only testing true start of input. } else if ((fModeFlags & UREGEX_MULTILINE) != 0 && (fModeFlags & UREGEX_UNIX_LINES) != 0) { - op = URX_CARET_M_UNIX; + appendOp(URX_CARET_M_UNIX, 0); } - fRXPat->fCompiledPat->addElement(URX_BUILD(op, 0), *fStatus); } break; case doDollar: { fixLiterals(FALSE); - int32_t op = 0; if ( (fModeFlags & UREGEX_MULTILINE) == 0 && (fModeFlags & UREGEX_UNIX_LINES) == 0) { - op = URX_DOLLAR; + appendOp(URX_DOLLAR, 0); } else if ((fModeFlags & UREGEX_MULTILINE) != 0 && (fModeFlags & UREGEX_UNIX_LINES) == 0) { - op = URX_DOLLAR_M; + appendOp(URX_DOLLAR_M, 0); } else if ((fModeFlags & UREGEX_MULTILINE) == 0 && (fModeFlags & UREGEX_UNIX_LINES) != 0) { - op = URX_DOLLAR_D; + appendOp(URX_DOLLAR_D, 0); } else if ((fModeFlags & UREGEX_MULTILINE) != 0 && (fModeFlags & UREGEX_UNIX_LINES) != 0) { - op = URX_DOLLAR_MD; + appendOp(URX_DOLLAR_MD, 0); } - fRXPat->fCompiledPat->addElement(URX_BUILD(op, 0), *fStatus); } break; case doBackslashA: fixLiterals(FALSE); - fRXPat->fCompiledPat->addElement(URX_BUILD(URX_CARET, 0), *fStatus); + appendOp(URX_CARET, 0); break; case doBackslashB: @@ -1186,7 +1179,7 @@ UBool RegexCompile::doParseActions(int32_t action) #endif fixLiterals(FALSE); int32_t op = (fModeFlags & UREGEX_UWORD)? URX_BACKSLASH_BU : URX_BACKSLASH_B; - fRXPat->fCompiledPat->addElement(URX_BUILD(op, 1), *fStatus); + appendOp(op, 1); } break; @@ -1199,63 +1192,84 @@ UBool RegexCompile::doParseActions(int32_t action) #endif fixLiterals(FALSE); int32_t op = (fModeFlags & UREGEX_UWORD)? URX_BACKSLASH_BU : URX_BACKSLASH_B; - fRXPat->fCompiledPat->addElement(URX_BUILD(op, 0), *fStatus); + appendOp(op, 0); } break; case doBackslashD: fixLiterals(FALSE); - fRXPat->fCompiledPat->addElement(URX_BUILD(URX_BACKSLASH_D, 1), *fStatus); + appendOp(URX_BACKSLASH_D, 1); break; case doBackslashd: fixLiterals(FALSE); - fRXPat->fCompiledPat->addElement(URX_BUILD(URX_BACKSLASH_D, 0), *fStatus); + appendOp(URX_BACKSLASH_D, 0); break; case doBackslashG: fixLiterals(FALSE); - fRXPat->fCompiledPat->addElement(URX_BUILD(URX_BACKSLASH_G, 0), *fStatus); + appendOp(URX_BACKSLASH_G, 0); + break; + + case doBackslashH: + fixLiterals(FALSE); + appendOp(URX_BACKSLASH_H, 1); + break; + + case doBackslashh: + fixLiterals(FALSE); + appendOp(URX_BACKSLASH_H, 0); + break; + + case doBackslashR: + fixLiterals(FALSE); + appendOp(URX_BACKSLASH_R, 0); break; case doBackslashS: fixLiterals(FALSE); - fRXPat->fCompiledPat->addElement( - URX_BUILD(URX_STAT_SETREF_N, URX_ISSPACE_SET), *fStatus); + appendOp(URX_STAT_SETREF_N, URX_ISSPACE_SET); break; case doBackslashs: fixLiterals(FALSE); - fRXPat->fCompiledPat->addElement( - URX_BUILD(URX_STATIC_SETREF, URX_ISSPACE_SET), *fStatus); + appendOp(URX_STATIC_SETREF, URX_ISSPACE_SET); + break; + + case doBackslashV: + fixLiterals(FALSE); + appendOp(URX_BACKSLASH_V, 1); + break; + + case doBackslashv: + fixLiterals(FALSE); + appendOp(URX_BACKSLASH_V, 0); break; case doBackslashW: fixLiterals(FALSE); - fRXPat->fCompiledPat->addElement( - URX_BUILD(URX_STAT_SETREF_N, URX_ISWORD_SET), *fStatus); + appendOp(URX_STAT_SETREF_N, URX_ISWORD_SET); break; case doBackslashw: fixLiterals(FALSE); - fRXPat->fCompiledPat->addElement( - URX_BUILD(URX_STATIC_SETREF, URX_ISWORD_SET), *fStatus); + appendOp(URX_STATIC_SETREF, URX_ISWORD_SET); break; case doBackslashX: fixLiterals(FALSE); - fRXPat->fCompiledPat->addElement(URX_BUILD(URX_BACKSLASH_X, 0), *fStatus); + appendOp(URX_BACKSLASH_X, 0); break; case doBackslashZ: fixLiterals(FALSE); - fRXPat->fCompiledPat->addElement(URX_BUILD(URX_DOLLAR, 0), *fStatus); + appendOp(URX_DOLLAR, 0); break; case doBackslashz: fixLiterals(FALSE); - fRXPat->fCompiledPat->addElement(URX_BUILD(URX_BACKSLASH_Z, 0), *fStatus); + appendOp(URX_BACKSLASH_Z, 0); break; case doEscapeError: @@ -1315,17 +1329,50 @@ UBool RegexCompile::doParseActions(int32_t action) U_ASSERT(groupNum > 0); // Shouldn't happen. '\0' begins an octal escape sequence, // and shouldn't enter this code path at all. fixLiterals(FALSE); - int32_t op; if (fModeFlags & UREGEX_CASE_INSENSITIVE) { - op = URX_BUILD(URX_BACKREF_I, groupNum); + appendOp(URX_BACKREF_I, groupNum); } else { - op = URX_BUILD(URX_BACKREF, groupNum); + appendOp(URX_BACKREF, groupNum); } - fRXPat->fCompiledPat->addElement(op, *fStatus); } break; + case doBeginNamedBackRef: + U_ASSERT(fCaptureName == NULL); + fCaptureName = new UnicodeString; + if (fCaptureName == NULL) { + error(U_MEMORY_ALLOCATION_ERROR); + } + break; + + case doContinueNamedBackRef: + fCaptureName->append(fC.fChar); + break; + case doCompleteNamedBackRef: + { + int32_t groupNumber = + fRXPat->fNamedCaptureMap ? uhash_geti(fRXPat->fNamedCaptureMap, fCaptureName) : 0; + if (groupNumber == 0) { + // Group name has not been defined. + // Could be a forward reference. If we choose to support them at some + // future time, extra mechanism will be required at this point. + error(U_REGEX_INVALID_CAPTURE_GROUP_NAME); + } else { + // Given the number, handle identically to a \n numbered back reference. + // See comments above, under doBackRef + fixLiterals(FALSE); + if (fModeFlags & UREGEX_CASE_INSENSITIVE) { + appendOp(URX_BACKREF_I, groupNumber); + } else { + appendOp(URX_BACKREF, groupNumber); + } + } + delete fCaptureName; + fCaptureName = NULL; + break; + } + case doPossessivePlus: // Possessive ++ quantifier. // Compiles to @@ -1342,22 +1389,18 @@ UBool RegexCompile::doParseActions(int32_t action) { // Emit the STO_SP int32_t topLoc = blockTopLoc(TRUE); - int32_t stoLoc = fRXPat->fDataSize; - fRXPat->fDataSize++; // Reserve the data location for storing save stack ptr. - int32_t op = URX_BUILD(URX_STO_SP, stoLoc); + int32_t stoLoc = allocateData(1); // Reserve the data location for storing save stack ptr. + int32_t op = buildOp(URX_STO_SP, stoLoc); fRXPat->fCompiledPat->setElementAt(op, topLoc); // Emit the STATE_SAVE - op = URX_BUILD(URX_STATE_SAVE, fRXPat->fCompiledPat->size()+2); - fRXPat->fCompiledPat->addElement(op, *fStatus); + appendOp(URX_STATE_SAVE, fRXPat->fCompiledPat->size()+2); // Emit the JMP - op = URX_BUILD(URX_JMP, topLoc+1); - fRXPat->fCompiledPat->addElement(op, *fStatus); + appendOp(URX_JMP, topLoc+1); // Emit the LD_SP - op = URX_BUILD(URX_LD_SP, stoLoc); - fRXPat->fCompiledPat->addElement(op, *fStatus); + appendOp(URX_LD_SP, stoLoc); } break; @@ -1377,23 +1420,20 @@ UBool RegexCompile::doParseActions(int32_t action) insertOp(topLoc); // emit STO_SP loc - int32_t stoLoc = fRXPat->fDataSize; - fRXPat->fDataSize++; // Reserve the data location for storing save stack ptr. - int32_t op = URX_BUILD(URX_STO_SP, stoLoc); + int32_t stoLoc = allocateData(1); // Reserve the data location for storing save stack ptr. + int32_t op = buildOp(URX_STO_SP, stoLoc); fRXPat->fCompiledPat->setElementAt(op, topLoc); // Emit the SAVE_STATE 5 int32_t L7 = fRXPat->fCompiledPat->size()+1; - op = URX_BUILD(URX_STATE_SAVE, L7); + op = buildOp(URX_STATE_SAVE, L7); fRXPat->fCompiledPat->setElementAt(op, topLoc+1); // Append the JMP operation. - op = URX_BUILD(URX_JMP, topLoc+1); - fRXPat->fCompiledPat->addElement(op, *fStatus); + appendOp(URX_JMP, topLoc+1); // Emit the LD_SP loc - op = URX_BUILD(URX_LD_SP, stoLoc); - fRXPat->fCompiledPat->addElement(op, *fStatus); + appendOp(URX_LD_SP, stoLoc); } break; @@ -1412,19 +1452,17 @@ UBool RegexCompile::doParseActions(int32_t action) insertOp(topLoc); // Emit the STO_SP - int32_t stoLoc = fRXPat->fDataSize; - fRXPat->fDataSize++; // Reserve the data location for storing save stack ptr. - int32_t op = URX_BUILD(URX_STO_SP, stoLoc); + int32_t stoLoc = allocateData(1); // Reserve the data location for storing save stack ptr. + int32_t op = buildOp(URX_STO_SP, stoLoc); fRXPat->fCompiledPat->setElementAt(op, topLoc); // Emit the SAVE_STATE int32_t continueLoc = fRXPat->fCompiledPat->size()+1; - op = URX_BUILD(URX_STATE_SAVE, continueLoc); + op = buildOp(URX_STATE_SAVE, continueLoc); fRXPat->fCompiledPat->setElementAt(op, topLoc+1); // Emit the LD_SP - op = URX_BUILD(URX_LD_SP, stoLoc); - fRXPat->fCompiledPat->addElement(op, *fStatus); + appendOp(URX_LD_SP, stoLoc); } break; @@ -1447,7 +1485,7 @@ UBool RegexCompile::doParseActions(int32_t action) case 0x78: /* 'x' */ bit = UREGEX_COMMENTS; break; case 0x2d: /* '-' */ fSetModeFlag = FALSE; break; default: - U_ASSERT(FALSE); // Should never happen. Other chars are filtered out + UPRV_UNREACHABLE; // Should never happen. Other chars are filtered out // by the scanner. } if (fSetModeFlag) { @@ -1481,8 +1519,8 @@ UBool RegexCompile::doParseActions(int32_t action) // is an '|' alternation within the parens. { fixLiterals(FALSE); - fRXPat->fCompiledPat->addElement(URX_BUILD(URX_NOP, 0), *fStatus); - fRXPat->fCompiledPat->addElement(URX_BUILD(URX_NOP, 0), *fStatus); + appendOp(URX_NOP, 0); + appendOp(URX_NOP, 0); // On the Parentheses stack, start a new frame and add the postions // of the two NOPs (a normal non-capturing () frame, except for the @@ -1559,6 +1597,48 @@ UBool RegexCompile::doParseActions(int32_t action) break; } + case doSetBackslash_h: + { + UnicodeSet *set = (UnicodeSet *)fSetStack.peek(); + UnicodeSet h; + h.applyIntPropertyValue(UCHAR_GENERAL_CATEGORY_MASK, U_GC_ZS_MASK, *fStatus); + h.add((UChar32)9); // Tab + set->addAll(h); + break; + } + + case doSetBackslash_H: + { + UnicodeSet *set = (UnicodeSet *)fSetStack.peek(); + UnicodeSet h; + h.applyIntPropertyValue(UCHAR_GENERAL_CATEGORY_MASK, U_GC_ZS_MASK, *fStatus); + h.add((UChar32)9); // Tab + h.complement(); + set->addAll(h); + break; + } + + case doSetBackslash_v: + { + UnicodeSet *set = (UnicodeSet *)fSetStack.peek(); + set->add((UChar32)0x0a, (UChar32)0x0d); // add range + set->add((UChar32)0x85); + set->add((UChar32)0x2028, (UChar32)0x2029); + break; + } + + case doSetBackslash_V: + { + UnicodeSet *set = (UnicodeSet *)fSetStack.peek(); + UnicodeSet v; + v.add((UChar32)0x0a, (UChar32)0x0d); // add range + v.add((UChar32)0x85); + v.add((UChar32)0x2028, (UChar32)0x2029); + v.complement(); + set->addAll(v); + break; + } + case doSetBackslash_w: { UnicodeSet *set = (UnicodeSet *)fSetStack.peek(); @@ -1701,7 +1781,7 @@ UBool RegexCompile::doParseActions(int32_t action) // and ICU UnicodeSet behavior. { UChar32 c = scanNamedChar(); - if (U_SUCCESS(*fStatus) && fLastSetLiteral > c) { + if (U_SUCCESS(*fStatus) && (fLastSetLiteral == U_SENTINEL || fLastSetLiteral > c)) { error(U_REGEX_INVALID_RANGE); } UnicodeSet *s = (UnicodeSet *)fSetStack.peek(); @@ -1770,7 +1850,8 @@ UBool RegexCompile::doParseActions(int32_t action) // Lower Limit > Upper limit being an error matches both Java // and ICU UnicodeSet behavior. { - if (fLastSetLiteral > fC.fChar) { + + if (fLastSetLiteral == U_SENTINEL || fLastSetLiteral > fC.fChar) { error(U_REGEX_INVALID_RANGE); } UnicodeSet *s = (UnicodeSet *)fSetStack.peek(); @@ -1779,9 +1860,7 @@ UBool RegexCompile::doParseActions(int32_t action) } default: - U_ASSERT(FALSE); - error(U_REGEX_INTERNAL_ERROR); - break; + UPRV_UNREACHABLE; } if (U_FAILURE(*fStatus)) { @@ -1819,7 +1898,6 @@ void RegexCompile::literalChar(UChar32 c) { // //------------------------------------------------------------------------------ void RegexCompile::fixLiterals(UBool split) { - int32_t op = 0; // An op from/for the compiled pattern. // If no literal characters have been scanned but not yet had code generated // for them, nothing needs to be done. @@ -1858,23 +1936,23 @@ void RegexCompile::fixLiterals(UBool split) { // Single character, emit a URX_ONECHAR op to match it. if ((fModeFlags & UREGEX_CASE_INSENSITIVE) && u_hasBinaryProperty(lastCodePoint, UCHAR_CASE_SENSITIVE)) { - op = URX_BUILD(URX_ONECHAR_I, lastCodePoint); + appendOp(URX_ONECHAR_I, lastCodePoint); } else { - op = URX_BUILD(URX_ONECHAR, lastCodePoint); + appendOp(URX_ONECHAR, lastCodePoint); } - fRXPat->fCompiledPat->addElement(op, *fStatus); } else { // Two or more chars, emit a URX_STRING to match them. + if (fLiteralChars.length() > 0x00ffffff || fRXPat->fLiteralText.length() > 0x00ffffff) { + error(U_REGEX_PATTERN_TOO_BIG); + } if (fModeFlags & UREGEX_CASE_INSENSITIVE) { - op = URX_BUILD(URX_STRING_I, fRXPat->fLiteralText.length()); + appendOp(URX_STRING_I, fRXPat->fLiteralText.length()); } else { // TODO here: add optimization to split case sensitive strings of length two // into two single char ops, for efficiency. - op = URX_BUILD(URX_STRING, fRXPat->fLiteralText.length()); + appendOp(URX_STRING, fRXPat->fLiteralText.length()); } - fRXPat->fCompiledPat->addElement(op, *fStatus); - op = URX_BUILD(URX_STRING_LEN, fLiteralChars.length()); - fRXPat->fCompiledPat->addElement(op, *fStatus); + appendOp(URX_STRING_LEN, fLiteralChars.length()); // Add this string into the accumulated strings of the compiled pattern. fRXPat->fLiteralText.append(fLiteralChars); @@ -1884,8 +1962,50 @@ void RegexCompile::fixLiterals(UBool split) { } +int32_t RegexCompile::buildOp(int32_t type, int32_t val) { + if (U_FAILURE(*fStatus)) { + return 0; + } + if (type < 0 || type > 255) { + UPRV_UNREACHABLE; + } + if (val > 0x00ffffff) { + UPRV_UNREACHABLE; + } + if (val < 0) { + if (!(type == URX_RESERVED_OP_N || type == URX_RESERVED_OP)) { + UPRV_UNREACHABLE; + } + if (URX_TYPE(val) != 0xff) { + UPRV_UNREACHABLE; + } + type = URX_RESERVED_OP_N; + } + return (type << 24) | val; +} + +//------------------------------------------------------------------------------ +// +// appendOp() Append a new instruction onto the compiled pattern +// Includes error checking, limiting the size of the +// pattern to lengths that can be represented in the +// 24 bit operand field of an instruction. +// +//------------------------------------------------------------------------------ +void RegexCompile::appendOp(int32_t op) { + if (U_FAILURE(*fStatus)) { + return; + } + fRXPat->fCompiledPat->addElement(op, *fStatus); + if ((fRXPat->fCompiledPat->size() > 0x00fffff0) && U_SUCCESS(*fStatus)) { + error(U_REGEX_PATTERN_TOO_BIG); + } +} +void RegexCompile::appendOp(int32_t type, int32_t val) { + appendOp(buildOp(type, val)); +} //------------------------------------------------------------------------------ @@ -1901,7 +2021,7 @@ void RegexCompile::insertOp(int32_t where) { UVector64 *code = fRXPat->fCompiledPat; U_ASSERT(where>0 && where < code->size()); - int32_t nop = URX_BUILD(URX_NOP, 0); + int32_t nop = buildOp(URX_NOP, 0); code->insertElementAt(nop, where, *fStatus); // Walk through the pattern, looking for any ops with targets that @@ -1922,7 +2042,7 @@ void RegexCompile::insertOp(int32_t where) { // Target location for this opcode is after the insertion point and // needs to be incremented to adjust for the insertion. opValue++; - op = URX_BUILD(opType, opValue); + op = buildOp(opType, opValue); code->setElementAt(op, loc); } } @@ -1947,6 +2067,58 @@ void RegexCompile::insertOp(int32_t where) { } +//------------------------------------------------------------------------------ +// +// allocateData() Allocate storage in the matcher's static data area. +// Return the index for the newly allocated data. +// The storage won't actually exist until we are running a match +// operation, but the storage indexes are inserted into various +// opcodes while compiling the pattern. +// +//------------------------------------------------------------------------------ +int32_t RegexCompile::allocateData(int32_t size) { + if (U_FAILURE(*fStatus)) { + return 0; + } + if (size <= 0 || size > 0x100 || fRXPat->fDataSize < 0) { + error(U_REGEX_INTERNAL_ERROR); + return 0; + } + int32_t dataIndex = fRXPat->fDataSize; + fRXPat->fDataSize += size; + if (fRXPat->fDataSize >= 0x00fffff0) { + error(U_REGEX_INTERNAL_ERROR); + } + return dataIndex; +} + + +//------------------------------------------------------------------------------ +// +// allocateStackData() Allocate space in the back-tracking stack frame. +// Return the index for the newly allocated data. +// The frame indexes are inserted into various +// opcodes while compiling the pattern, meaning that frame +// size must be restricted to the size that will fit +// as an operand (24 bits). +// +//------------------------------------------------------------------------------ +int32_t RegexCompile::allocateStackData(int32_t size) { + if (U_FAILURE(*fStatus)) { + return 0; + } + if (size <= 0 || size > 0x100 || fRXPat->fFrameSize < 0) { + error(U_REGEX_INTERNAL_ERROR); + return 0; + } + int32_t dataIndex = fRXPat->fFrameSize; + fRXPat->fFrameSize += size; + if (fRXPat->fFrameSize >= 0x00fffff0) { + error(U_REGEX_PATTERN_TOO_BIG); + } + return dataIndex; +} + //------------------------------------------------------------------------------ // @@ -1989,7 +2161,7 @@ int32_t RegexCompile::blockTopLoc(UBool reserveLoc) { theLoc--; } if (reserveLoc) { - int32_t nop = URX_BUILD(URX_NOP, 0); + int32_t nop = buildOp(URX_NOP, 0); fRXPat->fCompiledPat->insertElementAt(nop, theLoc, *fStatus); } } @@ -2064,8 +2236,7 @@ void RegexCompile::handleCloseParen() { 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); + appendOp(URX_END_CAPTURE, frameVarLocation); } break; case atomic: @@ -2076,8 +2247,7 @@ void RegexCompile::handleCloseParen() { int32_t stoOp = (int32_t)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); + appendOp(URX_LD_SP, stoLoc); } break; @@ -2086,8 +2256,7 @@ void RegexCompile::handleCloseParen() { int32_t startOp = (int32_t)fRXPat->fCompiledPat->elementAti(fMatchOpenParen-5); 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); + appendOp(URX_LA_END, dataLoc); } break; @@ -2097,19 +2266,16 @@ void RegexCompile::handleCloseParen() { int32_t startOp = (int32_t)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_BACKTRACK, 0); - fRXPat->fCompiledPat->addElement(op, *fStatus); - op = URX_BUILD(URX_LA_END, dataLoc); - fRXPat->fCompiledPat->addElement(op, *fStatus); + appendOp(URX_LA_END, dataLoc); + appendOp(URX_BACKTRACK, 0); + appendOp(URX_LA_END, dataLoc); // Patch the URX_SAVE near the top of the block. // The destination of the SAVE is the final LA_END that was just added. int32_t saveOp = (int32_t)fRXPat->fCompiledPat->elementAti(fMatchOpenParen); U_ASSERT(URX_TYPE(saveOp) == URX_STATE_SAVE); int32_t dest = fRXPat->fCompiledPat->size()-1; - saveOp = URX_BUILD(URX_STATE_SAVE, dest); + saveOp = buildOp(URX_STATE_SAVE, dest); fRXPat->fCompiledPat->setElementAt(saveOp, fMatchOpenParen); } break; @@ -2122,10 +2288,8 @@ void RegexCompile::handleCloseParen() { int32_t startOp = (int32_t)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); + appendOp(URX_LB_END, dataLoc); + appendOp(URX_LA_END, dataLoc); // Determine the min and max bounds for the length of the // string that the pattern can match. @@ -2133,10 +2297,21 @@ void RegexCompile::handleCloseParen() { int32_t patEnd = fRXPat->fCompiledPat->size() - 1; int32_t minML = minMatchLength(fMatchOpenParen, patEnd); int32_t maxML = maxMatchLength(fMatchOpenParen, patEnd); + if (URX_TYPE(maxML) != 0) { + error(U_REGEX_LOOK_BEHIND_LIMIT); + break; + } if (maxML == INT32_MAX) { error(U_REGEX_LOOK_BEHIND_LIMIT); break; } + if (minML == INT32_MAX) { + // This condition happens when no match is possible, such as with a + // [set] expression containing no elements. + // In principle, the generated code to evaluate the expression could be deleted, + // but it's probably not worth the complication. + minML = 0; + } U_ASSERT(minML <= maxML); // Insert the min and max match len bounds into the URX_LB_CONT op that @@ -2157,8 +2332,7 @@ void RegexCompile::handleCloseParen() { int32_t startOp = (int32_t)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); + appendOp(URX_LBN_END, dataLoc); // Determine the min and max bounds for the length of the // string that the pattern can match. @@ -2166,10 +2340,22 @@ void RegexCompile::handleCloseParen() { int32_t patEnd = fRXPat->fCompiledPat->size() - 1; int32_t minML = minMatchLength(fMatchOpenParen, patEnd); int32_t maxML = maxMatchLength(fMatchOpenParen, patEnd); + if (URX_TYPE(maxML) != 0) { + error(U_REGEX_LOOK_BEHIND_LIMIT); + break; + } if (maxML == INT32_MAX) { error(U_REGEX_LOOK_BEHIND_LIMIT); break; } + if (minML == INT32_MAX) { + // This condition happens when no match is possible, such as with a + // [set] expression containing no elements. + // In principle, the generated code to evaluate the expression could be deleted, + // but it's probably not worth the complication. + minML = 0; + } + U_ASSERT(minML <= maxML); // Insert the min and max match len bounds into the URX_LB_CONT op that @@ -2179,7 +2365,7 @@ void RegexCompile::handleCloseParen() { // Insert the pattern location to continue at after a successful match // as the last operand of the URX_LBN_CONT - op = URX_BUILD(URX_RELOC_OPRND, fRXPat->fCompiledPat->size()); + int32_t op = buildOp(URX_RELOC_OPRND, fRXPat->fCompiledPat->size()); fRXPat->fCompiledPat->setElementAt(op, fMatchOpenParen-1); } break; @@ -2187,7 +2373,7 @@ void RegexCompile::handleCloseParen() { default: - U_ASSERT(FALSE); + UPRV_UNREACHABLE; } // remember the next location in the compiled pattern. @@ -2220,7 +2406,7 @@ void RegexCompile::compileSet(UnicodeSet *theSet) case 0: { // Set of no elements. Always fails to match. - fRXPat->fCompiledPat->addElement(URX_BUILD(URX_BACKTRACK, 0), *fStatus); + appendOp(URX_BACKTRACK, 0); delete theSet; } break; @@ -2241,8 +2427,7 @@ void RegexCompile::compileSet(UnicodeSet *theSet) // Put it into the compiled pattern as a set. int32_t setNumber = fRXPat->fSets->size(); fRXPat->fSets->addElement(theSet, *fStatus); - int32_t setOp = URX_BUILD(URX_SETREF, setNumber); - fRXPat->fCompiledPat->addElement(setOp, *fStatus); + appendOp(URX_SETREF, setNumber); } } } @@ -2281,13 +2466,10 @@ void RegexCompile::compileInterval(int32_t InitOp, int32_t LoopOp) // counterLoc --> Loop counter // +1 --> Input index (for breaking non-progressing loops) // (Only present if unbounded upper limit on loop) - int32_t counterLoc = fRXPat->fFrameSize; - fRXPat->fFrameSize++; - if (fIntervalUpper < 0) { - fRXPat->fFrameSize++; - } + int32_t dataSize = fIntervalUpper < 0 ? 2 : 1; + int32_t counterLoc = allocateStackData(dataSize); - int32_t op = URX_BUILD(InitOp, counterLoc); + int32_t op = buildOp(InitOp, counterLoc); fRXPat->fCompiledPat->setElementAt(op, topOfBlock); // The second operand of CTR_INIT is the location following the end of the loop. @@ -2295,7 +2477,7 @@ void RegexCompile::compileInterval(int32_t InitOp, int32_t LoopOp) // compilation of something later on causes the code to grow and the target // position to move. int32_t loopEnd = fRXPat->fCompiledPat->size(); - op = URX_BUILD(URX_RELOC_OPRND, loopEnd); + op = buildOp(URX_RELOC_OPRND, loopEnd); fRXPat->fCompiledPat->setElementAt(op, topOfBlock+1); // Followed by the min and max counts. @@ -2304,8 +2486,7 @@ void RegexCompile::compileInterval(int32_t InitOp, int32_t LoopOp) // Apend the CTR_LOOP op. The operand is the location of the CTR_INIT op. // Goes at end of the block being looped over, so just append to the code so far. - op = URX_BUILD(LoopOp, topOfBlock); - fRXPat->fCompiledPat->addElement(op, *fStatus); + appendOp(LoopOp, topOfBlock); if ((fIntervalLow & 0xff000000) != 0 || (fIntervalUpper > 0 && (fIntervalUpper & 0xff000000) != 0)) { @@ -2329,7 +2510,15 @@ UBool RegexCompile::compileInlineInterval() { int32_t topOfBlock = blockTopLoc(FALSE); if (fIntervalUpper == 0) { // Pathological case. Attempt no matches, as if the block doesn't exist. + // Discard the generated code for the block. + // If the block included parens, discard the info pertaining to them as well. fRXPat->fCompiledPat->setSize(topOfBlock); + if (fMatchOpenParen >= topOfBlock) { + fMatchOpenParen = -1; + } + if (fMatchCloseParen >= topOfBlock) { + fMatchCloseParen = -1; + } return TRUE; } @@ -2350,7 +2539,7 @@ UBool RegexCompile::compileInlineInterval() { // int32_t endOfSequenceLoc = fRXPat->fCompiledPat->size()-1 + fIntervalUpper + (fIntervalUpper-fIntervalLow); - int32_t saveOp = URX_BUILD(URX_STATE_SAVE, endOfSequenceLoc); + int32_t saveOp = buildOp(URX_STATE_SAVE, endOfSequenceLoc); if (fIntervalLow == 0) { insertOp(topOfBlock); fRXPat->fCompiledPat->setElementAt(saveOp, topOfBlock); @@ -2363,19 +2552,128 @@ UBool RegexCompile::compileInlineInterval() { // it was put there when it was originally encountered. int32_t i; for (i=1; ifCompiledPat->addElement(saveOp, *fStatus); - } - if (i > fIntervalLow) { - fRXPat->fCompiledPat->addElement(saveOp, *fStatus); + if (i >= fIntervalLow) { + appendOp(saveOp); } - fRXPat->fCompiledPat->addElement(op, *fStatus); + appendOp(op); } return TRUE; } +//------------------------------------------------------------------------------ +// +// caseInsensitiveStart given a single code point from a pattern string, determine the +// set of characters that could potentially begin a case-insensitive +// match of a string beginning with that character, using full Unicode +// case insensitive matching. +// +// This is used in optimizing find(). +// +// closeOver(USET_CASE_INSENSITIVE) does most of what is needed, but +// misses cases like this: +// A string from the pattern begins with 'ss' (although all we know +// in this context is that it begins with 's') +// The pattern could match a string beginning with a German sharp-s +// +// To the ordinary case closure for a character c, we add all other +// characters cx where the case closure of cx incudes a string form that begins +// with the original character c. +// +// This function could be made smarter. The full pattern string is available +// and it would be possible to verify that the extra characters being added +// to the starting set fully match, rather than having just a first-char of the +// folded form match. +// +//------------------------------------------------------------------------------ +void RegexCompile::findCaseInsensitiveStarters(UChar32 c, UnicodeSet *starterChars) { + +// Machine Generated below. +// It may need updating with new versions of Unicode. +// Intltest test RegexTest::TestCaseInsensitiveStarters will fail if an update is needed. +// The update tool is here: svn+ssh://source.icu-project.org/repos/icu/tools/trunk/unicode/c/genregexcasing + +// Machine Generated Data. Do not hand edit. + static const UChar32 RECaseFixCodePoints[] = { + 0x61, 0x66, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x77, 0x79, 0x2bc, + 0x3ac, 0x3ae, 0x3b1, 0x3b7, 0x3b9, 0x3c1, 0x3c5, 0x3c9, 0x3ce, 0x565, + 0x574, 0x57e, 0x1f00, 0x1f01, 0x1f02, 0x1f03, 0x1f04, 0x1f05, 0x1f06, 0x1f07, + 0x1f20, 0x1f21, 0x1f22, 0x1f23, 0x1f24, 0x1f25, 0x1f26, 0x1f27, 0x1f60, 0x1f61, + 0x1f62, 0x1f63, 0x1f64, 0x1f65, 0x1f66, 0x1f67, 0x1f70, 0x1f74, 0x1f7c, 0x110000}; + + static const int16_t RECaseFixStringOffsets[] = { + 0x0, 0x1, 0x6, 0x7, 0x8, 0x9, 0xd, 0xe, 0xf, 0x10, + 0x11, 0x12, 0x13, 0x17, 0x1b, 0x20, 0x21, 0x2a, 0x2e, 0x2f, + 0x30, 0x34, 0x35, 0x37, 0x39, 0x3b, 0x3d, 0x3f, 0x41, 0x43, + 0x45, 0x47, 0x49, 0x4b, 0x4d, 0x4f, 0x51, 0x53, 0x55, 0x57, + 0x59, 0x5b, 0x5d, 0x5f, 0x61, 0x63, 0x65, 0x66, 0x67, 0}; + + static const int16_t RECaseFixCounts[] = { + 0x1, 0x5, 0x1, 0x1, 0x1, 0x4, 0x1, 0x1, 0x1, 0x1, + 0x1, 0x1, 0x4, 0x4, 0x5, 0x1, 0x9, 0x4, 0x1, 0x1, + 0x4, 0x1, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, + 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, + 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x1, 0x1, 0x1, 0}; + + static const UChar RECaseFixData[] = { + 0x1e9a, 0xfb00, 0xfb01, 0xfb02, 0xfb03, 0xfb04, 0x1e96, 0x130, 0x1f0, 0xdf, + 0x1e9e, 0xfb05, 0xfb06, 0x1e97, 0x1e98, 0x1e99, 0x149, 0x1fb4, 0x1fc4, 0x1fb3, + 0x1fb6, 0x1fb7, 0x1fbc, 0x1fc3, 0x1fc6, 0x1fc7, 0x1fcc, 0x390, 0x1fd2, 0x1fd3, + 0x1fd6, 0x1fd7, 0x1fe4, 0x3b0, 0x1f50, 0x1f52, 0x1f54, 0x1f56, 0x1fe2, 0x1fe3, + 0x1fe6, 0x1fe7, 0x1ff3, 0x1ff6, 0x1ff7, 0x1ffc, 0x1ff4, 0x587, 0xfb13, 0xfb14, + 0xfb15, 0xfb17, 0xfb16, 0x1f80, 0x1f88, 0x1f81, 0x1f89, 0x1f82, 0x1f8a, 0x1f83, + 0x1f8b, 0x1f84, 0x1f8c, 0x1f85, 0x1f8d, 0x1f86, 0x1f8e, 0x1f87, 0x1f8f, 0x1f90, + 0x1f98, 0x1f91, 0x1f99, 0x1f92, 0x1f9a, 0x1f93, 0x1f9b, 0x1f94, 0x1f9c, 0x1f95, + 0x1f9d, 0x1f96, 0x1f9e, 0x1f97, 0x1f9f, 0x1fa0, 0x1fa8, 0x1fa1, 0x1fa9, 0x1fa2, + 0x1faa, 0x1fa3, 0x1fab, 0x1fa4, 0x1fac, 0x1fa5, 0x1fad, 0x1fa6, 0x1fae, 0x1fa7, + 0x1faf, 0x1fb2, 0x1fc2, 0x1ff2, 0}; + +// End of machine generated data. + + if (c < UCHAR_MIN_VALUE || c > UCHAR_MAX_VALUE) { + // This function should never be called with an invalid input character. + UPRV_UNREACHABLE; + } else if (u_hasBinaryProperty(c, UCHAR_CASE_SENSITIVE)) { + UChar32 caseFoldedC = u_foldCase(c, U_FOLD_CASE_DEFAULT); + starterChars->set(caseFoldedC, caseFoldedC); + + int32_t i; + for (i=0; RECaseFixCodePoints[i]add(cpToAdd); + } + } + + starterChars->closeOver(USET_CASE_INSENSITIVE); + starterChars->removeAllStrings(); + } else { + // Not a cased character. Just return it alone. + starterChars->set(c, c); + } +} + + +// Increment with overflow check. +// val and delta will both be positive. + +static int32_t safeIncrement(int32_t val, int32_t delta) { + if (INT32_MAX - val > delta) { + return val + delta; + } else { + return INT32_MAX; + } +} + + //------------------------------------------------------------------------------ // // matchStartType Determine how a match can start. @@ -2474,7 +2772,7 @@ void RegexCompile::matchStartType() { fRXPat->fInitialChars->add(URX_VAL(op)); numInitialStrings += 2; } - currentLen++; + currentLen = safeIncrement(currentLen, 1); atStart = FALSE; break; @@ -2487,7 +2785,7 @@ void RegexCompile::matchStartType() { fRXPat->fInitialChars->addAll(*s); numInitialStrings += 2; } - currentLen++; + currentLen = safeIncrement(currentLen, 1); atStart = FALSE; break; @@ -2524,7 +2822,7 @@ void RegexCompile::matchStartType() { fRXPat->fInitialChars->addAll(*s); numInitialStrings += 2; } - currentLen++; + currentLen = safeIncrement(currentLen, 1); atStart = FALSE; break; @@ -2539,7 +2837,7 @@ void RegexCompile::matchStartType() { fRXPat->fInitialChars->addAll(sc); numInitialStrings += 2; } - currentLen++; + currentLen = safeIncrement(currentLen, 1); atStart = FALSE; break; @@ -2556,27 +2854,59 @@ void RegexCompile::matchStartType() { fRXPat->fInitialChars->addAll(s); numInitialStrings += 2; } - currentLen++; + currentLen = safeIncrement(currentLen, 1); + atStart = FALSE; + break; + + + case URX_BACKSLASH_H: + // Horiz white space + if (currentLen == 0) { + UnicodeSet s; + s.applyIntPropertyValue(UCHAR_GENERAL_CATEGORY_MASK, U_GC_ZS_MASK, *fStatus); + s.add((UChar32)9); // Tab + if (URX_VAL(op) != 0) { + s.complement(); + } + fRXPat->fInitialChars->addAll(s); + numInitialStrings += 2; + } + currentLen = safeIncrement(currentLen, 1); atStart = FALSE; break; + case URX_BACKSLASH_R: // Any line ending sequence + case URX_BACKSLASH_V: // Any line ending code point, with optional negation + if (currentLen == 0) { + UnicodeSet s; + s.add((UChar32)0x0a, (UChar32)0x0d); // add range + s.add((UChar32)0x85); + s.add((UChar32)0x2028, (UChar32)0x2029); + if (URX_VAL(op) != 0) { + // Complement option applies to URX_BACKSLASH_V only. + s.complement(); + } + fRXPat->fInitialChars->addAll(s); + numInitialStrings += 2; + } + currentLen = safeIncrement(currentLen, 1); + atStart = FALSE; + break; + + + case URX_ONECHAR_I: // Case Insensitive Single Character. if (currentLen == 0) { UChar32 c = URX_VAL(op); if (u_hasBinaryProperty(c, UCHAR_CASE_SENSITIVE)) { - - // Disable optimizations on first char of match. - // TODO: Compute the set of chars that case fold to this char, or to - // a string that begins with this char. - // For simple case folding, this code worked: - // UnicodeSet s(c, c); - // s.closeOver(USET_CASE_INSENSITIVE); - // fRXPat->fInitialChars->addAll(s); - - fRXPat->fInitialChars->clear(); - fRXPat->fInitialChars->complement(); + UnicodeSet starters(c, c); + starters.closeOver(USET_CASE_INSENSITIVE); + // findCaseInsensitiveStarters(c, &starters); + // For ONECHAR_I, no need to worry about text chars that expand on folding into strings. + // The expanded folding can't match the pattern. + fRXPat->fInitialChars->addAll(starters); } else { // Char has no case variants. Just add it as-is to the // set of possible starting chars. @@ -2584,7 +2914,7 @@ void RegexCompile::matchStartType() { } numInitialStrings += 2; } - currentLen++; + currentLen = safeIncrement(currentLen, 1); atStart = FALSE; break; @@ -2600,13 +2930,14 @@ void RegexCompile::matchStartType() { fRXPat->fInitialChars->complement(); numInitialStrings += 2; } - currentLen++; + currentLen = safeIncrement(currentLen, 1); atStart = FALSE; break; case URX_JMPX: loc++; // Except for extra operand on URX_JMPX, same as URX_JMP. + U_FALLTHROUGH; case URX_JMP: { int32_t jmpDest = URX_VAL(op); @@ -2679,7 +3010,7 @@ void RegexCompile::matchStartType() { fRXPat->fInitialStringLen = stringLen; } - currentLen += stringLen; + currentLen = safeIncrement(currentLen, stringLen); atStart = FALSE; } break; @@ -2699,18 +3030,12 @@ void RegexCompile::matchStartType() { // characters for this pattern. int32_t stringStartIdx = URX_VAL(op); UChar32 c = fRXPat->fLiteralText.char32At(stringStartIdx); - UnicodeSet s(c, c); - - // TODO: compute correct set of starting chars for full case folding. - // For the moment, say any char can start. - // s.closeOver(USET_CASE_INSENSITIVE); - s.clear(); - s.complement(); - + UnicodeSet s; + findCaseInsensitiveStarters(c, &s); fRXPat->fInitialChars->addAll(s); numInitialStrings += 2; // Matching on an initial string not possible. } - currentLen += stringLen; + currentLen = safeIncrement(currentLen, stringLen); atStart = FALSE; } break; @@ -2802,13 +3127,10 @@ void RegexCompile::matchStartType() { case URX_LB_END: case URX_LBN_CONT: case URX_LBN_END: - U_ASSERT(FALSE); // Shouldn't get here. These ops should be + UPRV_UNREACHABLE; // Shouldn't get here. These ops should be // consumed by the scan in URX_LA_START and LB_START - - break; - default: - U_ASSERT(FALSE); + UPRV_UNREACHABLE; } } @@ -2960,18 +3282,22 @@ int32_t RegexCompile::minMatchLength(int32_t start, int32_t end) { case URX_STAT_SETREF_N: case URX_SETREF: case URX_BACKSLASH_D: + case URX_BACKSLASH_H: + case URX_BACKSLASH_R: + case URX_BACKSLASH_V: case URX_ONECHAR_I: case URX_BACKSLASH_X: // Grahpeme Cluster. Minimum is 1, max unbounded. case URX_DOTANY_ALL: // . matches one or two. case URX_DOTANY: case URX_DOTANY_UNIX: - currentLen++; + currentLen = safeIncrement(currentLen, 1); break; case URX_JMPX: loc++; // URX_JMPX has an extra operand, ignored here, // otherwise processed identically to URX_JMP. + U_FALLTHROUGH; case URX_JMP: { int32_t jmpDest = URX_VAL(op); @@ -3016,7 +3342,7 @@ int32_t RegexCompile::minMatchLength(int32_t start, int32_t end) { { loc++; int32_t stringLenOp = (int32_t)fRXPat->fCompiledPat->elementAti(loc); - currentLen += URX_VAL(stringLenOp); + currentLen = safeIncrement(currentLen, URX_VAL(stringLenOp)); } break; @@ -3029,7 +3355,7 @@ int32_t RegexCompile::minMatchLength(int32_t start, int32_t end) { // Assume a min length of one for now. A min length of zero causes // optimization failures for a pattern like "string"+ // currentLen += URX_VAL(stringLenOp); - currentLen += 1; + currentLen = safeIncrement(currentLen, 1); } break; @@ -3075,7 +3401,7 @@ int32_t RegexCompile::minMatchLength(int32_t start, int32_t end) { // it assumes that the look-ahead match might be zero-length. // TODO: Positive lookahead could recursively do the block, then continue // with the longer of the block or the value coming in. Ticket 6060 - int32_t depth = (opType == URX_LA_START? 2: 1);; + int32_t depth = (opType == URX_LA_START? 2: 1); for (;;) { loc++; op = (int32_t)fRXPat->fCompiledPat->elementAti(loc); @@ -3124,7 +3450,7 @@ int32_t RegexCompile::minMatchLength(int32_t start, int32_t end) { break; default: - U_ASSERT(FALSE); + UPRV_UNREACHABLE; } } @@ -3139,18 +3465,6 @@ int32_t RegexCompile::minMatchLength(int32_t start, int32_t end) { return currentLen; } -// Increment with overflow check. -// val and delta will both be positive. - -static int32_t safeIncrement(int32_t val, int32_t delta) { - if (INT32_MAX - val > delta) { - return val + delta; - } else { - return INT32_MAX; - } -} - - //------------------------------------------------------------------------------ // // maxMatchLength Calculate the length of the longest string that could @@ -3169,7 +3483,6 @@ int32_t RegexCompile::maxMatchLength(int32_t start, int32_t end) { U_ASSERT(start <= end); U_ASSERT(end < fRXPat->fCompiledPat->size()); - int32_t loc; int32_t op; int32_t opType; @@ -3241,6 +3554,9 @@ int32_t RegexCompile::maxMatchLength(int32_t start, int32_t end) { case URX_STAT_SETREF_N: case URX_SETREF: case URX_BACKSLASH_D: + case URX_BACKSLASH_H: + case URX_BACKSLASH_R: + case URX_BACKSLASH_V: case URX_ONECHAR_I: case URX_DOTANY_ALL: case URX_DOTANY: @@ -3354,7 +3670,7 @@ int32_t RegexCompile::maxMatchLength(int32_t start, int32_t end) { break; } - int32_t maxLoopCount = fRXPat->fCompiledPat->elementAti(loc+3); + int32_t maxLoopCount = static_cast(fRXPat->fCompiledPat->elementAti(loc+3)); if (maxLoopCount == -1) { // Unbounded Loop. No upper bound on match length. currentLen = INT32_MAX; @@ -3362,22 +3678,22 @@ int32_t RegexCompile::maxMatchLength(int32_t start, int32_t end) { } U_ASSERT(loopEndLoc >= loc+4); - int32_t blockLen = maxMatchLength(loc+4, loopEndLoc-1); // Recursive call. - if (blockLen == INT32_MAX) { - currentLen = blockLen; + int64_t blockLen = maxMatchLength(loc+4, loopEndLoc-1); // Recursive call. + int64_t updatedLen = (int64_t)currentLen + blockLen * maxLoopCount; + if (updatedLen >= INT32_MAX) { + currentLen = INT32_MAX; break; } - currentLen += blockLen * maxLoopCount; + currentLen = (int32_t)updatedLen; loc = loopEndLoc; break; } case URX_CTR_LOOP: case URX_CTR_LOOP_NG: - // These opcodes will be skipped over by code for URX_CRT_INIT. + // These opcodes will be skipped over by code for URX_CTR_INIT. // We shouldn't encounter them here. - U_ASSERT(FALSE); - break; + UPRV_UNREACHABLE; case URX_LOOP_SR_I: case URX_LOOP_DOT_I: @@ -3397,33 +3713,26 @@ int32_t RegexCompile::maxMatchLength(int32_t start, int32_t end) { // End of look-ahead ops should always be consumed by the processing at // the URX_LA_START op. - // U_ASSERT(FALSE); - // break; + // UPRV_UNREACHABLE; case URX_LB_START: { // Look-behind. Scan forward until the matching look-around end, // without processing the look-behind block. - int32_t depth = 0; - for (;;) { - loc++; + int32_t dataLoc = URX_VAL(op); + for (loc = loc + 1; loc < end; ++loc) { op = (int32_t)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--; + int32_t opType = URX_TYPE(op); + if ((opType == URX_LA_END || opType == URX_LBN_END) && (URX_VAL(op) == dataLoc)) { + break; } - U_ASSERT(loc < end); } + U_ASSERT(loc < end); } break; default: - U_ASSERT(FALSE); + UPRV_UNREACHABLE; } @@ -3500,7 +3809,7 @@ void RegexCompile::stripNOPs() { int32_t operandAddress = URX_VAL(op); U_ASSERT(operandAddress>=0 && operandAddressfCompiledPat->setElementAt(op, dst); dst++; break; @@ -3515,7 +3824,7 @@ void RegexCompile::stripNOPs() { break; } where = fRXPat->fGroupMap->elementAti(where-1); - op = URX_BUILD(opType, where); + op = buildOp(opType, where); fRXPat->fCompiledPat->setElementAt(op, dst); dst++; @@ -3568,6 +3877,9 @@ void RegexCompile::stripNOPs() { case URX_LOOP_C: case URX_DOLLAR_D: case URX_DOLLAR_MD: + case URX_BACKSLASH_H: + case URX_BACKSLASH_R: + case URX_BACKSLASH_V: // These instructions are unaltered by the relocation. fRXPat->fCompiledPat->setElementAt(op, dst); dst++; @@ -3575,8 +3887,7 @@ void RegexCompile::stripNOPs() { default: // Some op is unaccounted for. - U_ASSERT(FALSE); - error(U_REGEX_INTERNAL_ERROR); + UPRV_UNREACHABLE; } } @@ -3593,7 +3904,7 @@ void RegexCompile::stripNOPs() { // //------------------------------------------------------------------------------ void RegexCompile::error(UErrorCode e) { - if (U_SUCCESS(*fStatus)) { + if (U_SUCCESS(*fStatus) || e == U_MEMORY_ALLOCATION_ERROR) { *fStatus = e; // Hmm. fParseErr (UParseError) line & offset fields are int32_t in public // API (see common/unicode/parseerr.h), while fLineNum and fCharNum are @@ -3712,7 +4023,7 @@ UChar32 RegexCompile::peekCharLL() { // //------------------------------------------------------------------------------ void RegexCompile::nextChar(RegexPatternChar &c) { - + tailRecursion: fScanIndex = UTEXT_GETNATIVEINDEX(fRXPat->fPattern); c.fChar = nextCharLL(); c.fQuoted = FALSE; @@ -3723,7 +4034,9 @@ void RegexCompile::nextChar(RegexPatternChar &c) { c.fChar == (UChar32)-1) { fQuoteMode = FALSE; // Exit quote mode, nextCharLL(); // discard the E - nextChar(c); // recurse to get the real next char + // nextChar(c); // recurse to get the real next char + goto tailRecursion; // Note: fuzz testing produced testcases that + // resulted in stack overflow here. } } else if (fInBackslashQuote) { @@ -3841,8 +4154,10 @@ void RegexCompile::nextChar(RegexPatternChar &c) { else if (peekCharLL() == chQ) { // "\Q" enter quote mode, which will continue until "\E" fQuoteMode = TRUE; - nextCharLL(); // discard the 'Q'. - nextChar(c); // recurse to get the real next char. + nextCharLL(); // discard the 'Q'. + // nextChar(c); // recurse to get the real next char. + goto tailRecursion; // Note: fuzz testing produced test cases that + // resulted in stack overflow here. } else { @@ -3867,7 +4182,7 @@ void RegexCompile::nextChar(RegexPatternChar &c) { //------------------------------------------------------------------------------ // // scanNamedChar - // Get a UChar32 from a \N{UNICODE CHARACTER NAME} in the pattern. +// Get a UChar32 from a \N{UNICODE CHARACTER NAME} in the pattern. // // The scan position will be at the 'N'. On return // the scan position should be just after the '}' @@ -4071,211 +4386,209 @@ static inline void addIdentifierIgnorable(UnicodeSet *set, UErrorCode& ec) { // Includes trying the Java "properties" that aren't supported as // normal ICU UnicodeSet properties // -static const UChar posSetPrefix[] = {0x5b, 0x5c, 0x70, 0x7b, 0}; // "[\p{" -static const UChar negSetPrefix[] = {0x5b, 0x5c, 0x50, 0x7b, 0}; // "[\P{" UnicodeSet *RegexCompile::createSetForProperty(const UnicodeString &propName, UBool negated) { - UnicodeString setExpr; - UnicodeSet *set; - uint32_t usetFlags = 0; if (U_FAILURE(*fStatus)) { - return NULL; + return nullptr; } + LocalPointer set; + UErrorCode status = U_ZERO_ERROR; - // - // First try the property as we received it - // - if (negated) { - setExpr.append(negSetPrefix, -1); - } else { - setExpr.append(posSetPrefix, -1); - } - setExpr.append(propName); - setExpr.append(chRBrace); - setExpr.append(chRBracket); - if (fModeFlags & UREGEX_CASE_INSENSITIVE) { - usetFlags |= USET_CASE_INSENSITIVE; - } - set = new UnicodeSet(setExpr, usetFlags, NULL, *fStatus); - if (U_SUCCESS(*fStatus)) { - return set; - } - delete set; - set = NULL; - - // - // The property as it was didn't work. - - // Do [:word:]. It is not recognized as a property by UnicodeSet. "word" not standard POSIX - // or standard Java, but many other regular expression packages do recognize it. - - if (propName.caseCompare(UNICODE_STRING_SIMPLE("word"), 0) == 0) { - *fStatus = U_ZERO_ERROR; - set = new UnicodeSet(*(fRXPat->fStaticSets[URX_ISWORD_SET])); - if (set == NULL) { - *fStatus = U_MEMORY_ALLOCATION_ERROR; - return set; + do { // non-loop, exists to allow breaks from the block. + // + // First try the property as we received it + // + UnicodeString setExpr; + uint32_t usetFlags = 0; + setExpr.append(u"[\\p{", -1); + setExpr.append(propName); + setExpr.append(u"}]", -1); + if (fModeFlags & UREGEX_CASE_INSENSITIVE) { + usetFlags |= USET_CASE_INSENSITIVE; } - if (negated) { - set->complement(); + set.adoptInsteadAndCheckErrorCode(new UnicodeSet(setExpr, usetFlags, NULL, status), status); + if (U_SUCCESS(status) || status == U_MEMORY_ALLOCATION_ERROR) { + break; } - return set; - } + // + // The incoming property wasn't directly recognized by ICU. - // Do Java fixes - - // InGreek -> InGreek or Coptic, that being the official Unicode name for that block. - // InCombiningMarksforSymbols -> InCombiningDiacriticalMarksforSymbols. - // - // Note on Spaces: either "InCombiningMarksForSymbols" or "InCombining Marks for Symbols" - // is accepted by Java. The property part of the name is compared - // case-insenstively. The spaces must be exactly as shown, either - // all there, or all omitted, with exactly one at each position - // if they are present. From checking against JDK 1.6 - // - // This code should be removed when ICU properties support the Java compatibility names - // (ICU 4.0?) - // - UnicodeString mPropName = propName; - if (mPropName.caseCompare(UNICODE_STRING_SIMPLE("InGreek"), 0) == 0) { - mPropName = UNICODE_STRING_SIMPLE("InGreek and Coptic"); - } - if (mPropName.caseCompare(UNICODE_STRING_SIMPLE("InCombining Marks for Symbols"), 0) == 0 || - mPropName.caseCompare(UNICODE_STRING_SIMPLE("InCombiningMarksforSymbols"), 0) == 0) { - mPropName = UNICODE_STRING_SIMPLE("InCombining Diacritical Marks for Symbols"); - } - else if (mPropName.compare(UNICODE_STRING_SIMPLE("all")) == 0) { - mPropName = UNICODE_STRING_SIMPLE("javaValidCodePoint"); - } + // Check [:word:] and [:all:]. These are not recognized as a properties by ICU UnicodeSet. + // Java accepts 'word' with mixed case. + // Java accepts 'all' only in all lower case. - // See if the property looks like a Java "InBlockName", which - // we will recast as "Block=BlockName" - // - static const UChar IN[] = {0x49, 0x6E, 0}; // "In" - static const UChar BLOCK[] = {0x42, 0x6C, 0x6f, 0x63, 0x6b, 0x3d, 00}; // "Block=" - if (mPropName.startsWith(IN, 2) && propName.length()>=3) { - setExpr.truncate(4); // Leaves "[\p{", or "[\P{" - setExpr.append(BLOCK, -1); - setExpr.append(UnicodeString(mPropName, 2)); // Property with the leading "In" removed. - setExpr.append(chRBrace); - setExpr.append(chRBracket); - *fStatus = U_ZERO_ERROR; - set = new UnicodeSet(setExpr, usetFlags, NULL, *fStatus); - if (U_SUCCESS(*fStatus)) { - return set; + status = U_ZERO_ERROR; + if (propName.caseCompare(u"word", -1, 0) == 0) { + set.adoptInsteadAndCheckErrorCode(new UnicodeSet(*(fRXPat->fStaticSets[URX_ISWORD_SET])), status); + break; + } + if (propName.compare(u"all", -1) == 0) { + set.adoptInsteadAndCheckErrorCode(new UnicodeSet(0, 0x10ffff), status); + break; } - delete set; - set = NULL; - } - if (propName.startsWith(UNICODE_STRING_SIMPLE("java")) || - propName.compare(UNICODE_STRING_SIMPLE("all")) == 0) - { - UErrorCode localStatus = U_ZERO_ERROR; - //setExpr.remove(); - set = new UnicodeSet(); - // - // Try the various Java specific properties. - // These all begin with "java" + + // Do Java InBlock expressions // - if (mPropName.compare(UNICODE_STRING_SIMPLE("javaDefined")) == 0) { - addCategory(set, U_GC_CN_MASK, localStatus); - set->complement(); - } - else if (mPropName.compare(UNICODE_STRING_SIMPLE("javaDigit")) == 0) { - addCategory(set, U_GC_ND_MASK, localStatus); - } - else if (mPropName.compare(UNICODE_STRING_SIMPLE("javaIdentifierIgnorable")) == 0) { - addIdentifierIgnorable(set, localStatus); - } - else if (mPropName.compare(UNICODE_STRING_SIMPLE("javaISOControl")) == 0) { - set->add(0, 0x1F).add(0x7F, 0x9F); - } - else if (mPropName.compare(UNICODE_STRING_SIMPLE("javaJavaIdentifierPart")) == 0) { - addCategory(set, U_GC_L_MASK, localStatus); - addCategory(set, U_GC_SC_MASK, localStatus); - addCategory(set, U_GC_PC_MASK, localStatus); - addCategory(set, U_GC_ND_MASK, localStatus); - addCategory(set, U_GC_NL_MASK, localStatus); - addCategory(set, U_GC_MC_MASK, localStatus); - addCategory(set, U_GC_MN_MASK, localStatus); - addIdentifierIgnorable(set, localStatus); - } - else if (mPropName.compare(UNICODE_STRING_SIMPLE("javaJavaIdentifierStart")) == 0) { - addCategory(set, U_GC_L_MASK, localStatus); - addCategory(set, U_GC_NL_MASK, localStatus); - addCategory(set, U_GC_SC_MASK, localStatus); - addCategory(set, U_GC_PC_MASK, localStatus); - } - else if (mPropName.compare(UNICODE_STRING_SIMPLE("javaLetter")) == 0) { - addCategory(set, U_GC_L_MASK, localStatus); - } - else if (mPropName.compare(UNICODE_STRING_SIMPLE("javaLetterOrDigit")) == 0) { - addCategory(set, U_GC_L_MASK, localStatus); - addCategory(set, U_GC_ND_MASK, localStatus); - } - else if (mPropName.compare(UNICODE_STRING_SIMPLE("javaLowerCase")) == 0) { - addCategory(set, U_GC_LL_MASK, localStatus); - } - else if (mPropName.compare(UNICODE_STRING_SIMPLE("javaMirrored")) == 0) { - set->applyIntPropertyValue(UCHAR_BIDI_MIRRORED, 1, localStatus); - } - else if (mPropName.compare(UNICODE_STRING_SIMPLE("javaSpaceChar")) == 0) { - addCategory(set, U_GC_Z_MASK, localStatus); - } - else if (mPropName.compare(UNICODE_STRING_SIMPLE("javaSupplementaryCodePoint")) == 0) { - set->add(0x10000, UnicodeSet::MAX_VALUE); - } - else if (mPropName.compare(UNICODE_STRING_SIMPLE("javaTitleCase")) == 0) { - addCategory(set, U_GC_LT_MASK, localStatus); - } - else if (mPropName.compare(UNICODE_STRING_SIMPLE("javaUnicodeIdentifierStart")) == 0) { - addCategory(set, U_GC_L_MASK, localStatus); - addCategory(set, U_GC_NL_MASK, localStatus); - } - else if (mPropName.compare(UNICODE_STRING_SIMPLE("javaUnicodeIdentifierPart")) == 0) { - addCategory(set, U_GC_L_MASK, localStatus); - addCategory(set, U_GC_PC_MASK, localStatus); - addCategory(set, U_GC_ND_MASK, localStatus); - addCategory(set, U_GC_NL_MASK, localStatus); - addCategory(set, U_GC_MC_MASK, localStatus); - addCategory(set, U_GC_MN_MASK, localStatus); - addIdentifierIgnorable(set, localStatus); - } - else if (mPropName.compare(UNICODE_STRING_SIMPLE("javaUpperCase")) == 0) { - addCategory(set, U_GC_LU_MASK, localStatus); - } - else if (mPropName.compare(UNICODE_STRING_SIMPLE("javaValidCodePoint")) == 0) { - set->add(0, UnicodeSet::MAX_VALUE); - } - else if (mPropName.compare(UNICODE_STRING_SIMPLE("javaWhitespace")) == 0) { - addCategory(set, U_GC_Z_MASK, localStatus); - set->removeAll(UnicodeSet().add(0xa0).add(0x2007).add(0x202f)); - set->add(9, 0x0d).add(0x1c, 0x1f); - } - else if (mPropName.compare(UNICODE_STRING_SIMPLE("all")) == 0) { - set->add(0, UnicodeSet::MAX_VALUE); + UnicodeString mPropName = propName; + if (mPropName.startsWith(u"In", 2) && mPropName.length() >= 3) { + status = U_ZERO_ERROR; + set.adoptInsteadAndCheckErrorCode(new UnicodeSet(), status); + if (U_FAILURE(status)) { + break; + } + UnicodeString blockName(mPropName, 2); // Property with the leading "In" removed. + set->applyPropertyAlias(UnicodeString(u"Block"), blockName, status); + break; } - if (U_SUCCESS(localStatus) && !set->isEmpty()) { - *fStatus = U_ZERO_ERROR; - if (usetFlags & USET_CASE_INSENSITIVE) { + // Check for the Java form "IsBooleanPropertyValue", which we will recast + // as "BooleanPropertyValue". The property value can be either a + // a General Category or a Script Name. + + if (propName.startsWith(u"Is", 2) && propName.length()>=3) { + mPropName.remove(0, 2); // Strip the "Is" + if (mPropName.indexOf(u'=') >= 0) { + // Reject any "Is..." property expression containing an '=', that is, + // any non-binary property expression. + status = U_REGEX_PROPERTY_SYNTAX; + break; + } + + if (mPropName.caseCompare(u"assigned", -1, 0) == 0) { + mPropName.setTo(u"unassigned", -1); + negated = !negated; + } else if (mPropName.caseCompare(u"TitleCase", -1, 0) == 0) { + mPropName.setTo(u"Titlecase_Letter", -1); + } + + mPropName.insert(0, u"[\\p{", -1); + mPropName.append(u"}]", -1); + set.adoptInsteadAndCheckErrorCode(new UnicodeSet(mPropName, *fStatus), status); + + if (U_SUCCESS(status) && !set->isEmpty() && (usetFlags & USET_CASE_INSENSITIVE)) { set->closeOver(USET_CASE_INSENSITIVE); } - if (negated) { + break; + + } + + if (propName.startsWith(u"java", -1)) { + status = U_ZERO_ERROR; + set.adoptInsteadAndCheckErrorCode(new UnicodeSet(), status); + if (U_FAILURE(status)) { + break; + } + // + // Try the various Java specific properties. + // These all begin with "java" + // + if (propName.compare(u"javaDefined", -1) == 0) { + addCategory(set.getAlias(), U_GC_CN_MASK, status); set->complement(); } - return set; + else if (propName.compare(u"javaDigit", -1) == 0) { + addCategory(set.getAlias(), U_GC_ND_MASK, status); + } + else if (propName.compare(u"javaIdentifierIgnorable", -1) == 0) { + addIdentifierIgnorable(set.getAlias(), status); + } + else if (propName.compare(u"javaISOControl", -1) == 0) { + set->add(0, 0x1F).add(0x7F, 0x9F); + } + else if (propName.compare(u"javaJavaIdentifierPart", -1) == 0) { + addCategory(set.getAlias(), U_GC_L_MASK, status); + addCategory(set.getAlias(), U_GC_SC_MASK, status); + addCategory(set.getAlias(), U_GC_PC_MASK, status); + addCategory(set.getAlias(), U_GC_ND_MASK, status); + addCategory(set.getAlias(), U_GC_NL_MASK, status); + addCategory(set.getAlias(), U_GC_MC_MASK, status); + addCategory(set.getAlias(), U_GC_MN_MASK, status); + addIdentifierIgnorable(set.getAlias(), status); + } + else if (propName.compare(u"javaJavaIdentifierStart", -1) == 0) { + addCategory(set.getAlias(), U_GC_L_MASK, status); + addCategory(set.getAlias(), U_GC_NL_MASK, status); + addCategory(set.getAlias(), U_GC_SC_MASK, status); + addCategory(set.getAlias(), U_GC_PC_MASK, status); + } + else if (propName.compare(u"javaLetter", -1) == 0) { + addCategory(set.getAlias(), U_GC_L_MASK, status); + } + else if (propName.compare(u"javaLetterOrDigit", -1) == 0) { + addCategory(set.getAlias(), U_GC_L_MASK, status); + addCategory(set.getAlias(), U_GC_ND_MASK, status); + } + else if (propName.compare(u"javaLowerCase", -1) == 0) { + addCategory(set.getAlias(), U_GC_LL_MASK, status); + } + else if (propName.compare(u"javaMirrored", -1) == 0) { + set->applyIntPropertyValue(UCHAR_BIDI_MIRRORED, 1, status); + } + else if (propName.compare(u"javaSpaceChar", -1) == 0) { + addCategory(set.getAlias(), U_GC_Z_MASK, status); + } + else if (propName.compare(u"javaSupplementaryCodePoint", -1) == 0) { + set->add(0x10000, UnicodeSet::MAX_VALUE); + } + else if (propName.compare(u"javaTitleCase", -1) == 0) { + addCategory(set.getAlias(), U_GC_LT_MASK, status); + } + else if (propName.compare(u"javaUnicodeIdentifierStart", -1) == 0) { + addCategory(set.getAlias(), U_GC_L_MASK, status); + addCategory(set.getAlias(), U_GC_NL_MASK, status); + } + else if (propName.compare(u"javaUnicodeIdentifierPart", -1) == 0) { + addCategory(set.getAlias(), U_GC_L_MASK, status); + addCategory(set.getAlias(), U_GC_PC_MASK, status); + addCategory(set.getAlias(), U_GC_ND_MASK, status); + addCategory(set.getAlias(), U_GC_NL_MASK, status); + addCategory(set.getAlias(), U_GC_MC_MASK, status); + addCategory(set.getAlias(), U_GC_MN_MASK, status); + addIdentifierIgnorable(set.getAlias(), status); + } + else if (propName.compare(u"javaUpperCase", -1) == 0) { + addCategory(set.getAlias(), U_GC_LU_MASK, status); + } + else if (propName.compare(u"javaValidCodePoint", -1) == 0) { + set->add(0, UnicodeSet::MAX_VALUE); + } + else if (propName.compare(u"javaWhitespace", -1) == 0) { + addCategory(set.getAlias(), U_GC_Z_MASK, status); + set->removeAll(UnicodeSet().add(0xa0).add(0x2007).add(0x202f)); + set->add(9, 0x0d).add(0x1c, 0x1f); + } else { + status = U_REGEX_PROPERTY_SYNTAX; + } + + if (U_SUCCESS(status) && !set->isEmpty() && (usetFlags & USET_CASE_INSENSITIVE)) { + set->closeOver(USET_CASE_INSENSITIVE); + } + break; + } + + // Unrecognized property. ICU didn't like it as it was, and none of the Java compatibility + // extensions matched it. + status = U_REGEX_PROPERTY_SYNTAX; + } while (false); // End of do loop block. Code above breaks out of the block on success or hard failure. + + if (U_SUCCESS(status)) { + U_ASSERT(set.isValid()); + if (negated) { + set->complement(); + } + return set.orphan(); + } else { + if (status == U_ILLEGAL_ARGUMENT_ERROR) { + status = U_REGEX_PROPERTY_SYNTAX; } - delete set; - set = NULL; + error(status); + return nullptr; } - error(*fStatus); - return NULL; } - // // SetEval Part of the evaluation of [set expressions]. // Perform any pending (stacked) operations with precedence @@ -4324,8 +4637,7 @@ void RegexCompile::setEval(int32_t nextOp) { delete rightOperand; break; default: - U_ASSERT(FALSE); - break; + UPRV_UNREACHABLE; } } }