Skip to content

Commit

Permalink
Performance: Eliminate Regex overhead in AvoidTrailingWhitespace -> S…
Browse files Browse the repository at this point in the history
…peedup 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 <[email protected]>

* Use IsWhiteSpace

Co-authored-by: Christoph Bergmeister <[email protected]>
Co-authored-by: Robert Holt <[email protected]>
  • Loading branch information
3 people authored Apr 28, 2020
1 parent 3e6987b commit 6fa29cb
Showing 1 changed file with 53 additions and 36 deletions.
89 changes: 53 additions & 36 deletions Rules/AvoidTrailingWhitespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,52 +36,69 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)

var diagnosticRecords = new List<DiagnosticRecord>();

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

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

var match = Regex.Match(line, @"\s+$");
if (match.Success)
if (line.Length == 0)
{
var startLine = lineNumber + 1;
var endLine = startLine;
var startColumn = match.Index + 1;
var endColumn = startColumn + match.Length;

var violationExtent = new ScriptExtent(
new ScriptPosition(
ast.Extent.File,
startLine,
startColumn,
line
),
new ScriptPosition(
ast.Extent.File,
endLine,
endColumn,
line
));
continue;
}

var suggestedCorrections = new List<CorrectionExtent>();
suggestedCorrections.Add(new CorrectionExtent(
violationExtent,
string.Empty,
ast.Extent.File
));
if (!char.IsWhiteSpace(line[line.Length - 1]) &&
line[line.Length - 1] != '\t')
{
continue;
}

diagnosticRecords.Add(
new DiagnosticRecord(
String.Format(CultureInfo.CurrentCulture, Strings.AvoidTrailingWhitespaceError),
int startColumnOfTrailingWhitespace = 1;
for (int i = line.Length - 2; i > 0; i--)
{
if (line[i] != ' ' && line[i] != '\t')
{
startColumnOfTrailingWhitespace = i + 2;
break;
}
}

int startLine = lineNumber + 1;
int endLine = startLine;
int startColumn = startColumnOfTrailingWhitespace;
int endColumn = line.Length + 1;

var violationExtent = new ScriptExtent(
new ScriptPosition(
ast.Extent.File,
startLine,
startColumn,
line
),
new ScriptPosition(
ast.Extent.File,
endLine,
endColumn,
line
));

var suggestedCorrections = new List<CorrectionExtent>();
suggestedCorrections.Add(new CorrectionExtent(
violationExtent,
GetName(),
GetDiagnosticSeverity(),
ast.Extent.File,
null,
suggestedCorrections
string.Empty,
ast.Extent.File
));
}

diagnosticRecords.Add(
new DiagnosticRecord(
String.Format(CultureInfo.CurrentCulture, Strings.AvoidTrailingWhitespaceError),
violationExtent,
GetName(),
GetDiagnosticSeverity(),
ast.Extent.File,
null,
suggestedCorrections
));
}

return diagnosticRecords;
Expand Down

0 comments on commit 6fa29cb

Please sign in to comment.