提交 8814c346 编写于 作者: C Cyrus Najmabadi

Speed up regex comment detector pattern.

上级 9e786147
......@@ -43,7 +43,7 @@ internal sealed class RegexPatternDetector
/// Option names are the values from the <see cref="RegexOptions"/> enum.
/// </summary>
private static readonly Regex s_languageCommentDetector =
new Regex(@"\blang(uage)?\s*=\s*regex(p)?\b((\s*,\s*)(?<option>[a-zA-Z]+))*",
new Regex(@"^((//)|(')|(/\*))\s*lang(uage)?\s*=\s*regex(p)?\b((\s*,\s*)(?<option>[a-zA-Z]+))*",
RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static readonly Dictionary<string, RegexOptions> s_nameToOption =
......
......@@ -10,9 +10,9 @@ namespace Microsoft.CodeAnalysis.UnitTests.EmbeddedLanguages.RegularExpressions.
{
public class RegexPatternDetectorTests
{
private void Match(string value, RegexOptions? expectedOptions = null)
private void Match(string value, RegexOptions? expectedOptions = null, string prefix = "//")
{
var (matched, options) = RegexPatternDetector.TestAccessor.TryMatch(value);
var (matched, options) = RegexPatternDetector.TestAccessor.TryMatch(prefix + value);
Assert.True(matched);
if (expectedOptions != null)
......@@ -21,9 +21,9 @@ private void Match(string value, RegexOptions? expectedOptions = null)
}
}
private void NoMatch(string value)
private void NoMatch(string value, string prefix = "//")
{
var (matched, options) = RegexPatternDetector.TestAccessor.TryMatch(value);
var (matched, _) = RegexPatternDetector.TestAccessor.TryMatch(prefix + value);
Assert.False(matched);
}
......@@ -33,6 +33,18 @@ public void TestSimpleForm()
Match("lang=regex");
}
[Fact]
public void TestSimpleFormVB()
{
Match("' lang=regex", prefix: "");
}
[Fact]
public void TestSimpleFormCSharpMultiLine()
{
Match("/* lang=regex", prefix: "");
}
[Fact]
public void TestEndingInP()
{
......@@ -96,13 +108,13 @@ public void TestNotWithWordCharAtEnd()
[Fact]
public void TestWithNoNWordBeforeStart1()
{
Match(":lang=regex");
NoMatch(":lang=regex");
}
[Fact]
public void TestWithNoNWordBeforeStart2()
{
Match(": lang=regex");
NoMatch(": lang=regex");
}
[Fact]
......@@ -152,5 +164,11 @@ public void TestInvalidOption2()
{
NoMatch("lang=regex,ecmascript,ignore");
}
[Fact]
public void TestNotOnDocComment()
{
NoMatch("/// lang=regex,ignore", prefix: "");
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册