Skip to content

Commit ebe4ed8

Browse files
committed
Fix parsing 'nan' on .NET Framework.
Utf8Parser is case-sensitive in .NET Framework. Run MySqlConnector.Tests unit tests on .NET Framework. Fix a difference between .NET Framework and .NET formatting of floats.
1 parent b7b3096 commit ebe4ed8

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/MySqlConnector/Core/TextRow.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,25 @@ protected override object GetValueCore(ReadOnlySpan<byte> data, ColumnDefinition
9898
case ColumnType.Float:
9999
if (Utf8Parser.TryParse(data, out float floatValue, out var floatBytesConsumed) && floatBytesConsumed == data.Length)
100100
return floatValue;
101-
ReadOnlySpan<byte> floatInfinity = new byte[] { 0x2D, 0x69, 0x6E, 0x66 }; // "-inf"
101+
ReadOnlySpan<byte> floatInfinity = "-inf"u8;
102102
if (data.SequenceEqual(floatInfinity))
103103
return float.NegativeInfinity;
104104
if (data.SequenceEqual(floatInfinity.Slice(1)))
105105
return float.PositiveInfinity;
106+
if (data.SequenceEqual("nan"u8))
107+
return float.NaN;
106108
throw new FormatException($"Couldn't parse value as float: {Encoding.UTF8.GetString(data)}");
107109

108110
case ColumnType.Double:
109111
if (Utf8Parser.TryParse(data, out double doubleValue, out var doubleBytesConsumed) && doubleBytesConsumed == data.Length)
110112
return doubleValue;
111-
ReadOnlySpan<byte> doubleInfinity = new byte[] { 0x2D, 0x69, 0x6E, 0x66 }; // "-inf"
113+
ReadOnlySpan<byte> doubleInfinity = "-inf"u8;
112114
if (data.SequenceEqual(doubleInfinity))
113115
return double.NegativeInfinity;
114116
if (data.SequenceEqual(doubleInfinity.Slice(1)))
115117
return double.PositiveInfinity;
118+
if (data.SequenceEqual("nan"u8))
119+
return double.NaN;
116120
throw new FormatException($"Couldn't parse value as double: {Encoding.UTF8.GetString(data)}");
117121

118122
case ColumnType.Decimal:

tests/MySqlConnector.Tests/MySqlConnector.Tests.csproj

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup Condition=" '$(Configuration)' != 'MySqlData' ">
4-
<TargetFrameworks>netcoreapp3.1;net7.0</TargetFrameworks>
4+
<TargetFrameworks>net481;net7.0</TargetFrameworks>
55
</PropertyGroup>
66

77
<PropertyGroup Condition=" '$(Configuration)' == 'MySqlData' ">
@@ -25,6 +25,10 @@
2525
<PackageReference Include="xunit" Version="2.4.2" />
2626
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
2727
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
28+
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3">
29+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
30+
<PrivateAssets>all</PrivateAssets>
31+
</PackageReference>
2832
</ItemGroup>
2933

3034
<ItemGroup Condition=" '$(Configuration)' != 'MySqlData' ">
@@ -36,9 +40,4 @@
3640
<Compile Remove="ByteBufferWriterTests.cs;CachedProcedureTests.cs;CancellationTests.cs;ConnectionTests.cs;FakeMySqlServer.cs;FakeMySqlServerConnection.cs;LoadBalancerTests.cs;MySqlDecimalTests.cs;MySqlExceptionTests.cs;MySqlParameterCollectionNameToIndexTests.cs;NormalizeTests.cs;ServerVersionTests.cs;StatementPreparerTests.cs;TypeMapperTests.cs;UtilityTests.cs" />
3741
</ItemGroup>
3842

39-
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
40-
<Reference Include="System" />
41-
<Reference Include="Microsoft.CSharp" />
42-
</ItemGroup>
43-
4443
</Project>

tests/MySqlConnector.Tests/StatementPreparerTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text;
66
using MySqlConnector.Core;
77
using MySqlConnector.Protocol.Serialization;
8+
using MySqlConnector.Utilities;
89
using Xunit;
910

1011
namespace MySqlConnector.Tests;
@@ -156,7 +157,11 @@ public void FormatParameter(object parameterValue, string replacedValue, bool no
156157
new object[] { 3_456_789_012u, "3456789012" },
157158
new object[] { -12_345_678_901L, "-12345678901" },
158159
new object[] { 12_345_678_901UL, "12345678901" },
160+
#if NET481
161+
new object[] { 1.0123456f, "1.01234555" },
162+
#else
159163
new object[] { 1.0123456f, "1.0123456" },
164+
#endif
160165
new object[] { 1.0123456789012346, "1.0123456789012346" },
161166
new object[] { 123456789.123456789m, "123456789.123456789" },
162167
new object[] { "1234", "'1234'" },

0 commit comments

Comments
 (0)