public FlexMatch Match(FlexString input, string pattern, RegexOptions options = RegexOptions.None, TimeSpan timeout = default, string captureGroup = null) { // Note: Instance Regex.Match has no timeout overload. Regex regex = GetOrCreateRegex(pattern, options); return(DotNetRegex.ToFlex(regex.Match(input), captureGroup)); }
private void AnalyzeCommand(IRegex engine) { var disabledSkimmers = new HashSet <string>(); var testLogger = new TestLogger(); // Acquire skimmers for searchers ISet <Skimmer <AnalyzeContext> > skimmers = PatternMatcher.AnalyzeCommand.CreateSkimmersFromDefinitionsFiles( fileSystemMock.Object, new string[] { searchDefinitionsPath }, engine); string scanTargetFileName = Path.Combine(@"C:\", Guid.NewGuid().ToString() + ".test"); FlexString fileContents = "bar foo foo"; var context = new AnalyzeContext() { TargetUri = new Uri(scanTargetFileName, UriKind.RelativeOrAbsolute), FileContents = fileContents, Logger = testLogger }; IEnumerable <Skimmer <AnalyzeContext> > applicableSkimmers = PatternMatcher.AnalyzeCommand.DetermineApplicabilityForTargetHelper(context, skimmers, disabledSkimmers); PatternMatcher.AnalyzeCommand.AnalyzeTargetHelper(context, applicableSkimmers, disabledSkimmers); }
private FlexMatch ToFlex(Match2 match, FlexString input, ref int lastUtf8Index, ref int lastUtf16Index) { if (match.Index == -1) { return(new FlexMatch() { Success = false, Index = -1, Length = -1, Value = null }); } // Get the value using the UTF-8 string and indices String8 value = ((String8)input).Substring(match.Index, match.Length); // Map the UTF-8 index to UTF-16 int mappedIndex = String8.Utf8ToUtf16(match.Index, input, lastUtf8Index, lastUtf16Index); lastUtf8Index = match.Index; lastUtf16Index = mappedIndex; // Map the length to UTF-16 int mappedEnd = String8.Utf8ToUtf16(match.Index + match.Length, input, lastUtf8Index, lastUtf16Index); int mappedLength = mappedEnd - mappedIndex; // Return the UTF-16 indices but the UTF-8 derived value return(new FlexMatch() { Success = true, Index = mappedIndex, Length = mappedLength, Value = value }); }
public FlexMatch Match(FlexString input, string pattern, RegexOptions options = RegexOptions.None, TimeSpan timeout = default, string captureGroup = null) { int lastUtf8Index = 0; int lastUtf16Index = 0; return(ToFlex(Regex2.Match(input, pattern, options), input, ref lastUtf8Index, ref lastUtf16Index)); }
public bool IsMatch(FlexString input, string pattern, RegexOptions options = RegexOptions.None, TimeSpan timeout = default, string captureGroup = null) { // Note: Instance Regex.IsMatch has no timeout overload. Regex regex = GetOrCreateRegex(pattern, options); Match match = regex.Match(input); return(match.Success && (captureGroup == null || match.Groups[captureGroup].Success)); }
public FlexMatch Match(FlexString input, string pattern, RegexOptions options = RegexOptions.None, TimeSpan timeout = default, string captureGroup = null) { if (timeout == default) { timeout = DefaultTimeout; } return(ToFlex(Regex.Match(input, pattern, options, timeout), captureGroup)); }
public bool IsMatch(FlexString input, string pattern, RegexOptions options = RegexOptions.None, TimeSpan timeout = default, string captureGroup = null) { if (timeout == default) { timeout = DefaultTimeout; } Match match = Regex.Match(input, pattern, options, timeout); return(match.Success && (captureGroup == null || match.Groups[captureGroup].Success)); }
public IEnumerable <FlexMatch> Matches(FlexString input, string pattern, RegexOptions options = RegexOptions.None, TimeSpan timeout = default, string captureGroup = null) { if (timeout == default) { timeout = DefaultTimeout; } foreach (Match m in Regex.Matches(input, pattern, options, timeout)) { yield return(ToFlex(m, captureGroup)); } }
public void FlexString_Basics() { string sample; String8 sample8; FlexString sampleF, sampleF2; // Null handling Assert.True(FlexString.IsNullOrEmpty((FlexString)null)); Assert.True(FlexString.IsNullOrEmpty((string)null)); Assert.True(FlexString.IsNullOrEmpty("")); Assert.True(FlexString.IsNullOrEmpty(String8.Empty)); Assert.True(FlexString.IsNull((FlexString)null)); Assert.True(FlexString.IsNull((string)null)); sample = null; sampleF = sample; sample8 = sampleF; Assert.True(FlexString.IsNullOrEmpty(sampleF)); //Null check with null strings Assert.True(FlexString.IsNull(sample)); Assert.True(FlexString.IsNull(sampleF)); Assert.False(FlexString.IsNull(sample8)); // Implicit conversions sample = "sample"; sampleF = sample; sample8 = sampleF; Assert.Equal("sample", sample8.ToString()); //Null check with populated strings Assert.False(FlexString.IsNull(sample)); Assert.False(FlexString.IsNull(sampleF)); Assert.False(FlexString.IsNull(sample8)); sample8 = String8.ConvertExpensively("sample2"); sampleF = sample8; sample = sampleF; Assert.Equal("sample2", sample); // CompareTo Assert.Equal(0, sampleF.CompareTo(sample)); Assert.Equal(0, sampleF.CompareTo(sample8)); Assert.Equal(0, sampleF.CompareTo(sampleF)); sampleF = "sample3"; Assert.NotEqual(0, sampleF.CompareTo(sample)); Assert.NotEqual(0, sampleF.CompareTo(sample8)); sampleF2 = "sample3"; Assert.Equal(0, sampleF.CompareTo(sampleF2)); sampleF2 = "different"; Assert.NotEqual(0, sampleF.CompareTo(sampleF2)); }
public IEnumerable <FlexMatch> Matches(FlexString input, string pattern, RegexOptions options = RegexOptions.None, TimeSpan timeout = default, string captureGroup = null) { Timeout t = timeout == default ? Timeout.Unlimited : Timeout.Start(timeout); int lastUtf8Index = 0; int lastUtf16Index = 0; foreach (Match2 match in Regex2.Matches(input, pattern, options, t)) { yield return(ToFlex(match, input, ref lastUtf8Index, ref lastUtf16Index)); } }
public IEnumerable <FlexMatch> Matches(FlexString input, string pattern, RegexOptions options = RegexOptions.None, TimeSpan timeout = default, string captureGroup = null) { if (timeout == default) { timeout = DefaultTimeout; } var w = Stopwatch.StartNew(); Regex regex = GetOrCreateRegex(pattern, options); foreach (Match m in regex.Matches(input)) { yield return(DotNetRegex.ToFlex(m, captureGroup)); // Instance Regex.Matches has no overload; check timeout between matches // (MatchesCollection *is* lazily computed). if (w.Elapsed > timeout) { break; } } }
public bool IsMatch(FlexString input, string pattern, RegexOptions options = RegexOptions.None, TimeSpan timeout = default, string captureGroup = null) { return(Regex2.IsMatch(input, pattern, options)); }
private static void AnalyzeCommand(IRegex engine) { var definitions = new SearchDefinitions() { Definitions = new List <SearchDefinition>(new[] { new SearchDefinition() { Name = "MinimalRule", Id = "Test1002", Level = FailureLevel.Error, FileNameAllowRegex = "(?i)\\.test$", Message = "A problem occurred in '{0:scanTarget}'.", MatchExpressions = new List <MatchExpression>(new[] { new MatchExpression() { ContentsRegex = "foo", Fixes = new Dictionary <string, SimpleFix>() { { "convertToPublic", new SimpleFix() { Description = "Make class public.", Find = "foo", ReplaceWith = "bar" } } } } }) } }) }; string definitionsText = JsonConvert.SerializeObject(definitions); string searchDefinitionsPath = Guid.NewGuid().ToString(); var disabledSkimmers = new HashSet <string>(); var testLogger = new TestLogger(); var mockFileSystem = new Mock <IFileSystem>(); mockFileSystem.Setup(x => x.FileReadAllText(searchDefinitionsPath)).Returns(definitionsText); // Acquire skimmers for searchers ISet <Skimmer <AnalyzeContext> > skimmers = PatternMatcher.AnalyzeCommand.CreateSkimmersFromDefinitionsFiles( mockFileSystem.Object, new string[] { searchDefinitionsPath }, engine); string scanTargetFileName = Path.Combine(@"C:\", Guid.NewGuid().ToString() + ".test"); FlexString fileContents = "bar foo foo"; FlexString fixedFileContents = "bar bar bar"; var context = new AnalyzeContext() { TargetUri = new Uri(scanTargetFileName, UriKind.RelativeOrAbsolute), FileContents = fileContents, Logger = testLogger }; IEnumerable <Skimmer <AnalyzeContext> > applicableSkimmers = PatternMatcher.AnalyzeCommand.DetermineApplicabilityForTargetHelper(context, skimmers, disabledSkimmers); PatternMatcher.AnalyzeCommand.AnalyzeTargetHelper(context, applicableSkimmers, disabledSkimmers); testLogger.Results.Should().NotBeNull(); testLogger.Results.Count.Should().Be(2); foreach (Result result in testLogger.Results) { result.Level.Should().Be(FailureLevel.Error); } }