Skip to content

Commit cb6bd18

Browse files
committed
.
1 parent 5704d9c commit cb6bd18

File tree

3 files changed

+53
-37
lines changed

3 files changed

+53
-37
lines changed

src/MarkdownSnippets/NewLineConfigReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ static string GetGitAttributePattern(string line)
189189
return EolValueToNewLine(eol);
190190
}
191191

192-
static bool EditorConfigSectionMatchesMd(ReadOnlySpan<char> section)
192+
static bool EditorConfigSectionMatchesMd(CharSpan section)
193193
{
194194
// Handle patterns like *.md, *.{md,txt}, etc.
195195
if (section.Length == 0 || section[0] != '*')

src/MarkdownSnippets/Processing/SnippetKey.cs

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ static class SnippetKey
22
{
33
public static bool ExtractStartCommentSnippet(Line line, [NotNullWhen(true)] out string? key)
44
{
5-
var lineCurrent = line.Current.TrimStart();
5+
var lineCurrent = line.Current.AsSpan().TrimStart();
66
if (!IsStartCommentSnippetLine(lineCurrent))
77
{
88
key = null;
99
return false;
1010
}
1111

1212
var substring = lineCurrent[14..];
13-
var indexOf = substring.IndexOf("-->");
13+
var indexOf = substring.IndexOf("-->", StringComparison.Ordinal);
1414
if (indexOf < 0)
1515
{
1616
throw new SnippetException($"Could not find closing '-->' in: {line.Original}. Path: {line.Path}. Line: {line.LineNumber}");
1717
}
1818

19-
key = substring[..indexOf].Trim();
19+
key = substring[..indexOf].Trim().ToString();
2020
return true;
2121
}
2222

@@ -25,7 +25,7 @@ public static bool ExtractStartCommentWebSnippet(Line line, [NotNullWhen(true)]
2525

2626
public static bool ExtractStartCommentWebSnippet(Line line, [NotNullWhen(true)] out string? url, [NotNullWhen(true)] out string? snippetKey, out string? viewUrl)
2727
{
28-
var lineCurrent = line.Current.TrimStart();
28+
var lineCurrent = line.Current.AsSpan().TrimStart();
2929
if (!IsStartCommentWebSnippetLine(lineCurrent))
3030
{
3131
url = null;
@@ -35,7 +35,7 @@ public static bool ExtractStartCommentWebSnippet(Line line, [NotNullWhen(true)]
3535
}
3636

3737
var substring = lineCurrent[18..]; // after "<!-- web-snippet: "
38-
var indexOf = substring.IndexOf("-->");
38+
var indexOf = substring.IndexOf("-->", StringComparison.Ordinal);
3939
if (indexOf < 0)
4040
{
4141
throw new SnippetException($"Could not find closing '-->' in: {line.Original}. Path: {line.Path}. Line: {line.LineNumber}");
@@ -44,12 +44,14 @@ public static bool ExtractStartCommentWebSnippet(Line line, [NotNullWhen(true)]
4444
var value = substring[..indexOf].Trim();
4545

4646
// Check for optional second URL separated by whitespace
47-
var parts = value.Split([' ', '\t'], StringSplitOptions.RemoveEmptyEntries);
48-
string firstPart;
49-
if (parts.Length >= 2)
47+
var firstSpaceIndex = value.IndexOfAny([' ', '\t']);
48+
CharSpan firstPart;
49+
if (firstSpaceIndex >= 0)
5050
{
51-
firstPart = parts[0];
52-
viewUrl = parts[1];
51+
firstPart = value[..firstSpaceIndex];
52+
var secondPart = value[(firstSpaceIndex + 1)..].TrimStart();
53+
var nextSpace = secondPart.IndexOfAny([' ', '\t']);
54+
viewUrl = (nextSpace >= 0 ? secondPart[..nextSpace] : secondPart).ToString();
5355
}
5456
else
5557
{
@@ -65,27 +67,27 @@ public static bool ExtractStartCommentWebSnippet(Line line, [NotNullWhen(true)]
6567
viewUrl = null;
6668
return false;
6769
}
68-
url = firstPart[..hashIndex];
69-
snippetKey = firstPart[(hashIndex + 1)..];
70+
url = firstPart[..hashIndex].ToString();
71+
snippetKey = firstPart[(hashIndex + 1)..].ToString();
7072
return true;
7173
}
7274

7375
public static bool ExtractSnippet(Line line, [NotNullWhen(true)] out string? key)
7476
{
75-
var lineCurrent = line.Current.TrimStart();
77+
var lineCurrent = line.Current.AsSpan().TrimStart();
7678
if (!IsSnippetLine(lineCurrent))
7779
{
7880
key = null;
7981
return false;
8082
}
8183

82-
key = lineCurrent[8..]
83-
.Trim();
84-
if (string.IsNullOrWhiteSpace(key))
84+
var keySpan = lineCurrent[8..].Trim();
85+
if (keySpan.IsWhiteSpace())
8586
{
8687
throw new SnippetException($"Could not parse snippet from: {line.Original}. Path: {line.Path}. Line: {line.LineNumber}");
8788
}
8889

90+
key = keySpan.ToString();
8991
return true;
9092
}
9193

@@ -94,7 +96,7 @@ public static bool ExtractWebSnippet(Line line, [NotNullWhen(true)] out string?
9496

9597
public static bool ExtractWebSnippet(Line line, [NotNullWhen(true)] out string? url, [NotNullWhen(true)] out string? snippetKey, out string? viewUrl)
9698
{
97-
var lineCurrent = line.Current.TrimStart();
99+
var lineCurrent = line.Current.AsSpan().TrimStart();
98100
if (!IsWebSnippetLine(lineCurrent))
99101
{
100102
url = null;
@@ -105,12 +107,14 @@ public static bool ExtractWebSnippet(Line line, [NotNullWhen(true)] out string?
105107
var value = lineCurrent[12..].Trim(); // after 'web-snippet:'
106108

107109
// Check for optional second URL separated by whitespace
108-
var parts = value.Split([' ', '\t'], StringSplitOptions.RemoveEmptyEntries);
109-
string firstPart;
110-
if (parts.Length >= 2)
110+
var firstSpaceIndex = value.IndexOfAny([' ', '\t']);
111+
CharSpan firstPart;
112+
if (firstSpaceIndex >= 0)
111113
{
112-
firstPart = parts[0];
113-
viewUrl = parts[1];
114+
firstPart = value[..firstSpaceIndex];
115+
var secondPart = value[(firstSpaceIndex + 1)..].TrimStart();
116+
var nextSpace = secondPart.IndexOfAny([' ', '\t']);
117+
viewUrl = (nextSpace >= 0 ? secondPart[..nextSpace] : secondPart).ToString();
114118
}
115119
else
116120
{
@@ -126,20 +130,32 @@ public static bool ExtractWebSnippet(Line line, [NotNullWhen(true)] out string?
126130
viewUrl = null;
127131
return false;
128132
}
129-
url = firstPart[..hashIndex];
130-
snippetKey = firstPart[(hashIndex + 1)..];
133+
url = firstPart[..hashIndex].ToString();
134+
snippetKey = firstPart[(hashIndex + 1)..].ToString();
131135
return true;
132136
}
133137

134138
public static bool IsSnippetLine(string line) =>
139+
IsSnippetLine(line.AsSpan());
140+
141+
public static bool IsSnippetLine(CharSpan line) =>
135142
line.StartsWith("snippet:", StringComparison.OrdinalIgnoreCase);
136143

137144
public static bool IsStartCommentSnippetLine(string line) =>
145+
IsStartCommentSnippetLine(line.AsSpan());
146+
147+
public static bool IsStartCommentSnippetLine(CharSpan line) =>
138148
line.StartsWith("<!-- snippet:", StringComparison.OrdinalIgnoreCase);
139149

140150
public static bool IsWebSnippetLine(string line) =>
151+
IsWebSnippetLine(line.AsSpan());
152+
153+
public static bool IsWebSnippetLine(CharSpan line) =>
141154
line.StartsWith("web-snippet:", StringComparison.OrdinalIgnoreCase);
142155

143156
public static bool IsStartCommentWebSnippetLine(string line) =>
157+
IsStartCommentWebSnippetLine(line.AsSpan());
158+
159+
public static bool IsStartCommentWebSnippetLine(CharSpan line) =>
144160
line.StartsWith("<!-- web-snippet:", StringComparison.OrdinalIgnoreCase);
145-
}
161+
}

src/Tests/MarkdownProcessor/TocBuilderTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public Task EmptyHeading()
88
new("##", "", 0)
99
};
1010

11-
var buildToc = TocBuilder.BuildToc(lines, 1, new(), "\r");
11+
var buildToc = TocBuilder.BuildToc(lines, 1, [], "\r");
1212
Assert.DoesNotContain("\r\n", buildToc);
1313
return Verify(buildToc);
1414
}
@@ -22,7 +22,7 @@ public Task IgnoreTop()
2222
new("## Heading2", "", 0)
2323
};
2424

25-
return Verify(TocBuilder.BuildToc(lines, 1, new(), Environment.NewLine));
25+
return Verify(TocBuilder.BuildToc(lines, 1, [], Environment.NewLine));
2626
}
2727

2828
[Fact]
@@ -41,7 +41,7 @@ public Task StripMarkdown()
4141
new("## **bold** *italic* [Link](link)", "", 0)
4242
};
4343

44-
return Verify(TocBuilder.BuildToc(lines, 1, new(), Environment.NewLine));
44+
return Verify(TocBuilder.BuildToc(lines, 1, [], Environment.NewLine));
4545
}
4646

4747
[Fact]
@@ -67,7 +67,7 @@ public Task Nested()
6767
new("### Heading4", "", 0)
6868
};
6969

70-
return Verify(TocBuilder.BuildToc(lines, 2, new(), Environment.NewLine));
70+
return Verify(TocBuilder.BuildToc(lines, 2, [], Environment.NewLine));
7171
}
7272

7373
[Fact]
@@ -81,7 +81,7 @@ public Task Deep()
8181
new("##### Heading4", "", 0)
8282
};
8383

84-
return Verify(TocBuilder.BuildToc(lines, 10, new(), Environment.NewLine));
84+
return Verify(TocBuilder.BuildToc(lines, 10, [], Environment.NewLine));
8585
}
8686

8787
[Fact]
@@ -94,7 +94,7 @@ public Task StopAtLevel()
9494
new("#### Heading3", "", 0)
9595
};
9696

97-
return Verify(TocBuilder.BuildToc(lines, 2, new(), Environment.NewLine));
97+
return Verify(TocBuilder.BuildToc(lines, 2, [], Environment.NewLine));
9898
}
9999

100100
[Fact]
@@ -105,7 +105,7 @@ public Task Single()
105105
new("## Heading", "", 0)
106106
};
107107

108-
return Verify(TocBuilder.BuildToc(lines, 1, new(), Environment.NewLine));
108+
return Verify(TocBuilder.BuildToc(lines, 1, [], Environment.NewLine));
109109
}
110110

111111
[Fact]
@@ -116,7 +116,7 @@ public Task WithSpaces()
116116
new("## A B ", "", 0)
117117
};
118118

119-
return Verify(TocBuilder.BuildToc(lines, 1, new(), Environment.NewLine));
119+
return Verify(TocBuilder.BuildToc(lines, 1, [], Environment.NewLine));
120120
}
121121

122122
[Fact]
@@ -129,7 +129,7 @@ public Task DuplicateNested()
129129
new("#### Heading", "", 0)
130130
};
131131

132-
return Verify(TocBuilder.BuildToc(lines,4, new(), Environment.NewLine));
132+
return Verify(TocBuilder.BuildToc(lines,4, [], Environment.NewLine));
133133
}
134134

135135
[Fact]
@@ -142,6 +142,6 @@ public Task Duplicates()
142142
new("## a", "", 0)
143143
};
144144

145-
return Verify(TocBuilder.BuildToc(lines, 1, new(), Environment.NewLine));
145+
return Verify(TocBuilder.BuildToc(lines, 1, [], Environment.NewLine));
146146
}
147-
}
147+
}

0 commit comments

Comments
 (0)