Skip to content

Commit 5398afb

Browse files
authored
More spans (#723)
1 parent cce960b commit 5398afb

File tree

10 files changed

+76
-56
lines changed

10 files changed

+76
-56
lines changed

src/MarkdownSnippets/Extensions.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static void TrimEnd(this StringBuilder builder)
6060

6161
public static IReadOnlyList<T> ToReadonlyList<T>(this IEnumerable<T> value) => value.ToList();
6262

63-
public static int LineCount(this string input)
63+
public static int LineCount(this CharSpan input)
6464
{
6565
var count = 1;
6666
var len = input.Length;
@@ -85,7 +85,7 @@ public static int LineCount(this string input)
8585
return count;
8686
}
8787

88-
public static int LastIndexOfSequence(this string value, char c, int max)
88+
public static int LastIndexOfSequence(this CharSpan value, char c, int max)
8989
{
9090
var index = 0;
9191
while (true)
@@ -110,7 +110,7 @@ public static int LastIndexOfSequence(this string value, char c, int max)
110110
}
111111
}
112112

113-
public static string TrimBackCommentChars(this string input, int startIndex)
113+
public static CharSpan TrimBackCommentChars(this CharSpan input, int startIndex)
114114
{
115115
for (var index = input.Length - 1; index >= startIndex; index--)
116116
{
@@ -127,6 +127,16 @@ public static string TrimBackCommentChars(this string input, int startIndex)
127127
public static string[] Lines(this string value) =>
128128
value.Split(["\r\n", "\r", "\n"], StringSplitOptions.None);
129129

130-
public static bool IsWhiteSpace(this string target) =>
131-
string.IsNullOrWhiteSpace(target);
130+
public static bool IsWhiteSpace(this CharSpan target)
131+
{
132+
for (var i = 0; i < target.Length; i++)
133+
{
134+
if (!char.IsWhiteSpace(target[i]))
135+
{
136+
return false;
137+
}
138+
}
139+
140+
return true;
141+
}
132142
}

src/MarkdownSnippets/Processing/MarkdownProcessor.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,19 @@ Snippet FileToSnippet(string key, string file, string? path)
416416

417417
(string text, int lineCount) ReadNonStartEndLines(string file)
418418
{
419-
var cleanedLines = File.ReadAllLines(file)
420-
.Where(_ => !StartEndTester.IsStartOrEnd(_.TrimStart())).ToList();
421-
var text = string.Join(newLine, cleanedLines).Trim();
422-
return (text, cleanedLines.Count);
419+
using var reader = File.OpenText(file);
420+
var lines = new List<string>();
421+
while (reader.ReadLine() is { } line)
422+
{
423+
if (StartEndTester.IsStartOrEnd(line))
424+
{
425+
continue;
426+
}
427+
428+
lines.Add(line);
429+
}
430+
431+
var text = string.Join(newLine, lines).Trim();
432+
return (text, lines.Count);
423433
}
424434
}

src/MarkdownSnippets/Processing/SimpleSnippetMarkdownHandling.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ static void WriteSnippet(Action<string> appendLine, Snippet snippet)
2323
{
2424
appendLine($"```{snippet.Language} {snippet.ExpressiveCode}");
2525
}
26+
2627
appendLine(snippet.Value);
2728
appendLine("```");
2829
}
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(CharSpan line);

src/MarkdownSnippets/Reading/FileSnippetExtractor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public static string GetLanguageFromPath(string path)
124124
var extension = Path.GetExtension(path);
125125
// ReSharper disable once ConstantConditionalAccessQualifier
126126
var s = extension?.TrimStart('.');
127-
return s ?? string.Empty;
127+
return s?.ToLowerInvariant() ?? string.Empty;
128128
}
129129

130130
static IEnumerable<Snippet> GetSnippets(TextReader stringReader, string path, int maxWidth, string newLine)
@@ -207,7 +207,7 @@ static Snippet BuildSnippet(string path, LoopStack loopStack, string language, i
207207
key: loopState.Key,
208208
value: value,
209209
path: path,
210-
language: language.ToLowerInvariant(),
210+
language: language,
211211
expressiveCode: loopState.ExpressiveCode
212212
);
213213
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace MarkdownSnippets;
2+
3+
public interface IContent
4+
{
5+
string? Path { get; }
6+
}

src/MarkdownSnippets/Reading/LoopStack.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ public void AppendLine(string line)
1515

1616
public void Pop() => stack.Pop();
1717

18-
public void Push(EndFunc endFunc, string key, int startLine, int maxWidth, string newLine, string? expressiveCode)
18+
public void Push(EndFunc endFunc, CharSpan key, int startLine, int maxWidth, string newLine, CharSpan expressiveCode)
1919
{
20-
var state = new LoopState(key, endFunc, startLine, maxWidth, newLine, expressiveCode);
20+
var expressiveCodeString = expressiveCode.Length == 0 ? null : expressiveCode.ToString();
21+
22+
var state = new LoopState(key.ToString(), endFunc, startLine, maxWidth, newLine, expressiveCodeString);
2123
stack.Push(state);
2224
}
2325

src/MarkdownSnippets/Reading/LoopState.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ public void AppendLine(string line)
4040
builder.Append(newLine);
4141
}
4242
}
43+
4344
var paddingToRemove = line.LastIndexOfSequence(paddingChar, paddingLength);
4445

4546
var lineLength = line.Length - paddingToRemove;
4647
if (lineLength > maxWidth)
4748
{
48-
throw new LineTooLongException(line.Substring(paddingToRemove,lineLength));
49+
throw new LineTooLongException(line.Substring(paddingToRemove, lineLength));
4950
}
51+
5052
builder.Append(line, paddingToRemove, lineLength);
5153
if (line.Length == 0)
5254
{
@@ -58,7 +60,7 @@ public void AppendLine(string line)
5860
}
5961
}
6062

61-
void CheckWhiteSpace(string line, char whiteSpace)
63+
void CheckWhiteSpace(CharSpan line, char whiteSpace)
6264
{
6365
var c = line[0];
6466
if (c != whiteSpace)

src/MarkdownSnippets/Reading/Snippet.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,4 @@ public override string ToString() =>
139139
Error: {Error}
140140
141141
""";
142-
}
143-
144-
public interface IContent
145-
{
146-
string? Path { get; }
147142
}

src/MarkdownSnippets/Reading/StartEndTester.cs

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
static class StartEndTester
22
{
3-
internal static bool IsStartOrEnd(string trimmedLine) =>
4-
IsBeginSnippet(trimmedLine) ||
5-
IsEndSnippet(trimmedLine) ||
6-
IsStartRegion(trimmedLine) ||
7-
IsEndRegion(trimmedLine);
3+
internal static bool IsStartOrEnd(CharSpan line)
4+
{
5+
var trimmedLine = line.Trim();
6+
return IsBeginSnippet(trimmedLine) ||
7+
IsEndSnippet(trimmedLine) ||
8+
IsStartRegion(trimmedLine) ||
9+
IsEndRegion(trimmedLine);
10+
}
811

912
internal static bool IsStart(
10-
string trimmedLine,
11-
string path,
12-
[NotNullWhen(true)] out string? currentKey,
13+
CharSpan trimmedLine,
14+
CharSpan path,
15+
out CharSpan currentKey,
1316
[NotNullWhen(true)] out EndFunc? endFunc,
14-
out string? expressiveCode)
17+
out CharSpan expressiveCode)
1518
{
1619
if (IsBeginSnippet(trimmedLine, path, out currentKey, out expressiveCode))
1720
{
@@ -35,18 +38,18 @@ internal static bool IsStart(
3538

3639
static EndFunc throwFunc = _ => throw new("Do not use out func");
3740

38-
static bool IsEndRegion(string line) =>
41+
static bool IsEndRegion(CharSpan line) =>
3942
line.StartsWith("#endregion", StringComparison.Ordinal);
4043

41-
static bool IsEndSnippet(string line) =>
44+
static bool IsEndSnippet(CharSpan line) =>
4245
IndexOf(line, "end-snippet") >= 0;
4346

44-
static bool IsStartRegion(string line) =>
47+
static bool IsStartRegion(CharSpan line) =>
4548
line.StartsWith("#region ", StringComparison.Ordinal);
4649

4750
internal static bool IsStartRegion(
48-
string line,
49-
[NotNullWhen(true)] out string? key)
51+
CharSpan line,
52+
out CharSpan key)
5053
{
5154
if (!line.StartsWith("#region ", StringComparison.Ordinal))
5255
{
@@ -56,33 +59,28 @@ internal static bool IsStartRegion(
5659

5760
var substring = line[8..].Trim();
5861

59-
if (substring.Contains(' '))
62+
if (substring.Contains(' ') ||
63+
!KeyValidator.IsValidKey(substring))
6064
{
6165
key = null;
6266
return false;
6367
}
6468

65-
if (!KeyValidator.IsValidKey(substring.AsSpan()))
66-
{
67-
key = null;
68-
return false;
69-
}
70-
71-
key = substring;
69+
key = substring.ToString();
7270
return true;
7371
}
7472

75-
static bool IsBeginSnippet(string line)
73+
static bool IsBeginSnippet(CharSpan line)
7674
{
7775
var startIndex = IndexOf(line, "begin-snippet: ");
7876
return startIndex != -1;
7977
}
8078

8179
internal static bool IsBeginSnippet(
82-
string line,
83-
string path,
84-
[NotNullWhen(true)] out string? key,
85-
out string? expressiveCode)
80+
CharSpan line,
81+
CharSpan path,
82+
out CharSpan key,
83+
out CharSpan expressiveCode)
8684
{
8785
expressiveCode = null;
8886
var beginSnippetIndex = IndexOf(line, "begin-snippet: ");
@@ -117,11 +115,7 @@ internal static bool IsBeginSnippet(
117115
""");
118116
}
119117

120-
expressiveCode = substring[(startArgs +1)..^1].Trim();
121-
if (expressiveCode.Length == 0)
122-
{
123-
expressiveCode = null;
124-
}
118+
expressiveCode = substring[(startArgs + 1)..^1].Trim();
125119
}
126120

127121
if (key.Length == 0)
@@ -134,7 +128,7 @@ No Key could be derived.
134128
""");
135129
}
136130

137-
if (KeyValidator.IsValidKey(key.AsSpan()))
131+
if (KeyValidator.IsValidKey(key))
138132
{
139133
return true;
140134
}
@@ -148,14 +142,14 @@ Key cannot contain whitespace or start/end with symbols.
148142
""");
149143
}
150144

151-
static int IndexOf(string line, string value)
145+
static int IndexOf(CharSpan line, CharSpan value)
152146
{
153147
if (value.Length > line.Length)
154148
{
155149
return -1;
156150
}
157151

158152
var charactersToScan = Math.Min(line.Length, value.Length + 10);
159-
return line.IndexOf(value, startIndex: 0, count: charactersToScan, StringComparison.Ordinal);
153+
return line[..charactersToScan].IndexOf(value, StringComparison.Ordinal);
160154
}
161155
}

0 commit comments

Comments
 (0)