public OperationResult ScopedRegexOperationDelegate()

in DevSkim-DotNet/Microsoft.DevSkim/ScopedRegexOperation.cs [22:68]


        public OperationResult ScopedRegexOperationDelegate(Clause c, object? state1, object? state2, IEnumerable<ClauseCapture>? captures)
        {
            if (state1 is TextContainer tc && c is ScopedRegexClause src)
            {
                var regexOpts = RegexOptions.Compiled;
                if (src.Arguments.Contains("i"))
                {
                    regexOpts |= RegexOptions.IgnoreCase;
                }
                if (src.Arguments.Contains("m"))
                {
                    regexOpts |= RegexOptions.Multiline;
                }
                var boundaries = new List<Boundary>();
                var target = tc.Target;
                if (Analyzer != null)
                {
                    foreach (var pattern in src.Data.Select(x => regexEngine.StringToRegex(x, regexOpts)))
                    {
                        if (pattern is Regex r)
                        {
                            var matches = r.Matches(target);
                            foreach (var match in matches)
                            {
                                if (match is Match m)
                                {
                                    Boundary translatedBoundary = new Boundary()
                                    {
                                        Length = m.Length,
                                        Index = m.Index + tc.GetLineBoundary(tc.LineNumber).Index
                                    };
                                    // Should return only scoped matches
                                    if (tc.ScopeMatch(src.Scopes, translatedBoundary))
                                    {
                                        boundaries.Add(translatedBoundary);
                                    }
                                }
                            }
                        }
                    }
                    
                    var result = src.Invert ? boundaries.Count == 0 : boundaries.Count > 0;
                    return new OperationResult(result, result && src.Capture ? new TypedClauseCapture<List<Boundary>>(c, boundaries, state1) : null);
                }
            }
            return new OperationResult(false, null);
        }