Skip to content

Commit aa2f4e0

Browse files
committed
fix: "ax" is not same as "Ay"
It ignore next chars if there is a char diffrent only in case before them
1 parent 45e60ea commit aa2f4e0

4 files changed

Lines changed: 37 additions & 6 deletions

File tree

src/NaturalStringComparer/NaturalComparer.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,7 @@ public static int Compare(ReadOnlySpan<char> x, ReadOnlySpan<char> y, StringComp
7575

7676
for (var i = 0; i < length; i++)
7777
{
78-
var xCh = x[i];
79-
var yCh = y[i];
80-
81-
if (char.IsDigit(xCh) && char.IsDigit(yCh))
78+
if (char.IsDigit(x[i]) && char.IsDigit(y[i]))
8279
{
8380
var xOut = GetNumber(x.Slice(i), out var xNumAsSpan);
8481
var yOut = GetNumber(y.Slice(i), out var yNumAsSpan);
@@ -98,9 +95,10 @@ public static int Compare(ReadOnlySpan<char> x, ReadOnlySpan<char> y, StringComp
9895
continue;
9996
}
10097

101-
if (xCh != yCh)
98+
var charCompareResult = x.Slice(i, 1).CompareTo(y.Slice(i, 1), stringComparison);
99+
if (charCompareResult != 0)
102100
{
103-
return x.Slice(i, 1).CompareTo(y.Slice(i, 1), stringComparison);
101+
return charCompareResult;
104102
}
105103
}
106104

test/NaturalStringComparerTest/NaturalStringComparerTest.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="FluentAssertions" Version="6.12.1" />
914
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="*" />
1015
<PackageReference Include="xunit" Version="*" />
1116
<PackageReference Include="xunit.runner.visualstudio" Version="*" />

test/NaturalStringComparerTest/UnitTestUnsafe.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace NaturalStringComparerTest
44
using System.Collections.Generic;
55
using System.Numerics;
66

7+
using FluentAssertions;
8+
79
using GihanSoft.String;
810

911
using Xunit;
@@ -24,6 +26,28 @@ public UnitTestUnsafe()
2426
}
2527
}
2628

29+
[InlineData("abc", "ABC")]
30+
[InlineData("abc", "aBc")]
31+
[InlineData("XX11xx", "xx11XX")]
32+
[InlineData("11xx22", "11XX22")]
33+
[Theory]
34+
public void Given_OrdinalIgnoreCase_When_strings_only_different_in_case_Then_return_0(string x, string y)
35+
{
36+
var actual = NaturalComparer.Compare(x, y, StringComparison.OrdinalIgnoreCase);
37+
actual.Should().Be(0);
38+
}
39+
40+
[Fact]
41+
public void Given_OrdinalIgnoreCase_When_there_is_a_different_char_after_a_case_different_char_Then_should_check_rest_of_span()
42+
{
43+
var x = "ax";
44+
var y = "Ay";
45+
var expected = StringComparer.OrdinalIgnoreCase.Compare("x", "y");
46+
47+
var actual = NaturalComparer.Compare(x, y, StringComparison.OrdinalIgnoreCase);
48+
actual.Should().Be(expected);
49+
}
50+
2751
[Fact]
2852
public void TestLargeNumber()
2953
{
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
3+
"methodDisplayOptions": "all"
4+
}

0 commit comments

Comments
 (0)