Delphi 10.1 - Invalid Match. Know in TMatchEvaluator when TRegEx.Replace () is called

I found one bug with the latest Delphi 10.1 Berlin (and 10.2 Tokyo too).

If you call TRegEx.Replace with the specified TMatchEvaluator, you might get the wrong TMatch function inside the Evaluator function. This works well in Delphi XE-XE5.

appraiser:

function TForm1.EvaluatorU(const Match: TMatch): string;
var
  lChar: Word;
  lMatchVal: string;
begin
  Result := '';
  lMatchVal := Match.Groups[1].Value;
  lChar := StrToIntDef('$'+lMatchVal, 0);
  if lChar <> 0 then
    Result := Char(lChar);
end;

      

Call:

Result := TRegEx.Replace('\u0418\u0443, \u0427\u0436\u044d\u0446\u0437\u044f\u043d', '\\u([0-9a-f]{4})', EvaluatorU, [roIgnoreCase]);

      

The first call to Evaluator will result in the context TMatch.Value (or TMatch.Group []. Value), but the second call will result in an incorrect Match.Value function for the callback (

Do you have any idea of ​​a workaround?

I am going to test this issue on TPerlRegEx class, maybe something is wrong in wrapper functions (TRegEx).

Update: with replacing TPerlRegEx with a callback function (OnReplace) works well ...

Update2: There seems to be a bug in TPerlRegEx. It returns incorrect GroupOffsets options. The first time the OnReplace callback is called, this value is correct. The following calls return +1 offset more than necessary. But calling TPerlRegEx.Groups returns the correct subgroup value ...

Latest update and solution found :

I found the problem in optimizing TPerlRegEx.UTF8IndexToUnicode. There are LastIndex * / LastIndexResult * fields used to optimize sequential function calls with the same parameters. But after replacement done via callback functions and when MatchAgain is called in TPerlRegEx.ReplaceAll function it might do a bad trick (

A simple solution is to copy System.RegularExpressionsCore.pas from \ source \ rtl \ common to your project directory and replace the TPerlRegEx.UTF8IndexToUnicode call with the deprecated unoptimized UTF8IndexToUnicode function ... Or clear those internal fields somewhere in the ClearStoredGroups function, for example.

obn . Here is my central embarcadero quality problem .

+3
regex delphi


source to share


No one has answered this question yet

Check out similar questions:

sixteen
Why does this line contain reference count 4? (Delphi 2007)
6
Difficulty calling Android NDK function directly from Delphi
five
How to skip quoted text in regex (or how to use HyperStr ParseWord with Unicode text?)
4
Delphi DLL call from C ++ using GetProcAddress: callback function fails with invalid parameter
3
Calling a Delphi 6 DLL from C # results in different rounding?
3
Delphi Get LocationSensor Speed ​​on Android
2
Why does AJAX callback in Delphi 2007 return [Object]
1
how to find a string containing a square bracket?
0
Generating dynamic TForm identifiers (errors?) In Delphi XE5
0
Delphi XML recursive function, find a node with a specific attribute value



All Articles
Loading...
X
Show
Funny
Dev
Pics