Example #1
0
        public unsafe PcreRegex(string pattern, PcreOptions options, PcreStudyOptions studyOptions)
        {
            _pattern = pattern;
            _options = options;
            _studyOptions = studyOptions;

            int errorCode = 0;
            IntPtr error;
            int errorOffset = 0;

            byte[] patternBytes = Encoding.UTF8.GetBytes(pattern);
            fixed (byte* bytes = patternBytes)
            {
                _code = PcreWrapper.pcre_compile2(bytes, (int)_options, ref errorCode, out error, ref errorOffset, IntPtr.Zero);
            }
            if (errorCode != 0)
            {
                throw new Exception(Marshal.PtrToStringAuto(error));
            }

            _extra = PcreWrapper.pcre_study(_code, (int)studyOptions, out error);
            if ((studyOptions & PcreStudyOptions.PCRE_STUDY_JIT_COMPILE) != 0)
            {
                if (_stack == IntPtr.Zero)
                {
                    _stack = PcreWrapper.pcre_jit_stack_alloc(64*1024, 16*1024*1024);
                }
            }
        }
Example #2
0
 private PcreRegexSettings(PcreRegexSettings settings, bool readOnly)
 {
     _options     = settings._options;
     _newLine     = settings._newLine;
     _backslashR  = settings._backslashR;
     _parensLimit = settings._parensLimit;
     _readOnly    = readOnly;
 }
Example #3
0
        public static JitCompileOptions ToJitCompileOptions(this PcreOptions options)
        {
            var jitOptions = JitCompileOptions.None;

            if ((options & PcreOptions.Compiled) != 0)
            {
                jitOptions |= JitCompileOptions.Complete;
            }

            if ((options & PcreOptions.CompiledPartial) != 0)
            {
                jitOptions |= JitCompileOptions.PartialSoft | JitCompileOptions.PartialHard;
            }

            return(jitOptions);
        }
Example #4
0
 public static string Replace(string subject, string pattern, Func <PcreMatch, string> replacementFunc, PcreOptions options)
 => Replace(subject, pattern, replacementFunc, options, -1, 0);
Example #5
0
 public static string Replace(string subject, string pattern, Func <PcreMatch, string> replacementFunc, PcreOptions options, int count, int startIndex)
 => new PcreRegex(pattern, options).Replace(subject, replacementFunc, count, startIndex);
Example #6
0
 public static bool IsMatch(string subject, string pattern, PcreOptions options)
 => IsMatch(subject, pattern, options, 0);
Example #7
0
 internal PcreRegexSettings(PcreOptions options)
 {
     _options = options;
 }
Example #8
0
 public static PatternOptions ToPatternOptions(this PcreOptions options)
 {
     return((PatternOptions)((long)options & 0xFFFFFFFF) | PatternOptions.Utf);
 }
Example #9
0
 public PcreRegex(string pattern, PcreOptions options)
     : this(pattern, options, PcreStudyOptions.NONE)
 {
 }
Example #10
0
 public static IEnumerable <string> Split(string subject, string pattern, PcreOptions options, PcreSplitOptions splitOptions, int count, int startIndex)
 => new PcreRegex(pattern, options).Split(subject, splitOptions, count, startIndex);
Example #11
0
 public PcreMatch Match(string input, int beginning, PcreOptions options)
 {
     return Match(input, beginning, -1, options);
 }
Example #12
0
        public PcreMatch Match(string input, int beginning, int length, PcreOptions options)
        {
            if (length != -1 || beginning > 0)
            {
                if (length == -1)
                {
                    length = input.Length - beginning;
                }

                input = input.Substring(beginning, length);
            }
            byte[] tgtBytes = Encoding.UTF8.GetBytes(input);
            return GetMatch(tgtBytes, 0, (int)_options);
        }
Example #13
0
 public bool IsMatch(string input, PcreOptions options)
 {
     return IsMatch(input, 0, options);
 }
Example #14
0
        public bool IsMatch(string input, int startat, PcreOptions options)
        {
            byte[] tgtBytes = Encoding.UTF8.GetBytes(input);

            PcreMatch match = GetMatch(tgtBytes, startat, (int)options);
            return match.Success;
        }
Example #15
0
 public static PcreMatch Match(string subject, string pattern, PcreOptions options)
 => Match(subject, pattern, options, 0);
Example #16
0
 public static bool IsMatch(string subject, string pattern, PcreOptions options, int startIndex)
 => new PcreRegex(pattern, options).IsMatch(subject, startIndex);
Example #17
0
        private static void RunTest(TestCase testCase, TestOutput expectedResult, PcreOptions options)
        {
            options = (options | testCase.Pattern.PatternOptions) & ~testCase.Pattern.ResetOptionBits;

            Console.WriteLine("Options: {0}", options);

            PcreRegex regex;

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

                throw;
            }

            for (var line = 0; line < testCase.SubjectLines.Count; ++line)
            {
                Console.WriteLine("  Subject #{0}: {1}", line, testCase.SubjectLines[line]);

                var subject  = testCase.SubjectLines[line];
                var expected = expectedResult.ExpectedResults[line];

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

                subject = testCase.Pattern.HexEncoding
                    ? subject.UnescapeBinarySubject()
                    : subject.UnescapeSubject();

                var matches = regex
                              .Matches(subject)
                              .Take(testCase.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(actualMatch, expectedMatch);

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

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

            Console.WriteLine("OK");
            Console.WriteLine();
        }
Example #18
0
 public static string Replace(string subject, string pattern, string replacement, PcreOptions options, int count)
 {
     return(Replace(subject, pattern, replacement, options, count, 0));
 }
Example #19
0
 public PcreMatch Match(string input, PcreOptions options)
 {
     return Match(input, 0, -1, options);
 }
Example #20
0
 public static IEnumerable <string> Split(string subject, string pattern, PcreOptions options)
 {
     return(Split(subject, pattern, options, PcreSplitOptions.None, -1, 0));
 }
Example #21
0
 public unsafe int MatchCount(string input, PcreOptions options)
 {
     int count = -1;
     int pos = 0;
     byte[] tgtBytes = Encoding.UTF8.GetBytes(input);
     int bytesLen = tgtBytes.Length;
     int opts = (int) options;
     fixed (byte* bytes = tgtBytes)
     {
         fixed (int* ovector = _ovectorArr)
         {
             int res;
             do
             {
                 res = Exec(bytes, bytesLen, pos, opts, ovector);
                 pos = _ovectorArr[1];
                 count++;
             } while (res > 0);
         }
     }
     return count;
 }
Example #22
0
        public PcreMatchCollection Matches(string input, int start, PcreOptions options)
        {
            byte[] tgtBytes = Encoding.UTF8.GetBytes(input);
            int opts = (int) options;

            return new PcreMatchCollection(this, tgtBytes, start, opts);
        }
Example #23
0
 public static PcreMatch Match(string subject, string pattern, PcreOptions options)
 {
     return(Match(subject, pattern, options, 0));
 }
Example #24
0
 public PcreRegex(string pattern, PcreOptions options)
     : this(pattern, new PcreRegexSettings(options))
 {
 }
Example #25
0
 public static IEnumerable <PcreMatch> Matches(string subject, string pattern, PcreOptions options)
 {
     return(Matches(subject, pattern, options, 0));
 }
Example #26
0
 public PcreMatchCollection Matches(string input, PcreOptions options)
 {
     return Matches(input, 0, options);
 }
Example #27
0
 public static uint ToPatternOptions(this PcreOptions options)
 => (uint)((long)options & 0xFFFFFFFF) | PcreConstants.UTF;
Example #28
0
 public static PcreMatch Match(string subject, string pattern, PcreOptions options, int startIndex)
 {
     return(new PcreRegex(pattern, options).Match(subject, startIndex));
 }
Example #29
0
        public string Replace(string input, PcreMatchEvaluator evaluator, int maxCount, int start, PcreOptions options)
        {
            var collection = Matches(input, start, options);
            int count;
            if (maxCount == -1)
            {
                count = collection.Count;
            }
            else
            {
                count = collection.GetMatch(maxCount) != null ? maxCount : collection.Count;
            }

            if (count == 0) return input;

            throw new NotImplementedException();
        }
Example #30
0
 public static IEnumerable <PcreMatch> Matches(string subject, string pattern, PcreOptions options, int startIndex)
 {
     return(new PcreRegex(pattern, options).Matches(subject, startIndex));
 }
Example #31
0
        public unsafe string Replace(string input, string replacement, int maxCount, int start, PcreOptions options)
        {
            byte[] srcBytes = Encoding.UTF8.GetBytes(input);
            int opts = (int)options;
            var collection = new PcreMatchCollection(this, srcBytes, start, opts);
            int count;
            if (maxCount == -1)
            {
                count = collection.Count;
            }
            else
            {
                count = collection.GetMatch(maxCount) != null ? maxCount : collection.Count;
            }

            if (count == 0) return input;

            int totalLen = 0;

            for (int i = 0; i < count; i++)
            {
                var currMatch = collection[i];
                totalLen += currMatch.Length;
            }

            byte[] replacementBytes = Encoding.UTF8.GetBytes(replacement);
            var replacementLen = replacementBytes.Length;
            var resultLen = srcBytes.Length - totalLen + replacementLen * count;

            var resultBytes = new byte[resultLen];

            int currIndex = 0;
            int currSrcPos = 0;
            int currDstPos = 0;

            fixed (byte* pinnedReplacementBytes = replacementBytes)
            {
                fixed (byte* pinnedSrcBytes = srcBytes)
                {
                    fixed (byte* pinnedResultBytes = resultBytes)
                    {
                        while (currIndex < count)
                        {
                            var currMatch = collection[currIndex];
                            var copyCount = currMatch._start - currSrcPos;
                            FastCopy(pinnedSrcBytes + currSrcPos, pinnedResultBytes + currDstPos, copyCount);
                            currSrcPos += copyCount;
                            currDstPos += copyCount;

                            FastCopy(pinnedReplacementBytes, pinnedResultBytes + currDstPos, replacementLen);
                            currSrcPos += currMatch.Length;
                            currDstPos += replacementLen;

                            currIndex++;
                        }
                        FastCopy(pinnedSrcBytes + currSrcPos, pinnedResultBytes + currDstPos, srcBytes.Length - currSrcPos);
                    }
                }
            }

            return Encoding.UTF8.GetString(resultBytes, 0, resultBytes.Length);
        }
Example #32
0
 public static string Replace(string subject, string pattern, string replacement, PcreOptions options)
 => Replace(subject, pattern, replacement, options, -1, 0);
Example #33
0
 public string Replace(string input, PcreMatchEvaluator evaluator, int maxCount, PcreOptions options)
 {
     return Replace(input, evaluator, maxCount, 0, options);
 }
Example #34
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);
                            }
                        }
                    }
                }
            }
        }
Example #35
0
 public string Replace(string input, PcreMatchEvaluator evaluator, PcreOptions options)
 {
     return Replace(input, evaluator, -1, 0, options);
 }
Example #36
0
 public static string Replace(string subject, string pattern, Func <PcreMatch, string> replacementFunc, PcreOptions options, int count)
 {
     return(Replace(subject, pattern, replacementFunc, options, count, 0));
 }
Example #37
0
 public string Replace(string input, string replacement, int maxCount, PcreOptions options)
 {
     return Replace(input, replacement, maxCount, 0, options);
 }
Example #38
0
 public static string Replace(string subject, string pattern, string replacement, PcreOptions options, int count, int startIndex)
 {
     return(new PcreRegex(pattern, options).Replace(subject, replacement, count, startIndex));
 }
Example #39
0
 public string Replace(string input, string replacement, PcreOptions options)
 {
     return Replace(input, replacement, -1, 0, options);
 }
Example #40
0
 public static IEnumerable <string> Split(string subject, string pattern, PcreOptions options, PcreSplitOptions splitOptions)
 => Split(subject, pattern, options, splitOptions, -1, 0);
Example #41
0
 public static IEnumerable <string> Split(string subject, string pattern, PcreOptions options, PcreSplitOptions splitOptions, int count)
 {
     return(Split(subject, pattern, options, splitOptions, count, 0));
 }