Skip to content

Commit ff580f8

Browse files
committed
fix: code clean up, optimizing string comparison
1 parent 0d16d87 commit ff580f8

21 files changed

+119
-104
lines changed

src/.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,6 @@ dotnet_naming_style.fields_begin_with__.required_prefix = _
225225
dotnet_naming_style.fields_begin_with__.required_suffix =
226226
dotnet_naming_style.fields_begin_with__.word_separator =
227227
dotnet_naming_style.fields_begin_with__.capitalization = camel_case
228+
229+
# MA0012: Do not raise reserved exception type
230+
dotnet_diagnostic.MA0012.severity = none

src/AngleSharp.Diffing.Tests/Core/HtmlDifferenceEngineTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ private static IEnumerable<Comparison> NoneNodeMatcher(IDiffContext ctx, SourceC
451451
private static Func<IDiffContext, SourceCollection, SourceCollection, IEnumerable<Comparison>> SpecificIndexNodeMatcher(int index)
452452
=> (ctx, controlNodes, testNodes) =>
453453
{
454-
return new List<Comparison> { new Comparison(controlNodes[index], testNodes[index]) };
454+
return new List<Comparison> { new(controlNodes[index], testNodes[index]) };
455455
};
456456

457457
private static IEnumerable<Comparison> OneToOneNodeListMatcher(
@@ -477,7 +477,7 @@ private static Func<IDiffContext, SourceMap, SourceMap, IEnumerable<AttributeCom
477477
{
478478
return (ctx, ctrlAttrs, testAttrs) => new List<AttributeComparison>
479479
{
480-
new AttributeComparison(ctrlAttrs[matchAttrName], testAttrs[matchAttrName] )
480+
new(ctrlAttrs[matchAttrName], testAttrs[matchAttrName] )
481481
};
482482
}
483483

src/AngleSharp.Diffing.Tests/DiffBuilderTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public void Test001()
2020
[Fact(DisplayName = "Builder throws if null is passed to control and test")]
2121
public void Test002()
2222
{
23-
Should.Throw<ArgumentNullException>(() => DiffBuilder.Compare(null!)).ParamName.ShouldBe(nameof(DiffBuilder.Control));
24-
Should.Throw<ArgumentNullException>(() => DiffBuilder.Compare("").WithTest(null!)).ParamName.ShouldBe(nameof(DiffBuilder.Test));
23+
Should.Throw<ArgumentNullException>(() => DiffBuilder.Compare(null!)).ParamName.ShouldBe("value");
24+
Should.Throw<ArgumentNullException>(() => DiffBuilder.Compare("").WithTest(null!)).ParamName.ShouldBe("value");
2525
}
2626

2727
[Fact(DisplayName = "Calling Build() with DefaultOptions() returns expected diffs")]

src/AngleSharp.Diffing.Tests/DiffingTestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public abstract class DiffingTestBase : IClassFixture<DiffingTestFixture>
55
private readonly DiffingTestFixture _testFixture;
66

77

8-
public static readonly TheoryData<CompareResult> SameAndSkipCompareResult = new TheoryData<CompareResult>
8+
public static readonly TheoryData<CompareResult> SameAndSkipCompareResult = new()
99
{
1010
CompareResult.Same,
1111
CompareResult.Skip,

src/AngleSharp.Diffing.sln

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.29230.61
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.11.35312.102
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AngleSharp.Diffing", "AngleSharp.Diffing\AngleSharp.Diffing.csproj", "{2BFFA992-22C2-4A65-94D8-CA06E81D2364}"
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AngleSharp.DiffingTests", "AngleSharp.Diffing.Tests\AngleSharp.DiffingTests.csproj", "{18203D98-66B4-4736-B79A-3B7D02EFA9E8}"
99
EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E8E3C8B4-92C3-4DB7-B920-D28651E24A57}"
1111
ProjectSection(SolutionItems) = preProject
12-
..\.editorconfig = ..\.editorconfig
12+
.editorconfig = .editorconfig
1313
..\tools\anglesharp.cake = ..\tools\anglesharp.cake
1414
..\build.cake = ..\build.cake
1515
..\build.ps1 = ..\build.ps1
Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
5-
<GenerateDocumentationFile>true</GenerateDocumentationFile>
6-
</PropertyGroup>
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
6+
</PropertyGroup>
77

8-
<PropertyGroup>
9-
<Description>Provides a complete diffing model of HTML.</Description>
10-
<Product>AngleSharp.Diffing</Product>
11-
<Authors>AngleSharp</Authors>
12-
<PackageId>AngleSharp.Diffing</PackageId>
13-
<PackageLicenseExpression>MIT</PackageLicenseExpression>
14-
<PackageProjectUrl>https://anglesharp.github.io</PackageProjectUrl>
15-
<PackageIcon>logo.png</PackageIcon>
16-
<PackageIconUrl>https://raw.github.com/AngleSharp/AngleSharp.Diffing/master/logo.png</PackageIconUrl>
17-
<PackageTags>html html5 css css3 dom library diffing anglesharp diff difference compare comparison testing</PackageTags>
18-
<Copyright>Egil Hansen</Copyright>
19-
<RepositoryUrl>https://github.com/AngleSharp/AngleSharp.Diffing</RepositoryUrl>
20-
<RepositoryType>git</RepositoryType>
21-
<PublishRepositoryUrl>true</PublishRepositoryUrl>
22-
<IncludeSymbols>true</IncludeSymbols>
23-
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
24-
</PropertyGroup>
8+
<PropertyGroup>
9+
<Description>Provides a complete diffing model of HTML.</Description>
10+
<Product>AngleSharp.Diffing</Product>
11+
<Authors>AngleSharp</Authors>
12+
<PackageId>AngleSharp.Diffing</PackageId>
13+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
14+
<PackageProjectUrl>https://anglesharp.github.io</PackageProjectUrl>
15+
<PackageIcon>logo.png</PackageIcon>
16+
<PackageIconUrl>https://raw.github.com/AngleSharp/AngleSharp.Diffing/master/logo.png</PackageIconUrl>
17+
<PackageTags>html html5 css css3 dom library diffing anglesharp diff difference compare comparison testing</PackageTags>
18+
<Copyright>Egil Hansen</Copyright>
19+
<RepositoryUrl>https://github.com/AngleSharp/AngleSharp.Diffing</RepositoryUrl>
20+
<RepositoryType>git</RepositoryType>
21+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
22+
<IncludeSymbols>true</IncludeSymbols>
23+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
24+
</PropertyGroup>
2525

26-
<ItemGroup>
27-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
28-
</ItemGroup>
26+
<ItemGroup>
27+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
28+
</ItemGroup>
2929

30-
<ItemGroup>
31-
<PackageReference Include="TunnelVisionLabs.ReferenceAssemblyAnnotator" Version="1.0.0-alpha.160" PrivateAssets="all" />
32-
<PackageDownload Include="Microsoft.NETCore.App.Ref" Version="[5.0.0]" />
33-
</ItemGroup>
30+
<ItemGroup>
31+
<PackageReference Include="Meziantou.Analyzer" Version="2.0.169">
32+
<PrivateAssets>all</PrivateAssets>
33+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
34+
</PackageReference>
35+
<PackageReference Include="TunnelVisionLabs.ReferenceAssemblyAnnotator" Version="1.0.0-alpha.160" PrivateAssets="all" />
36+
<PackageDownload Include="Microsoft.NETCore.App.Ref" Version="[5.0.0]" />
37+
</ItemGroup>
3438

35-
<ItemGroup>
36-
<None Include="..\..\logo.png" Pack="true" PackagePath="\" />
37-
</ItemGroup>
39+
<ItemGroup>
40+
<None Include="..\..\logo.png" Pack="true" PackagePath="\" />
41+
</ItemGroup>
3842

3943
</Project>

src/AngleSharp.Diffing/Core/AttributeComparison.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
using System.Runtime.InteropServices;
2+
13
namespace AngleSharp.Diffing.Core;
24

35
/// <summary>
46
/// A match between two attributes that should be compared.
57
/// </summary>
68
/// <param name="Control">Gets the control attribute which the <see cref="Test"/> attribute is supposed to match.</param>
79
/// <param name="Test">Gets the test attribute which should be compared to the <see cref="Control"/> attribute.</param>
10+
[StructLayout(LayoutKind.Auto)]
811
public readonly record struct AttributeComparison(in AttributeComparisonSource Control, in AttributeComparisonSource Test)
912
{
1013
/// <summary>

src/AngleSharp.Diffing/Core/CompareResult.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,39 @@ public readonly record struct CompareResult(CompareDecision Decision, IDiff? Dif
1515
/// <summary>
1616
/// Use when the two compared nodes or attributes are the same.
1717
/// </summary>
18-
public static readonly CompareResult Same = new CompareResult(CompareDecision.Same);
18+
public static readonly CompareResult Same = new(CompareDecision.Same);
1919

2020
/// <summary>
2121
/// Use when the comparison should be skipped and any child-nodes or attributes skipped as well.
2222
/// </summary>
23-
public static readonly CompareResult Skip = new CompareResult(CompareDecision.Skip);
23+
public static readonly CompareResult Skip = new(CompareDecision.Skip);
2424

2525
/// <summary>
2626
/// Use when the comparison should skip any child-nodes.
2727
/// </summary>
28-
public static readonly CompareResult SkipChildren = new CompareResult(CompareDecision.SkipChildren);
28+
public static readonly CompareResult SkipChildren = new(CompareDecision.SkipChildren);
2929

3030
/// <summary>
3131
/// Use when the comparison should skip any attributes.
3232
/// </summary>
33-
public static readonly CompareResult SkipAttributes = new CompareResult(CompareDecision.SkipAttributes);
33+
public static readonly CompareResult SkipAttributes = new(CompareDecision.SkipAttributes);
3434

3535
/// <summary>
3636
/// Use when the comparison should skip any attributes.
3737
/// </summary>
38-
public static readonly CompareResult SkipChildrenAndAttributes = new CompareResult(CompareDecision.SkipChildren | CompareDecision.SkipAttributes);
38+
public static readonly CompareResult SkipChildrenAndAttributes = new(CompareDecision.SkipChildren | CompareDecision.SkipAttributes);
3939

4040
/// <summary>
4141
/// Use when the two compared nodes or attributes are the different.
4242
/// </summary>
43-
public static CompareResult Different => new CompareResult(CompareDecision.Different);
43+
public static CompareResult Different => new(CompareDecision.Different);
4444

4545
/// <summary>
4646
/// Use when the two compared nodes or attributes are the different.
4747
/// </summary>
4848
/// <param name="diff">The associated <see cref="IDiff"/> describing the difference.</param>
4949
/// <returns>Returns a <see cref="CompareResult"/> with <see cref="CompareResult.Decision"/> set to <see cref="CompareDecision.Different"/>.</returns>
50-
public static CompareResult FromDiff(IDiff diff) => new CompareResult(CompareDecision.Different, diff);
50+
public static CompareResult FromDiff(IDiff diff) => new(CompareDecision.Different, diff);
5151

5252
/// <summary>
5353
/// Checks if a <see cref="CompareResult"/> is either a <see cref="CompareResult.Same"/> or <see cref="CompareResult.Skip"/>.

src/AngleSharp.Diffing/Core/Comparison.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
using System.Runtime.InteropServices;
2+
13
namespace AngleSharp.Diffing.Core;
24

35
/// <summary>
46
/// Represent a comparison between two nodes.
57
/// </summary>
68
/// <param name="Control">Gets the control source in the comparison.</param>
79
/// <param name="Test">Gets the test source in the comparison.</param>
10+
[StructLayout(LayoutKind.Auto)]
811
public readonly record struct Comparison(in ComparisonSource Control, in ComparisonSource Test)
912
{
1013
/// <summary>

src/AngleSharp.Diffing/Core/DiffTarget.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,3 @@ public enum DiffTarget
3030
/// </summary>
3131
Text
3232
}
33-
34-
/// <summary>
35-
/// Helper methods for working with <see cref="DiffTarget"/>.
36-
/// </summary>
37-
public static class NodeTypeExtensions
38-
{
39-
/// <summary>
40-
/// Gets the diff target based on the node type.
41-
/// </summary>
42-
/// <param name="nodeType">Mode type to get the diff target off.</param>
43-
public static DiffTarget ToDiffTarget(this NodeType nodeType)
44-
{
45-
return nodeType switch
46-
{
47-
NodeType.Element => DiffTarget.Element,
48-
NodeType.Comment => DiffTarget.Comment,
49-
NodeType.Text => DiffTarget.Text,
50-
_ => DiffTarget.Node
51-
};
52-
}
53-
}

src/AngleSharp.Diffing/Core/FilterDecision.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,3 @@ public enum FilterDecision
1414
/// </summary>
1515
Exclude
1616
}
17-
18-
/// <summary>
19-
/// Helper methods for <see cref="FilterDecision"/>.
20-
/// </summary>
21-
public static class FilterDecisionExtensions
22-
{
23-
/// <summary>
24-
/// Gets whether the <see cref="FilterDecision"/> is <see cref="FilterDecision.Exclude"/>.
25-
/// </summary>
26-
public static bool IsExclude(this FilterDecision decision) => decision == FilterDecision.Exclude;
27-
28-
/// <summary>
29-
/// Gets whether the <see cref="FilterDecision"/> is <see cref="FilterDecision.Keep"/>.
30-
/// </summary>
31-
public static bool IsKeep(this FilterDecision decision) => decision == FilterDecision.Keep;
32-
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace AngleSharp.Diffing.Core;
2+
3+
/// <summary>
4+
/// Helper methods for <see cref="FilterDecision"/>.
5+
/// </summary>
6+
public static class FilterDecisionExtensions
7+
{
8+
/// <summary>
9+
/// Gets whether the <see cref="FilterDecision"/> is <see cref="FilterDecision.Exclude"/>.
10+
/// </summary>
11+
public static bool IsExclude(this FilterDecision decision) => decision == FilterDecision.Exclude;
12+
13+
/// <summary>
14+
/// Gets whether the <see cref="FilterDecision"/> is <see cref="FilterDecision.Keep"/>.
15+
/// </summary>
16+
public static bool IsKeep(this FilterDecision decision) => decision == FilterDecision.Keep;
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace AngleSharp.Diffing.Core;
2+
3+
/// <summary>
4+
/// Helper methods for working with <see cref="DiffTarget"/>.
5+
/// </summary>
6+
public static class NodeTypeExtensions
7+
{
8+
/// <summary>
9+
/// Gets the diff target based on the node type.
10+
/// </summary>
11+
/// <param name="nodeType">Mode type to get the diff target off.</param>
12+
public static DiffTarget ToDiffTarget(this NodeType nodeType)
13+
{
14+
return nodeType switch
15+
{
16+
NodeType.Element => DiffTarget.Element,
17+
NodeType.Comment => DiffTarget.Comment,
18+
NodeType.Text => DiffTarget.Text,
19+
_ => DiffTarget.Node
20+
};
21+
}
22+
}

src/AngleSharp.Diffing/Core/SourceMap.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ namespace AngleSharp.Diffing.Core;
66
[SuppressMessage("Naming", "CA1710:Identifiers should have correct suffix")]
77
public class SourceMap : IEnumerable<AttributeComparisonSource>
88
{
9-
private readonly HashSet<string> _matched = new HashSet<string>();
10-
private readonly Dictionary<string, AttributeComparisonSource> _sources = new Dictionary<string, AttributeComparisonSource>();
9+
private readonly HashSet<string> _matched = new(StringComparer.OrdinalIgnoreCase);
10+
private readonly Dictionary<string, AttributeComparisonSource> _sources = new(StringComparer.OrdinalIgnoreCase);
1111

1212
/// <summary>
1313
/// Gets the type of the sources in the collection.

src/AngleSharp.Diffing/DiffBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ public class DiffBuilder
1717
/// <summary>
1818
/// Gets or sets the control markup string.
1919
/// </summary>
20-
public string Control { get => _control; set => _control = value ?? throw new ArgumentNullException(nameof(Control)); }
20+
public string Control { get => _control; set => _control = value ?? throw new ArgumentNullException(nameof(value)); }
2121

2222
/// <summary>
2323
/// Gets or sets the test markup string.
2424
/// </summary>
25-
public string Test { get => _test; set => _test = value ?? throw new ArgumentNullException(nameof(Test)); }
25+
public string Test { get => _test; set => _test = value ?? throw new ArgumentNullException(nameof(value)); }
2626

2727
private DiffBuilder(string control)
2828
{

src/AngleSharp.Diffing/Extensions/NodeListExtensions.cs renamed to src/AngleSharp.Diffing/Extensions/AngleSharpDomExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ public static IEnumerable<ComparisonSource> ToComparisonSourceList(this IEnumera
3434
/// Creates a comparison source from a node.
3535
/// </summary>
3636
public static ComparisonSource ToComparisonSource(this INode node, int index, ComparisonSourceType sourceType, string path = "")
37-
=> new ComparisonSource(node, index, path, sourceType);
37+
=> new(node, index, path, sourceType);
3838
}

src/AngleSharp.Diffing/Strategies/AttributeStrategies/ClassAttributeComparer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static CompareResult Compare(in AttributeComparison comparison, CompareRe
2222
if (ctrlElm.ClassList.Length != testElm.ClassList.Length)
2323
return CompareResult.FromDiff(new AttrDiff(comparison, AttrDiffKind.Value));
2424

25-
return ctrlElm.ClassList.All(x => testElm.ClassList.Contains(x))
25+
return ctrlElm.ClassList.All(x => testElm.ClassList.Contains(x, StringComparer.Ordinal))
2626
? CompareResult.Same
2727
: CompareResult.FromDiff(new AttrDiff(comparison, AttrDiffKind.Value));
2828
}

src/AngleSharp.Diffing/Strategies/AttributeStrategies/OrderingStyleAttributeComparer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ private static bool CompareCssStyleDeclarations(ICssStyleDeclaration control, IC
3939
if (control.Length != test.Length)
4040
return false;
4141

42-
var orderedControl = control.CssText.Split(';').Select(x => x.Trim()).OrderBy(x => x);
43-
var orderedTest = test.CssText.Split(';').Select(x => x.Trim()).OrderBy(x => x);
42+
var orderedControl = control.CssText.Split(';').Select(x => x.Trim()).OrderBy(x => x, StringComparer.Ordinal);
43+
var orderedTest = test.CssText.Split(';').Select(x => x.Trim()).OrderBy(x => x, StringComparer.Ordinal);
4444

4545
return orderedControl.SequenceEqual(orderedTest, StringComparer.Ordinal);
4646
}

0 commit comments

Comments
 (0)