Skip to content

Commit

Permalink
Unify parsing part of BigInteger with CoreLib (dotnet#85978)
Browse files Browse the repository at this point in the history
  • Loading branch information
huoyaoyuan authored Nov 29, 2023
1 parent 563664e commit f66c1c1
Show file tree
Hide file tree
Showing 10 changed files with 584 additions and 594 deletions.
378 changes: 378 additions & 0 deletions src/libraries/Common/src/System/Number.Parsing.Common.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,6 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Number.Dragon4.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Number.Formatting.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Number.Grisu3.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Number.NumberBuffer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Number.NumberToFloatingPointBits.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Number.Parsing.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\BitOperations.cs" />
Expand Down Expand Up @@ -1420,6 +1419,12 @@
<Compile Include="$(CommonPath)System\NotImplemented.cs">
<Link>Common\System\NotImplemented.cs</Link>
</Compile>
<Compile Include="$(CommonPath)System\Number.NumberBuffer.cs">
<Link>System\Number.NumberBuffer.cs</Link>
</Compile>
<Compile Include="$(CommonPath)System\Number.Parsing.Common.cs">
<Link>System\Number.Parsing.Common.cs</Link>
</Compile>
<Compile Include="$(CommonPath)System\Numerics\Crc32ReflectedTable.cs">
<Link>Common\System\Numerics\Crc32ReflectedTable.cs</Link>
</Compile>
Expand Down
11 changes: 0 additions & 11 deletions src/libraries/System.Private.CoreLib/src/System/Number.Parsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1361,17 +1361,6 @@ private static bool TrailingZeros<TChar>(ReadOnlySpan<TChar> value, int index)
return null;
}

private static bool IsWhite(uint ch) => (ch == 0x20) || ((ch - 0x09) <= (0x0D - 0x09));

private static bool IsDigit(uint ch) => (ch - '0') <= 9;

internal enum ParsingStatus
{
OK,
Failed,
Overflow
}

[DoesNotReturn]
internal static void ThrowOverflowOrFormatException<TChar, TInteger>(ParsingStatus status, ReadOnlySpan<TChar> value)
where TChar : unmanaged, IUtfChar<TChar>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,24 @@
<Compile Include="System\Numerics\BigIntegerCalculator.SquMul.cs" />
<Compile Include="System\Numerics\BigIntegerCalculator.Utils.cs" />
<Compile Include="System\Numerics\BigInteger.cs" />
<Compile Include="System\Numerics\BigNumber.cs" />
<Compile Include="System\Number.BigInteger.cs" />
<Compile Include="System\Numerics\NumericsHelpers.cs" />
<Compile Include="System\Numerics\Complex.cs" />
<Compile Include="System\Globalization\FormatProvider.BigInteger.cs" />
<Compile Include="System\Globalization\FormatProvider.Number.cs" />
<Compile Include="System\Globalization\FormatProvider.NumberBuffer.cs" />
<Compile Include="Properties\InternalsVisibleTo.cs" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(CommonPath)System\Globalization\FormatProvider.Number.cs"
Link="System\Globalization\FormatProvider.Number.cs" />
<Compile Include="$(CommonPath)System\Text\ValueStringBuilder.cs"
Link="CoreLib\System\Text\ValueStringBuilder.cs" />
<Compile Include="$(CommonPath)System\HexConverter.cs"
Link="Common\System\HexConverter.cs" />
<Compile Include="$(CommonPath)System\Number.NumberBuffer.cs"
Link="Common\System\Number.NumberBuffer.cs" />
<Compile Include="$(CommonPath)System\Number.Parsing.Common.cs"
Link="Common\System\Number.Parsing.Common.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Security;
using System.Text;

namespace System.Globalization
Expand Down Expand Up @@ -32,43 +31,5 @@ internal static void FormatBigInteger(ref ValueStringBuilder sb, int precision,
}
}
}

internal static bool TryStringToBigInteger(
ReadOnlySpan<char> s,
NumberStyles styles,
NumberFormatInfo numberFormatInfo,
StringBuilder receiver, // Receives the decimal digits
out int precision,
out int scale,
out bool sign
)
{
FormatProvider.Number.NumberBuffer numberBuffer = default;

unsafe
{
// Note: because we passed a non-null StringBuilder (receiver) to TryStringToNumber, it streams the digits into
// that instead of numberBuffer.digits. This is quite important since numberBuffer.digits is a fixed-buffer size
// and BigNumbers can have an arbitrary number of digits.
//
// Just in case a bug is ever introduced into TryStringToNumber that violates this, set the pointer that numberBuffer.digits returns
// to something that will AV.
numberBuffer.overrideDigits = (char*)0x1;
}
if (!Number.TryStringToNumber(s, styles, ref numberBuffer, receiver, numberFormatInfo, parseDecimal: false))
{
precision = default(int);
scale = default(int);
sign = default(bool);
return false;
}
else
{
precision = numberBuffer.precision;
scale = numberBuffer.scale;
sign = numberBuffer.sign;
return true;
}
}
}
}
Loading

0 comments on commit f66c1c1

Please sign in to comment.