From 84dbd173b10c5c12b48391c263e4cf24fde45455 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 2 Jan 2019 16:22:06 -0600 Subject: [PATCH] 1.0.2 release Natural comparer handles decimals. Removed Jetbrains Nuget dependency. Updated packages. --- Commons.Test/Commons.Test.csproj | 8 +-- Commons.Test/Util/GenericParserTests.cs | 2 - Commons.Test/Util/NaturalComparerTests.cs | 62 ++++++++++------------- Commons/Commons.csproj | 13 +++-- Commons/Util/NaturalComparer.cs | 30 +++++------ Commons/Util/NaturalComparerOptions.cs | 13 +++++ 6 files changed, 65 insertions(+), 63 deletions(-) create mode 100644 Commons/Util/NaturalComparerOptions.cs diff --git a/Commons.Test/Commons.Test.csproj b/Commons.Test/Commons.Test.csproj index a97385f..d5598c8 100644 --- a/Commons.Test/Commons.Test.csproj +++ b/Commons.Test/Commons.Test.csproj @@ -11,10 +11,10 @@ - - - - + + + + all runtime; build; native; contentfiles; analyzers diff --git a/Commons.Test/Util/GenericParserTests.cs b/Commons.Test/Util/GenericParserTests.cs index 36856a8..49b4180 100644 --- a/Commons.Test/Util/GenericParserTests.cs +++ b/Commons.Test/Util/GenericParserTests.cs @@ -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 @@ -493,7 +492,6 @@ public void TestParse_NullableGuid(string stringValue, Guid? expectedValue) #region Utility Methods - [AssertionMethod] private static void TestParse(string stringValue, T expectedValue) { var result1 = GenericParser.Parse(stringValue); diff --git a/Commons.Test/Util/NaturalComparerTests.cs b/Commons.Test/Util/NaturalComparerTests.cs index ed3166e..90e5524 100644 --- a/Commons.Test/Util/NaturalComparerTests.cs +++ b/Commons.Test/Util/NaturalComparerTests.cs @@ -6,12 +6,6 @@ namespace CG.Commons.Test.Util { public class NaturalComparerTests { - [Fact] - public void TestCanary() - { - Assert.True(true); - } - public enum ComparerEquality { LessThan = -1, @@ -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)] @@ -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); } diff --git a/Commons/Commons.csproj b/Commons/Commons.csproj index 7ea0e70..e75d261 100644 --- a/Commons/Commons.csproj +++ b/Commons/Commons.csproj @@ -6,9 +6,9 @@ true A collection of useful C# utilities, extensions, and data structures. Chris Gonzales - 1.0.1 + 1.0.2 Commons C# DataStructures Dictionary Extensions - Copyright 2017 Chris Gonzales + Copyright 2019 Chris Gonzales https://chrisg32.github.io/assets/cg_logo.png https://github.com/chrisg32/Commons https://github.com/chrisg32/Commons/blob/master/LICENSE @@ -17,12 +17,11 @@ CG Commons Library Git https://github.com/chrisg32/Commons - First release. + Natural comparer handles decimals. +Removed Jetbrains Nuget dependency. +Updated packages. CG.Commons + en-US - - - - diff --git a/Commons/Util/NaturalComparer.cs b/Commons/Util/NaturalComparer.cs index d8f6db3..4cd2bed 100644 --- a/Commons/Util/NaturalComparer.cs +++ b/Commons/Util/NaturalComparer.cs @@ -1,7 +1,7 @@ -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 { @@ -9,19 +9,13 @@ public class NaturalComparer : IComparer { 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 @@ -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; } @@ -106,7 +104,7 @@ public int Compare(string left, string right) private string GetNumericString(IReadOnlyList 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++; diff --git a/Commons/Util/NaturalComparerOptions.cs b/Commons/Util/NaturalComparerOptions.cs new file mode 100644 index 0000000..b5f4bd8 --- /dev/null +++ b/Commons/Util/NaturalComparerOptions.cs @@ -0,0 +1,13 @@ +using System; + +namespace CG.Commons.Util +{ + [Flags] + public enum NaturalComparerOptions + { + None, + IgnoreCase, + IgnoreWhiteSpace, + CheckTrailingDecimalLength + } +} \ No newline at end of file