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); } } }
private PcreRegexSettings(PcreRegexSettings settings, bool readOnly) { _options = settings._options; _newLine = settings._newLine; _backslashR = settings._backslashR; _parensLimit = settings._parensLimit; _readOnly = readOnly; }
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); }
public static string Replace(string subject, string pattern, Func <PcreMatch, string> replacementFunc, PcreOptions options) => Replace(subject, pattern, replacementFunc, options, -1, 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);
public static bool IsMatch(string subject, string pattern, PcreOptions options) => IsMatch(subject, pattern, options, 0);
internal PcreRegexSettings(PcreOptions options) { _options = options; }
public static PatternOptions ToPatternOptions(this PcreOptions options) { return((PatternOptions)((long)options & 0xFFFFFFFF) | PatternOptions.Utf); }
public PcreRegex(string pattern, PcreOptions options) : this(pattern, options, PcreStudyOptions.NONE) { }
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);
public PcreMatch Match(string input, int beginning, PcreOptions options) { return Match(input, beginning, -1, options); }
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); }
public bool IsMatch(string input, PcreOptions options) { return IsMatch(input, 0, options); }
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; }
public static PcreMatch Match(string subject, string pattern, PcreOptions options) => Match(subject, pattern, options, 0);
public static bool IsMatch(string subject, string pattern, PcreOptions options, int startIndex) => new PcreRegex(pattern, options).IsMatch(subject, startIndex);
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(); }
public static string Replace(string subject, string pattern, string replacement, PcreOptions options, int count) { return(Replace(subject, pattern, replacement, options, count, 0)); }
public PcreMatch Match(string input, PcreOptions options) { return Match(input, 0, -1, options); }
public static IEnumerable <string> Split(string subject, string pattern, PcreOptions options) { return(Split(subject, pattern, options, PcreSplitOptions.None, -1, 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; }
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); }
public static PcreMatch Match(string subject, string pattern, PcreOptions options) { return(Match(subject, pattern, options, 0)); }
public PcreRegex(string pattern, PcreOptions options) : this(pattern, new PcreRegexSettings(options)) { }
public static IEnumerable <PcreMatch> Matches(string subject, string pattern, PcreOptions options) { return(Matches(subject, pattern, options, 0)); }
public PcreMatchCollection Matches(string input, PcreOptions options) { return Matches(input, 0, options); }
public static uint ToPatternOptions(this PcreOptions options) => (uint)((long)options & 0xFFFFFFFF) | PcreConstants.UTF;
public static PcreMatch Match(string subject, string pattern, PcreOptions options, int startIndex) { return(new PcreRegex(pattern, options).Match(subject, startIndex)); }
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(); }
public static IEnumerable <PcreMatch> Matches(string subject, string pattern, PcreOptions options, int startIndex) { return(new PcreRegex(pattern, options).Matches(subject, startIndex)); }
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); }
public static string Replace(string subject, string pattern, string replacement, PcreOptions options) => Replace(subject, pattern, replacement, options, -1, 0);
public string Replace(string input, PcreMatchEvaluator evaluator, int maxCount, PcreOptions options) { return Replace(input, evaluator, maxCount, 0, options); }
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); } } } } } }
public string Replace(string input, PcreMatchEvaluator evaluator, PcreOptions options) { return Replace(input, evaluator, -1, 0, options); }
public static string Replace(string subject, string pattern, Func <PcreMatch, string> replacementFunc, PcreOptions options, int count) { return(Replace(subject, pattern, replacementFunc, options, count, 0)); }
public string Replace(string input, string replacement, int maxCount, PcreOptions options) { return Replace(input, replacement, maxCount, 0, options); }
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)); }
public string Replace(string input, string replacement, PcreOptions options) { return Replace(input, replacement, -1, 0, options); }
public static IEnumerable <string> Split(string subject, string pattern, PcreOptions options, PcreSplitOptions splitOptions) => Split(subject, pattern, options, splitOptions, -1, 0);
public static IEnumerable <string> Split(string subject, string pattern, PcreOptions options, PcreSplitOptions splitOptions, int count) { return(Split(subject, pattern, options, splitOptions, count, 0)); }