Skip to content

Commit

Permalink
1.0.2 release
Browse files Browse the repository at this point in the history
Natural comparer handles decimals.
Removed Jetbrains Nuget dependency.
Updated packages.
  • Loading branch information
chrisg32 committed Jan 2, 2019
1 parent dddea64 commit 84dbd17
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 63 deletions.
8 changes: 4 additions & 4 deletions Commons.Test/Commons.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0">
<PackageReference Include="FluentAssertions" Version="5.5.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
2 changes: 0 additions & 2 deletions Commons.Test/Util/GenericParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Globalization;
using CG.Commons.Extensions;
using CG.Commons.Util;
using JetBrains.Annotations;
using Xunit;

namespace CG.Commons.Test.Util
Expand Down Expand Up @@ -493,7 +492,6 @@ public void TestParse_NullableGuid(string stringValue, Guid? expectedValue)

#region Utility Methods

[AssertionMethod]
private static void TestParse<T>(string stringValue, T expectedValue)
{
var result1 = GenericParser.Parse<T>(stringValue);
Expand Down
62 changes: 28 additions & 34 deletions Commons.Test/Util/NaturalComparerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ namespace CG.Commons.Test.Util
{
public class NaturalComparerTests
{
[Fact]
public void TestCanary()
{
Assert.True(true);
}

public enum ComparerEquality
{
LessThan = -1,
Expand All @@ -31,18 +25,31 @@ public enum ComparerEquality
//single number & character
[InlineData("4", "a", ComparerEquality.LessThan)]
[InlineData("a", "4", ComparerEquality.GreaterThan)]
//single number & single number
//single digit & single digit
[InlineData("4", "6", ComparerEquality.LessThan)]
[InlineData("7", "6", ComparerEquality.GreaterThan)]
[InlineData("5", "5", ComparerEquality.Equal)]
//double number & single number
//double digit & single digit
[InlineData("4", "16", ComparerEquality.LessThan)]
[InlineData("17", "6", ComparerEquality.GreaterThan)]
[InlineData("5", "05", ComparerEquality.Equal)]
//double number & double number
//double digit & double digit
[InlineData("14", "16", ComparerEquality.LessThan)]
[InlineData("17", "16", ComparerEquality.GreaterThan)]
[InlineData("15", "15", ComparerEquality.Equal)]
//decimal & decimal
[InlineData("1.4", "1.6", ComparerEquality.LessThan)]
[InlineData("1.7", "1.6", ComparerEquality.GreaterThan)]
[InlineData("1.5", "1.5", ComparerEquality.Equal)]
//decimal & trailing decimal
[InlineData("1.4", "1.60", ComparerEquality.LessThan)]
[InlineData("1.7", "1.60", ComparerEquality.GreaterThan)]
[InlineData("1.5", "1.50", ComparerEquality.Equal)]
//sandwiched decimal and decimal length
[InlineData("1.5", "1.5000", ComparerEquality.Equal)]
[InlineData("1.5", "1.5000", ComparerEquality.LessThan, NaturalComparerOptions.CheckTrailingDecimalLength)]
[InlineData("a1.5b", "a1.5000b", ComparerEquality.LessThan, NaturalComparerOptions.CheckTrailingDecimalLength)]
[InlineData("a1.5b", "a1.5000b", ComparerEquality.Equal)]
//ignore leading and trailing whitespace
[InlineData("aa", "a a", ComparerEquality.GreaterThan)]
[InlineData("aa", " aa", ComparerEquality.Equal)]
Expand All @@ -55,32 +62,19 @@ public enum ComparerEquality
[InlineData("12a 17b", "12a 19a", ComparerEquality.LessThan)]
[InlineData("12a 17b", "12a 17a", ComparerEquality.GreaterThan)]
[InlineData("12a 17a", "12a 17a", ComparerEquality.Equal)]
public void TestCompare(string left, string right, ComparerEquality expectedResult)
{
var comparer = new NaturalComparer();
DoTest(left, right, expectedResult, comparer);
}

[Theory]
[InlineData("a", "a", ComparerEquality.Equal)]
[InlineData("a", "A", ComparerEquality.Equal)]
public void TestCompareIgnoreCase(string left, string right, ComparerEquality expectedResult)
{
var comparer = new NaturalComparer(NaturalComparer.NaturalComparerOptions.IgnoreCase);
DoTest(left, right, expectedResult, comparer);
}


[Theory]
[InlineData("a a", "a a", ComparerEquality.Equal)]
[InlineData("a a", "aa", ComparerEquality.Equal)]
[InlineData("aa", "a a", ComparerEquality.Equal)]
[InlineData("aa", " aa", ComparerEquality.Equal)]
[InlineData("aa ", " aa", ComparerEquality.Equal)]
[InlineData("aa", " a\ta", ComparerEquality.Equal)]
public void TestCompareIgnoreWhitespace(string left, string right, ComparerEquality expectedResult)
//ignore case
[InlineData("a", "a", ComparerEquality.Equal, NaturalComparerOptions.IgnoreCase)]
[InlineData("a", "A", ComparerEquality.Equal, NaturalComparerOptions.IgnoreCase)]
//ignore whitespace
[InlineData("a a", "a a", ComparerEquality.Equal, NaturalComparerOptions.IgnoreWhiteSpace)]
[InlineData("a a", "aa", ComparerEquality.Equal, NaturalComparerOptions.IgnoreWhiteSpace)]
[InlineData("aa", "a a", ComparerEquality.Equal, NaturalComparerOptions.IgnoreWhiteSpace)]
[InlineData("aa", " aa", ComparerEquality.Equal, NaturalComparerOptions.IgnoreWhiteSpace)]
[InlineData("aa ", " aa", ComparerEquality.Equal, NaturalComparerOptions.IgnoreWhiteSpace)]
[InlineData("aa", " a\ta", ComparerEquality.Equal, NaturalComparerOptions.IgnoreWhiteSpace)]
public void TestCompare(string left, string right, ComparerEquality expectedResult, NaturalComparerOptions options = NaturalComparerOptions.None)
{
var comparer = new NaturalComparer(NaturalComparer.NaturalComparerOptions.IgnoreWhiteSpace);
var comparer = new NaturalComparer(options);
DoTest(left, right, expectedResult, comparer);
}

Expand Down
13 changes: 6 additions & 7 deletions Commons/Commons.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>A collection of useful C# utilities, extensions, and data structures.</Description>
<Authors>Chris Gonzales</Authors>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
<PackageTags>Commons C# DataStructures Dictionary Extensions</PackageTags>
<Copyright>Copyright 2017 Chris Gonzales</Copyright>
<Copyright>Copyright 2019 Chris Gonzales</Copyright>
<PackageIconUrl>https://chrisg32.github.io/assets/cg_logo.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/chrisg32/Commons</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/chrisg32/Commons/blob/master/LICENSE</PackageLicenseUrl>
Expand All @@ -17,12 +17,11 @@
<Product>CG Commons Library</Product>
<RepositoryType>Git</RepositoryType>
<RepositoryUrl>https://github.com/chrisg32/Commons</RepositoryUrl>
<PackageReleaseNotes>First release.</PackageReleaseNotes>
<PackageReleaseNotes>Natural comparer handles decimals.
Removed Jetbrains Nuget dependency.
Updated packages.</PackageReleaseNotes>
<AssemblyName>CG.Commons</AssemblyName>
<NeutralLanguage>en-US</NeutralLanguage>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2018.2.1" />
</ItemGroup>

</Project>
30 changes: 14 additions & 16 deletions Commons/Util/NaturalComparer.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using CG.Commons.Extensions;

namespace CG.Commons.Util
{
public class NaturalComparer : IComparer<string>
{
private readonly bool _ignoreCase;
private readonly bool _ignoreWhitespace;

[Flags]
public enum NaturalComparerOptions
{
None,
IgnoreCase,
IgnoreWhiteSpace
}
private readonly bool _checkTrailingDecimalLength;

public NaturalComparer(NaturalComparerOptions options = NaturalComparerOptions.None)
{
_ignoreCase = options.HasFlag(NaturalComparerOptions.IgnoreCase);
_ignoreWhitespace = options.HasFlag(NaturalComparerOptions.IgnoreWhiteSpace);
_checkTrailingDecimalLength = options.HasFlag(NaturalComparerOptions.CheckTrailingDecimalLength);
}

//less than zero = x is less than y
Expand Down Expand Up @@ -83,11 +77,15 @@ public int Compare(string left, string right)
//if both characters are numeric then we have to compare the full numeric string part
if (xIsNum && yIsNum)
{
var xints = GetNumericString(xarray, ref xindex);
var yints = GetNumericString(yarray, ref yindex);
var xint = int.Parse(xints);
var yint = int.Parse(yints);
var iresult = xint.CompareTo(yint);
var xnums = GetNumericString(xarray, ref xindex);
var ynums = GetNumericString(yarray, ref yindex);
var xnum = decimal.Parse(xnums);
var ynum = decimal.Parse(ynums);
var iresult = xnum.CompareTo(ynum);
if (_checkTrailingDecimalLength && iresult == 0 && xnums.Length != ynums.Length)
{
return xnums.Length.CompareTo(ynums.Length);
}
if (iresult != 0) return iresult;
continue;
}
Expand All @@ -106,7 +104,7 @@ public int Compare(string left, string right)
private string GetNumericString(IReadOnlyList<char> source, ref int index)
{
var sb = new StringBuilder();
while (index < source.Count && char.IsDigit(source[index]))
while (index < source.Count && (char.IsDigit(source[index]) || source[index] == '.'))
{
sb.Append(source[index]);
index++;
Expand Down
13 changes: 13 additions & 0 deletions Commons/Util/NaturalComparerOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace CG.Commons.Util
{
[Flags]
public enum NaturalComparerOptions
{
None,
IgnoreCase,
IgnoreWhiteSpace,
CheckTrailingDecimalLength
}
}

0 comments on commit 84dbd17

Please sign in to comment.