Browse Source

* Allow to set lastindex

Michaël Van Canneyt 11 months ago
parent
commit
1084346b00

+ 1 - 0
packages/wasm-utils/src/wasm.regexp.api.pas

@@ -28,6 +28,7 @@ function __wasm_regexp_test(aExprID : TWasmRegExpID; aString : PByte; aStringLen
 function __wasm_regexp_get_flags(aExprID : TWasmRegExpID; aFlags : PLongint) : TWasmRegexpResult; external regexpExportName name regexpFN_GetFlags;
 function __wasm_regexp_get_flags(aExprID : TWasmRegExpID; aFlags : PLongint) : TWasmRegexpResult; external regexpExportName name regexpFN_GetFlags;
 function __wasm_regexp_get_expression(aExprID : TWasmRegExpID; aExp : PByte; aExpLen : PLongint) : TWasmRegexpResult; external regexpExportName name regexpFN_GetExpression;
 function __wasm_regexp_get_expression(aExprID : TWasmRegExpID; aExp : PByte; aExpLen : PLongint) : TWasmRegexpResult; external regexpExportName name regexpFN_GetExpression;
 function __wasm_regexp_get_last_index(aExprID : TWasmRegExpID; aLastIndex : PLongint) : TWasmRegexpResult; external regexpExportName name regexpFN_GetLastIndex;
 function __wasm_regexp_get_last_index(aExprID : TWasmRegExpID; aLastIndex : PLongint) : TWasmRegexpResult; external regexpExportName name regexpFN_GetLastIndex;
+function __wasm_regexp_set_last_index(aExprID : TWasmRegExpID; aLastIndex : Longint) : TWasmRegexpResult; external regexpExportName name regexpFN_SetLastIndex;
 function __wasm_regexp_get_result_match(aExprID : TWasmRegExpID; aIndex : Longint; Res : PByte; ResLen : PLongint) : TWasmRegexpResult; external regexpExportName name regexpFN_GetResultMatch;
 function __wasm_regexp_get_result_match(aExprID : TWasmRegExpID; aIndex : Longint; Res : PByte; ResLen : PLongint) : TWasmRegexpResult; external regexpExportName name regexpFN_GetResultMatch;
 function __wasm_regexp_get_group_count(aExprID : TWasmRegExpID; aCount: PLongint) : TWasmRegexpResult; external regexpExportName name regexpFN_GetGroupCount;
 function __wasm_regexp_get_group_count(aExprID : TWasmRegExpID; aCount: PLongint) : TWasmRegexpResult; external regexpExportName name regexpFN_GetGroupCount;
 function __wasm_regexp_get_group_name(aExprID : TWasmRegExpID; aIndex : Longint; aName : PByte; aNameLen : PLongint) : TWasmRegexpResult; external regexpExportName name regexpFN_GetGroupName;
 function __wasm_regexp_get_group_name(aExprID : TWasmRegExpID; aIndex : Longint; aName : PByte; aNameLen : PLongint) : TWasmRegexpResult; external regexpExportName name regexpFN_GetGroupName;

+ 19 - 1
packages/wasm-utils/src/wasm.regexp.objects.pas

@@ -17,6 +17,7 @@ unit wasm.regexp.objects;
 
 
 {$mode ObjFPC}{$H+}
 {$mode ObjFPC}{$H+}
 {$modeswitch typehelpers}
 {$modeswitch typehelpers}
+{$modeswitch advancedrecords}
 
 
 interface
 interface
 
 
@@ -57,9 +58,12 @@ Type
     Property AsFlags : Longint Read ToFlags Write SetAsFlags;
     Property AsFlags : Longint Read ToFlags Write SetAsFlags;
   end;
   end;
 
 
+  { TRegExpMatch }
+
   TRegExpMatch = record
   TRegExpMatch = record
     Value : UTF8String;
     Value : UTF8String;
     StartIndex, StopIndex : Integer;
     StartIndex, StopIndex : Integer;
+    function MatchPos(aStartIndex, aStopIndex : Integer) : Boolean;
   end;
   end;
   TRegExpMatchArray = array of TRegExpMatch;
   TRegExpMatchArray = array of TRegExpMatch;
 
 
@@ -86,6 +90,7 @@ Type
     function GetGroups(aCount: Integer): TRegExpGroupArray;
     function GetGroups(aCount: Integer): TRegExpGroupArray;
     function GetLastIndex: Longint;
     function GetLastIndex: Longint;
     function GetMatches(aCount: Integer): TRegExpMatchArray;
     function GetMatches(aCount: Integer): TRegExpMatchArray;
+    procedure SetLastIndex(AValue: Longint);
   protected
   protected
     function CheckRegExpResult(Res : TWasmRegexpResult; const aOperation : String; aRaise : Boolean = true) : Boolean;
     function CheckRegExpResult(Res : TWasmRegexpResult; const aOperation : String; aRaise : Boolean = true) : Boolean;
   Public
   Public
@@ -95,7 +100,8 @@ Type
     destructor Destroy; override;
     destructor Destroy; override;
     Function Exec(const aString : String) : TRegExpResult;
     Function Exec(const aString : String) : TRegExpResult;
     Function Test(const aString : String) : Boolean;
     Function Test(const aString : String) : Boolean;
-    Property LastIndex : Longint Read GetLastIndex;
+    // 0 - based !
+    Property LastIndex : Longint Read GetLastIndex Write SetLastIndex;
     Property RegexpID : TWasmRegExpID Read FRegexpID;
     Property RegexpID : TWasmRegExpID Read FRegexpID;
     Property FlagsAsInteger : Integer Read FFlags;
     Property FlagsAsInteger : Integer Read FFlags;
     Property Flags : TRegexpFlags Read GetFlags;
     Property Flags : TRegexpFlags Read GetFlags;
@@ -166,6 +172,13 @@ begin
   Result.AsFlags:=aFlags;
   Result.AsFlags:=aFlags;
 end;
 end;
 
 
+{ TRegExpMatch }
+
+function TRegExpMatch.MatchPos(aStartIndex, aStopIndex: Integer): Boolean;
+begin
+  Result:=(aStartIndex=StartIndex) and (aStopIndex=StopIndex);
+end;
+
 
 
 { TWasmRegExp }
 { TWasmRegExp }
 
 
@@ -244,6 +257,11 @@ begin
     end;
     end;
 end;
 end;
 
 
+procedure TWasmRegExp.SetLastIndex(AValue: Longint);
+begin
+  CheckRegExpResult(__wasm_regexp_set_last_index(FRegexpID,AValue),'set_last_index');
+end;
+
 function TWasmRegExp.GetGroups(aCount: Integer): TRegExpGroupArray;
 function TWasmRegExp.GetGroups(aCount: Integer): TRegExpGroupArray;
 
 
 var
 var

+ 1 - 0
packages/wasm-utils/src/wasm.regexp.shared.pas

@@ -67,6 +67,7 @@ Const
   regexpFN_GetFlags = 'get_flags';
   regexpFN_GetFlags = 'get_flags';
   regexpFN_GetExpression = 'get_expression';
   regexpFN_GetExpression = 'get_expression';
   regexpFN_GetLastIndex = 'get_last_index';
   regexpFN_GetLastIndex = 'get_last_index';
+  regexpFN_SetLastIndex = 'set_last_index';
   regexpFN_GetResultMatch = 'get_result_match';
   regexpFN_GetResultMatch = 'get_result_match';
   regexpFN_GetGroupCount = 'get_group_count';
   regexpFN_GetGroupCount = 'get_group_count';
   regexpFN_GetGroupName = 'get_group_name';
   regexpFN_GetGroupName = 'get_group_name';