ICU-4057 incorrect regex appendReplacement after multiple find()s.

X-SVN-Rev: 16601
This commit is contained in:
Andy Heninger 2004-10-22 22:51:02 +00:00
parent 1675fa4465
commit 1f42f071fc
3 changed files with 52 additions and 9 deletions

View file

@ -149,10 +149,11 @@ RegexMatcher &RegexMatcher::appendReplacement(UnicodeString &dest,
}
// Copy input string from the end of previous match to start of current match
int32_t len = fMatchStart-fLastMatchEnd;
int32_t len = fMatchStart-fLastReplaceEnd;
if (len > 0) {
dest.append(*fInput, fLastMatchEnd, len);
dest.append(*fInput, fLastReplaceEnd, len);
}
fLastReplaceEnd = fMatchEnd;
// scan the replacement text, looking for substitutions ($n) and \escapes.
@ -314,8 +315,7 @@ UBool RegexMatcher::find() {
int32_t startPos = fMatchEnd;
if (fMatch) {
// Save the position of any previous successful match for use by the
// appendReplacement() and appendTail() functions.
// Save the position of any previous successful match.
fLastMatchEnd = fMatchEnd;
if (fMatchStart == fMatchEnd) {
@ -700,10 +700,11 @@ UnicodeString RegexMatcher::replaceFirst(const UnicodeString &replacement, UErro
//
//--------------------------------------------------------------------------------
RegexMatcher &RegexMatcher::reset() {
fMatchStart = 0;
fMatchEnd = 0;
fLastMatchEnd = -1;
fMatch = FALSE;
fMatchStart = 0;
fMatchEnd = 0;
fLastMatchEnd = -1;
fLastReplaceEnd = 0;
fMatch = FALSE;
resetStack();
return *this;
}

View file

@ -890,6 +890,7 @@ private:
int32_t fMatchEnd; // First position after the end of the most recent match
int32_t fLastMatchEnd; // First position after the end of the previous match,
// or -1 if there was no previous match.
int32_t fLastReplaceEnd; // First position after the end of the previous appendReplacement();
UVector32 *fStack;
REStackFrame *fFrame; // After finding a match, the last active stack

View file

@ -1105,7 +1105,48 @@ void RegexTest::API_Replace() {
}
// TODO: need more through testing of capture substitutions.
// Bug 4057
//
{
status = U_ZERO_ERROR;
UnicodeString s = "The matches start with ss and end with ee ss stuff ee fin";
RegexMatcher m("ss(.*?)ee", 0, status);
REGEX_CHECK_STATUS;
UnicodeString result;
// Multiple finds do NOT bump up the previous appendReplacement postion.
m.reset(s);
m.find();
m.find();
m.appendReplacement(result, "ooh", status);
REGEX_CHECK_STATUS;
REGEX_ASSERT(result == "The matches start with ss and end with ee ooh");
// After a reset into the interior of a string, appendReplacemnt still starts at beginning.
status = U_ZERO_ERROR;
result.truncate(0);
m.reset(10, status);
m.find();
m.find();
m.appendReplacement(result, "ooh", status);
REGEX_CHECK_STATUS;
REGEX_ASSERT(result == "The matches start with ss and end with ee ooh");
// find() at interior of string, appendReplacemnt still starts at beginning.
status = U_ZERO_ERROR;
result.truncate(0);
m.reset();
m.find(10, status);
m.find();
m.appendReplacement(result, "ooh", status);
REGEX_CHECK_STATUS;
REGEX_ASSERT(result == "The matches start with ss and end with ee ooh");
m.appendTail(result);
REGEX_ASSERT(result == "The matches start with ss and end with ee ooh fin");
}
delete matcher2;
delete pat2;
delete matcher;