Esempio n. 1
0
        public void Match(ref PcreRefMatch match, ReadOnlySpan <char> subject, PcreMatchSettings settings, int startIndex, uint additionalOptions)
        {
            var input = new Native.match_input();

            settings.FillMatchInput(ref input);

            Native.match_result result;
            CalloutInterop.CalloutInteropInfo calloutInterop;

            fixed(char *pSubject = subject)
            fixed(uint *pOVec = match.OutputVector)
            {
                input.code               = _code;
                input.subject            = pSubject;
                input.subject_length     = (uint)subject.Length;
                input.output_vector      = pOVec;
                input.start_index        = (uint)startIndex;
                input.additional_options = additionalOptions;

                if (input.subject == (char *)0 && input.subject_length == 0)
                {
                    input.subject = (char *)1; // PCRE doesn't like null subjects, even if the length is zero
                }
                CalloutInterop.Prepare(subject, this, ref input, out calloutInterop, settings.RefCallout);

                Native.match(ref input, out result);
            }

            AfterMatch(result, ref calloutInterop);

            match.Update(subject, result);
        }
Esempio n. 2
0
        public PcreMatch Match(string subject, PcreMatchSettings settings, int startIndex, uint additionalOptions)
        {
            var input = new Native.match_input();

            settings.FillMatchInput(ref input);

            var oVector = new uint[OutputVectorSize];

            Native.match_result result;
            CalloutInterop.CalloutInteropInfo calloutInterop;

            fixed(char *pSubject = subject)
            fixed(uint *pOVec = &oVector[0])
            {
                input.code               = _code;
                input.subject            = pSubject;
                input.subject_length     = (uint)subject.Length;
                input.output_vector      = pOVec;
                input.start_index        = (uint)startIndex;
                input.additional_options = additionalOptions;

                CalloutInterop.Prepare(subject, this, ref input, out calloutInterop, settings.Callout);

                Native.match(ref input, out result);
            }

            AfterMatch(result, ref calloutInterop);

            return(new PcreMatch(subject, this, ref result, oVector));
        }
Esempio n. 3
0
        public PcreMatch Match(string subject, PcreMatchSettings settings)
        {
            var input = new Native.match_input();

            settings.FillMatchInput(ref input);
            return(Match(subject, settings, ref input));
        }
Esempio n. 4
0
        public PcreMatch Match(string subject, PcreMatchSettings settings, int startIndex, uint additionalOptions)
        {
            var input = new Native.match_input();

            settings.FillMatchInput(ref input);
            input.start_index        = (uint)startIndex;
            input.additional_options = additionalOptions;
            return(Match(subject, settings, ref input));
        }
        internal MatchContext CreateMatchContext(string subject)
        {
            var context = new MatchContext
            {
                Subject           = subject,
                StartIndex        = StartIndex,
                AdditionalOptions = ((PcreMatchOptions)AdditionalOptions).ToPatternOptions(),
                CalloutHandler    = PcreMatchSettings.WrapCallout(OnCallout),
                DfaMaxResults     = (AdditionalOptions & PcreDfaMatchOptions.ShortestMatch) != 0 ? 1 : MaxResults,
                DfaWorkspaceSize  = WorkspaceSize
            };

            return(context);
        }
Esempio n. 6
0
        private PcreMatch Match(string subject, PcreMatchSettings settings, ref Native.match_input input)
        {
            var oVector = new uint[2 * (CaptureCount + 1)];

            Native.match_result result;
            CalloutInterop.CalloutInteropInfo calloutInterop;

            fixed(char *pSubject = subject)
            fixed(uint *pOVec = &oVector[0])
            {
                input.code           = _code;
                input.subject        = pSubject;
                input.subject_length = (uint)subject.Length;
                input.output_vector  = pOVec;

                CalloutInterop.Prepare(subject, this, ref input, out calloutInterop, settings.Callout);

                Native.match(ref input, out result);
            }

            AfterMatch(result, ref calloutInterop);

            return(new PcreMatch(subject, this, ref result, oVector));
        }
Esempio n. 7
0
        private static void RunTest(TestInput testInput, TestOutput expectedResult, PcreOptions options, bool span)
        {
            var pattern = testInput.Pattern;

            if (pattern.NotSupported)
            {
                Assert.Inconclusive("Feature not supported");
            }

            options = (options | pattern.PatternOptions) & ~pattern.ResetOptionBits;

            PcreRegex regex;

            try
            {
                regex = new PcreRegex(pattern.Pattern, options);
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains(@"\C is not allowed in a lookbehind assertion"))
                {
                    Assert.Inconclusive(ex.Message);
                }

                throw;
            }

            var jitStack = pattern.JitStack != 0 && (options & PcreOptions.Compiled) != 0
                ? new PcreJitStack(1, pattern.JitStack)
                : null;

            using (jitStack)
            {
                for (var line = 0; line < testInput.SubjectLines.Count; ++line)
                {
                    var subject  = testInput.SubjectLines[line];
                    var expected = expectedResult.ExpectedResults[line];

                    Assert.That(expected.SubjectLine, Is.EqualTo(subject));

                    if (!pattern.SubjectLiteral)
                    {
                        subject = pattern.HexEncoding
                            ? subject.UnescapeBinarySubject()
                            : subject.UnescapeSubject();
                    }

                    var matchSettings = new PcreMatchSettings
                    {
                        JitStack = jitStack
                    };

                    if (span)
                    {
                        var matchCount = 0;

                        foreach (var actualMatch in regex.Matches(subject.AsSpan(), matchSettings))
                        {
                            Assert.That(matchCount, Is.LessThan(expected.Matches.Count));

                            var expectedMatch = expected.Matches[matchCount];
                            ++matchCount;

                            CompareGroups(pattern, actualMatch, expectedMatch);

                            if (pattern.ExtractMarks)
                            {
                                CompareMark(actualMatch, expectedMatch);
                            }

                            if (pattern.GetRemainingString)
                            {
                                CompareRemainingString(actualMatch, expectedMatch);
                            }

                            if (!pattern.AllMatches)
                            {
                                break;
                            }
                        }

                        Assert.That(matchCount, Is.EqualTo(expected.Matches.Count));
                    }
                    else
                    {
                        var matches = regex
                                      .Matches(subject, matchSettings)
                                      .Take(pattern.AllMatches ? int.MaxValue : 1)
                                      .ToList();

                        Assert.That(matches.Count, Is.EqualTo(expected.Matches.Count));

                        for (var matchIndex = 0; matchIndex < matches.Count; ++matchIndex)
                        {
                            var actualMatch   = matches[matchIndex];
                            var expectedMatch = expected.Matches[matchIndex];

                            CompareGroups(pattern, actualMatch, expectedMatch);

                            if (pattern.ExtractMarks)
                            {
                                CompareMark(actualMatch, expectedMatch);
                            }

                            if (pattern.GetRemainingString)
                            {
                                CompareRemainingString(actualMatch, expectedMatch);
                            }
                        }
                    }
                }
            }
        }