Skip to content

Commit

Permalink
update test & fix "033" < "33"
Browse files Browse the repository at this point in the history
  • Loading branch information
GihanSoft committed Dec 19, 2022
1 parent 799af6a commit afb1a5c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
19 changes: 14 additions & 5 deletions NaturalStringComparer/NaturalComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,26 @@ public static int Compare(ReadOnlySpan<char> x, ReadOnlySpan<char> y, StringComp

if (char.IsDigit(xCh) && char.IsDigit(yCh))
{
x = GetNumber(x.Slice(i), out var xNum);
y = GetNumber(y.Slice(i), out var yNum);
var xOut = GetNumber(x.Slice(i), out var xNum);
var yOut = GetNumber(y.Slice(i), out var yNum);

if (xNum != yNum)
{
return xNum < yNum ? -1 : 1;
}

i = -1;
length = Math.Min(x.Length, y.Length);
continue;
length = Math.Min(xOut.Length, yOut.Length);
if (length == 0 && xOut.Length == yOut.Length && x.Length != y.Length)
{
return y.Length < x.Length ? -1 : 1; // "033" < "33" === true
}
else
{
x = xOut;
y = yOut;
continue;
}
}

if (xCh != yCh)
Expand All @@ -110,7 +119,7 @@ private static ReadOnlySpan<char> GetNumber(ReadOnlySpan<char> span, out int num
i++;
}

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
number = int.Parse(span[..i]);
return span[i..];
#else
Expand Down
2 changes: 1 addition & 1 deletion NaturalStringComparer/NaturalStringComparer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>

<Version>3.0.0</Version>
<Version>3.0.1</Version>
<RootNamespace>GihanSoft.String</RootNamespace>
<Authors>Muhammad Babayi</Authors>
<Company>GihanSoft</Company>
Expand Down
34 changes: 15 additions & 19 deletions NaturalStringComparerTest/UnitTestUnsafe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Test00()
{
var num1 = rnd.Next();
var num2 = rnd.Next();
var result = NaturalComparer.Ordinal.Compare($"text {num1}", $"text {num2}");
var result = NaturalComparer.Compare($"text {num1}", $"text {num2}", StringComparison.Ordinal);
Assert.Equal(num1.CompareTo(num2), result);
}

Expand All @@ -35,65 +35,63 @@ public void Test01()
{
var r1 = NaturalComparer.Compare("_qwerty", "0qwerty", StringComparison.InvariantCulture);
Assert.True(r1 < 0);
var r2 = NaturalComparer.Ordinal.Compare("[qwerty", "_qwerty");
var r2 = NaturalComparer.Compare("[qwerty", "_qwerty", StringComparison.Ordinal);
Assert.True(r2 < 0);
}

[Fact]
public void Test02()
{
var r = NaturalComparer.Ordinal.Compare("some text", "some text");
var r = NaturalComparer.Compare("some text", "some text", StringComparison.Ordinal);
Assert.True(r == 0);
}

[Fact]
public void Test03()
{
var r = NaturalComparer.Ordinal.Compare("number 1", "number 2");
var r = NaturalComparer.Compare("number 1", "number 2", StringComparison.Ordinal);
Assert.True(r < 0);
}

[Fact]
public void Test04()
{
var r = NaturalComparer.Ordinal.Compare("12 hi 2", "3 hi 11");
var r = NaturalComparer.Compare("12 hi 2", "3 hi 11", StringComparison.Ordinal);
Assert.True(r > 0);
}

[Fact]
public void Test05()
{
var r = NaturalComparer.Ordinal.Compare("text 1326 with", "text 999");
var r = NaturalComparer.Compare("text 1326 with", "text 999", StringComparison.Ordinal);
Assert.True(r > 0);
}

[Fact]
public void Test06()
{
var r = NaturalComparer.Ordinal.Compare("1278.jpg", "33.jpg");
var r = NaturalComparer.Compare("1278.jpg", "33.jpg", StringComparison.Ordinal);
Assert.True(r > 0);
}

[Fact]
public void Test07()
{
var r = NaturalComparer.Ordinal.Compare("compare 01278 and 3", "compare 1278 and 1000");
var r = NaturalComparer.Compare("compare 01278 and 3", "compare 1278 and 1000", StringComparison.Ordinal);
Assert.True(r < 0);
}



[Fact]
public void Test08()
{
var r = NaturalComparer.Ordinal.Compare("033", "33");
Assert.True(r > 0);
var r = NaturalComparer.Compare("033", "33", StringComparison.Ordinal);
Assert.True(r < 0);
}

[Fact]
public void Test08_2()
{
var r = NaturalComparer.Ordinal.Compare("33", "33");
var r = NaturalComparer.Compare("33", "33", StringComparison.Ordinal);
Assert.Equal(0, r);
}

Expand All @@ -102,14 +100,14 @@ public void Test09()
{
var num1 = 911651651;
var num2 = 911651615;
var r = NaturalComparer.Ordinal.Compare($"{num1}", $"{num2}");
var r = NaturalComparer.Compare($"{num1}", $"{num2}", StringComparison.Ordinal);
Assert.Equal(num1.CompareTo(num2), r);
}

[Fact]
public void Test10()
{
var r = NaturalComparer.Ordinal.Compare("hi2", "hi2hi");
var r = NaturalComparer.Compare("hi2", "hi2hi", StringComparison.Ordinal);
Assert.True(r < 0);
}

Expand All @@ -118,9 +116,7 @@ public void Test21()
{
for (var i = 0; i < n - 1; i++)
{
var result = NaturalComparer.Ordinal.Compare(
$"a long text to show better difference of compare methods {nums[i]}",
$"a long text to show better difference of compare methods {nums[i + 1]} hghf");
var result = NaturalComparer.Compare($"a long text to show better difference of compare methods {nums[i]}", $"a long text to show better difference of compare methods {nums[i + 1]} hghf", StringComparison.Ordinal);
Assert.Equal(nums[i].CompareTo(nums[i + 1]), result);
}
}
Expand All @@ -134,7 +130,7 @@ void TT()
, "number9", "number33", "number5", "number12"
};

stringList.Sort(NaturalComparer.Ordinal);
stringList.Sort(new NaturalComparer(StringComparison.Ordinal));
//or
stringList.NaturalSort(); // need "using Gihan.Helpers.Linq;"

Expand Down

0 comments on commit afb1a5c

Please sign in to comment.