Skip to content

Commit 03341fa

Browse files
authored
Merge 589f040 into 5d98796
2 parents 5d98796 + 589f040 commit 03341fa

File tree

3 files changed

+93
-18
lines changed

3 files changed

+93
-18
lines changed

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1625ElementDocumentationMustNotBeCopiedAndPasted.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,11 @@ protected override void HandleXmlElement(SyntaxNodeAnalysisContext context, Styl
101101
continue;
102102
}
103103

104-
if (documentationTexts.Contains(documentation))
104+
if (!documentationTexts.Add(documentation))
105105
{
106106
// Add violation
107107
context.ReportDiagnostic(Diagnostic.Create(Descriptor, documentationSyntax.GetLocation()));
108108
}
109-
else
110-
{
111-
documentationTexts.Add(documentation);
112-
}
113109
}
114110

115111
objectPool.ClearAndFree(documentationTexts);
@@ -148,15 +144,11 @@ protected override void HandleCompleteDocumentation(SyntaxNodeAnalysisContext co
148144
continue;
149145
}
150146

151-
if (documentationTexts.Contains(documentation))
147+
if (!documentationTexts.Add(documentation))
152148
{
153149
// Add violation
154150
context.ReportDiagnostic(Diagnostic.Create(Descriptor, diagnosticLocations.First()));
155151
}
156-
else
157-
{
158-
documentationTexts.Add(documentation);
159-
}
160152
}
161153

162154
objectPool.ClearAndFree(documentationTexts);

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/ObjectPools/StringBuilderPool.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ public static void Free(StringBuilder builder)
2222

2323
public static string ReturnAndFree(StringBuilder builder)
2424
{
25-
SharedPools.Default<StringBuilder>();
26-
return builder.ToString();
25+
string result = builder.ToString();
26+
27+
StringBuilderPool.Free(builder);
28+
29+
return result;
2730
}
2831
}
2932
}

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/XmlCommentHelper.cs

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ namespace StyleCop.Analyzers.Helpers
77
{
88
using System.Linq;
99
using System.Text;
10-
using System.Text.RegularExpressions;
1110
using System.Xml.Linq;
1211
using Microsoft.CodeAnalysis;
1312
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -268,20 +267,101 @@ internal static string GetText(XmlTextSyntax textElement, bool normalizeWhitespa
268267
return null;
269268
}
270269

271-
StringBuilder stringBuilder = StringBuilderPool.Allocate();
270+
bool lastWhitespace = false;
271+
272+
string single = string.Empty;
273+
274+
StringBuilder stringBuilder = null;
272275

273276
foreach (var item in textElement.TextTokens)
274277
{
275-
stringBuilder.Append(item);
278+
if (single.Length == 0)
279+
{
280+
single = item.ToString();
281+
}
282+
else
283+
{
284+
if (stringBuilder == null)
285+
{
286+
stringBuilder = StringBuilderPool.Allocate();
287+
stringBuilder.AppendNormalize(single, normalizeWhitespace, ref lastWhitespace);
288+
}
289+
290+
stringBuilder.AppendNormalize(item.ToString(), normalizeWhitespace, ref lastWhitespace);
291+
}
276292
}
277293

278-
string result = StringBuilderPool.ReturnAndFree(stringBuilder);
294+
if (stringBuilder == null)
295+
{
296+
if (normalizeWhitespace)
297+
{
298+
stringBuilder = StringBuilderPool.Allocate();
299+
300+
if (!stringBuilder.AppendNormalize(single, normalizeWhitespace, ref lastWhitespace))
301+
{
302+
StringBuilderPool.Free(stringBuilder);
303+
304+
// No change is needed, return original string.
305+
return single;
306+
}
307+
}
308+
else
309+
{
310+
return single;
311+
}
312+
}
313+
314+
return StringBuilderPool.ReturnAndFree(stringBuilder);
315+
}
316+
317+
/// <summary>
318+
/// Append to StringBuilder and perform white space normalization.
319+
/// </summary>
320+
/// <param name="builder">StringBuilder to append to.</param>
321+
/// <param name="text">String to append.</param>
322+
/// <param name="normalizeWhitespace">Normalize flag.</param>
323+
/// <param name="lastWhitespace">last char is white space flag.</param>
324+
/// <returns>True if output is different.</returns>
325+
internal static bool AppendNormalize(this StringBuilder builder, string text, bool normalizeWhitespace, ref bool lastWhitespace)
326+
{
327+
bool diff = false;
328+
279329
if (normalizeWhitespace)
280330
{
281-
result = Regex.Replace(result, @"\s+", " ");
331+
foreach (char ch in text)
332+
{
333+
if (char.IsWhiteSpace(ch))
334+
{
335+
if (lastWhitespace)
336+
{
337+
diff = true;
338+
}
339+
else
340+
{
341+
if (ch != ' ')
342+
{
343+
diff = true;
344+
}
345+
346+
builder.Append(' ');
347+
}
348+
349+
lastWhitespace = true;
350+
}
351+
else
352+
{
353+
builder.Append(ch);
354+
355+
lastWhitespace = false;
356+
}
357+
}
358+
}
359+
else
360+
{
361+
builder.Append(text);
282362
}
283363

284-
return result;
364+
return diff;
285365
}
286366

287367
internal static T GetFirstAttributeOrDefault<T>(XmlNodeSyntax nodeSyntax)

0 commit comments

Comments
 (0)