Skip to content

Commit 9876521

Browse files
authored
Remove regex from line parsing (#722)
* . * . * Update Extensions.cs
1 parent 86e6bb7 commit 9876521

File tree

5 files changed

+46
-39
lines changed

5 files changed

+46
-39
lines changed

src/MarkdownSnippets/Extensions.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,6 @@ public static string TrimBackCommentChars(this string input, int startIndex)
124124
return string.Empty;
125125
}
126126

127-
public static string[] SplitBySpace(this string value) =>
128-
value.Split(' ', StringSplitOptions.RemoveEmptyEntries);
129-
130127
public static string[] Lines(this string value) =>
131128
value.Split(["\r\n", "\r", "\n"], StringSplitOptions.None);
132129

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
delegate bool EndFunc(string line);
1+
delegate bool EndFunc(string line);

src/MarkdownSnippets/Reading/ExpressiveCode.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/MarkdownSnippets/Reading/StartEndTester.cs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
static class StartEndTester
1+
static class StartEndTester
22
{
33
internal static bool IsStartOrEnd(string trimmedLine) =>
44
IsBeginSnippet(trimmedLine) ||
@@ -96,37 +96,44 @@ internal static bool IsBeginSnippet(
9696
var substring = line
9797
.TrimBackCommentChars(startIndex);
9898

99-
var match = ExpressiveCode.Pattern.Match(substring);
100-
101-
if (match.Length == 0)
99+
var startArgs = substring.IndexOf('(');
100+
if (startArgs == -1)
102101
{
103-
throw new SnippetReadingException(
104-
$"""
105-
No Key could be derived.
106-
Path: {path}
107-
Line: '{line}'
108-
""");
102+
key = substring.Trim();
103+
}
104+
else
105+
{
106+
substring = substring.Trim();
107+
key = substring[..startArgs].Trim();
108+
109+
if (!substring.EndsWith(')'))
110+
{
111+
throw new SnippetReadingException(
112+
$"""
113+
ExpressiveCode must end with ')`.
114+
Key: {key}
115+
Path: {path}
116+
Line: {line}
117+
""");
118+
}
119+
120+
expressiveCode = substring[(startArgs +1)..^1].Trim();
121+
if (expressiveCode.Length == 0)
122+
{
123+
expressiveCode = null;
124+
}
109125
}
110126

111-
var partOne = match.Groups[1].Value;
112-
var split = partOne.SplitBySpace();
113-
if (split.Length != 1)
127+
if (key.Length == 0)
114128
{
115129
throw new SnippetReadingException(
116130
$"""
117-
Too many parts.
131+
No Key could be derived.
118132
Path: {path}
119133
Line: '{line}'
120134
""");
121135
}
122136

123-
key = split[0];
124-
expressiveCode = match.Groups[2].Value;
125-
if (expressiveCode.Length == 0)
126-
{
127-
expressiveCode = null;
128-
}
129-
130137
if (KeyValidator.IsValidKey(key.AsSpan()))
131138
{
132139
return true;

src/Tests/StartEndTester_IsBeginSnippetTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,21 @@ public void CanExtractWithExpressiveCodeWithCsharpComment()
9999
Assert.Equal("CodeKey", key);
100100
Assert.Equal("""title="Program.cs" {1-3}""", expressive);
101101
}
102+
[Fact]
103+
public void CanExtractWithExpressiveCodeWithHtmlSnippetTrailingWhitespace()
104+
{
105+
var isBeginSnippet = StartEndTester.IsBeginSnippet("""<!--begin-snippet: CodeKey(title="Program.cs" {1-3}) -->""", "file", out var key, out var block);
106+
Assert.True(isBeginSnippet);
107+
Assert.Equal("CodeKey", key);
108+
Assert.Equal("""title="Program.cs" {1-3}""", block);
109+
}
110+
111+
[Fact]
112+
public void CanExtractWithExpressiveCodeWithCsharpCommentTrailingWhitespace()
113+
{
114+
var isBeginSnippet = StartEndTester.IsBeginSnippet("""/*begin-snippet: CodeKey(title="Program.cs" {1-3}) */""", "file", out var key, out var expressive);
115+
Assert.True(isBeginSnippet);
116+
Assert.Equal("CodeKey", key);
117+
Assert.Equal("""title="Program.cs" {1-3}""", expressive);
118+
}
102119
}

0 commit comments

Comments
 (0)