From 9482cc8a08223a11cd56d3a6d565229ec42c0649 Mon Sep 17 00:00:00 2001 From: James A Sutherland Date: Tue, 16 Jul 2024 10:01:21 -0500 Subject: [PATCH 1/2] Throw exceptions on invalid conversion attempts --- TypeGuesser/Deciders/BoolTypeDecider.cs | 3 ++- TypeGuesser/Deciders/DecimalTypeDecider.cs | 2 +- TypeGuesser/Deciders/IntTypeDecider.cs | 2 +- TypeGuesser/Guesser.cs | 4 ++-- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/TypeGuesser/Deciders/BoolTypeDecider.cs b/TypeGuesser/Deciders/BoolTypeDecider.cs index 024c45d..f2cc372 100644 --- a/TypeGuesser/Deciders/BoolTypeDecider.cs +++ b/TypeGuesser/Deciders/BoolTypeDecider.cs @@ -25,7 +25,7 @@ protected override IDecideTypesForStrings CloneImpl(CultureInfo newCulture) candidateString = StripWhitespace(candidateString); - return candidateString.Length switch + bool? b = candidateString.Length switch { 1 => "1tTyYjJ0fFnN".IndexOf(candidateString[0]) != -1 ? "0fFnN".IndexOf(candidateString[0]) == -1 : null, 2 => candidateString.Equals("ja",StringComparison.OrdinalIgnoreCase) ? true : @@ -41,6 +41,7 @@ protected override IDecideTypesForStrings CloneImpl(CultureInfo newCulture) 5 => candidateString.Equals("false",StringComparison.OrdinalIgnoreCase) ? false : null, _ => null }; + return b ?? throw new Exception("Invalid bool"); } private static ReadOnlySpan StripWhitespace(ReadOnlySpan candidateString) diff --git a/TypeGuesser/Deciders/DecimalTypeDecider.cs b/TypeGuesser/Deciders/DecimalTypeDecider.cs index e9d2bed..4f495f9 100644 --- a/TypeGuesser/Deciders/DecimalTypeDecider.cs +++ b/TypeGuesser/Deciders/DecimalTypeDecider.cs @@ -31,7 +31,7 @@ protected override IDecideTypesForStrings CloneImpl(CultureInfo culture) } /// - protected override object? ParseImpl(ReadOnlySpan value) => decimal.TryParse(value, NumberStyles.Any, Culture.NumberFormat, out var d) ? d : null; + protected override object ParseImpl(ReadOnlySpan value) => decimal.Parse(value, NumberStyles.Any, Culture.NumberFormat); /// protected override bool IsAcceptableAsTypeImpl(ReadOnlySpan candidateString, IDataTypeSize? sizeRecord) diff --git a/TypeGuesser/Deciders/IntTypeDecider.cs b/TypeGuesser/Deciders/IntTypeDecider.cs index a6be2fb..de3b345 100644 --- a/TypeGuesser/Deciders/IntTypeDecider.cs +++ b/TypeGuesser/Deciders/IntTypeDecider.cs @@ -19,7 +19,7 @@ protected override IDecideTypesForStrings CloneImpl(CultureInfo newCulture) } /// - protected override object? ParseImpl(ReadOnlySpan value) => int.TryParse(value, NumberStyles.Any, Culture.NumberFormat, out var i) ? i : null; + protected override object ParseImpl(ReadOnlySpan value) => int.Parse(value, NumberStyles.Any, Culture.NumberFormat); /// protected override bool IsAcceptableAsTypeImpl(ReadOnlySpan candidateString, IDataTypeSize? sizeRecord) diff --git a/TypeGuesser/Guesser.cs b/TypeGuesser/Guesser.cs index 5f00abd..9aabaab 100644 --- a/TypeGuesser/Guesser.cs +++ b/TypeGuesser/Guesser.cs @@ -132,10 +132,10 @@ public void AdjustToCompensateForValue(object? o) SR.Guesser_AdjustToCompensateForValue_GuesserPassedMixedTypeValues,o,o.GetType(), Guess.CSharpType)); - var oToString = o.ToString(); + var oToString = o.ToString() ?? string.Empty; //we might need to fallback on a string later on, in this case we should always record the maximum length of input seen before even if it is acceptable as int, double, dates etc - Guess.Width = Math.Max(Guess.Width ?? -1,GetStringLength(oToString??string.Empty)); + Guess.Width = Math.Max(Guess.Width ?? -1, GetStringLength(oToString.AsSpan())); //if it's a string if (o is string oAsString) From 72d6ebad9076ff03dce262f5a1896c1c6dc4b330 Mon Sep 17 00:00:00 2001 From: James A Sutherland Date: Tue, 16 Jul 2024 10:02:26 -0500 Subject: [PATCH 2/2] Update for 1.2.6 release --- CHANGELOG.md | 7 ++++++- SharedAssemblyInfo.cs | 6 +++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5927f74..36b01e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.2.6] - 2024-07-16 + +- Throw exceptions on invalid conversion attempts + ## [1.2.5] - 2024-07-11 - Internal performance improvements, using ReadOnlySpan<char> instead of string internally @@ -84,7 +88,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Initial port of content from [FAnsiSql](https://github.com/HicServices/FAnsiSql) -[Unreleased]: https://github.com/HicServices/TypeGuesser/compare/v1.2.5...main +[Unreleased]: https://github.com/HicServices/TypeGuesser/compare/v1.2.6...main +[1.2.6]: https://github.com/HicServices/TypeGuesser/compare/v1.2.5...v1.2.6 [1.2.5]: https://github.com/HicServices/TypeGuesser/compare/v1.2.4...v1.2.5 [1.2.4]: https://github.com/HicServices/TypeGuesser/compare/v1.2.3...v1.2.4 [1.2.3]: https://github.com/HicServices/TypeGuesser/compare/v1.2.2...v1.2.3 diff --git a/SharedAssemblyInfo.cs b/SharedAssemblyInfo.cs index e869cfd..e7486c5 100644 --- a/SharedAssemblyInfo.cs +++ b/SharedAssemblyInfo.cs @@ -9,6 +9,6 @@ [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -[assembly: AssemblyVersion("1.2.5")] -[assembly: AssemblyFileVersion("1.2.5")] -[assembly: AssemblyInformationalVersion("1.2.5")] +[assembly: AssemblyVersion("1.2.6")] +[assembly: AssemblyFileVersion("1.2.6")] +[assembly: AssemblyInformationalVersion("1.2.6")]