Skip to content

Commit 9fe80c7

Browse files
authored
Refactor line removal logic in RemoveUntilMatch (#734)
Optimized the RemoveUntilMatch method to remove a range of lines up to and including the match, improving efficiency and readability. The exception is now only thrown if the match is not found.
1 parent 7640632 commit 9fe80c7

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/MarkdownSnippets/Processing/Lines.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,19 @@ public static void RemoveUntil(
77
string? path,
88
Line startLine)
99
{
10-
while (true)
10+
var endIndex = index;
11+
while (endIndex < lines.Count)
1112
{
12-
if (index == lines.Count)
13+
if (lines[endIndex].Current.Contains(match))
1314
{
14-
throw new MarkdownProcessingException($"Expected to find `{match}`.", path, startLine.LineNumber);
15+
lines.RemoveRange(index, endIndex - index + 1);
16+
return;
1517
}
1618

17-
var lineCurrent = lines[index].Current;
18-
var shouldExit = lineCurrent.Contains(match);
19-
if (shouldExit)
20-
{
21-
lines.RemoveAt(index);
22-
break;
23-
}
24-
25-
lines.RemoveAt(index);
19+
endIndex++;
2620
}
21+
22+
throw new MarkdownProcessingException($"Expected to find `{match}`.", path, startLine.LineNumber);
2723
}
2824

2925
public static IEnumerable<Line> ReadAllLines(TextReader textReader, string? path)

0 commit comments

Comments
 (0)