#include "unicode/utf16.h"
#include "uassert.h"
#include "cmemory.h"
+#include "cstr.h"
#include "uvector.h"
#include "uvectr32.h"
#include "uvectr64.h"
// #include <malloc.h> // Needed for heapcheck testing
+
U_NAMESPACE_BEGIN
// Default limit for the size of the back track stack, to avoid system
if (fMatch) {
return TRUE;
}
- UTEXT_SETNATIVEINDEX(fInputText, pos);
+ UTEXT_SETNATIVEINDEX(fInputText, startPos);
}
if (startPos > testStartLimit) {
fMatch = FALSE;
return (REStackFrame *)newFP;
}
+#if defined(REGEX_DEBUG)
+namespace {
+UnicodeString StringFromUText(UText *ut) {
+ UnicodeString result;
+ for (UChar32 c = utext_next32From(ut, 0); c != U_SENTINEL; c = UTEXT_NEXT32(ut)) {
+ result.append(c);
+ }
+ return result;
+}
+}
+#endif // REGEX_DEBUG
+
//--------------------------------------------------------------------------------
//
int32_t opValue; // and the operand value.
#ifdef REGEX_RUN_DEBUG
- if (fTraceDebug)
- {
+ if (fTraceDebug) {
printf("MatchAt(startIdx=%ld)\n", startIdx);
- printf("Original Pattern: ");
- UChar32 c = utext_next32From(fPattern->fPattern, 0);
- while (c != U_SENTINEL) {
- if (c<32 || c>256) {
- c = '.';
- }
- printf("%c", c);
-
- c = UTEXT_NEXT32(fPattern->fPattern);
- }
- printf("\n");
- printf("Input String: ");
- c = utext_next32From(fInputText, 0);
- while (c != U_SENTINEL) {
- if (c<32 || c>256) {
- c = '.';
- }
- printf("%c", c);
-
- c = UTEXT_NEXT32(fInputText);
- }
- printf("\n");
- printf("\n");
+ printf("Original Pattern: \"%s\"\n", CStr(StringFromUText(fPattern->fPattern))());
+ printf("Input String: \"%s\"\n\n", CStr(StringFromUText(fInputText))());
}
#endif
// of this op in the pattern.
int32_t minML = (int32_t)pat[fp->fPatIdx++];
int32_t maxML = (int32_t)pat[fp->fPatIdx++];
+ if (!UTEXT_USES_U16(fInputText)) {
+ // utf-8 fix to maximum match length. The pattern compiler assumes utf-16.
+ // The max length need not be exact; it just needs to be >= actual maximum.
+ maxML *= 3;
+ }
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];
- if (*lbStartIdx < 0) {
+ int64_t &lbStartIdx = fData[opValue+2];
+ if (lbStartIdx < 0) {
// First time through loop.
- *lbStartIdx = fp->fInputIdx - minML;
+ lbStartIdx = fp->fInputIdx - minML;
+ if (lbStartIdx > 0) {
+ // move index to a code point boudary, if it's not on one already.
+ UTEXT_SETNATIVEINDEX(fInputText, lbStartIdx);
+ lbStartIdx = UTEXT_GETNATIVEINDEX(fInputText);
+ }
} else {
// 2nd through nth time through the loop.
// Back up start position for match by one.
- if (*lbStartIdx == 0) {
- (*lbStartIdx)--;
+ if (lbStartIdx == 0) {
+ (lbStartIdx)--;
} else {
- UTEXT_SETNATIVEINDEX(fInputText, *lbStartIdx);
+ UTEXT_SETNATIVEINDEX(fInputText, lbStartIdx);
(void)UTEXT_PREVIOUS32(fInputText);
- *lbStartIdx = UTEXT_GETNATIVEINDEX(fInputText);
+ lbStartIdx = UTEXT_GETNATIVEINDEX(fInputText);
}
}
- if (*lbStartIdx < 0 || *lbStartIdx < fp->fInputIdx - maxML) {
+ 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
// Look Behind altogether.
// 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;
+ fp->fInputIdx = lbStartIdx;
}
break;
// Fetch the extra parameters of this op.
int32_t minML = (int32_t)pat[fp->fPatIdx++];
int32_t maxML = (int32_t)pat[fp->fPatIdx++];
+ if (!UTEXT_USES_U16(fInputText)) {
+ // utf-8 fix to maximum match length. The pattern compiler assumes utf-16.
+ // The max length need not be exact; it just needs to be >= actual maximum.
+ maxML *= 3;
+ }
int32_t continueLoc = (int32_t)pat[fp->fPatIdx++];
continueLoc = URX_VAL(continueLoc);
U_ASSERT(minML <= maxML);
// 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];
- if (*lbStartIdx < 0) {
+ int64_t &lbStartIdx = fData[opValue+2];
+ if (lbStartIdx < 0) {
// First time through loop.
- *lbStartIdx = fp->fInputIdx - minML;
+ lbStartIdx = fp->fInputIdx - minML;
+ if (lbStartIdx > 0) {
+ // move index to a code point boudary, if it's not on one already.
+ UTEXT_SETNATIVEINDEX(fInputText, lbStartIdx);
+ lbStartIdx = UTEXT_GETNATIVEINDEX(fInputText);
+ }
} else {
// 2nd through nth time through the loop.
// Back up start position for match by one.
- if (*lbStartIdx == 0) {
- (*lbStartIdx)--;
+ if (lbStartIdx == 0) {
+ (lbStartIdx)--;
} else {
- UTEXT_SETNATIVEINDEX(fInputText, *lbStartIdx);
+ UTEXT_SETNATIVEINDEX(fInputText, lbStartIdx);
(void)UTEXT_PREVIOUS32(fInputText);
- *lbStartIdx = UTEXT_GETNATIVEINDEX(fInputText);
+ lbStartIdx = UTEXT_GETNATIVEINDEX(fInputText);
}
}
- if (*lbStartIdx < 0 || *lbStartIdx < fp->fInputIdx - maxML) {
+ 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
// a whole has succeeded. Jump forward to the continue location
// 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;
+ fp->fInputIdx = lbStartIdx;
}
break;
#ifdef REGEX_RUN_DEBUG
if (fTraceDebug) {
printf("MatchAt(startIdx=%d)\n", startIdx);
- printf("Original Pattern: ");
- UChar32 c = utext_next32From(fPattern->fPattern, 0);
- while (c != U_SENTINEL) {
- if (c<32 || c>256) {
- c = '.';
- }
- printf("%c", c);
-
- c = UTEXT_NEXT32(fPattern->fPattern);
- }
- printf("\n");
- printf("Input String: ");
- c = utext_next32From(fInputText, 0);
- while (c != U_SENTINEL) {
- if (c<32 || c>256) {
- c = '.';
- }
- printf("%c", c);
-
- c = UTEXT_NEXT32(fInputText);
- }
- printf("\n");
- printf("\n");
+ printf("Original Pattern: \"%s\"\n", CStr(StringFromUText(fPattern->fPattern))());
+ printf("Input String: \"%s\"\n\n", CStr(StringFromUText(fInputText))());
}
#endif
// 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];
- if (*lbStartIdx < 0) {
+ int64_t &lbStartIdx = fData[opValue+2];
+ if (lbStartIdx < 0) {
// First time through loop.
- *lbStartIdx = fp->fInputIdx - minML;
+ lbStartIdx = fp->fInputIdx - minML;
+ if (lbStartIdx > 0) {
+ U16_SET_CP_START(inputBuf, 0, lbStartIdx);
+ }
} else {
// 2nd through nth time through the loop.
// Back up start position for match by one.
- if (*lbStartIdx == 0) {
- (*lbStartIdx)--;
+ if (lbStartIdx == 0) {
+ lbStartIdx--;
} else {
- U16_BACK_1(inputBuf, 0, *lbStartIdx);
+ U16_BACK_1(inputBuf, 0, lbStartIdx);
}
}
- if (*lbStartIdx < 0 || *lbStartIdx < fp->fInputIdx - maxML) {
+ 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
// Look Behind altogether.
// 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;
+ fp->fInputIdx = lbStartIdx;
}
break;
// 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];
- if (*lbStartIdx < 0) {
+ int64_t &lbStartIdx = fData[opValue+2];
+ if (lbStartIdx < 0) {
// First time through loop.
- *lbStartIdx = fp->fInputIdx - minML;
+ lbStartIdx = fp->fInputIdx - minML;
+ if (lbStartIdx > 0) {
+ U16_SET_CP_START(inputBuf, 0, lbStartIdx);
+ }
} else {
// 2nd through nth time through the loop.
// Back up start position for match by one.
- if (*lbStartIdx == 0) {
- (*lbStartIdx)--; // Because U16_BACK is unsafe starting at 0.
+ if (lbStartIdx == 0) {
+ lbStartIdx--; // Because U16_BACK is unsafe starting at 0.
} else {
- U16_BACK_1(inputBuf, 0, *lbStartIdx);
+ U16_BACK_1(inputBuf, 0, lbStartIdx);
}
}
- if (*lbStartIdx < 0 || *lbStartIdx < fp->fInputIdx - maxML) {
+ 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
// a whole has succeeded. Jump forward to the continue location
// 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;
+ fp->fInputIdx = lbStartIdx;
}
break;
#include "unicode/regex.h"
#include "unicode/uclean.h"
+#include "cstr.h"
#include "uassert.h"
#include "uhash.h"
#include "uvector.h"
}
-
//---------------------------------------------------------------------
//
// dump Output the compiled form of the pattern.
case URX_ONECHAR:
case URX_ONECHAR_I:
- printf("%c", val<256?val:'?');
+ if (val < 0x20) {
+ printf("%#x", val);
+ } else {
+ printf("'%s'", CStr(UnicodeString(val))());
+ }
break;
case URX_STRING:
int32_t lengthOp = fCompiledPat->elementAti(index+1);
U_ASSERT(URX_TYPE(lengthOp) == URX_STRING_LEN);
int32_t length = URX_VAL(lengthOp);
- int32_t i;
- for (i=val; i<val+length; i++) {
- UChar c = fLiteralText[i];
- if (c < 32 || c >= 256) {c = '.';}
- printf("%c", c);
- }
+ UnicodeString str(fLiteralText, val, length);
+ printf("%s", CStr(str)());
}
break;
UnicodeString s;
UnicodeSet *set = (UnicodeSet *)fSets->elementAt(val);
set->toPattern(s, TRUE);
- for (int32_t i=0; i<s.length(); i++) {
- printf("%c", s.charAt(i));
- }
+ printf("%s", CStr(s)());
}
break;
}
UnicodeSet *set = fStaticSets[val];
set->toPattern(s, TRUE);
- for (int32_t i=0; i<s.length(); i++) {
- printf("%c", s.charAt(i));
- }
+ printf("%s", CStr(s)());
}
break;
void RegexPattern::dumpPattern() const {
#if defined(REGEX_DEBUG)
- // TODO: This function assumes an ASCII based charset.
int index;
- int i;
-
- printf("Original Pattern: ");
- UChar32 c = utext_next32From(fPattern, 0);
- while (c != U_SENTINEL) {
- if (c<32 || c>256) {
- c = '.';
- }
- printf("%c", c);
- c = UTEXT_NEXT32(fPattern);
+ UnicodeString patStr;
+ for (UChar32 c = utext_next32From(fPattern, 0); c != U_SENTINEL; c = utext_next32(fPattern)) {
+ patStr.append(c);
}
- printf("\n");
+ printf("Original Pattern: \"%s\"\n", CStr(patStr)());
printf(" Min Match Length: %d\n", fMinMatchLen);
printf(" Match Start Type: %s\n", START_OF_MATCH_STR(fStartType));
if (fStartType == START_STRING) {
- printf(" Initial match string: \"");
- for (i=fInitialStringIdx; i<fInitialStringIdx+fInitialStringLen; i++) {
- printf("%c", fLiteralText[i]); // TODO: non-printables, surrogates.
- }
- printf("\"\n");
-
+ UnicodeString initialString(fLiteralText,fInitialStringIdx, fInitialStringLen);
+ printf(" Initial match string: \"%s\"\n", CStr(initialString)());
} else if (fStartType == START_SET) {
- int32_t numSetChars = fInitialChars->size();
- if (numSetChars > 20) {
- numSetChars = 20;
- }
- printf(" Match First Chars : ");
- for (i=0; i<numSetChars; i++) {
- UChar32 c = fInitialChars->charAt(i);
- if (0x20<c && c <0x7e) {
- printf("%c ", c);
- } else {
- printf("%#x ", c);
- }
- }
- if (numSetChars < fInitialChars->size()) {
- printf(" ...");
- }
- printf("\n");
+ UnicodeString s;
+ fInitialChars->toPattern(s, TRUE);
+ printf(" Match First Chars: %s\n", CStr(s)());
} else if (fStartType == START_CHAR) {
- printf(" First char of Match : ");
- if (0x20 < fInitialChar && fInitialChar<0x7e) {
- printf("%c\n", fInitialChar);
+ printf(" First char of Match: ");
+ if (fInitialChar > 0x20) {
+ printf("'%s'\n", CStr(UnicodeString(fInitialChar))());
} else {
printf("%#x\n", fInitialChar);
}
const UHashElement *el = NULL;
while ((el = uhash_nextElement(fNamedCaptureMap, &pos))) {
const UnicodeString *name = (const UnicodeString *)el->key.pointer;
- char s[100];
- name->extract(0, 99, s, sizeof(s), US_INV); // capture group names are invariant.
int32_t number = el->value.integer;
- printf(" %d\t%s\n", number, s);
+ printf(" %d\t%s\n", number, CStr(*name)());
}
}