Codeunit 3961 Regex Impl.
src/System Application/App/Regex/src/RegexImpl.Codeunit.al408 lines, Copyright (c) Microsoft Corporation. MIT
// ------------------------------------------------------------------------------------------------// Copyright (c) Microsoft Corporation. All rights reserved.// Licensed under the MIT License. See License.txt in the project root for license information.// ------------------------------------------------------------------------------------------------namespace System.Utilities;using System;codeunit 3961 "Regex Impl."{ Access = Internal; InherentEntitlements = X; InherentPermissions = X; var DotNetRegex: DotNet Regex; DotNetRegexOptions: DotNet RegexOptions; DotNetMatchCollection: DotNet MatchCollection; DotNetGroupCollection: DotNet GroupCollection; DotNetCaptureCollection: DotNet CaptureCollection; TimeoutTooLowErr: Label 'The regular expression timeout should be at least 1000 ms'; TimeoutTooHighErr: Label 'The regular expression timeout should be at most 10000 ms'; RegexIsNotInstantiatedErr: Label 'Regex is not Instantiated. Consider calling Regex() first or use an overload supporting a pattern.'; procedure GetCacheSize(): Integer begin exit(DotNetRegex.CacheSize()); end; procedure SetCacheSize(CacheSize: Integer) begin DotNetRegex.CacheSize := CacheSize; end; procedure GetGroupNames(var "Array": List of [Text]) var GroupName: Text; GroupNames: DotNet Array; begin GroupNames := DotNetRegex.GetGroupNames(); foreach GroupName in GroupNames do "Array".Add(GroupName); end; procedure GetGroupNumbers(var "Array": List of [Integer]) var GroupNumber: Integer; GroupNumbers: DotNet Array; begin GroupNumbers := DotNetRegex.GetGroupNumbers(); foreach GroupNumber in GroupNumbers do "Array".Add(GroupNumber); end; procedure GroupNameFromNumber(Number: Integer): Text begin exit(DotNetRegex.GroupNameFromNumber(Number)); end; procedure GroupNumberFromName(Name: Text): Integer begin exit(DotNetRegex.GroupNumberFromName(Name)); end; procedure IsMatch(Input: Text; Pattern: Text; StartAt: Integer): Boolean begin Regex(Pattern); exit(IsMatch(Input, StartAt)) end; procedure IsMatch(Input: Text; StartAt: Integer): Boolean begin exit(DotNetRegex.IsMatch(Input, StartAt)) end; procedure IsMatch(Input: Text; Pattern: Text; StartAt: Integer; var RegexOptions: Record "Regex Options"): Boolean begin Regex(Pattern, RegexOptions); exit(DotNetRegex.IsMatch(Input, StartAt)) end; procedure IsMatch(Input: Text; Pattern: Text): Boolean begin Regex(Pattern); exit(IsMatch(Input)); end; procedure IsMatch(Input: Text): Boolean begin CheckIfInstantiated(); exit(DotNetRegex.IsMatch(Input)); end; procedure IsMatch(Input: Text; Pattern: Text; var RegexOptions: Record "Regex Options"): Boolean begin Regex(Pattern, RegexOptions); exit(DotNetRegex.IsMatch(Input)); end; procedure Match(Input: Text; Pattern: Text; StartAt: Integer; var Matches: Record Matches) begin Regex(Pattern); Match(Input, StartAt, Matches); end; procedure Match(Input: Text; StartAt: Integer; var Matches: Record Matches) begin CheckIfInstantiated(); DotNetMatchCollection := DotNetRegex.Matches(Input, StartAt); InsertMatch(Matches); end; procedure Match(Input: Text; Pattern: Text; StartAt: Integer; var RegexOptions: Record "Regex Options"; var Matches: Record Matches) begin Regex(Pattern, RegexOptions); Match(Input, StartAt, Matches); end; procedure Match(Input: Text; Pattern: Text; StartAt: Integer; Length: Integer; var Matches: Record Matches) begin Regex(Pattern); Match(Input, StartAt, Length, Matches) end; procedure Match(Input: Text; StartAt: Integer; Length: Integer; var Matches: Record Matches) begin CheckIfInstantiated(); Input := Input.Substring(1, StartAt + Length); DotNetMatchCollection := DotNetRegex.Matches(Input, StartAt); InsertMatch(Matches); end; procedure Match(Input: Text; Pattern: Text; StartAt: Integer; Length: Integer; var RegexOptions: Record "Regex Options"; var Matches: Record Matches) begin Regex(Pattern, RegexOptions); Match(Input, StartAt, Length, Matches) end; procedure Match(Input: Text; Pattern: Text; var Matches: Record Matches) begin Regex(Pattern); Match(Input, Matches); end; procedure Match(Input: Text; var Matches: Record Matches) begin CheckIfInstantiated(); DotNetMatchCollection := DotNetRegex.Matches(Input); InsertMatch(Matches); end; procedure Match(Input: Text; Pattern: Text; var RegexOptions: Record "Regex Options"; var Matches: Record Matches) begin Regex(Pattern, RegexOptions); DotNetMatchCollection := DotNetRegex.Matches(Input); InsertMatch(Matches); end; procedure Replace(Input: Text; Pattern: Text; Replacement: Text; "Count": Integer): Text begin Regex(Pattern); exit(Replace(Input, Replacement, "Count")); end; procedure Replace(Input: Text; Replacement: Text; "Count": Integer): Text begin CheckIfInstantiated(); exit(DotNetRegex.Replace(Input, Replacement, "Count")); end; procedure Replace(Input: Text; Pattern: Text; Replacement: Text; "Count": Integer; var RegexOptions: Record "Regex Options"): Text begin Regex(Pattern, RegexOptions); exit(DotNetRegex.Replace(Input, Replacement, "Count")); end; procedure Replace(Input: Text; Pattern: Text; Replacement: Text; "Count": Integer; StartAt: Integer): Text begin Regex(Pattern); exit(Replace(Input, Replacement, "Count", StartAt)); end; procedure Replace(Input: Text; Replacement: Text; "Count": Integer; StartAt: Integer): Text begin CheckIfInstantiated(); exit(DotNetRegex.Replace(Input, Replacement, "Count", StartAt)); end; procedure Replace(Input: Text; Pattern: Text; Replacement: Text; "Count": Integer; StartAt: Integer; var RegexOptions: Record "Regex Options"): Text begin Regex(Pattern, RegexOptions); exit(DotNetRegex.Replace(Input, Replacement, "Count", StartAt)); end; procedure Replace(Input: Text; Pattern: Text; Replacement: Text): Text begin Regex(Pattern); exit(Replace(Input, Replacement)); end; procedure Replace(Input: Text; Replacement: Text): Text begin CheckIfInstantiated(); exit(DotNetRegex.Replace(Input, Replacement)); end; procedure Replace(Input: Text; Pattern: Text; Replacement: Text; var RegexOptions: Record "Regex Options"): Text begin Regex(Pattern, RegexOptions); exit(DotNetRegex.Replace(Input, Replacement)); end; procedure Split(Input: Text; Pattern: Text; "Count": Integer; var "Array": List of [Text]) begin Regex(Pattern); Split(Input, "Count", "Array"); end; procedure Split(Input: Text; "Count": Integer; var "Array": List of [Text]) var StringsDotNetArray: DotNet Array; SplitElement: Text; begin CheckIfInstantiated(); StringsDotNetArray := DotNetRegex.Split(Input, "Count"); foreach SplitElement in StringsDotNetArray do "Array".Add(SplitElement); end; procedure Split(Input: Text; Pattern: Text; "Count": Integer; var RegexOptions: Record "Regex Options"; var "Array": List of [Text]) begin Regex(Pattern, RegexOptions); Split(Input, "Count", "Array"); end; procedure Split(Input: Text; Pattern: Text; "Count": Integer; StartAt: Integer; var "Array": List of [Text]) begin Regex(Pattern); Split(Input, "Count", StartAt, "Array"); end; procedure Split(Input: Text; "Count": Integer; StartAt: Integer; var "Array": List of [Text]) var StringsDotNetArray: DotNet Array; SplitElement: Text; begin CheckIfInstantiated(); StringsDotNetArray := DotNetRegex.Split(Input, "Count", StartAt); foreach SplitElement in StringsDotNetArray do "Array".Add(SplitElement); end; procedure Split(Input: Text; Pattern: Text; "Count": Integer; StartAt: Integer; var RegexOptions: Record "Regex Options"; var "Array": List of [Text]) begin Regex(Pattern, RegexOptions); Split(Input, "Count", StartAt, "Array"); end; procedure Split(Input: Text; Pattern: Text; var "Array": List of [Text]) begin Regex(Pattern); Split(Input, "Array"); end; procedure Split(Input: Text; var "Array": List of [Text]) var StringsDotNetArray: DotNet Array; SplitElement: Text; begin CheckIfInstantiated(); StringsDotNetArray := DotNetRegex.Split(Input); foreach SplitElement in StringsDotNetArray do "Array".Add(SplitElement); end; procedure Split(Input: Text; Pattern: Text; var RegexOptions: Record "Regex Options"; var "Array": List of [Text]) begin Regex(Pattern, RegexOptions); Split(Input, "Array"); end; procedure GetHashCode(): Integer begin exit(DotNetRegex.GetHashCode()); end; procedure Escape(String: Text): Text begin exit(DotNetRegex.Escape(String)); end; procedure Unescape(String: Text): Text begin exit(DotNetRegex.Unescape(String)); end; procedure MatchResult(var Matches: Record Matches; Replacement: Text): Text var MatchIndex: Integer; begin MatchIndex := Matches.MatchIndex; exit(DotNetMatchCollection.Item(MatchIndex).Result(Replacement)); end; procedure Groups(var MatchesRec: Record Matches; var GroupsRec: Record Groups) var MatchIndex: Integer; begin MatchIndex := MatchesRec.MatchIndex; DotNetGroupCollection := DotNetMatchCollection.Item(MatchIndex).Groups; InsertGroups(GroupsRec); end; procedure Captures(var GroupsRec: Record Groups; var CapturesRec: Record Captures) var GroupIndex: Integer; begin GroupIndex := GroupsRec.GroupIndex; DotNetCaptureCollection := DotNetGroupCollection.Item(GroupIndex).Captures; InsertCaptures(CapturesRec) end; procedure Regex(Pattern: Text) var TempRegexOptions: Record "Regex Options"; begin Regex(Pattern, TempRegexOptions); end; procedure Regex(Pattern: Text; var RegexOptions: Record "Regex Options") var TimeoutDuration: DotNet TimeSpan; MatchTimeoutInMs: BigInteger; begin DotNetRegexOptions := RegexOptions.GetRegexOptions(); MatchTimeoutInMs := RegexOptions.MatchTimeoutInMs; if MatchTimeoutInMs < 1000 then Error(TimeoutTooLowErr); if MatchTimeoutInMs > 10000 then Error(TimeoutTooHighErr); DotNetRegex := DotNetRegex.Regex(Pattern, DotNetRegexOptions, TimeoutDuration.FromTicks(MatchTimeoutInMs * 10000)); end; local procedure CheckIfInstantiated() begin if IsNull(DotNetRegex) then Error(RegexIsNotInstantiatedErr); end; local procedure InsertMatch(var Matches: Record Matches) var DotNetMatch: DotNet Match; Index: Integer; begin Matches.DeleteAll(); Index := 0; foreach DotNetMatch in DotNetMatchCollection do begin Matches.Init(); Matches.MatchIndex := Index; Matches.Index := DotNetMatch.Index; Matches.InsertValue(DotNetMatch.Value); Matches.Length := DotNetMatch.Length; Matches.Success := DotNetMatch.Success; Matches.Insert(); Index += 1; end; if Matches.FindFirst() then; end; local procedure InsertGroups(var GroupsRec: Record Groups) var DotNetGroup: DotNet Group; Index: Integer; begin GroupsRec.DeleteAll(); Index := 0; foreach DotNetGroup in DotNetGroupCollection do begin GroupsRec.Init(); GroupsRec.GroupIndex := Index; GroupsRec.Index := DotNetGroup.Index; GroupsRec.Name := DotNetGroup.Name; GroupsRec.InsertValue(DotNetGroup.Value); GroupsRec.Length := DotNetGroup.Length; GroupsRec.Success := DotNetGroup.Success; GroupsRec.Insert(); Index += 1; end; if GroupsRec.FindFirst() then; end; local procedure InsertCaptures(var CapturesRec: Record Captures) var DotNetCapture: DotNet Capture; Index: Integer; begin CapturesRec.DeleteAll(); Index := 0; foreach DotNetCapture in DotNetCaptureCollection do begin CapturesRec.Init(); CapturesRec.CaptureIndex := Index; CapturesRec.Index := DotNetCapture.Index; CapturesRec.Length := DotNetCapture.Length; CapturesRec.InsertValue(DotNetCapture.Value); CapturesRec.Insert(); Index += 1; end; if CapturesRec.FindFirst() then; end;}