Skip to content

Commit 7691aa8

Browse files
Use correct NumberStyle when parsing double and float (#59205)
* User correct NumberStyle when parsing double and float IParseable<float> and IParseable<double> would fail when parsing a decimal point number due to incorrect NumberStyles parameter. E.g. IParseable<double>.TryParse would fail as opposed to IParseable<double>.Parse because the former explicitly states to use NumberStyles.Integer * Adding tests covering parse for float/double/half * Delete MSBuild_pid-37116_6904092b1c9a481da960c9e28eba2a27.failure.txt Co-authored-by: Tanner Gooding <tagoo@outlook.com>
1 parent d498440 commit 7691aa8

File tree

9 files changed

+429
-48
lines changed

9 files changed

+429
-48
lines changed

src/libraries/System.Private.CoreLib/src/System/Double.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ static double IParseable<double>.Parse(string s, IFormatProvider? provider)
12421242

12431243
[RequiresPreviewFeatures]
12441244
static bool IParseable<double>.TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, out double result)
1245-
=> TryParse(s, NumberStyles.Integer, provider, out result);
1245+
=> TryParse(s, NumberStyles.Float | NumberStyles.AllowThousands, provider, out result);
12461246

12471247
//
12481248
// ISignedNumber
@@ -1257,11 +1257,11 @@ static bool IParseable<double>.TryParse([NotNullWhen(true)] string? s, IFormatPr
12571257

12581258
[RequiresPreviewFeatures]
12591259
static double ISpanParseable<double>.Parse(ReadOnlySpan<char> s, IFormatProvider? provider)
1260-
=> Parse(s, NumberStyles.Integer, provider);
1260+
=> Parse(s, NumberStyles.Float | NumberStyles.AllowThousands, provider);
12611261

12621262
[RequiresPreviewFeatures]
12631263
static bool ISpanParseable<double>.TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out double result)
1264-
=> TryParse(s, NumberStyles.Integer, provider, out result);
1264+
=> TryParse(s, NumberStyles.Float | NumberStyles.AllowThousands, provider, out result);
12651265

12661266
//
12671267
// ISubtractionOperators

src/libraries/System.Private.CoreLib/src/System/Half.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public static bool IsSubnormal(Half value)
241241
public static Half Parse(string s)
242242
{
243243
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
244-
return Number.ParseHalf(s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo);
244+
return Number.ParseHalf(s, DefaultParseStyle, NumberFormatInfo.CurrentInfo);
245245
}
246246

247247
/// <summary>
@@ -266,7 +266,7 @@ public static Half Parse(string s, NumberStyles style)
266266
public static Half Parse(string s, IFormatProvider? provider)
267267
{
268268
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
269-
return Number.ParseHalf(s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.GetInstance(provider));
269+
return Number.ParseHalf(s, DefaultParseStyle, NumberFormatInfo.GetInstance(provider));
270270
}
271271

272272
/// <summary>
@@ -1554,7 +1554,7 @@ static Half IParseable<Half>.Parse(string s, IFormatProvider? provider)
15541554

15551555
[RequiresPreviewFeatures]
15561556
static bool IParseable<Half>.TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, out Half result)
1557-
=> TryParse(s, NumberStyles.Integer, provider, out result);
1557+
=> TryParse(s, DefaultParseStyle, provider, out result);
15581558

15591559
//
15601560
// ISignedNumber
@@ -1569,11 +1569,11 @@ static bool IParseable<Half>.TryParse([NotNullWhen(true)] string? s, IFormatProv
15691569

15701570
[RequiresPreviewFeatures]
15711571
static Half ISpanParseable<Half>.Parse(ReadOnlySpan<char> s, IFormatProvider? provider)
1572-
=> Parse(s, NumberStyles.Integer, provider);
1572+
=> Parse(s, DefaultParseStyle, provider);
15731573

15741574
[RequiresPreviewFeatures]
15751575
static bool ISpanParseable<Half>.TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out Half result)
1576-
=> TryParse(s, NumberStyles.Integer, provider, out result);
1576+
=> TryParse(s, DefaultParseStyle, provider, out result);
15771577

15781578
//
15791579
// ISubtractionOperators

src/libraries/System.Private.CoreLib/src/System/Single.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ static float IParseable<float>.Parse(string s, IFormatProvider? provider)
12341234

12351235
[RequiresPreviewFeatures]
12361236
static bool IParseable<float>.TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, out float result)
1237-
=> TryParse(s, NumberStyles.Integer, provider, out result);
1237+
=> TryParse(s, NumberStyles.Float | NumberStyles.AllowThousands, provider, out result);
12381238

12391239
//
12401240
// ISignedNumber
@@ -1249,11 +1249,11 @@ static bool IParseable<float>.TryParse([NotNullWhen(true)] string? s, IFormatPro
12491249

12501250
[RequiresPreviewFeatures]
12511251
static float ISpanParseable<float>.Parse(ReadOnlySpan<char> s, IFormatProvider? provider)
1252-
=> Parse(s, NumberStyles.Integer, provider);
1252+
=> Parse(s, NumberStyles.Float | NumberStyles.AllowThousands, provider);
12531253

12541254
[RequiresPreviewFeatures]
12551255
static bool ISpanParseable<float>.TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out float result)
1256-
=> TryParse(s, NumberStyles.Integer, provider, out result);
1256+
=> TryParse(s, NumberStyles.Float | NumberStyles.AllowThousands, provider, out result);
12571257

12581258
//
12591259
// ISubtractionOperators

src/libraries/System.Runtime.Experimental/tests/System.Runtime.Experimental.Tests.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
<ItemGroup>
1717
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\ByteTests.cs" />
1818
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\CharTests.cs" />
19+
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\DoubleTests.cs" />
20+
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\HalfTests.cs" />
1921
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\Int16Tests.cs" />
2022
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\Int32Tests.cs" />
2123
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\Int64Tests.cs" />
2224
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\IntPtrTests.cs" />
2325
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\SByteTests.cs" />
26+
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\SingleTests.cs" />
2427
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\UInt16Tests.cs" />
2528
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\UInt32Tests.cs" />
2629
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\UInt64Tests.cs" />
@@ -29,18 +32,22 @@
2932
<ItemGroup Condition="'$(FeatureGenericMath)' == 'true'">
3033
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\ByteTests.GenericMath.cs" />
3134
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\CharTests.GenericMath.cs" />
35+
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\DoubleTests.GenericMath.cs" />
3236
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\GenericMathHelpers.cs" />
37+
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\HalfTests.GenericMath.cs" />
3338
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\Int16Tests.GenericMath.cs" />
3439
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\Int32Tests.GenericMath.cs" />
3540
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\Int64Tests.GenericMath.cs" />
3641
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\IntPtrTests.GenericMath.cs" />
3742
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\SByteTests.GenericMath.cs" />
43+
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\SingleTests.GenericMath.cs" />
3844
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\UInt16Tests.GenericMath.cs" />
3945
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\UInt32Tests.GenericMath.cs" />
4046
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\UInt64Tests.GenericMath.cs" />
4147
<Compile Include="$(LibrariesProjectRoot)System.Runtime\tests\System\UIntPtrTests.GenericMath.cs" />
4248
</ItemGroup>
4349
<ItemGroup>
50+
<PackageReference Include="System.Runtime.Numerics.TestData" Version="$(SystemRuntimeNumericsTestDataVersion)" GeneratePathProperty="true" />
4451
<!-- it's a reference assembly, but the project system doesn't know that - include it during compilation, but don't publish it -->
4552
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Experimental\ref\System.Runtime.Experimental.csproj" IncludeAssets="compile" Private="false" />
4653
<ProjectReference Include="$(CommonTestPath)TestUtilities.Unicode\TestUtilities.Unicode.csproj" />

src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,19 @@
1414
<DefineConstants Condition="'$(FeatureGenericMath)' == 'true'">$(DefineConstants);FEATURE_GENERIC_MATH</DefineConstants>
1515
</PropertyGroup>
1616
<ItemGroup>
17-
<Compile Include="$(CommonTestPath)System\EnumTypes.cs"
18-
Link="Common\System\EnumTypes.cs" />
19-
<Compile Include="$(CommonTestPath)System\MockType.cs"
20-
Link="Common\System\MockType.cs" />
21-
<Compile Include="$(CommonTestPath)System\Collections\CollectionAsserts.cs"
22-
Link="Common\System\Collections\CollectionAsserts.cs" />
23-
<Compile Include="$(CommonTestPath)System\Collections\ICollection.Generic.Tests.cs"
24-
Link="Common\System\Collections\ICollection.Generic.Tests.cs" />
25-
<Compile Include="$(CommonTestPath)System\Collections\IEnumerable.Generic.Tests.cs"
26-
Link="Common\System\Collections\IEnumerable.Generic.Tests.cs" />
27-
<Compile Include="$(CommonTestPath)System\Collections\IList.Generic.Tests.cs"
28-
Link="Common\System\Collections\IList.Generic.Tests.cs" />
29-
<Compile Include="$(CommonTestPath)System\Collections\TestBase.Generic.cs"
30-
Link="Common\System\Collections\TestBase.Generic.cs" />
31-
<Compile Include="$(CommonTestPath)System\Collections\TestBase.NonGeneric.cs"
32-
Link="Common\System\Collections\TestBase.NonGeneric.cs" />
33-
<Compile Include="$(CommonTestPath)Tests\System\StringTests.cs"
34-
Link="Common\System\StringTests.cs" />
35-
<Compile Include="$(CommonTestPath)System\Collections\IDictionary.NonGeneric.Tests.cs"
36-
Link="Common\System\Collections\IDictionary.NonGeneric.Tests.cs" />
37-
<Compile Include="$(CommonTestPath)System\Collections\IList.NonGeneric.Tests.cs"
38-
Link="Common\System\Collections\IList.NonGeneric.Tests.cs" />
39-
<Compile Include="$(CommonTestPath)System\Collections\ICollection.NonGeneric.Tests.cs"
40-
Link="Common\System\Collections\ICollection.NonGeneric.Tests.cs" />
41-
<Compile Include="$(CommonTestPath)System\Collections\IEnumerable.NonGeneric.Tests.cs"
42-
Link="Common\System\Collections\IEnumerable.NonGeneric.Tests.cs" />
17+
<Compile Include="$(CommonTestPath)System\EnumTypes.cs" Link="Common\System\EnumTypes.cs" />
18+
<Compile Include="$(CommonTestPath)System\MockType.cs" Link="Common\System\MockType.cs" />
19+
<Compile Include="$(CommonTestPath)System\Collections\CollectionAsserts.cs" Link="Common\System\Collections\CollectionAsserts.cs" />
20+
<Compile Include="$(CommonTestPath)System\Collections\ICollection.Generic.Tests.cs" Link="Common\System\Collections\ICollection.Generic.Tests.cs" />
21+
<Compile Include="$(CommonTestPath)System\Collections\IEnumerable.Generic.Tests.cs" Link="Common\System\Collections\IEnumerable.Generic.Tests.cs" />
22+
<Compile Include="$(CommonTestPath)System\Collections\IList.Generic.Tests.cs" Link="Common\System\Collections\IList.Generic.Tests.cs" />
23+
<Compile Include="$(CommonTestPath)System\Collections\TestBase.Generic.cs" Link="Common\System\Collections\TestBase.Generic.cs" />
24+
<Compile Include="$(CommonTestPath)System\Collections\TestBase.NonGeneric.cs" Link="Common\System\Collections\TestBase.NonGeneric.cs" />
25+
<Compile Include="$(CommonTestPath)Tests\System\StringTests.cs" Link="Common\System\StringTests.cs" />
26+
<Compile Include="$(CommonTestPath)System\Collections\IDictionary.NonGeneric.Tests.cs" Link="Common\System\Collections\IDictionary.NonGeneric.Tests.cs" />
27+
<Compile Include="$(CommonTestPath)System\Collections\IList.NonGeneric.Tests.cs" Link="Common\System\Collections\IList.NonGeneric.Tests.cs" />
28+
<Compile Include="$(CommonTestPath)System\Collections\ICollection.NonGeneric.Tests.cs" Link="Common\System\Collections\ICollection.NonGeneric.Tests.cs" />
29+
<Compile Include="$(CommonTestPath)System\Collections\IEnumerable.NonGeneric.Tests.cs" Link="Common\System\Collections\IEnumerable.NonGeneric.Tests.cs" />
4330
<Compile Include="Helpers.cs" />
4431
<Compile Include="Microsoft\Win32\SafeHandles\CriticalHandleZeroOrMinusOneIsInvalid.cs" />
4532
<Compile Include="Microsoft\Win32\SafeHandles\SafeHandleZeroOrMinusOneIsInvalid.cs" />
@@ -248,21 +235,23 @@
248235
<Compile Include="System\Type\TypePropertyTests.cs" />
249236
<Compile Include="System\Type\TypeTests.cs" />
250237
<Compile Include="System\Type\TypeTests.Get.cs" />
251-
<Compile Include="$(CommonTestPath)System\RandomDataGenerator.cs"
252-
Link="Common\System\RandomDataGenerator.cs" />
238+
<Compile Include="$(CommonTestPath)System\RandomDataGenerator.cs" Link="Common\System\RandomDataGenerator.cs" />
253239
</ItemGroup>
254240
<ItemGroup Condition="'$(TargetsUnix)' == 'true' or '$(TargetsBrowser)' == 'true'">
255241
<Compile Include="System\ExitCodeTests.Unix.cs" />
256242
</ItemGroup>
257243
<ItemGroup Condition="'$(FeatureGenericMath)' == 'true'">
258244
<Compile Include="System\ByteTests.GenericMath.cs" />
259245
<Compile Include="System\CharTests.GenericMath.cs" />
246+
<Compile Include="System\DoubleTests.GenericMath.cs" />
260247
<Compile Include="System\GenericMathHelpers.cs" />
248+
<Compile Include="System\HalfTests.GenericMath.cs" />
261249
<Compile Include="System\Int16Tests.GenericMath.cs" />
262250
<Compile Include="System\Int32Tests.GenericMath.cs" />
263251
<Compile Include="System\Int64Tests.GenericMath.cs" />
264252
<Compile Include="System\IntPtrTests.GenericMath.cs" />
265253
<Compile Include="System\SByteTests.GenericMath.cs" />
254+
<Compile Include="System\SingleTests.GenericMath.cs" />
266255
<Compile Include="System\UInt16Tests.GenericMath.cs" />
267256
<Compile Include="System\UInt32Tests.GenericMath.cs" />
268257
<Compile Include="System\UInt64Tests.GenericMath.cs" />
@@ -283,31 +272,27 @@
283272
<Compile Include="System\Text\Unicode\Utf8Tests.cs" />
284273
<Compile Include="System\Text\Unicode\Utf8UtilityTests.ValidateBytes.cs" />
285274
<Compile Include="System\ArgIteratorTests.cs" />
286-
<Compile Include="$(CommonPath)..\tests\System\RealFormatterTestsBase.cs"
287-
Link="System\RealFormatterTestsBase.cs" />
275+
<Compile Include="$(CommonPath)..\tests\System\RealFormatterTestsBase.cs" Link="System\RealFormatterTestsBase.cs" />
288276
<Compile Include="System\RealFormatterTests.cs" />
289-
<Compile Include="$(CommonPath)..\tests\System\RealParserTestsBase.cs"
290-
Link="System\RealParserTestsBase.cs" />
277+
<Compile Include="$(CommonPath)..\tests\System\RealParserTestsBase.cs" Link="System\RealParserTestsBase.cs" />
291278
<Compile Include="System\RealParserTests.cs" />
292279

293280
<TrimmerRootDescriptor Include="$(ILLinkDescriptorsPath)ILLink.Descriptors.Castle.xml" />
294281
<TrimmerRootDescriptor Include="$(MSBuildThisFileDirectory)ILLink.Descriptors.xml" />
295282
</ItemGroup>
296283
<ItemGroup>
297-
<Compile Include="$(CommonTestPath)System\Collections\IEnumerable.Generic.Serialization.Tests.cs"
298-
Link="Common\System\Collections\IEnumerable.Generic.Serialization.Tests.cs" />
284+
<Compile Include="$(CommonTestPath)System\Collections\IEnumerable.Generic.Serialization.Tests.cs" Link="Common\System\Collections\IEnumerable.Generic.Serialization.Tests.cs" />
299285
<EmbeddedResource Include="System\Reflection\EmbeddedImage.png">
300286
<LogicalName>System.Reflection.Tests.EmbeddedImage.png</LogicalName>
301287
</EmbeddedResource>
302288
<EmbeddedResource Include="System\Reflection\EmbeddedTextFile.txt">
303289
<LogicalName>System.Reflection.Tests.EmbeddedTextFile.txt</LogicalName>
304290
</EmbeddedResource>
305-
<Compile Include="$(CommonTestPath)System\Runtime\Serialization\Formatters\BinaryFormatterHelpers.cs"
306-
Link="Common\System\Runtime\Serialization\Formatters\BinaryFormatterHelpers.cs" />
291+
<Compile Include="$(CommonTestPath)System\Runtime\Serialization\Formatters\BinaryFormatterHelpers.cs" Link="Common\System\Runtime\Serialization\Formatters\BinaryFormatterHelpers.cs" />
307292
</ItemGroup>
308293
<ItemGroup>
309294
<PackageReference Include="Moq" Version="$(MoqVersion)" />
310-
<PackageReference Include="System.Runtime.Numerics.TestData" Version="$(SystemRuntimeNumericsTestDataVersion)" GeneratePathProperty="true"/>
295+
<PackageReference Include="System.Runtime.Numerics.TestData" Version="$(SystemRuntimeNumericsTestDataVersion)" GeneratePathProperty="true" />
311296
<ProjectReference Include="TestLoadAssembly\TestLoadAssembly.csproj" />
312297
<ProjectReference Include="TestCollectibleAssembly\TestCollectibleAssembly.csproj" />
313298
<ProjectReference Include="TestModule\System.Reflection.TestModule.ilproj" />

0 commit comments

Comments
 (0)