strings.bif/sub,gsub: Respect anchors in pattern

Anchors within pattern passed to sub() or gsub() were previously ignored,
replacing any occurrence of '<text>' even when '^<text>' was used as a
pattern.

This is a pretty user-visible change (and we even have anchored patterns
within the base scripts), but seems "the right thing to do".

Relates to #3455
This commit is contained in:
Arne Welzel 2023-11-17 13:33:22 +01:00
parent d9b8154c4e
commit e339e93e69
5 changed files with 76 additions and 1 deletions

View file

@ -789,15 +789,22 @@ StringValPtr StringVal::Replace(RE_Matcher* re, const String& repl, bool do_all)
vector<std::pair<int, int>> cut_points;
int size = 0; // size of result
bool bol = true;
const bool eol = true;
while ( n > 0 ) {
// Find next match offset.
int end_of_match;
while ( n > 0 && (end_of_match = re->MatchPrefix(&s[offset], n)) <= 0 ) {
while ( n > 0 ) {
end_of_match = re->MatchPrefix(&s[offset], n, bol, eol);
if ( end_of_match > 0 )
break;
// This character is going to be copied to the result.
++size;
// Move on to next character.
bol = false;
++offset;
--n;
}