Skip to content

Commit

Permalink
Move BigNumber to Number.BigInteger
Browse files Browse the repository at this point in the history
  • Loading branch information
huoyaoyuan committed May 9, 2023
1 parent fe2f087 commit 8387024
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/libraries/Common/src/System/Number.Parsing.Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ private static bool TrailingZeros(ReadOnlySpan<char> value, int index) =>
// For compatibility, we need to allow trailing zeros at the end of a number string
value.Slice(index).IndexOfAnyExcept('\0') < 0;

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<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" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,13 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;

namespace System.Numerics
namespace System
{
internal static class BigNumber
internal static partial class Number
{
private const NumberStyles InvalidNumberStyles = ~(NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite
| NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingSign
Expand All @@ -289,13 +290,6 @@ internal static class BigNumber

private static ReadOnlySpan<uint> UInt32PowersOfTen => new uint[] { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };

internal enum ParsingStatus
{
OK,
Failed,
Overflow
}

[DoesNotReturn]
internal static void ThrowOverflowOrFormatException(ParsingStatus status) => throw GetException(status);

Expand Down Expand Up @@ -360,9 +354,9 @@ internal static unsafe ParsingStatus TryParseBigIntegerNumber(ReadOnlySpan<char>

fixed (byte* ptr = buffer) // NumberBuffer expects pinned span
{
Number.NumberBuffer number = new Number.NumberBuffer(Number.NumberBufferKind.Integer, buffer);
NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, buffer);

if (!Number.TryStringToNumber(value, style, ref number, info))
if (!TryStringToNumber(value, style, ref number, info))
{
result = default;
ret = ParsingStatus.Failed;
Expand Down Expand Up @@ -406,7 +400,7 @@ internal static ParsingStatus TryParseBigIntegerHexNumberStyle(ReadOnlySpan<char
{
for (whiteIndex = 0; whiteIndex < value.Length; whiteIndex++)
{
if (!Number.IsWhite(value[whiteIndex]))
if (!IsWhite(value[whiteIndex]))
break;
}

Expand All @@ -418,7 +412,7 @@ internal static ParsingStatus TryParseBigIntegerHexNumberStyle(ReadOnlySpan<char
{
for (whiteIndex = value.Length - 1; whiteIndex >= 0; whiteIndex--)
{
if (!Number.IsWhite(value[whiteIndex]))
if (!IsWhite(value[whiteIndex]))
break;
}

Expand Down Expand Up @@ -538,7 +532,7 @@ internal static ParsingStatus TryParseBigIntegerHexNumberStyle(ReadOnlySpan<char
// a divide-and-conquer algorithm with a running time of O(NlogN).
//
private static int s_naiveThreshold = 20000; // non-readonly for testing
private static ParsingStatus NumberToBigInteger(ref Number.NumberBuffer number, out BigInteger result)
private static ParsingStatus NumberToBigInteger(ref NumberBuffer number, out BigInteger result)
{
int currentBufferSize = 0;

Expand Down Expand Up @@ -581,7 +575,7 @@ private static ParsingStatus NumberToBigInteger(ref Number.NumberBuffer number,
}
}

ParsingStatus Naive(ref Number.NumberBuffer number, out BigInteger result)
ParsingStatus Naive(ref NumberBuffer number, out BigInteger result)
{
Span<uint> stackBuffer = stackalloc uint[BigIntegerCalculator.StackAllocThreshold];
Span<uint> currentBuffer = stackBuffer;
Expand Down Expand Up @@ -662,7 +656,7 @@ bool ProcessChunk(ReadOnlySpan<byte> chunkDigits, ref Span<uint> currentBuffer)
}
}

ParsingStatus DivideAndConquer(ref Number.NumberBuffer number, out BigInteger result)
ParsingStatus DivideAndConquer(ref NumberBuffer number, out BigInteger result)
{
Span<uint> currentBuffer;
int[]? arrayFromPoolForMultiplier = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ public static bool TryParse([NotNullWhen(true)] string? value, NumberStyles styl

public static BigInteger Parse(ReadOnlySpan<char> value, NumberStyles style = NumberStyles.Integer, IFormatProvider? provider = null)
{
return BigNumber.ParseBigInteger(value, style, NumberFormatInfo.GetInstance(provider));
return Number.ParseBigInteger(value, style, NumberFormatInfo.GetInstance(provider));
}

public static bool TryParse(ReadOnlySpan<char> value, out BigInteger result)
Expand All @@ -701,7 +701,7 @@ public static bool TryParse(ReadOnlySpan<char> value, out BigInteger result)

public static bool TryParse(ReadOnlySpan<char> value, NumberStyles style, IFormatProvider? provider, out BigInteger result)
{
return BigNumber.TryParseBigInteger(value, style, NumberFormatInfo.GetInstance(provider), out result) == BigNumber.ParsingStatus.OK;
return Number.TryParseBigInteger(value, style, NumberFormatInfo.GetInstance(provider), out result) == Number.ParsingStatus.OK;
}

public static int Compare(BigInteger left, BigInteger right)
Expand Down Expand Up @@ -1570,22 +1570,22 @@ private int WriteTo(Span<uint> buffer)

public override string ToString()
{
return BigNumber.FormatBigInteger(this, null, NumberFormatInfo.CurrentInfo);
return Number.FormatBigInteger(this, null, NumberFormatInfo.CurrentInfo);
}

public string ToString(IFormatProvider? provider)
{
return BigNumber.FormatBigInteger(this, null, NumberFormatInfo.GetInstance(provider));
return Number.FormatBigInteger(this, null, NumberFormatInfo.GetInstance(provider));
}

public string ToString([StringSyntax(StringSyntaxAttribute.NumericFormat)] string? format)
{
return BigNumber.FormatBigInteger(this, format, NumberFormatInfo.CurrentInfo);
return Number.FormatBigInteger(this, format, NumberFormatInfo.CurrentInfo);
}

public string ToString([StringSyntax(StringSyntaxAttribute.NumericFormat)] string? format, IFormatProvider? provider)
{
return BigNumber.FormatBigInteger(this, format, NumberFormatInfo.GetInstance(provider));
return Number.FormatBigInteger(this, format, NumberFormatInfo.GetInstance(provider));
}

private string DebuggerDisplay
Expand Down Expand Up @@ -1645,7 +1645,7 @@ private string DebuggerDisplay

public bool TryFormat(Span<char> destination, out int charsWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan<char> format = default, IFormatProvider? provider = null)
{
return BigNumber.TryFormatBigInteger(this, format, NumberFormatInfo.GetInstance(provider), destination, out charsWritten);
return Number.TryFormatBigInteger(this, format, NumberFormatInfo.GetInstance(provider), destination, out charsWritten);
}

private static BigInteger Add(ReadOnlySpan<uint> leftBits, int leftSign, ReadOnlySpan<uint> rightBits, int rightSign)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ public static void ToString_InvalidFormat_ThrowsFormatException()
Assert.Throws<FormatException>(() => b.ToString("E1000000000"));
Assert.Throws<FormatException>(() => b.ToString("E000001000000000"));

// Check ParseFormatSpecifier in BigNumber.cs with `G` format
// Check ParseFormatSpecifier in Number.BigInteger.cs with `G` format
Assert.Throws<FormatException>(() => b.ToString("G" + int.MaxValue.ToString()));
Assert.Throws<FormatException>(() => b.ToString("G" + intMaxPlus1String));
Assert.Throws<FormatException>(() => b.ToString("G4772185890"));
Expand All @@ -510,7 +510,7 @@ public static void ToString_ValidLargeFormat()
b.ToString("E999999999"); // Should not throw
b.ToString("E00000999999999"); // Should not throw

// Check ParseFormatSpecifier in BigNumber.cs with `G` format
// Check ParseFormatSpecifier in Number.BigInteger.cs with `G` format
b.ToString("G999999999"); // Should not throw
b.ToString("G00000999999999"); // Should not throw
}
Expand Down

0 comments on commit 8387024

Please sign in to comment.