From 9a6db4cbeae0ab320ffed10d49c9d626fc6983c2 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Wed, 22 Apr 2020 20:33:05 +0100 Subject: [PATCH 1/6] Improve performance by not using regex (2% improvement) --- Rules/AvoidTrailingWhitespace.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Rules/AvoidTrailingWhitespace.cs b/Rules/AvoidTrailingWhitespace.cs index 987ff6dd9..c7bd908c2 100644 --- a/Rules/AvoidTrailingWhitespace.cs +++ b/Rules/AvoidTrailingWhitespace.cs @@ -36,12 +36,22 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) var diagnosticRecords = new List(); - string[] lines = Regex.Split(ast.Extent.Text, @"\r?\n"); + //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]; + if (line.Length == 0) + { + continue; + } + if (line[line.Length - 1] != ' ' && line[line.Length - 1] != '\t') + { + continue; + } + var match = Regex.Match(line, @"\s+$"); if (match.Success) { From eab63121e66811f52d7060cf08d274cf0e647384 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 27 Apr 2020 19:13:31 +0100 Subject: [PATCH 2/6] replace regex --- Rules/AvoidTrailingWhitespace.cs | 82 +++++++++++++++++--------------- 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/Rules/AvoidTrailingWhitespace.cs b/Rules/AvoidTrailingWhitespace.cs index c7bd908c2..fb1ec2dea 100644 --- a/Rules/AvoidTrailingWhitespace.cs +++ b/Rules/AvoidTrailingWhitespace.cs @@ -47,51 +47,57 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) { continue; } - if (line[line.Length - 1] != ' ' && line[line.Length - 1] != '\t') + if (line[line.Length - 1] != ' ' && + line[line.Length - 1] != '\t') { continue; } - - var match = Regex.Match(line, @"\s+$"); - if (match.Success) + int startColumnOfTrailingWhitespace = 0; + for (int i = line.Length - 2; i > 0; i--) { - 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 - )); - - var suggestedCorrections = new List(); - suggestedCorrections.Add(new CorrectionExtent( - violationExtent, - string.Empty, - ast.Extent.File - )); + if (line[i] != ' ' && line[i] != '\t') + { + startColumnOfTrailingWhitespace = i + 2; + break; + } + } - diagnosticRecords.Add( - new DiagnosticRecord( - String.Format(CultureInfo.CurrentCulture, Strings.AvoidTrailingWhitespaceError), + var startLine = lineNumber + 1; + var endLine = startLine; + var startColumn = startColumnOfTrailingWhitespace; + var 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(); + 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; From 172ebddac729c53820415d15f36952f208447b52 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 27 Apr 2020 19:45:53 +0100 Subject: [PATCH 3/6] fix index and simplify --- Rules/AvoidTrailingWhitespace.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rules/AvoidTrailingWhitespace.cs b/Rules/AvoidTrailingWhitespace.cs index fb1ec2dea..98f50813c 100644 --- a/Rules/AvoidTrailingWhitespace.cs +++ b/Rules/AvoidTrailingWhitespace.cs @@ -52,7 +52,7 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) { continue; } - int startColumnOfTrailingWhitespace = 0; + int startColumnOfTrailingWhitespace = 1; for (int i = line.Length - 2; i > 0; i--) { if (line[i] != ' ' && line[i] != '\t') From 9456321a32219557cbb28cc75c25d991813e8a07 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 27 Apr 2020 21:45:12 +0100 Subject: [PATCH 4/6] tidy --- Rules/AvoidTrailingWhitespace.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Rules/AvoidTrailingWhitespace.cs b/Rules/AvoidTrailingWhitespace.cs index 98f50813c..219f80019 100644 --- a/Rules/AvoidTrailingWhitespace.cs +++ b/Rules/AvoidTrailingWhitespace.cs @@ -36,7 +36,6 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) var diagnosticRecords = new List(); - //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++) From c35ec9213945355535c02ad7e8944483afe7beab Mon Sep 17 00:00:00 2001 From: "Christoph Bergmeister [MVP]" Date: Mon, 27 Apr 2020 22:05:21 +0100 Subject: [PATCH 5/6] Apply suggestions from code review Co-Authored-By: Robert Holt --- Rules/AvoidTrailingWhitespace.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Rules/AvoidTrailingWhitespace.cs b/Rules/AvoidTrailingWhitespace.cs index 219f80019..9d3cfc213 100644 --- a/Rules/AvoidTrailingWhitespace.cs +++ b/Rules/AvoidTrailingWhitespace.cs @@ -46,11 +46,13 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) { continue; } + if (line[line.Length - 1] != ' ' && line[line.Length - 1] != '\t') { continue; } + int startColumnOfTrailingWhitespace = 1; for (int i = line.Length - 2; i > 0; i--) { @@ -61,10 +63,10 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) } } - var startLine = lineNumber + 1; - var endLine = startLine; - var startColumn = startColumnOfTrailingWhitespace; - var endColumn = line.Length + 1; + int startLine = lineNumber + 1; + int endLine = startLine; + int startColumn = startColumnOfTrailingWhitespace; + int endColumn = line.Length + 1; var violationExtent = new ScriptExtent( new ScriptPosition( From aedbc132857b9aa86a11fd1e2814804e178a7b8a Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Tue, 28 Apr 2020 20:38:21 +0100 Subject: [PATCH 6/6] Use IsWhiteSpace --- Rules/AvoidTrailingWhitespace.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rules/AvoidTrailingWhitespace.cs b/Rules/AvoidTrailingWhitespace.cs index 9d3cfc213..47f576d5b 100644 --- a/Rules/AvoidTrailingWhitespace.cs +++ b/Rules/AvoidTrailingWhitespace.cs @@ -47,7 +47,7 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) continue; } - if (line[line.Length - 1] != ' ' && + if (!char.IsWhiteSpace(line[line.Length - 1]) && line[line.Length - 1] != '\t') { continue;