Skip to content

Commit

Permalink
Address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed Feb 12, 2020
1 parent c4940fd commit 664a9f9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/libraries/System.Private.CoreLib/src/System/Decimal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,15 @@ public static long ToOACurrency(decimal value)
// equally valid, and all are numerically equivalent.
//
public Decimal(int[] bits) :
this(bits != null ? bits.AsSpan() : throw new ArgumentNullException(nameof(bits)))
this((ReadOnlySpan<int>)(bits ?? throw new ArgumentNullException(nameof(bits))))
{
}

/// <summary>
/// Initializes a new instance of <see cref="decimal"/> to a decimal value represented in binary and contained in the specified span.
/// </summary>
/// <param name="bits">A span of four <see cref="int"/>s containing a binary representation of a decimal value.</param>
/// <exception cref="ArgumentException">The length of <paramref name="bits"/> is not 4, or the representation of the decimal value in <paramref name="bits"/> is not valid.</exception>
public Decimal(ReadOnlySpan<int> bits)
{
if (bits.Length == 4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ internal static class DecimalUtilities
{
public static int GetScale(this decimal value)
{
#if NETCOREAPP
Span<int> bits = stackalloc int[4];
decimal.GetBits(value, bits);
return unchecked((byte)(bits[3] >> 16));
#else
return unchecked((byte)(decimal.GetBits(value)[3] >> 16));
#endif
}

public static void GetBits(this decimal value, out bool isNegative, out byte scale, out uint low, out uint mid, out uint high)
Expand Down
6 changes: 4 additions & 2 deletions src/libraries/System.Runtime/tests/System/DecimalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -657,20 +657,22 @@ public static void TryGetBits(decimal input, int[] expected)
Span<int> bits;
int valuesWritten;

bits = new int[3];
bits = new int[3] { 42, 43, 44 };
Assert.False(decimal.TryGetBits(input, bits, out valuesWritten));
Assert.Equal(0, valuesWritten);
Assert.Equal(new int[3] { 42, 43, 44 }, bits.ToArray());

bits = new int[4];
Assert.True(decimal.TryGetBits(input, bits, out valuesWritten));
Assert.Equal(4, valuesWritten);
Assert.Equal(expected, bits.ToArray());

bits = new int[5];
bits[4] = 42;
Assert.True(decimal.TryGetBits(input, bits, out valuesWritten));
Assert.Equal(4, valuesWritten);
Assert.Equal(expected, bits.Slice(0, 4).ToArray());
Assert.Equal(0, bits[4]);
Assert.Equal(42, bits[4]);
}

[Fact]
Expand Down

0 comments on commit 664a9f9

Please sign in to comment.