Skip to content

Commit 6fa29cb

Browse files
bergmeisterChristoph Bergmeisterrjmholt
authored
Performance: Eliminate Regex overhead in AvoidTrailingWhitespace -> Speedup of 5% (PowerShell 5.1) or 2.5 % (PowerShell 7.1-preview.2) (#1465)
* Improve performance by not using regex (2% improvement) * replace regex * fix index and simplify * tidy * Apply suggestions from code review Co-Authored-By: Robert Holt <rjmholt@gmail.com> * Use IsWhiteSpace Co-authored-by: Christoph Bergmeister <christoph.bergmeister@bjss.com> Co-authored-by: Robert Holt <rjmholt@gmail.com>
1 parent 3e6987b commit 6fa29cb

File tree

1 file changed

+53
-36
lines changed

1 file changed

+53
-36
lines changed

Rules/AvoidTrailingWhitespace.cs

+53-36
Original file line numberDiff line numberDiff line change
@@ -36,52 +36,69 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
3636

3737
var diagnosticRecords = new List<DiagnosticRecord>();
3838

39-
string[] lines = Regex.Split(ast.Extent.Text, @"\r?\n");
39+
string[] lines = ast.Extent.Text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
4040

4141
for (int lineNumber = 0; lineNumber < lines.Length; lineNumber++)
4242
{
4343
var line = lines[lineNumber];
4444

45-
var match = Regex.Match(line, @"\s+$");
46-
if (match.Success)
45+
if (line.Length == 0)
4746
{
48-
var startLine = lineNumber + 1;
49-
var endLine = startLine;
50-
var startColumn = match.Index + 1;
51-
var endColumn = startColumn + match.Length;
52-
53-
var violationExtent = new ScriptExtent(
54-
new ScriptPosition(
55-
ast.Extent.File,
56-
startLine,
57-
startColumn,
58-
line
59-
),
60-
new ScriptPosition(
61-
ast.Extent.File,
62-
endLine,
63-
endColumn,
64-
line
65-
));
47+
continue;
48+
}
6649

67-
var suggestedCorrections = new List<CorrectionExtent>();
68-
suggestedCorrections.Add(new CorrectionExtent(
69-
violationExtent,
70-
string.Empty,
71-
ast.Extent.File
72-
));
50+
if (!char.IsWhiteSpace(line[line.Length - 1]) &&
51+
line[line.Length - 1] != '\t')
52+
{
53+
continue;
54+
}
7355

74-
diagnosticRecords.Add(
75-
new DiagnosticRecord(
76-
String.Format(CultureInfo.CurrentCulture, Strings.AvoidTrailingWhitespaceError),
56+
int startColumnOfTrailingWhitespace = 1;
57+
for (int i = line.Length - 2; i > 0; i--)
58+
{
59+
if (line[i] != ' ' && line[i] != '\t')
60+
{
61+
startColumnOfTrailingWhitespace = i + 2;
62+
break;
63+
}
64+
}
65+
66+
int startLine = lineNumber + 1;
67+
int endLine = startLine;
68+
int startColumn = startColumnOfTrailingWhitespace;
69+
int endColumn = line.Length + 1;
70+
71+
var violationExtent = new ScriptExtent(
72+
new ScriptPosition(
73+
ast.Extent.File,
74+
startLine,
75+
startColumn,
76+
line
77+
),
78+
new ScriptPosition(
79+
ast.Extent.File,
80+
endLine,
81+
endColumn,
82+
line
83+
));
84+
85+
var suggestedCorrections = new List<CorrectionExtent>();
86+
suggestedCorrections.Add(new CorrectionExtent(
7787
violationExtent,
78-
GetName(),
79-
GetDiagnosticSeverity(),
80-
ast.Extent.File,
81-
null,
82-
suggestedCorrections
88+
string.Empty,
89+
ast.Extent.File
8390
));
84-
}
91+
92+
diagnosticRecords.Add(
93+
new DiagnosticRecord(
94+
String.Format(CultureInfo.CurrentCulture, Strings.AvoidTrailingWhitespaceError),
95+
violationExtent,
96+
GetName(),
97+
GetDiagnosticSeverity(),
98+
ast.Extent.File,
99+
null,
100+
suggestedCorrections
101+
));
85102
}
86103

87104
return diagnosticRecords;

0 commit comments

Comments
 (0)