Browse Source

* Re-Add MatchIndexFromName. Fixes issue #40478

Michaël Van Canneyt 1 year ago
parent
commit
457daac896
1 changed files with 20 additions and 9 deletions
  1. 20 9
      packages/regexpr/src/regexpr.pas

+ 20 - 9
packages/regexpr/src/regexpr.pas

@@ -785,6 +785,10 @@ type
     // not found in input string.
     // not found in input string.
     property Match[Idx: Integer]: RegExprString read GetMatch;
     property Match[Idx: Integer]: RegExprString read GetMatch;
 
 
+    // get index of group (subexpression) by name, to support named groups
+    // like in Python: (?P<name>regex)
+    function MatchIndexFromName(const AName: RegExprString): Integer;
+
     function MatchFromName(const AName: RegExprString): RegExprString;
     function MatchFromName(const AName: RegExprString): RegExprString;
 
 
     // Returns position in r.e. where compiler stopped.
     // Returns position in r.e. where compiler stopped.
@@ -910,7 +914,7 @@ uses
 const
 const
   // TRegExpr.VersionMajor/Minor return values of these constants:
   // TRegExpr.VersionMajor/Minor return values of these constants:
   REVersionMajor = 1;
   REVersionMajor = 1;
-  REVersionMinor = 169;
+  REVersionMinor = 171;
 
 
   OpKind_End = REChar(1);
   OpKind_End = REChar(1);
   OpKind_MetaClass = REChar(2);
   OpKind_MetaClass = REChar(2);
@@ -2030,8 +2034,7 @@ begin
     fRegexEnd := fRegexStart + Length(fExpression);
     fRegexEnd := fRegexStart + Length(fExpression);
     InvalidateProgramm;
     InvalidateProgramm;
   end;
   end;
-end; { of procedure TRegExpr.SetExpression
-  -------------------------------------------------------------- }
+end;
 
 
 function TRegExpr.GetSubExprCount: Integer;
 function TRegExpr.GetSubExprCount: Integer;
 begin
 begin
@@ -2048,35 +2051,43 @@ begin
   Result := -1;
   Result := -1;
   if Length(GrpIndexes) = 0 then
   if Length(GrpIndexes) = 0 then
     Exit;
     Exit;
+  if (Idx < 0) or (Idx >= Length(GrpIndexes)) then
+    Exit;
   Idx := GrpIndexes[Idx];
   Idx := GrpIndexes[Idx];
   if (Idx >= 0) and (GrpBounds[0].GrpStart[Idx] <> nil) then
   if (Idx >= 0) and (GrpBounds[0].GrpStart[Idx] <> nil) then
     Result := GrpBounds[0].GrpStart[Idx] - fInputStart + 1;
     Result := GrpBounds[0].GrpStart[Idx] - fInputStart + 1;
-end; { of function TRegExpr.GetMatchPos
-  -------------------------------------------------------------- }
+end;
 
 
 function TRegExpr.GetMatchLen(Idx: Integer): PtrInt;
 function TRegExpr.GetMatchLen(Idx: Integer): PtrInt;
 begin
 begin
   Result := -1;
   Result := -1;
   if Length(GrpIndexes) = 0 then
   if Length(GrpIndexes) = 0 then
     Exit;
     Exit;
+  if (Idx < 0) or (Idx >= Length(GrpIndexes)) then
+    Exit;
   Idx := GrpIndexes[Idx];
   Idx := GrpIndexes[Idx];
   if (Idx >= 0) and (GrpBounds[0].GrpStart[Idx] <> nil) then
   if (Idx >= 0) and (GrpBounds[0].GrpStart[Idx] <> nil) then
     Result := GrpBounds[0].GrpEnd[Idx] - GrpBounds[0].GrpStart[Idx];
     Result := GrpBounds[0].GrpEnd[Idx] - GrpBounds[0].GrpStart[Idx];
-end; { of function TRegExpr.GetMatchLen
-  -------------------------------------------------------------- }
+end;
 
 
 function TRegExpr.GetMatch(Idx: Integer): RegExprString;
 function TRegExpr.GetMatch(Idx: Integer): RegExprString;
 begin
 begin
   Result := '';
   Result := '';
   if Length(GrpIndexes) = 0 then
   if Length(GrpIndexes) = 0 then
     Exit;
     Exit;
+  if (Idx < 0) or (Idx >= Length(GrpIndexes)) then
+    Exit;
   Idx := GrpIndexes[Idx];
   Idx := GrpIndexes[Idx];
   if (Idx >= 0) and (GrpBounds[0].GrpStart[Idx] <> nil) and
   if (Idx >= 0) and (GrpBounds[0].GrpStart[Idx] <> nil) and
      (GrpBounds[0].GrpEnd[Idx] > GrpBounds[0].GrpStart[Idx])
      (GrpBounds[0].GrpEnd[Idx] > GrpBounds[0].GrpStart[Idx])
   then
   then
     SetString(Result, GrpBounds[0].GrpStart[Idx], GrpBounds[0].GrpEnd[Idx] - GrpBounds[0].GrpStart[Idx]);
     SetString(Result, GrpBounds[0].GrpStart[Idx], GrpBounds[0].GrpEnd[Idx] - GrpBounds[0].GrpStart[Idx]);
-end; { of function TRegExpr.GetMatch
-  -------------------------------------------------------------- }
+end;
+
+function TRegExpr.MatchIndexFromName(const AName: RegExprString): Integer;
+begin
+  Result := GrpNames.MatchIndexFromName(AName);
+end;
 
 
 function TRegExpr.MatchFromName(const AName: RegExprString): RegExprString;
 function TRegExpr.MatchFromName(const AName: RegExprString): RegExprString;
 var
 var