// Constructor and Destructor
//
//-----------------------------------------------------------------------------
-RegexMatcher::RegexMatcher(const RegexPattern *pat) {
+RegexMatcher::RegexMatcher(const RegexPattern *pat) {
fDeferredStatus = U_ZERO_ERROR;
init(fDeferredStatus);
if (U_FAILURE(fDeferredStatus)) {
UParseError pe;
fPatternOwned = RegexPattern::compile(regexp, flags, pe, status);
fPattern = fPatternOwned;
-
+
UText inputText = UTEXT_INITIALIZER;
utext_openConstUnicodeString(&inputText, &input, &status);
init2(&inputText, status);
utext_close(&inputText);
- fInputUniStrMaybeMutable = TRUE;
+ fInputUniStrMaybeMutable = TRUE;
}
}
-RegexMatcher::RegexMatcher(const UnicodeString ®exp,
+RegexMatcher::RegexMatcher(const UnicodeString ®exp,
uint32_t flags, UErrorCode &status) {
init(status);
if (U_FAILURE(status)) {
init2(RegexStaticSets::gStaticSets->fEmptyText, status);
}
-RegexMatcher::RegexMatcher(UText *regexp,
+RegexMatcher::RegexMatcher(UText *regexp,
uint32_t flags, UErrorCode &status) {
init(status);
if (U_FAILURE(status)) {
fPatternOwned = NULL;
fPattern = NULL;
}
-
+
if (fInput) {
delete fInput;
}
if (fAltInputText) {
utext_close(fAltInputText);
}
-
+
#if UCONFIG_NO_BREAK_ITERATION==0
delete fWordBreakItr;
#endif
fDeferredStatus = status;
fData = fSmallData;
fWordBreakItr = NULL;
-
+
fStack = NULL;
fInputText = NULL;
fAltInputText = NULL;
}
if (fPattern->fDataSize > (int32_t)(sizeof(fSmallData)/sizeof(fSmallData[0]))) {
- fData = (int64_t *)uprv_malloc(fPattern->fDataSize * sizeof(int64_t));
+ fData = (int64_t *)uprv_malloc(fPattern->fDataSize * sizeof(int64_t));
if (fData == NULL) {
status = fDeferredStatus = U_MEMORY_ALLOCATION_ERROR;
return;
const UnicodeString &replacement,
UErrorCode &status) {
UText replacementText = UTEXT_INITIALIZER;
-
+
utext_openConstUnicodeString(&replacementText, &replacement, &status);
- if (U_SUCCESS(status)) {
+ if (U_SUCCESS(status)) {
UText resultText = UTEXT_INITIALIZER;
utext_openUnicodeString(&resultText, &dest, &status);
-
+
if (U_SUCCESS(status)) {
appendReplacement(&resultText, &replacementText, status);
utext_close(&resultText);
}
utext_close(&replacementText);
}
-
+
return *this;
}
status = U_REGEX_INVALID_STATE;
return *this;
}
-
+
// Copy input string from the end of previous match to start of current match
int64_t destLen = utext_nativeLength(dest);
if (fMatchStart > fAppendPosition) {
if (UTEXT_FULL_TEXT_IN_CHUNK(fInputText, fInputLength)) {
- destLen += utext_replace(dest, destLen, destLen, fInputText->chunkContents+fAppendPosition,
+ destLen += utext_replace(dest, destLen, destLen, fInputText->chunkContents+fAppendPosition,
(int32_t)(fMatchStart-fAppendPosition), &status);
} else {
int32_t len16;
}
}
fAppendPosition = fMatchEnd;
-
-
+
+
// scan the replacement text, looking for substitutions ($n) and \escapes.
// TODO: optimize this loop by efficiently scanning for '$' or '\',
// move entire ranges not containing substitutions.
if (c == U_SENTINEL) {
break;
}
-
+
if (c==0x55/*U*/ || c==0x75/*u*/) {
// We have a \udddd or \Udddddddd escape sequence.
int32_t offset = 0;
// We've got a $. Pick up a capture group number if one follows.
// Consume at most the number of digits necessary for the largest capture
// number that is valid for this pattern.
-
+
int32_t numDigits = 0;
int32_t groupNum = 0;
UChar32 digitC;
break;
}
}
-
-
+
+
if (numDigits == 0) {
// The $ didn't introduce a group number at all.
// Treat it as just part of the substitution text.
}
}
}
-
+
if (U_FAILURE(status)) {
break;
} else {
c = UTEXT_NEXT32(replacement);
}
}
-
+
return *this;
}
UErrorCode status = U_ZERO_ERROR;
UText resultText = UTEXT_INITIALIZER;
utext_openUnicodeString(&resultText, &dest, &status);
-
+
if (U_SUCCESS(status)) {
appendTail(&resultText, status);
utext_close(&resultText);
}
-
+
return dest;
}
// appendTail, UText mode
//
UText *RegexMatcher::appendTail(UText *dest, UErrorCode &status) {
- UBool bailOut = FALSE;
if (U_FAILURE(status)) {
- bailOut = TRUE;
+ return dest;
}
if (U_FAILURE(fDeferredStatus)) {
status = fDeferredStatus;
- bailOut = TRUE;
- }
-
- if (bailOut) {
- // dest must not be NULL
- if (dest) {
- utext_replace(dest, utext_nativeLength(dest), utext_nativeLength(dest), NULL, 0, &status);
- return dest;
- }
+ return dest;
}
-
+
if (fInputLength > fAppendPosition) {
if (UTEXT_FULL_TEXT_IN_CHUNK(fInputText, fInputLength)) {
int64_t destLen = utext_nativeLength(dest);
- utext_replace(dest, destLen, destLen, fInputText->chunkContents+fAppendPosition,
+ utext_replace(dest, destLen, destLen, fInputText->chunkContents+fAppendPosition,
(int32_t)(fInputLength-fAppendPosition), &status);
} else {
int32_t len16;
len16 = utext_extract(fInputText, fAppendPosition, fInputLength, NULL, 0, &status);
status = U_ZERO_ERROR; // buffer overflow
}
-
+
UChar *inputChars = (UChar *)uprv_malloc(sizeof(UChar)*(len16));
if (inputChars == NULL) {
fDeferredStatus = U_MEMORY_ALLOCATION_ERROR;
} else {
- utext_extract(fInputText, fAppendPosition, fInputLength, inputChars, len16, &status); // unterminated
+ utext_extract(fInputText, fAppendPosition, fInputLength, inputChars, len16, &status); // unterminated
int64_t destLen = utext_nativeLength(dest);
utext_replace(dest, destLen, destLen, inputChars, len16, &status);
uprv_free(inputChars);
}
int64_t e = -1;
if (group == 0) {
- e = fMatchEnd;
+ e = fMatchEnd;
} else {
// Get the position within the stack frame of the variables for
// this capture group.
U_ASSERT(groupOffset >= 0);
e = fFrame->fExtra[groupOffset + 1];
}
-
+
return e;
}
if (U_FAILURE(fDeferredStatus)) {
return FALSE;
}
-
+
if (UTEXT_FULL_TEXT_IN_CHUNK(fInputText, fInputLength)) {
return findUsingChunk();
}
switch (fPattern->fStartType) {
case START_NO_INFO:
- // No optimization was found.
+ // No optimization was found.
// Try a match at each input position.
for (;;) {
MatchAt(startPos, FALSE, fDeferredStatus);
status = U_INDEX_OUTOFBOUNDS_ERROR;
return FALSE;
}
-
+
int64_t nativeStart = start;
if (nativeStart < fActiveStart || nativeStart > fActiveLimit) {
status = U_INDEX_OUTOFBOUNDS_ERROR;
return FALSE;
}
- fMatchEnd = nativeStart;
+ fMatchEnd = nativeStart;
return find();
}
if (startPos==0) {
startPos = (int32_t)fActiveStart;
}
-
+
const UChar *inputBuf = fInputText->chunkContents;
if (fMatch) {
// Save the position of any previous successful match.
fLastMatchEnd = fMatchEnd;
-
+
if (fMatchStart == fMatchEnd) {
// Previous match had zero length. Move start position up one position
// to avoid sending find() into a loop on zero-length matches.
return FALSE;
}
}
-
-
+
+
// Compute the position in the input string beyond which a match can not begin, because
// the minimum length match would extend past the end of the input.
// Note: some patterns that cannot match anything will have fMinMatchLength==Max Int.
fHitEnd = TRUE;
return FALSE;
}
-
+
UChar32 c;
U_ASSERT(startPos >= 0);
-
+
switch (fPattern->fStartType) {
case START_NO_INFO:
- // No optimization was found.
+ // No optimization was found.
// Try a match at each input position.
for (;;) {
MatchChunkAt(startPos, FALSE, fDeferredStatus);
return FALSE;
}
U_ASSERT(FALSE);
-
+
case START_START:
// Matches are only possible at the start of the input string
// (pattern begins with ^ or \A)
return FALSE;
}
return fMatch;
-
-
+
+
case START_SET:
{
// Match may start on any char from a pre-computed set.
}
}
U_ASSERT(FALSE);
-
+
case START_STRING:
case START_CHAR:
{
}
}
U_ASSERT(FALSE);
-
+
case START_LINE:
{
UChar32 c;
}
U16_FWD_1(inputBuf, startPos, fActiveLimit);
}
-
+
if (fPattern->fFlags & UREGEX_UNIX_LINES) {
for (;;) {
c = inputBuf[startPos-1];
}
}
}
-
+
default:
U_ASSERT(FALSE);
}
-
+
U_ASSERT(FALSE);
return FALSE;
}
// Return immutable shallow clone
UText *RegexMatcher::group(int32_t groupNum, UText *dest, int64_t &group_len, UErrorCode &status) const {
group_len = 0;
- UBool bailOut = FALSE;
if (U_FAILURE(status)) {
return dest;
}
if (U_FAILURE(fDeferredStatus)) {
status = fDeferredStatus;
- bailOut = TRUE;
- }
- if (fMatch == FALSE) {
+ } else if (fMatch == FALSE) {
status = U_REGEX_INVALID_STATE;
- bailOut = TRUE;
- }
- if (groupNum < 0 || groupNum > fPattern->fGroupMap->size()) {
+ } else if (groupNum < 0 || groupNum > fPattern->fGroupMap->size()) {
status = U_INDEX_OUTOFBOUNDS_ERROR;
- bailOut = TRUE;
}
-
- if (bailOut) {
- return (dest) ? dest : utext_openUChars(NULL, NULL, 0, &status);
+
+ if (U_FAILURE(status)) {
+ return dest;
}
-
+
int64_t s, e;
if (groupNum == 0) {
s = fMatchStart;
}
U_ASSERT(s <= e);
group_len = e - s;
-
+
dest = utext_clone(dest, fInputText, FALSE, TRUE, &status);
if (dest)
UTEXT_SETNATIVEINDEX(dest, s);
// Technology Preview (as an API), but note that the UnicodeString API is implemented
// using this function.
UText *RegexMatcher::group(int32_t groupNum, UText *dest, UErrorCode &status) const {
- UBool bailOut = FALSE;
if (U_FAILURE(status)) {
return dest;
}
+
if (U_FAILURE(fDeferredStatus)) {
status = fDeferredStatus;
- bailOut = TRUE;
- }
-
- if (fMatch == FALSE) {
+ } else if (fMatch == FALSE) {
status = U_REGEX_INVALID_STATE;
- bailOut = TRUE;
- }
- if (groupNum < 0 || groupNum > fPattern->fGroupMap->size()) {
+ } else if (groupNum < 0 || groupNum > fPattern->fGroupMap->size()) {
status = U_INDEX_OUTOFBOUNDS_ERROR;
- bailOut = TRUE;
}
-
- if (bailOut) {
- if (dest) {
- utext_replace(dest, 0, utext_nativeLength(dest), NULL, 0, &status);
- return dest;
- } else {
- return utext_openUChars(NULL, NULL, 0, &status);
- }
+ if (U_FAILURE(status)) {
+ return dest;
}
-
+
int64_t s, e;
if (groupNum == 0) {
s = fMatchStart;
s = fFrame->fExtra[groupOffset];
e = fFrame->fExtra[groupOffset+1];
}
-
+
if (s < 0) {
- // A capture group wasn't part of the match
+ // A capture group wasn't part of the match
if (dest) {
utext_replace(dest, 0, utext_nativeLength(dest), NULL, 0, &status);
return dest;
}
}
U_ASSERT(s <= e);
-
+
if (UTEXT_FULL_TEXT_IN_CHUNK(fInputText, fInputLength)) {
U_ASSERT(e <= fInputLength);
if (dest) {
dest = utext_clone(NULL, &groupText, TRUE, FALSE, &status);
utext_close(&groupText);
}
-
+
uprv_free(groupChars);
}
return dest;
return 0;
}
int64_t destLen = utext_nativeLength(dest);
-
+
if (fMatch == FALSE) {
status = U_REGEX_INVALID_STATE;
return utext_replace(dest, destLen, destLen, NULL, 0, &status);
status = U_INDEX_OUTOFBOUNDS_ERROR;
return utext_replace(dest, destLen, destLen, NULL, 0, &status);
}
-
+
int64_t s, e;
if (groupNum == 0) {
s = fMatchStart;
s = fFrame->fExtra[groupOffset];
e = fFrame->fExtra[groupOffset+1];
}
-
+
if (s < 0) {
- // A capture group wasn't part of the match
+ // A capture group wasn't part of the match
return utext_replace(dest, destLen, destLen, NULL, 0, &status);
}
U_ASSERT(s <= e);
-
+
int64_t deltaLen;
if (UTEXT_FULL_TEXT_IN_CHUNK(fInputText, fInputLength)) {
U_ASSERT(e <= fInputLength);
return 0;
}
utext_extract(fInputText, s, e, groupChars, len16+1, &status);
-
+
deltaLen = utext_replace(dest, destLen, destLen, groupChars, len16, &status);
uprv_free(groupChars);
}
status = U_ZERO_ERROR; // overflow, length status
}
UnicodeString *result = new UnicodeString(len16, 0, 0);
-
+
UChar *inputChars = result->getBuffer(len16);
utext_extract(fInputText, 0, fInputLength, inputChars, len16, &status); // unterminated warning
result->releaseBuffer(len16);
-
+
(*(const UnicodeString **)&fInput) = result; // pointer assignment, rather than operator=
}
-
+
return *fInput;
}
//
//--------------------------------------------------------------------------------
UText *RegexMatcher::getInput (UText *dest, UErrorCode &status) const {
- UBool bailOut = FALSE;
if (U_FAILURE(status)) {
return dest;
}
if (U_FAILURE(fDeferredStatus)) {
status = fDeferredStatus;
- bailOut = TRUE;
- }
-
- if (bailOut) {
- if (dest) {
- utext_replace(dest, 0, utext_nativeLength(dest), NULL, 0, &status);
- return dest;
- } else {
- return utext_clone(NULL, fInputText, FALSE, TRUE, &status);
- }
+ return dest;
}
-
+
if (dest) {
if (UTEXT_FULL_TEXT_IN_CHUNK(fInputText, fInputLength)) {
utext_replace(dest, 0, utext_nativeLength(dest), fInputText->chunkContents, (int32_t)fInputLength, &status);
if (inputChars == NULL) {
return dest;
}
-
+
status = U_ZERO_ERROR;
utext_extract(fInputText, 0, fInputLength, inputChars, input16Len, &status); // not terminated warning
status = U_ZERO_ERROR;
utext_replace(dest, 0, utext_nativeLength(dest), inputChars, input16Len, &status);
-
+
uprv_free(inputChars);
}
return dest;
static UBool compat_SyncMutableUTextContents(UText *ut);
static UBool compat_SyncMutableUTextContents(UText *ut) {
UBool retVal = FALSE;
-
+
// In the following test, we're really only interested in whether the UText should switch
// between heap and stack allocation. If length hasn't changed, we won't, so the chunkContents
// will still point to the correct data.
if (utext_nativeLength(ut) != ut->nativeIndexingLimit) {
UnicodeString *us=(UnicodeString *)ut->context;
-
+
// Update to the latest length.
// For example, (utext_nativeLength(ut) != ut->nativeIndexingLimit).
int32_t newLength = us->length();
-
+
// Update the chunk description.
// The buffer may have switched between stack- and heap-based.
ut->chunkContents = us->getBuffer();
status = fDeferredStatus;
return FALSE;
}
-
+
if (fInputUniStrMaybeMutable) {
if (compat_SyncMutableUTextContents(fInputText)) {
fInputLength = utext_nativeLength(fInputText);
return FALSE;
}
reset();
-
+
if (start < 0) {
status = U_INDEX_OUTOFBOUNDS_ERROR;
return FALSE;
}
-
+
if (fInputUniStrMaybeMutable) {
if (compat_SyncMutableUTextContents(fInputText)) {
fInputLength = utext_nativeLength(fInputText);
status = U_INDEX_OUTOFBOUNDS_ERROR;
return FALSE;
}
-
+
if (UTEXT_FULL_TEXT_IN_CHUNK(fInputText, fInputLength)) {
MatchChunkAt((int32_t)nativeStart, FALSE, status);
} else {
return FALSE;
}
reset();
-
+
if (start < 0) {
status = U_INDEX_OUTOFBOUNDS_ERROR;
return FALSE;
if (U_FAILURE(status)) {
return *this;
}
-
+
if (regionStart>regionLimit || regionStart<0 || regionLimit<0) {
status = U_ILLEGAL_ARGUMENT_ERROR;
}
-
+
int64_t nativeStart = regionStart;
int64_t nativeLimit = regionLimit;
if (nativeStart > fInputLength || nativeLimit > fInputLength) {
if (startIndex == -1)
this->reset();
else
- resetPreserveRegion();
-
+ resetPreserveRegion();
+
fRegionStart = nativeStart;
fRegionLimit = nativeLimit;
fActiveStart = nativeStart;
if (startIndex < fActiveStart || startIndex > fActiveLimit) {
status = U_INDEX_OUTOFBOUNDS_ERROR;
}
- fMatchEnd = startIndex;
+ fMatchEnd = startIndex;
}
if (!fTransparentBounds) {
if (U_FAILURE(status)) {
return resultString;
}
-
+
utext_openConstUnicodeString(&replacementText, &replacement, &status);
utext_openUnicodeString(&resultText, &resultString, &status);
-
+
replaceAll(&replacementText, &resultText, status);
utext_close(&resultText);
utext_close(&replacementText);
-
+
return resultString;
}
status = fDeferredStatus;
return dest;
}
-
+
if (dest == NULL) {
UnicodeString emptyString;
UText empty = UTEXT_INITIALIZER;
-
+
utext_openUnicodeString(&empty, &emptyString, &status);
dest = utext_clone(NULL, &empty, TRUE, FALSE, &status);
utext_close(&empty);
}
appendTail(dest, status);
}
-
+
return dest;
}
UText replacementText = UTEXT_INITIALIZER;
UText resultText = UTEXT_INITIALIZER;
UnicodeString resultString;
-
+
utext_openConstUnicodeString(&replacementText, &replacement, &status);
utext_openUnicodeString(&resultText, &resultString, &status);
-
+
replaceFirst(&replacementText, &resultText, status);
-
+
utext_close(&resultText);
utext_close(&replacementText);
-
+
return resultString;
}
if (!find()) {
return getInput(dest, status);
}
-
+
if (dest == NULL) {
UnicodeString emptyString;
UText empty = UTEXT_INITIALIZER;
-
+
utext_openUnicodeString(&empty, &emptyString, &status);
dest = utext_clone(NULL, &empty, TRUE, FALSE, &status);
utext_close(&empty);
}
-
+
appendReplacement(dest, replacement, status);
appendTail(dest, status);
-
+
return dest;
}
fAltInputText = utext_clone(fAltInputText, fInputText, FALSE, TRUE, &fDeferredStatus);
}
fInputLength = utext_nativeLength(fInputText);
-
+
reset();
delete fInput;
fInput = NULL;
// Do the following for any UnicodeString.
// This is for compatibility for those clients who modify the input string "live" during regex operations.
- fInputUniStrMaybeMutable = TRUE;
-
+ fInputUniStrMaybeMutable = TRUE;
+
if (fWordBreakItr != NULL) {
#if UCONFIG_NO_BREAK_ITERATION==0
UErrorCode status = U_ZERO_ERROR;
fInputText = utext_clone(fInputText, input, FALSE, TRUE, &fDeferredStatus);
if (fPattern->fNeedsAltInput) fAltInputText = utext_clone(fAltInputText, fInputText, FALSE, TRUE, &fDeferredStatus);
fInputLength = utext_nativeLength(fInputText);
-
+
delete fInput;
fInput = NULL;
-
+
if (fWordBreakItr != NULL) {
#if UCONFIG_NO_BREAK_ITERATION==0
UErrorCode status = U_ZERO_ERROR;
return *this;
}
reset(); // Reset also resets the region to be the entire string.
-
+
if (position < 0 || position > fActiveLimit) {
status = U_INDEX_OUTOFBOUNDS_ERROR;
return *this;
for (i = 0; i < destCapacity; i++) {
destText[i] = utext_openUnicodeString(NULL, &dest[i], &status);
}
-
+
int32_t fieldCount = split(&inputText, destText, destCapacity, status);
-
+
for (i = 0; i < destCapacity; i++) {
utext_close(destText[i]);
}
if (fActiveLimit > nextOutputStringStart) {
if (UTEXT_FULL_TEXT_IN_CHUNK(input, fInputLength)) {
if (dest[i]) {
- utext_replace(dest[i], 0, utext_nativeLength(dest[i]),
- input->chunkContents+nextOutputStringStart,
+ utext_replace(dest[i], 0, utext_nativeLength(dest[i]),
+ input->chunkContents+nextOutputStringStart,
(int32_t)(fActiveLimit-nextOutputStringStart), &status);
} else {
UText remainingText = UTEXT_INITIALIZER;
- utext_openUChars(&remainingText, input->chunkContents+nextOutputStringStart,
+ utext_openUChars(&remainingText, input->chunkContents+nextOutputStringStart,
fActiveLimit-nextOutputStringStart, &status);
dest[i] = utext_clone(NULL, &remainingText, TRUE, FALSE, &status);
utext_close(&remainingText);
}
} else {
UErrorCode lengthStatus = U_ZERO_ERROR;
- int32_t remaining16Length =
+ int32_t remaining16Length =
utext_extract(input, nextOutputStringStart, fActiveLimit, NULL, 0, &lengthStatus);
UChar *remainingChars = (UChar *)uprv_malloc(sizeof(UChar)*(remaining16Length+1));
if (remainingChars == NULL) {
dest[i] = utext_clone(NULL, &remainingText, TRUE, FALSE, &status);
utext_close(&remainingText);
}
-
+
uprv_free(remainingChars);
}
}
// up until the start of the delimiter into the next output string.
if (UTEXT_FULL_TEXT_IN_CHUNK(input, fInputLength)) {
if (dest[i]) {
- utext_replace(dest[i], 0, utext_nativeLength(dest[i]),
- input->chunkContents+nextOutputStringStart,
+ utext_replace(dest[i], 0, utext_nativeLength(dest[i]),
+ input->chunkContents+nextOutputStringStart,
(int32_t)(fMatchStart-nextOutputStringStart), &status);
} else {
UText remainingText = UTEXT_INITIALIZER;
- utext_openUChars(&remainingText, input->chunkContents+nextOutputStringStart,
+ utext_openUChars(&remainingText, input->chunkContents+nextOutputStringStart,
fMatchStart-nextOutputStringStart, &status);
dest[i] = utext_clone(NULL, &remainingText, TRUE, FALSE, &status);
utext_close(&remainingText);
dest[i] = utext_clone(NULL, &remainingText, TRUE, FALSE, &status);
utext_close(&remainingText);
}
-
+
uprv_free(remainingChars);
}
nextOutputStringStart = fMatchEnd;
}
}
break;
-
- }
+
+ }
}
else
{
// All the remaining text goes into the current output string.
if (UTEXT_FULL_TEXT_IN_CHUNK(input, fInputLength)) {
if (dest[i]) {
- utext_replace(dest[i], 0, utext_nativeLength(dest[i]),
- input->chunkContents+nextOutputStringStart,
+ utext_replace(dest[i], 0, utext_nativeLength(dest[i]),
+ input->chunkContents+nextOutputStringStart,
(int32_t)(fActiveLimit-nextOutputStringStart), &status);
} else {
UText remainingText = UTEXT_INITIALIZER;
- utext_openUChars(&remainingText, input->chunkContents+nextOutputStringStart,
+ utext_openUChars(&remainingText, input->chunkContents+nextOutputStringStart,
fActiveLimit-nextOutputStringStart, &status);
dest[i] = utext_clone(NULL, &remainingText, TRUE, FALSE, &status);
utext_close(&remainingText);
status = U_MEMORY_ALLOCATION_ERROR;
break;
}
-
+
utext_extract(input, nextOutputStringStart, fActiveLimit, remainingChars, remaining16Length+1, &status);
if (dest[i]) {
utext_replace(dest[i], 0, utext_nativeLength(dest[i]), remainingChars, remaining16Length, &status);
dest[i] = utext_clone(NULL, &remainingText, TRUE, FALSE, &status);
utext_close(&remainingText);
}
-
+
uprv_free(remainingChars);
}
break;
}
int64_t s;
if (group == 0) {
- s = fMatchStart;
+ s = fMatchStart;
} else {
int32_t groupOffset = fPattern->fGroupMap->elementAti(group-1);
U_ASSERT(groupOffset < fPattern->fFrameSize);
U_ASSERT(groupOffset >= 0);
s = fFrame->fExtra[groupOffset];
}
-
+
return s;
}
status = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
-
+
// Reset the matcher. This is needed here in case there is a current match
- // whose final stack frame (containing the match results, pointed to by fFrame)
+ // whose final stack frame (containing the match results, pointed to by fFrame)
// would be lost by resizing to a smaller stack size.
reset();
-
+
if (limit == 0) {
// Unlimited stack expansion
fStack->setMaxCapacity(0);
} else {
// Change the units of the limit from bytes to ints, and bump the size up
- // to be big enough to hold at least one stack frame for the pattern,
+ // to be big enough to hold at least one stack frame for the pattern,
// if it isn't there already.
int32_t adjustedLimit = limit / sizeof(int32_t);
if (adjustedLimit < fPattern->fFrameSize) {
//
// resetStack
// Discard any previous contents of the state save stack, and initialize a
-// new stack frame to all -1. The -1s are needed for capture group limits,
+// new stack frame to all -1. The -1s are needed for capture group limits,
// where they indicate that a group has not yet matched anything.
//--------------------------------------------------------------------------------
REStackFrame *RegexMatcher::resetStack() {
//--------------------------------------------------------------------------------
//
-// isWordBoundary
+// isWordBoundary
// in perl, "xab..cd..", \b is true at positions 0,3,5,7
// For us,
// If the current char is a combining mark,
UBool RegexMatcher::isWordBoundary(int64_t pos) {
UBool isBoundary = FALSE;
UBool cIsWord = FALSE;
-
+
if (pos >= fLookLimit) {
fHitEnd = TRUE;
} else {
}
cIsWord = fPattern->fStaticSets[URX_ISWORD_SET]->contains(c);
}
-
+
// Back up until we come to a non-combining char, determine whether
// that char is a word char.
UBool prevCIsWord = FALSE;
UBool RegexMatcher::isChunkWordBoundary(int32_t pos) {
UBool isBoundary = FALSE;
UBool cIsWord = FALSE;
-
+
const UChar *inputBuf = fInputText->chunkContents;
-
+
if (pos >= fLookLimit) {
fHitEnd = TRUE;
} else {
}
cIsWord = fPattern->fStaticSets[URX_ISWORD_SET]->contains(c);
}
-
+
// Back up until we come to a non-combining char, determine whether
// that char is a word char.
UBool prevCIsWord = FALSE;
//--------------------------------------------------------------------------------
//
-// isUWordBoundary
+// isUWordBoundary
//
// Test for a word boundary using RBBI word break.
//
UBool RegexMatcher::isUWordBoundary(int64_t pos) {
UBool returnVal = FALSE;
#if UCONFIG_NO_BREAK_ITERATION==0
-
+
// If we haven't yet created a break iterator for this matcher, do it now.
if (fWordBreakItr == NULL) {
- fWordBreakItr =
+ fWordBreakItr =
(RuleBasedBreakIterator *)BreakIterator::createWordInstance(Locale::getEnglish(), fDeferredStatus);
if (U_FAILURE(fDeferredStatus)) {
return FALSE;
// ReportFindProgress This function is called once for each advance in the target
// string from the find() function, and calls the user progress callback
// function if there is one installed.
-//
-// NOTE:
+//
+// NOTE:
//
// If the match operation needs to be aborted because the user
// callback asked for it, just set an error status.
// whole thing being relocated in memory.
//
// Parameters:
-// fp The top frame pointer when called. At return, a new
+// fp The top frame pointer when called. At return, a new
// fame will be present
// savePatIdx An index into the compiled pattern. Goes into the original
// (not new) frame. If execution ever back-tracks out of the
//
//--------------------------------------------------------------------------------
inline REStackFrame *RegexMatcher::StateSave(REStackFrame *fp, int64_t savePatIdx, UErrorCode &status) {
- // push storage for a new frame.
+ // push storage for a new frame.
int64_t *newFP = fStack->reserveBlock(fFrameSize, status);
if (newFP == NULL) {
// Failure on attempted stack expansion.
return fp;
}
fp = (REStackFrame *)(newFP - fFrameSize); // in case of realloc of stack.
-
+
// New stack frame = copy of old top frame.
int64_t *source = (int64_t *)fp;
int64_t *dest = newFP;
break;
}
}
-
+
fTickCounter--;
if (fTickCounter <= 0) {
IncrementTime(status); // Re-initializes fTickCounter
//--------------------------------------------------------------------------------
void RegexMatcher::MatchAt(int64_t startIdx, UBool toEnd, UErrorCode &status) {
UBool isMatch = FALSE; // True if the we have a match.
-
+
int64_t backSearchIndex = U_INT64_MAX; // used after greedy single-character matches for searching backwards
int32_t op; // Operation from the compiled pattern, split into
int32_t opType; // the opcode
int32_t opValue; // and the operand value.
-
+
#ifdef REGEX_RUN_DEBUG
if (fTraceDebug)
{
c = '.';
}
REGEX_DUMP_DEBUG_PRINTF(("%c", c));
-
+
c = UTEXT_NEXT32(fPattern->fPattern);
}
printf("\n");
c = '.';
}
printf("%c", c);
-
+
c = UTEXT_NEXT32(fInputText);
}
printf("\n");
fprintf(stderr, "Heap Trouble\n");
}
#endif
-
+
op = (int32_t)pat[fp->fPatIdx];
opType = URX_TYPE(op);
opValue = URX_VAL(op);
}
#endif
fp->fPatIdx++;
-
+
switch (opType) {
int32_t stringLen = URX_VAL(op);
U_ASSERT(opType == URX_STRING_LEN);
U_ASSERT(stringLen >= 2);
-
+
const UChar *patternString = litText+stringStartIdx;
int32_t patternStringIndex = 0;
UTEXT_SETNATIVEINDEX(fInputText, fp->fInputIdx);
break;
}
}
-
+
if (success) {
fp->fInputIdx = UTEXT_GETNATIVEINDEX(fInputText);
} else {
fRequireEnd = TRUE;
break;
}
-
+
UTEXT_SETNATIVEINDEX(fInputText, fp->fInputIdx);
-
+
// If we are positioned just before a new-line that is located at the
// end of input, succeed.
UChar32 c = UTEXT_NEXT32(fInputText);
// At new-line at end of input. Success
fHitEnd = TRUE;
fRequireEnd = TRUE;
-
+
break;
}
}
// Check whether character just before the current pos is a new-line
// unless we are at the end of input
UTEXT_SETNATIVEINDEX(fInputText, fp->fInputIdx);
- UChar32 c = UTEXT_PREVIOUS32(fInputText);
- if ((fp->fInputIdx < fAnchorLimit) &&
+ UChar32 c = UTEXT_PREVIOUS32(fInputText);
+ if ((fp->fInputIdx < fAnchorLimit) &&
((c<=0x0d && c>=0x0a) || c==0x85 ||c==0x2028 || c==0x2029)) {
// It's a new-line. ^ is true. Success.
// TODO: what should be done with positions between a CR and LF?
break;
- case URX_BACKSLASH_X:
+ case URX_BACKSLASH_X:
// Match a Grapheme, as defined by Unicode TR 29.
// Differs slightly from Perl, which consumes combining marks independently
// of context.
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
break;
}
-
+
UTEXT_SETNATIVEINDEX(fInputText, fp->fInputIdx);
// Examine (and consume) the current char.
goto GC_Done;
GC_Control:
- // Most control chars stand alone (don't combine with combining chars),
+ // Most control chars stand alone (don't combine with combining chars),
// except for that CR/LF sequence is a single grapheme cluster.
if (c == 0x0d && fp->fInputIdx < fActiveLimit && UTEXT_CURRENT32(fInputText) == 0x0a) {
c = UTEXT_NEXT32(fInputText);
}
break;
}
-
+
break;
}
- UBool success = ((opValue & URX_NEG_SET) == URX_NEG_SET);
+ UBool success = ((opValue & URX_NEG_SET) == URX_NEG_SET);
opValue &= ~URX_NEG_SET;
U_ASSERT(opValue > 0 && opValue < URX_LAST_SET);
}
}
break;
-
+
case URX_STAT_SETREF_N:
{
- // Test input character for NOT being a member of one of
+ // Test input character for NOT being a member of one of
// the predefined sets (Word Characters, for example)
if (fp->fInputIdx >= fActiveLimit) {
fHitEnd = TRUE;
U_ASSERT(opValue > 0 && opValue < URX_LAST_SET);
UTEXT_SETNATIVEINDEX(fInputText, fp->fInputIdx);
-
+
UChar32 c = UTEXT_NEXT32(fInputText);
if (c < 256) {
Regex8BitSet *s8 = &fPattern->fStaticSets8[opValue];
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
}
break;
-
+
case URX_SETREF:
if (fp->fInputIdx >= fActiveLimit) {
break;
} else {
UTEXT_SETNATIVEINDEX(fInputText, fp->fInputIdx);
-
+
// There is input left. Pick up one char and test it for set membership.
UChar32 c = UTEXT_NEXT32(fInputText);
U_ASSERT(opValue > 0 && opValue < sets->size());
break;
}
}
-
+
// the character wasn't in the set.
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
}
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
break;
}
-
+
UTEXT_SETNATIVEINDEX(fInputText, fp->fInputIdx);
-
+
// There is input left. Advance over one char, unless we've hit end-of-line
UChar32 c = UTEXT_NEXT32(fInputText);
if (((c & 0x7f) <= 0x29) && // First quickly bypass as many chars as possible
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
break;
}
-
+
UTEXT_SETNATIVEINDEX(fInputText, fp->fInputIdx);
-
+
// There is input left. Advance over one char, except if we are
// at a cr/lf, advance over both of them.
- UChar32 c;
+ UChar32 c;
c = UTEXT_NEXT32(fInputText);
fp->fInputIdx = UTEXT_GETNATIVEINDEX(fInputText);
if (c==0x0d && fp->fInputIdx < fActiveLimit) {
}
UTEXT_SETNATIVEINDEX(fInputText, fp->fInputIdx);
-
+
// There is input left. Advance over one char, unless we've hit end-of-line
UChar32 c = UTEXT_NEXT32(fInputText);
if (c == 0x0a) {
fp = StateSave(fp, fp->fPatIdx, status); // State save to loc following current
fp->fPatIdx = opValue;
fp->fExtra[frameLoc] = fp->fInputIdx;
- }
+ }
// If the input position did not advance, we do nothing here,
// execution will fall out of the loop.
}
fp->fExtra[opValue] = 0; // Set the loop counter variable to zero
// Pick up the three extra operands that CTR_INIT has, and
- // skip the pattern location counter past
+ // skip the pattern location counter past
int32_t instrOperandLoc = (int32_t)fp->fPatIdx;
fp->fPatIdx += 3;
int32_t loopLoc = URX_VAL(pat[instrOperandLoc]);
fp->fExtra[opValue] = 0; // Set the loop counter variable to zero
// Pick up the three extra operands that CTR_INIT_NG has, and
- // skip the pattern location counter past
+ // skip the pattern location counter past
int32_t instrOperandLoc = (int32_t)fp->fPatIdx;
fp->fPatIdx += 3;
int32_t loopLoc = URX_VAL(pat[instrOperandLoc]);
fp = StateSave(fp, fp->fPatIdx, status);
}
fp->fPatIdx = loopLoc+1; // Continue with stuff after repeated block
- }
+ }
}
break;
UTEXT_SETNATIVEINDEX(fInputText, fp->fInputIdx);
// Note: if the capture group match was of an empty string the backref
- // match succeeds. Verified by testing: Perl matches succeed
+ // match succeeds. Verified by testing: Perl matches succeed
// in this case, so we do too.
-
+
UBool success = TRUE;
for (;;) {
if (utext_getNativeIndex(fAltInputText) >= groupEndIdx) {
CaseFoldingUTextIterator inputItr(*fInputText);
// Note: if the capture group match was of an empty string the backref
- // match succeeds. Verified by testing: Perl matches succeed
+ // match succeeds. Verified by testing: Perl matches succeed
// in this case, so we do too.
-
+
UBool success = TRUE;
for (;;) {
if (!captureGroupItr.inExpansion() && utext_getNativeIndex(fAltInputText) >= groupEndIdx) {
}
if (success && inputItr.inExpansion()) {
- // We otained a match by consuming part of a string obtained from
- // case-folding a single code point of the input text.
+ // We otained a match by consuming part of a string obtained from
+ // case-folding a single code point of the input text.
// This does not count as an overall match.
success = FALSE;
}
} else {
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
}
-
+
}
break;
-
+
case URX_STO_INP_LOC:
{
U_ASSERT(opValue >= 0 && opValue < fFrameSize);
} else {
fHitEnd = TRUE;
}
-
+
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
break;
opValue = URX_VAL(op);
U_ASSERT(opType == URX_STRING_LEN);
int32_t patternStringLen = opValue; // Length of the string from the pattern.
-
-
+
+
UChar32 cPattern;
UChar32 cText;
UBool success = TRUE;
}
// Look-behind match is good. Restore the orignal input string length,
- // which had been truncated to pin the end of the lookbehind match to the
+ // which had been truncated to pin the end of the lookbehind match to the
// position being looked-behind.
int64_t originalInputLen = fData[opValue+3];
U_ASSERT(originalInputLen >= fActiveLimit);
// Look-behind expression matched, which means look-behind test as
// a whole Fails
-
- // Restore the orignal input string length, which had been truncated
- // inorder to pin the end of the lookbehind match
+
+ // Restore the orignal input string length, which had been truncated
+ // inorder to pin the end of the lookbehind match
// to the position being looked-behind.
int64_t originalInputLen = fData[opValue+3];
U_ASSERT(originalInputLen >= fActiveLimit);
int32_t newStackSize = (int32_t)fData[opValue];
U_ASSERT(fStack->size() > newStackSize);
fStack->setSize(newStackSize);
-
- // FAIL, which will take control back to someplace
+
+ // FAIL, which will take control back to someplace
// prior to entering the look-behind test.
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
}
U_ASSERT(backSearchIndex <= fp->fInputIdx);
if (backSearchIndex == fp->fInputIdx) {
// We've backed up the input idx to the point that the loop started.
- // The loop is done. Leave here without saving state.
+ // The loop is done. Leave here without saving state.
// Subsequent failures won't come back here.
break;
}
UTEXT_SETNATIVEINDEX(fInputText, fp->fInputIdx);
UChar32 prevC = UTEXT_PREVIOUS32(fInputText);
fp->fInputIdx = UTEXT_GETNATIVEINDEX(fInputText);
-
+
UChar32 twoPrevC = UTEXT_PREVIOUS32(fInputText);
- if (prevC == 0x0a &&
+ if (prevC == 0x0a &&
fp->fInputIdx > backSearchIndex &&
twoPrevC == 0x0d) {
int32_t prevOp = (int32_t)pat[fp->fPatIdx-2];
break;
}
}
-
+
breakFromLoop:
fMatch = isMatch;
if (isMatch) {
//--------------------------------------------------------------------------------
void RegexMatcher::MatchChunkAt(int32_t startIdx, UBool toEnd, UErrorCode &status) {
UBool isMatch = FALSE; // True if the we have a match.
-
+
int32_t backSearchIndex = INT32_MAX; // used after greedy single-character matches for searching backwards
int32_t op; // Operation from the compiled pattern, split into
int32_t opType; // the opcode
int32_t opValue; // and the operand value.
-
+
#ifdef REGEX_RUN_DEBUG
if (fTraceDebug)
{
c = '.';
}
REGEX_DUMP_DEBUG_PRINTF(("%c", c));
-
+
c = UTEXT_NEXT32(fPattern->fPattern);
}
printf("\n");
c = '.';
}
printf("%c", c);
-
+
c = UTEXT_NEXT32(fInputText);
}
printf("\n");
printf("\n");
}
#endif
-
+
if (U_FAILURE(status)) {
return;
}
-
+
// Cache frequently referenced items from the compiled pattern
//
int64_t *pat = fPattern->fCompiledPat->getBuffer();
-
+
const UChar *litText = fPattern->fLiteralText.getBuffer();
UVector *sets = fPattern->fSets;
-
+
const UChar *inputBuf = fInputText->chunkContents;
-
+
fFrameSize = fPattern->fFrameSize;
REStackFrame *fp = resetStack();
-
+
fp->fPatIdx = 0;
fp->fInputIdx = startIdx;
-
+
// Zero out the pattern's static data
int32_t i;
for (i = 0; i<fPattern->fDataSize; i++) {
fData[i] = 0;
}
-
+
//
// Main loop for interpreting the compiled pattern.
// One iteration of the loop per pattern operation performed.
fprintf(stderr, "Heap Trouble\n");
}
#endif
-
+
op = (int32_t)pat[fp->fPatIdx];
opType = URX_TYPE(op);
opValue = URX_VAL(op);
}
#endif
fp->fPatIdx++;
-
+
switch (opType) {
-
-
+
+
case URX_NOP:
break;
-
-
+
+
case URX_BACKTRACK:
// Force a backtrack. In some circumstances, the pattern compiler
// will notice that the pattern can't possibly match anything, and will
// emit one of these at that point.
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
break;
-
-
+
+
case URX_ONECHAR:
if (fp->fInputIdx < fActiveLimit) {
UChar32 c;
}
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
break;
-
-
+
+
case URX_STRING:
{
// Test input against a literal string.
// offset to the string text, and one for the length.
int32_t stringStartIdx = opValue;
int32_t stringLen;
-
+
op = (int32_t)pat[fp->fPatIdx]; // Fetch the second operand
fp->fPatIdx++;
opType = URX_TYPE(op);
stringLen = URX_VAL(op);
U_ASSERT(opType == URX_STRING_LEN);
U_ASSERT(stringLen >= 2);
-
+
const UChar * pInp = inputBuf + fp->fInputIdx;
const UChar * pInpLimit = inputBuf + fActiveLimit;
const UChar * pPat = litText+stringStartIdx;
break;
}
}
-
+
if (success) {
fp->fInputIdx += stringLen;
} else {
}
}
break;
-
-
+
+
case URX_STATE_SAVE:
fp = StateSave(fp, opValue, status);
break;
-
-
+
+
case URX_END:
// The match loop will exit via this path on a successful match,
// when we reach the end of the pattern.
}
isMatch = TRUE;
goto breakFromLoop;
-
+
// Start and End Capture stack frame variables are laid out out like this:
// fp->fExtra[opValue] - The start of a completed capture group
// opValue+1 - The end of a completed capture group
U_ASSERT(opValue >= 0 && opValue < fFrameSize-3);
fp->fExtra[opValue+2] = fp->fInputIdx;
break;
-
-
+
+
case URX_END_CAPTURE:
U_ASSERT(opValue >= 0 && opValue < fFrameSize-3);
U_ASSERT(fp->fExtra[opValue+2] >= 0); // Start pos for this group must be set.
fp->fExtra[opValue+1] = fp->fInputIdx; // End position
U_ASSERT(fp->fExtra[opValue] <= fp->fExtra[opValue+1]);
break;
-
-
+
+
case URX_DOLLAR: // $, test for End of line
// or for position before new line at end of input
if (fp->fInputIdx < fAnchorLimit-2) {
fRequireEnd = TRUE;
break;
}
-
+
// If we are positioned just before a new-line that is located at the
// end of input, succeed.
if (fp->fInputIdx == fAnchorLimit-1) {
UChar32 c;
U16_GET(inputBuf, fAnchorStart, fp->fInputIdx, fAnchorLimit, c);
-
+
if ((c>=0x0a && c<=0x0d) || c==0x85 || c==0x2028 || c==0x2029) {
if ( !(c==0x0a && fp->fInputIdx>fAnchorStart && inputBuf[fp->fInputIdx-1]==0x0d)) {
// At new-line at end of input. Success
fRequireEnd = TRUE;
break; // At CR/LF at end of input. Success
}
-
+
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
-
+
break;
-
-
+
+
case URX_DOLLAR_D: // $, test for End of Line, in UNIX_LINES mode.
if (fp->fInputIdx >= fAnchorLimit-1) {
// Either at the last character of input, or off the end.
break;
}
}
-
+
// Not at end of input. Back-track out.
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
break;
-
-
+
+
case URX_DOLLAR_M: // $, test for End of line in multi-line mode
{
if (fp->fInputIdx >= fAnchorLimit) {
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
}
break;
-
-
+
+
case URX_DOLLAR_MD: // $, test for End of line in multi-line and UNIX_LINES mode
{
if (fp->fInputIdx >= fAnchorLimit) {
}
}
break;
-
-
+
+
case URX_CARET: // ^, test for start of line
if (fp->fInputIdx != fAnchorStart) {
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
}
break;
-
-
+
+
case URX_CARET_M: // ^, test for start of line in mulit-line mode
{
if (fp->fInputIdx == fAnchorStart) {
}
// Check whether character just before the current pos is a new-line
// unless we are at the end of input
- UChar c = inputBuf[fp->fInputIdx - 1];
- if ((fp->fInputIdx < fAnchorLimit) &&
+ UChar c = inputBuf[fp->fInputIdx - 1];
+ if ((fp->fInputIdx < fAnchorLimit) &&
((c<=0x0d && c>=0x0a) || c==0x85 ||c==0x2028 || c==0x2029)) {
// It's a new-line. ^ is true. Success.
// TODO: what should be done with positions between a CR and LF?
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
}
break;
-
-
+
+
case URX_CARET_M_UNIX: // ^, test for start of line in mulit-line + Unix-line mode
{
U_ASSERT(fp->fInputIdx >= fAnchorStart);
}
// Check whether character just before the current pos is a new-line
U_ASSERT(fp->fInputIdx <= fAnchorLimit);
- UChar c = inputBuf[fp->fInputIdx - 1];
+ UChar c = inputBuf[fp->fInputIdx - 1];
if (c != 0x0a) {
// Not at the start of a line. Back-track out.
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
}
}
break;
-
+
case URX_BACKSLASH_B: // Test for word boundaries
{
UBool success = isChunkWordBoundary((int32_t)fp->fInputIdx);
}
}
break;
-
-
+
+
case URX_BACKSLASH_BU: // Test for word boundaries, Unicode-style
{
UBool success = isUWordBoundary(fp->fInputIdx);
}
}
break;
-
-
+
+
case URX_BACKSLASH_D: // Test for decimal digit
{
if (fp->fInputIdx >= fActiveLimit) {
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
break;
}
-
+
UChar32 c;
U16_NEXT(inputBuf, fp->fInputIdx, fActiveLimit, c);
int8_t ctype = u_charType(c); // TODO: make a unicode set for this. Will be faster.
}
}
break;
-
-
+
+
case URX_BACKSLASH_G: // Test for position at end of previous match
if (!((fMatch && fp->fInputIdx==fMatchEnd) || (fMatch==FALSE && fp->fInputIdx==fActiveStart))) {
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
}
break;
-
-
- case URX_BACKSLASH_X:
+
+
+ case URX_BACKSLASH_X:
// Match a Grapheme, as defined by Unicode TR 29.
// Differs slightly from Perl, which consumes combining marks independently
// of context.
goto GC_Done;
GC_Control:
- // Most control chars stand alone (don't combine with combining chars),
+ // Most control chars stand alone (don't combine with combining chars),
// except for that CR/LF sequence is a single grapheme cluster.
if (c == 0x0d && fp->fInputIdx < fActiveLimit && inputBuf[fp->fInputIdx] == 0x0a) {
fp->fInputIdx++;
}
break;
}
-
-
-
-
+
+
+
+
case URX_BACKSLASH_Z: // Test for end of Input
if (fp->fInputIdx < fAnchorLimit) {
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
fRequireEnd = TRUE;
}
break;
-
-
-
+
+
+
case URX_STATIC_SETREF:
{
// Test input character against one of the predefined sets
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
break;
}
-
- UBool success = ((opValue & URX_NEG_SET) == URX_NEG_SET);
+
+ UBool success = ((opValue & URX_NEG_SET) == URX_NEG_SET);
opValue &= ~URX_NEG_SET;
U_ASSERT(opValue > 0 && opValue < URX_LAST_SET);
-
+
UChar32 c;
U16_NEXT(inputBuf, fp->fInputIdx, fActiveLimit, c);
if (c < 256) {
}
}
break;
-
-
+
+
case URX_STAT_SETREF_N:
{
- // Test input character for NOT being a member of one of
+ // Test input character for NOT being a member of one of
// the predefined sets (Word Characters, for example)
if (fp->fInputIdx >= fActiveLimit) {
fHitEnd = TRUE;
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
break;
}
-
+
U_ASSERT(opValue > 0 && opValue < URX_LAST_SET);
-
+
UChar32 c;
U16_NEXT(inputBuf, fp->fInputIdx, fActiveLimit, c);
if (c < 256) {
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
}
break;
-
-
+
+
case URX_SETREF:
{
if (fp->fInputIdx >= fActiveLimit) {
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
break;
}
-
+
U_ASSERT(opValue > 0 && opValue < sets->size());
// There is input left. Pick up one char and test it for set membership.
break;
}
}
-
+
// the character wasn't in the set.
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
}
break;
-
-
+
+
case URX_DOTANY:
{
// . matches anything, but stops at end-of-line.
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
break;
}
-
+
// There is input left. Advance over one char, unless we've hit end-of-line
UChar32 c;
U16_NEXT(inputBuf, fp->fInputIdx, fActiveLimit, c);
}
}
break;
-
-
+
+
case URX_DOTANY_ALL:
{
// . in dot-matches-all (including new lines) mode
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
break;
}
-
+
// There is input left. Advance over one char, except if we are
// at a cr/lf, advance over both of them.
- UChar32 c;
+ UChar32 c;
U16_NEXT(inputBuf, fp->fInputIdx, fActiveLimit, c);
if (c==0x0d && fp->fInputIdx < fActiveLimit) {
// In the case of a CR/LF, we need to advance over both.
}
}
break;
-
-
+
+
case URX_DOTANY_UNIX:
{
// '.' operator, matches all, but stops at end-of-line.
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
break;
}
-
+
// There is input left. Advance over one char, unless we've hit end-of-line
- UChar32 c;
+ UChar32 c;
U16_NEXT(inputBuf, fp->fInputIdx, fActiveLimit, c);
if (c == 0x0a) {
// End of line in normal mode. '.' does not match the \n
}
}
break;
-
-
+
+
case URX_JMP:
fp->fPatIdx = opValue;
break;
-
+
case URX_FAIL:
isMatch = FALSE;
goto breakFromLoop;
-
+
case URX_JMP_SAV:
U_ASSERT(opValue < fPattern->fCompiledPat->size());
fp = StateSave(fp, fp->fPatIdx, status); // State save to loc following current
fp->fPatIdx = opValue; // Then JMP.
break;
-
+
case URX_JMP_SAV_X:
// This opcode is used with (x)+, when x can match a zero length string.
// Same as JMP_SAV, except conditional on the match having made forward progress.
fp = StateSave(fp, fp->fPatIdx, status); // State save to loc following current
fp->fPatIdx = opValue;
fp->fExtra[frameLoc] = fp->fInputIdx;
- }
+ }
// If the input position did not advance, we do nothing here,
// execution will fall out of the loop.
}
break;
-
+
case URX_CTR_INIT:
{
U_ASSERT(opValue >= 0 && opValue < fFrameSize-2);
fp->fExtra[opValue] = 0; // Set the loop counter variable to zero
-
+
// Pick up the three extra operands that CTR_INIT has, and
- // skip the pattern location counter past
+ // skip the pattern location counter past
int32_t instrOperandLoc = (int32_t)fp->fPatIdx;
fp->fPatIdx += 3;
int32_t loopLoc = URX_VAL(pat[instrOperandLoc]);
U_ASSERT(minCount>=0);
U_ASSERT(maxCount>=minCount || maxCount==-1);
U_ASSERT(loopLoc>=fp->fPatIdx);
-
+
if (minCount == 0) {
fp = StateSave(fp, loopLoc+1, status);
}
}
}
break;
-
+
case URX_CTR_LOOP:
{
U_ASSERT(opValue>0 && opValue < fp->fPatIdx-2);
fp->fPatIdx = opValue + 4; // Loop back.
}
break;
-
+
case URX_CTR_INIT_NG:
{
// Initialize a non-greedy loop
U_ASSERT(opValue >= 0 && opValue < fFrameSize-2);
fp->fExtra[opValue] = 0; // Set the loop counter variable to zero
-
+
// Pick up the three extra operands that CTR_INIT_NG has, and
- // skip the pattern location counter past
+ // skip the pattern location counter past
int32_t instrOperandLoc = (int32_t)fp->fPatIdx;
fp->fPatIdx += 3;
int32_t loopLoc = URX_VAL(pat[instrOperandLoc]);
if (maxCount == -1) {
fp->fExtra[opValue+1] = fp->fInputIdx; // Save initial input index for loop breaking.
}
-
+
if (minCount == 0) {
if (maxCount != 0) {
fp = StateSave(fp, fp->fPatIdx, status);
}
fp->fPatIdx = loopLoc+1; // Continue with stuff after repeated block
- }
+ }
}
break;
-
+
case URX_CTR_LOOP_NG:
{
// Non-greedy {min, max} loops
U_ASSERT(*pCounter == maxCount);
break;
}
-
+
if (*pCounter < minCount) {
// We haven't met the minimum number of matches yet.
// Loop back for another one.
}
}
break;
-
+
case URX_STO_SP:
U_ASSERT(opValue >= 0 && opValue < fPattern->fDataSize);
fData[opValue] = fStack->size();
break;
-
+
case URX_LD_SP:
{
U_ASSERT(opValue >= 0 && opValue < fPattern->fDataSize);
fStack->setSize(newStackSize);
}
break;
-
+
case URX_BACKREF:
{
U_ASSERT(opValue < fFrameSize);
}
}
break;
-
+
case URX_BACKREF_I:
{
U_ASSERT(opValue < fFrameSize);
CaseFoldingUCharIterator inputItr(inputBuf, fp->fInputIdx, fActiveLimit);
// Note: if the capture group match was of an empty string the backref
- // match succeeds. Verified by testing: Perl matches succeed
+ // match succeeds. Verified by testing: Perl matches succeed
// in this case, so we do too.
-
+
UBool success = TRUE;
for (;;) {
UChar32 captureGroupChar = captureGroupItr.next();
}
if (success && inputItr.inExpansion()) {
- // We otained a match by consuming part of a string obtained from
- // case-folding a single code point of the input text.
+ // We otained a match by consuming part of a string obtained from
+ // case-folding a single code point of the input text.
// This does not count as an overall match.
success = FALSE;
}
fp->fExtra[opValue] = fp->fInputIdx;
}
break;
-
+
case URX_JMPX:
{
int32_t instrOperandLoc = (int32_t)fp->fPatIdx;
}
}
break;
-
+
case URX_LA_START:
{
// Entering a lookahead block.
fActiveLimit = fLookLimit; // transparent bounds.
}
break;
-
+
case URX_LA_END:
{
// Leaving a look-ahead block.
fStack->setSize(newStackSize);
}
fp->fInputIdx = fData[opValue+1];
-
+
// Restore the active region bounds in the input string; they may have
// been changed because of transparent bounds on a Region.
fActiveStart = fRegionStart;
fActiveLimit = fRegionLimit;
}
break;
-
+
case URX_ONECHAR_I:
if (fp->fInputIdx < fActiveLimit) {
- UChar32 c;
+ UChar32 c;
U16_NEXT(inputBuf, fp->fInputIdx, fActiveLimit, c);
if (u_foldCase(c, U_FOLD_CASE_DEFAULT) == opValue) {
break;
}
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
break;
-
+
case URX_STRING_I:
// Case-insensitive test input against a literal string.
// Strings require two slots in the compiled pattern, one for the
opValue = URX_VAL(op);
U_ASSERT(opType == URX_STRING_LEN);
int32_t patternStringLen = opValue; // Length of the string from the pattern.
-
+
UChar32 cText;
UChar32 cPattern;
UBool success = TRUE;
fActiveLimit = fp->fInputIdx;
}
break;
-
-
+
+
case URX_LB_CONT:
{
// Positive Look-Behind, at top of loop checking for matches of LB expression
// at all possible input starting positions.
-
+
// Fetch the min and max possible match lengths. They are the operands
// of this op in the pattern.
int32_t minML = (int32_t)pat[fp->fPatIdx++];
int32_t maxML = (int32_t)pat[fp->fPatIdx++];
U_ASSERT(minML <= maxML);
U_ASSERT(minML >= 0);
-
+
// Fetch (from data) the last input index where a match was attempted.
U_ASSERT(opValue>=0 && opValue+1<fPattern->fDataSize);
int64_t *lbStartIdx = &fData[opValue+2];
U16_BACK_1(inputBuf, 0, *lbStartIdx);
}
}
-
+
if (*lbStartIdx < 0 || *lbStartIdx < fp->fInputIdx - maxML) {
// We have tried all potential match starting points without
// getting a match. Backtrack out, and out of the
fActiveLimit = restoreInputLen;
break;
}
-
+
// Save state to this URX_LB_CONT op, so failure to match will repeat the loop.
// (successful match will fall off the end of the loop.)
fp = StateSave(fp, fp->fPatIdx-3, status);
fp->fInputIdx = *lbStartIdx;
}
break;
-
+
case URX_LB_END:
// End of a look-behind block, after a successful match.
{
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
break;
}
-
+
// Look-behind match is good. Restore the orignal input string length,
- // which had been truncated to pin the end of the lookbehind match to the
+ // which had been truncated to pin the end of the lookbehind match to the
// position being looked-behind.
int64_t originalInputLen = fData[opValue+3];
U_ASSERT(originalInputLen >= fActiveLimit);
fActiveLimit = originalInputLen;
}
break;
-
-
+
+
case URX_LBN_CONT:
{
// Negative Look-Behind, at top of loop checking for matches of LB expression
// at all possible input starting positions.
-
+
// Fetch the extra parameters of this op.
int32_t minML = (int32_t)pat[fp->fPatIdx++];
int32_t maxML = (int32_t)pat[fp->fPatIdx++];
U_ASSERT(minML <= maxML);
U_ASSERT(minML >= 0);
U_ASSERT(continueLoc > fp->fPatIdx);
-
+
// Fetch (from data) the last input index where a match was attempted.
U_ASSERT(opValue>=0 && opValue+1<fPattern->fDataSize);
int64_t *lbStartIdx = &fData[opValue+2];
U16_BACK_1(inputBuf, 0, *lbStartIdx);
}
}
-
+
if (*lbStartIdx < 0 || *lbStartIdx < fp->fInputIdx - maxML) {
// We have tried all potential match starting points without
// getting a match, which means that the negative lookbehind as
fp->fPatIdx = continueLoc;
break;
}
-
+
// Save state to this URX_LB_CONT op, so failure to match will repeat the loop.
// (successful match will cause a FAIL out of the loop altogether.)
fp = StateSave(fp, fp->fPatIdx-4, status);
fp->fInputIdx = *lbStartIdx;
}
break;
-
+
case URX_LBN_END:
// End of a negative look-behind block, after a successful match.
{
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
break;
}
-
+
// Look-behind expression matched, which means look-behind test as
// a whole Fails
-
- // Restore the orignal input string length, which had been truncated
- // inorder to pin the end of the lookbehind match
+
+ // Restore the orignal input string length, which had been truncated
+ // inorder to pin the end of the lookbehind match
// to the position being looked-behind.
int64_t originalInputLen = fData[opValue+3];
U_ASSERT(originalInputLen >= fActiveLimit);
U_ASSERT(originalInputLen <= fInputLength);
fActiveLimit = originalInputLen;
-
+
// Restore original stack position, discarding any state saved
// by the successful pattern match.
U_ASSERT(opValue>=0 && opValue+1<fPattern->fDataSize);
int32_t newStackSize = (int32_t)fData[opValue];
U_ASSERT(fStack->size() > newStackSize);
fStack->setSize(newStackSize);
-
- // FAIL, which will take control back to someplace
+
+ // FAIL, which will take control back to someplace
// prior to entering the look-behind test.
fp = (REStackFrame *)fStack->popFrame(fFrameSize);
}
break;
-
-
+
+
case URX_LOOP_SR_I:
// Loop Initialization for the optimized implementation of
// [some character set]*
U_ASSERT(opValue > 0 && opValue < sets->size());
Regex8BitSet *s8 = &fPattern->fSets8[opValue];
UnicodeSet *s = (UnicodeSet *)sets->elementAt(opValue);
-
+
// Loop through input, until either the input is exhausted or
// we reach a character that is not a member of the set.
int32_t ix = (int32_t)fp->fInputIdx;
}
}
}
-
+
// If there were no matching characters, skip over the loop altogether.
// The loop doesn't run at all, a * op always succeeds.
if (ix == fp->fInputIdx) {
fp->fPatIdx++; // skip the URX_LOOP_C op.
break;
}
-
+
// Peek ahead in the compiled pattern, to the URX_LOOP_C that
// must follow. It's operand is the stack location
// that holds the starting input index for the match of this [set]*
U_ASSERT(stackLoc >= 0 && stackLoc < fFrameSize);
fp->fExtra[stackLoc] = fp->fInputIdx;
fp->fInputIdx = ix;
-
+
// Save State to the URX_LOOP_C op that follows this one,
// so that match failures in the following code will return to there.
// Then bump the pattern idx so the LOOP_C is skipped on the way out of here.
fp->fPatIdx++;
}
break;
-
-
+
+
case URX_LOOP_DOT_I:
// Loop Initialization for the optimized implementation of .*
// This op scans through all remaining input.
}
}
}
-
+
// If there were no matching characters, skip over the loop altogether.
// The loop doesn't run at all, a * op always succeeds.
if (ix == fp->fInputIdx) {
fp->fPatIdx++; // skip the URX_LOOP_C op.
break;
}
-
+
// Peek ahead in the compiled pattern, to the URX_LOOP_C that
// must follow. It's operand is the stack location
// that holds the starting input index for the match of this .*
U_ASSERT(stackLoc >= 0 && stackLoc < fFrameSize);
fp->fExtra[stackLoc] = fp->fInputIdx;
fp->fInputIdx = ix;
-
+
// Save State to the URX_LOOP_C op that follows this one,
// so that match failures in the following code will return to there.
// Then bump the pattern idx so the LOOP_C is skipped on the way out of here.
fp->fPatIdx++;
}
break;
-
-
+
+
case URX_LOOP_C:
{
U_ASSERT(opValue>=0 && opValue<fFrameSize);
U_ASSERT(backSearchIndex <= fp->fInputIdx);
if (backSearchIndex == fp->fInputIdx) {
// We've backed up the input idx to the point that the loop started.
- // The loop is done. Leave here without saving state.
+ // The loop is done. Leave here without saving state.
// Subsequent failures won't come back here.
break;
}
U_ASSERT(fp->fInputIdx > 0);
UChar32 prevC;
U16_PREV(inputBuf, 0, fp->fInputIdx, prevC); // !!!: should this 0 be one of f*Limit?
-
- if (prevC == 0x0a &&
+
+ if (prevC == 0x0a &&
fp->fInputIdx > backSearchIndex &&
inputBuf[fp->fInputIdx-1] == 0x0d) {
int32_t prevOp = (int32_t)pat[fp->fPatIdx-2];
U16_BACK_1(inputBuf, 0, fp->fInputIdx);
}
}
-
-
+
+
fp = StateSave(fp, fp->fPatIdx-1, status);
}
break;
-
-
-
+
+
+
default:
// Trouble. The compiled pattern contains an entry with an
// unrecognized type tag.
U_ASSERT(FALSE);
}
-
+
if (U_FAILURE(status)) {
isMatch = FALSE;
break;
}
}
-
+
breakFromLoop:
fMatch = isMatch;
if (isMatch) {
REGEX_RUN_DEBUG_PRINTF(("No match\n\n"));
}
}
-
+
fFrame = fp; // The active stack frame when the engine stopped.
// Contains the capture group results that we need to
// access later.