// #include <malloc.h> // Needed for heapcheck testing
-
-// Find progress callback
-// ----------------------
-// Macro to inline test & call to ReportFindProgress(). Eliminates unnecessary function call.
-//
-#define REGEXFINDPROGRESS_INTERRUPT(pos, status) \
- (fFindProgressCallbackFn != NULL) && (ReportFindProgress(pos, status) == FALSE)
-
-
-// Smart Backtracking
-// ------------------
-// When a failure would go back to a LOOP_C instruction,
-// strings, characters, and setrefs scan backwards for a valid start
-// character themselves, pop the stack, and save state, emulating the
-// LOOP_C's effect but assured that the next character of input is a
-// possible matching character.
-//
-// Good idea in theory; unfortunately it only helps out a few specific
-// cases and slows the engine down a little in the rest.
-
U_NAMESPACE_BEGIN
// Default limit for the size of the back track stack, to avoid system
//
//--------------------------------------------------------------------------------
UBool RegexMatcher::find() {
+ if (U_FAILURE(fDeferredStatus)) {
+ return FALSE;
+ }
+ UErrorCode status = U_ZERO_ERROR;
+ UBool result = find(status);
+ return result;
+}
+
+//--------------------------------------------------------------------------------
+//
+// find()
+//
+//--------------------------------------------------------------------------------
+UBool RegexMatcher::find(UErrorCode &status) {
// Start at the position of the last match end. (Will be zero if the
// matcher has been reset.)
//
+ if (U_FAILURE(status)) {
+ return FALSE;
+ }
if (U_FAILURE(fDeferredStatus)) {
+ status = fDeferredStatus;
return FALSE;
}
if (UTEXT_FULL_TEXT_IN_CHUNK(fInputText, fInputLength)) {
- return findUsingChunk();
+ return findUsingChunk(status);
}
int64_t startPos = fMatchEnd;
// No optimization was found.
// Try a match at each input position.
for (;;) {
- MatchAt(startPos, FALSE, fDeferredStatus);
- if (U_FAILURE(fDeferredStatus)) {
+ MatchAt(startPos, FALSE, status);
+ if (U_FAILURE(status)) {
return FALSE;
}
if (fMatch) {
// Note that it's perfectly OK for a pattern to have a zero-length
// match at the end of a string, so we must make sure that the loop
// runs with startPos == testStartLimit the last time through.
- if (REGEXFINDPROGRESS_INTERRUPT(startPos, fDeferredStatus))
+ if (findProgressInterrupt(startPos, status))
return FALSE;
}
U_ASSERT(FALSE);
fMatch = FALSE;
return FALSE;
}
- MatchAt(startPos, FALSE, fDeferredStatus);
- if (U_FAILURE(fDeferredStatus)) {
+ MatchAt(startPos, FALSE, status);
+ if (U_FAILURE(status)) {
return FALSE;
}
return fMatch;
// and handle end of text in the following block.
if (c >= 0 && ((c<256 && fPattern->fInitialChars8->contains(c)) ||
(c>=256 && fPattern->fInitialChars->contains(c)))) {
- MatchAt(pos, FALSE, fDeferredStatus);
- if (U_FAILURE(fDeferredStatus)) {
+ MatchAt(pos, FALSE, status);
+ if (U_FAILURE(status)) {
return FALSE;
}
if (fMatch) {
fHitEnd = TRUE;
return FALSE;
}
- if (REGEXFINDPROGRESS_INTERRUPT(startPos, fDeferredStatus))
+ if (findProgressInterrupt(startPos, status))
return FALSE;
}
}
c = UTEXT_NEXT32(fInputText);
startPos = UTEXT_GETNATIVEINDEX(fInputText);
if (c == theChar) {
- MatchAt(pos, FALSE, fDeferredStatus);
- if (U_FAILURE(fDeferredStatus)) {
+ MatchAt(pos, FALSE, status);
+ if (U_FAILURE(status)) {
return FALSE;
}
if (fMatch) {
fHitEnd = TRUE;
return FALSE;
}
- if (REGEXFINDPROGRESS_INTERRUPT(startPos, fDeferredStatus))
+ if (findProgressInterrupt(startPos, status))
return FALSE;
}
}
{
UChar32 c;
if (startPos == fAnchorStart) {
- MatchAt(startPos, FALSE, fDeferredStatus);
- if (U_FAILURE(fDeferredStatus)) {
+ MatchAt(startPos, FALSE, status);
+ if (U_FAILURE(status)) {
return FALSE;
}
if (fMatch) {
if (fPattern->fFlags & UREGEX_UNIX_LINES) {
for (;;) {
if (c == 0x0a) {
- MatchAt(startPos, FALSE, fDeferredStatus);
- if (U_FAILURE(fDeferredStatus)) {
+ MatchAt(startPos, FALSE, status);
+ if (U_FAILURE(status)) {
return FALSE;
}
if (fMatch) {
// Note that it's perfectly OK for a pattern to have a zero-length
// match at the end of a string, so we must make sure that the loop
// runs with startPos == testStartLimit the last time through.
- if (REGEXFINDPROGRESS_INTERRUPT(startPos, fDeferredStatus))
+ if (findProgressInterrupt(startPos, status))
return FALSE;
}
} else {
(void)UTEXT_NEXT32(fInputText);
startPos = UTEXT_GETNATIVEINDEX(fInputText);
}
- MatchAt(startPos, FALSE, fDeferredStatus);
- if (U_FAILURE(fDeferredStatus)) {
+ MatchAt(startPos, FALSE, status);
+ if (U_FAILURE(status)) {
return FALSE;
}
if (fMatch) {
// Note that it's perfectly OK for a pattern to have a zero-length
// match at the end of a string, so we must make sure that the loop
// runs with startPos == testStartLimit the last time through.
- if (REGEXFINDPROGRESS_INTERRUPT(startPos, fDeferredStatus))
+ if (findProgressInterrupt(startPos, status))
return FALSE;
}
}
return FALSE;
}
fMatchEnd = nativeStart;
- return find();
+ return find(status);
}
// entire string is available in the UText's chunk buffer.
//
//--------------------------------------------------------------------------------
-UBool RegexMatcher::findUsingChunk() {
+UBool RegexMatcher::findUsingChunk(UErrorCode &status) {
// Start at the position of the last match end. (Will be zero if the
// matcher has been reset.
//
// No optimization was found.
// Try a match at each input position.
for (;;) {
- MatchChunkAt(startPos, FALSE, fDeferredStatus);
- if (U_FAILURE(fDeferredStatus)) {
+ MatchChunkAt(startPos, FALSE, status);
+ if (U_FAILURE(status)) {
return FALSE;
}
if (fMatch) {
// Note that it's perfectly OK for a pattern to have a zero-length
// match at the end of a string, so we must make sure that the loop
// runs with startPos == testLen the last time through.
- if (REGEXFINDPROGRESS_INTERRUPT(startPos, fDeferredStatus))
+ if (findProgressInterrupt(startPos, status))
return FALSE;
}
U_ASSERT(FALSE);
fMatch = FALSE;
return FALSE;
}
- MatchChunkAt(startPos, FALSE, fDeferredStatus);
- if (U_FAILURE(fDeferredStatus)) {
+ MatchChunkAt(startPos, FALSE, status);
+ if (U_FAILURE(status)) {
return FALSE;
}
return fMatch;
U16_NEXT(inputBuf, startPos, fActiveLimit, c); // like c = inputBuf[startPos++];
if ((c<256 && fPattern->fInitialChars8->contains(c)) ||
(c>=256 && fPattern->fInitialChars->contains(c))) {
- MatchChunkAt(pos, FALSE, fDeferredStatus);
- if (U_FAILURE(fDeferredStatus)) {
+ MatchChunkAt(pos, FALSE, status);
+ if (U_FAILURE(status)) {
return FALSE;
}
if (fMatch) {
fHitEnd = TRUE;
return FALSE;
}
- if (REGEXFINDPROGRESS_INTERRUPT(startPos, fDeferredStatus))
+ if (findProgressInterrupt(startPos, status))
return FALSE;
}
}
int32_t pos = startPos;
U16_NEXT(inputBuf, startPos, fActiveLimit, c); // like c = inputBuf[startPos++];
if (c == theChar) {
- MatchChunkAt(pos, FALSE, fDeferredStatus);
- if (U_FAILURE(fDeferredStatus)) {
+ MatchChunkAt(pos, FALSE, status);
+ if (U_FAILURE(status)) {
return FALSE;
}
if (fMatch) {
fHitEnd = TRUE;
return FALSE;
}
- if (REGEXFINDPROGRESS_INTERRUPT(startPos, fDeferredStatus))
+ if (findProgressInterrupt(startPos, status))
return FALSE;
}
}
{
UChar32 c;
if (startPos == fAnchorStart) {
- MatchChunkAt(startPos, FALSE, fDeferredStatus);
- if (U_FAILURE(fDeferredStatus)) {
+ MatchChunkAt(startPos, FALSE, status);
+ if (U_FAILURE(status)) {
return FALSE;
}
if (fMatch) {
for (;;) {
c = inputBuf[startPos-1];
if (c == 0x0a) {
- MatchChunkAt(startPos, FALSE, fDeferredStatus);
- if (U_FAILURE(fDeferredStatus)) {
+ MatchChunkAt(startPos, FALSE, status);
+ if (U_FAILURE(status)) {
return FALSE;
}
if (fMatch) {
// Note that it's perfectly OK for a pattern to have a zero-length
// match at the end of a string, so we must make sure that the loop
// runs with startPos == testLen the last time through.
- if (REGEXFINDPROGRESS_INTERRUPT(startPos, fDeferredStatus))
+ if (findProgressInterrupt(startPos, status))
return FALSE;
}
} else {
if (c == 0x0d && startPos < fActiveLimit && inputBuf[startPos] == 0x0a) {
startPos++;
}
- MatchChunkAt(startPos, FALSE, fDeferredStatus);
- if (U_FAILURE(fDeferredStatus)) {
+ MatchChunkAt(startPos, FALSE, status);
+ if (U_FAILURE(status)) {
return FALSE;
}
if (fMatch) {
// Note that it's perfectly OK for a pattern to have a zero-length
// match at the end of a string, so we must make sure that the loop
// runs with startPos == testLen the last time through.
- if (REGEXFINDPROGRESS_INTERRUPT(startPos, fDeferredStatus))
+ if (findProgressInterrupt(startPos, status))
return FALSE;
}
}
// Return deep (mutable) clone
-// Technology Preview (as an API), but note that the UnicodeString API is implemented
-// using this function.
+// 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 {
if (U_FAILURE(status)) {
return dest;
//--------------------------------------------------------------------------------
//
-// ReportFindProgress This function is called once for each advance in the target
+// findProgressInterrupt 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:
-//
-// If the match operation needs to be aborted because the user
-// callback asked for it, just set an error status.
-// The engine will pick that up and stop in its outer loop.
+// Return: TRUE if the find operation is to be terminated.
+// FALSE if the find operation is to continue running.
//
//--------------------------------------------------------------------------------
-UBool RegexMatcher::ReportFindProgress(int64_t matchIndex, UErrorCode &status) {
- if (fFindProgressCallbackFn != NULL) {
- if ((*fFindProgressCallbackFn)(fFindProgressCallbackContext, matchIndex) == FALSE) {
- status = U_ZERO_ERROR /*U_REGEX_STOPPED_BY_CALLER*/;
- return FALSE;
- }
+UBool RegexMatcher::findProgressInterrupt(int64_t pos, UErrorCode &status) {
+ if (fFindProgressCallbackFn && !(*fFindProgressCallbackFn)(fFindProgressCallbackContext, pos)) {
+ status = U_REGEX_STOPPED_BY_CALLER;
+ return TRUE;
}
- return TRUE;
+ return FALSE;
}
//--------------------------------------------------------------------------------
virtual UBool find();
+ /**
+ * Find the next pattern match in the input string.
+ * The find begins searching the input at the location following the end of
+ * the previous match, or at the start of the string if there is no previous match.
+ * If a match is found, <code>start(), end()</code> and <code>group()</code>
+ * will provide more information regarding the match.
+ * <p>Note that if the input string is changed by the application,
+ * use find(startPos, status) instead of find(), because the saved starting
+ * position may not be valid with the altered input string.</p>
+ * @param status A reference to a UErrorCode to receive any errors.
+ * @return TRUE if a match is found.
+ * @stable @internal
+ */
+ virtual UBool find(UErrorCode &status);
+
/**
* Resets this RegexMatcher and then attempts to find the next substring of the
* input string that matches the pattern, starting at the specified index.
REStackFrame *resetStack();
inline REStackFrame *StateSave(REStackFrame *fp, int64_t savePatIdx, UErrorCode &status);
void IncrementTime(UErrorCode &status);
- UBool ReportFindProgress(int64_t matchIndex, UErrorCode &status);
+
+ // Call user find callback function, if set. Return TRUE if operation should be interrupted.
+ inline UBool findProgressInterrupt(int64_t matchIndex, UErrorCode &status);
int64_t appendGroup(int32_t groupNum, UText *dest, UErrorCode &status) const;
- UBool findUsingChunk();
+ UBool findUsingChunk(UErrorCode &status);
void MatchChunkAt(int32_t startIdx, UBool toEnd, UErrorCode &status);
UBool isChunkWordBoundary(int32_t pos);
/*
*******************************************************************************
-* Copyright (C) 2004-2013, International Business Machines
+* Copyright (C) 2004-2014, International Business Machines
* Corporation and others. All Rights Reserved.
*******************************************************************************
* file name: uregex.cpp
re->fPatStringLen = patternLength;
u_memcpy(patBuf, pattern, actualPatLen);
patBuf[actualPatLen] = 0;
-
+
UText patText = UTEXT_INITIALIZER;
utext_openUChars(&patText, patBuf, patternLength, status);
re->fPat = RegexPattern::compile(&patText, flags, *status);
}
utext_close(&patText);
-
+
if (U_FAILURE(*status)) {
goto ErrorExit;
}
uint32_t flags,
UParseError *pe,
UErrorCode *status) {
-
+
if (U_FAILURE(*status)) {
return NULL;
}
*status = U_ILLEGAL_ARGUMENT_ERROR;
return NULL;
}
-
+
int64_t patternNativeLength = utext_nativeLength(pattern);
-
+
if (patternNativeLength == 0) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return NULL;
}
-
+
RegularExpression *re = new RegularExpression;
-
+
UErrorCode lengthStatus = U_ZERO_ERROR;
int32_t pattern16Length = utext_extract(pattern, 0, patternNativeLength, NULL, 0, &lengthStatus);
-
+
u_atomic_int32_t *refC = (u_atomic_int32_t *)uprv_malloc(sizeof(int32_t));
UChar *patBuf = (UChar *)uprv_malloc(sizeof(UChar)*(pattern16Length+1));
if (re == NULL || refC == NULL || patBuf == NULL) {
}
re->fPatRefCount = refC;
*re->fPatRefCount = 1;
-
+
//
// Make a copy of the pattern string, so we can return it later if asked.
// For compiling the pattern, we will use a read-only UText wrapper
re->fPatString = patBuf;
re->fPatStringLen = pattern16Length;
utext_extract(pattern, 0, patternNativeLength, patBuf, pattern16Length+1, status);
-
+
UText patText = UTEXT_INITIALIZER;
utext_openUChars(&patText, patBuf, pattern16Length, status);
-
+
//
// Compile the pattern
//
re->fPat = RegexPattern::compile(&patText, flags, *status);
}
utext_close(&patText);
-
+
if (U_FAILURE(*status)) {
goto ErrorExit;
}
-
+
//
// Create the matcher object
//
if (U_SUCCESS(*status)) {
return (URegularExpression*)re;
}
-
+
ErrorExit:
delete re;
return NULL;
-
+
}
//----------------------------------------------------------------------------------------
// uregex_clone
//
//----------------------------------------------------------------------------------------
-U_CAPI URegularExpression * U_EXPORT2
+U_CAPI URegularExpression * U_EXPORT2
uregex_clone(const URegularExpression *source2, UErrorCode *status) {
RegularExpression *source = (RegularExpression*)source2;
if (validateRE(source, FALSE, status) == FALSE) {
}
clone->fPat = source->fPat;
- clone->fPatRefCount = source->fPatRefCount;
+ clone->fPatRefCount = source->fPatRefCount;
clone->fPatString = source->fPatString;
clone->fPatStringLen = source->fPatStringLen;
umtx_atomic_inc(source->fPatRefCount);
// uregex_pattern
//
//------------------------------------------------------------------------------
-U_CAPI const UChar * U_EXPORT2
+U_CAPI const UChar * U_EXPORT2
uregex_pattern(const URegularExpression *regexp2,
int32_t *patLength,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
-
+
if (validateRE(regexp, FALSE, status) == FALSE) {
return NULL;
}
// uregex_flags
//
//------------------------------------------------------------------------------
-U_CAPI int32_t U_EXPORT2
+U_CAPI int32_t U_EXPORT2
uregex_flags(const URegularExpression *regexp2, UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, FALSE, status) == FALSE) {
// uregex_setText
//
//------------------------------------------------------------------------------
-U_CAPI void U_EXPORT2
+U_CAPI void U_EXPORT2
uregex_setText(URegularExpression *regexp2,
const UChar *text,
int32_t textLength,
*status = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
-
+
if (regexp->fOwnsText && regexp->fText != NULL) {
uprv_free((void *)regexp->fText);
}
-
+
regexp->fText = text;
regexp->fTextLength = textLength;
regexp->fOwnsText = FALSE;
-
+
UText input = UTEXT_INITIALIZER;
utext_openUChars(&input, text, textLength, status);
regexp->fMatcher->reset(&input);
// uregex_setUText
//
//------------------------------------------------------------------------------
-U_CAPI void U_EXPORT2
+U_CAPI void U_EXPORT2
uregex_setUText(URegularExpression *regexp2,
UText *text,
UErrorCode *status) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
-
+
if (regexp->fOwnsText && regexp->fText != NULL) {
uprv_free((void *)regexp->fText);
}
-
+
regexp->fText = NULL; // only fill it in on request
regexp->fTextLength = -1;
regexp->fOwnsText = TRUE;
// uregex_getText
//
//------------------------------------------------------------------------------
-U_CAPI const UChar * U_EXPORT2
+U_CAPI const UChar * U_EXPORT2
uregex_getText(URegularExpression *regexp2,
int32_t *textLength,
UErrorCode *status) {
if (validateRE(regexp, FALSE, status) == FALSE) {
return NULL;
}
-
+
if (regexp->fText == NULL) {
// need to fill in the text
UText *inputText = regexp->fMatcher->inputText();
UErrorCode lengthStatus = U_ZERO_ERROR;
regexp->fTextLength = utext_extract(inputText, 0, inputNativeLength, NULL, 0, &lengthStatus); // buffer overflow error
UChar *inputChars = (UChar *)uprv_malloc(sizeof(UChar)*(regexp->fTextLength+1));
-
+
utext_extract(inputText, 0, inputNativeLength, inputChars, regexp->fTextLength+1, status);
regexp->fText = inputChars;
regexp->fOwnsText = TRUE; // should already be set but just in case
}
}
-
+
if (textLength != NULL) {
*textLength = regexp->fTextLength;
}
// uregex_getUText
//
//------------------------------------------------------------------------------
-U_CAPI UText * U_EXPORT2
+U_CAPI UText * U_EXPORT2
uregex_getUText(URegularExpression *regexp2,
UText *dest,
UErrorCode *status) {
// uregex_refreshUText
//
//------------------------------------------------------------------------------
-U_CAPI void U_EXPORT2
+U_CAPI void U_EXPORT2
uregex_refreshUText(URegularExpression *regexp2,
UText *text,
UErrorCode *status) {
// uregex_matches
//
//------------------------------------------------------------------------------
-U_CAPI UBool U_EXPORT2
+U_CAPI UBool U_EXPORT2
uregex_matches(URegularExpression *regexp2,
int32_t startIndex,
UErrorCode *status) {
return uregex_matches64( regexp2, (int64_t)startIndex, status);
}
-U_CAPI UBool U_EXPORT2
+U_CAPI UBool U_EXPORT2
uregex_matches64(URegularExpression *regexp2,
int64_t startIndex,
UErrorCode *status) {
// uregex_lookingAt
//
//------------------------------------------------------------------------------
-U_CAPI UBool U_EXPORT2
+U_CAPI UBool U_EXPORT2
uregex_lookingAt(URegularExpression *regexp2,
int32_t startIndex,
UErrorCode *status) {
return uregex_lookingAt64( regexp2, (int64_t)startIndex, status);
}
-U_CAPI UBool U_EXPORT2
+U_CAPI UBool U_EXPORT2
uregex_lookingAt64(URegularExpression *regexp2,
int64_t startIndex,
UErrorCode *status) {
// uregex_find
//
//------------------------------------------------------------------------------
-U_CAPI UBool U_EXPORT2
+U_CAPI UBool U_EXPORT2
uregex_find(URegularExpression *regexp2,
- int32_t startIndex,
+ int32_t startIndex,
UErrorCode *status) {
return uregex_find64( regexp2, (int64_t)startIndex, status);
}
-U_CAPI UBool U_EXPORT2
+U_CAPI UBool U_EXPORT2
uregex_find64(URegularExpression *regexp2,
- int64_t startIndex,
+ int64_t startIndex,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
UBool result = FALSE;
}
if (startIndex == -1) {
regexp->fMatcher->resetPreserveRegion();
- result = regexp->fMatcher->find();
+ result = regexp->fMatcher->find(*status);
} else {
result = regexp->fMatcher->find(startIndex, *status);
}
// uregex_findNext
//
//------------------------------------------------------------------------------
-U_CAPI UBool U_EXPORT2
+U_CAPI UBool U_EXPORT2
uregex_findNext(URegularExpression *regexp2,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, TRUE, status) == FALSE) {
return FALSE;
}
- UBool result = regexp->fMatcher->find();
+ UBool result = regexp->fMatcher->find(*status);
return result;
}
// uregex_groupCount
//
//------------------------------------------------------------------------------
-U_CAPI int32_t U_EXPORT2
+U_CAPI int32_t U_EXPORT2
uregex_groupCount(URegularExpression *regexp2,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
// uregex_group
//
//------------------------------------------------------------------------------
-U_CAPI int32_t U_EXPORT2
+U_CAPI int32_t U_EXPORT2
uregex_group(URegularExpression *regexp2,
int32_t groupNum,
UChar *dest,
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
-
+
if (destCapacity == 0 || regexp->fText != NULL) {
// If preflighting or if we already have the text as UChars,
// this is a little cheaper than going through uregex_groupUTextDeep()
-
+
//
// Pick up the range of characters from the matcher
//
//
// Trim length based on buffer capacity
- //
+ //
int32_t fullLength = endIx - startIx;
int32_t copyLength = fullLength;
if (copyLength < destCapacity) {
copyLength = destCapacity;
*status = U_BUFFER_OVERFLOW_ERROR;
}
-
+
//
// Copy capture group to user's buffer
//
// uregex_groupUText
//
//------------------------------------------------------------------------------
-U_CAPI UText * U_EXPORT2
+U_CAPI UText * U_EXPORT2
uregex_groupUText(URegularExpression *regexp2,
int32_t groupNum,
UText *dest,
// uregex_groupUTextDeep
//
//------------------------------------------------------------------------------
-U_CAPI UText * U_EXPORT2
+U_CAPI UText * U_EXPORT2
uregex_groupUTextDeep(URegularExpression *regexp2,
int32_t groupNum,
UText *dest,
UErrorCode emptyTextStatus = U_ZERO_ERROR;
return (dest ? dest : utext_openUChars(NULL, NULL, 0, &emptyTextStatus));
}
-
+
if (dest) {
utext_replace(dest, 0, utext_nativeLength(dest), ®exp->fText[startIx], endIx - startIx, status);
} else {
dest = utext_clone(NULL, &groupText, TRUE, FALSE, status);
utext_close(&groupText);
}
-
+
return dest;
} else {
return regexp->fMatcher->group(groupNum, dest, *status);
// uregex_start
//
//------------------------------------------------------------------------------
-U_CAPI int32_t U_EXPORT2
+U_CAPI int32_t U_EXPORT2
uregex_start(URegularExpression *regexp2,
int32_t groupNum,
UErrorCode *status) {
return (int32_t)uregex_start64( regexp2, groupNum, status);
}
-U_CAPI int64_t U_EXPORT2
+U_CAPI int64_t U_EXPORT2
uregex_start64(URegularExpression *regexp2,
int32_t groupNum,
UErrorCode *status) {
// uregex_end
//
//------------------------------------------------------------------------------
-U_CAPI int32_t U_EXPORT2
+U_CAPI int32_t U_EXPORT2
uregex_end(URegularExpression *regexp2,
int32_t groupNum,
UErrorCode *status) {
return (int32_t)uregex_end64( regexp2, groupNum, status);
}
-U_CAPI int64_t U_EXPORT2
+U_CAPI int64_t U_EXPORT2
uregex_end64(URegularExpression *regexp2,
int32_t groupNum,
UErrorCode *status) {
// uregex_reset
//
//------------------------------------------------------------------------------
-U_CAPI void U_EXPORT2
+U_CAPI void U_EXPORT2
uregex_reset(URegularExpression *regexp2,
int32_t index,
UErrorCode *status) {
uregex_reset64( regexp2, (int64_t)index, status);
}
-U_CAPI void U_EXPORT2
+U_CAPI void U_EXPORT2
uregex_reset64(URegularExpression *regexp2,
int64_t index,
UErrorCode *status) {
// uregex_setRegion
//
//------------------------------------------------------------------------------
-U_CAPI void U_EXPORT2
+U_CAPI void U_EXPORT2
uregex_setRegion(URegularExpression *regexp2,
int32_t regionStart,
int32_t regionLimit,
uregex_setRegion64( regexp2, (int64_t)regionStart, (int64_t)regionLimit, status);
}
-U_CAPI void U_EXPORT2
+U_CAPI void U_EXPORT2
uregex_setRegion64(URegularExpression *regexp2,
int64_t regionStart,
int64_t regionLimit,
// uregex_setRegionAndStart
//
//------------------------------------------------------------------------------
-U_CAPI void U_EXPORT2
+U_CAPI void U_EXPORT2
uregex_setRegionAndStart(URegularExpression *regexp2,
int64_t regionStart,
int64_t regionLimit,
// uregex_regionStart
//
//------------------------------------------------------------------------------
-U_CAPI int32_t U_EXPORT2
+U_CAPI int32_t U_EXPORT2
uregex_regionStart(const URegularExpression *regexp2,
UErrorCode *status) {
return (int32_t)uregex_regionStart64(regexp2, status);
}
-U_CAPI int64_t U_EXPORT2
+U_CAPI int64_t U_EXPORT2
uregex_regionStart64(const URegularExpression *regexp2,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
// uregex_regionEnd
//
//------------------------------------------------------------------------------
-U_CAPI int32_t U_EXPORT2
+U_CAPI int32_t U_EXPORT2
uregex_regionEnd(const URegularExpression *regexp2,
UErrorCode *status) {
return (int32_t)uregex_regionEnd64(regexp2, status);
}
-U_CAPI int64_t U_EXPORT2
+U_CAPI int64_t U_EXPORT2
uregex_regionEnd64(const URegularExpression *regexp2,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
// uregex_hasTransparentBounds
//
//------------------------------------------------------------------------------
-U_CAPI UBool U_EXPORT2
+U_CAPI UBool U_EXPORT2
uregex_hasTransparentBounds(const URegularExpression *regexp2,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
// uregex_useTransparentBounds
//
//------------------------------------------------------------------------------
-U_CAPI void U_EXPORT2
+U_CAPI void U_EXPORT2
uregex_useTransparentBounds(URegularExpression *regexp2,
UBool b,
UErrorCode *status) {
// uregex_hasAnchoringBounds
//
//------------------------------------------------------------------------------
-U_CAPI UBool U_EXPORT2
+U_CAPI UBool U_EXPORT2
uregex_hasAnchoringBounds(const URegularExpression *regexp2,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
// uregex_useAnchoringBounds
//
//------------------------------------------------------------------------------
-U_CAPI void U_EXPORT2
+U_CAPI void U_EXPORT2
uregex_useAnchoringBounds(URegularExpression *regexp2,
UBool b,
UErrorCode *status) {
// uregex_hitEnd
//
//------------------------------------------------------------------------------
-U_CAPI UBool U_EXPORT2
+U_CAPI UBool U_EXPORT2
uregex_hitEnd(const URegularExpression *regexp2,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
// uregex_requireEnd
//
//------------------------------------------------------------------------------
-U_CAPI UBool U_EXPORT2
+U_CAPI UBool U_EXPORT2
uregex_requireEnd(const URegularExpression *regexp2,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
// uregex_setTimeLimit
//
//------------------------------------------------------------------------------
-U_CAPI void U_EXPORT2
+U_CAPI void U_EXPORT2
uregex_setTimeLimit(URegularExpression *regexp2,
int32_t limit,
UErrorCode *status) {
// uregex_getTimeLimit
//
//------------------------------------------------------------------------------
-U_CAPI int32_t U_EXPORT2
+U_CAPI int32_t U_EXPORT2
uregex_getTimeLimit(const URegularExpression *regexp2,
UErrorCode *status) {
int32_t retVal = 0;
// uregex_setStackLimit
//
//------------------------------------------------------------------------------
-U_CAPI void U_EXPORT2
+U_CAPI void U_EXPORT2
uregex_setStackLimit(URegularExpression *regexp2,
int32_t limit,
UErrorCode *status) {
// uregex_getStackLimit
//
//------------------------------------------------------------------------------
-U_CAPI int32_t U_EXPORT2
+U_CAPI int32_t U_EXPORT2
uregex_getStackLimit(const URegularExpression *regexp2,
UErrorCode *status) {
int32_t retVal = 0;
// uregex_getMatchCallback
//
//------------------------------------------------------------------------------
-U_CAPI void U_EXPORT2
+U_CAPI void U_EXPORT2
uregex_getMatchCallback(const URegularExpression *regexp2,
URegexMatchCallback **callback,
const void **context,
// uregex_getMatchCallback
//
//------------------------------------------------------------------------------
-U_CAPI void U_EXPORT2
+U_CAPI void U_EXPORT2
uregex_getFindProgressCallback(const URegularExpression *regexp2,
URegexFindProgressCallback **callback,
const void **context,
// uregex_replaceAll
//
//------------------------------------------------------------------------------
-U_CAPI int32_t U_EXPORT2
+U_CAPI int32_t U_EXPORT2
uregex_replaceAll(URegularExpression *regexp2,
const UChar *replacementText,
int32_t replacementLength,
&destBuf, &destCapacity, status);
}
len += uregex_appendTail(regexp2, &destBuf, &destCapacity, status);
-
+
if (U_FAILURE(findStatus)) {
// If anything went wrong with the findNext(), make that error trump
// whatever may have happened with the append() operations.
// uregex_replaceAllUText
//
//------------------------------------------------------------------------------
-U_CAPI UText * U_EXPORT2
+U_CAPI UText * U_EXPORT2
uregex_replaceAllUText(URegularExpression *regexp2,
UText *replacementText,
UText *dest,
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
-
+
dest = regexp->fMatcher->replaceAll(replacementText, dest, *status);
return dest;
}
-
+
//------------------------------------------------------------------------------
//
// uregex_replaceFirst
//
//------------------------------------------------------------------------------
-U_CAPI int32_t U_EXPORT2
+U_CAPI int32_t U_EXPORT2
uregex_replaceFirst(URegularExpression *regexp2,
const UChar *replacementText,
int32_t replacementLength,
uregex_reset(regexp2, 0, status);
findSucceeded = uregex_find(regexp2, 0, status);
if (findSucceeded) {
- len = uregex_appendReplacement(regexp2, replacementText, replacementLength,
+ len = uregex_appendReplacement(regexp2, replacementText, replacementLength,
&destBuf, &destCapacity, status);
}
len += uregex_appendTail(regexp2, &destBuf, &destCapacity, status);
// uregex_replaceFirstUText
//
//------------------------------------------------------------------------------
-U_CAPI UText * U_EXPORT2
+U_CAPI UText * U_EXPORT2
uregex_replaceFirstUText(URegularExpression *regexp2,
UText *replacementText,
UText *dest,
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
-
+
dest = regexp->fMatcher->replaceFirst(replacementText, dest, *status);
return dest;
}
UChar **destBuf,
int32_t *destCapacity,
UErrorCode *status);
-
+
inline static int32_t split(RegularExpression *regexp,
UChar *destBuf,
int32_t destCapacity,
return 0;
}
if (replacementText == NULL || replacementLength < -1 ||
- destCapacity == NULL || destBuf == NULL ||
+ destCapacity == NULL || destBuf == NULL ||
(*destBuf == NULL && *destCapacity > 0) ||
*destCapacity < 0) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
int32_t capacity = *destCapacity;
int32_t destIdx = 0;
int32_t i;
-
+
// If it wasn't supplied by the caller, get the length of the replacement text.
// TODO: slightly smarter logic in the copy loop could watch for the NUL on
// the fly and avoid this step.
}
for (i=lastMatchEnd; i<matchStart; i++) {
appendToBuf(regexp->fText[i], &destIdx, dest, capacity);
- }
+ }
} else {
UErrorCode possibleOverflowError = U_ZERO_ERROR; // ignore
destIdx += utext_extract(m->fInputText, m->fLastMatchEnd, m->fMatchStart,
UChar c = replacementText[replIdx];
replIdx++;
if (c != DOLLARSIGN && c != BACKSLASH) {
- // Common case, no substitution, no escaping,
+ // Common case, no substitution, no escaping,
// just copy the char to the dest buf.
appendToBuf(c, &destIdx, dest, capacity);
continue;
if (c==0x55/*U*/ || c==0x75/*u*/) {
// We have a \udddd or \Udddddddd escape sequence.
- UChar32 escapedChar =
+ UChar32 escapedChar =
u_unescapeAt(uregex_ucstr_unescape_charAt,
- &replIdx, // Index is updated by unescapeAt
+ &replIdx, // Index is updated by unescapeAt
replacementLength, // Length of replacement text
(void *)replacementText);
} else {
*status = U_BUFFER_OVERFLOW_ERROR;
}
-
+
//
// Return an updated dest buffer and capacity to the caller.
//
//
// appendReplacement the actual API function,
//
-U_CAPI int32_t U_EXPORT2
+U_CAPI int32_t U_EXPORT2
uregex_appendReplacement(URegularExpression *regexp2,
const UChar *replacementText,
int32_t replacementLength,
UChar **destBuf,
int32_t *destCapacity,
UErrorCode *status) {
-
+
RegularExpression *regexp = (RegularExpression*)regexp2;
return RegexCImpl::appendReplacement(
regexp, replacementText, replacementLength,destBuf, destCapacity, status);
//
// uregex_appendReplacementUText...can just use the normal C++ method
//
-U_CAPI void U_EXPORT2
+U_CAPI void U_EXPORT2
uregex_appendReplacementUText(URegularExpression *regexp2,
UText *replText,
UText *dest,
if (validateRE(regexp, TRUE, status) == FALSE) {
return 0;
}
-
- if (destCapacity == NULL || destBuf == NULL ||
+
+ if (destCapacity == NULL || destBuf == NULL ||
(*destBuf == NULL && *destCapacity > 0) ||
*destCapacity < 0)
{
int32_t destIdx = 0;
int32_t destCap = *destCapacity;
UChar *dest = *destBuf;
-
+
if (regexp->fText != NULL) {
int32_t srcIdx;
int64_t nativeIdx = (m->fMatch ? m->fMatchEnd : m->fLastMatchEnd);
UErrorCode status = U_ZERO_ERROR;
srcIdx = utext_extract(m->fInputText, 0, nativeIdx, NULL, 0, &status);
}
-
+
for (;;) {
U_ASSERT(destIdx >= 0);
}
srcIdx++;
destIdx++;
- }
+ }
} else {
int64_t srcIdx;
if (m->fMatch) {
- // The most recent call to find() succeeded.
+ // The most recent call to find() succeeded.
srcIdx = m->fMatchEnd;
} else {
// The last call to find() on this matcher failed().
//
// appendTail the actual API function
//
-U_CAPI int32_t U_EXPORT2
+U_CAPI int32_t U_EXPORT2
uregex_appendTail(URegularExpression *regexp2,
UChar **destBuf,
int32_t *destCapacity,
//
// uregex_appendTailUText...can just use the normal C++ method
//
-U_CAPI UText * U_EXPORT2
+U_CAPI UText * U_EXPORT2
uregex_appendTailUText(URegularExpression *regexp2,
UText *dest,
UErrorCode *status) {
i = destFieldsCapacity-1;
destIdx = (int32_t)(destFields[i] - destFields[0]);
}
-
+
destFields[i] = &destBuf[destIdx];
destIdx += 1 + utext_extract(inputText, nextOutputStringStart, inputLen,
&destBuf[destIdx], REMAINING_CAPACITY(destIdx, destCapacity), status);
}
break;
}
-
+
if (regexp->fMatcher->find()) {
// We found another delimiter. Move everything from where we started looking
// up until the start of the delimiter into the next output string.
destFields[i] = &destBuf[destIdx];
-
+
destIdx += 1 + utext_extract(inputText, nextOutputStringStart, regexp->fMatcher->fMatchStart,
&destBuf[destIdx], REMAINING_CAPACITY(destIdx, destCapacity), &tStatus);
if (tStatus == U_BUFFER_OVERFLOW_ERROR) {
*status = tStatus;
}
nextOutputStringStart = regexp->fMatcher->fMatchEnd;
-
+
// If the delimiter pattern has capturing parentheses, the captured
// text goes out into the next n destination strings.
int32_t groupNum;
break;
}
i++;
-
+
// Set up to extract the capture group contents into the dest buffer.
destFields[i] = &destBuf[destIdx];
tStatus = U_ZERO_ERROR;
- int32_t t = uregex_group((URegularExpression*)regexp,
- groupNum,
- destFields[i],
- REMAINING_CAPACITY(destIdx, destCapacity),
+ int32_t t = uregex_group((URegularExpression*)regexp,
+ groupNum,
+ destFields[i],
+ REMAINING_CAPACITY(destIdx, destCapacity),
&tStatus);
destIdx += t + 1; // Record the space used in the output string buffer.
// +1 for the NUL that terminates the string.
}
if (nextOutputStringStart == inputLen) {
- // The delimiter was at the end of the string.
+ // The delimiter was at the end of the string.
// Output an empty string, and then we are done.
if (destIdx < destCapacity) {
destBuf[destIdx] = 0;
//
// uregex_split The actual API function
//
-U_CAPI int32_t U_EXPORT2
+U_CAPI int32_t U_EXPORT2
uregex_split(URegularExpression *regexp2,
UChar *destBuf,
int32_t destCapacity,
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
-
+
return RegexCImpl::split(regexp, destBuf, destCapacity, requiredCapacity, destFields, destFieldsCapacity, status);
}
-
+
//
// uregex_splitUText...can just use the normal C++ method
//
-U_CAPI int32_t U_EXPORT2
+U_CAPI int32_t U_EXPORT2
uregex_splitUText(URegularExpression *regexp2,
UText *destFields[],
int32_t destFieldsCapacity,
/********************************************************************
* COPYRIGHT:
- * Copyright (c) 2004-2013, International Business Machines Corporation and
+ * Copyright (c) 2004-2014, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
/********************************************************************************
#include "unicode/utext.h"
#include "cintltst.h"
+#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
+
#define TEST_ASSERT_SUCCESS(status) {if (U_FAILURE(status)) { \
-log_data_err("Failure at file %s, line %d, error = %s (Are you missing data?)\n", __FILE__, __LINE__, u_errorName(status));}}
+log_data_err("Failure at file %s:%d - error = %s (Are you missing data?)\n", __FILE__, __LINE__, u_errorName(status));}}
#define TEST_ASSERT(expr) {if ((expr)==FALSE) { \
-log_data_err("Test Failure at file %s, line %d (Are you missing data?)\n", __FILE__, __LINE__);}}
+log_err("Test Failure at file %s:%d - ASSERT(%s) failed.\n", __FILE__, __LINE__, #expr);}}
/*
* TEST_SETUP and TEST_TEARDOWN
static void TestUTextAPI(void);
static void TestRefreshInput(void);
static void TestBug8421(void);
+static void TestBug10815(void);
void addURegexTest(TestNode** root);
addTest(root, &TestUTextAPI, "regex/TestUTextAPI");
addTest(root, &TestRefreshInput, "regex/TestRefreshInput");
addTest(root, &TestBug8421, "regex/TestBug8421");
+ addTest(root, &TestBug10815, "regex/TestBug10815");
}
/*
memset(&minus1, -1, sizeof(minus1));
/* Mimimalist open/close */
- u_uastrncpy(pat, "abc*", sizeof(pat)/2);
+ u_uastrncpy(pat, "abc*", LENGTHOF(pat));
re = uregex_open(pat, -1, 0, 0, &status);
if (U_FAILURE(status)) {
log_data_err("Failed to open regular expression, %s:%d, error is \"%s\" (Are you missing data?)\n", __FILE__, __LINE__, u_errorName(status));
/* The TEST_ASSERT_SUCCESS above should change too... */
if(U_SUCCESS(status)) {
- u_uastrncpy(pat, "abc*", sizeof(pat)/2);
+ u_uastrncpy(pat, "abc*", LENGTHOF(pat));
TEST_ASSERT(u_strcmp(pat, p) == 0);
TEST_ASSERT(len==(int32_t)strlen("abc*"));
}
TEST_ASSERT_SUCCESS(status);
TEST_ASSERT(clone3 != NULL);
- u_uastrncpy(testString1, "abcccd", sizeof(pat)/2);
- u_uastrncpy(testString2, "xxxabcccd", sizeof(pat)/2);
+ u_uastrncpy(testString1, "abcccd", LENGTHOF(pat));
+ u_uastrncpy(testString2, "xxxabcccd", LENGTHOF(pat));
status = U_ZERO_ERROR;
uregex_setText(clone1, testString1, -1, &status);
{
const UChar *resultPat;
int32_t resultLen;
- u_uastrncpy(pat, "hello", sizeof(pat)/2);
+ u_uastrncpy(pat, "hello", LENGTHOF(pat));
status = U_ZERO_ERROR;
re = uregex_open(pat, -1, 0, NULL, &status);
resultPat = uregex_pattern(re, &resultLen, &status);
UChar text2[50];
UBool result;
- u_uastrncpy(text1, "abcccd", sizeof(text1)/2);
- u_uastrncpy(text2, "abcccxd", sizeof(text2)/2);
+ u_uastrncpy(text1, "abcccd", LENGTHOF(text1));
+ u_uastrncpy(text2, "abcccxd", LENGTHOF(text2));
status = U_ZERO_ERROR;
- u_uastrncpy(pat, "abc*d", sizeof(pat)/2);
+ u_uastrncpy(pat, "abc*d", LENGTHOF(pat));
re = uregex_open(pat, -1, 0, NULL, &status);
TEST_ASSERT_SUCCESS(status);
const UChar *result;
int32_t textLength;
- u_uastrncpy(text1, "abcccd", sizeof(text1)/2);
- u_uastrncpy(text2, "abcccxd", sizeof(text2)/2);
+ u_uastrncpy(text1, "abcccd", LENGTHOF(text1));
+ u_uastrncpy(text2, "abcccxd", LENGTHOF(text2));
status = U_ZERO_ERROR;
- u_uastrncpy(pat, "abc*d", sizeof(pat)/2);
+ u_uastrncpy(pat, "abc*d", LENGTHOF(pat));
re = uregex_open(pat, -1, 0, NULL, &status);
uregex_setText(re, text1, -1, &status);
int len;
UChar nullString[] = {0,0,0};
- u_uastrncpy(text1, "abcccde", sizeof(text1)/2);
+ u_uastrncpy(text1, "abcccde", LENGTHOF(text1));
status = U_ZERO_ERROR;
- u_uastrncpy(pat, "abc*d", sizeof(pat)/2);
+ u_uastrncpy(pat, "abc*d", LENGTHOF(pat));
re = uregex_open(pat, -1, 0, NULL, &status);
uregex_setText(re, text1, -1, &status);
{
UChar text1[50];
UBool result;
- u_uastrncpy(text1, "012rx5rx890rxrx...", sizeof(text1)/2);
+ u_uastrncpy(text1, "012rx5rx890rxrx...", LENGTHOF(text1));
status = U_ZERO_ERROR;
re = uregex_openC("rx", 0, NULL, &status);
UChar buf[80];
UBool result;
int32_t resultSz;
- u_uastrncpy(text1, "noise abc interior def, and this is off the end", sizeof(text1)/2);
+ u_uastrncpy(text1, "noise abc interior def, and this is off the end", LENGTHOF(text1));
status = U_ZERO_ERROR;
re = uregex_openC("abc(.*?)def", 0, NULL, &status);
/* Capture Group 0, the full match. Should succeed. */
status = U_ZERO_ERROR;
- resultSz = uregex_group(re, 0, buf, sizeof(buf)/2, &status);
+ resultSz = uregex_group(re, 0, buf, LENGTHOF(buf), &status);
TEST_ASSERT_SUCCESS(status);
TEST_ASSERT_STRING("abc interior def", buf, TRUE);
TEST_ASSERT(resultSz == (int32_t)strlen("abc interior def"));
/* Capture group #1. Should succeed. */
status = U_ZERO_ERROR;
- resultSz = uregex_group(re, 1, buf, sizeof(buf)/2, &status);
+ resultSz = uregex_group(re, 1, buf, LENGTHOF(buf), &status);
TEST_ASSERT_SUCCESS(status);
TEST_ASSERT_STRING(" interior ", buf, TRUE);
TEST_ASSERT(resultSz == (int32_t)strlen(" interior "));
/* Capture group out of range. Error. */
status = U_ZERO_ERROR;
- uregex_group(re, 2, buf, sizeof(buf)/2, &status);
+ uregex_group(re, 2, buf, LENGTHOF(buf), &status);
TEST_ASSERT(status == U_INDEX_OUTOFBOUNDS_ERROR);
/* NULL buffer, pure pre-flight */
TEST_ASSERT(uregex_regionStart(re, &status) == 3);
TEST_ASSERT(uregex_regionEnd(re, &status) == 6);
TEST_ASSERT(uregex_findNext(re, &status));
- TEST_ASSERT(uregex_group(re, 0, resultString, sizeof(resultString)/2, &status) == 3)
+ TEST_ASSERT(uregex_group(re, 0, resultString, LENGTHOF(resultString), &status) == 3)
TEST_ASSERT_STRING("345", resultString, TRUE);
TEST_TEARDOWN;
UChar replText[80];
UChar buf[80];
int32_t resultSz;
- u_uastrncpy(text1, "Replace xaax x1x x...x.", sizeof(text1)/2);
- u_uastrncpy(text2, "No match here.", sizeof(text2)/2);
- u_uastrncpy(replText, "<$1>", sizeof(replText)/2);
+ u_uastrncpy(text1, "Replace xaax x1x x...x.", LENGTHOF(text1));
+ u_uastrncpy(text2, "No match here.", LENGTHOF(text2));
+ u_uastrncpy(replText, "<$1>", LENGTHOF(replText));
status = U_ZERO_ERROR;
re = uregex_openC("x(.*?)x", 0, NULL, &status);
/* Normal case, with match */
uregex_setText(re, text1, -1, &status);
- resultSz = uregex_replaceFirst(re, replText, -1, buf, sizeof(buf)/2, &status);
+ resultSz = uregex_replaceFirst(re, replText, -1, buf, LENGTHOF(buf), &status);
TEST_ASSERT_SUCCESS(status);
TEST_ASSERT_STRING("Replace <aa> x1x x...x.", buf, TRUE);
TEST_ASSERT(resultSz == (int32_t)strlen("Replace xaax x1x x...x."));
/* No match. Text should copy to output with no changes. */
status = U_ZERO_ERROR;
uregex_setText(re, text2, -1, &status);
- resultSz = uregex_replaceFirst(re, replText, -1, buf, sizeof(buf)/2, &status);
+ resultSz = uregex_replaceFirst(re, replText, -1, buf, LENGTHOF(buf), &status);
TEST_ASSERT_SUCCESS(status);
TEST_ASSERT_STRING("No match here.", buf, TRUE);
TEST_ASSERT(resultSz == (int32_t)strlen("No match here."));
int32_t expectedResultSize2;
int32_t i;
- u_uastrncpy(text1, "Replace xaax x1x x...x.", sizeof(text1)/2);
- u_uastrncpy(text2, "No match here.", sizeof(text2)/2);
- u_uastrncpy(replText, "<$1>", sizeof(replText)/2);
- u_uastrncpy(replText2, "<<$1>>", sizeof(replText2)/2);
+ u_uastrncpy(text1, "Replace xaax x1x x...x.", LENGTHOF(text1));
+ u_uastrncpy(text2, "No match here.", LENGTHOF(text2));
+ u_uastrncpy(replText, "<$1>", LENGTHOF(replText));
+ u_uastrncpy(replText2, "<<$1>>", LENGTHOF(replText2));
expectedResultSize = strlen(expectedResult);
expectedResultSize2 = strlen(expectedResult2);
/* Normal case, with match */
uregex_setText(re, text1, -1, &status);
- resultSize = uregex_replaceAll(re, replText, -1, buf, sizeof(buf)/2, &status);
+ resultSize = uregex_replaceAll(re, replText, -1, buf, LENGTHOF(buf), &status);
TEST_ASSERT_SUCCESS(status);
TEST_ASSERT_STRING(expectedResult, buf, TRUE);
TEST_ASSERT(resultSize == expectedResultSize);
/* No match. Text should copy to output with no changes. */
status = U_ZERO_ERROR;
uregex_setText(re, text2, -1, &status);
- resultSize = uregex_replaceAll(re, replText, -1, buf, sizeof(buf)/2, &status);
+ resultSize = uregex_replaceAll(re, replText, -1, buf, LENGTHOF(buf), &status);
TEST_ASSERT_SUCCESS(status);
TEST_ASSERT_STRING("No match here.", buf, TRUE);
TEST_ASSERT(resultSize == u_strlen(text2));
re = uregex_openC(".*", 0, 0, &status);
TEST_ASSERT_SUCCESS(status);
- u_uastrncpy(text, "whatever", sizeof(text)/2);
- u_uastrncpy(repl, "some other", sizeof(repl)/2);
+ u_uastrncpy(text, "whatever", LENGTHOF(text));
+ u_uastrncpy(repl, "some other", LENGTHOF(repl));
uregex_setText(re, text, -1, &status);
/* match covers whole target string */
uregex_find(re, 0, &status);
TEST_ASSERT_SUCCESS(status);
bufPtr = buf;
- bufCap = sizeof(buf) / 2;
+ bufCap = LENGTHOF(buf);
uregex_appendReplacement(re, repl, -1, &bufPtr, &bufCap, &status);
TEST_ASSERT_SUCCESS(status);
TEST_ASSERT_STRING("some other", buf, TRUE);
uregex_find(re, 0, &status);
TEST_ASSERT_SUCCESS(status);
bufPtr = buf;
- bufCap = sizeof(buf) / 2;
- u_uastrncpy(repl, "abc\\u0041\\U00000042 \\\\ $ \\abc", sizeof(repl)/2);
+ bufCap = LENGTHOF(buf);
+ u_uastrncpy(repl, "abc\\u0041\\U00000042 \\\\ $ \\abc", LENGTHOF(repl));
uregex_appendReplacement(re, repl, -1, &bufPtr, &bufCap, &status);
TEST_ASSERT_SUCCESS(status);
TEST_ASSERT_STRING("abcAB \\ $ abc", buf, TRUE);
int32_t spaceNeeded;
int32_t sz;
- u_uastrncpy(textToSplit, "first : second: third", sizeof(textToSplit)/2);
- u_uastrncpy(text2, "No match here.", sizeof(text2)/2);
+ u_uastrncpy(textToSplit, "first : second: third", LENGTHOF(textToSplit));
+ u_uastrncpy(text2, "No match here.", LENGTHOF(text2));
status = U_ZERO_ERROR;
re = uregex_openC(":", 0, NULL, &status);
if (U_SUCCESS(status)) {
memset(fields, -1, sizeof(fields));
numFields =
- uregex_split(re, buf, sizeof(buf)/2, &requiredCapacity, fields, 10, &status);
+ uregex_split(re, buf, LENGTHOF(buf), &requiredCapacity, fields, 10, &status);
TEST_ASSERT_SUCCESS(status);
/* The TEST_ASSERT_SUCCESS call above should change too... */
if(U_SUCCESS(status)) {
memset(fields, -1, sizeof(fields));
numFields =
- uregex_split(re, buf, sizeof(buf)/2, &requiredCapacity, fields, 2, &status);
+ uregex_split(re, buf, LENGTHOF(buf), &requiredCapacity, fields, 2, &status);
TEST_ASSERT_SUCCESS(status);
/* The TEST_ASSERT_SUCCESS call above should change too... */
int32_t spaceNeeded;
int32_t sz;
- u_uastrncpy(textToSplit, "first <tag-a> second<tag-b> third", sizeof(textToSplit)/2);
+ u_uastrncpy(textToSplit, "first <tag-a> second<tag-b> third", LENGTHOF(textToSplit));
status = U_ZERO_ERROR;
re = uregex_openC("<(.*?)>", 0, NULL, &status);
if(U_SUCCESS(status)) {
memset(fields, -1, sizeof(fields));
numFields =
- uregex_split(re, buf, sizeof(buf)/2, &requiredCapacity, fields, 10, &status);
+ uregex_split(re, buf, LENGTHOF(buf), &requiredCapacity, fields, 10, &status);
TEST_ASSERT_SUCCESS(status);
/* The TEST_ASSERT_SUCCESS call above should change too... */
status = U_ZERO_ERROR;
memset(fields, -1, sizeof(fields));
numFields =
- uregex_split(re, buf, sizeof(buf)/2, &requiredCapacity, fields, 2, &status);
+ uregex_split(re, buf, LENGTHOF(buf), &requiredCapacity, fields, 2, &status);
TEST_ASSERT_SUCCESS(status);
/* The TEST_ASSERT_SUCCESS call above should change too... */
status = U_ZERO_ERROR;
memset(fields, -1, sizeof(fields));
numFields =
- uregex_split(re, buf, sizeof(buf)/2, &requiredCapacity, fields, 3, &status);
+ uregex_split(re, buf, LENGTHOF(buf), &requiredCapacity, fields, 3, &status);
TEST_ASSERT_SUCCESS(status);
/* The TEST_ASSERT_SUCCESS call above should change too... */
status = U_ZERO_ERROR;
memset(fields, -1, sizeof(fields));
numFields =
- uregex_split(re, buf, sizeof(buf)/2, &requiredCapacity, fields, 5, &status);
+ uregex_split(re, buf, LENGTHOF(buf), &requiredCapacity, fields, 5, &status);
TEST_ASSERT_SUCCESS(status);
/* The TEST_ASSERT_SUCCESS call above should change too... */
if(U_SUCCESS(status)) {
memset(fields, -1, sizeof(fields));
numFields =
- uregex_split(re, buf, sizeof(buf)/2, &requiredCapacity, fields, 9, &status);
+ uregex_split(re, buf, LENGTHOF(buf), &requiredCapacity, fields, 9, &status);
TEST_ASSERT_SUCCESS(status);
/* The TEST_ASSERT_SUCCESS call above should change too... */
TEST_ASSERT_SUCCESS(status);
TEST_ASSERT(clone3 != NULL);
- u_uastrncpy(testString1, "abcccd", sizeof(pat)/2);
- u_uastrncpy(testString2, "xxxabcccd", sizeof(pat)/2);
+ u_uastrncpy(testString1, "abcccd", LENGTHOF(pat));
+ u_uastrncpy(testString2, "xxxabcccd", LENGTHOF(pat));
status = U_ZERO_ERROR;
uregex_setText(clone1, testString1, -1, &status);
UText *resultText;
const char str_hello[] = { 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00 }; /* hello */
const char str_hel[] = { 0x68, 0x65, 0x6c, 0x00 }; /* hel */
- u_uastrncpy(pat, "hello", sizeof(pat)/2); /* for comparison */
+ u_uastrncpy(pat, "hello", LENGTHOF(pat)); /* for comparison */
status = U_ZERO_ERROR;
utext_openUTF8(&patternText, str_hello, -1, &status);
status = U_ZERO_ERROR;
utext_openUTF8(&text1, str_abcccd, -1, &status);
- u_uastrncpy(text2Chars, str_abcccxd, sizeof(text2)/2);
+ u_uastrncpy(text2Chars, str_abcccxd, LENGTHOF(text2Chars));
utext_openUChars(&text2, text2Chars, -1, &status);
utext_openUTF8(&patternText, str_abcd, -1, &status);
{
UChar text1[50];
UBool result;
- u_uastrncpy(text1, "012rx5rx890rxrx...", sizeof(text1)/2);
+ u_uastrncpy(text1, "012rx5rx890rxrx...", LENGTHOF(text1));
status = U_ZERO_ERROR;
re = uregex_openC("rx", 0, NULL, &status);
const char str_interior[] = { 0x20, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x69, 0x6f, 0x72, 0x20, 0x00 }; /* ' interior ' */
- u_uastrncpy(text1, "noise abc interior def, and this is off the end", sizeof(text1)/2);
+ u_uastrncpy(text1, "noise abc interior def, and this is off the end", LENGTHOF(text1));
status = U_ZERO_ERROR;
re = uregex_openC("abc(.*?)def", 0, NULL, &status);
const char str_1x[] = { 0x3c, 0x24, 0x31, 0x3e, 0x00 }; /* <$1> */
const char str_ReplaceAaaBax1xxx[] = { 0x52, 0x65, 0x70, 0x6c, 0x61, 0x63, 0x65, 0x20, 0x5c, 0x41, 0x61, 0x61, 0x42, 0x24, 0x61, 0x20, 0x78, 0x31, 0x78, 0x20, 0x78, 0x2e, 0x2e, 0x2e, 0x78, 0x2e, 0x00 }; /* Replace \AaaB$a x1x x...x. */
status = U_ZERO_ERROR;
- u_uastrncpy(text1, "Replace xaax x1x x...x.", sizeof(text1)/2);
- u_uastrncpy(text2, "No match here.", sizeof(text2)/2);
+ u_uastrncpy(text1, "Replace xaax x1x x...x.", LENGTHOF(text1));
+ u_uastrncpy(text2, "No match here.", LENGTHOF(text2));
utext_openUTF8(&replText, str_1x, -1, &status);
re = uregex_openC("x(.*?)x", 0, NULL, &status);
const char str_Replaceaa1[] = { 0x52, 0x65, 0x70, 0x6c, 0x61, 0x63, 0x65, 0x20, 0x3c, 0x61, 0x61, 0x3e, 0x20, 0x3c, 0x31, 0x3e, 0x20, 0x3c, 0x2e, 0x2e, 0x2e, 0x3e, 0x2e, 0x00 }; /* Replace <aa> <1> <...>. */
const char str_Nomatchhere[] = { 0x4e, 0x6f, 0x20, 0x6d, 0x61, 0x74, 0x63, 0x68, 0x20, 0x68, 0x65, 0x72, 0x65, 0x2e, 0x00 }; /* No match here. */
status = U_ZERO_ERROR;
- u_uastrncpy(text1, "Replace xaax x1x x...x.", sizeof(text1)/2);
- u_uastrncpy(text2, "No match here.", sizeof(text2)/2);
+ u_uastrncpy(text1, "Replace xaax x1x x...x.", LENGTHOF(text1));
+ u_uastrncpy(text2, "No match here.", LENGTHOF(text2));
utext_openUTF8(&replText, str_1, -1, &status);
re = uregex_openC("x(.*?)x", 0, NULL, &status);
re = uregex_openC(".*", 0, 0, &status);
TEST_ASSERT_SUCCESS(status);
- u_uastrncpy(text, "whatever", sizeof(text)/2);
- u_uastrncpy(repl, "some other", sizeof(repl)/2);
+ u_uastrncpy(text, "whatever", LENGTHOF(text));
+ u_uastrncpy(repl, "some other", LENGTHOF(repl));
uregex_setText(re, text, -1, &status);
/* match covers whole target string */
uregex_find(re, 0, &status);
TEST_ASSERT_SUCCESS(status);
bufPtr = buf;
- bufCap = sizeof(buf) / 2;
+ bufCap = LENGTHOF(buf);
uregex_appendReplacement(re, repl, -1, &bufPtr, &bufCap, &status);
TEST_ASSERT_SUCCESS(status);
TEST_ASSERT_STRING("some other", buf, TRUE);
uregex_find(re, 0, &status);
TEST_ASSERT_SUCCESS(status);
bufPtr = buf;
- bufCap = sizeof(buf) / 2;
- u_uastrncpy(repl, "abc\\u0041\\U00000042 \\\\ $ \\abc", sizeof(repl)/2);
+ bufCap = LENGTHOF(buf);
+ u_uastrncpy(repl, "abc\\u0041\\U00000042 \\\\ $ \\abc", LENGTHOF(repl));
uregex_appendReplacement(re, repl, -1, &bufPtr, &bufCap, &status);
TEST_ASSERT_SUCCESS(status);
TEST_ASSERT_STRING("abcAB \\ $ abc", buf, TRUE);
int32_t numFields;
int32_t i;
- u_uastrncpy(textToSplit, "first : second: third", sizeof(textToSplit)/2);
- u_uastrncpy(text2, "No match here.", sizeof(text2)/2);
+ u_uastrncpy(textToSplit, "first : second: third", LENGTHOF(textToSplit));
+ u_uastrncpy(text2, "No match here.", LENGTHOF(text2));
status = U_ZERO_ERROR;
re = uregex_openC(":", 0, NULL, &status);
int32_t numFields;
int32_t i;
- u_uastrncpy(textToSplit, "first <tag-a> second<tag-b> third", sizeof(textToSplit)/2);
+ u_uastrncpy(textToSplit, "first <tag-a> second<tag-b> third", LENGTHOF(textToSplit));
status = U_ZERO_ERROR;
re = uregex_openC("<(.*?)>", 0, NULL, &status);
uregex_close(re);
}
+static UBool U_CALLCONV FindCallback(const void* context , int64_t matchIndex) {
+ return FALSE;
+}
+
+static UBool U_CALLCONV MatchCallback(const void *context, int32_t steps) {
+ return FALSE;
+}
+
+static void TestBug10815() {
+ /* Bug 10815: uregex_findNext() does not set U_REGEX_STOPPED_BY_CALLER
+ * when the callback function specified by uregex_setMatchCallback() returns FALSE
+ */
+ URegularExpression *re;
+ UErrorCode status = U_ZERO_ERROR;
+ UChar text[100];
+
+
+ // findNext() with a find progress callback function.
+
+ re = uregex_openC(".z", 0, 0, &status);
+ TEST_ASSERT_SUCCESS(status);
+
+ u_uastrncpy(text, "Hello, World.", LENGTHOF(text));
+ uregex_setText(re, text, -1, &status);
+ TEST_ASSERT_SUCCESS(status);
+
+ uregex_setFindProgressCallback(re, FindCallback, NULL, &status);
+ TEST_ASSERT_SUCCESS(status);
+
+ uregex_findNext(re, &status);
+ TEST_ASSERT(status == U_REGEX_STOPPED_BY_CALLER);
+
+ uregex_close(re);
+
+ // findNext() with a match progress callback function.
+
+ status = U_ZERO_ERROR;
+ re = uregex_openC("((xxx)*)*y", 0, 0, &status);
+ TEST_ASSERT_SUCCESS(status);
+
+ // Pattern + this text gives an exponential time match. Without the callback to stop the match,
+ // it will appear to be stuck in a (near) infinite loop.
+ u_uastrncpy(text, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", LENGTHOF(text));
+ uregex_setText(re, text, -1, &status);
+ TEST_ASSERT_SUCCESS(status);
+
+ uregex_setMatchCallback(re, MatchCallback, NULL, &status);
+ TEST_ASSERT_SUCCESS(status);
+
+ uregex_findNext(re, &status);
+ TEST_ASSERT(status == U_REGEX_STOPPED_BY_CALLER);
+
+ uregex_close(re);
+}
+
#endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS */
utext_openUnicodeString(&destText, &dest, &status);
UText *result;
//const char str_0123456789[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x00 }; /* 0123456789 */
- // Test shallow-clone API
+ // Test shallow-clone API
int64_t group_len;
result = matcher->group((UText *)NULL, group_len, status);
REGEX_CHECK_STATUS;
void reset(int32_t max) {maxCalls=max; numCalls=0;lastIndex=0;};
};
+// call-back function for find().
+// Return TRUE to continue the find().
+// Return FALSE to stop the find().
U_CDECL_BEGIN
static UBool U_CALLCONV
testProgressCallBackFn(const void *context, int64_t matchIndex) {
const void *returnedContext;
URegexFindProgressCallback *returnedFn;
UErrorCode status = U_ZERO_ERROR;
- RegexMatcher matcher(UNICODE_STRING_SIMPLE("((.)+\\2)+x"), 0, status); // A pattern that can run long.
+ RegexMatcher matcher(UNICODE_STRING_SIMPLE("((.)\\2)x"), 0, status);
REGEX_CHECK_STATUS;
matcher.setFindProgressCallback(testProgressCallBackFn, &cbInfo, status);
REGEX_CHECK_STATUS;
REGEX_ASSERT(returnedFn == testProgressCallBackFn);
REGEX_ASSERT(returnedContext == &cbInfo);
- // A short-running match should NOT invoke the callback.
+ // A find that matches on the initial position does NOT invoke the callback.
status = U_ZERO_ERROR;
cbInfo.reset(100);
- UnicodeString s = "abxxx";
+ UnicodeString s = "aaxxx";
matcher.reset(s);
#if 0
matcher.setTrace(TRUE);
REGEX_CHECK_STATUS;
REGEX_ASSERT(cbInfo.numCalls == 0);
- // A medium running match that causes matcher.find() to invoke our callback for each index.
+ // A medium running find() that causes matcher.find() to invoke our callback for each index,
+ // but not so many times that we interrupt the operation.
status = U_ZERO_ERROR;
s = "aaaaaaaaaaaaaaaaaaab";
cbInfo.reset(s.length()); // Some upper limit for number of calls that is greater than size of our input string
cbInfo.reset(s1.length() - 5); // Bail early somewhere near the end of input string
matcher.reset(s1);
REGEX_ASSERT(matcher.find(0, status)==FALSE);
- REGEX_CHECK_STATUS;
+ REGEX_ASSERT(status == U_REGEX_STOPPED_BY_CALLER);
REGEX_ASSERT(cbInfo.numCalls == s1.length() - 5);
-#if 0
// Now a match that will succeed, but after an interruption
status = U_ZERO_ERROR;
UnicodeString s2 = "aaaaaaaaaaaaaa aaaaaaaaab xxx";
cbInfo.reset(s2.length() - 10); // Bail early somewhere near the end of input string
matcher.reset(s2);
REGEX_ASSERT(matcher.find(0, status)==FALSE);
- REGEX_CHECK_STATUS;
+ REGEX_ASSERT(status == U_REGEX_STOPPED_BY_CALLER);
// Now retry the match from where left off
cbInfo.maxCalls = 100; // No callback limit
+ status = U_ZERO_ERROR;
REGEX_ASSERT(matcher.find(cbInfo.lastIndex, status));
REGEX_CHECK_STATUS;
-#endif
}
TestCase11049("A|B|C", "a string \\ud800\\udc00", FALSE, __LINE__);
TestCase11049("A|B|C", "string matches at end C", TRUE, __LINE__);
- // Test again with a pattern starting with a single character,
+ // Test again with a pattern starting with a single character,
// which takes a different code path than starting with an OR expression,
// but with similar logic.
TestCase11049("C", "a string \\ud800\\udc00", FALSE, __LINE__);