Skip to content

Commit 5af39fb

Browse files
author
Stefán J. Sigurðarson
committed
Improving throw helper.
1 parent 0f515b6 commit 5af39fb

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

projects/RabbitMQ.Client/client/impl/WireFormatting.cs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,50 +40,46 @@
4040

4141
namespace RabbitMQ.Client.Impl
4242
{
43-
// * DESCRIPTION TAKEN FROM MS REFERENCE SOURCE *
44-
// https://github.com/microsoft/referencesource/blob/master/mscorlib/system/decimal.cs
45-
// The lo, mid, hi, and flags fields contain the representation of the
46-
// Decimal value. The lo, mid, and hi fields contain the 96-bit integer
47-
// part of the Decimal. Bits 0-15 (the lower word) of the flags field are
48-
// unused and must be zero; bits 16-23 contain must contain a value between
49-
// 0 and 28, indicating the power of 10 to divide the 96-bit integer part
50-
// by to produce the Decimal value; bits 24-30 are unused and must be zero;
51-
// and finally bit 31 indicates the sign of the Decimal value, 0 meaning
52-
// positive and 1 meaning negative.
53-
readonly struct DecimalData
43+
internal static class WireFormatting
5444
{
55-
public readonly uint Flags;
56-
public readonly uint Hi;
57-
public readonly uint Lo;
58-
public readonly uint Mid;
59-
60-
internal DecimalData(uint flags, uint hi, uint lo, uint mid)
45+
// * DESCRIPTION TAKEN FROM MS REFERENCE SOURCE *
46+
// https://github.com/microsoft/referencesource/blob/master/mscorlib/system/decimal.cs
47+
// The lo, mid, hi, and flags fields contain the representation of the
48+
// Decimal value. The lo, mid, and hi fields contain the 96-bit integer
49+
// part of the Decimal. Bits 0-15 (the lower word) of the flags field are
50+
// unused and must be zero; bits 16-23 contain must contain a value between
51+
// 0 and 28, indicating the power of 10 to divide the 96-bit integer part
52+
// by to produce the Decimal value; bits 24-30 are unused and must be zero;
53+
// and finally bit 31 indicates the sign of the Decimal value, 0 meaning
54+
// positive and 1 meaning negative.
55+
readonly struct DecimalData
6156
{
62-
Flags = flags;
63-
Hi = hi;
64-
Lo = lo;
65-
Mid = mid;
57+
public readonly uint Flags;
58+
public readonly uint Hi;
59+
public readonly uint Lo;
60+
public readonly uint Mid;
61+
62+
internal DecimalData(uint flags, uint hi, uint lo, uint mid)
63+
{
64+
Flags = flags;
65+
Hi = hi;
66+
Lo = lo;
67+
Mid = mid;
68+
}
6669
}
67-
}
6870

69-
internal static class WireFormatting
70-
{
7171
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7272
public static decimal ReadDecimal(ReadOnlySpan<byte> span)
7373
{
7474
byte scale = span[0];
75-
ValidateDecimalScale(scale);
76-
uint unsignedMantissa = NetworkOrderDeserializer.ReadUInt32(span.Slice(1));
77-
var data = new DecimalData(((uint)(scale << 16)) | unsignedMantissa & 0x80000000, 0, unsignedMantissa & 0x7FFFFFFF, 0);
78-
return Unsafe.As<DecimalData, decimal>(ref data);
79-
}
80-
81-
private static void ValidateDecimalScale(byte scale)
82-
{
8375
if (scale > 28)
8476
{
85-
throw new SyntaxErrorException($"Unrepresentable AMQP decimal table field: scale={scale}");
77+
ThrowInvalidDecimalScale(scale);
8678
}
79+
80+
uint unsignedMantissa = NetworkOrderDeserializer.ReadUInt32(span.Slice(1));
81+
var data = new DecimalData(((uint)(scale << 16)) | unsignedMantissa & 0x80000000, 0, unsignedMantissa & 0x7FFFFFFF, 0);
82+
return Unsafe.As<DecimalData, decimal>(ref data);
8783
}
8884

8985
public static IList ReadArray(ReadOnlySpan<byte> span, out int bytesRead)
@@ -880,5 +876,10 @@ private static int ThrowWireFormattingException(decimal value)
880876
{
881877
throw new WireFormattingException("Decimal overflow in AMQP encoding", value);
882878
}
879+
880+
private static decimal ThrowInvalidDecimalScale(int scale)
881+
{
882+
throw new SyntaxErrorException($"Unrepresentable AMQP decimal table field: scale={scale}");
883+
}
883884
}
884885
}

0 commit comments

Comments
 (0)